简体   繁体   中英

Legacy OpenGL Textures not working correctly

I am usign legacy openGL. I am drawing multiple objects in a scene. I want the sphere that is being drawn to be textured but all other objects to be solid colours. However, if I try to disable the texture after drawing the sphere, everything else is black.

This is the code where I create the texture

    glGenTextures(1, &textures);
    glBindTexture(GL_TEXTURE_2D, textures);

    glEnable(GL_TEXTURE_2D);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->Width(), image->Height(), 0, GL_RGB, GL_UNSIGNED_BYTE, image->imageField());
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);  

This is where i draw the sphere:

id Objects::sphere(float xPos, float yPos, float zPos){
    const int NR_PHI = 20;
    const int NR_THETA = 20;

    glColor3f(1, 1, 1);

  for(int longitude = 0; longitude < NR_PHI; longitude++)
    for(int latitude = 0; latitude < NR_THETA; latitude++){
      float d_phi   = 2*M_PI/NR_PHI;
      float d_theta = M_PI/NR_THETA; 
      glBegin(GL_TRIANGLES);
      glBindTexture(GL_TEXTURE_2D, textures);
      double x, y, z;

      x = cos(longitude*d_phi)*sin(latitude*d_theta) + xPos;
      y = sin(longitude*d_phi)*sin(latitude*d_theta) + yPos;
      z = cos(latitude*d_theta) + zPos;
      glNormal3f(x, y, z);
      glTexCoord2f(static_cast<float>(longitude)/NR_PHI, static_cast<float>(latitude)/NR_THETA);
      glVertex3f(x, y, z);
      x = cos((longitude+1)*d_phi)*sin(latitude*d_theta) + xPos;
      y = sin((longitude+1)*d_phi)*sin(latitude*d_theta) + yPos;
      z = cos(latitude*d_theta) + zPos;
      glNormal3f(x, y, z);
      glTexCoord2f(d_phi,0);
      glTexCoord2f(static_cast<float>(longitude+1)/NR_PHI, static_cast<float>(latitude)/NR_THETA);
      glVertex3f(x, y, z);
      x = cos((longitude+1)*d_phi)*sin((latitude+1)*d_theta) + xPos;
      y = sin((longitude+1)*d_phi)*sin((latitude+1)*d_theta) + yPos;
      z = cos((latitude+1)*d_theta) + zPos;
      glNormal3f(x, y, z);
      glTexCoord2f(static_cast<float>(longitude+1)/NR_PHI, static_cast<float>(latitude+1)/NR_THETA);
      glVertex3f(x, y, z);      

      x = cos(longitude*d_phi)*sin(latitude*d_theta) + xPos;
      y = sin(longitude*d_phi)*sin(latitude*d_theta) + yPos;
      z = cos(latitude*d_theta) + zPos; 
      glNormal3f(x, y, z);
      glTexCoord2f(static_cast<float>(longitude)/NR_PHI, static_cast<float>(latitude)/NR_THETA);
      glVertex3f(x, y, z);
      x = cos((longitude+1)*d_phi)*sin((latitude+1)*d_theta) + xPos;
      y = sin((longitude+1)*d_phi)*sin((latitude+1)*d_theta) + yPos;
      z = cos((latitude+1)*d_theta) + zPos;
      glNormal3f(x, y, z);
      glTexCoord2f(static_cast<float>(longitude+1)/NR_PHI, static_cast<float>(latitude+1)/NR_THETA);
      glVertex3f(x, y, z);
      x = cos((longitude)*d_phi)*sin((latitude+1)*d_theta) + xPos;
      y = sin((longitude)*d_phi)*sin((latitude+1)*d_theta) + yPos;
      z = cos((latitude+1)*d_theta) + zPos;
      glNormal3f(x, y, z);
      glTexCoord2f(static_cast<float>(longitude)/NR_PHI, static_cast<float>(latitude+1)/NR_THETA);
      glVertex3f(x, y, z);      

      glBindTexture(GL_TEXTURE_2D, 0);     
      glEnd();
  } 
}

You can't call glBindTexture() inside a glBegin() / glEnd() pair:

Only a subset of GL commands can be used between glBegin and glEnd. The commands are glVertex, glColor, glSecondaryColor, glIndex, glNormal, glFogCoord, glTexCoord, glMultiTexCoord, glVertexAttrib, glEvalCoord, glEvalPoint, glArrayElement, glMaterial, and glEdgeFlag. Also, it is acceptable to use glCallList or glCallLists to execute display lists that include only the preceding commands. If any other GL command is executed between glBegin and glEnd, the error flag is set and the command is ignored.

So move that glBindTexture() call a couple lines up to before glBegin() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM