1
votes

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();
  } 
}
1
May be the problem is glBindtexture. try to write it out the loop. And I think the normal coord must been clamped between -1 and 1. - user10809571
What's up with that double glTexCoord2f() call in the middle? - genpfault

1 Answers

4
votes

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