要求:给定三维图元(立方体,四面体和球),把图元拼接成象形动物;
要求图元必须按照层级组织;
实现:创建二次几何体,对于每一次变换,建立数据结构存取图元(glPushMatrix()和glPopMatrix();)。
Just translate, rotate, andscale each one to get the right size, shape, position, and orientation.
Transformation Hierarchies:
Transformations apply to everything under them—start at the bottom and work your way up.
example:(左手)
//左手
glColor3f(1.0, 1.0, 1.0);
glPushMatrix();
gluQuadricDrawStyle(quadratic, GLU_SMOOTH);
glTranslatef(0.5, 0.3, 0.3);
glRotatef((GLfloat)shoulder2 + 90, 1.0, 1.0, 0.0);
gluCylinder(quadratic, 0.05, 0.05, 0.5, 20.0, 8.0);
glPushMatrix();
glColor3f(0.3, 1.0, 1.0);
glTranslatef(0.0, 0.0, 0.5);
//左手轴
glRotatef((GLfloat)elbow + 90, 0.0, 1.0, 0.0);
//glTranslatef(0.5, 0.0, 0.0);
gluCylinder(quadratic, 0.05, 0.05, 0.5, 20.0, 8.0);
glPopMatrix();
glPopMatrix();
glColor3f(1.0, 1.0, 1.0);
glPushMatrix();
gluQuadricDrawStyle(quadratic, GLU_SMOOTH);
glTranslatef(-0.5, 0.3, 0.3);
glRotatef((GLfloat)shoulder1 - 20, 0.0, 1.0, 1.0);
gluCylinder(quadratic, 0.05, 0.05, 1.0, 20.0, 8.0);
glPopMatrix();
添加键盘监听
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case'b':
shoulder1 = (shoulder1 +5) % 360;
glutPostRedisplay();
break;
case'v':
shoulder2 = (shoulder2 + 5) % 360;
glutPostRedisplay();
break;
case'm':
leg1 = (leg1 + 5) % 360;
glutPostRedisplay();
break;
case'n':
leg2= (leg2 +5) % 360;
glutPostRedisplay();
break;
case'c':
elbow = (elbow+ 5) % 360;
glutPostRedisplay();
break;
default:
break;
}
}
输出:
键盘转动之后: