Step01:
1.File-New-Empty File 另存新檔改名week14-1.cpp
寫入
#include <stdio.h>
int main(){
FILE *fout=fopen("file.txt","w+");
printf("HELLO WORLD\n");
fprintf(fout,"HELLO WORLD\n");
fclose(fout);
}
2.File-New-Empty File 另存新檔改名week14-2.cpp
寫入
#include <stdio.h>
int main(){
FILE *fout=fopen("file.txt","w+");
fprintf(fout,"3.1415926\n");
fclose(fout);
float angle=0;
FILE * fin=fopen("file.txt","r");
fscanf(fin,"%f",&
angle);//沒加&會當掉
angle);//沒加&會當掉
printf("讀到角度:%f",angle);
fclose(fin);
}

Step02:
1.載入上週程式
#include <GL/glut.h>
float angle[20],oldX=0;
int angleID=0;
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;
}
void mouse(int button,int state,int x,int y){
oldX=x;
}
void motion(int x,int y){
angle[angleID]+=(x-oldX);
oldX=x;
glutPostRedisplay();///重畫畫面
}
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf(0.3,0.5,-0.3,-0.5);///身體
glPushMatrix();
glTranslatef(0.3,0.4,0);///手臂掛到身體
glRotatef(angle[0],0,0,1);///旋轉
glTranslatef(-0.3,-0.4,0);///把手臂旋轉中心放到中心
glColor3f(1,0,0);///紅
glRectf(0.3,0.5,0.7,0.3);///上手臂
glPushMatrix();
glTranslatef(0.7,.4,0);
glRotatef(angle[1],0,0,1);///旋轉
glTranslatef(-0.7,-0.4,0);///把手臂旋轉中心放到中心
glColor3f(0,1,0);///紅
glRectf(0.7,0.5,1.0,0.3);///下手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.3,0.4,0);///手臂掛到身體
glRotatef(angle[2],0,0,1);///旋轉
glTranslatef(0.3,-0.4,0);///把手臂旋轉中心放到中心
glColor3f(1,0,0);///紅
glRectf(-0.3,0.5,-0.7,0.3);///上手臂
glPushMatrix();
glTranslatef(-0.7,.4,0);
glRotatef(angle[3],0,0,1);///旋轉
glTranslatef(0.7,-0.4,0);///把手臂旋轉中心放到中心
glColor3f(0,1,0);///紅
glRectf(-0.7,0.5,-1.0,0.3);///下手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char** argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(600,600);
glutCreateWindow("week13_RECT_TRT");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
2.加入
#include <stdio.h>
FILE * fout=NULL;
void myWrite(){
if(fout==NULL) fout=fopen("file.txt","w");
for(int i=0;i<20;i++){
printf("%.1f",angle[i]);
fprintf(fout,"%.1f",angle[i]);
}
}
motion()加入
myWrite();
3.跳行
printf("\n");//跳行
fprintf(fout,"\n);
4.開新檔加入
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
int angleID=0;
FILE * fout=NULL,*fin=NULL;
void myWrite(){
if(fout==NULL) fout=fopen("file.txt","w");
for(int i=0;i<20;i++){
printf("%.1f ",angle[i]);
fprintf(fout,"%.1f ",angle[i]);
}
printf("\n");//跳行
fprintf(fout,"\n");
}
void myRead(){
if(fout!=NULL){fclose(fout);fout=NULL;}
if(fin==NULL) fin=fopen("file.txt","r");
for(int i=0;i<20;i++){
fscanf(fin,"%.f",&angle[i]);
}
glutPostRedisplay();//重畫畫面
}
void keyboard(unsigned char key,int x,int y){
if(key=='r')myRead();
if(key=='0')angleID=0;
if(key=='1')angleID=1;
if(key=='2')angleID=2;
if(key=='3')angleID=3;
}
void mouse(int button,int state,int x,int y){
oldX=x;
}
void motion(int x,int y){
angle[angleID]+=(x-oldX);
myWrite();
oldX=x;
glutPostRedisplay();///重畫畫面
}
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glRectf(0.3,0.5,-0.3,-0.5);///身體
glPushMatrix();
glTranslatef(0.3,0.4,0);///手臂掛到身體
glRotatef(angle[0],0,0,1);///旋轉
glTranslatef(-0.3,-0.4,0);///把手臂旋轉中心放到中心
glColor3f(1,0,0);///紅
glRectf(0.3,0.5,0.7,0.3);///上手臂
glPushMatrix();
glTranslatef(0.7,.4,0);
glRotatef(angle[1],0,0,1);///旋轉
glTranslatef(-0.7,-0.4,0);///把手臂旋轉中心放到中心
glColor3f(0,1,0);///紅
glRectf(0.7,0.5,1.0,0.3);///下手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.3,0.4,0);///手臂掛到身體
glRotatef(angle[2],0,0,1);///旋轉
glTranslatef(0.3,-0.4,0);///把手臂旋轉中心放到中心
glColor3f(1,0,0);///紅
glRectf(-0.3,0.5,-0.7,0.3);///上手臂
glPushMatrix();
glTranslatef(-0.7,.4,0);
glRotatef(angle[3],0,0,1);///旋轉
glTranslatef(0.7,-0.4,0);///把手臂旋轉中心放到中心
glColor3f(0,1,0);///紅
glRectf(-0.7,0.5,-1.0,0.3);///下手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char** argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(600,600);
glutCreateWindow("week13_RECT_TRT");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
Step03timer:
1.寫入
#include <GL/glut.h>
#include <stdio.h>
void timer(int t){
printf("起床現在:%d\n",t);
}
void display(){
}
int main(int argc,char** argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week14_timer");
glutTimerFunc(1000,timer,1);
glutTimerFunc(2000,timer,2);
glutTimerFunc(3000,timer,3);
glutTimerFunc(4000,timer,4);
glutTimerFunc(5000,timer,5);
glutDisplayFunc(display);
glutMainLoop();
}
2.自動
#include <GL/glut.h>
#include <stdio.h>
void timer(int t){
printf("起床現在:%d\n",t);
glutTimerFunc(1000,timer,t+1);
}
void display(){
}
int main(int argc,char** argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week14_timer");
glutTimerFunc(5000,timer,0);
glutDisplayFunc(display);
glutMainLoop();
}
3.加入聲音在timer()
PlaySound("do.wav",NULL,SND_ASYNC);







沒有留言:
張貼留言