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. Finall 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 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.
Model matrix
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.
The model matrix looks like this:
e.g.:
View sapce (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 wolrd 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 clipspace the clipping of the scene is performed.
A point is in clip space if the
x
,y
andz
components are in the range defined by the invertedw
component and thew
component of the homogeneous coordinates of the point: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)
perspcetive projection: field of view (y) of 100°, near plane at 0.1, far plane at 20.0
Normaliced device coordinates
The normalized device coordinates are the clip space coordinates divide by the
w
component of the clip coordinates. This is called Perspective divideWindow 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 byglDepthRange
and is by default [0, 1].