OpenGL의 일부 (x, y) 좌표에서 문자열을 렌더링하는 함수를 작성하려고합니다. 이 응용 프로그램은 2D로만 렌더링 할 것이므로 매트릭스 모드와 직교 투영을 제 방식대로 초기화합니다. GLFW를 사용하여 창을 초기화하고 이벤트를 폴링합니다.
int render_loop()
{
int r;
GLFWwindow *window;
int width;
int height;
if(!glfwInit())
return -EINVAL;
window = glfwCreateWindow(S_WINDOW_W, S_WINDOW_H,
"My Window", NULL, NULL);
if(!window)
{
glfwTerminate();
return -EINVAL;
}
glfwMakeContextCurrent(window);
while(!glfwWindowShouldClose(window))
{
// Initialize the GL viewport.
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-0.5f, (width - 1) + 0.5f,
(height - 1) + 0.5f, -0.5f, 0.0f, 1.0f);
// Draw some text.
FT_Face font = get_font();
render_text("f", 5, 5, font);
// End GL rendering, swap the buffer, and poll for events.
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
텍스트를 렌더링하는 기능은 다음과 같습니다.
int render_text(const char *t, int x, int y, FT_Face f)
{
int ret = 0;
GLuint *textures;
size_t i;
size_t length = strlen(t);
FT_GlyphSlot g = f->glyph;
// Allocate a texture for each character.
textures = (GLuint *) malloc(sizeof(GLuint) * length);
if(textures == NULL)
{
ret = -ENOMEM;
goto done;
}
glGenTextures(length, textures);
// FreeType glyphs are 1-byte greyscale, so we can't use alignment.
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Render each character...
for(i = 0; i < length; ++i)
{
// Render this particular character using FreeType.
if(FT_Load_Char(f, t[i], FT_LOAD_RENDER))
continue;
// Make the current glyph's texture active.
glActiveTexture(textures[i]);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textures[i]);
/*
* To prevent artifacts when a character is not rendered exactly on
* pixel boundaries, clamp the texture to edges, and enable linear
* interpolation.
*/
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
// Load the texture.
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, g->bitmap.width,
g->bitmap.rows, 0, GL_ALPHA, GL_UNSIGNED_BYTE,
g->bitmap.buffer);
// Render the texture on the screen.
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2i(0, 0);
glVertex2i(x + 0, y + 0);
glTexCoord2i(g->bitmap.width, 0);
glVertex2i(x + g->bitmap.width, y + 0);
glTexCoord2i(0, g->bitmap.rows);
glVertex2i(x + 0, y + g->bitmap.rows);
glTexCoord2i(g->bitmap.width, 0);
glVertex2i(x + g->bitmap.width, y + 0);
glTexCoord2i(g->bitmap.width, g->bitmap.rows);
glVertex2i(x + g->bitmap.width, y + g->bitmap.rows);
glTexCoord2i(0, g->bitmap.rows);
glVertex2i(x + 0, y + g->bitmap.rows);
glEnd();
}
// Free our textures and we're done.
glDeleteTextures(length, textures);
free(textures);
done:
return ret;
}
그 결과 문자 대신 (5, 5)에 흰색 사각형이 생깁니다. 문제는 내가 전화를 걸 때 OpenGL이 알파 데이터를 잘못 해석한다는 것입니다 glTexImage2D
. 그러나 제대로 설정하는 방법을 잘 모르겠습니다.
텍스처 오브젝트는 텍스처 맵 및 관련 텍스처 매개 변수 상태를 저장합니다. glBindTexture ()에 대한 단일 호출로 텍스처간에 전환 할 수 있습니다.
...
다음 함수는 텍스처 객체에 영향을 미치고 상태를 저장합니다 : glTexImage * (), glTexSubImage * (), glCopyTexImage * (), glCopyTexSubImage * (), glTexParameter * () 및 glPrioritizeTextures (). 밉맵 피라미드를 빌드하기위한 GLU 루틴은 궁극적으로 glTexImage * ()를 호출하기 때문에 텍스처 객체 상태에도 영향을줍니다. 이 목록에는 glTexEnv * () 및 glTexGen * (); 텍스쳐 객체에 상태를 저장하지 않습니다.
따라서 GL_TEXTURE_MIN_FILTER
/ GL_TEXTURE_MAG_FILTER
상태는 텍스처 객체와 함께 저장됩니다.
실제 텍스처 오브젝트를 바인딩 하기 전에 설정 하므로 텍스처 오브젝트를 엉망으로 만드는 것 0
입니다. 별로 도움이되지 않습니다.
모든 새로 생성 된 텍스처가 기본이됩니다 GL_NEAREST_MIPMAP_LINEAR
에 대한 설정을 GL_TEXTURE_MIN_FILTER
. 그러나 밉맵을 제공하지 않습니다. 결과적으로 흰색의 불완전한 질감 이 나타납니다.
해결 방법 : glTexParameter()
통화를 이후로 이동하십시오 glBindTexture()
.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다