[OpenGL] thoughts on the sequence of transformations in OpenGL views and model Transformations

Source: Internet
Author: User

In view model transformation, vertex v transformation occurs in the reverse order, rather than in the order of its code. The following is an example.

Consider the following code:

glMatrixMode(GL_MODELVIEW);glLoadIdentity();glMultMatrixf(N);glMultMatrixf(M);glMultMatrixf(L);glBegin(GL_POINTS);glVertex3dv(v);glEnd();

In this Code, the model view matrix contains I, n, nm, and NML in sequence, where I represents the unit matrix. The changed vertex is nmlv. Therefore, the vertex transformation is n (m (LV )).

The following code draws the display () function of the robot arm:

Void display () {glclear (gl_color_buffer_bit); glpushmatrix (); gltranslatef (-1.0, 0.0, 0.0); glrotatef (glfloat) shoulder, 0.0, 0.0, 1.0 ); gltranslatef (1.0, 0.0, 0.0); // draw the first arm glpushmatrix (); glscalef (2.0, 0.4, 1.0); glwirecube (1.0); glpopmatrix (); gltranslatef (1.0, 0.0, 0.0); glrotatef (glfloat) elbow, 0.0, 0.0, 1.0); gltranslatef (1.0, 0.0, 0.0 ); // draw the second arm glpushmatrix (); glscalef (2.0, 0.4, 1.0); glwirecube (1.0); glpopmatrix (); glswapbuffers ();}

We can check the effect through step-by-step debugging and running.

Now comment out the code:

glTranslatef(-1.0,0.0,0.0);glRotatef((GLfloat)shoulder,0.0,0.0,1.0);glTranslatef(1.0,0.0,0.0);

And the code for drawing the second arm:

// Draw the second arm glpushmatrix (); glscalef (2.0, 0.4, 1.0); glwirecube (1.0); glpopmatrix ();

Only the code of the first segment is retained for you to view the effect.

Running result:

The scaled cube is displayed.

Then remove the following line of code comments: gltranslatef (1.0, 0.0, 0.0); (code that is under rotation)

Running result:

The result is scaled and translated.

Comment on removing this line of code: glrotatef (glfloat) shoulder, 0.0, 0.0, 1.0 );

Running result:

The result is scaled, translated, and rotated.

Uncomment this line of code: gltranslatef (-1.0, 0.0, 0.0); (code located above the rotation)

Running result:

It can be seen that it is the effect after the result of the previous step is translated.

As shown in the preceding step, the order of multiplication is the opposite to that in their code.

Finally, the Code for drawing the robot arm at both ends is provided:

// Draw the robot arm # include <Gl/glut. h> static int shoulder = 0, elbow = 0; // void Init () {glclearcolor (0.0, 0.0, 0.0, 0.0 ); glshademodel (gl_flat);} void display () {glclear (gl_color_buffer_bit); glpushmatrix (); gltranslatef (-1.0, 0.0, 0.0); glrotatef (glfloat) shoulder, 0.0, 0.0, 1.0); gltranslatef (1.0, 0.0, 0.0); // draw the first arm glpushmatrix (); glscalef (2.0, 0.4, 1.0); glwirecube (1.0 ); glpopmatrix (); gltranslatef (1.0, 0.0, 0.0); glrotatef (glfloat) elbow, 0.0, 0.0, 1.0); gltranslatef (1.0, 0.0, 0.0 ); // draw the second arm glpushmatrix (); glscalef (2.0, 0.4, 1.0); glwirecube (1.0); glpopmatrix (); glswapbuffers ();} void reshape (int w, int h) {glviewport (65.0, (glsizei) W, (glsizei) H); glmatrixmode (gl_projection); glloadidentity (); gluperspective, (glfloat) W/(glfloat) h, 1.0, 20.0); glmatrixmode (gl_modelview); glloadidentity (); gltranslatef (0.0, 0.0,-5.0 );} void keyboard (unsigned char key, int X, int y) {Switch (key) // and E keys control the Rotation Angle of both ends of the arm {Case's ': shoulder = (shoulder + 5) % 360; fig (); break; Case's ': Shoulder = (shoulder-5) % 360; fig (); break; case 'E': Elbow = (elbow + 5) % 360; fig (); break; Case 'E': Elbow = (elbow-5) % 360; fig (); break; default: break;} int main (INT argc, char ** argv) {gluinit (& argc, argv); gluinitdisplaymode (glu_double | glu_rgb); gluinitwindowsize (400,400 ); gluinitwindowposition (100,100); glucreatewindow (argv [0]); Init (); gludisplayfunc (Display); glureshapefunc (reshape); glukeyboardfunc (keyboard); glumainloop (); Return 0 ;}

Running effect (the S and E keys can be used to control the Rotation Angle of the two ends of the arm ):

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.