OpenGL在2个纹理之间切换

时间:2014-05-10 16:50:05

标签: c++ opengl textures glut texture-mapping

我正在研究这个OpenGL项目,我有两个PNG文件需要绑定到两个不同的对象。到目前为止,我只是将最近加载的纹理绑定到所有内容。

我加载了这样的PNG:

void load_texture(GLuint texture, const char* filename, int x, int y) {
  Fl_PNG_Image image(filename);    // load texturemap
  glBindTexture(GL_TEXTURE_2D, texture);
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE , GL_MODULATE);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  gluBuild2DMipmaps(GL_TEXTURE_2D, 3, x, y, GL_RGB, GL_UNSIGNED_BYTE, image.data()[0]);
}

在初学者中我得到了这个:

GLuint* textures;
...
int main(void) {
   ...
   textures = new GLuint[2];
   glGenTextures(2, textures);
   ...

在我的绘图功能中我正在这样做:

  glEnable(GL_TEXTURE_2D);      
  load_texture(textures[1], "wood.png", 64, 64);
  load_texture(textures[0], "brick.png", 128, 128);

  for (unsigned int x = 0; x < x_size; ++x) {
    for (unsigned int z = 0; z < z_size; ++z) {
    char type = m->get_object(x,z);
    switch (type) {
      case 'W': {
      // Front Face
        glBegin(GL_QUADS);
        glBindTexture(GL_TEXTURE_2D, textures[0]);
        glColor3f(1.0, 1.0, 1.0);
        glTexCoord2f(0.0, 1.0); glVertex3f(x    , 1, z + 1);
        glTexCoord2f(1.0, 1.0); glVertex3f(x + 1, 1, z + 1);
        glTexCoord2f(1.0, 0.0); glVertex3f(x + 1, 0, z + 1);
        glTexCoord2f(0.0, 0.0); glVertex3f(x    , 0, z + 1);
        // Back Face
        glTexCoord2f(0.0, 1.0); glVertex3f(x + 1, 1, z);
        glTexCoord2f(1.0, 1.0); glVertex3f(x    , 1, z);
        glTexCoord2f(1.0, 0.0); glVertex3f(x    , 0, z);
        glTexCoord2f(0.0, 0.0); glVertex3f(x + 1, 0, z);
        // Right face
        glTexCoord2f(0.0, 1.0); glVertex3f(x + 1, 1, z + 1);
        glTexCoord2f(1.0, 1.0); glVertex3f(x + 1, 1, z    );
        glTexCoord2f(1.0, 0.0); glVertex3f(x + 1, 0, z    );
        glTexCoord2f(0.0, 0.0); glVertex3f(x + 1, 0, z + 1);
        // Left Face
        glTexCoord2f(0.0, 1.0); glVertex3f(x, 1, z    );
        glTexCoord2f(1.0, 1.0); glVertex3f(x, 1, z + 1);
        glTexCoord2f(1.0, 0.0); glVertex3f(x, 0, z + 1);
        glTexCoord2f(0.0, 0.0); glVertex3f(x, 0, z    );
        glEnd();
        break;
      }
      case ' ': {
        // Draw floor
        glBegin(GL_QUADS);
        glBindTexture(GL_TEXTURE_2D, textures[1]);
        glTexCoord2f(0.0, 0.0); glVertex3f(x    , 0, z    );
        glTexCoord2f(0.0, 1.0); glVertex3f(x    , 0, z + 1);
        glTexCoord2f(1.0, 1.0); glVertex3f(x + 1, 0, z + 1);
        glTexCoord2f(1.0, 0.0); glVertex3f(x + 1, 0, z    );
        glEnd();

        // Draw roof
        glBegin(GL_QUADS);
        glBindTexture(GL_TEXTURE_2D, textures[0]);
        glTexCoord2f(0.0, 0.0); glVertex3f(x    , 1, z    );
        glTexCoord2f(0.0, 1.0); glVertex3f(x    , 1, z + 1);
        glTexCoord2f(1.0, 1.0); glVertex3f(x + 1, 1, z + 1);
        glTexCoord2f(1.0, 0.0); glVertex3f(x + 1, 1, z    );
        glEnd();
        break;
      }
     default:
        break;
     }
   }

看起来像这样: output

但是wood.png看起来像这样: wood.png

应该在地板和天花板上渲染。 任何见解将不胜感激。 提前致谢, 最大

1 个答案:

答案 0 :(得分:4)

您无法在glBindTextureglBegin之间致电glEnd

  

如果执行glBindTexture,则生成GL_INVALID_OPERATION               在执行glBegin和相应的之间               执行glEnd。

请参阅glBindTexture documentation