我在通过SDL_ttf在OpenGL中显示文本时遇到了麻烦。2我已经编写了LoadFont()函数,它加载字体并制作OpenGL纹理。
int LoadFont( char *name, GLubyte r, GLubyte g, GLubyte b, char *text, int ptsize)
{
SDL_Color color = {r, g, b, 0};
TTF_Font *font = TTF_OpenFont(name, ptsize);
Uint32 rmask, gmask, bmask, amask;
# if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
# else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
# endif
SDL_Surface *msg = TTF_RenderText_Blended(font, text, color);
SDL_SetAlpha(msg, 0, 0);
if(!msg) {
printf("Unable to Render text\n");
return -1;
}
SDL_Surface *tmp = SDL_CreateRGBSurface(0, msg->w, msg->h, 32, rmask, gmask, bmask, amask);
SDL_BlitSurface(msg, NULL, tmp, NULL);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, msg->w, msg->h, 0, GL_RGBA,
GL_UNSIGNED_BYTE, msg->pixels);
return 1;
}
我在initGL()中使用了这个函数:
int initGL( GLvoid )
{
glViewport(0, 0, SCREEN_WIDHT, SCREEN_HEIGHT);
LoadFont("THWACK.TTF", 255, 255, 255, "Hello!", 14);
glEnable(GL_TEXTURE_2D);
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)SCREEN_WIDHT/(GLfloat)SCREEN_HEIGHT, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
return TRUE;
}
此外,我尝试在多边形上显示此文本:
int drawGLScene( GLvoid )
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -5.0f);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glEnable(GL_TEXTURE_2D);
glBegin(GL_POLYGON);
glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, 1.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
SDL_GL_SwapBuffers();
return TRUE;
}
但文本不显示,只有白色多边形。
1条答案
按热度按时间yruzcnhs1#
GL_TEXTURE_MIN_FILTER
的默认值为GL_NEAREST_MIPMAP_LINEAR
。如果您请求mipmap,但没有上传完整的mipmap,则会得到白色纹理。
你有这个:
请尝试以下顺序:
纹理过滤状态属于纹理对象,而不是纹理单元,因此
glTexParameterf()
afterglBindTexture()
:OpenGL 2.1 spec,第3.8.11节(第180页)和第3.8.12节(第182页):
3.8.11:...接下来,有两组纹理属性;每个都由选定的缩小和放大滤镜...
3.8.12:...生成的纹理对象是一个新的状态向量,包括3.8.11节中列出的所有状态值,设置为相同的初始值...
**EDIT:**如果要使用
TTF_Render*_Blended()
,还必须在渲染之前启用混合:示例: