旧版OpenGL纹理无法正常工作



我是USIGN LECACY OPENGL。我正在场景中绘制多个对象。我希望要绘制的球体纹理,但所有其他对象都是纯色。但是,如果我尝试在绘制球体后禁用纹理,其他一切都是黑色。

这是我创建纹理的代码

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

这是我绘制球体的地方:

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

您无法在glBegin()/glEnd()配对中调用glBindTexture()

只能在GLBEGIN和GLEND之间使用GL命令的子集。 命令是 Glvertex, Glcolor, GlsecondaryColor, Glindex, glnormal, Glfogcoord,, Gltexcoord, Glmultitexcoord, Glvertexattrib, glevalcoord, glevalpoint, Glarrayelement, glmaterial和 GLEDGEFLAG。 还, 可以接受 glcalllist或 glcalllists要执行 显示列表仅包括前面命令。 如果在Glbegin和Glend之间执行任何其他GL命令, 设置了错误标志并忽略了命令。

所以移动glBindTexture()呼叫几个在glBegin()之前排队。

最新更新