1

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

4

1 回答 1

0

我通过更改纹理坐标解决了这个问题。新代码是这样的:

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();
}
于 2013-08-08T08:07:32.677 回答