变换模型矩阵 [英] Transform the modelMatrix

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

问题描述

使用 glm 设置 ViewMatrix 很容易:

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

但是如果我尝试将 funktion 与 modelMatrix 一起使用,我会得到令人困惑的值(模型的位置不正确,而且旋转看起来也不正确).我只想以与设置相机相同的方式设置对象.我可以使用 lookAt 功能并在之后进行一些更改吗?还是我必须为此编写自己的功能?如果是这样,如何?

我用这个固定了位置:

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];

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

gl_Position = CameraMatrix * ModelMatrix * Pos;

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

解决方案

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

模型坐标(物体坐标)

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

例如:

世界坐标

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

模型矩阵

模型矩阵定义了场景中模型(对象、网格)的位置、方向和相对大小.模型矩阵将单个网格的顶点位置转换到世界空间以进行单个特定定位.有不同的模型矩阵,一个用于模型(对象)和对象在世界空间中的位置的每种组合.

模型矩阵如下所示:

( 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 )

视图空间(眼坐标)

视点空间是局部系统,由场景中的视点定义.视图的位置、视线和视图的向上方向,定义了一个相对于世界坐标系的坐标系.场景的对象必须相对于视图坐标系进行绘制,以便被看到".从观看位置.视图坐标系的逆矩阵称为视图矩阵.
一般来说,世界坐标和视图坐标是

剪辑坐标

剪辑空间坐标是

标准化设备坐标

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

窗口坐标(屏幕坐标)

窗口坐标是视口矩形的坐标.最后将窗口坐标传递给评级过程.

视口和深度范围

标准化的设备坐标线性映射到窗口坐标(屏幕坐标)和深度缓冲区的深度.视口由 glViewport<定义/a>.深度范围由 glDepthRange<设置/a> 并且默认为 [0, 1].

It is easy to set the ViewMatrix with glm:

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

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?

I fixed the position with this:

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;

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. Finally the projected scene is mapped to the viewport.

Model coordinates (Object coordinates)

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

e.g.:

World coordinates

The world space 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.

Model matrix

The model matrix defines the location, orientation 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 world space.

The model matrix looks like this:

( 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 )

e.g.:

(  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 )

View space (Eye coordinates)

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

View matrix

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

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

Clip coordinates

Clip space coordinates are Homogeneous coordinates. In clip space 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.

Projection matrix

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.

e.g.:

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

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

Normalized device coordinates

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

Window coordinates (Screen coordinates)

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

Viewport and depthrange

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

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

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