gpt4 book ai didi

opengl - 在 OpenGL 中设置相机和视锥体

转载 作者:行者123 更新时间:2023-12-01 12:54:03 26 4
gpt4 key购买 nike

为什么没有渲染四边形?我尝试过使用 glOrtho 和 glFrustrum,输出是一样的:黑屏。据我了解,这段代码表示相机设置在位置 (0,0,-3) 并且它看着原点。近平面位于 -2(距离相机 1 m)。我做错了什么?

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
//glOrtho (-5,5,-5,5,-5,5);
glFrustum(-5,5,-5,5,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,-3,0,0,0,0,1,0);

glColor3f(1.0,1.0,1.0);
glBegin(GL_QUADS);
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();
// Don't wait start processing buffered OpenGL routines
glFlush();
glutSwapBuffers();

最佳答案

它对我来说工作正常,一定是你代码的另一部分有问题。

这是我使用的完整代码:

#include <GL/glut.h>

void display(void) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
//glOrtho (-5,5,-5,5,-5,5);
glFrustum(-5,5,-5,5,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,-3,0,0,0,0,1,0);

glColor3f(1.0,1.0,1.0);
glBegin(GL_QUADS);
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();
// Don't wait start processing buffered OpenGL routines
glFlush();
glutSwapBuffers();

}

int main(int argc, char **argv) {
glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(200,200);
glutInitWindowSize(512,512);
glutCreateWindow("Test");
glutDisplayFunc(display);

glutMainLoop();

return 0;
}

我是这样编译的:

gcc test.cpp -lGL -lglut -lGLU -o test

截图如下:

enter image description here

关于opengl - 在 OpenGL 中设置相机和视锥体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10669460/

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