glFrustum,gluPerspective和FOV

问题描述 投票:2回答:2

我有一个程序可以做增强现实,在OpenGL中我使用:

glFrustum(-near*centerImageX/(GLfloat)fx, near*(imageWidth-centerImageX)/(GLfloat)fx, near*(centerImageY-imageHeight)/(GLfloat)fy, near*centerImageY/(GLfloat)fy, near, far);

可以,我有很好的视角,并且3D对象很好地插入了我的照片中。现在,我希望能够放大/缩小。通常,这是通过在gluPerspective中更改fov来完成的,但是我不使用gluPerspective,因为我无法很好地插入。

使用http://dmi.uib.es/~josemaria/files/OpenGLFAQ/transformations.htm,问题“ 9.085如何对glFrustum()进行调用,使其与对gluPerspective()的调用相匹配?”,我尝试使用:

fov=360.0*atan(imageHeight/(2*fy))/Pi;  //computed with parameters top and bottom of my glFrustum
aspect=-centerImageY*fx/(fy*(centerImageX-imageWidth)); //computed with parameters left and bottom of my glFrustum

void glFrustum(GLdouble左,GLdouble右,GLdouble底部,GLdouble顶部,GLdouble靠近,GLdouble远离);

void gluPerspective(GLdouble fovy,GLdouble Aspect,GLdouble near,GLdouble far);

然后:

gluPerspective(fov, aspect, near, far);

但是我的对象变形,未在所有轴上正确缩放,未保持比例。

那么,我需要在glFrustum参数中做/修改什么才能获得放大/缩小效果?

opengl glut augmented-reality perspectivecamera frustum
2个回答
0
投票

通常,不能通过更改gluPerspective()中的fov来完成。

通常,对此您需要将变换应​​用于模型和/或视图矩阵。如果要移动模型,通常可以在“模型矩阵”中进行。如果要更改“相机位置”,通常可以在“视图矩阵”中进行。

在您的情况下,您可能会翻译视图矩阵。这样可以解决3D对象移开或靠近相机的幻觉。

如果您仍在使用固定功能管道,则可以通过调用以下某些功能来执行这些更改。

  • glMatrixMode(x);-其中x为GL_PROJECTION_MATRIXGL_MODELVIEW_MATRIX
  • glLoadIdentity();-“重置”当前选定的矩阵。
  • glTranslate*(x, y, z);
  • glRotate*(angle, x, y, z);
  • glScale*(x, y, z);

但是,如果您使用的是现代OpenGL,则不要使用以上(您实际上也不能使用)。相反,您想自己计算所有矩阵变换并将它们传递给着色器。如果您不想自己计算所有这些高级矩阵运算,则可以获得类似GLM(OpenGL Mathematics)的信息。


0
投票

我不是OpenGL程序员。我的兴趣主要在于代数射影几何。

根据我的理解,OpenGL函数GLFrustum()中投影矩阵的几何含义,已在OpenGL官方指南(本书的第9版,OpenGL编程指南-学习OpenGL的官方指南)中进行了说明从代数的角度来看,实际上对应于许多几何意义,这些意义与作者所期望的不同。

它们之间的显着区别是:1.在官方指南中,作者正在说明一个复合线性变换,其因素之一为central projection,因此最终的复合透视投影矩阵应为奇异或退化矩阵;2.虽然仍在官方指南中,但在附录中,GLFrustum()透视投影矩阵是非奇异的4乘4方阵!

注意:作者正在尝试将非奇异矩阵解释为理论上奇异的矩阵!

以下矩阵分解(不是唯一的)对应于非奇异GLFrustum()矩阵的几何含义之一:enter image description here

乳胶配方代码:

$$n\underbrace{\color{blue}\left[
\begin{array}{cccc}
 1 & 0 & 0 & 0 \\[12pt]
 0 & 1 & 0 & 0 \\[12pt]
 0 & 0 &\dfrac{n+f}{(n-f) n} & 0 \\[12pt]
 0 & 0 & 0 & 1 \\[12pt]
\end{array}
\right]}_{\color{red}(1)}\cdot \underbrace{\color{blue}\left[
\begin{array}{cccc}
\dfrac{ -2}{l-r} & 0 & 0 & \dfrac{l+r}{l-r} \\[12pt]
 0 & \dfrac{ -2}{b-t} & 0 & \dfrac{b+t}{b-t} \\[12pt]
 0 & 0 & 1 & 0 \\[12pt]
 0 & 0 & 0 & 1 \\[12pt]
\end{array}
\right]}_{\color{red}(2)}\cdot\underbrace{\color{blue}\left[
\begin{array}{cccc}
 1 & 0 & 0 & 0 \\[12pt]
 0 & 1 & 0 & 0 \\[12pt]
 0 & 0 & 1 & 0 \\[12pt]
 0 & 0 & -\dfrac{1}{n} & 1 \\[12pt]
\end{array}
\right]}_{\color{red}(3)}\cdot\underbrace{\color{blue}\left[
\begin{array}{cccc}
 1 & 0 & 0 & 0 \\[12pt]
 0 & 1 & 0 & 0 \\[12pt]
 0 & 0 & 1 & 1 \\[12pt]
 0 & 0 & 0 & \dfrac{1}{n} \\[12pt]
\end{array}
\right]}_{\color{red}(4)}\cdot\underbrace{\color{blue}\left[
\begin{array}{cccc}
 1 & 0 & 0 & 0 \\[12pt]
 0 & 1 & 0 & 0 \\[12pt]
 0 & 0 & 1 & 0 \\[12pt]
 0 & 0 & 0 & \dfrac{2 n f}{f+n} \\[12pt]
\end{array}
\right]}_{\color{red}(5)} $$

[2]以外的所有上述矩阵因数,在Unified Framework of Elementary Geometric Transformations中均明确定义了它们的几何意义。如果选择此因式分解作为GLFrustum()透视投影矩阵的几何含义解释,则必须确保您在代码中进行的任何计算或变换均与其几何含义一致。

因此,当您使用OpenGL GLFrustum()进行编程时,可能必须与官方指南中说明的内容以及从纯代数投影几何的角度来看GLFrustum()透视投影矩阵的真正含义进行比较,并使用您自己的任何一个。

© www.soinside.com 2019 - 2024. All rights reserved.