2013-07-30 198 views
0

我必須組成一個立方體的6個四邊形,我想綁定不同的紋理到每個四邊形。我到底該怎麼做?這是我到目前爲止有:openGL綁定多個紋理四邊形

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

我每次運行應用程序前剛剛出現的黑色,其餘都是相同的紋理。 感謝您的幫助!

回答

0

您不能在glBegin/glEnd塊中更改紋理綁定。這是一個在沒有繪製基元時必須改變的狀態。這就是說:不要使用glBegin/glEnd,也稱爲即時模式。它已被棄用,並從現代OpenGL中刪除。改爲使用頂點數組,理想情況下使用頂點緩衝對象(VBO)。

而不是切換紋理,你應該使用紋理數組。

+0

您不能在固定函數GL中使用數組紋理。 –

+0

@NicolBolas:嗯,我建議除了放棄固定功能之外,還要使用紋理陣列。 – datenwolf

+0

我更新了我的代碼以現在使用Vertex數組,但是非常感謝 – APerson