Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

coordinate transformation - OpenGL translation before and after a rotation

The following is code (taken from http://www.glprogramming.com/red/chapter03.html) regarding how to draw a robot's arm and shoulder and rotating them by some user input:

glPushMatrix();
    glTranslatef (-1.0, 0.0, 0.0);
    glRotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0);
    glTranslatef (1.0, 0.0, 0.0);
    glPushMatrix();
        glScalef (2.0, 0.4, 1.0);
        glutWireCube (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);
    glPushMatrix();
        glScalef (2.0, 0.4, 1.0);
        glutWireCube (1.0);
    glPopMatrix();
glPopMatrix();  

glutSwapBuffers();

I understand the code for drawing the shoulder and rotating it. For the shoulder: first we translate it one unit back along the x-axis so that when we do the rotation, it rotates along the origin as a pivot. Then we translate it back (forward one unit on the x-axis). This transformation will be applied to the cube that has been scaled.

Now, my question is for the elbow. Why is there a translation forward on the x-axis both before and after the rotate?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Now, my question is for the elbow. Why is there a translation forward on the x-axis both before and after the rotate?

If you want to imagine how the matrix operations change the model, then you need to "read" the operations in the reverse order. This is, because the current matrix of the matrix stack is multiplied by the matrix which is specified by the new operation and the matrices are stored in column-major order (fixed function pipeline).

Start with the elbow cube

glutWireCube(1.0f);

arm 01

Scale the elbow

glPushMatrix();
glScalef(2.0f, 0.4f, 1.0f);
glutWireCube(1.0f);
glPopMatrix();

arm 02

Move it to the right

glTranslatef(1.0f, 0.0f, 0.0f);

arm 03

Rotate the elbow

glRotatef(45.0f, 0.0f, 0.0f, 1.0f);

arm 04

Move the rotated elbow to the right

glTranslatef(1.0f, 0.0f, 0.0f);

arm 05

Draw the shoulder cube

glutWireCube(1.0f);

arm 06

Scale the shoulder

glPushMatrix();
glScalef(2.0f, 0.4f, 1.0f);
glutWireCube(1.0f);
glPopMatrix();

arm 07

Move the arm (elbow and shoulder) to the right

glTranslatef(1.0f, 0.0f, 0.0f);

arm 08

Rotate the arm

glRotatef(-15.0f, 0.0f, 0.0f, 1.0f);

arm 09

Move the arm to its final position (to the left)

glTranslatef(-1.0f, 0.0f, 0.0f);

arm 10


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...