gpt4 book ai didi

c++ - 旧版 OpenGL 纹理无法正常工作

转载 作者:行者123 更新时间:2023-11-30 01:35:35 29 4
gpt4 key购买 nike

我使用旧版 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();
}
}

最佳答案

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.

所以移动 glBindTexture() 调用几行直到 glBegin() 之前。

关于c++ - 旧版 OpenGL 纹理无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53858975/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com