OpenGL viewpoint transformation, model transformation, projection transformation, and view Transformation

Source: Internet
Author: User

OpenGL uses camera simulation to realize the most basic 3D transformations in computer graphics, including geometric transformations, projection transformations, cropping transformations, and viewport transformations. At the same time, OpenGL also implements matrix stacks. Understanding the content of coordinate transformation, even if it truly enters the wonderful 3D world.

I,OpenGLDisplay of 3D objects in

(1) Coordinate System

In the real world, all objects have three-dimensional characteristics, but the computer itself can only process numbers and display two-dimensional images. The only link connecting three-dimensional objects and two-dimensional data is coordinates.

To digitize the displayed 3D object, define a coordinate system in the space of the displayed object. The length unit of the coordinate system and the direction of the coordinate axis should be suitable for the description of the displayed object. This coordinate system is called the world coordinate system. The world coordinate system remains unchanged.

OpenGL also defines the concept of a local coordinate system. The so-called local coordinate system, that is, the coordinate system takes the center of an object as the coordinate origin. operations such as rotation or translation of an object are performed around the local coordinate system, when the object model is rotated or translated, the local coordinate system also performs the rotation or translation operation. Note that if you scale the object model, the local coordinate system also scales accordingly. If the scaling ratio is different on each coordinate axis, after rotation, the local coordinate axes may not be perpendicular to each other. The program code is the same for conversion in the world coordinate system or in the local coordinate system, but the conversion methods are different for different coordinate systems.

After a computer processes a Digital Display object and displays it on a graphic display, it defines a two-dimensional Cartesian coordinate system on the screen of the graphic display. This coordinate system is called the screen coordinate system. The coordinates of the coordinate system are usually taken in the direction parallel to the edge of the screen. The coordinate origin is taken in the lower left corner, and the length unit is usually taken as a pixel.

(2) camera simulation of 3D objects

To illustrate the transformation between a 3D object and a two-dimensional image, we introduced a Camera (Camera) simulation method, assuming that a Camera is used to take a picture of the world, in the camera's viewfinder, there is a transformation process between the human eye and the real world.

Figure 1. The camera simulates various coordinate transformations in OpenGL

From a 3D object to a two-dimensional image, just like taking a photo with a camera, the following steps are usually taken:

1. Place the camera on a tripod to align it with a 3D scene. It is equivalent to adjusting the viewpoint position in OpenGL, that is, Viewing Transformation ).

2. Place a 3D object in a proper position in the scene. It is equivalent to Modeling Transformation in OpenGL, that is, rotating, translating, and scaling the model.

3. Select a camera lens and focus on it so that a 3D object is projected on a two-dimensional film. It is equivalent to the process in OpenGL to project a three-dimensional model to a two-dimensional screen, that is, Projection Transformation of OpenGL. There are two Projection methods in OpenGL: normal Projection and Perspective Projection. In order for the displayed object to be displayed in a proper position, size, and direction, projection is required. Sometimes, to highlight a part of the image, only a part of the image is displayed. In this case, you can define a three-dimensional visual object (Viewing Volume ). Normal projection is generally a cube visual body, while perspective projection is generally a pyramid-like visual body. Only objects in the visual view can be projected on the display plane. Other parts cannot.

4. Rinse the negative film to determine the size of the Two-Dimensional Photo. It is equivalent to the Viewport Transformation in OpenGL (a rectangle can be defined in the screen window, called the Viewport ), specifies the range and size of the scene displayed on the screen.

II,OpenGLSeveral transformations in

Various conversions in OpenGL are implemented through matrix operations. Specifically, when a conversion command is issued, this command will generate a 4x4 transformation matrix (the object coordinates in OpenGL are always homogeneous, that is, (x, y, z, w ), therefore, all transformation matrices use a 4x4 matrix). The current matrix is multiplied by this conversion matrix to generate a new current matrix. For example, for vertex coordinate v, the conversion command is usually issued before the vertex coordinate command. If the current matrix is C and the conversion command matrix is M, after the conversion command is issued, the generated new current matrix is CM. This matrix is multiplied by the vertex coordinate v to form the new vertex coordinate. The above process shows that the last transformation command before the vertex is drawn in the program first acts on the vertex. This also shows that in OpenGL programming, the actual conversion order is the opposite of the specified order.

(1) Viewpoint Change

The viewpoint transformation determines the object's viewpoint position and direction in the scene. As mentioned above, it is like a camera is placed in the scene to align the camera with the object to be taken. It is indeed time-saving. The camera (I .e. the viewpoint) locates at the origin of the coordinate system (the camera initially points to the Z-negative axis). It is consistent with the default position of the object model. Obviously, if the viewpoint is not changed, the camera and the object are overlapped.

