The coordinate transformation of OpenGL learning (UP)

Source: Internet
Author: User

coordinate transformation is the basis of a deep understanding of the three-dimensional world, very important. The first part of this study is to understand several concepts: viewpoint Transformation, model transformation, projection transformation, viewport transformation.

In the real world, all objects have three-dimensional features, but the computer itself can only handle numbers, showing two-dimensional graphics, so we want to use three-dimensional objects to represent the two-dimensional data, the point of contact is coordinates . There are two types of coordinates in OpenGL three-dimensional space: the World coordinate system and the local coordinate system .

① World coordinate system: always fixed. For example, the Earth's orbit around the sun is the transformation of the world's coordinates, with the establishment of a world coordinate system centered on the sun in the center of the solar system.

② local coordinate system: the center of the object itself. The Earth's autobiography is the rotation transformation in the local coordinate system, and the scaling is also the transformation under the local coordinate system.

The transformation of three-dimensional object to two-dimensional image is realized by viewpoint transformation, model transformation, projection transformation and viewport transformation , which is similar to the process of photographing with a camera.

(1) Viewpoint transform--select observation point, Align Object

(2) Model transformation----Adjust the object (i.e. the model), including rotation, scaling, panning

(3) Projection transform--zoom in or out of an object, project onto a two-dimensional canvas or a negative film to form a two-dimensional image

(4) The viewport transforms--and finally determines where the resulting two-dimensional image is displayed on the screen and the size of the display window, which can be understood to scale the image.

The operation of the projection transform and the matrix stack is slightly more complicated, put in the next article, because the viewport transformation is the projection transformation of the formation of the two-dimensional image to do the operation, and the projection transformation together with the next article, here first introduce the other two transformations.

First, let's see how the changes are in OpenGL:

   The various transformations in OpenGL are achieved by matrix operations, whether moving, rotating, or scaling, by multiplying a new matrix on the basis of the current matrix to achieve the goal. Assuming that the current matrix isC(that is, the matrix at the top of the stack, if it has not been transformed before, the stack top matrix default is the unit matrix), the Rotation transformation command constitutes a matrix ofR, the new current matrix generated after the transform command is issued isCR, The matrix is multiplied by the vertex coordinatesv, thus constituting a new vertex coordinate(CR) v, this completes the rotation transformation, and the current matrix becomesCR,。 If you are doing multiple transformations in succession, such as the rotation transformation above, proceed with the move transformationT, the new current matrix is generated asCRT, multiply the vertex coordinates with this total transformation matrixvTo get the final vertex coordinates(CRT) v, due to the combined rate of matrix multiplication,(CRT) v=(C (R (Tv) )), which shows that before the vertex is drawn in the program, thethe last transform command first acts on top of a vertex。 This also shows that in OpenGL programming,The actual transformation order is the opposite of the specified order, which is actually moving first, then rotating.

Entity drawing

Before you talk about transformations, you first need to know the specific functions of drawing three-dimensional objects in OpenGL in order to understand the given program instances. The entity drawing functions in OpenGL are mainly in the glut library (glut is a utility library, GL is the core library, and Glu is part of the package for GL). Below are some of the commonly used entity drawing functions, and give code examples:

Using code Examples:

