如何制作一个开放的立方体(缺少一个表面)而不影响在 OpenGL 中镜像或切换边的位置?
How do I make an open cube (missing one surface) without getting effect where sides are mirrored or switched in OpenGL?
我正在尝试创建一个缺少一个表面的立方体(实际上是一个打开的盒子),但是当我创建它时,与缺少的一侧相对的一侧会超级强加到对象时开口应该在的位置旋转。
我的意思是:
立方体正面和背面角度视图:
如何摆脱奇怪的颜色覆盖?
这是我当前的代码:
#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);
我正在尝试创建一个缺少一个表面的立方体(实际上是一个打开的盒子),但是当我创建它时,与缺少的一侧相对的一侧会超级强加到对象时开口应该在的位置旋转。
我的意思是:
立方体正面和背面角度视图:
如何摆脱奇怪的颜色覆盖?
这是我当前的代码:
#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);