Transform the modelMatrix
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
Solution 1:
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].