簡體   English   中英

openGL將多個紋理綁定到四邊形

[英]openGL bind multiple textures to quads

我必須有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();

每次我運行該應用程序時,頂部都會顯示為黑色,其余的紋理相同。 謝謝你的幫助!

您無法在glBegin / glEnd塊內更改紋理綁定。 在沒有繪制原始圖元的情況下,必須更改此狀態。 話雖這么說:不要使用glBegin / glEnd,也稱為即時模式。 它已不推薦使用,並已從現代OpenGL中刪除。 取而代之,最好使用“頂點數組”(Vertex Arrays)。

同樣,除了切換紋理,還應該使用紋理數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM