3D图形处理 - 如何计算模型视图矩阵 [英] 3D Graphics Processing - How to calculate modelview matrix

查看:210
本文介绍了3D图形处理 - 如何计算模型视图矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解数学从对象空间转换到浏览空间。我这样做是在硬件上,我有以下Atranspose矩阵:

I am having trouble understanding the math to convert from object space to view space. I am doing this in hardware and I have the Atranspose matrix below:

ATranspose =

ATranspose =

         [rightx      upx     lookx    0]
         [righty      upy     looky    0]
         [rightz      upz     lookz    0]
         [-eyeright -eyeup -eyelook    1]

然后找到我们会做点:

Then to find the point we would do:

  [x,y,z,1] = [x',y',z',1]*ATranspose

  xnew = xold*rightx + xold*righty + xold*rightz + xold*(-eyeright)

但我不知道这是否是正确的。

but I am not sure if this is correct.

它也可能是

   [x,y,z,1]=atranspose*[x',y',z',1]T

有人可以解释这样对我?我找不到任何网上关于它不是直接的OpenGL code有关我只是想了解背后的对象点变换数学坐标到眼睛坐标。

Can someone please explain this to me? I can't find anything online about it that isn't directly opengl code related I just want to understand the math behind transforming points from object coordinates to eye coordinates.

推荐答案

这答案很可能是更长的时间比它需要。跳转下到谷底2段左右,如果你已经知道大部分的矩阵数学的。

This answer is probably much longer than it needs to be. Jump down to the bottom 2 paragraphs or so if you already understand most of the matrix math.

这可能是最简单的开始寻找在1维问题。在1D,我们一行点。我们可以扩展它们,或者我们可以把它们翻译。考虑三点 I,J,K 并变换矩阵的 M

It might be easiest to start by looking at a 1 dimensional problem. In 1D, we have points on a line. We can scale them or we can translate them. Consider three points i,j,k and transformation matrix M.

M = [ s t ]
    [ 0 1 ]

i = [1]   j = [-2]   k = [0]
    [1]       [ 1]       [1]

 j     k  i
─┴──┴──┴──┴──┴─
-2 -1  0  1  2

当我们乘上的 M ,我们得到:

When we multiply by M, we get:

i' = Mi = [ s t ][ 1] = [ s+t ]
          [ 0 1 ][ 1]   [  1  ]

j' = Mj = [ s t ][-2] = [-2s+t]
          [ 0 1 ][ 1]   [  1  ]

k' = Mk = [ s t ][ 0] = [  t  ]
          [ 0 1 ][ 1]   [  1  ]

因此​​,如果我们指定值的取值 T 的,那么我们得到不同的转换对我们的一维三角。缩放改变'点'之间的距离,而纯翻译移动其周围相对于所述原点,同时保持恒定的间距:

So if we assign values to s and t, then we get various transformations on our 1D 'triangle'. Scaling changes the distance between the 'points', while pure translation moves them around with respect to the origin while keeping the spacing constant:

   s=1 t=0           s=2 t=1           s=1 t=2
 j     k  i        j     k  i        j     k  i   
─┴──┴──┴──┴──┴─   ─┴──┴──┴──┴──┴─   ─┴──┴──┴──┴──┴─
-2 -1  0  1  2    -3 -1  1  3  5     0  1  2  3  4

重要的是要注意变换顺序是至关重要的。这些1D转换规模和然后的翻译。如果是第一次翻译,那么'点'将是从原点不同的距离,因此缩放因子会影响它不同。出于这个原因,所述转换是经常保持在分开的矩阵,使得顺序是明确的。

It's important to note that order of the transformations is critical. These 1D transformations scale and then translate. If you were to translate first, then the 'point' would be a different distance from the origin and so the scaling factor would affect it differently. For this reason, the transformations are often kept in separate matrices so that the order is clear.

如果我们动起来,以2D,我们得到矩阵的 N

If we move up to 2D, we get matrix N:

   [1 0 tx][ cos(a) sin(a) 0][sx  0 0] [ sx*cos(a) sx*sin(a) tx ]   
N =[0 1 ty][-sin(a) cos(a) 0][ 0 sy 0]=[-sy*sin(a) sy*cos(a) ty ]  
   [0 0 1 ][   0      0    1][ 0  0 1] [    0         0       1 ]

这矩阵将 1) SX,SY 2)按绕原点到该点的一个缩放点的程度,然后 3 转换点通过的 TX,TY 的。注意,这个矩阵的点psented的列向量和乘法将作为再$ P $的假设下建造。正如datenwolf说,如果你想用行向量重新$ P $点psentation但采用同样的改造,就可以转一切,交换顺序。这是矩阵乘法的一般属性:(AB)^ T =(B ^ T)(A ^ T)

