本文介绍了设置 3D OpenGL 透视投影的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多教程都建议将 gluPerspectiveglFrustum 与其他东西结合使用,但我在设置正确的矩阵时遇到了困难.设置向下看 +z 轴的 45° 透视图需要什么代码?

There have been many tutorials where each suggests using gluPerspective or glFrustum with a combination of other things, yet I've had difficulties setting up the right matrix. What code do I need to set up a 45˚ perspective view looking down the +z axis?

到目前为止我有:

glShadeModel(GL_SMOOTH);
glClearColor(0,0,0,0);
glClearDepth(1);
glDepthFunc(GL_LEQUAL);
glViewport(0,0,width,height);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,1,0.1,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

但这似乎不起作用.当我尝试绘制东西时,我得到的只是黑屏.

But that doesn't seem to work. All I get is a black screen when I attempt to draw things.

这是最小的绘图代码:

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3ub(255,255,255);
glBegin(GL_TRIANGLE_STRIP);
 glVertex3f(20,20,20);
 glVertex3f(20,30,20);
 glVertex3f(30,20,20);
 glVertex3f(30,30,20);
glEnd();

推荐答案

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3ub(255,255,255);
glBegin(GL_TRIANGLE_STRIP);
 glVertex3f(20,20,20);
 glVertex3f(20,30,20);
 glVertex3f(30,20,20);
 glVertex3f(30,30,20);
glEnd();

您的顶点坐标位于视域之外.首先,OpenGL 默认向下看"负 Z 轴,因此您的 Z 坐标必须为 -100 <<-0.1 为您选择的近和远剪裁平面.

Your vertex coordinates lie way outside the viewing volume. First, OpenGL by default "looks" down the negative Z axis, so your Z coordinates must be -100 < z < -0.1 for your choosen near and far clip plane.

但即使您翻转 Z 坐标上的符号,您的顶点仍会位于 45° FOV 之外.(20, 0, 20) 与视轴成 45°,而 (30, 0, 20) 更远.尝试将顶点坐标集中在 (0,0,-5) 附近,例如 (+/-1, +/-1, -5)

But even if you flipped the sign on the Z coordinate, your vertices still would lie outside the 45° FOV. (20, 0, 20) is 45° from the viewing axis, and (30, 0, 20) even farther. Try centering your vertex coodinates around (0,0,-5) like (+/-1, +/-1, -5)

这篇关于设置 3D OpenGL 透视投影的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 15:56