gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:40:48 25 4
gpt4 key购买 nike

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

我的意思是:立方体正面和背面的角度 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com