为glFrustum获取坐标 [英] Getting coordinates for glFrustum

查看:108
本文介绍了为glFrustum获取坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始阅读关于的OpenGL 主题,特别是有关的 查看 以我目前的需要。我想了解 glFrustum 的对象,我已经在屏幕上绘制的立体投影,因为我明白 glFrustum 将使更远的物体比较近的物体小。
我使用了openFrameworks 这里绘制的对象,如图片 / 等,和这里的什么,我试图做一个简单的例子:

I just started reading about OpenGL topics, specifically about Viewing for my current needs. I am trying to understand glFrustum for a perspective projection of objects that I have drawn on the screen and as I understand glFrustum would make the farther objects smaller than the nearer objects.
I am using openFrameworks here for drawing objects such as Image/Box etc. and here's a short example of what I am trying to do:

了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 从上面的红色书链接。当然,参数为左,右,底,顶,近,远,但如何为 1.0 1.0 被计算出来的?我期待屏幕坐标去那里,并试图像 glFrustum(100,984,100,668,3,500),但没有出现在屏幕上。如何为这些参数为 glFrustum 如何计算的?

With the above glFrustum, I am just trying to to clip the image and see a particular portion of the image for an initial test. I am not sure about what those arguments of glFrustum actually are even as I read about glFrustum from above link of Red book. The arguments of course are left, right, bottom, top, near, far, but how is 1.0 or 1.0 being calculated? I was expecting screen coordinates to go in there and tried something like glFrustum(100, 984, 100, 668,3, 500) but nothing appeared on the screen. How are those arguments for glFrustum calculated?

推荐答案

我希望我收到了你的问题的权利,如果不只是让我知道,但如果你问的你应该怎么计算 glFrustum 参数的这是怎么了:

I hope I got your question right, if not just let me know, but if you're asking "how you should calculate the glFrustum arguments" this is how:

其实际上是所有关于长宽比和视野。通常你会找到如何让使用垂直FOV的投影矩阵的信息,但有些时候你要使用水平视野。因此,继承人什么我想出了,我不使用 glFrustum ,但我认为你可以简单地切换我的透视功能用它,它仍然应该工作:

Its actually all about the aspect ratio and the fov. Usually you're going to find information about how to make the perspective matrix using the vertical fov, but some times you'll want to use the horizontal fov. So heres what I came up with, I don't use glFrustum but I think you can simply switch my perspective function with it and it should still work:

//--------------------------------------------------------------------------------
// 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)

在code主要是注释,应该有意义,而无需进一步解释。参数应该是大致相同的事情:

The code is mostly commented and should make sense without having to further explain it. The parameters should be something along the lines of:

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 矩阵确保你先转了。

If you want to go more in depth and actually see the numbers working you can put some break points or printf it to see how it works. The equivalent of 85 degrees horizontal is about 45 degrees vertical. Also, opengl uses column major, so if you end up using a matrix like this instead of glFrustum make sure you transpose it first.

EDIT(更多信息请下面的评论):

EDIT (more about the comment below):

让我们一个窗口,是标准的HD -400像素:(1920-400),宽(1080-400)高。一个标准的HD的纵横比为1.77,但是-400像素版本是(1920-400)/(1080-400)= 2.23

Lets take a window that is standard hd -400 pixels: (1920-400) wide and (1080-400) tall. The aspect ratio of a standard hd is 1.77 but -400 pixels version is (1920-400)/(1080-400) = 2.23.

现在调用 perspective_horizo​​ntal 函数的长宽比(1920-400)/(1080-400),的FOV 85 ,并把一个断点透视前(...)电话是想给我们以下的变量

Now calling the perspective_horizontal function with aspect ration of (1920-400)/(1080-400), fov of 85 and putting a breakpoint before the perspective(...) call is going to give us the following variables:

  • 在FOV浮动0.778087676
  • 纵横浮动2.2352941
  • 在前面浮0.00100000005
  • 回浮动100
  • 在切线浮动0.40993762
  • 在高度浮动0.000409937638
  • 在宽度浮动0.000916331192

需要注意的是0.000916331192 / 0.000409937638 = 2.23529412052和0.778087676弧度度= 44.5811399度垂直这是85度的等效水平。

take note that 0.000916331192/0.000409937638 = 2.23529412052 and 0.778087676 radians to degrees = 44.5811399 degrees vertical which is the equivalent of 85 degrees horizontal.

另外调用 perspective_horizo​​ntal 函数的长宽比(1920-400)/(1080-400),的FOV 105 ,而不是要给我们以下的变量:

Also calling the perspective_horizontal function with aspect ration of (1920-400)/(1080-400), fov of 105 instead is going to give us the following variables:

  • 在FOV浮动1.05568409
  • 纵横浮动2.2352941
  • 在前面浮0.00100000005
  • 回浮动100
  • 在切线浮动0.583021879
  • 在高度浮动0.000583021902
  • 在宽度浮动0.00130322541

请注意再次证明0.00130322541 / 0.000583021902 = 2.23529408677与1.05568409弧度度= 60.4862429度垂直它是105度的水平。

take note again that 0.00130322541/0.000583021902 = 2.23529408677 and 1.05568409 radians to degrees = 60.4862429 degrees vertical which is the equivalent of 105 degrees horizontal.

至于实际的投影矩阵,我无法解释你如何式工作原理,但试想一下,有在GPU神奇的独角兽,如果你喂它们 GL_POSITION = projectionMatrix * viewMatrix * modelMatrix * vec4(inputPosition,1.0F); 他们会做出一些神奇的发生,其将显示在屏幕上美丽的东西; 3。

As for the actual perspective matrix, I can't explain you how the formula works, but just imagine that there are magical unicorns in the gpu and if you feed them gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(inputPosition, 1.0f); they're going to make some magic happen and its going to display beautiful things on the screen ;3.

glFrustum 也解释的此处这里这里,的此处,的这里,最重要的这里

glFrustum is also explained here, here, here, here, here and most importantly here.

另外那里有一个很好的解释<一个href="http://www.scratchapixel.com/lessons/3d-advanced-lessons/perspective-and-orthographic-projection-matrix/perspective-projection-matrix/"相对=nofollow>这里。

Also theres one good explanation here.

这篇关于为glFrustum获取坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