我是旧版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();
}
}
答案 0 :(得分:4)
You can't call glBindTexture()
inside a glBegin()
/glEnd()
pair:
在glBegin和glEnd之间只能使用GL命令的子集。 这些命令是 glVertex, glColor, glSecondaryColor, glIndex, glNormal, glFogCoord, glTexCoord, glMultiTexCoord, glVertexAttrib, glEvalCoord, glEvalPoint, glArrayElement, glMaterial,以及 glEdgeFlag。 也, 可以使用 glCallList或 glCallLists执行 显示仅包含前面命令的列表。 如果在glBegin和glEnd之间执行了其他任何GL命令, 设置了错误标志并且忽略了该命令。
请移动glBindTexture()
,直到glBegin()
前调用几行。