void display (void) {    glcolor3f (0.0f, 1.0f, 0.0f);//Green    glutsolidsphere (0.5,200,200);//Radius r=0.5, fine degree transverse longitudinal 200 parts    Glflush ();}
void display (void) {    glcolor3f (0.0f, 1.0f, 0.0f);//Green    Glutsolidsphere (0.5,10,10)///RADIUS r=0.5, fine degree transverse longitudinal 10 parts, will find that The higher the degree of precision, the more accurate this is the embodiment of calculus. But high-precision performance requirements high, too high rendering efficiency will be reduced, the slang card    glflush ();}

Example diagram:

Mesh Sphere:

void display (void) {    glcolor3f (0.0f, 1.0f, 0.0f);    Glutwiresphere (0.5,20,20);//Mesh sphere, more stereoscopic    glflush ();}

Example diagram:

Next, change the view image.

1. Viewpoint transformation

Viewpoints, which can be used in the form of the photographer's point of view, the three-dimensional object is regarded as the person or thing being photographed, then the change of viewpoint is the photographer looking for the shooting angle.
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 position of void Glulookat (gldouble eyex,gldouble eyey,gldouble eyez,//(eyex,eyey,eyez) viewpoint;    gldouble CenterX, Gldouble centery,gldouble centerz,//(Centerx,centery,centerz) The point of gaze, and the connection between the viewpoints to form a gaze direction; gldouble upx,gldouble Upy    , Gldouble Upz//(UPX,UPY,UPZ) upward direction, that is, the direction of the head);

Point of view than a person's eyes, when the eyes look at the object, when the head (standing) to see the object and head to the ground (upside down) See the object is not the same. You can also tilt the head left and right to look at objects from different angles. So, Glulookat's upward vector is used to determine this direction. However, by default, the head is upside-down, that is, the upward vector is (0, 1, 0).

Here, the direction of the head and the direction of the line of sight with parameters to set is not easy to understand.

The upward direction (that is, the head direction) once determined, the scope of the line of sight is determined, and the person standing still, can see the angle is limited.

Or use the code example test to see the effect, we use the teapot as a three-dimensional entity. The orientation of the screen outward is the z-axis forward.

1> by default, head direction: (0,1,0)
void display (void) {    glcolor3f (0.0f, 1.0f, 0.0f);//Brush color    {        glmatrixmode (Gl_modelview); The scope of the Glulookat function is the Model view, where the setting is necessary        glloadidentity ();//initializes the current matrix as the unit matrix, does not set the default unit matrix        Glulookat (0,0,1,  0,0,0,  0,1,0);//head direction: Y-Axis forward, the point of view is the z-axis positive 1 units (off-screen), along the z-axis to see the Origin        Glutwireteapot (0.5);//Draw Teapot    }    Glflush ();}

2> out of sight, you can't see the object.
/* Viewpoint in the y-axis positive 1 units, and the direction of the head is the y-axis forward, at this time, the line of sight can not see the origin of the foot, unless the bow, and the bow of the words will change the direction of the head, so the following settings are not see the object! */glulookat (0,1,0,  0,0,0,  0,1,0);
3> View Settings
Glulookat (0,1,0,  0,0,0,  0,0,-1);//Overlook: Head forward, view point along the y-axis looking at the origin

2. Model transformation

From the point of view of relative movement, changing the position and direction of the observer (i.e. the photographer) and changing the position and orientation of the object itself (the subject being photographed) is equivalent. In fact, the viewpoint itself is a model, you can also call the Model transformation function.

There are three main types of model transformations:

(1) Model translation

void Mydisplay (void) {    glcolor3f (0.0f, 1.0f, 0.0f);    {        Glmatrixmode (gl_modelview);         This is the default setting when there is no panning, and (x, y, z) translates to 0        Gltranslatef (0,0,0);        Draw a teapot: Be sure to do the transformation in the drawing, first transform is to change the matrix stack of the current matrix, and then use the current matrix to draw the graph to do the transformation        glutwireteapot (0.5);//Draw a teapot    }    Glflush ();}
It is important to note that, by default, our viewpoint position is at (0,0,1) (float parameter), so if you pan beyond the point where you can see the object, you cannot see it. such as Gltranslatef (0,0,2): To the head of the point of view, the natural invisible object.

Gltranslatef (0.5,0.5,0);//offset above right

(2) Model rotation

void Mydisplay (void) {    glcolor3f (0.0f, 1.0f, 0.0f);    {        Glmatrixmode (gl_modelview);         Glrotatef (45, 0,0,1);//After three parameters determine a direction point, and the origin of the line to form the axis of rotation, here is the z axis, that is, around the z axis rotation 45 degrees        glutwireteapot (0.5);    }    Glflush ();}

(3) Model scaling

void Mydisplay (void) {    glcolor3f (0.0f, 1.0f, 0.0f);    {        Glmatrixmode (gl_modelview);         Glscalef (1,3,0.5);//Represents the scaling Glutwireteapot (0.5) of the x, Y, Z axis respectively        ;    }    Glflush ();}




The coordinate transformation of OpenGL learning (UP)

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.