使用鍵盤控制上週做的茶壺手臂的動作
用鍵盤0,1,2,3 控制手臂移動
#include <stdio.h>
#include <GL/glut.h>
float angle[20]={},diff=2;
int angleID=0;//有20個angle (angle[0],angle[1]...angle[angleID])
void timer(int t){
glutTimerFunc(30,timer,t+1);
angle[angleID] += diff;
if(angle[angleID]>90)diff=-2;
if(angle[angleID]<0) diff=+2;
glutPostRedisplay();
}
void keyboard(unsigned char key,int x ,int y){
if(key=='0')angleID=0; //左ALL動
if(key=='1')angleID=1; //左下動
if(key=='2')angleID=2; //右ALL動
if(key=='3')angleID=3; //右下動
}
void display(){
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
glColor3f( 1,0,1 );
glutSolidTeapot( 0.3 );//身體
glPushMatrix();
glTranslatef(-0.3,0,0);
glRotatef(angle[0], 0,0,1);
glTranslatef(-0.3,0,0);//左1
glutSolidTeapot( 0.3 );
glPushMatrix();
glTranslatef(-0.3, 0,0);
glRotatef(angle[1], 0,0,1);
glTranslatef(-0.3, 0,0);//左2
glutSolidTeapot( 0.3 );
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(+0.3,0,0);
glRotatef(-angle[2], 0,0,1);
glTranslatef(+0.3,0,0);//右1
glutSolidTeapot( 0.3 );
glPushMatrix();
glTranslatef(+0.3,0,0);
glRotatef(-angle[3], 0,0,1);
glTranslatef(+0.3,0,0);//右2
glutSolidTeapot( 0.3 );
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char** argv){
glutInit( &argc,argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("08160465 Week16");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutTimerFunc(0,timer,0);
glutMainLoop();
}
用mouse()和motion()移動關節
#include <stdio.h>
#include <GL/glut.h>
float angle[20]={}, diff=2;
int angleID=0;//用 mouse()、motion() 控制關節
int oldX=0, oldY=0;
void mouse( int button, int state, int x, int y ){
oldX = x;//mouse按下去紀錄現在的位置
oldY = y;
}
void motion( int x, int y ){
angle[angleID] += x - oldX;
oldX = x;
glutPostRedisplay();
}
void display(){
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
glColor3f( 1,0,1 );
glutSolidTeapot( 0.3 );//身體
glPushMatrix();
glTranslatef(-0.3,0,0);
glRotatef(angle[0], 0,0,1);
glTranslatef(-0.3,0,0);//左1
glutSolidTeapot( 0.3 );
glPushMatrix();
glTranslatef(-0.3, 0,0);
glRotatef(angle[1], 0,0,1);
glTranslatef(-0.3, 0,0);//左2
glutSolidTeapot( 0.3 );
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(+0.3,0,0);
glRotatef(-angle[2], 0,0,1);
glTranslatef(+0.3,0,0);//右1
glutSolidTeapot( 0.3 );
glPushMatrix();
glTranslatef(+0.3,0,0);
glRotatef(-angle[3], 0,0,1);
glTranslatef(+0.3,0,0);//右2
glutSolidTeapot( 0.3 );
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main( int argc, char** argv ){
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow( "08160465 Week16" );
glutMouseFunc( mouse );
glutMotionFunc( motion );
glutDisplayFunc( display );
glutMainLoop();
}
#include <stdio.h>
#include <GL/glut.h>
float angle[20]={},diff=2;
int angleID=0;
int oldX=0, oldY=0;//用 mouse()、motion() 控制關節
FILE * fout = NULL;
FILE * fin = NULL;
void mouse( int button, int state, int x, int y){
oldX = x;//mouse按下去紀錄現在的位置
oldY = y;
}
void motion(int x, int y){
angle[angleID] += x - oldX;
oldX = x;
glutPostRedisplay();//WEEK15 display();
}
void keyboard(unsigned char key, int x, int y){
if( key=='0') angleID=0;
if( key=='1') angleID=1;
if( key=='2') angleID=2;
if( key=='3') angleID=3;
if( key=='s')
{//r存檔
if(fout == NULL) fout = fopen("angle.txt", "w+");//若NULL,fopen()
for(int i=0; i<20; i++) printf("%.2f", angle[i] );//for迴圈印陣列
printf("\n");
for(int i=0; i<20; i++) fprintf(fout, "%.2f", angle[i] );//for迴圈印陣列
fprintf(fout,"\n");
}
if( key=='r')
{//讀進來
if(fin == NULL) fin = fopen("angle.txt", "r");
for(int i=0; i<20; i++) fscanf(fin, "%f", &angle[i]);
glutPostRedisplay();// 重畫畫面
}
}
void display(){
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
glColor3f( 1,0,1 );
glutSolidTeapot( 0.3 );//身體
glPushMatrix();
glTranslatef(-0.3,0,0);
glRotatef(angle[0], 0,0,1);
glTranslatef(-0.3,0,0);//左1
glutSolidTeapot( 0.3 );
glPushMatrix();
glTranslatef(-0.3, 0,0);
glRotatef(angle[1], 0,0,1);
glTranslatef(-0.3, 0,0);//左2
glutSolidTeapot( 0.3 );
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(+0.3,0,0);
glRotatef(-angle[2], 0,0,1);
glTranslatef(+0.3,0,0);//右1
glutSolidTeapot( 0.3 );
glPushMatrix();
glTranslatef(+0.3,0,0);
glRotatef(-angle[3], 0,0,1);
glTranslatef(+0.3,0,0);//右2
glutSolidTeapot( 0.3 );
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main( int argc, char** argv ){
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow( "08160465 Week16" );
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc( keyboard );
glutDisplayFunc( display );
glutMainLoop();
}



沒有留言:
張貼留言