3D 图形/OpenGL 中的模型矩阵 [英] Model matrix in 3D graphics / OpenGL

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

问题描述

我正在学习 openGL 的一些教程(来自 www.opengl-tutorial.org,如果它有任何不同),并且有一个练习要求我在屏幕上绘制一个立方体和一个三角形,它说提示我应该计算两个 MVP 矩阵,每个对象一个.MVP 矩阵由 Projection*View*Model 给出,据我所知,屏幕上所有对象的投影和视图矩阵都是相同的(它们仅受我选择的相机"位置和设置的影响).但是,模型矩阵应该改变,因为它应该给我全局坐标中对象的坐标和旋转.按照教程,对于我的立方体,模型矩阵只是单位矩阵,因为它位于原点并且没有旋转或缩放.然后我绘制我的三角形,使其顶点位于 (2,2,0)、(2,3,0) 和 (3,2,0).现在我的问题是,我的三角形的模型矩阵是什么?

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. 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). 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). 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?

推荐答案

模型矩阵就像其他矩阵(投影、视图)一样是一个 4x4 矩阵,布局相同.根据您使用的是列向量还是行向量,矩阵由本地框架的 x、y、z 轴和指定平移部分的 t1、t2、t3 向量组成

The model matrix is like the other matrices (projection, view) a 4x4 matrix with the same layout. 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

所以对于列向量 p,变换矩阵 (M) 看起来像

so for a column vector p the transformation matrix (M) looks like

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

p' = M * p

因此对于行向量,您可以尝试找出矩阵布局必须如何.另请注意,如果您有行向量 p' = p * M.

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.

如果你没有旋转分量,你的局部坐标系有通常的 x、y、z 轴作为模型矩阵的 3x3 子矩阵的行..

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..

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

第四列指定平移向量 (t1,t2,t3).如果你有一个点 p =

the forth column specifies the translation vector (t1,t2,t3). If you have a point p =

 1, 
 0,
 0,
 1 

在局部坐标系中,并且您希望它在 z 方向上平移 +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:

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

p' = M * p .. p' 是世界坐标中的变换点.

p' = M * p .. p' is the transformed point in world coordinates.

对于上面的示例,您已经可以在本地坐标系中指定 (2,2,0)、(2,3,0) 和 (3,2,0) 中的三角形.那么模型矩阵是微不足道的.否则,您必须了解如何计算旋转等.我建议您阅读 3d 游戏编程和计算机图形学的前几章数学.这是一本非常简单的 3d 数学书,您应该从中获得处理大部分 3d 图形数学所需的最少信息.

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. 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. 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 图形/OpenGL 中的模型矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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