This matrix will 1) scale a point by sx,sy, 2) rotate the point around the origin by a degrees, and then 3 translate the point by tx,ty. Note that this matrix is constructed under the assumption that points are represented as column vectors and that the multiplication will take place as Np. As datenwolf said, if you want to use row vector representation of points but apply the same transformation, you can transpose everything and swap the order. This is a general property of matrix multiplication: (AB)^T = (B^T)(A^T).

这是说,我们可以在对象世界,眼坐标方面谈转变。如果眼睛坐在世界的起源,俯视世界负z轴,与+ X的权利和+ Y和对象,多维数据集,坐着10个单位下降-Z(集中在z轴),以2宽度沿着世界的x,沿z深度3,和高度的4沿世界年。然后,如果立方体的中心是参照对象的本地帧和局部坐标方便地对准世界上轴。然后在对象坐标框的顶点上变化[+/- 1,+ / - 2,+ / - 1.5] ^ T 。不久,上,右(从眼睛的角度来看的)顶点具有的对象坐标 [1,2,1.5] ^ T ,中的世界坐标的,相同的顶点 [1,2,-8.5] ^ T (1.5-10 = -8.5)。因为那里的眼睛,哪一种方式它的指向,那我们定义我们的眼睛同样的方式作为OpenGL的一个事实,即顶点有相同的眼坐标的作为的世界坐标的。因此,让我们的移动和旋转的眼睛,使眼睛的 X 右(RT)的和眼睛的向上和眼睛的 -z 的样子(LK)的和眼睛定位在 [eyeright(前)eyeup(EY)eyelook(EZ) ] ^ T 。既然我们要反对转变为眼部坐标(这意味着我们将治疗眼睛为原点)坐标,我们将这些变革的倒数,并将其应用到对象的顶点(它们已变成世界上仅次于坐标)。因此,我们将有:

