#include <stdio.h>
#include <GL/glut.h>
float angle[20]={}, diff=2;///都是0的陣列 angle;///上週程式,會增加、會減少!!!
int angleID=0;///現在有20個angle, angle[0], angle[1]... angle[angleID}
void timer(int t)///上週程式,會增加、會減少!!!
{///timer()什麼都不做
/// glutTimerFunc( 30, timer, t+1 );///上週程式 設定新的timer
}
///想要用 mouse() 及 motion() 來操作關節!!!! (期末作品),第07週教過
int oldX=0, oldY=0;
void mouse( int button, int state, int x, int y )
{///mouse按下去時, 記下現在的位置, 冰箱門打開, 把大象放進去, 冰箱門關起來
oldX = x;
oldY = y;
}
void motion( int x, int y )
{///mouse motion時, 會叫這個 motion()
angle[angleID] += x - oldX;
oldX = x;
glutPostRedisplay();///上週教display();
}
void keyboard(unsigned char key, int x, int y){///keyboard()需要glutKeyboardFunc(keyboard)
if( key=='0' ) angleID=0; ///之後timer()會改變關節 angle[0]
if( key=='1' ) angleID=1; ///之後timer()會改變關節 angle[1]
if( key=='2' ) angleID=2; ///之後timer()會改變關節 angle[2]
if( key=='3' ) angleID=3; ///之後timer()會改變關節 angle[3]
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();///Step05-2
glutSolidTeapot( 0.3 );///身體
glPushMatrix();///Step05-2
glTranslatef(-0.3,0,0);
glRotatef(angle[0], 0,0,1);
glTranslatef(-0.3,0,0);
glutSolidTeapot( 0.3 );///左手臂, 但是,要用 T-R-T移位置
glPushMatrix();
glTranslatef(-0.3, 0,0);
glRotatef(angle[1], 0,0,1);
glTranslatef(-0.3, 0,0);
glutSolidTeapot( 0.3 );///左手肘
glPopMatrix();
glPopMatrix();///Step05-2
glPushMatrix();///Step05-2
glTranslatef(+0.3,0,0);
glRotatef(-angle[2], 0,0,1);
glTranslatef(+0.3,0,0);
glutSolidTeapot( 0.3 );///右手臂
glPushMatrix();///step05-3
glTranslatef(+0.3,0,0);
glRotatef(-angle[3], 0,0,1);
glTranslatef(+0.3,0,0);
glutSolidTeapot( 0.3 );///右手肘
glPopMatrix();///Step05-3
glPopMatrix();///Step05-2
glPopMatrix();///Step05-2
glutSwapBuffers();
}
int main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow( "week16 many angles" );
glutMouseFunc( mouse );
glutMotionFunc( motion );
glutKeyboardFunc( keyboard );
glutDisplayFunc( display );
glutTimerFunc( 0, timer, 0);
glutMainLoop();
}///先不放貼圖, 先不放打光
之後利用上週教的存檔讀檔配合for迴圈印出20個角度
///新專案,刪光光,放入上週的程式,改成week16,把 float angle 改成 float angle[20]
#include <stdio.h>
#include <GL/glut.h>
float angle[20]={}, diff=2;///都是0的陣列 angle;///上週程式,會增加、會減少!!!
int angleID=0;///現在有20個angle, angle[0], angle[1]... angle[angleID}
void timer(int t)///上週程式,會增加、會減少!!!
{///timer()什麼都不做
/// glutTimerFunc( 30, timer, t+1 );///上週程式 設定新的timer
}
///想要用 mouse() 及 motion() 來操作關節!!!! (期末作品),第07週教過
int oldX=0, oldY=0;
void mouse( int button, int state, int x, int y )
{///mouse按下去時, 記下現在的位置, 冰箱門打開, 把大象放進去, 冰箱門關起來
oldX = x;
oldY = y;
}
void motion( int x, int y )
{///mouse motion時, 會叫這個 motion()
angle[angleID] += x - oldX;
oldX = x;
glutPostRedisplay();///上週教display();
}
FILE * fout = NULL;
void keyboard(unsigned char key, int x, int y){///keyboard()需要glutKeyboardFunc(keyboard)
if( key=='0' ) angleID=0; ///之後timer()會改變關節 angle[0]
if( key=='1' ) angleID=1; ///之後timer()會改變關節 angle[1]
if( key=='2' ) angleID=2; ///之後timer()會改變關節 angle[2]
if( key=='3' ) angleID=3; ///之後timer()會改變關節 angle[3]
if( key=='s' ) {
if(fout == NULL)fout = fopen("angle.txt","w+");
for(int i=0;i<20;i++) printf("%.2f",angle[i]);
printf("\n");
for(int i=0;i<20;i++) fprintf(fout,"%.2f",angle[i]);
fprintf(fout,"\n");
}
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();///Step05-2
glutSolidTeapot( 0.3 );///身體
glPushMatrix();///Step05-2
glTranslatef(-0.3,0,0);
glRotatef(angle[0], 0,0,1);
glTranslatef(-0.3,0,0);
glutSolidTeapot( 0.3 );///左手臂, 但是,要用 T-R-T移位置
glPushMatrix();
glTranslatef(-0.3, 0,0);
glRotatef(angle[1], 0,0,1);
glTranslatef(-0.3, 0,0);
glutSolidTeapot( 0.3 );///左手肘
glPopMatrix();
glPopMatrix();///Step05-2
glPushMatrix();///Step05-2
glTranslatef(+0.3,0,0);
glRotatef(-angle[2], 0,0,1);
glTranslatef(+0.3,0,0);
glutSolidTeapot( 0.3 );///右手臂
glPushMatrix();///step05-3
glTranslatef(+0.3,0,0);
glRotatef(-angle[3], 0,0,1);
glTranslatef(+0.3,0,0);
glutSolidTeapot( 0.3 );///右手肘
glPopMatrix();///Step05-3
glPopMatrix();///Step05-2
glPopMatrix();///Step05-2
glutSwapBuffers();
}
int main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow( "week16 many angles" );
glutMouseFunc( mouse );
glutMotionFunc( motion );
glutKeyboardFunc( keyboard );
glutDisplayFunc( display );
glutTimerFunc( 0, timer, 0);
glutMainLoop();
}///先不放貼圖, 先不放打光
///新專案,刪光光,放入上週的程式,改成week16,把 float angle 改成 float angle[20]
#include <stdio.h>
#include <GL/glut.h>
float angle[20]={}, diff=2;///都是0的陣列 angle;///上週程式,會增加、會減少!!!
int angleID=0;///現在有20個angle, angle[0], angle[1]... angle[angleID}
void timer(int t)///上週程式,會增加、會減少!!!
{///timer()什麼都不做
/// glutTimerFunc( 30, timer, t+1 );///上週程式 設定新的timer
}
///想要用 mouse() 及 motion() 來操作關節!!!! (期末作品),第07週教過
int oldX=0, oldY=0;
void mouse( int button, int state, int x, int y )
{///mouse按下去時, 記下現在的位置, 冰箱門打開, 把大象放進去, 冰箱門關起來
oldX = x;
oldY = y;
}
void motion( int x, int y )
{///mouse motion時, 會叫這個 motion()
angle[angleID] += x - oldX;
oldX = x;
glutPostRedisplay();///上週教display();
}
#include <stdio.h>
FILE * fout = NULL;
FILE * fin = NULL;
void keyboard(unsigned char key, int x, int y){///keyboard()需要glutKeyboardFunc(keyboard)
if( key=='0' ) angleID=0; ///之後timer()會改變關節 angle[0]
if( key=='1' ) angleID=1; ///之後timer()會改變關節 angle[1]
if( key=='2' ) angleID=2; ///之後timer()會改變關節 angle[2]
if( key=='3' ) angleID=3; ///之後timer()會改變關節 angle[3]
if( key=='s' ) {
if(fout == NULL)fout = fopen("angle.txt","w+");
for(int i=0;i<20;i++) printf("%.2f",angle[i]);
printf("\n");
for(int i=0;i<20;i++) fprintf(fout,"%.2f",angle[i]);
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();///Step05-2
glutSolidTeapot( 0.3 );///身體
glPushMatrix();///Step05-2
glTranslatef(-0.3,0,0);
glRotatef(angle[0], 0,0,1);
glTranslatef(-0.3,0,0);
glutSolidTeapot( 0.3 );///左手臂, 但是,要用 T-R-T移位置
glPushMatrix();
glTranslatef(-0.3, 0,0);
glRotatef(angle[1], 0,0,1);
glTranslatef(-0.3, 0,0);
glutSolidTeapot( 0.3 );///左手肘
glPopMatrix();
glPopMatrix();///Step05-2
glPushMatrix();///Step05-2
glTranslatef(+0.3,0,0);
glRotatef(-angle[2], 0,0,1);
glTranslatef(+0.3,0,0);
glutSolidTeapot( 0.3 );///右手臂
glPushMatrix();///step05-3
glTranslatef(+0.3,0,0);
glRotatef(-angle[3], 0,0,1);
glTranslatef(+0.3,0,0);
glutSolidTeapot( 0.3 );///右手肘
glPopMatrix();///Step05-3
glPopMatrix();///Step05-2
glPopMatrix();///Step05-2
glutSwapBuffers();
}
int main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow( "week16 many angles" );
glutMouseFunc( mouse );
glutMotionFunc( motion );
glutKeyboardFunc( keyboard );
glutDisplayFunc( display );
glutTimerFunc( 0, timer, 0);
glutMainLoop();
}///先不放貼圖, 先不放打光
之後把
if(fout == NULL)fout = fopen("angle.txt","w+");
for(int i=0;i<20;i++) printf("%.2f",angle[i]);
printf("\n");
for(int i=0;i<20;i++) fprintf(fout,"%.2f",angle[i]);
fprintf(fout,"\n");
這幾行程式複製到void motion()裡面
就可以讓他在妳移動時就自動存檔



沒有留言:
張貼留言