我从一个大型项目中取出我的字体系统并修复它以使其正常工作。将其重新插入我的主项目,现在字体无法正确呈现。我留下了白框而不是字形纹理渲染。这是与此问题相关的代码。
bool KFont::MapGlyph(char a) {
const char* T = &a;
SDL_Surface* ThisGlyph;
//Font is a struct that holds the font information Font->Font is the TTF pointer.
if(!(ThisGlyph = TTF_RenderUTF8_Blended(Font->Font, T, GlyphColor))) {
//std::cout << "!" << std::endl;
return false;
}
KTexture* Temp;
Temp = new KTexture;
if(!(Temp->LoadImage(ThisGlyph))) {
//std::cout<< "!!" << std::endl;
return false;
}
SDL_FreeSurface(ThisGlyph);
Glyphs->Glyphs.insert( std::pair<char,KTexture*>(a,Temp) );
return true;
}
LoadImage 函数是:
bool KTexture::LoadImage(SDL_Surface* Surface) {
glEnable(GL_TEXTURE_2D);
if(TextureID > 0) {
glDeleteTextures(1, &TextureID);
TextureID = 0;
}
if(Surface == NULL) return false;
GLenum Mode = 0;
switch (Surface->format->BytesPerPixel) {
case 1: {
Mode = GL_ALPHA;
break;
}
case 3: {
Mode = GL_RGB;
break;
}
case 4: {
Mode = GL_RGBA;
break;
}
default: { break;
}
}
glGenTextures(1, &TextureID);
if(TextureID & GL_INVALID_VALUE) exit(1);
glBindTexture(GL_TEXTURE_2D, TextureID);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, Mode, Surface->w, Surface->h, 0, Mode, GL_UNSIGNED_BYTE, Surface->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
_Width = Surface->w;
_Height = Surface->h;
glDisable(GL_TEXTURE_2D);
return true;
}
在渲染过程中,字符是正确的,要渲染的 X/Y 位置是正确的,Surface->w/h 都一样吗?TextureID 始终保持为 0,而在工作版本中它会更改。glBindTexture() 似乎有问题,但我已经多次查看不同的代码,无法弄清楚为什么这不是渲染。
渲染功能(如果有帮助)
void KTexture::RenderQuad(int x, int y, int w, int h) {
glPushMatrix();
glEnable(GL_TEXTURE_2D);
if(TextureID & GL_INVALID_VALUE) exit(1);
glBindTexture(GL_TEXTURE_2D,TextureID);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(x,y,0);
glTexCoord2f(1,0); glVertex3f(x+w,y,0);
glTexCoord2f(1,1); glVertex3f(x+w,y+h,0);
glTexCoord2f(0,1); glVertex3f(x,y+h,0);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
glFlush();
}
这些是我的初始
if(SDL_Init(SDL_INIT_EVERYTHING) < 0 ) {
exit(0x1);
}
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);
if((Surf_Display = SDL_SetVideoMode(winWidth,winHeight,32, SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER |SDL_OPENGL | SDL_RESIZABLE )) == NULL) {
exit(2);
}
glClearColor(0, 0, 0, 0);
glClearDepth(1.0f);
glViewport(0, 0, winWidth, winHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, winWidth, winHeight, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable (GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glLoadIdentity();
知道为什么我的字体不会渲染吗?我尝试将glTexEnvf
模式更改为贴花、调制和混合,这些都没有任何效果。只是白色的小方块。