主題:機器人2.0 (關節轉動、存讀檔)
1. fout 、fscanf
- 用fout時,file指標fout配合模式w+
#include <stdio.h>
int main(int argc, char**argv)
{
FILE*fout=NULL;///檔案的指標
fout=fopen("new.txt","w+");///用w+模式開檔
printf("Hello World\n");
fprintf(fout,"Hello World\n");
}
- 用fscanf時,file指標fin配合模式r
FILE * fin = NULL;
fin = fopen("new1.txt","r");
char line[100];
fscanf(fin,"%s",line);
printf("what you read: %s\n",line);
fscanf(fin,"%s",line);
printf("what you read: %s\n ",line);
fin = fopen("new1.txt","r");
char line[100];
fscanf(fin,"%s",line);
printf("what you read: %s\n",line);
fscanf(fin,"%s",line);
printf("what you read: %s\n ",line);
2. 關節
- 先複製老師給的程式碼,用display畫出三個茶壺(手臂)
- 接著利用timer改變角度
#include <stdio.h>#include <GL/glut.h>float angle=0,diff=2;void timer(int t){glutTimerFunc(30,timer,t+1);angle+=diff;if(angle>90) diff=-2;if(angle<0) diff=+2;glutPostRedisplay();///重畫}void display(){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glPushMatrix();///glutSolidTeapot( 0.3 );///身體glPushMatrix();glTranslatef(-0.3,0,0);///(3)掛在正確的地方glRotatef(angle,0,0,1);///(2)旋轉glTranslatef(-0.4,0,0);///(1)把關節旋轉中心放到畫面中心glutSolidTeapot( 0.3 );///左手臂(重疊了)glPushMatrix();glutSolidTeapot( 0.3 );///左手肘(重疊了)glPopMatrix();glPopMatrix();glPopMatrix();glutSwapBuffers();}int main( int argc, char ** argv ){glutInit( &argc, argv );glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);glutCreateWindow("week15");glutTimerFunc(0,timer,0);glutDisplayFunc(display);glutMainLoop();}
執行結果:
- 利用TRT完成左手臂
void display(){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glPushMatrix();glutSolidTeapot( 0.3 );///身體glPushMatrix();glTranslatef(-0.3,0,0);///(3)掛在正確的地方glRotatef(angle,0,0,1);///(2)旋轉glTranslatef(-0.4,0,0);///(1)把關節旋轉中心放到畫面中心glutSolidTeapot( 0.3 );///左手臂(重疊了)glPushMatrix();glTranslatef(-0.3,0,0);///(3)掛在正確的地方glRotatef(angle,0,0,1);///(2)旋轉glTranslatef(-0.4,0,0);///(1)把關節旋轉中心放到畫面中心glutSolidTeapot( 0.3 );///左手肘(重疊了)glPopMatrix();glPopMatrix();glPopMatrix();glutSwapBuffers();}
執行結果:
- 用陣列整合angle
- 用鍵盤控制轉動
#include <stdio.h>#include <GL/glut.h>float angle[20]={},diff=2;///本來只有一個角度,現在有很多個,初始為0int angleID=0;///現在要改第幾個角度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=='1') angleID=1;if(key=='0') angleID=0;if(key=='2') angleID=2;if(key=='3') angleID=3;}void display(){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glPushMatrix();glutSolidTeapot( 0.3 );///身體glPushMatrix();glTranslatef(-0.3,0,0);///(3)掛在正確的地方glRotatef(angle[0],0,0,1);///(2)旋轉glTranslatef(-0.4,0,0);///(1)把關節旋轉中心放到畫面中心glutSolidTeapot( 0.3 );///左手臂(重疊了)glPushMatrix();glTranslatef(-0.3,0,0);///(3)掛在正確的地方glRotatef(angle[1],0,0,1);///(2)旋轉glTranslatef(-0.4,0,0);///(1)把關節旋轉中心放到畫面中心glutSolidTeapot( 0.3 );///左手肘(重疊了)glPopMatrix();glPopMatrix();glPopMatrix();glPushMatrix();glTranslatef(0.3,0,0);///(3)掛在正確的地方glRotatef(angle[2],0,0,1);///(2)旋轉glTranslatef(0.3,0,0);///(1)把關節旋轉中心放到畫面中心glutSolidTeapot( 0.3 );///右手臂(重疊了)glPushMatrix();glTranslatef(0.3,0,0);///(3)掛在正確的地方glRotatef(angle[3],0,0,1);///(2)旋轉glTranslatef(0.3,0,0);///(1)把關節旋轉中心放到畫面中心glutSolidTeapot( 0.3 );///右手肘(重疊了)glPopMatrix();glPopMatrix();glutSwapBuffers();}int main( int argc, char ** argv ){glutInit( &argc, argv );glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);glutCreateWindow("week15");glutKeyboardFunc(keyboard);glutTimerFunc(0,timer,0);glutDisplayFunc(display);glutMainLoop();}

沒有留言:
張貼留言