设置 3D OpenGL 透视投影的最简单方法

编程入门 行业动态 更新时间:2024-10-25 22:30:08
本文介绍了设置 3D OpenGL 透视投影的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有很多教程都建议将 gluPerspective 或 glFrustum 与其他东西结合使用,但我在设置正确的矩阵时遇到了困难.设置向下看 +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();

推荐答案

这是最小的绘图代码:

Here's the minimal drawing code: 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 透视投影的最简单方法

本文发布于:2023-07-17 17:08:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1135386.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最简单   透视   方法   OpenGL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!