2021年4月28日 星期三

播放音效

 


今天上課教如何撥放音效
下載下來的音樂要放到執行目錄
(freeglut\bin裡面)
程式碼:

#include <windows.h>
#include <GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
void keyboard(unsigned char key, int x,int y)
{
    if(key=='1') PlaySound("do.wav", NULL, SND_ASYNC);
    if(key=='2') PlaySound("re.wav", NULL, SND_ASYNC);
    if(key=='3') PlaySound("mi.wav", NULL, SND_ASYNC);
}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week10");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}




播放mp3檔要先下載MP3_MCI檔案
並放在專案目錄裡面,下載mp3檔
程式碼:
#include <windows.h>
#include <GL/glut.h>
#include "CMP3_MCI.h"
CMP3_MCI mp3;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
void keyboard(unsigned char key, int x,int y)
{
    if(key=='1') PlaySound("do.wav", NULL, SND_ASYNC);
    if(key=='2') PlaySound("re.wav", NULL, SND_ASYNC);
    if(key=='3') PlaySound("mi.wav", NULL, SND_ASYNC);
}
int main(int argc, char**argv)
{
    mp3.Load("music.mp3");
    mp3.Play();
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week10");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}
再增加一個按滑鼠也有音效的程式碼:
#include <windows.h>
#include <GL/glut.h>
#include "CMP3_MCI.h"
CMP3_MCI mp3;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
void keyboard(unsigned char key, int x,int y)
{
    if(key=='1') PlaySound("do.wav", NULL, SND_ASYNC);
    if(key=='2') PlaySound("re.wav", NULL, SND_ASYNC);
    if(key=='3') PlaySound("mi.wav", NULL, SND_ASYNC);
}
void mouse(int buttom, int state, int x, int y)
{
    if(state==GLUT_DOWN) PlaySound("shot.wav", NULL, SND_ASYNC);
}
int main(int argc, char**argv)
{
    mp3.Load("music.mp3");
    mp3.Play();
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week10");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMainLoop();
}



這個程式碼可以讓滑鼠左中右鍵分別有不同音效

#include <windows.h>
#include <GL/glut.h>
#include "CMP3_MCI.h"
CMP3_MCI mp3;
CMP3_MCI shot1,shot2,shot3;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
void keyboard(unsigned char key, int x,int y)
{
    if(key=='1') PlaySound("do.wav", NULL, SND_ASYNC);
    if(key=='2') PlaySound("re.wav", NULL, SND_ASYNC);
    if(key=='3') PlaySound("mi.wav", NULL, SND_ASYNC);
}
void mouse(int button, int state, int x, int y)
{
    if(state==GLUT_DOWN&&button==GLUT_LEFT_BUTTON) shot1.Play();
    if(state==GLUT_DOWN&&button==GLUT_MIDDLE_BUTTON) shot2.Play();
    if(state==GLUT_DOWN&&button==GLUT_RIGHT_BUTTON) shot3.Play();
}
int main(int argc, char**argv)
{
    shot1.Load("1.wav"); shot2.Load("2.mav"); shot3.Load("3.wav");
    mp3.Load("music.mp3");
    mp3.Play();
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week10");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMainLoop();
}


沒有留言:

張貼留言

距地表面160 Week11

 #include "glm.h" GLMmodel* pmodel = NULL; void drawmodel(void) {     if (!pmodel) { pmodel = glmReadOBJ("data/porsche.obj...