OpenGL + Slick Loaded纹理正在旋转

时间:2013-08-07 10:30:22

标签: java opengl textures slick

我正在使用 OpenGL 在Java中创建 3D游戏引擎。我正在使用光滑的库来实现纹理。 我正在使用一种方法来创建矩形/正方形棱镜(6个正方形/矩形)。在同样的方法中,我实现了纹理。

问题在于棱镜的5个边以90度旋转纹理渲染。当maing地板时并不重要,但因为我在砖墙纹理上使用它看起来不正确。

由于这是一面墙,我只关心它的四面。

墙面纹理在棱镜的三个侧面上旋转90度。并且只在一侧进行旋转而不进行旋转。

我无法解决问题。我是否需要更改纹理的实现方式?

这是创建棱镜的方法。

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

使用以下行,我创建一个带有墙纹理的立方体。

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

以下是三个旋转边之一One of the Three Wrong Sides

以下是唯一正确的方面The only Correct Side

1 个答案:

答案 0 :(得分: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();
}