旧版OpenGL纹理立方体

时间:2018-12-20 14:46:56

标签: c++ 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();
}

1 个答案:

答案 0 :(得分:4)

您正在倒退,在glVertex()之前调用glTexCoord()

glVertex

  在glBegin / glEnd对中使用

glVertex命令来指定               点,线和多边形顶点。 当前颜色,常规,纹理               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()将使用最后设置的任何随机纹理坐标。