gpt4 book ai didi

c++ - 使用OpenGL进行多边形的透视投影

转载 作者:行者123 更新时间:2023-12-02 10:15:28 28 4
gpt4 key购买 nike

我正在尝试使用open-GL实现透视投影
但是当我应用gluPerspective(0,0.5,0.5,5)方法时,多边形未在透 View 中显示,而在正交 View 中显示
这是输出
enter image description here
任何人都可以帮助
我的代码:

#include<GL/glut.h>
float angle = 2;
void myinit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 5);
//glFrustum(-0.5, 2.4, -0.5, 0.5, -0.5, 0.5);
//glFrustum(-5.0, 5.0, -5.0, 5.0, 5, 100);
gluPerspective(0,0.5,0.5,5);
}

void polygon(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
//glLineWidth(2);
//glRotatef(angle, 0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
//glVertex3f(0, 0.5, 0.0);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(50, 100);
glutInitWindowSize(1000, 1000);
glutCreateWindow("Open Gl 2D Geometric Transformation");
myinit();
glutDisplayFunc(polygon);
glutMainLoop();


return 0;
}

最佳答案

gluPerspective 的第一个参数是错误的:

gluPerspective(0,0.5,0.5,5);


gluPerspective 的第一个参数是垂直视角,以度为单位。该值必须大于0.0且小于180。0.0是无效的参数,会导致未定义的行为。
该指令可能根本没有设置矩阵。

无论如何,如果设置了正确的 Angular ,则几何将被裁剪。透视投影矩阵定义 Viewing frustum。裁剪不在票价平面和票价平面之间的所有几何图形。在您的情况下,近平面为0.5,远平面为5.0。



设置 View 矩阵,并通过沿负z轴移动几何图形在近平面和远平面之间转换。例如(0,0,-2.5)。
gluPerspective的第二个参数是宽高比。由于窗口的大小为1000x1000,因此宽高比必须为1.0:

void myinit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

GLdouble fov = 90.0; // 90 degrees
GLdouble aspect = 1.0;
GLdouble near_dist = 0.5;
GLdouble far_dist = 5.0;
gluPerspective(fov, aspect, near_dist, far_dist);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -2.5f); // near_dist < 2.5 < far_dist
}

关于c++ - 使用OpenGL进行多边形的透视投影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62140700/

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