几天前,我开始使用opengl 1.1进行小型体素游戏(我知道它已经过时了)。我使用sfml库来创建opengl上下文。我认为问题在于纹理坐标,但我不确定。渲染代码:
glPushMatrix();
glBegin(GL_QUADS);
glColor3f(0.5, 0.5, 0.5);
//front
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(1, 0); glVertex3f(0, 1, 0);
glTexCoord2f(1, 1); glVertex3f(1, 1, 0);
glTexCoord2f(1, 0); glVertex3f(1, 0, 0);
//back
glTexCoord2f(0, 0); glVertex3f(0, 0, 1);
glTexCoord2f(1, 0); glVertex3f(0, 1, 1);
glTexCoord2f(1, 1); glVertex3f(1, 1, 1);
glTexCoord2f(1, 0); glVertex3f(1, 0, 1);
glColor3f(0.6, 0.6, 0.6);
//top
glTexCoord2f(0, 0); glVertex3f(0, 1, 0);
glTexCoord2f(1, 0); glVertex3f(0, 1, 1);
glTexCoord2f(1, 1); glVertex3f(1, 1, 1);
glTexCoord2f(1, 0); glVertex3f(1, 1, 0);
//bottom
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(1, 0); glVertex3f(0, 0, 1);
glTexCoord2f(1, 1); glVertex3f(1, 0, 1);
glTexCoord2f(1, 0); glVertex3f(1, 0, 0);
glColor3f(0.7, 0.7, 0.7);
//left
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(1, 0); glVertex3f(0, 1, 0);
glTexCoord2f(1, 1); glVertex3f(0, 1, 1);
glTexCoord2f(1, 0); glVertex3f(0, 0, 1);
//right
glTexCoord2f(0, 0); glVertex3f(1, 0, 0);
glTexCoord2f(1, 0); glVertex3f(1, 1, 0);
glTexCoord2f(1, 1); glVertex3f(1, 1, 1);
glTexCoord2f(1, 0); glVertex3f(1, 0, 1);
glEnd();
glPopMatrix();
这是游戏中的木质纹理之一:
这是木质纹理的图像:
它真的很奇怪。怎么了?有人可以发布一个普通纹理立方体的例子吗?
答案 0 :(得分:4)
尝试更改代码:
glTexCoord2f(0, 0); ...
glTexCoord2f(1, 0); ...
glTexCoord2f(1, 1); ...
glTexCoord2f(1, 0); ...
到
glTexCoord2f(0, 0); ...
glTexCoord2f(1, 0); ...
glTexCoord2f(1, 1); ...
glTexCoord2f(0, 1); ... (changed this line)
您将立方体的两个角映射到纹理中的同一个点。