c++ - 如何制作一个开放的立方体(缺少一个表面)而不影响在 OpenGL 中镜像或切换边的位置?

标签 c++ opengl glut

我正在尝试创建一个缺少一个表面的立方体(实际上是一个打开的盒子),但是当我创建它时,与缺少的一面相对的一面会在对象旋转时强加到开口应该在的位置.

我的意思是: 立方体正面和背面的角度 View :enter image description here Front of cube

如何摆脱奇怪的颜色覆盖?

这是我当前的代码:

#include <stdio.h>
#include <stdarg.h>
#include <math.h>
#include <stdlib.h>
#include <GL/glut.h>


void display();
void viewButtons();


double rotationY = 0;
double rotationX = 0;

void display()
{
 //Clear the screen and clear z-buffer
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
    glLoadIdentity();

// Rotate when user changes rotate_x and rotate_y
    glRotatef(rotationX, 1.0, 0.0, 0.0);
    glRotatef(rotationY, 0.0, 1.0, 0.0);
    glBegin(GL_POLYGON);
//Vertices and colors
    glColor3f(1.0, 0.0, 0.0);     
glVertex3f(0.5, -0.5, -0.5);      // P1 is red
glColor3f(0.0, 1.0, 0.0);    
glVertex3f(0.5, 0.5, -0.5);      // P2 is green
glColor3f(0.0, 0.0, 1.0);     
glVertex3f(-0.5, 0.5, -0.5);      // P3 is blue
glColor3f(1.0, 0.0, 1.0);     
glVertex3f(-0.5, -0.5, -0.5);      // P4 is purple

glEnd();

/*// White side - BACK
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glEnd();*/

// Purple side - RIGHT
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, -0.5, 0.5);
glEnd();

// Green side - LEFT
glBegin(GL_POLYGON);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, -0.5, -0.5);
glEnd();

// Blue side - TOP
glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, 0.5);
glEnd();

// Red side - BOTTOM
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, -0.5);
glEnd();

glFlush();
glutSwapBuffers();


}

void viewButtons(int button, int x, int y)
{
if (button == GLUT_KEY_RIGHT)
    rotationY += 5;

else if (button == GLUT_KEY_LEFT)
    rotationY -= 5;

else if (button == GLUT_KEY_UP)
    rotationX += 5;

else if (button == GLUT_KEY_DOWN)
    rotationX -= 5;

//update display
glutPostRedisplay();
}
int main(int argc, char **argv) {

// init GLUT and create window
// init GLUT and create Window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(720, 640);
glutCreateWindow("Room with objects");

// register callbacks
glutDisplayFunc(display);
glutSpecialFunc(viewButtons);
// enter GLUT event processing cycle
glutMainLoop();

return 1;
}

最佳答案

您需要启用深度测试。把这个放在你程序的某个地方,在初始化中:

glEnable(GL_DEPTH_TEST);

关于c++ - 如何制作一个开放的立方体(缺少一个表面)而不影响在 OpenGL 中镜像或切换边的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28664558/

相关文章:

c++ - Visual Studio下编译调试GLUT

c++ - 用cuda计算一张图片,直接用OpenGL显示

c++ - RegQueryValueEx 的奇怪字符

c++ - 我的循环链表中的 remove 方法是否定义明确?

c++ - 在 opengl 应用程序中不断滞后

c++ - XCode 中的 libSOIL 链接器错误

c++ - OpenGL/GLUT - 更新多个窗口

c++ - 提取角度和轴的信息

c++ - 按降序对这些元素进行排序?

java - 为什么多边形可以显示但点却不能显示