OpenGL Matrix Transformations

Source: Internet
Author: User
Tags truncated

Overview

The geometry data-the vertex position, and the standard vector (normal vectors)-can be changed by vertex operations (Vertex operation) and basic combination operations before the OpenGL pipeline raterization process.

Object coordinates

The local coordinate system of the object-the original position before any transformations. In order to transform (transformation) These objects, you can call Glrotate (), Gltranslatef (), Glscalef () these methods.

Eye coordinates

Use the Gl_modelview matrix and the object coordinates to multiply the resulting. In OpenGL, the object space is transformed into the visual space by using Gl_modelview. Gl_modelview

The matrix is a combination of the model matrix and the visual matrix (View matrices). The Model transformation refers to the conversion of object space to world space.

(World space is worth three-dimensional space in OpenGL), and the view transform is to transform world space into eye space.

Note: There is no separate camera (view) matrix in OpenGL. Therefore, in order to simulate camera or view transformations, the scene (3D objects and lighting) must be transformed in the opposite direction of the view. That is, OpenGL always defines the camera at the (0,0,0) point and enforces the-Z axis direction in the eye space coordinate system and cannot be transformed. More information about the Gl_modelview matrix can be found here: Http://www.songho.ca/opengl/gl_transform.html#modelview

The standard vector (normal vectors), which transforms from the object coordinate system (coordinates) to the visual coordinate system (eye coordinates), is used to calculate the illumination (lighting calculation). Note The standard vector ( Normal vectors) is different from the vertex. where the visual matrix (view matrix) is the Gl_modelview inverse matrix of the transpose matrix and the standard vector (normal vectors are) multiplied, namely:

More information on standard vector transformations (normal vector transformation) can be connected here: http://www.songho.ca/opengl/gl_normaltransform.html

Shear Plane coordinate system (clip coordinates)

By multiplying the visual coordinate system and the gl_projection matrix, the shear plane coordinate system is obtained. The gl_projection matrix defines the visible space (the truncated cone). I also looked up the information on what the truncated cone was, and found it to look like this:

, this is the effect of the projection) and how the vertex data is projected onto the screen (angle of view or orthogonal (orthogonal)), which is referred to as the tangent plane coordinate system because after the (x, Y, z) transformation

To compare with ±w. More information on the gl_projection matrix is visible: http://www.songho.ca/opengl/gl_transform.html#projection

Standardized device coordinate system (NDC)

