1
votes

I’m just starting out with 3D graphics programming and I’ve got a nicely encapsulated Cube object. This class has a render() method, which proceeds to push a new matrix, perform transformations, glBegin, specify all the vertices and texture coordinates, glEnd, and pop the matrix. Now, I have written this Cube class with methods for using different textures on different faces of the cube, but that isn't happening at runtime. I understood textures to be like colors, which I can easily change per face of the cube by making a call to glColor before the appropriate vertices, but using texture.bind() (from the slick-util library) seems to do nothing.

This is my render method:

public void render(){
    glPushMatrix();

    top.bind(); 
    top.setTextureFilter(GL_NEAREST);
    glColor4f(1, 1, 1, 0);
    glTranslatef(x, y, z);
    glRotatef(yaw, 0, 1, 0);

    glBegin(GL_QUADS);
    //Top
    glTexCoord2f(0, 0); glVertex3f(0, size, 0);
    glTexCoord2f(0, 1); glVertex3f(0, size, size);
    glTexCoord2f(1, 1); glVertex3f(size, size, size);
    glTexCoord2f(1, 0); glVertex3f(size, size, 0);

    //Front
    top.release();
    side.bind();
    glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
    glTexCoord2f(0, 1); glVertex3f(0, size, 0);
    glTexCoord2f(1, 1); glVertex3f(size, size, 0);
    glTexCoord2f(1, 0); glVertex3f(size, 0, 0);

    //Left
    glTexCoord2f(0, 0); glVertex3f(0, 0, size);
    glTexCoord2f(0, 1); glVertex3f(0, size, size);
    glTexCoord2f(1, 1); glVertex3f(0, size, 0);
    glTexCoord2f(1, 0); glVertex3f(0, 0, 0);

    //Right
    glTexCoord2f(0, 0); glVertex3f(size, 0, size);
    glTexCoord2f(0, 1); glVertex3f(size, size, size);
    glTexCoord2f(1, 1); glVertex3f(size, size, 0);
    glTexCoord2f(1, 0); glVertex3f(size, 0, 0);

    //Back
    glTexCoord2f(0, 0); glVertex3f(0, 0, size);
    glTexCoord2f(0, 1); glVertex3f(0, size, size);
    glTexCoord2f(1, 1); glVertex3f(size, size, size);
    glTexCoord2f(1, 0); glVertex3f(size, 0, size);

    //Bottom
    side.release();
    bottom.bind();
    glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
    glTexCoord2f(0, 1); glVertex3f(0, 0, size);
    glTexCoord2f(1, 1); glVertex3f(size, 0, size);
    glTexCoord2f(1, 0); glVertex3f(size, 0, 0);

    bottom.release();
    glEnd();
    glPopMatrix();
}

Variables top, side, and bottom are the textures I want to put on the faces of the cube.

The result is that every face of the cube has the “top” texture.

The result is the same whether I call texture.release() or not.

1

1 Answers

4
votes

You can't bind textures between glBegin() and glEnd(). Only a limited set of GL calls can be made between glBegin() and glEnd() (see https://www.opengl.org/sdk/docs/man2/xhtml/glBegin.xml for the full list), and glBindTexture() is not one of them.

To bind a different texture for each side, you need to start a new begin/end pair for each side:

//Top
top.bind(); 
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, size, 0);
glTexCoord2f(0, 1); glVertex3f(0, size, size);
glTexCoord2f(1, 1); glVertex3f(size, size, size);
glTexCoord2f(1, 0); glVertex3f(size, size, 0);
glEnd();

//Front
side.bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
...
glEnd();

//Bottom
bottom.bind();
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
...
glEnd();