简体   繁体   English

3D图形/ OpenGL中的模型矩阵

[英]Model matrix in 3D graphics / OpenGL

I'm following some tutorials to learn openGL (from www.opengl-tutorial.org if it makes any difference) and there is an exercise that asks me to draw a cube and a triangle on the screen and it says as a hint that I'm supposed to calculate two MVP-matrices, one for each object. 我正在关注一些教程来学习openGL(如果有任何区别,请访问www.opengl-tutorial.org),并且有一个练习要求我在屏幕上绘制一个立方体和一个三角形,并提示我应该计算两个MVP矩阵,每个对象一个。 MVP matrix is given by Projection*View*Model and as far as I understand, the projection and view matrices are the same for all the objects on the screen (they are only affected by my choice of "camera" location and settings). MVP矩阵由Projection * View * Model给出,据我所知,屏幕上所有对象的投影和视图矩阵都是相同的(它们仅受我对“相机”位置和设置的选择的影响)。 However, the model matrix should change since it's supposed to give me the coordinates and rotation of the object in the global coordinates. 但是,模型矩阵应该更改,因为它应该在全局坐标中为我提供对象的坐标和旋转。 Following the tutorials, for my cube the model matrix is just the unit matrix since it is located at the origin and there's no rotation or scaling. 按照教程进行操作,对于我的多维数据集,模型矩阵只是单位矩阵,因为它位于原点,并且没有旋转或缩放。 Then I draw my triangle so that its vertices are at (2,2,0), (2,3,0) and (3,2,0). 然后绘制三角形,使其顶点位于(2,2,0),(2,3,0)和(3,2,0)。 Now my question is, what is the model matrix for my triangle? 现在我的问题是,三角形的模型矩阵是什么?

My own reasoning says that if I don't want to rotate or scale it, the model matrix should be just translation matrix. 我自己的推理是,如果我不想旋转或缩放它,则模型矩阵应该只是平移矩阵。 But what gives the translation coordinates here? 但是,这里给出的翻译坐标是什么? Should it include the location of one of the vertices or the center of the triangle or what? 它应该包括顶点之一的位置还是三角形的中心? Or have I completely misunderstood what the model matrix is? 还是我完全误解了模型矩阵是什么?

The model matrix is like the other matrices (projection, view) a 4x4 matrix with the same layout. 模型矩阵就像其他矩阵(投影,视图)一样具有相同布局的4x4矩阵。 Depending on whether you're using column or row vectors the matrix consists of the x,y,z axis of your local frame and a t1,t2,t3 vector specifying the translation part 根据使用的是列向量还是行向量,矩阵由局部帧的x,y,z轴和指定平移部分的t1,t2,t3向量组成

so for a column vector p the transformation matrix (M) looks like 因此对于列向量p,转换矩阵(M)看起来像

x1, x2, x3, t1,
y1, y2, y3, t2,
z1, z2, z3, t3,
 0,  0,  0,  1 

p' = M * p p'= M * p

so for row vectors you could try to find out how the matrix layout must be. 因此对于行向量,您可以尝试找出矩阵布局必须如何。 Also note that if you have row vectors p' = p * M. 还要注意,如果您有行向量p'= p *M。

If you have no rotational component your local frame has the usual x,y,z axis as the rows of the 3x3 submatrix of the model matrix.. 如果没有旋转分量,则本地框架具有通常的x,y,z轴作为模型矩阵的3x3子矩阵的行。

1 0 0 t1 -> x axis 
0 1 0 t2 -> y axis 
0 0 1 t3 -> z axis 
0 0 0 1 

the forth column specifies the translation vector (t1,t2,t3). 第四列指定转换向量(t1,t2,t3)。 If you have a point p = 如果你有一点p =

 1, 
 0,
 0,
 1 

in a local coordinate system and you want it to translate +1 in z direction to place it in the world coordinate system the model matrix is simply: 在局部坐标系中,并且您希望它在z方向上平移+1并将其放置在世界坐标系中,模型矩阵就是:

1 0 0 0  
0 1 0 0  
0 0 1 1  
0 0 0 1 

p' = M * p .. p' is the transformed point in world coordinates. p'= M * p .. p'是世界坐标系中的变换点。

For your example above you could already specify the triangle in (2,2,0), (2,3,0) and (3,2,0) in your local coordinate system. 对于上面的示例,您已经可以在本地坐标系中的(2,2,0),(2,3,0)和(3,2,0)中指定三角形。 Then the model matrix is trivial. 那么,模型矩阵是微不足道的。 Otherwise you have to find out how you compute rotation etc.. I recommend reading the first few chapters of mathematics for 3d game programming and computer graphics. 否则,您必须找出如何计算旋转度等。我建议阅读有关3d游戏编程和计算机图形学的数学的前几章。 It's a very simple 3d math book, there you should get the minimal information you need to handle the most of the 3d graphics math. 这是一本非常简单的3d数学书,在那里您应该获得处理大多数3d图形数学所需的最少信息。

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

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