Coordinate system in openGL

Right-handed coordinate system is used in openGL

  • Right-handed coordinate system: Extend the right hand, the thumb points to the positive direction of the X-axis, the index finger points to the positive direction of the Y-axis, and the other three fingers point to the positive direction of the Z-axis
  • Left-handed coordinate system: stretch out the left hand, the thumb points to the positive direction of the X-axis, the index finger points to the positive direction of the Y-axis, and the other three fingers point to the positive direction
    of the Z-axis. The main difference between the two is that the direction of the Z-axis is opposite.

1. Coordinate system

Several main coordinate systems in openGL

  • The world coordinate system
    takes the center of the screen as the origin (0, 0, 0). When you face the screen, the positive X axis is on the right, the positive Y axis is on the top, and the direction the screen points to you is the positive Z axis. The window range is from (-1,1), that is, the coordinates of the lower left corner of the screen are (-1,-1,0), and the coordinates of the upper right corner are (1,1,0). We use this coordinate system to describe the position
    of objects and light sources. When objects are placed in the scene (translation, rotation, etc.), these operations are coordinate transformations. OpenGL provides three coordinate transformation commands, glTranslate / glScale / glRotate. Using the transformation matrix operation command, any complex coordinate transformation can be realized.
  • The inertial coordinate system
    is jointly understood by the world coordinate system and
    the object coordinate system. It is the rotation of the object coordinate system, which is only a description of an intermediate state, which is convenient for switching the object coordinate system to the world coordinate system.
  • The object coordinate system/local space coordinate system
    is a coordinate established with a certain point of the object as the origin. This coordinate is only applicable to the object and is used to simplify the description of the coordinates of each part of the object. When the object is placed in the scene, the coordinate transformations experienced by each part are the same, and the relative position remains unchanged, which can be regarded as a whole
  • Camera coordinate system : Observer coordinate system
    Take the observer as the origin, and the direction of the line of sight is the positive direction of the Z axis. The openGL pipeline will first transform the world coordinates to the observer coordinates, and then clip. Only the scene within the line of sight will be calculated in the next step

Private message me to receive the latest and most complete C++ audio and video learning and improvement materials, including ( C/C++ , Linux , FFmpeg , webRTC , rtmp , hls , rtsp , ffplay , srs )

 

Coordinate transformation process

After each vertex coloring in openGL, the visible vertices are standardized device coordinates, that is, the x, y, and z values ​​of each vertex should be between -1 and 1, and vertices beyond this range are invisible.
Convert the coordinates to The process of normalizing device coordinates and then converting to screen coordinates is a step-by-step process in which the vertices of objects are also converted to multiple coordinate systems before being finally converted to screen coordinates. In these excessively specific coordinate systems, some operations or operations are more convenient.

transformation matrix

In order to transform coordinates from one coordinate system to another, we need to use several transformation matrices, commonly used three matrices: Model, View, and Projection

Coordinate transformation matrix stack

The top of the stack is the current coordinate transformation matrix. Each coordinate entering the openGL pipeline will be multiplied by this matrix first, and the result is the world coordinate of the corresponding point in the specific scene. Coordinate transformation in openGL is done through matrix operations. The matrix multiplication in the transformation is a cross product, and the result contains the direction, which does not conform to the commutative law.
The starting coordinates of the vertices of the object are local coordinates, which will then be converted to world coordinates, observer coordinates, clipping coordinates, and finally end in the form of screen coordinates. You can refer to the picture below

2. Coordinate space

local space

Refers to the coordinate space where the object is located, that is, the place where the object begins at the beginning, which is local relative to the object

world space

