对本地化矩阵感到困惑 - 当传递到 OpenGL 时有效,但在做我自己的算术时无效?
我很困惑我的问题在这里。我已经设置了一个矩阵,它将全局/世界坐标转换为对象的局部坐标空间。该转换矩阵是使用来自四个向量(向前、向上、侧面和位置)的对象信息构建的。然后在每个对象的绘制时将该定位矩阵传递给glMultMatrixf(),以便我可以在每个对象周围绘制一个简单的轴以可视化局部坐标系。这工作得很好并且符合预期,并且当对象在世界中移动和旋转时,它们的局部坐标轴也会移动和旋转。
问题是,当我采用相同的矩阵并将其乘以列向量(将一个对象的全局位置转换为另一个对象的局部坐标系)时,结果根本不符合我的预期。例如:
我的定位矩阵如下:
0.84155 0.138 0.5788 0
0.3020 0.8428 -0.5381 8.5335
0.4949 -0.5381 -0.6830 -11.6022
0.0 0.0 0.0 1.0
我输入位置列向量:
-30.0
-30.0
-30.0
1.0
并得到输出:
-99.2362
-1.0199
4.8909
1.0000
因为我的对象在此时的位置是 (-50.8, 8.533, -11.602, 1),所以我知道输出因为 x 坐标不可能大到 -99.2362。此外,当我找到两个全局点之间的距离和局部点与原点之间的距离时,它们是不同的。
我已经在Matlab中检查过这一点,看来我的矩阵乘法是正确的(注意:在Matlab中你必须首先转置本地化矩阵)。所以我认为我的局部矩阵没有正确构建 - 但随后 OpenGL 成功地使用这个矩阵来绘制局部坐标轴!
我试图在这个问题中不包含不必要的细节,但如果您觉得需要更多信息,请随时询问! :)
非常感谢。
I'm very confused as to what my problem is here. I've set up a matrix which converts global/world coordinates into a local coordinate space of an object. This conversion matrix is constructed using object information from four vectors (forward, up, side and position). This localization matrix is then passed to glMultMatrixf()
at the draw time for each object so as I can draw a simple axes around each object to visualize the local coordinate system. This works completely fine and as expected, and as the objects move and rotate in the world, so do their local coordinate axes.
The problem is that when I take this same matrix and multiply it by a column vector (to convert the global position of one object into the local coordinate system of another object) the result is not at all as I would expect. For example:
My localize matrix is as follows:
0.84155 0.138 0.5788 0
0.3020 0.8428 -0.5381 8.5335
0.4949 -0.5381 -0.6830 -11.6022
0.0 0.0 0.0 1.0
I input the position column vector:
-30.0
-30.0
-30.0
1.0
And get the output of:
-99.2362
-1.0199
4.8909
1.0000
As my object's position at this point in time is (-50.8, 8.533, -11.602, 1), I know that the output for the x coordinate cannot possibly be as great as -99.2362. Futhermore, when I find the distance between two global points, and the distance between the localized point and the origin, they are different.
I've checked this in Matlab and it seems that my matrix multiplication is correct (Note: in Matlab you have to first transpose the localize matrix). So I'm left to think that my localize matrix is not being constructed correctly - but then OpenGL is successfully using this matrix to draw the local coordinate axes!
I've tried to not include unnecessary details in this question but if you feel that you need more please don't hesitate to ask! :)
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我必须猜测,但我想指出 OpenGL 矩阵乘法问题的两个根源:
模型视图矩阵转换为一个坐标系,其中相机始终位于原点 (0,0,0) 处z 轴。因此,如果您在应用局部->全局变换之前进行了一些变换以“移动相机”,则必须补偿相机移动,否则您将获得相机坐标空间的局部坐标。构建矩阵时是否包含摄像机变换?
OpenGL 中的矩阵是列主矩阵。如果您有一个包含 16 个值的数组,则元素将以这种方式排序:
[0][4][8][12]
[1][5][9][13]
[2][6][10][14]
[3][7][11][15]
你的矩阵看起来也很奇怪。前三列告诉我,您应用了一些旋转或缩放变换。最后一列显示应用于每个坐标元素的平移量。这些数字与物体的位置相同。这意味着,如果您希望输出 x 坐标为 -50.8,则第一行中的前三个元素加起来应为零:
-30*0.8154 -30*0.3020 -30*0.4939 + 1 * -50.8967
<-- -这应该为零-------->但是是-48,339。
所以我认为,构建矩阵时确实存在问题。也许你可以解释一下如何构建矩阵......
I have to guess, but I would like to point out two sources of problems with OpenGL-matrix multiplication:
the modelview matrix transforms to a coordinate system where the camera is always at the origin (0,0,0) looking along the z-axis. So if you made some transformations to "move the camera" before applying local->global transformations, you must compensate for the camera movement or you will get coordinates local to the camera's coordinate space. Did you include camera transformations when you constructed the matrix?
Matrices in OpenGL are COLUMN-major. If you have an array with 16 values, the elements will be ordered that way:
[0][4][ 8][12]
[1][5][ 9][13]
[2][6][10][14]
[3][7][11][15]
Your matrix also seems strange. The first three columns tell me, that you applied some rotation or scaling transformations. The last column shows the amount of translation applied to each coordinate element. The numbers are the same as your object's position. That means, if you want the output x coordinate to be -50.8, the first three elements in the first row should add up to zero:
-30*0.8154 -30*0.3020 -30*0.4939 + 1 * -50.8967
<---this should be zero--------> but is -48,339.
So I think, there really is a problem when constructing the matrix. Perhaps you can explain how you construct the matrix...