openGL将多个纹理绑定到四边形

时间:2013-07-30 18:25:35

标签: java opengl textures

我需要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();

每次我运行应用程序时,顶部只会出现黑色,其余部分都是相同的纹理。 感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

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

而不是切换纹理,你应该使用纹理数组。