传统的OpenGL纹理立方体
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了传统的OpenGL纹理立方体相关的知识,希望对你有一定的参考价值。
我试图在传统的OpenGL中纹理一个长方体。无论我首先渲染的长方体的哪一侧,这一侧都没有正确纹理,因为所有其他边都正确纹理。我先尝试渲染立方体的不同侧面。
这是绘制立方体的代码,包括它的纹理坐标:
void Objects::cuboidTextured(float width, float height, float depth, float xpos, float ypos, float zpos, float r, float g, float b){
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(xpos, ypos, zpos);
glColor3f(r, g, b);
glBegin(GL_QUADS);
glVertex3f( width, -height, depth );
glTexCoord2f(1,0);
glVertex3f( width, height, depth );
glTexCoord2f(1,1);
glVertex3f( -width, height, depth );
glTexCoord2f(0,1);
glVertex3f( -width, -height, depth );
glTexCoord2f(0,0);
glEnd();
glBegin(GL_QUADS);
glVertex3f( width, -height, -depth );
glTexCoord2f(1,0);
glVertex3f( width, height, -depth );
glTexCoord2f(1,1);
glVertex3f( width, height, depth );
glTexCoord2f(0,1);
glVertex3f( width, -height, depth );
glTexCoord2f(0,0);
glEnd();
glBegin(GL_QUADS);
glVertex3f( -width, -height, depth );
glTexCoord2f(1,0);
glVertex3f( -width, height, depth );
glTexCoord2f(1,1);
glVertex3f( -width, height, -depth );
glTexCoord2f(0,1);
glVertex3f( -width, -height, -depth );
glTexCoord2f(0,0);
glEnd();
glBegin(GL_QUADS);
glVertex3f( width, height, depth );
glTexCoord2f(1,0);
glVertex3f( width, height, -depth );
glTexCoord2f(1,1);
glVertex3f( -width, height, -depth );
glTexCoord2f(0,1);
glVertex3f( -width, height, depth );
glTexCoord2f(0,0);
glEnd();
glBegin(GL_QUADS);
glVertex3f( width, -height, -depth );
glTexCoord2f(0,1);
glVertex3f( width, -height, depth );
glTexCoord2f(0,0);
glVertex3f( -width, -height, depth );
glTexCoord2f(1,1);
glVertex3f( -width, -height, -depth );
glTexCoord2f(1,0);
glEnd();
glBegin(GL_QUADS);
glVertex3f( -width, -height, -depth);
glTexCoord2f(1,0);
glVertex3f( -width, height, -depth);
glTexCoord2f(1,1);
glVertex3f( width, height, -depth);
glTexCoord2f(0,1);
glVertex3f( width, -height, -depth);
glTexCoord2f(0,0);
glEnd();
glPopMatrix();
}
答案
你正在倒退,在glVertex()
之前打电话给glTexCoord()
。
glVertex命令在glBegin / glEnd对中用于指定点,线和多边形顶点。当调用glVertex时,当前颜色,法线,纹理坐标和雾坐标与顶点相关联。
所以,通过glTexCoord()
和朋友设置你所有的顶点状态,然后调用glVertex()
:
glBegin(GL_QUADS);
glTexCoord2f(1,0);
glVertex3f( width, -height, -depth );
glTexCoord2f(1,1);
glVertex3f( width, height, -depth );
glTexCoord2f(0,1);
glVertex3f( width, height, depth );
glTexCoord2f(0,0);
glVertex3f( width, -height, depth );
glEnd();
否则glVertex()
将使用最后设置的任何随机纹理坐标。
以上是关于传统的OpenGL纹理立方体的主要内容,如果未能解决你的问题,请参考以下文章