0

我必须有 6 个四边形组成一个立方体,我想将不同的纹理绑定到每个四边形。我怎么能这样做?这是我到目前为止所拥有的:

glPushMatrix(); {
        glTranslatef(x, y, z);
        glBegin(GL_QUADS);
        {
            //FrontFace
            if(id != Blocks.Air){
                setTexCords = true;
                glColor3f(1, 1, 1);
                id.getFrontTexture().bind();
            }else{
                setTexCords = false;
                if(textureType == "IMAGE"){

                }else{
                    glColor3d(toFloatColor(tid.getRed()), toFloatColor(tid.getGreen()), toFloatColor(tid.getBlue()));
                }
            }
            if(setTexCords) glTexCoord2f(0, 0);
            glVertex3f(-Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE);
            if(setTexCords) glTexCoord2f(0, 1);
            glVertex3f(Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE);
            if(setTexCords) glTexCoord2f(1, 1);
            glVertex3f(Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE);
            if(setTexCords) glTexCoord2f(1, 0);
            glVertex3f(-Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE);
            {
            glEnd();


            glBegin(GL_QUADS);
            {
            //BackFace
            if(id != Blocks.Air){
                setTexCords = true;
                id.getBackTexture().bind();
            }else{
                setTexCords = false;
                if(textureType == "IMAGE"){

                }else{
                    glColor3d(toFloatColor(tid.getRed()), toFloatColor(tid.getGreen()), toFloatColor(tid.getBlue()));
                }
            }
            if(setTexCords) glTexCoord2f(0, 0);
            glVertex3f(-Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE);
            if(setTexCords) glTexCoord2f(0, 1);
            glVertex3f(-Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE);
            if(setTexCords) glTexCoord2f(1, 1);
            glVertex3f(Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE);
            if(setTexCords) glTexCoord2f(1, 0);
            glVertex3f(Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE);
            }
            glEnd();

            glBegin(GL_QUADS);
            {
            //BottomFace
            if(id != Blocks.Air){
                setTexCords = true;
                id.getBottomTexture().bind();
            }else{
                setTexCords = false;
                if(textureType == "IMAGE"){

                }else{
                    glColor3d(toFloatColor(tid.getRed()), toFloatColor(tid.getGreen()), toFloatColor(tid.getBlue()));
                }
            }
            if(setTexCords) glTexCoord2f(0, 0);
            glVertex3f(-Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE);
            if(setTexCords) glTexCoord2f(0, 1);
            glVertex3f(-Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE);
            if(setTexCords) glTexCoord2f(1, 1);
            glVertex3f(-Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE);
            if(setTexCords) glTexCoord2f(1, 0);
            glVertex3f(-Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE);

            }
            glEnd();

            glBegin(GL_QUADS);
            {
            //TopFace
            if(id != Blocks.Air){
                glColor3f(1, 1, 1);
                setTexCords = true;
                id.getTopTexture().bind();
            }else{
                setTexCords = false;
                if(textureType == "IMAGE"){

                }else{
                    glColor3d(toFloatColor(tid.getRed()), toFloatColor(tid.getGreen()), toFloatColor(tid.getBlue()));
                }
            }
            if(setTexCords) glTexCoord2f(0, 0);
            glVertex3f(Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE);
            if(setTexCords) glTexCoord2f(0, 1);
            glVertex3f(Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE);
            if(setTexCords) glTexCoord2f(1, 1);
            glVertex3f(Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE);
            if(setTexCords) glTexCoord2f(1, 0);
            glVertex3f(Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE);

            }
            glEnd();

            glBegin(GL_QUADS);
            {
            //LeftFace
            if(id != Blocks.Air){
                setTexCords = true;
                id.getLeftTexture().bind();
            }else{
                setTexCords = false;
                if(textureType == "IMAGE"){

                }else{
                    glColor3d(toFloatColor(tid.getRed()), toFloatColor(tid.getGreen()), toFloatColor(tid.getBlue()));
                }
            }
            if(setTexCords) glTexCoord2f(0, 0);
            glVertex3f(-Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE);
            if(setTexCords) glTexCoord2f(0, 1);
            glVertex3f(Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE);
            if(setTexCords) glTexCoord2f(1, 1);
            glVertex3f(Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE);
            if(setTexCords) glTexCoord2f(1, 0);
            glVertex3f(-Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE);

            }
            glEnd();

            glBegin(GL_QUADS);
            {

            //Right Face
            if(id != Blocks.Air){
                setTexCords = true;
                id.getRightTexture().bind();
            }else{
                setTexCords = false;
                if(textureType == "IMAGE"){

                }else{
                    glColor3d(toFloatColor(tid.getRed()), toFloatColor(tid.getGreen()), toFloatColor(tid.getBlue()));
                }
            }
            if(textureType == "IMAGE"){

            }else{
                glColor3d(toFloatColor(tid.getRed()), toFloatColor(tid.getGreen()), toFloatColor(tid.getBlue()));
            }
            if(setTexCords) glTexCoord2f(0, 0);
            glVertex3f(-Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE);
            if(setTexCords) glTexCoord2f(0, 1);
            glVertex3f(Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE,-Blocks.BLOCK_SIZE);
            if(setTexCords) glTexCoord2f(1, 1);
            glVertex3f(Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE);
            if(setTexCords) glTexCoord2f(1, 0);
            glVertex3f(-Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE,Blocks.BLOCK_SIZE);
        }
    glEnd();
    }
    glPopMatrix();

每次我运行应用程序时,顶部都会变黑,其余的都是相同的纹理。谢谢你的帮助!

4

1 回答 1

0

您不能在 glBegin/glEnd 块中更改纹理绑定。这是一个在没有绘制图元时必须更改的状态。话虽如此:不要使用 glBegin/glEnd,也称为立即模式。它已被弃用并从现代 OpenGL 中删除。请改用顶点数组,最好使用顶点缓冲区对象 (VBO)。

此外,您应该使用纹理数组而不是切换纹理。

于 2013-07-30T18:42:01.170 回答