转换modelMatrix [英] Transform the modelMatrix

查看:126
本文介绍了转换modelMatrix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用glm设置ViewMatrix很容易:

It is easy to set the ViewMatrix with glm:

glm::lookAt(Position, Direction, UpVector);

但是,如果我尝试将功能与modelMatrix一起使用,则会得到混淆的值(Model的位置不正确,旋转也看起来不正确). 我只想以与设置相机相同的方式设置对象. 我可以使用lookAt函数然后进行一些更改吗?还是我必须为此编写自己的功能?如果可以,怎么办?

but if I try to use the funktion with the modelMatrix, I'll get comfusing values (the Model is not in the correct position and also the rotation looks wrong). I just want to set an object the same way than setting the camera. Can I use the lookAt funktion and make some changes afterwards? Or does I have to program an own funtion for that? And if so, how?

我以此固定位置:

m_Orientation = glm::lookAtLH(Position, Direction, UpVector);
m_Orientation[3][0] = -m_Orientation[3][0];
m_Orientation[3][1] = -m_Orientation[3][1];
m_Orientation[3][2] = -m_Orientation[3][2];

在顶点着色器中我也使用这个:

also inside the vertexshader I use this:

gl_Position = CameraMatrix * ModelMatrix * Pos;

其中CameraMatrix是viewProjectionMatrix,ModelMatrix(我的问题),Pos是我的顶点在模型空间中的位置

where CameraMatrix is a viewProjectionMatrix, ModelMatrix (my problem) and Pos is the position of my vertex in modelspace

推荐答案

在渲染中,通常通过模型矩阵,视图矩阵和投影矩阵来变换场景的每个网格.最后,将投影的场景映射到视口.

In a rendering, each mesh of the scene usually is transformed by the model matrix, the view matrix and the projection matrix. Finall the projected scene is mapped to the viewport.

模型空间是局部空间,在其中定义了网格.顶点坐标在模型空间中定义.

The model space is the local space, where within a mesh is defined. The vertex coordinates are defined in model space.

例如:

世界的空间是场景的坐标系.可以将不同的模型(对象)多次放置在世界空间中,以形成一个场景.

The world sapce is the coordinate system of the scene. Different models (objects) can be placed multiple times in the world space to form a scene, in together.

模型矩阵

模型矩阵定义场景中模型(对象,网格)的位置,方向和相对大小.模型矩阵将单个网格的顶点位置转换为单个特定位置的世界空间.有不同的模型矩阵,每个模型矩阵分别对应一个模型(对象)和该对象在世界空间中的位置.

The model matrix defines the location, oriantation and the relative size of a model (object, mesh) in the scene. The model matrix transforms the vertex positions of a single mesh to world space for a single specific positioning. There are different model matrices, one for each combination of a model (object) and a location of the object in the wolrd space.

模型矩阵如下:

( X-axis.x, X-axis.y, X-axis.z, 0 )
( Y-axis.x, Y-axis.y, Y-axis.z, 0 )
( Z-axis.x, Z-axis.y, Z-axis.z, 0 )
( trans.x,  trans.y,  trans.z,  1 )

例如:

(  0.0, -0.5,  0.0,  0.0 )
(  2.0,  0.0,  0.0,  0.0 )
(  0.0,  0.0,  1.0,  0.0 )
(  0.4,  0.0,  0.0,  1.0 )

视图空间是由场景上的视点定义的本地系统. 视图的位置,视线和视图的向上方向定义了相对于世界坐标系的坐标系.场景的对象必须相对于视图坐标系绘制,以便从查看位置看到".视图坐标系的逆矩阵称为视图矩阵.
通常,世界坐标和视图坐标为笛卡尔坐标

The view space is the local system which is defined by the point of view onto the scene. The position of the view, the line of sight and the upwards direction of the view, define a coordinate system relative to the world coordinate system. The objects of a scene have to be drawn in relation to the view coordinate system, to be "seen" from the viewing position. The inverse matrix of the view coordinate system is named the view matrix.
In general world coordinates and view coordinates are Cartesian coordinates

查看矩阵

视图坐标系描述了从中查看场景的方向和位置.视图矩阵从世界空间转换为视图(眼睛)空间.

The view coordinates system describes the direction and position from which the scene is looked at. The view matrix transforms from the wolrd space to the view (eye) space.

如果视图空间的coordiante系统是右手系统,然后X轴指向左侧,Y轴指向上方,Z轴指向视图之外(注意,在右手系统中,Z轴是X轴与Y轴的叉积) .

If the coordiante system of the view space is a Right-handed system, then the X-axis points to the left, the Y-axis up and the Z-axis out of the view (Note in a right hand system the Z-Axis is the cross product of the X-Axis and the Y-Axis).

剪辑空间坐标为同质坐标.在剪辑空间中,将执行场景的剪辑.
如果xyz分量在由点的齐次坐标的倒置w分量和w分量定义的范围内,则点在剪辑空间中:

Clip space coordinates are Homogeneous coordinates. In clipspace the clipping of the scene is performed.
A point is in clip space if the x, y and z components are in the range defined by the inverted w component and the w component of the homogeneous coordinates of the point:

-w <=  x, y, z  <= w.

投影矩阵

投影矩阵描述从场景的3D点到视口的2D点的映射.投影矩阵从视图空间转换为剪辑空间.通过除以剪辑坐标的w分量,将剪辑空间中的坐标转换为范围为(-1,-1,-1)至(1、1、1)的归一化设备坐标(NDC).

The projection matrix describes the mapping from 3D points of a scene, to 2D points of the viewport. The projection matrix transforms from view space to the clip space. The coordinates in the clip space are transformed to the normalized device coordinates (NDC) in the range (-1, -1, -1) to (1, 1, 1) by dividing with the w component of the clip coordinates.

例如:

查看:眼睛位置(2.5,-1.5,3.5),中心(2,0,0),向上矢量(0,1, 0)

look at: eye position (2.5, -1.5, 3.5), center (2, 0, 0), up vector (0, 1, 0)

透视投影: 100°的视场(y),在 0.1 处的近平面,在 20.0

perspcetive projection: field of view (y) of 100°, near plane at 0.1, far plane at 20.0

归一化设备坐标是剪辑空间坐标除以剪辑坐标的w分量.这称为透视划分

The normalized device coordinates are the clip space coordinates divide by the w component of the clip coordinates. This is called Perspective divide

窗口坐标是视口矩形的坐标.窗口坐标最终被传递到分级处理.

The window coordinates are the coordinates of the viewport rectangle. The window coordinates finally are passed to the raterization process.

视口和深度范围

归一化的设备坐标线性映射到窗口坐标(屏幕坐标)和深度缓冲区的深度. 视口由 glViewport 定义.深度范围是由 glDepthRange 设置的,并且默认情况下为[0,1].

The normalized device coordinates are linearly mapped to the Window Coordinates (Screen Coordinates) and to the depth for the depth buffer. The viewport is defined by glViewport. The depthrange is set by glDepthRange and is by default [0, 1].

这篇关于转换modelMatrix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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