gpt4 book ai didi

c++ - 时髦的 OpenGL 立方体

转载 作者:行者123 更新时间:2023-11-28 07:47:43 32 4
gpt4 key购买 nike

啊哈!看来我的问题是我给 gluPerspective 的 zNear 值必须大于 0,而且我必须启用深度缓冲区才能使其工作。我更新了下面的代码以使其正常工作。

我尝试过很多次这样做,并且一直认为我以错误的顺序定义了我的四边形顶点,但现在,我知道这是另外一回事了。

我试过启用剔除、将正面更改为顺时针、禁用混合、添加法线,但我总是得到一个看起来像这样的立方体;

what the cube looks like

希望您甚至不必查看我的代码就知道问题出在哪里,因为这样解决问题并不难。

如果您不能立即知道问题出在哪里,这里是用于设置和绘制立方体的代码。

// FIXED CODE.
// reshape, called on init, and window resize
void reshape(int w, int h) {
scrw=w;
scrh=h;
glClearColor(0.8,0.8,0.8,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(cfov,(float) scrw/ (float) scrh,1,1000); // this is also a part of the fix.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST); // this is a part of the fix
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glViewport(0,0,scrw,scrh);
}
// drawQuadCube(), called every frame.
void drawQuadCube() {
glPushMatrix();
glTranslated(0.5,0.5,0.5);
glRotated(xangle,0,1,0);
glRotated(yangle,1,0,0);
glRotated(zangle,0,0,1);
glTranslated(-0.5,-0.5,-0.5);
glBegin(GL_QUADS);
// bottom
glColor4ub(30,30,255,255);
glVertex3f(0.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
// top
glColor4ub(40,40,255,255);
glVertex3f(0.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
// left
glColor4ub(60,60,255,255);
glVertex3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 1.0f);
// right
glColor4ub(60,60,200,255);
glVertex3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
// near
glColor4ub(70,70,100,255);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
// far
glColor4ub(20,20,90,255);
glVertex3f(0.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(0.0f, 1.0f, 1.0f);
glNormal3f(0,0,0);
glNormal3f(0,0,1);
glNormal3f(0,1,0);
glNormal3f(1,0,0);
glNormal3f(1,0,1);
glNormal3f(1,1,0);
glNormal3f(1,1,1);
glNormal3f(0,1,1);
glEnd();
glPopMatrix();

}
// if that isn't enough, this is the function used to set up the view.
void setView(void) {
glLoadIdentity();
gluLookAt(0.5,0.5,-5,0.5,0.5,0.5,0,1,0);
}

最佳答案

您的绕线方式不正确。

glFrontFace 默认为 GL_CCW,但是你的“前置四边形”,在这个例子中是“近”的四边形,是顺时针方向缠绕的(从你的相机位置的引用框架;注意它在负 Z 轴上,并且沿着正 Z)。 glCullFace 默认为 GL_BACK,因此它会被剔除。正确设置:

glFrontFace(GL_CW);

另见 http://www.opengl.org/sdk/docs/man/xhtml/glFrontFace.xml

完成该设置后,您将需要启用深度缓冲,这样您的四边形就可以正确地重绘,而无需依赖绘画顺序。请参阅:http://www.opengl.org/archives/resources/faq/technical/depthbuffer.htm

关于c++ - 时髦的 OpenGL 立方体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14551929/

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