Divide the tangent plane coordinate system by the W (the discussion about W is visible here:, http://www.songho.ca/math/homogeneous/homogeneous.html), which is called Perspective Division (Perspective Division)

It is more like a window coordinate system, but it has not been converted or shrunk to screen pixels. of it the range of values in 3 axes from 1 to 1 were standardized.

Windows coordinate System (window coordinates)/screen coordinate system (screens coordinates)

Applies the standardized device coordinate system (NDC) to the viewport transformation. NDC shrinks and pans to fit the perspective of the screen. The pipeline processing that the window coordinate system eventually passes to OpenGL becomes fragment. Glviewport () function

The projection area used to define the final picture map. Similarly, Gldepthrange () is used to determine the z-coordinate of the window's coordinate system. The window coordinate system is calculated from the parameters given by the following two methods

Glviewport (X,Y,W,H);

Gldepthrange (N,F);

The viewport conversion formula is simple and is derived through the linear relationship between NDC and the window coordinate system:

OpenGL transformation matrix

OpenGL uses a 4x4 matrix transformation. Note that these 16 elements are stored in a 1D array, sorted in column order. If you want to arrange in a behavioral order, you need to transpose the matrix.

OpenGL has 4 of matrices that are not used: Gl_modelview,gl_projection,gl_texture and gl_color. You can

The code uses the Glmatrixmode () function to change the current type. For example, in order to select the Gl_modelview matrix, you can:

Glmatrixmode (Gl_modelview);

--------------------------------------------------------------------------------------------------------------- ------------------------------

Model-view Matrix (Gl_modelview)

The Gl_modelview matrix contains the view matrix and the model matrix in a matrix, in order to transform view (camera), you need to put the entire

The scene is applied inverse transformation. Glulookat () is used to set the viewing transform.

The rightmost three matrix elements (M12, M13, M14) are used as displacement transformations. The M15 element is a homogeneous coordinate. (What is the homogeneous coordinate, see: http://www.songho.ca/math/homogeneous/homogeneous.html), which is used to project the transformation.

3 element Sets (M0, M1, M2), (M4, M5, M6) and (M8, M9, M10) are used as Euler transformations and affine transformations, such as glrotate (), Scaling Glscalef ().

Note that these three set of elements actually refer to 3 orthogonal coordinate systems:

(M0, M1, m2): +x axis, left vector (estimated to be relative to screen), default to (1,0,0)

(M4, M5, M6): +y axis, upward vector (up vector), default = (0,1,0)

(M8, M9, M10): +z axis, forward vector, default (0,0,1).


4 Columns of Gl_modelview matrix

We can construct the Gl_modelview matrix directly without using OpenGL transform functions. Here are some useful code constructs

Gl_modelview Matrix

1. Angles to Axes

2. Lookat to Axes

3. Matrix4 class

Note that OpenGL multiplies in the opposite order matrix when multiple transformations are applied to the vertices at the same time. For example, if a vertex is preceded by a MA

Transform, and then transform in megabytes. OpenGL first uses the MB x MA before multiplying the vertices. So the final transformation is now before the matrix is multiplied, and the first transformation appears at the end.

Projection matrix PROJECTION matrix (gl_projection)

The gl_projection matrix is used to define the truncated cone. The truncated cone determines which parts of the object or object will be clipped off. It also determines how the 3D scene is projected onto the screen.

(For how to build a projection matrix, see

Http://www.songho.ca/opengl/gl_projectionmatrix.html

OpenGL provides 2 functions for gl_projection transformations. Glfrustum () produces a projection perspective. Glortho () produces orthogonal (or parallel) projections.

Two functions require 6 parameters to determine 6 cut faces: left, right, bottom, top, near, and far plane. The 8 vertices of a truncated cone are as follows:


OpenGL Perspective Viewing Frustum

The vertices of the distal plane (rear) can be calculated simply by the ratio of similar triangles. For example, the left side of the remote plane can be calculated as follows:

For orthographic projections, the ratio is 1, so the left,right,bottom and top values of the distal plane are the same as the values of the near-end plane.

Similarly, you can also use the gluperspective () and gluortho2d () functions, but pass fewer arguments. Gluperspective () requires only 4 parameters: the vertical area of the view (vertical field of view (FOV)),

The ratio of the width/height, as well as the distance between the proximal plane and the far side plane. The following code uses Gluperspective () and glfrustum () to achieve the same functionality:

OpenGL Orthogonal truncated cone


OpenGL orthographic Frustum

However, if you want an asymmetric visual space, you can use Glfrustum () directly. For example

If you want to present a large scene to 2 adjacent screens, you can truncate the truncated cone into 2 asymmetric truncated cones (left and right). And then

Renders each truncated cone scene.

(This sentence is too bad translation, the position is as follows:

For example, if you want to render a wide scene into 2 adjoining screens, you can break down the frustum into 2 asymmetric Frustums (left and right). Then, render the scene with each frustum.


An example of asymmetric frustum

Texture matrix (gl_texture)

The texture coordinates (S,T,R,Q) are multiplied by the gl_texture matrix before any texture mappings, and are identical by default. So the texture mapping to the object's position will be exactly where you assign the texture coordinates.

By changing the gl_texture, you can slide, rotate, stretch, or scale the texture.

Color Matrix (Gl_color)

The color portion is obtained by multiplying the Gl_color matrix. The matrix is used for the transformation of color spaces and color components. (The position is as follows: It can used for color space conversion and color component swaping)

The color matrix is not generic and requires a gl_arb_imaging extension (what is the gl_arb_imaging extension?). Solving

Other matrix examples

Glpushmatrix ()--presses the current matrix into the matrix stack

Glpopmatrix ()-pops the current matrix from the current matrix stack

Glloadidentity ()--Set the current matrix as the equivalent matrix

GLLOADMATRIX{FD} (m)--Replace the current matrix with a matrix M

GLLOADTRANSPOSEMATRIX{FD} (m)--Replace the current matrix with its transpose matrix

GLMULTMATRIX{FD} (m)--Multiplies the current matrix by the matrix m, and updates the current matrix

GLMULTTRANSPOSEMATRIX{FD} (m)--Multiplies the current matrix by its transpose matrix, and updates the current matrix

GLGETFLOATV (Gl_modelview_matrix, m)--load 16 values of the Gl_modelview matrix into M

Example 1:modelview Matrix

This demo app shows how to use the Gltranslatef () and GLROTATEF () operations Gl_modelview

Download Link:

Matrixmodelview.zip:

Http://www.songho.ca/opengl/files/matrixModelView.zip

(OS X 10.6+) matrixmodelview_mac.zip:http://www.songho.ca/opengl/files/matrixmodelview_mac.zip

Note that all OpenGL functions are implemented in ModelGL.h and ModelGL.cpp under both Mac and Windows, and the files in these packages are identical.

The demo app uses a custom 4x4 class (linked to: http://www.songho.ca/opengl/gl_matrix.html) as the default OpenGL matrix example, in order to specify model and camera transformations.

There are 3 matrix objects in ModelGL.cpp: Matrixmodel,matrixview and Matrixmodelview. Each matrix holds a pre-computed transformation. These matrix elements are then passed to OpenGL's function--glloadmatrix (). The actual drawing program should look like this:

Using the OpenGL default matrix function, the same code is as follows:

Example of a projection matrix:

The Demo app shows how to manipulate the projection transformation using the Glfrustum () and Glortho () functions.

Source and binary File download links:

Matrixprojection.zip:
Http://www.songho.ca/opengl/files/matrixProjection.zip

Matrixprojection_mac.zip (OS X 10.6+):

Http://www.songho.ca/opengl/files/matrixProjection_mac.zip

Similarly, ModelGL.h and ModelGL.cpp have the same files in their packages, and all OpenGL functions are placed in these files.

The Modelgl class has a custom matrix object: Matrixprojection, two member functions: Setfrustum () and Setorthofrustum ().

The function is the same as the Glfrustum () and the Glortho () function

The 16 parameters of the gl_projection matrix are shown here:

Http://www.songho.ca/opengl/gl_projectionmatrix.html

OpenGL Matrix Transformations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.