That said, we can talk about transformations in terms of object, world, and eye coordinates. If the eye is sitting at the origin of the world, looking down the world's negative z-axis, with +x to the right and +y up and the object, a cube, is sitting 10 units down -z (centered on the z axis), with width of 2 along the world's x, depth of 3 along the z, and height of 4 along world y. Then, if the center of the cube is the object's local frame of reference and its local axes conveniently align with the world's axes. Then the vertices of the box in object coordinates are the variations on [+/-1,+/-2,+/-1.5]^T. The near, top, right (from the eye's point-of-view) vertex has object coordinates [1,2,1.5]^T, in world coordinates, the same vertex is [1,2,-8.5]^T (1.5-10=-8.5). Because of where the eye is, which way it's pointing, and the fact that we define our eye the same way as OpenGL, that vertex has the same eye coordinates as world coordinates. So let's move and rotate the eye such that the eye's x is right(rt) and the eye's y is up and the eye's -z is look(lk) and the eye is positioned at [eyeright(ex) eyeup(ey) eyelook(ez)]^T. Since we want object coordinates transformed to eye coordinates (meaning that we'll treat the eye as the origin), we'll take the inverse of these transformations and apply them to the object vertices (after they have been transformed into world coordinates). So we'll have:

ep = [WORLD_TO_EYE]*[OBJECT_TO_WORLD]*wp;

更具体地说,我们感兴趣的顶点,我们将有:

More specifically, for our vertex of interest, we'll have:

[ rt.x  rt.y  rt.z 0][1 0 0 -ex][1 0 0  0 ][ 1 ]
[ up.x  up.y  up.z 0][0 1 0 -ey][0 1 0  0 ][ 2 ]
[-lk.x -lk.y -lk.z 0][0 0 1 -ez][0 0 1 -10][1.5]
[   0     0     0  1][0 0 0  1 ][0 0 0  1 ][ 1 ]

为了方便起见,我已经分离出的翻译眼球的转动会影响它。其实,现在我已经写了这么多,这可能是混乱的地步。你给会旋转,然后转换矩阵。我以为,眼睛的翻译是在世界坐标。但是当你在你的问题写的,它实际上在执行转换眼坐标。我也被否定的 LK 的,因为我们已经定义了眼睛要向下看负z轴,而是做一个标准的旋转矩阵,我们要使用积极的价值观。

For convenience, I've separated out the translation the rotation of the eye affects it. Actually, now that I've written so much, this may be the point of confusion. The matrix that you gave will rotate and then translate. I assumed that the eye's translation was in world coordinates. But as you wrote it in your question, it's actually performing the translation in eye coordinates. I've also negated lk because we've defined the eye to be looking down the negative z-axis, but to make a standard rotation matrix, we want to use positive values.

不过,我可以继续下去,但也许这已经回答了你的问题。

Anyway, I can keep going, but maybe this answers your question already.

继续:

解释上述远一点,眼睛的变换分离成两个组件也使得它更容易找到的倒数。它很容易地看到由移动世界上的一切,如果翻译的 TX 的移动某处相对于世界上的物体的眼球,我们能保持在世界的眼睛和点之间的相同的相对位置 -tx 的,保持眼部平稳。

Explaining the above a little further, separating the eye's transformation into two components also makes it much easier to find the inverse. It's easy to see that if translation tx moves the eye somewhere relative to the objects in the world, we can maintain the same relative positions between the eye and points in the world by moving the everything in the world by -tx and keeping the eye stationary.

同样,考虑眼睛的方向通过其默认定义的右键向上的载体:

Likewise, consider the eye's orientation as defined by its default right, up, and look vectors:

     [1]      [0]      [ 0]
d_rt=[0] d_up=[1] d_lk=[ 0]
     [0]      [0]      [-1]

创建一个旋转矩阵指向这三个矢量一个新的方向是很容易。我们只是排队了三个新坐标轴的 RT LK 的(作为列向量):

Creating a rotation matrix that points these three vectors in a new direction is easy. We just line up our three new axes rt, up, lk (as column vectors):

[rt.x up.x -lk.x 0]
[rt.y up.y -lk.y 0]
[rt.z up.z -lk.z 0]
[  0    0     0  1]

可以很容易地看到,如果你增加d_rt,d_up和d_lk和乘以上述矩阵,你得到的 RT 向上 LK 的背面分别。因此,我们应用了我们想要的变革。为一个适当的转动,三个向量必须是正交的。这真是基地只是一个改变。由于这一事实,我们可以通过取它的转找到该矩阵的逆相当方便。这就是我所做的上方。如果应用该转置矩阵的所有在世界坐标中的点和离开眼睛静止,点将保持相同的位置,相对于眼睛,如如果眼睛已转动。

It's easy to see that if you augment d_rt, d_up, and d_lk and multiply by the above matrix, you get the rt, up, and lk back respectively. So we've applied the transformation that we wanted. To be a proper rotation, the three vectors must be orthonormal. This is really just a change of bases. Because of that fact, we can find the inverse of this matrix quite conveniently by taking its transpose. That's what I did above. If you apply that transposed matrix to all of the points in world coordinates and leave the eye still, the points will maintain the same position, relative to the eye, as if the eye had rotated.

例如:

分配(在世界坐标):

   [ 0]    [0]    [-1]     [-2]     [1.5]
rt=[ 0] up=[1] lk=[ 0] eye=[ 0] obj=[ 0 ]
   [-1]    [0]    [ 0]     [ 1]     [-3 ]

这篇关于3D图形处理 - 如何计算模型视图矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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