简体   繁体   中英

Legacy OpenGL texturing cube

I am trying to texture a cuboid in legacy OpenGL. No matter which side of the cuboid I render first, this side is not textured correctly where as all other sides are textured correctly. I have tried rendering different sides of the cube first.

This is the code that draws the cube including it's texture coordinates:

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();
}

You're doing it backwards, calling glVertex() before glTexCoord() .

glVertex :

glVertex commands are used within glBegin/glEnd pairs to specify point, line, and polygon vertices. The current color, normal, texture coordinates, and fog coordinate are associated with the vertex when glVertex is called.

So, set all your vertex state by glTexCoord() and friends, then call 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();

Otherwise glVertex() will use whatever random texture coordinate was last set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM