簡體   English   中英

舊版OpenGL紋理無法正常工作

[英]Legacy OpenGL Textures not working correctly

我是舊版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()之前的幾行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM