OpenGL Zero-based coordinate transformation

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:

//in all of the following functions, radius represents the radius of the sphere, slices represents the number of vertical divisions (meridians) of the sphere, mainly the fine density of the drawing, stacks represents the number of horizontal divisions of the sphere (parallels), creates a sphere that draws the center at the origin of the model coordinates, radius of radius voidGlutwiresphere (gldouble radius, glint slices, glint stacks);//Mesh BallvoidGlutsolidsphere (gldouble radius, glint slices, glint stacks);//Solid BallvoidGlutwirecube (gldouble size);//Mesh CubevoidGlutsolidcube (gldouble size);//Solid CubesvoidGlutwiretorus (gldouble Innerradius, gldouble Outerradius, glint nsides, glint rings);//Mesh RingvoidGlutsolidtorus (gldouble Innerradius, gldouble Outerradius, glint nsides, glint rings);//Solid RingvoidGlutwirecone (gldouble radius, gldouble height, glint slices, glint stacks);//Mesh ConevoidGlutsolidcone (gldouble radius, gldouble height, glint slices, glint stacks);//Solid ConevoidGlutwireteapot (gldouble size);//Mesh TeapotvoidGlutsolidteapot (gldouble size);//Solid Teapot

Using code Examples:

 void  display (void   0.0f , 1.0f , 0.0f ); //  Green  Glutsolidsphere (0.5 , 200 , 200 ); //  radius r=0.5, depth 200 parts   Glflush ();}  
void display (void) {    glcolor3f (0.0f1.0f0.0f);   Green    glutsolidsphere (0.5,ten,ten);   radius r=0.5, fine degree transverse longitudinal 10 parts, will find, the finer degree is 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.0f1.0f0.0f);    Glutwiresphere (0.5,a); // 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.

void Glulookat (    gldouble eyex,gldouble eyey,gldouble eyez,//(eyex,eyey,eyez) position of the 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;     //(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)
voidDisplayvoid) {glcolor3f (0.0f,1.0f,0.0f);//Brush Color{glmatrixmode (gl_modelview);//the scope of the Glulookat function is the Model view, where Setup is necessaryGlloadidentity ();//initializes the current matrix to the unit matrix, without setting the default unit matrixGlulookat (0,0,1,0,0,0,0,1,0);//Head direction: Y-Axis forward, viewpoint position is z-axis forward 1 units (off-screen), along the z-axis to see the origin pointGlutwireteapot (0.5);//Drawing Teapot} glflush ();}

2> out of sight, you can't 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); // Top view: Head forward, the viewpoint looks up to the origin along the y-axis.

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   0.0f , 1.0f , 0.0f  );         {Glmatrixmode (Gl_modelview);         //  This is the default setting when there is no translation, (x, Y, z) is 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 in the current matrix, The graph to be drawn is then transformed with the current matrix  glutwireteapot (0.5 ); //  draw a teapot   

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.0f1.0f0.0f);    {        Glmatrixmode (gl_modelview);         Glrotatef (0,0,1); // The last three parameters determine a direction point, and the Origin line is the axis of rotation, here is the z axis, that is, 45 degrees        rotation around the z axis Glutwireteapot (0.5);    }    Glflush ();}

(3) Model scaling

void mydisplay (void) {    glcolor3f (0.0f1.0f0.0f);    {        Glmatrixmode (gl_modelview);         Glscalef (1,3,0.5); // scale for x, y, z axes, respectively        Glutwireteapot (0.5);    }    Glflush ();}

OpenGL Zero-based 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.