freetype을 사용하고 있으며 텍스트를 렌더링하기 위해 수행해야 할 유일한 작업은 ft_bitmap을 OpenGL로 렌더링 할 수있는 것으로 변환하는 것입니다. 나는 glfw를 사용하고있다. 내가 텍스트 렌더러를 초기화하는 데 사용하고 무엇렌더링 텍스트 - freetype 빈 화면
#include <exception>
#include <iostream>
#include <string>
#include <glew.h>
#include <GL/glfw.h>
#include <iterator>
#include "../include/TextRenderer.h"
#include <ft2build.h>
#include FT_FREETYPE_H
#include <stdexcept>
#include <freetype/ftglyph.h>
using std::runtime_error;
using std::cout;
TextRenderer::TextRenderer(int x, int y, FT_Face Face, std::string s)
{
FT_Set_Char_Size(
Face, /* handle to face object */
0, /* char_width in 1/64th of points */
16*64, /* char_height in 1/64th of points */
0, /* horizontal device resolution */
0); /* vertical device resolution */
slot= Face->glyph;
text = s;
setsx(x);
setsy(y);
penX = x;
penY = y;
face = Face;
//shaders
GLuint v = glCreateShader(GL_VERTEX_SHADER) ;
const char* vs = "void main(){ gl_Position = ftransform();}";
glShaderSource(v,1,&vs,NULL);
glCompileShader(v);
GLuint f = glCreateShader(GL_FRAGMENT_SHADER) ;
const char* fs = "uniform sampler2D texture1; void main() { gl_FragColor = texture2D(texture1, gl_TexCoord[0].st); //And that is all we need}";
glShaderSource(f,1,&fs,NULL);
glCompileShader(f);
Program= glCreateProgram();
glAttachShader(Program,v);
glAttachShader(Program,f);
glLinkProgram(Program);
}
void TextRenderer::render()
{
glUseProgram(Program);
FT_UInt glyph_index;
for (int n = 0; n < text.size(); n++)
{
/* retrieve glyph index from character code */
glyph_index = FT_Get_Char_Index(face, text[n]);
/* load glyph image into the slot (erase previous one) */
error = FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER);
draw(&face->glyph->bitmap,penX + slot->bitmap_left,penY - slot->bitmap_top);
penX += *(&face->glyph->bitmap.width)+3;
penY += slot->advance.y >> 6; /* not useful for now */
}
}
void TextRenderer::draw(FT_Bitmap * bitmap,float x,float y)
{
GLuint texture [0] ;
glGenTextures(1,texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RED , bitmap->width, bitmap->rows, 0, GL_RED , GL_UNSIGNED_BYTE, bitmap);
// int loc = glGetUniformLocation(Program, "texture1");
// glUniform1i(loc, 0);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glEnable(GL_TEXTURE_2D);
int height=bitmap->rows/10;
int width=bitmap->width/10;
glBegin(GL_QUADS);
glTexCoord2f (0.0, 0.0);
glVertex2f(x,y);
glTexCoord2f (1.0, 0.0);
glVertex2f(x+width,y);
glTexCoord2f (1.0, 1.0);
glVertex2f(x+width,y+height);
glTexCoord2f (0.0, 1.0);
glVertex2f(x,y+height);
glEnd();
glDisable(GL_TEXTURE_2D);
}
가 :
이FT_Library library;
FT_Face arial;
FT_Error error = FT_Init_FreeType(&library);
if (error)
{
throw std::runtime_error("Freetype failed");
}
error = FT_New_Face(library,
"C:/Windows/Fonts/Arial.ttf",
0,
&arial);
if (error == FT_Err_Unknown_File_Format)
{
throw std::runtime_error("font format not available");
}
else if (error)
{
throw std::runtime_error("Freetype font failed");
}
TextRenderer t(5,10,arial,"Hello");
t.render();
처음에는 초기화되지 않은 원시 포인터를 null이 아닌 배열 * 기본 포인터가 필요한 함수 ('glGenTextures()')에 전달합니다. 텍스쳐 배열에 적절한 양의 메모리를 할당하거나 자동 고정 배열을 전달하십시오 (귀하의 경우에는 하나만 얻으므로 후자가 될 가능성이 큽니다). – WhozCraig
기능이 적절하게 수정되었다는 잘못된 생각이 잘못되었습니다. 이제는 빈 화면이 나타납니다. – user2673108