茶壺
轉手臂的程式碼
#include <GL/glut.h>
float angle=0;///NOW
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(255,255,255);///白色的
glutSolidTeapot( 0.3 );///身體
glPushMatrix();///右邊手臂
glTranslatef(0.3, 0, 0);///(3) 掛在身體的右肩
glRotatef(angle, 0,0,1);///(2) 旋轉
glTranslatef(0.15, 0,0);///(1) 把茶壼柄,移動到畫面的中心,當旋轉軸
glColor3f(1,0,0);///紅色的
glutSolidTeapot( 0.15 );///右手臂
glPopMatrix();
glutSwapBuffers();
angle++;///NOW
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("week11 TRT");
glutIdleFunc(display);///NOW
glutDisplayFunc(display);
glutMainLoop();
}
增加手肘
程式碼變成下面
#include <GL/glut.h>
float angle=0;///NOW
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(255,255,255);///白色的
glutSolidTeapot( 0.3 );///身體
glPushMatrix();///右邊手臂
glTranslatef(0.2, 0, 0);///(3) 掛在身體的右肩
glRotatef(angle, 0,0,1);///(2) 旋轉
glTranslatef(0.15, 0,0);///(1) 把茶壼柄,移動到畫面的中心,當旋轉軸
glColor3f(1,0,0);///紅色的
glutSolidTeapot( 0.15 );///右手臂
glPushMatrix();///NOW2
glTranslatef(0.2, 0, 0);///(3) 掛在身體的右肘///NOW2
glRotatef(angle, 0,0,1);///(2) 旋轉///NOW2
glTranslatef(0.15, 0,0);///(1) 把茶壼柄,移動到畫面的中心,當旋轉軸///NOW2
glColor3f(1,0,0);///紅色的///NOW2
glutSolidTeapot( 0.15 );///右下手臂///NOW2
glPopMatrix();///NOW2
glPopMatrix();
glutSwapBuffers();
angle++;///NOW
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("week11 TRT");
glutIdleFunc(display);///NOW
glutDisplayFunc(display);
glutMainLoop();
}
增加第二個手臂->複製前面的->更改第一個translatef的x值
第二個手肘
全部程式碼
#include <GL/glut.h>
float angle1=0, angle2=0, angle3=0, angle4=0;
int jointid=1;
float angle=0;///NOW
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(255,255,255);///白色的
glutSolidTeapot( 0.3 );///身體
glPushMatrix();///右邊手臂
glTranslatef(0.2, 0, 0);///(3) 掛在身體的右肩
glRotatef(angle1, 0,0,1);///(2) 旋轉
glTranslatef(0.15, 0,0);///(1) 把茶壼柄,移動到畫面的中心,當旋轉軸
glColor3f(1,0,0);///紅色的
glutSolidTeapot( 0.15 );///右手臂
glPushMatrix();///NOW2
glTranslatef(0.2, 0, 0);///(3) 掛在身體的右肘///NOW2
glRotatef(angle2, 0,0,1);///(2) 旋轉///NOW2
glTranslatef(0.15, 0,0);///(1) 把茶壼柄,移動到畫面的中心,當旋轉軸///NOW2
glColor3f(1,0,0);///紅色的///NOW2
glutSolidTeapot( 0.15 );///右下手臂///NOW2
glPopMatrix();///NOW2
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,0,0); ///掛在身體的左肩 複製左半邊
glRotatef(-angle3,0,0,1);/// 旋轉
glTranslatef(-0.15,0,0);///把茶壺柄移動到畫面中心->當旋轉軸
///
glColor3f(1,0,0);///紅色
glutSolidTeapot(0.15);///左手臂
glPushMatrix();
glTranslatef(-0.2,0,0);///掛在身體左肘
glRotatef(-angle4,0,0,1);///旋轉
glTranslatef(-0.15,0,0);///把茶壺柄移動到畫面中心->當旋轉軸
glColor3f(1,0,0);///紅色
glutSolidTeapot(0.15);///左下手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
///angle++;///NOW
}
int oldx, oldy;
void motion(int x, int y)
{
if(jointid==1) angle1 += (x-oldx);
if(jointid==2) angle2 += (x-oldx);
if(jointid==3) angle3 += (x-oldx);
if(jointid==4) angle4 += (x-oldx);
oldx=x;
display();
}
void mouse (int button, int state, int x, int y)
{
oldx=x;
oldy=y;
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') jointid=1;
if(key=='2') jointid=2;
if(key=='3') jointid=3;
if(key=='4') jointid=4;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("week11 TRT");
glutIdleFunc(display);///NOW
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
沒有留言:
張貼留言