我已经实现了我的“引擎”,用于很好地绘制线条。但是现在,在我实现了简单的精灵之后,线条不再渲染(但精灵会这样做)。当我将glEnable(GL_TEXTURE_2D)放入我的初始化代码中时,一切都出错了。
glClearColor(0.0f,0.0f,0.0f,0.0f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glEnable( GL_BLEND );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, 1, -1);
我的线条是按照以下方式绘制的:
glLineWidth(lineWidth);
glColor4f(color.getR(), color.getG(), color.getB(), color.getA());
if(transformToViewport) {
Point a = vp.transformCoords(begin);
Point b = vp.transformCoords(end);
glVertex3f(a.coords[0], a.coords[1], 0);
glVertex3f(b.coords[0], b.coords[1], 0);
}
else {
glVertex3f(begin.coords[0], begin.coords[1], 0);
glVertex3f(end.coords[0], end.coords[1], 0);
}
精灵:
float transformedH = vp.transformLength(height);
float transformedW = vp.transformLength(width);
Point transformedCenter = vp.transformCoords(center);
glBindTexture(GL_TEXTURE_2D, texture.getTextureID());
glTexCoord2f(0f, 1f);
glVertex2f(transformedCenter.coords[0] - transformedW / 2, transformedCenter.coords[1] - transformedH / 2);
glTexCoord2f(1f, 1f);
glVertex2f(transformedCenter.coords[0] + transformedW / 2, transformedCenter.coords[1] - transformedH / 2);
glTexCoord2f(0f, 0f);
glVertex2f(transformedCenter.coords[0] - transformedW / 2, transformedCenter.coords[1] + transformedH / 2);
glTexCoord2f(1f, 1f);
glVertex2f(transformedCenter.coords[0] + transformedW / 2, transformedCenter.coords[1] - transformedH / 2);
glTexCoord2f(1f, 0f);
glVertex2f(transformedCenter.coords[0] + transformedW / 2, transformedCenter.coords[1] + transformedH / 2);
glTexCoord2f(0f, 0f);
glVertex2f(transformedCenter.coords[0] - transformedW / 2, transformedCenter.coords[1] + transformedH / 2);
glBindTexture(GL_TEXTURE_2D, 0);
答案 0 :(得分:3)
在绘制纹理之前使用glEnable(GL_TEXTURE_2D);
(例如在方法中),然后立即使用glDisable(GL_TEXTURE_2D);
。
对于初学者(有时候对于更复杂的系统),最好将glEnable
和glDisable
调用配对在一起,以防止将不必要的功能用于事物(在某些情况下会导致问题) ,像这样),你不需要它们。