旧版OpenGL纹理无法正常工作
Legacy OpenGL Textures not working correctly
我是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()
之前排队。
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- sdl软件渲染器不工作,工作在硬件加速的一个
- C++程序已停止工作-求解常微分方程