2013-07-30 155 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