为什么符号在opengl投影矩阵中很重要
我正在研究一个计算机视觉问题,需要使用校准相机渲染 3D 模型。我正在编写一个函数,将校准的相机矩阵分解为模型视图矩阵和投影矩阵,但我在 opengl 中遇到了一个有趣的现象,它无法解释(至少对我来说)。
简而言之,否定投影矩阵不会导致任何内容被渲染(至少根据我的经验)。我希望将投影矩阵乘以任何标量都不会产生任何效果,因为它会转换齐次坐标,而不受缩放的影响。
以下是我认为这是出乎意料的原因;也许有人可以指出我的推理有缺陷。
想象一下下面的透视投影矩阵,它给出了正确的结果:
[ a b c 0 ]
P = [ 0 d e 0 ]
[ 0 0 f g ]
[ 0 0 h 0 ]
将其乘以相机坐标得到齐次剪辑坐标:
[x_c] [ a b c 0 ] [X_e]
[y_c] = [ 0 d e 0 ] * [Y_e]
[z_c] [ 0 0 f g ] [Z_e]
[w_c] [ 0 0 h 0 ] [W_e]
最后,为了获得归一化的设备坐标,我们将 x_c、y_c 和 z_c 除以 w_c:
[x_n] [x_c/w_c]
[y_n] = [y_c/w_c]
[z_n] [z_c/w_c]
现在,如果我们对 P 求负,则得到的结果是 :剪辑坐标应该被否定,但由于它们是齐次坐标,乘以任何标量(例如-1)不应该对生成的标准化设备坐标产生任何影响。然而,在 openGl 中,否定 P 不会导致任何内容被渲染。我可以将 P 乘以任何非负标量并获得完全相同的渲染结果,但是一旦我乘以负标量,就不会渲染任何内容。这是怎么回事?
谢谢!
I'm working on a computer vision problem which requires rendering a 3d model using a calibrated camera. I'm writing a function that breaks the calibrated camera matrix into a modelview matrix and a projection matrix, but I've run into an interesting phenomenon in opengl that defies explanation (at least by me).
The short description is that negating the projection matrix results in nothing being rendered (at least in my experience). I would expect that multiplying the projection matrix by any scalar would have no effect, because it transforms homogeneous coordinates, which are unaffected by scaling.
Below is my reasoning why I find this to be unexpected; maybe someone can point out where my reasoning is flawed.
Imagine the following perspective projection matrix, which gives correct results:
[ a b c 0 ]
P = [ 0 d e 0 ]
[ 0 0 f g ]
[ 0 0 h 0 ]
Multiplying this by camera coordinates gives homogeneous clip coordinates:
[x_c] [ a b c 0 ] [X_e]
[y_c] = [ 0 d e 0 ] * [Y_e]
[z_c] [ 0 0 f g ] [Z_e]
[w_c] [ 0 0 h 0 ] [W_e]
Finally, to get normalized device coordinates, we divide x_c, y_c, and z_c by w_c:
[x_n] [x_c/w_c]
[y_n] = [y_c/w_c]
[z_n] [z_c/w_c]
Now, if we negate P, the resulting clip coordinates should be negated, but since they are homogeneous coordinates, multiplying by any scalar (e.g. -1) shouldn't have any affect on the resulting normalized device coordinates. However, in openGl, negating P results in nothing being rendered. I can multiply P by any non-negative scalar and get the exact same rendered results, but as soon as I multiply by a negative scalar, nothing renders. What is going on here??
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,其要点是削波测试是通过以下方式完成的:
乘以负值会破坏此测试。
Well, the gist of it is that clipping testing is done through:
Multiplying by a negative value breaks this test.
我刚刚发现了这个花絮,它在寻找答案方面取得了进展:
来自红皮书,附录G:
反转投影矩阵将导致负 W 裁剪坐标,显然 opengl 不喜欢这样。但谁能解释为什么 opengl 不处理这种情况?
参考:http://glprogramming.com/red/appendixg.html
I just found this tidbit, which makes progress toward an answer:
From Red book, appendix G:
Inverting the projection matrix will result in negative W clipping coordinate, and apparently opengl doesn't like this. But can anyone explain WHY opengl doesn't handle this case?
reference: http://glprogramming.com/red/appendixg.html
我能想到的原因:
因此 ,既然你提到使用相机矩阵并且你已经反转了投影矩阵,我不得不问......你将相机矩阵中的内容应用到哪些矩阵?对投影矩阵进行操作保存近/远/fovy/aspect 会导致深度缓冲区中出现各种问题,包括使用 z 的任何内容(深度测试、面剔除等)。
有关转换的 OpenGL 常见问题解答部分提供了更多详细信息。
Reasons I can think of:
Also, since you mentioned using a camera matrix and that you have inverted the projection matrix, I have to ask... to which matrices are you applying what from the camera matrix? Operating on the projection matrix save near/far/fovy/aspect causes all sorts of problems in the depth buffer including anything that uses z (depth testing, face culling, etc).
The OpenGL FAQ section on transformations has some more details.