2023年4月26日 星期三

旻_Week11

 電腦圖學 2023-04-26 Week11

1. 主題: 鍵盤
2. 主題: 聲音
3. 主題: 滑鼠、轉動
4. 補充: 工作執行目錄

Week11-1 鍵盤
1.開GLUT專案


#include <GL/glut.h>

void display()

{

    glutSolidTeapot(0.3);

    glutSwapBuffers();

}

void keyboard(unsigned char key, int x, int y)

{

    if(key==27) exit(0);//關掉

}//按ESC鍵關掉

int main(int argc, char**argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("Week11");


    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);


    glutMainLoop();

}

用 void keyboard(), 呼叫 exit(0) 程式就結束,並return 0

2.出現茶壺圖片後按下ESC,測試是否關閉


Week11-2 音樂

1.想要播放音樂,建立一個新的專案11-2
2.程式碼:

#include <windows.h>
#include <GL/glut.h>
{
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='1') PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\do.wav",NULL,SND_ASYNC);
    if(key=='2') PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\re.wav",NULL,SND_ASYNC);
    if(key=='3') PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\mi.wav",NULL,SND_ASYNC);
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week11");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}
3. 執行結果(出現茶壺圖案,按數字鍵1、2、3,會出現Do,Re,Mi的聲音)


Week11-3 音樂自動撥放

1. 建立新的File並取名為week11-3_PlaySound.cpp

2. 加入程式碼
#include <windows.h>
int main()
{
    PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\do.wav",NULL,SND_SYNC);
    PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\do.wav",NULL,SND_SYNC);
    PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\do.wav",NULL,SND_SYNC);
    PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\re.wav",NULL,SND_SYNC);
    PlaySound("C:\\Users\\Administrator\\Desktop\\do_re_mi\\mi.wav",NULL,SND_SYNC);
}
無法成功執行
3. 在Setting--Compiler的Linker Setting加上winmm,因為實際上PlaySound是在winmm的函式庫裡

4. 執行結果(會依序撥放音檔,結束並停止)


Week11-4 聲音mp3

1. 建立新的File並取名為week11-4_glut_mp3

2. 加入幾行程式碼,修改成以下樣子



3. 將CMP3_MCI.h檔案加入week11-4_glut_mp3資料夾目錄中



4.執行聽有沒有音樂


Week11-5 音檔路徑

1. 繼續使用week11-4_glut_mp3檔案

2. 更改程式碼


3. 儲存檔案

4. 在Notepad++開啟week11-4_glut_mp3目錄中的cbp檔,並將音檔目錄更改為「.」,更改完成後,將檔案儲存,開啟codeblocks檔案,並按下 "Yes"

5. 將freeglutbinfreeglut.dll、suzumi音檔加入week11-4_glut_mp3目錄中


6.執行即可





沒有留言:

張貼留言