作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个程序,在 OpenGL 上绘制一个立方体,并通过单击鼠标连续旋转它。在特定角度,我能够看穿立方体(透明)。我已经启用了深度测试,所以我不知道为什么会发生这种情况。我不确定我是否正确启用了它。
#include <math.h>
#include <vector>
#include <Windows.h>
#include <gl\glut.h>
using namespace std;
void myInit() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0, 0, 0, 1);
glOrtho(-2, 2, -2, 2, 2, -2);
glMatrixMode(GL_MODELVIEW);
}
float Cube[][3] = { {-1, -1, -1}, {1, -1, -1}, {1, 1, -1}, {-1, 1, -1}, {-1, -1, 1}, {1, -1, 1}, {1, 1, 1}, {-1, 1, 1} };
float Colors[][3] = { {0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {1, 1, 0}, {0, 1, 1}, {1, 0, 1}, {1, 1, 1} };
int axis = 0, theta[3] = {0, 0, 0};
void draw_face (int a, int b, int c, int d) {
glBegin(GL_QUADS);
glColor3fv(Colors[a]);
glVertex3fv(Cube[a]);
glColor3fv(Colors[b]);
glVertex3fv(Cube[b]);
glColor3fv(Colors[c]);
glVertex3fv(Cube[c]);
glColor3fv(Colors[d]);
glVertex3fv(Cube[d]);
glEnd();
}
void draw_cube () {
draw_face(0, 3, 2, 1);
draw_face(2, 3, 7, 6);
draw_face(0, 4, 7, 3);
draw_face(1, 2, 6, 5);
draw_face(4, 5, 6, 7);
draw_face(0, 1, 5, 4);
}
void spin_cube() {
theta[axis] += 2;
if (theta[axis] > 360)
theta[axis] = -360;
glutPostRedisplay();
}
void idle_func() {
Sleep(10);
spin_cube();
}
void mouse_func(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
axis = 0;
else if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
axis = 1;
else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
axis = 2;
}
void myDrawing() {
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(theta[0], 1, 0, 0);
glRotatef(theta[1], 0, 1, 0);
glRotatef(theta[2], 0, 0, 1);
draw_cube();
glPopMatrix();
glFlush();
glutSwapBuffers();
}
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glEnable(GL_DEPTH_TEST);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("sample");
glutDisplayFunc(myDrawing);
glutIdleFunc(idle_func);
glutMouseFunc(mouse_func);
myInit();
glutMainLoop();
}
最佳答案
多个问题:
您没有向 GLUT 请求深度缓冲区:
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
解决方案:或在 GLUT_DEPTH
中确保 GLUT 在 GL 上下文创建期间从操作系统请求一些深度缓冲区位:
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
您在 GLUT 创建 GL 上下文之前调用 glEnable(GL_DEPTH_TEST)
:
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
// no GL context yet
glEnable(GL_DEPTH_TEST);
解决方案:将 glEnable()
移至 glutCreateWindow()
之后,以便它具有可使用的当前 GL 上下文:
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("sample");
glEnable(GL_DEPTH_TEST);
...
您永远不会清除深度缓冲区:
void myDrawing() {
// where's GL_DEPTH_BUFFER_BIT?
glClear(GL_COLOR_BUFFER_BIT);
...
解决方案:或在GL_DEPTH_BUFFER_BIT
中与您的glClear()
参数:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
所有修复一起:
#include <cmath>
#include <vector>
#include <GL/glut.h>
using namespace std;
void myInit() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0, 0, 0, 1);
glOrtho(-2, 2, -2, 2, 2, -2);
glMatrixMode(GL_MODELVIEW);
}
float Cube[][3] = { {-1, -1, -1}, {1, -1, -1}, {1, 1, -1}, {-1, 1, -1}, {-1, -1, 1}, {1, -1, 1}, {1, 1, 1}, {-1, 1, 1} };
float Colors[][3] = { {0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {1, 1, 0}, {0, 1, 1}, {1, 0, 1}, {1, 1, 1} };
int axis = 0, theta[3] = {0, 0, 0};
void draw_face (int a, int b, int c, int d) {
glBegin(GL_QUADS);
glColor3fv(Colors[a]);
glVertex3fv(Cube[a]);
glColor3fv(Colors[b]);
glVertex3fv(Cube[b]);
glColor3fv(Colors[c]);
glVertex3fv(Cube[c]);
glColor3fv(Colors[d]);
glVertex3fv(Cube[d]);
glEnd();
}
void draw_cube () {
draw_face(0, 3, 2, 1);
draw_face(2, 3, 7, 6);
draw_face(0, 4, 7, 3);
draw_face(1, 2, 6, 5);
draw_face(4, 5, 6, 7);
draw_face(0, 1, 5, 4);
}
void spin_cube() {
theta[axis] += 2;
if (theta[axis] > 360)
theta[axis] = -360;
glutPostRedisplay();
}
void idle_func() {
Sleep(10);
spin_cube();
}
void mouse_func(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
axis = 0;
else if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
axis = 1;
else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
axis = 2;
}
void myDrawing() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(theta[0], 1, 0, 0);
glRotatef(theta[1], 0, 1, 0);
glRotatef(theta[2], 0, 0, 1);
draw_cube();
glPopMatrix();
glFlush();
glutSwapBuffers();
}
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("sample");
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(myDrawing);
glutIdleFunc(idle_func);
glutMouseFunc(mouse_func);
myInit();
glutMainLoop();
}
关于c++ - 如何在 OpenGL 上使立方体面不透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58936951/
我是一名优秀的程序员,十分优秀!