Special topic in OpenGL coordinate transformation

Source: Internet
Author: User
Tags in degrees

OpenGL through the camera simulation, can achieve the most basic three-dimensional transformation of computer graphics, namely, geometric transformation, projection transformation, clipping transformation, viewport transformation, and OpenGL also realizes the Matrix stack and so on. Understanding the content of the coordinate transformation, even if really into the wonderful three-dimensional world.
first, the display of three-dimensional objects in OpenGL
(i) Coordinate system
In the real world, all objects have three-dimensional characteristics, but the computer itself can only deal with numbers, display two-dimensional graphics, the three-dimensional objects and two-dimensional data linked to the only link is the coordinates.
In order to digitize a displayed three-dimensional object, a coordinate system is defined in the space where the object being displayed is located. The length units of this coordinate system and the direction of the axes are appropriate for the description of the displayed object, which is called the world coordinate system. The world coordinate system is always constant.
OpenGL also defines the concept of a local coordinate system, the so-called local coordinate system, that is, the coordinate system with the center of the object as the coordinate origin, the object rotation or translation operations are all around the local coordinate system, when the object model for rotation or translation operations, the local coordinate system also performs the corresponding rotation or translation operations. It is important to note that if the object model is scaled, the local coordinate system is scaled accordingly, and if the scaling is different on each axis, then the local axes may no longer be perpendicular to each other after the rotation operation. Whether you are converting in a world coordinate system or in a local coordinate system, the program code is the same, except that the different coordinate systems consider the conversion differently.
After the computer has processed the digitized display object, it should be displayed on the graphic display, which will define a two-dimensional Cartesian coordinate system, which is called the screen coordinate system, on the graphic display screen. The coordinates of this coordinate axis are usually taken parallel to the edge of the screen, the coordinate origin is taken in the lower left corner, and the unit of length is often taken as a pixel.
(ii) Camera simulation of three-dimensional objects
To illustrate what transformations need to be made between three-dimensional objects and two-dimensional images, we have introduced a camera simulation that assumes a camera to capture the world, and in the camera viewfinder there is a process of transformation between the human eye and the real world.


Figure i. Various coordinate transformations in the camera simulation OpenGL

