简体   繁体   English

OpenGL中矩阵乘法的顺序

[英]Order of matrix multiplications in OpenGL

glDisable(GL_DEPTH_TEST);
glViewport(0/*left*/, 0/*botton*/, 200/*width*/, 200/*height*/); //T4   
glMatrixMode(GL_PROJECTION);    
glLoadIdentity();   
gluPerspective(90 /*fov*/, 1/*aspect*/, 1/*fp*/, 1000/*bp*/); //T3
glMatrixMode(GL_MODELVIEW); 
glLoadIdentity();     
gluLookAt(0/*eyex*/,0/*eyey*/,0/*eyez*/, 0/*lax*/,0/*lay*/,-1/*laz*/, 0/*upx*/,1/*upy*/,0/*upz*/); //T2
glTranslatef(-15.0, -10.0, -49.0); //T1
glBegin(GL_POINTS);
glVertex4f(0, 0, -1, 1);
glEnd();

Given this code, in what order does the matrix multiplication happen? 鉴于此代码,矩阵乘法的顺序是什么? What do I have to know to follow and verify the calculations on paper? 我必须知道如何跟踪并验证纸上的计算?

I suspect to following order, but haven't figured out a way to verify it: v=[0,0,-1,1] 我怀疑要遵循顺序,但还没有找到验证它的方法:v = [0,0,-1,1]

T4 * T3 * T2 * T1 * v

Is this correct? 这个对吗?

This is mostly correct, however glViewport (...) by itself does not define a matrix. 这大多是正确的,但glViewport (...)本身并没有定义矩阵。 It is a simple bias and scale operation. 这是一个简单的偏差和缩放操作。 It defines the total width, height and offset in X and Y. There is another component you are missing, which is the depth range. 它定义了X和Y中的总宽度,高度和偏移量。还有另一个缺少的组件,即深度范围。

The multiplication occurs in that order, but since these are column-major matrices and post-multiplied, you conceptually start on the right and then work your way to the left. 乘法按顺序发生,但由于这些是列主矩阵并后乘,所以从概念上说,从右边开始,然后向左行进。 Cross out T4 because it alone is not a matrix, and the end result of all this is a clip-space vertex coordinate. T4因为它本身不是一个矩阵,所有这一切的最终结果是一个剪辑空间顶点坐标。 You still need to divide v.xyz by vw and then do the viewport transformation to fully replicate what GL does. 您仍需要将v.xyz除以vw ,然后执行视口转换以完全复制GL所执行的操作。

You can implement the viewport transform using a matrix, but you also need to factor in glDepthRange (...) which biases and scales the Z coordinate from NDC space to window space. 可以使用矩阵实现视口变换,但您还需要考虑glDepthRange (...) ,它将Z坐标从NDC空间偏移并缩放到窗口空间。

Here is what such a matrix would look like: 这就是这样一个矩阵的样子:

行主视口矩阵

The whole process is discussed in better detail here, under 4.1 Coordinates Transformation . 4.1坐标转换下,这里将更详细地讨论整个过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM