获取glFrustum的坐标

Getting coordinates for glFrustum

本文关键字:坐标 glFrustum 获取      更新时间:2023-10-16

我刚开始阅读关于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.01.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为85perspective_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也在这里、这里、这里和这里进行了解释,最重要的是在这里。

这里还有一个很好的解释。