The command for executing viewpoint transformation is the same as the command for executing model conversion. Think about it. When shooting an object with a camera, we can keep the object's position unchanged and move the camera away from the object, this is equivalent to the viewpoint transformation. In addition, we can keep the fixed position of the camera and move the object away from the camera. This is equivalent to model conversion. In OpenGL, rotating an object counterclockwise is equivalent to rotating the camera clockwise. Therefore, we must combine viewpoint conversion and model conversion, and it is meaningless to consider these two conversions separately.

In addition to using the model conversion command to execute viewpoint conversion, the OpenGL utility Library also provides the gluLookAt () function, which has three variables, the position of the viewpoint, the reference point of the camera's orientation, and the upward direction of the camera are defined respectively. The function is prototype:

Void gluLookAt (GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx, GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy, GLdouble upz );

This function defines the viewpoint matrix and times it by the current matrix. Eyex, eyey, and eyez define the position of the viewpoint. The centerx, centery, and centerz variables specify the position of the reference point, which is usually the point on the central axis of the scene targeted by the camera; the upx, upy, and upz variables specify the direction of the UP vector.

Generally, the viewpoint conversion operation is issued before the model conversion operation, so that the model conversion takes effect on the object first. In a scenario, the vertex of an object moves to the desired position after model conversion, and then positions the viewpoint in the scenario. Model conversion and viewpoint conversion form a visual matrix of the model.

(2) model transformation

Model transformation is performed in the world coordinate system. The center of the object model is located at the center of the coordinate system. OpenGL has three commands in this coordinate system, which can be used for model transformation.

1. Model Translation

GlTranslate {fd} (TYPE x, TYPE y, TYPE z );

This function uses the specified x, y, and z values to translate objects along the x axis, y axis, and z axis (or move the local coordinate system according to the same value ).

2. Model Rotation

GlRotate {fd} (TYPE angle, TYPE x, TYPE, y, TYPE z );

Angle, the first variable in this function, specifies the angle of the model rotation. The unit is degree. The last three variables indicate the point (x, y, z) at the origin (0, 0) the line of the line is the axis clockwise rotating object. For example, 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 );

This function can zoom in and out objects along the x, y, and Z axes respectively. The three parameters in the function are proportional transformation factors in the x, y, and Z axes directions. The lack of time is 1.0, that is, the object does not change. The Y-axis ratio of the object in the program is 2.0, and the remaining values are 1.0, that is, the cube is changed to a cube.

(3) Projection Transformation

After the transformation of the model Visual View, the objects in the scene are placed in the desired position, but because the display can only display three-dimensional objects with two-dimensional images, therefore, we need to reduce the dimension by projection (projection transformation is similar to selecting the camera lens ).

In fact, the purpose of projection transformation is to define a visual object, so that the excess part of the visual object is cropped out, and the final part that enters the image is only the relevant part of the visual body. Projection includes Perspective Projection and Orthographic Projection.

Perspective Projection, in line with people's psychological habits, that is, objects close to the viewpoint are large, objects far away from the viewpoint are small, and objects far to the Pole disappear, becoming a point of extinction. Its visual body is similar to an edges that have been cut at the top and bottom, that is, a prism. This projection is usually used for animation, visual simulation, and many other aspects with authenticity reflection.

There are two OpenGL perspective projection functions, in which the prototype of the glFrustum () function is:

Void glFrustum (GLdouble left, GLdouble Right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far );

It creates a perspective visual body. The operation is to create a pivot projection matrix and multiply it by the current matrix. The parameters of this function only define the three-dimensional space coordinates of the lower-left and upper-right vertices of the near-cropping plane, namely (left, bottom,-near) and (right, top,-near ); the final far parameter is the Z negative value of the far-cropping plane. The coordinate between the point space in the lower-left corner and the upper-right corner is automatically generated by the function based on the perspective projection principle. Near and far indicate the distance from the viewpoint. They are always positive values.

Another perspective function is:

Void gluPerspective (GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar );

It also creates a symmetric perspective visual body, but its parameters are defined as different above, the fovy parameter defines the angle of the field of view in the X-Z plane, range is [0.0, 180.0]; the aspect parameter is the ratio of the width and height of the projection plane. The zNear and Far parameters are the distance from the distance crop surface along the Z negative axis to the viewpoint, respectively, and they are always positive values.

The above two functions are time-saving, and the viewpoint is at the origin. The line of sight points to the negative direction along the Z axis.

Normal projection, also called Parallel projection. The projected visual object is a parallel pipe of a rectangle, that is, a cube, as shown in Figure 5. The biggest feature of normal projection is that the size of the projected object remains unchanged no matter how far the object is from the camera. This projection is usually used in the drawing of building blueprints and computer-aided design. These industries require that the size and angle of the projected objects remain unchanged, so that the proportion of the objects during construction or manufacturing is correct.

