1.利用TRT學習如何製造關節
float angle1=0, angle2=0, angle3=0, angle4=0;
int oldx, oldy;
int jointID=1;
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();///在手臂旁邊加下手臂
glTranslatef(0.2, 0, 0);///(3) 掛在身體的右肘
glRotatef(angle2, 0,0,1);///(2) 旋轉
glTranslatef(0.15, 0,0);///(1) 把茶壼柄,移動到畫面的中心,當旋轉軸
glColor3f(1,0,0);
glutSolidTeapot( 0.15 );
glPopMatrix();///下手臂結束
glPopMatrix();
glPushMatrix();///左邊手臂
glTranslatef(-0.3, 0, 0);
glRotatef(-angle3, 0,0,1);
glTranslatef(-0.15, 0,0);
glColor3f(1,0,0);
glutSolidTeapot( 0.15 );
glPushMatrix();///在手臂旁邊加下手臂
glTranslatef(-0.3, 0, 0);
glRotatef(-angle4, 0,0,1);
glTranslatef(-0.15, 0,0);
glColor3f(1,0,0);
glutSolidTeapot( 0.15 )
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void motion(int nowx,int nowy)
{
angle += nowx-oldx; ///更改angle
oldx=nowx; ///再把現在的存成old
display();
}
void mouse(int button, int state, 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();
///紀錄一開始滑鼠按下去的X座標
}
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);///NOW3 用滑鼠mouse來控制angle
glutMouseFunc(mouse);///NOW5 要知道mouse的按鍵狀況
glutKeyboardFunc(keyboard);///NOW6 配合鍵盤
glutMainLoop();
}
沒有留言:
張貼留言