2022年5月24日 星期二

AhFatKr的電腦圖學筆記week14

 #week15

1.寫檔、讀檔

2.關節、做動畫

3.計時器、glutTimeFunc(時間,timer,參數t)

4.播放聲音

##step01-1

寫檔 File Output

0.File-New-Empty File,

1.fopen()開啟檔案

2.print()=>fprintf()

3.fclose()關閉檔案


##step01-2

讀檔File Input
0.File-New-Empty File,存檔到week14-2_fprintf_fscanf 目錄裡的week14-2.cpp
1.把剛才的week14-2.cpp拿來用
2.另外一組File *  fin =fopen("檔名","r") ;
3.scanf()=>fscanf() File Input
4.fclose()

##step01-3
把上週的程式碼拿來用
並加入本周所學
```
#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重畫畫面Re display
}
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)旋轉 對Z軸轉
        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();

        glPushMatrix();///左半部
        glTranslatef(-0.3,0.4,0);///(3)把手臂掛回身體
        glRotatef(angle[2],0,0,1);///(2)旋轉 對Z軸轉
        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();
}
```

再開一個新專案 名稱week14_angles_fpintf_fscanf
```
///a
#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]);///檔案印出來
    }///這裡老師沒有fclose
    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=='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重畫畫面Re display
}
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)旋轉 對Z軸轉
        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();

        glPushMatrix();///左半部
        glTranslatef(-0.3,0.4,0);///(3)把手臂掛回身體
        glRotatef(angle[2],0,0,1);///(2)旋轉 對Z軸轉
        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();
}
```


#step02-2
為甚麼產生的檔案file.txt放在奇怪的目錄?
1.原來是歷史餘讀 GLUT專案 需要 freeglut.dll所以working_dir被設到freeglut\bin裡面
2.在.cbp CodeBlocks Progect 檔裡,有working_dir的設定 工作執行的目錄
3.Notepad++存檔後,CodeBlocks Reload他,便成功了!!
4.小心 歷史餘毒 freeglut.dll 要再修正, 放到week14_angles_fprintf_fscanf 程式專案的同目錄, 再執行時, 便可以 mouse motion 動動動, 按r重播,而且file.txt 也放在你的程式專案目錄囉!!!

#step03-1
開一個新專案week14_timer
模擬一下早晨鬧鐘響的樣子吧
#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();
}

#step03-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();
}

##step03-3
播放聲音 PlaySound()請先下載do.wav
1.繼續改 week14_timer_one_by_one
2.加上一行#include<mmsystem.h>
3.PlaySound(do.wav,NULL,SND_ASYNC);



沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

一.     一樣先安裝且設定好freeglut,OpecCV, 開啟CodeBlocks建立新專案 week11_gundam,                 把 MyGundam.zip下載解壓縮後的data資料夾放到freeglut/bin裡面 把week09_openc...