旋转前后的OpenGL翻译 [英] OpenGL translation before and after a rotation

查看:103
本文介绍了旋转前后的OpenGL翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是代码(摘自 http://www.glprogramming.com/red/第03.html 章),介绍了如何绘制机器人的手臂和肩膀以及如何通过一些用户输入来旋转手臂:

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();

我了解绘制肩膀和旋转肩膀的代码.对于肩部:首先,我们将其沿x轴向后平移一个单位,以便在进行旋转时,其沿原点作为枢轴旋转.然后,我们将其平移回去(在x轴上向前移动一个单位).此转换将应用于已缩放的多维数据集.

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.

现在,我的问题是肘部.为什么旋转前后在x轴上都向前平移?

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

推荐答案

现在,我的问题是肘部.为什么旋转前后在x轴上都向前平移?

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).

从肘部立方体开始

glutWireCube(1.0f);

缩放肘部

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

将其移至右侧

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

旋转肘部

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

将旋转的肘向右移动

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

画一个肩部立方体

glutWireCube(1.0f);

缩放肩膀

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

将手臂(肘部和肩膀)向右移动

Move the arm (elbow and shoulder) to the right

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

旋转手臂

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

将手臂移至最终位置(向左)

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

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

这篇关于旋转前后的OpenGL翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