这是从this派生的字体渲染器。我主要将其代码更改为更快的方式(如我所见)。现在我明白了,我看到了
glCallList() < glDrawArrays() < glDrawElements(GL_QUADS) < glDrawElements(GL_TRIANGLE_STRIP) ...所以这意味着glDrawElements(GL_TRIANGLE_STRIP)是这种渲染中最有效的方式。
但是,从 切换GL_QUADS
到后GL_TRIANGLE_STRIP
,纹理变成了这样:
GL_QUADS
(请注意,从 切换到时,我交换了顶点和纹理坐标的最后 4 个值GL_TRIANGLE_STRIP
)
尤其是。编码:
void Text2D::PrintText5( BMfont& font, int x, int y, const char* text) {
float w = 0;
glPushAttrib(GL_LIST_BIT | GL_CURRENT_BIT | GL_ENABLE_BIT | GL_TRANSFORM_BIT);
glEnable(GL_TEXTURE_2D);
//glDisable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glGetFloatv(GL_MODELVIEW_MATRIX, font.mmat);
glMatrixMode(GL_MODELVIEW);
glScalef(font.scale, font.scale, 1.f);
if (!compiledd)
while (*text)
{
compiledd = true;
const BMchar& _char(font.chars[(int)*(text++)]);
if (_char.width) {
const float verices[] = {
(float) x + w + _char.x_ofs , (float) y + _char.height + _char.y_ofs,
(float) x + w + _char.width + _char.x_ofs, (float) y + _char.height + _char.y_ofs,
(float) x + w + _char.x_ofs , (float) y + _char.y_ofs,
(float) x + w + _char.width + _char.x_ofs, (float) y + _char.y_ofs,
};
for (int i = 0;i < sizeof(verices) / sizeof(float); ++i) {
verts3.push_back(verices[i]);
}
const float texcoords[] = {
(float) _char.x / (float) font.scaleW , (float) InvertY2((_char.y + _char.height) / (float) font.scaleH),
(float)(_char.x + _char.width) / (float) font.scaleW, (float) InvertY2((_char.y + _char.height) / (float) font.scaleH),
(float) _char.x / (float) font.scaleW , (float) InvertY2( _char.y / (float) font.scaleH),
(float)(_char.x + _char.width) / (float) font.scaleW, (float) InvertY2( _char.y / (float) font.scaleH),
};
for (int i = 0;i < sizeof(texcoords) / sizeof(float); ++i) {
coords3.push_back(texcoords[i]);
}
const unsigned char colors[] = {
255, 0, 255,
255, 0, 255,
255, 0, 255,
255, 0, 255
};
indices3.push_back(a++);indices3.push_back(a++);indices3.push_back(a++);indices3.push_back(a++);
for (int i = 0;i < sizeof(colors) / sizeof(unsigned char); ++i) {
color3.push_back(colors[i]);
}
w += (float)(_char.x_advance - _char.x_ofs);
} else w += (float)font.base;
}
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_INDEX_ARRAY);
glVertexPointer (2, GL_FLOAT, 0, &verts3[0]);
glTexCoordPointer(2, GL_FLOAT, 0, &coords3[0]);
glColorPointer(3, GL_UNSIGNED_BYTE, 0, &color3[0]);
glBindTexture(GL_TEXTURE_2D, font.texture);
glDrawElements(GL_TRIANGLE_STRIP, verts3.size() / 2, GL_UNSIGNED_INT, &indices3[0]);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_INDEX_ARRAY);
glDisable(GL_TEXTURE_2D);
glPopAttrib();
}
如何使最后一个字体渲染器与其他字体渲染器相同?
(即使我没有它仍然有效glIndexPointer()
???)