获取glFrustum的坐标
Getting coordinates for glFrustum
我刚开始阅读关于OpenGL
的主题,特别是关于满足我当前需求的Viewing
。我试图将glFrustum
理解为我在屏幕上绘制的对象的透视投影,正如我所理解的,glFrustum
会使更远的对象比更近的对象更小
我在这里使用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使用了column-major,所以如果您最终使用这样的矩阵而不是glFrustum
,请确保首先将其转置。
编辑(更多关于下面的评论(:
让我们取一个标准高清-400像素的窗口:(1920-400(宽和(1080-400(高。标准高清的纵横比是1.77,但-400像素版本是(1920-400(/(1080-400(=2.23。
现在调用长宽比为(1920-400)/(1080-400)
、fov为85
的perspective_horizontal
函数,并在perspective(...)
调用之前设置断点,将为我们提供以下变量:
- fov浮子0.778087676
- 纵横比浮动2.2352941
- 前浮子0.0010000005
- 后浮子100
- 切线浮动0.40993762
- 高度浮子0.000409937638
- 宽度浮动0.000916331192
请注意,0.000916331192/000409937638=2.23529412052和0.778087676弧度到度=垂直44.5811399度,相当于水平85度。
同样调用长宽比为(1920-400)/(1080-400)
的perspective_horizontal
函数,105
的fov将给我们以下变量:
- fov浮子1.055568409
- 纵横比浮动2.2352941
- 前浮子0.0010000005
- 后浮子100
- 切线浮动0.583021879
- 高度浮子0.000583021902
- 宽度浮动0.001130322541
再次注意,0.001303225541/000583021902=2.23529408677和1.05568409弧度到度=60.48662429垂直度,相当于105水平度。
至于实际的透视矩阵,我无法解释这个公式是如何运作的,但只要想象一下gpu中有神奇的独角兽,如果你给它们喂食gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(inputPosition, 1.0f);
,它们就会产生一些魔法,并在屏幕上显示美丽的东西;3.
glFrustum
也在这里、这里、这里和这里进行了解释,最重要的是在这里。
这里还有一个很好的解释。
- QGraphicsPolygonItem在拖动时未更新QPolygonF坐标
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- UE4-如何在给定4个屏幕坐标的情况下缩放纹理或材质
- OpenGL 4.3 错误地将第 4 个纹理坐标映射到与第 3 个纹理坐标相同的位置
- 比较两个节点坐标的最佳方法是什么?
- 在 OpenGL 中将笛卡尔世界坐标转换为球面局部坐标
- SDL2 调整窗口大小后如何缩放鼠标坐标?
- 如何在C++中获取坐标?
- 如何将 x.y 坐标的值存储在不同的数组中
- 如何将鼠标坐标转换为"mm"
- 重心坐标并不总是有效(3d)
- 尝试渲染像素坐标时,简单线条渲染失败
- 使用 GTK3 C++获取指针坐标
- 从 CGAL 3D 网格生成中获取顶点坐标的 -6.27744e+66:mesh_implicit_sphere示例
- 我想制作设置鼠标回调函数,以便在仍然按下鼠标左键时连续传递坐标
- 将鼠标悬停在图片框上时,如何显示带有 x-y 坐标的十字准线光标?
- 跳转到通过输入坐标定义的控制台屏幕位置
- QImage::p ixel 和 QImage::setPixel 坐标超出范围误差
- 如何从 3d 对象文件中获取 3d 坐标
- 获取glFrustum的坐标