我正在研究字体生成解决方案并面临纹理问题。我正在动态创建纹理图集,然后通过它们的纹理坐标映射每个字母。
这是映射“t”字母的结果:(生成的纹理在顶部,我绘制的在底部)
似乎问题在于图元和纹理区域的大小不匹配,这就是 openGL 尝试调整它大小的原因。加载/渲染纹理时,我在 Min 和 Mag 上都使用 GL_NEAREST
这是渲染代码:
glBegin(GL_TRIANGLE_STRIP);
float twidth = 7.f / 512.f;
float theight = 11.f / 512.f;
float width = 7.f;
float height = 11.f;
float w = width / 2.f;
float h = height / 2.f;
float x = 100;
float y = 100;
float tx = 135.f / 512.f;
float ty = 0;
glTexCoord2f(tx, ty);
glVertex2f(x - w, y-h);
glTexCoord2f(tx + twidth, ty);
glVertex2f(x + w, y-h);
glTexCoord2f(tx, ty + theight);
glVertex2f(x - w, y+h);
glTexCoord2f(tx + twidth, ty + theight);
glVertex2f(x + w, y+h);
glEnd();
这是初始化代码:
glViewport(0,0,width,height);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
// Set up the GL state.
glClearColor(1, 1, 1, 1);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
//glDisable(GL_BLEND);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, height, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
纹理加载代码:
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 512, 512,
0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, &texBinary[0]);