From a three-dimensional object to a two-dimensional image, as with a camera, there are usually several steps to follow:
1, the camera placed on the tripod, so that it aligned to three-dimensional scenery, it is equivalent to OpenGL in the position of the adjustment point of view, that is, the viewpoint transformation (viewing transformation).
2, the three-dimensional object in the scene in the appropriate position, it is equivalent to OpenGL model transformation (Modeling transformation), that is, the model rotation, translation and scaling.
3, select the camera lens and focus, so that three-dimensional objects projected on two-dimensional film, it is equivalent to OpenGL in the three-dimensional model projection to a two-dimensional screen process, that is OpenGL projection transformation (Projection transformation), OpenGL projection in two ways, That is, orthographic projection and perspective projection. In order for the displayed object to be displayed in the appropriate position, size and direction, it must be projected. Sometimes a three-dimensional visual body (viewing Volume) can be defined in order to highlight part of a graphic and only display a portion of the graph. Orthographic projection is generally a box of the visual body, perspective projection is generally a bevel-like scene. Only objects in the scene can be projected on the display plane, while the rest of the body cannot.
4, wash the negative, determine the size of two-dimensional photo, it is quite with OpenGL in the viewport transformation (Viewport transformation) (In the screen window can define a rectangle, called the viewport (Viewport), The graphics shown in the viewport are displayed in the viewport, and the scope and dimensions of the scene are shown on the screen.
Through the above steps, an object in a three-dimensional space can be represented by a corresponding two-dimensional plane object, and it can be displayed correctly on a two-dimensional computer screen. In general, the display of three-dimensional objects is as follows:


Figure two or three-dimensional object display process

Ii. several transformations in OpenGL
The various transformations in OpenGL are implemented by matrix operations, specifically, when a conversion command is emitted, the command generates a 4x4-level transformation matrix (the object coordinates in OpenGL are all homogeneous coordinates, i.e. (x, Y, Z, W), so all transformation matrices use a 4x4 matrix), The current matrix is multiplied by this transformation matrix to generate a new current matrix. For example, for vertex coordinates V, the conversion command is usually issued before the vertex coordinate command, if the current matrix is C and the transformation command is composed of M, then after the conversion command is issued, the resulting new matrix is cm, which is multiplied by the vertex coordinate V to form the new vertex coordinate CMV. The above procedure illustrates that the last transformation command before a vertex is drawn in a program is first applied to the vertex. This also shows that in OpenGL programming, the actual transformation order is the opposite of the specified order.
(a) Viewpoint transformation
The viewpoint transformation determines the position and direction of the object's viewpoint in the scene, as mentioned above, it is like placing a camera in the scene and aligning the camera with the object to be photographed. The camera (i.e., the viewpoint) is positioned at the origin of the coordinate system (the initial orientation of the camera points to the Z-negative axis), which is consistent with the default position of the object model, and it is clear that the camera and the object overlap without the viewpoint transformation.
The command that performs the viewpoint transformation is the same as the command that performs the model transformation, and think that when you take the object with the camera, we can keep the object in position and move the camera away from the object, which is equivalent to the viewpoint transformation, and we can also keep the camera in a fixed position and move the object away from the camera. Thus, in OpenGL, rotating the object counterclockwise is equivalent to rotating the camera clockwise. Therefore, we must consider the viewpoint transformation and model transformation together, and it is meaningless to consider these two transformations separately.
In addition to using the Model Transformation command to perform a viewpoint conversion, the OpenGL utility Library provides the Glulookat () function, which has three variables that define the position of the viewpoint, the reference point of the camera's sighting direction, and the camera's upward direction. The prototype of this function is:

void Glulookat (gldouble eyex,gldouble eyey,gldouble eyez,gldouble centerx,gldouble centery,gldouble upx,GLdouble upy, Gldouble Upz);

The function defines the viewpoint matrix and multiplies it by the current matrix. Eyex,eyey,eyez defines the position of the viewpoint, and the CenterX, CenterY, and Centerz variables specify the position of the reference point, which is usually the point on the center axis of the scene that the camera is aiming at, and the UPX, Upy, and Upz variables specify the direction of the upward vector.
In general, the viewpoint transformation operation is emitted before the model transformation operation so that the model transformation first acts on the object. The vertex of an object in the scene moves to the desired position after the model is transformed, and then the scene is positioned as a viewpoint. The model transformation and the viewpoint transformation together form the Model view matrix.
(ii) Model transformation
Model transformations are performed in the world coordinate system. By default, the center of the object model is positioned at the center of the coordinate system. OpenGL in this coordinate system, there are three commands that can be transformed by a model.
1. Model Translation

GLTRANSLATE{FD} (TYPE x,type y,type z);

The function pans the object (or moves the local coordinate system by the same measure) along the x, y, and z axes with the specified z/y values.
2. Model rotation

GLROTATE{FD} (TYPE angle,type x,type,y,type z);

The first variable in the function angle the angle of the rotation of the model, in degrees, and the latter three variables represent the axis counterclockwise rotation of an object at the origin (0,0,0) point (x, Y, z). For example, the result of Glrotatef (45.0,0.0,0.0,1.0) is rotated 45 degrees around the z axis.
3. Model Scaling

GLSCALE{FD} (TYPE x,type y,type z);

The function can zoom in and out of the object along the x, Y, Z axes respectively. The three parameters in the function are the proportional transformation factors in the X, y, and z axes, respectively. The default is 1.0, that is, the object does not change. The object's y-axis ratio is 2.0 and the rest is 1.0, which means that the cube becomes cuboid.
(c) Projection transformation
After the transformation of the Model view, the object in the scene is placed in the desired position, but since the display can only display three-dimensional objects with a two-dimensional image, it is necessary to reduce the dimension by projection (the projection transformation is similar to the camera's lens).
In fact, the purpose of the projection transformation is to define a visual body, so that the extra parts of the scene outside the cut off, the final entry into the image is only the relevant parts of the scene body. Projections include perspective projection (Perspective Projection) and orthographic projection (orthographic Projection).
Perspective projection, in line with people's psychological habits, that is, from the point of view near the object is large, far from the viewpoint of the object is small, far to the pole is to disappear, become the vanishing points. Its visual body is similar to a rib that has been cut at the top and bottom, that is, the bevel. This projection is often used for animations, visual simulations, and many other aspects that have a realistic reflection.
There are two OpenGL perspective projection functions, where the prototype of the function Glfrustum () is:

void Glfrustum (gldouble left,gldouble right,gldouble bottom,gldouble top,gldouble near,gldouble FAR);

It creates a perspective view body. Its operation is to create a perspective projection matrix and multiply the matrix by the current matrix. The parameter of this function only defines the three-dimensional space coordinates of the lower-left corner and the upper-right corner of the clipping plane, i.e. (left,bottom,-near) and (right,top,-near); The last parameter far is the z negative value of the long clipping plane, the lower-left corner and the upper-right corner of the point are empty The coordinates are generated automatically by the function according to the perspective projection principle. Near and far represent the distance from the viewpoint, and they are always positive. This function forms the visual body of the three shown.


Figure III, perspective projection film and television view body

Another perspective function is:

void Gluperspective (gldouble fovy,gldouble aspect,gldouble znear, gldouble Zfar);

It also creates a symmetrical perspective body, but its parameters are defined in front of the difference, the parameter Fovy defines the angle of view in the X-z plane, the range is [0.0, 180.0]; The parameter aspect is the ratio of the projection plane width to the height The parameters znear and far are the distances between the clipping surface and the distance from the z negative axis to the viewpoint, which are always positive values.


Figure IV, perspective projection film and television view body

When the above two functions default, the viewpoints are at the origin, and the line of sight is pointing in the negative direction along the z axis.
Orthographic projection, also known as parallel projection. The visual body of this projection is a rectangular parallel pipe, which is a cuboid, shown in five. The most important feature of orthographic projection is that the size of the object after projection is constant no matter how far away the object is from the camera. Such projections are often used in architectural blueprints and computer-aided design, which require the size and angle of the objects to be projected to be the same, so that the proportions of objects are correct during construction or manufacturing.


Picture five, the projection of the film and television scene body

OpenGL Orthographic projection functions also have two, a function is:

void Glortho (gldouble left,gldouble right,gldouble bottom,gldouble top, gldouble near,gldouble FAR)

It creates a parallel visual body. In fact, the operation of this function is to create a orthographic projection matrix, and multiply this matrix by the current matrix. The near clipping plane is a rectangle, the rectangle left bottom corner point three-dimensional space coordinates is (left,bottom,-near), the upper right corner is (right,top,-near), the far clipping plane is also a rectangle, the lower left corner space coordinates is (LEFT,BOTTOM,-FAR), The upper-right corner is (Right,top,-far). All near and far values are either positive or negative at the same time. If there are no other transformations, the direction of the orthographic projection is parallel to the z axis, and the viewpoint is toward the z negative axis. This means that objects are negative in front of the viewpoint, far and near, and objects are positive at the far and near when they are behind the viewpoint.
Another function is:

void gluortho2d (gldouble left,gldouble right,gldouble bottom,gldouble top)

It is a special orthographic projection function, which is mainly used for projection on two-dimensional images to two-dimensional screens. Its near and far default values are 1.0 and 1.0, and all two-dimensional objects have a z-coordinate of 0.0. So its clipping surface is a rectangle with the lower-left corner (Left,bottom) and the upper-right corner (Right,top).

(iv) Viewport transformation.
The viewport transformation is to display objects projected in the scene in the two-dimensional viewport plane. Using the camera simulation, we can easily understand that the viewport transformation is similar to the enlargement and reduction of the photo. In computer graphics, it is defined as a geometric transformation, a projection transformation, and the cropping of the transformed object is displayed in the screen window within the specified area, which is usually a rectangle, called a viewport. The relevant functions in OpenGL are:

Glviewport (Glint x,glint y,glsizei width, glsizei height);

This function defines a viewport. The function parameter (x, y) is the bottom-left point coordinate of the viewport in the screen window's coordinate system, and the parameter width and height are the width and heights of the viewport, respectively. By default, the parameter value (0, 0, winwidth, winheight) refers to the actual size of the screen window. All of these values are in pixels and are all integers.
(5) Cropping transformation
In OpenGL, in addition to the six clipping planes defined by the visual body (top, bottom, left, right, front, and back), the user can define one or more additional clipping planes themselves to remove extraneous targets from the scene, as shown in six.


Figure VI, additional clipping plane

Additional planar clipping functions are:
1, void Glclipplane (Glenum plane,const gldouble *equation);
The function parameter equation points to an array of four coefficient values, the four coefficients of a, B, C, D, respectively, of the clipping plane ax+by+cz+d=0. Therefore, a clipping plane can be determined by these four coefficients. The parameter plane is Gl_clip_planei (i=0,1,...), which specifies the clipping face number.
Before calling an additional clipping function, you must start glenable (Gl_clip_planei) so that the currently defined clipping plane is valid, and when an additional clipping plane is no longer called, the corresponding additional cropping function can be turned off with gldisable (Gl_clip_planei).
The following example not only illustrates the use of additional clipping functions, but also calls the gluperspective () Perspective projection function, which allows the reader to understand its usage. The routines are as follows:

#include "Glos.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
void Myinit (void);
void CALLBACK Myreshape (Glsizei W, Glsizei h);
void CALLBACK display (void);
void CALLBACK display (void)
{
Gldouble Eqn[4] = {1.0, 0.0, 0.0, 0.0};
Glclear (Gl_color_buffer_bit);
GLCOLOR3F (1.0, 0.0, 1.0);
Glpushmatrix ();
Gltranslatef (0.0, 0.0,-5.0);
/* Clip the left part of Wire_sphere:x<0 * *
Glclipplane (GL_CLIP_PLANE0, eqn);
Glenable (GL_CLIP_PLANE0);
Glrotatef (-90.0, 1.0, 0.0, 0.0);
Auxwiresphere (1.0);
Glpopmatrix ();
Glflush ();
}
void Myinit (void)
{
Glshademodel (Gl_flat);
}
void CALLBACK Myreshape (Glsizei W, Glsizei h)
{
Glviewport (0, 0, W, h);
Glmatrixmode (gl_projection);
Glloadidentity ();
Gluperspective (60.0, (glfloat) w/(Glfloat) H, 1.0, 20.0);
Glmatrixmode (Gl_modelview);
}
void Main (void)
{
Auxinitdisplaymode (Aux_single | AUX_RGB);
Auxinitposition (0, 0, 500, 500);
Auxinitwindow ("arbitrary Clipping Planes");
Myinit ();
Auxreshapefunc (Myreshape);
Auxmainloop (display);
}

(vi) operation of the Matrix stack
Before you tell the matrix stack, let's start by introducing two basic OpenGL matrix operation functions:
1, void GLLOADMATRIX{FD} (const TYPE *m)
Sets the value of an element in the current matrix. The function parameter *m is a pointer to 16 elements (M0, M1, ..., M15), and these 16 elements are the elements in the current matrix M, arranged in the following way:
M = | M0 M4 M8 M12 |
| M1 M5 M9 M13 |
| M2 M6 M10 M14 |
| M3 M7 M11 M15 |
2, void GLMULTMATRIX{FD} (const TYPE *m)
The current matrix is used to multiply the matrix specified by the *m and the result is stored in the *m. The current matrix can be either a matrix specified with Glloadmatrix () or a composite result of other matrix transformation functions.
OpenGL's matrix stack refers to a particular area of memory that is dedicated to storing the matrix data. In general, matrix stacks are often used to construct models that are inherited, that is, complex models that consist of some simple objects. Matrix stacks are advantageous for the linkage and independence of multiple transformations in complex model motions. Because all matrix manipulation functions such as Glloadmatrix (), Glmultmatrix (), glloadidentity () handle only the current matrix or stack top matrix, the other matrices below the stack are unaffected. The stack operation function has the following two:
void Glpushmatrix (void);
This function means that all matrices are pressed into the stack sequentially, the top matrix is a backup of the second matrix, and the number of matrices cannot be pressed too much, otherwise an error occurs.
void Glpopmatrix (void);
This function represents the matrix at the top of the pop-up stack, making the original second matrix the top matrix, accepting the current operation, so that the original top matrix is destroyed, and when there is only one matrix in the stack, the eject operation cannot occur, otherwise an error occurs.

Special topic in OpenGL coordinate transformation

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.