筆記
電腦圖學 Week14
1.寫檔、讀檔
2.關節、做動畫
3.計時器 glutTimerFunc (時間,timer,參數t)
4.播放聲音
Step1-1 為了記錄動畫的動作,我們需要寫檔。fopen()開檔,fprintf0()寫檔,fclose()關檔
寫檔 File Output (記錄)
0. File-New-Empty File,存檔成 week14-1_fprintf 目錄裡的 week14-1.cpp
#include <stdio.h>
int main()
{///檔案指標 fout 開啟檔案(檔名 , 寫)
FILE * fout = fopen("file.txt", "w+");
printf( "Hello world\n");
fprintf(fout,"Hello World\n");
fclose(fout);///關閉檔案
}
1. fopen() 開啟檔案
2. printf() => fprintf() File Output
3. fclose() 關閉檔案
Step1-2 讀檔 File Input
0. File-New-EMpty File,存檔到 week14-2_fprintf_fscanf 目錄裡的 week14-2.cpp
1. 打剛剛 week14-1.cpp拿來用
2 . 另外一組 `FILE * fin = fopen("檔名", "r");
3. scanf() => fscanf() File Input
4. fclose()
#include <stdio.h>
int main()
{///檔案指標 fout 開啟檔案(檔名 , 寫)
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);///&符號要記得
printf("讀到了角度:%f",angle);
fclose(fin);
}
Step1-3 把上週的程式 week13_rect_many_TRT 拿來改
1. 先設立資料夾在桌面 命名為week14-3_ angle_fprintf
2. 開新專案 命名為week14-3.cpp
3. 貼上上週many_TRT程式碼,並新增紅色程式碼
#include<GL/glut.h>
#include<stdio.h>
float angle[20] ,oldX=0;
int angleID=0;
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]);///檔案印出來
}///這邊沒有fclose
}
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);
myWrite();
oldX=x;
glutPostRedisplay();///請GLUT重畫畫面
}
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);///(3)整體移位
glRotatef(angle[0],0,0,1);///(2)
glTranslatef(-0.3,-0.4,0);///(1)把軸心放在正中心
glColor3f(1,0,0);///紅
glRectf(0.3,0.5,0.7,0.3);///上手臂
glPushMatrix();
glTranslatef(0.7,0.4,0);///(3)整體移位
glRotatef(angle[1],0,0,1);///(2)
glTranslatef(-0.7,-0.4,0);///(1)把軸心放在正中心
glColor3f(0,1,0);///綠
glRectf(0.7,0.5,1.0,0.3);///下手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();///左邊
glTranslatef(-0.3,0.4,0);///(3)整體移位
glRotatef(angle[2],0,0,1);///(2)
glTranslatef(0.3,-0.4,0);///(1)把軸心放在正中心
glColor3f(1,0,0);///紅
glRectf(-0.3,0.5,-0.7,0.3);///上手臂
glPushMatrix();
glTranslatef(-0.7,0.4,0);///(3)整體移位
glRotatef(angle[3],0,0,1);///(2)
glTranslatef(0.7,-0.4,0);///(1)把軸心放在正中心
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");
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
Step2-1 開始做動畫
0. File-New-Project,GLUT專案 week14_angles_fprintf_fscanf
1. copy前一個版本的程式來修改
2. 要寫void myRead()
3. keyboard()裡,按下 ' r ' 呼叫 myRoad()
#include <stdio.h>
FILE * fin =NULL;
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;
} |
Step2-2 為啥產生的檔案file.txt放在奇怪的目錄 C:\Users\user\Desktop\freeglut\gin ,將它放在程式專案的那個目錄!
0. What! 好奇怪!!! file.txt放在奇怪的目錄
1. Why? 原來是歷史餘毒 GLUT專案 需要 freeglut.dll 所以 working_dir被設到 freeglut\bin 裡面
2. How? 在 .cbp CodeBlocks Project 檔裡, 有 working_dir的設定 工作執行的目錄
3. 使用 Notepad++ 把 .cbp 的 working_dir="....." 改 working_dir="." 小數點
4. Notepad++存檔後, CodeBlocks Reload它, 便成功了!!!!
5. 小心 歷史餘毒 freeglut.dll 要再修正, 放到week14_angles_fprintf_fscanf 程式專案的同目錄, 再執行時, 便可以 mouse motion 動動動, 按r重播,而且file.txt 也放在你的程式專案目錄囉!!!
沒裝 Notepad++ 的, 快去下載台灣人 Don Ho 侯今吾先生寫的 Notepad++
Step3-1 Timer
0. 看電腦按R的速度
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. 動畫30秒,程式呼叫程式#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();
}
播放聲音 PlaySound()
0. 準備好聲音檔
1. 繼續改 week14_timer
2. #incloude <Mmsystem.h>
3. playsound("檔名")
沒有留言:
張貼留言