It refers to the coordinate space of the vertices of the object relative to the world. If the objects are scattered in the world, the coordinates of the object will be transformed from the local space to the world space. The transformation is implemented by the Model Matrix

  • Model Matrix A Model Matrix
    is ​​a transformation matrix that can place an object in its proper position and orientation by shifting, scaling, and rotating it. Imagine you want to put a chair in a room, you need to shrink it first (it's too big in local space), move it somewhere in the room, and then rotate it a little bit left and right on the y axis to arrange neatly. is to transform local coordinates to different locations in the scene/world

observation space/observation space

The observation space is also called the camera of openGL, so it is sometimes called the camera space or the eye space. The viewing space is the result of converting world space coordinates to coordinates in front of the user's field of view. That is to say, the observation space is the space observed from the camera's point of view.
And this is usually done by a combination of displacements and rotations, translating/rotating the scene so that certain objects are transformed in front of the camera.
These combined transformations are usually stored in a View Matrix , which is used to transform world coordinates into view space.

clipping space

  • Clipping process
    At the end of a vertex shader run, openGL expects all coordinates to fall within a certain range (-1,1), and any points outside this range should be clipped. The clipped ones are ignored, and the rest of the coordinates become fragments visible on the screen. This space is called clipping space.
    In order to transform vertex coordinates from observer coordinates to clipping space, we need to define a projection matrix (Projection Matrix) , which can specify a range of coordinates, and the projection matrix will transform the coordinates within this specified range. In order to normalize the range of device coordinates (-1,1), all coordinates outside the range will not be mapped in the range -1 to 1, so they will be clipped off
    such as -1000 to 1000 in each dimension, After clipping, the coordinates (12400, 500, 700) will not be visible because its x-coordinate is out of range (greater than 1000), it is converted to a normalized device coordinate greater than 1, so it is clipped out of the
    projection: will be within a certain range The process of transforming the coordinates of the Frustum into a normalized device coordinate system, known as the projection
    frustum Frustum: The viewing box created by the projection matrix is ​​called the frustum, and each coordinate that occurs within the frustum will eventually appear in the on the user's screen. (I don’t quite understand, is it similar to a cone or cylinder, cut with a knife from the middle, and the range of the cut surface is equivalent to a frustum??

  • Perspective Division
  • Perspective division is performed once all vertices have been transformed into clip space. In this process, we divide the x, y, z components of the position vector by the homogeneous component w (depth) of the vector, respectively.
    Perspective division is the process of transforming 4D clip space coordinates into 3D normalized device coordinates. This step is automatically performed at the end of each vertex shader run.
    Afterwards, the final coordinates are mapped into screen space (using the settings in glViewport) and transformed into fragments
    . Projection matrices that transform view coordinates into crop coordinates can be divided into two different forms: Orthographic Projection Matrix) or perspective projection matrix (Perspective Projection Matrix)

perspective projection

In real life, the farther away from the observer, the smaller the thing looks, this phenomenon is called perspective (Perspective)
The perspective effect in openGL is achieved by the perspective matrix. This perspective matrix maps the given frustum extent to clip space, in addition to modifying the w (depth) value of each vertex coordinate so that the w component of the vertex coordinate farther from the viewer is larger . Coordinates that are transformed into clip space are always between -w and w. So, once the coordinates are in clip space, perspective division is applied to the clip space coordinates

orthographic projection

When using orthographic projection, each vertex coordinate is mapped directly into clip space without any fine perspective division (it still does perspective division, but the w component is not changed and remains at 1, so it doesn't work)

3. Combining coordinate systems together

We have created a transformation matrix for each of the above steps: model matrix, observation matrix, projection matrix , a vertex coordinate will be transformed to clip coordinate according to the following process
Vclip = Mpro * Mview * Mmodel * Vlocal
. This series of matrix transformations needs to be from right to left, in order MVP. The final vertex should be assigned to gl_Position in the vertex shader, openGL will automatically perform perspective division and crop
viewport transformation: openGL performs perspective division on the crop coordinates to transform them to normalized device coordinates, then openGL uses the parameters inside glViewPort To map standardized device coordinates to screen coordinates, each coordinate is associated with a point on the screen, this process is called viewport transformation
. is done by the system

 

 

 

Guess you like

Origin blog.csdn.net/m0_60259116/article/details/124355940