我刚开始阅读有关 OpenGL
的主题,特别是关于 Viewing
的内容满足我目前的需求。我正在尝试理解 glFrustum
以了解我在屏幕上绘制的对象的透视投影,据我所知,glFrustum
会使较远的对象小于较近的对象。< br/>
我在这里使用 openFrameworks
来绘制对象,例如 Image
/Box
等,这是我正在尝试做的一个简短示例:
openFrameworks 绘制方法
draw()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 3.0, 500.0);
glMatrixMode(GL_MODELVIEW);
sceneImage.draw(0,0,ofGetWidth(), ofGetHeight()); //draw an image on the screen with window's width and dheight
}
使用上面的 glFrustum,我只是想剪裁图像并查看图像的特定部分以进行初始测试。我不确定 glFrustum
的那些参数实际上是什么,即使我从上面的红皮书链接中读到 glFrustum
也是如此。参数当然是 left, right, bottom, top, near, far
,但是 1.0
或 1.0
是如何计算的?我期待屏幕坐标进入那里并尝试类似 glFrustum(100, 984, 100, 668,3, 500)
但屏幕上没有任何显示。 glFrustum
的参数是如何计算的?
最佳答案
我希望我的问题是正确的,如果不只是让我知道,但如果你问“你应该如何计算 glFrustum
参数”这就是:
它实际上全都与宽高比和视野有关。通常您会找到有关如何使用垂直 fov 制作透视矩阵的信息,但有时您会想要使用水平 fov。所以这是我的想法,我不使用 glFrustum
但我认为您可以简单地用它切换我的 perspective
函数,它应该仍然有效:
//--------------------------------------------------------------------------------
// set a perspective frustum (right hand)
// (left, right, bottom, top, near, far)
//--------------------------------------------------------------------------------
void perspective(float l, float r, float b, float t, float n, float f)
{
m_projection.identity();
m_projection[0] = 2.0f * n / (r - l);
m_projection[2] = (r + l) / (r - l);
m_projection[5] = 2.0f * n / (t - b);
m_projection[6] = (t + b) / (t - b);
m_projection[10] = -(f + n) / (f - n);
m_projection[11] = -(2.0f * f * n) / (f - n);
m_projection[14] = -1.0f;
m_projection[15] = 0.0f;
update();
}
//--------------------------------------------------------------------------------
// set a symmetric perspective frustum
// ((vertical, degrees) field of view, (width/height) aspect ratio, near, far)
//--------------------------------------------------------------------------------
void perspective_vertical(float fov, float aspect, float front, float back)
{
fov = DEG_TO_RAD(fov); // transform fov from degrees to radians
float tangent = tanf(fov / 2.0f); // tangent of half vertical fov
float height = front * tangent; // half height of near plane
float width = height * aspect; // half width of near plane
perspective(-width, width, -height, height, front, back);
}
//--------------------------------------------------------------------------------
// set a symmetric perspective frustum
// ((horizontal, degrees) field of view, (width/height) aspect ratio, near, far)
//--------------------------------------------------------------------------------
void perspective_horizontal(float fov, float aspect, float front, float back)
{
fov = DEG_TO_RAD(fov); // transform fov from degrees to radians
fov = 2.0f * atanf(tanf(fov * 0.5f) / aspect); // transform from horizontal fov to vertical fov
float tangent = tanf(fov / 2.0f); // tangent of half vertical fov
float height = front * tangent; // half height of near plane
float width = height * aspect; // half width of near plane
perspective(-width, width, -height, height, front, back);
}
还有帮助宏:
#define PI_OVER_180 0.0174532925199432957692369076849f
#define 180_OVER_PI 57.2957795130823208767981548141f
#define DEG_TO_RAD(x) (x * PI_OVER_180)
#define RAD_TO_DEG(x) (x * 180_OVER_PI)
代码大部分都带有注释,无需进一步解释就应该能理解。参数应该是这样的:
perspective_horizontal(85.0f /* fov of 85 degrees */, (float)width/height, 0.001f /* using near of 3.0f is kinda too much, just don't use 0.0f */, 1000.0f)
如果您想更深入地了解并实际查看数字,您可以放置一些断点或 printf
以查看其工作原理。水平 85 度相当于垂直 45 度。此外,opengl 使用主列,因此如果您最终使用这样的矩阵而不是 glFrustum
,请确保先转置它。
编辑(更多关于下面的评论):
让我们以标准高清 -400 像素的窗口为例:(1920-400) 宽和 (1080-400) 高。标准高清的纵横比为 1.77,但 -400 像素版本为 (1920-400)/(1080-400) = 2.23。
现在调用 perspective_horizontal
函数,纵横比为 (1920-400)/(1080-400)
,fov 为 85
并放置perspective(...)
调用之前的断点将为我们提供以下变量:
- fov float 0.778087676
- 纵横 float 2.2352941
- 前面的 float 0.00100000005
- 后浮100
- 切线 float 0.40993762
- 高度 float 0.000409937638
- 宽度 float 0.000916331192
请注意 0.000916331192/0.000409937638 = 2.23529412052 和 0.778087676 弧度转为度 = 44.5811399 垂直度,相当于 85 度水平。
同时调用 perspective_horizontal
函数,纵横比为 (1920-400)/(1080-400)
,fov 为 105
将为我们提供以下变量:
- fov float 1.05568409
- 纵横 float 2.2352941
- 前面的 float 0.00100000005
- 后浮100
- 切线 float 0.583021879
- 高度 float 0.000583021902
- 宽度 float 0.00130322541
再次注意 0.00130322541/0.000583021902 = 2.23529408677 和 1.05568409 弧度转换为度数 = 60.4862429 度垂直,相当于 105 度水平。
至于实际的透视矩阵,我无法向你解释这个公式是如何工作的,但想象一下 gpu 中有神奇的 unicorn ,如果你给它们喂食 gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4( inputPosition, 1.0f);
他们会让一些魔法发生,它会在屏幕上显示美丽的东西 ;3.
glFrustum
也有解释 here , here , here , here , here最重要的是 here .
还有一个很好的解释here .
关于c++ - 获取 glFrustum 的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16398463/