Search code examples
javaopengltexturesslick2d

OpenGL + Slick Loaded Textures Are Rotating


I am using OpenGL to create a 3D game engine in Java. And I am using the slick library to implement textures. I am using a method to create rectangle/square prisms (6 squares/rectangles). In the same method, I implement the textures.

The problem is that 5 sides of the prism render with 90 degrees rotated textures. It doesn't really matter when maing floor but since I am using it on a brick wall texture it isn't looking right.

Since this is a wall I only care about 4 sides of it.

The wall texture is being rendered 90 degrees rotated on 3 sides of the prism. And is rendered without rotation on only 1 side.

I couldn't work the problem out. Do I need to change how the texture is implemented?

Here is the method for creating a prism.

public static void quadPrizm(float x, float y, float z, float sx, float sy, float sz, Texture tex){
    glPushMatrix();
    {
        glTranslatef(x, y, z);

        tex.bind();
        glBegin(GL_QUADS);
        {
            //ROTATED (WRONG)
            glTexCoord2f(0, 0); glVertex3f(-sx/2, -sy/2, sz/2);
            glTexCoord2f(0, 4); glVertex3f(sx/2, -sy/2, sz/2);
            glTexCoord2f(4, 4); glVertex3f(sx/2, sy/2, sz/2);
            glTexCoord2f(4, 0); glVertex3f(-sx/2, sy/2, sz/2);

            //THE CORRECT SIDE
            glTexCoord2f(0, 0); glVertex3f(-sx/2, -sy/2, -sz/2);
            glTexCoord2f(0, 4); glVertex3f(-sx/2, sy/2, -sz/2);
            glTexCoord2f(4, 4); glVertex3f(sx/2, sy/2, -sz/2);
            glTexCoord2f(4, 0); glVertex3f(sx/2, -sy/2, -sz/2);

            //ROTATED (WRONG)
            glTexCoord2f(0, 0); glVertex3f(-sx/2, -sy/2, -sz/2);
            glTexCoord2f(0, 4); glVertex3f(-sx/2, -sy/2, sz/2);
            glTexCoord2f(4, 4); glVertex3f(-sx/2, sy/2, sz/2);
            glTexCoord2f(4, 0); glVertex3f(-sx/2, sy/2, -sz/2);

            //ROTATED (WRONG)
            glTexCoord2f(0, 0); glVertex3f(sx/2, -sy/2, -sz/2);
            glTexCoord2f(0, 4); glVertex3f(sx/2, -sy/2, sz/2);
            glTexCoord2f(4, 4); glVertex3f(sx/2, sy/2, sz/2);
            glTexCoord2f(4, 0); glVertex3f(sx/2, sy/2, -sz/2);

            //BOTTOM SIDE
            glTexCoord2f(0, 0); glVertex3f(-sx/2, -sy/2, -sz/2);
            glTexCoord2f(0, 4); glVertex3f(sx/2, -sy/2, -sz/2);
            glTexCoord2f(4, 4); glVertex3f(sx/2, -sy/2, sz/2);
            glTexCoord2f(4, 0); glVertex3f(-sx/2, -sy/2, sz/2);

            //TOP SIDE
            glTexCoord2f(0, 0); glVertex3f(-sx/2, sy/2, -sz/2);
            glTexCoord2f(0, 4); glVertex3f(sx/2, sy/2, -sz/2);
            glTexCoord2f(4, 4); glVertex3f(sx/2, sy/2, sz/2);
            glTexCoord2f(4, 0); glVertex3f(-sx/2, sy/2, sz/2);
        }
        glEnd();
    }
    glPopMatrix();
}

With the following line, I create a cube with the wall texture.

Draw.cube(0, 10, 10, 5, 5, 5, wall);

Here is the one of the three rotated sides: One of the Three Wrong Sides

Here is the only side that is correct: The only Correct Side


Solution

  • I have solved the problem by changing the Texture Coordinates. The new code is like this:

    public static void quadPrizm(float x, float y, float z, float sx, float sy, float sz, Texture tex){
    glPushMatrix();
    {
        glTranslatef(x, y, z);
    
        tex.bind();
        glBegin(GL_QUADS);
        {
            //ROTATED (WRONG)
            glTexCoord2f(4, 0); glVertex3f(-sx/2, -sy/2, sz/2);
            glTexCoord2f(0, 0); glVertex3f(sx/2, -sy/2, sz/2);
            glTexCoord2f(0, 4); glVertex3f(sx/2, sy/2, sz/2);
            glTexCoord2f(4, 4); glVertex3f(-sx/2, sy/2, sz/2);
    
            //THE CORRECT SIDE
            glTexCoord2f(0, 0); glVertex3f(-sx/2, -sy/2, -sz/2);
            glTexCoord2f(0, 4); glVertex3f(-sx/2, sy/2, -sz/2);
            glTexCoord2f(4, 4); glVertex3f(sx/2, sy/2, -sz/2);
            glTexCoord2f(4, 0); glVertex3f(sx/2, -sy/2, -sz/2);
    
            //ROTATED (WRONG)
            glTexCoord2f(4, 0); glVertex3f(-sx/2, -sy/2, -sz/2);
            glTexCoord2f(0, 0); glVertex3f(-sx/2, -sy/2, sz/2);
            glTexCoord2f(0, 4); glVertex3f(-sx/2, sy/2, sz/2);
            glTexCoord2f(4, 4); glVertex3f(-sx/2, sy/2, -sz/2);
    
            //ROTATED (WRONG)
            glTexCoord2f(4, 0); glVertex3f(sx/2, -sy/2, -sz/2);
            glTexCoord2f(0, 0); glVertex3f(sx/2, -sy/2, sz/2);
            glTexCoord2f(0, 4); glVertex3f(sx/2, sy/2, sz/2);
            glTexCoord2f(4, 4); glVertex3f(sx/2, sy/2, -sz/2);
    
            //BOTTOM SIDE
            glTexCoord2f(0, 0); glVertex3f(-sx/2, -sy/2, -sz/2);
            glTexCoord2f(0, 4); glVertex3f(sx/2, -sy/2, -sz/2);
            glTexCoord2f(4, 4); glVertex3f(sx/2, -sy/2, sz/2);
            glTexCoord2f(4, 0); glVertex3f(-sx/2, -sy/2, sz/2);
    
            //TOP SIDE
            glTexCoord2f(0, 0); glVertex3f(-sx/2, sy/2, -sz/2);
            glTexCoord2f(0, 4); glVertex3f(sx/2, sy/2, -sz/2);
            glTexCoord2f(4, 4); glVertex3f(sx/2, sy/2, sz/2);
            glTexCoord2f(4, 0); glVertex3f(-sx/2, sy/2, sz/2);
            }
            glEnd();
        }
        glPopMatrix();
    }