OpenGL has two projection functions, one of which is:

Void glOrtho (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far)

It creates a parallel visual object. In fact, this function is used to create a normal projection matrix and multiply it by the current matrix. The near-crop plane is a rectangle, the three-dimensional coordinate of the point in the lower left corner of the rectangle is (left, bottom,-near), and the point in the upper right corner is (right, top,-near ); the far crop plane is also a rectangle. the coordinate of the point space in the lower left corner is (left, bottom,-far), and the point in the upper right corner is (right, top,-far ). All near and far values are both positive and negative. If there are no other transformations, the direction of the projection is parallel to the Z axis, and the viewpoint is oriented to the Z negative axis. This means that far and near are both negative values before the object's viewpoint, and far and near are both positive values after the object's viewpoint.

Another function is:

Void gluOrtho2D (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top)

It is a special normal projection function, which is mainly used for the projection of two-dimensional images to two-dimensional screens. The default values of near and far are-1.0 and 1.0, respectively. The zcoordinates of all two-dimensional objects are 0.0. Therefore, the cropping area is a rectangle with the (left, bottom) and (right, top) points in the lower left corner.

(4) modify the view.

The object projected in the visual body is displayed on a two-dimensional view plane. Using the camera simulation method, we can easily understand that the viewport transformation is similar to the zoom-in and zoom-out of a photo. In computer graphics, it is defined to display objects after geometric transformation, projection transformation, and cropping transformation in the area specified in the screen window, which is usually a rectangle, it is called a viewport. Related functions in OpenGL are:

GlViewport (GLint x, GLint y, GLsizei width, GLsizei height );

This function defines a view. Function parameters (x, y) are the coordinate points in the lower left corner of the screen window coordinate system. The width and height parameters are the width and height of the view respectively. The parameter value (0, 0, winWidth, winHeight) indicates the actual size of the screen window. All these values are in pixels and all are integer values.

(5) cropping and Transformation

In OpenGL, apart from the six cropping planes (top, bottom, left, right, front, and back) defined by the visual object, you can also define one or more additional cropping planes to remove irrelevant targets in the scenario.

The additional flat cropping function is:

Void glClipPlane (GLenum plane, Const GLdouble * equation );

The equation function parameter points to an array with four numbers. These four coefficients are A, B, C, and D values of the cropping plane Ax + By + Cz + D = 0. Therefore, the four coefficients can determine a cropping plane. The plane parameter is GL_CLIP_PLANEi (I =,...) and specifies the cropping surface number.

Before calling the additional cropping function, you must first start the glable (GL_CLIP_PLANEi) function to make the currently defined cropping plane valid. When no additional cropping plane is called, glDisable (GL_CLIP_PLANEi) is available) disable the corresponding additional cropping function.

The following example not only describes the usage of the additional cropping function, but also calls the gluPerspective () Perspective Projection Function. You can understand the usage in detail. The routine is 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 );

}

(6) operations on the matrix Stack

Before talking about the Matrix stack, we will first introduce two basic OpenGL matrix operation functions:

Void glLoadMatrix {fd} (const TYPE * m)

Set the element values in the current matrix. * M is a pointer to 16 elements (m0, m1,..., m15). These 16 elements are the elements in the current matrix M, which are arranged as follows:

M = | m0 m4 m8 m12 |

| M1 m5 m9 m13 |

| M2 m6 m10 m14 |

| M3 m7 m11 M15 |

2. void glMultMatrix {fd} (const TYPE * m)

Use the current matrix to multiply the matrix specified by * m and store the result in * m. The current matrix can be a matrix specified by glLoadMatrix () or a comprehensive result of other matrix transformation functions.

OpenGL's matrix stack refers to a Special Area in memory that is used to store matrix data. Generally, a matrix stack is used to construct an inherited model, that is, a complex model consisting of some simple targets. The relationship and independence between multiple transformation operations during complex model motion is very favorable. Because all matrix operation functions, such as glLoadMatrix (), glMultMatrix (), and glLoadIdentity (), only process the current matrix or the top matrix of the stack. In this way, other matrices in the stack are not affected. Stack operation functions include the following:

· Void glPushMatrix (void );

This function indicates that all matrices are pushed into the stack in sequence, and the top matrix is the backup of the second matrix. The number of matrices pushed in cannot be too large. Otherwise, an error occurs.

· Void glPopMatrix (void );

This function indicates that the matrix at the top of the stack is popped up, so that the original second matrix becomes the top matrix and accepts the current operation. Therefore, the original Top matrix is damaged. When only one matrix exists in the stack, you cannot perform the pop-up operation. Otherwise, an error occurs.

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.