Question

I have this OpenGL project I'm working on and I have these 2 PNG files that I need to bind to two different objects. What I have so far just binds the most recently loaded texture to everything.

I load my PNGs like this:

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]);
}

In init I've got this:

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

in my draw function I'm doing this:

  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;
     }
   }

It looks like this: output

But the wood.png looks like this: wood.png

and should be rendering on the floor and ceiling. Any insights would be greatly appreciated. Thanks in advance, Max

Était-ce utile?

La solution

You can not call glBindTexture between glBegin and glEnd.

GL_INVALID_OPERATION is generated if glBindTexture is executed between the execution of glBegin and the corresponding execution of glEnd.

See glBindTexture documentation.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top