OpenGL 在实心球体上绘制透明球体
OpenGL draw transparent sphere over solid sphere
我有一个带有地球纹理的小球体,我想要一个稍微大一点的球体,上面有云层纹理,它是透明的。
我画的对象是这样的
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPushMatrix();
auto sphere = gluNewQuadric();
gluQuadricDrawStyle(sphere, GLU_FILL);
gluQuadricTexture(sphere, true);
gluQuadricNormals(sphere, GLU_SMOOTH);
th4->bind(); // Cloud texture
gluSphere(sphere, 0.42, 32, 32);
glPopMatrix();
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glPushMatrix();
sphere = gluNewQuadric();
gluQuadricDrawStyle(sphere, GLU_FILL);
gluQuadricTexture(sphere, true);
gluQuadricNormals(sphere, GLU_SMOOTH);
th2->bind(); // Earth texture
gluSphere(sphere, 0.4, 32, 32);
glPopMatrix();
glutSwapBuffers();
我这样初始化过剩
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
问题是,如果我按这个顺序绘制它们,我的地球球会出现在云纹理球的前面,如果我交换绘制顺序,我就再也无法透过我的云球看到我的地球球了。
更新代码:
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glPushMatrix();
auto sphere = gluNewQuadric();
gluQuadricDrawStyle(sphere, GLU_FILL);
gluQuadricTexture(sphere, true);
gluQuadricNormals(sphere, GLU_SMOOTH);
th2->bind();
gluSphere(sphere, 0.4, 32, 32);
glPopMatrix();
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPushMatrix();
sphere = gluNewQuadric();
gluQuadricDrawStyle(sphere, GLU_FILL);
gluQuadricTexture(sphere, true);
gluQuadricNormals(sphere, GLU_SMOOTH);
th4->bind();
gluSphere(sphere, 0.42, 32, 32);
glPopMatrix();
glDisable(GL_BLEND);
执行以下操作:
- 启用backface culling
- 启用depth test
- 禁用blending
- 绘制实心地球
- 启用blending
- 绘制透明的云彩
我有一个带有地球纹理的小球体,我想要一个稍微大一点的球体,上面有云层纹理,它是透明的。
我画的对象是这样的
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPushMatrix();
auto sphere = gluNewQuadric();
gluQuadricDrawStyle(sphere, GLU_FILL);
gluQuadricTexture(sphere, true);
gluQuadricNormals(sphere, GLU_SMOOTH);
th4->bind(); // Cloud texture
gluSphere(sphere, 0.42, 32, 32);
glPopMatrix();
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glPushMatrix();
sphere = gluNewQuadric();
gluQuadricDrawStyle(sphere, GLU_FILL);
gluQuadricTexture(sphere, true);
gluQuadricNormals(sphere, GLU_SMOOTH);
th2->bind(); // Earth texture
gluSphere(sphere, 0.4, 32, 32);
glPopMatrix();
glutSwapBuffers();
我这样初始化过剩
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
问题是,如果我按这个顺序绘制它们,我的地球球会出现在云纹理球的前面,如果我交换绘制顺序,我就再也无法透过我的云球看到我的地球球了。
更新代码:
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glPushMatrix();
auto sphere = gluNewQuadric();
gluQuadricDrawStyle(sphere, GLU_FILL);
gluQuadricTexture(sphere, true);
gluQuadricNormals(sphere, GLU_SMOOTH);
th2->bind();
gluSphere(sphere, 0.4, 32, 32);
glPopMatrix();
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPushMatrix();
sphere = gluNewQuadric();
gluQuadricDrawStyle(sphere, GLU_FILL);
gluQuadricTexture(sphere, true);
gluQuadricNormals(sphere, GLU_SMOOTH);
th4->bind();
gluSphere(sphere, 0.42, 32, 32);
glPopMatrix();
glDisable(GL_BLEND);
执行以下操作:
- 启用backface culling
- 启用depth test
- 禁用blending
- 绘制实心地球
- 启用blending
- 绘制透明的云彩