2020年5月13日 星期三

Week12

Step1.讓程式可以發出聲音
(1)建立專案,並將聲音放在freeglut-MinGW-3.0.0-1.mp->freeglut->bin
(2)使用外掛
(3)播放聲音的函式

*** SYNC 同步,ASYNC 不同步



****運用mouse函式  記得要在下面加上 glutMouseFunc(mouse)





Step2.鍵盤鋼琴
(1)從moodle下載7個音階,並放在執行目錄裡
(2)修改 keyboard 函式的內容,並在 switch case 內加入程式碼

程式碼如下:

case '1':            PlaySound("Do.wav",NULL,SND_ASYNC );            break;
        case '2':            PlaySound("Re.wav",NULL,SND_ASYNC );            break;
        case '3':            PlaySound("Mi.wav",NULL,SND_ASYNC );            break;
        case '4':            PlaySound("Fa.wav",NULL,SND_ASYNC );            break;
        case '5':            PlaySound("Sol.wav",NULL,SND_ASYNC );            break;
        case '6':            PlaySound("La.wav",NULL,SND_ASYNC );            break;
        case '7':            PlaySound("Si.wav",NULL,SND_ASYNC );            break;




Step3.播放自己的聲音
#include <windows.h> //要使用 windows.h 的一些內建#include <mmsystem.h>#include <stdio.h>int main(){    PlaySound("Queen.wav",NULL,SND_ASYNC ); //不同步、不等他    printf("Hello World\n"); // 立刻執行下一行    int n;    scanf("%d",&n);//只要輸入完整數 n 立刻就結束了}


用更BEST的音樂讀檔"CMP3_MCI.h"
(到moodle下載)

///只留下有陣列有light字樣的打光
#include <GL/glut.h>
#include <stdio.h>
///float angle=0,angle2=0,angle3=0,angle4=0, X=150, Y=150, oldX=0, oldY=0;
float angle[20], X=150, Y=150, oldX=0, oldY=0;
int ID=0;
void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glutSolidTeapot(0.2);
        glPushMatrix();
            glTranslatef(0.2, 0, 0);
            glRotatef(angle[0], 0.0f, 0.0f, 1.0f);
            glTranslatef(0.2, 0, 0);
            glutSolidTeapot(0.2);

        glPushMatrix();
            glTranslatef(0.2, 0, 0);
            glRotatef(angle[1], 0.0f, 0.0f, 1.0f);
            glTranslatef(0.2, 0, 0);
            glutSolidTeapot(0.2);
        glPopMatrix();
      glPopMatrix();


      glPushMatrix();
            glTranslatef(-0.2, 0, 0);
            glRotatef(angle[2], 0.0f, 0.0f, 1.0f);
            glTranslatef(-0.2, 0, 0);
            glutSolidTeapot(0.2);

        glPushMatrix();
            glTranslatef(-0.2, 0, 0);
            glRotatef(angle[3], 0.0f, 0.0, 1.0f);
            glTranslatef(-0.2, 0, 0);
            glutSolidTeapot(0.2);
        glPopMatrix();
      glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
    if(key=='1') ID=1;
    if(key=='2') ID=2;
    if(key=='3') ID=3;
    if(key=='4') ID=4;
    if(key=='5') ID=5;
}
#include "CMP3_MCI.h"
#include <windows.h>
#include <mmsystem.h>
CMP3_MCI myMP3;
void mouse(int button, int state, int x, int y)
{
    oldX = x; oldY = y;
    if(button == GLUT_RIGHT_BUTTON)
    PlaySound("Shot.wav",NULL,SND_ASYNC);
}
void motion(int x, int y)
{
    angle[ID]+= x-oldX;
    oldX=x;
    glutPostRedisplay();
}


const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

/* Program entry point */

int main(int argc, char *argv[])
{
    myMP3.Load("123.mp3");
    myMP3.Play();
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
///    glutReshapeFunc(resize);
/// glutDisplayFunc(display);
///    glutKeyboardFunc(key);
///    glutIdleFunc(idle);

    glClearColor(1,1,1,1);
///    glEnable(GL_CULL_FACE); ///   glCullFace(GL_BACK);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

    glutMainLoop();

    return EXIT_SUCCESS;
}






#include <GL/glut.h>
#include <stdio.h>
FILE *fout=NULL;
FILE *fin=NULL;
///float angle=0,angle2=0,angle3=0,angle4=0, X=150, Y=150, oldX=0, oldY=0;
float angle[20], X=150, Y=150, oldX=0, oldY=0;
int ID=0;
#include "glm.h"
GLMmodel * pmodel=NULL;
void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    if(pmodel==NULL){
        pmodel=glmReadOBJ("123.obj");
        if (!pmodel) exit(0);
        glmUnitize(pmodel);
        glmFacetNormals(pmodel);
        glmVertexNormals(pmodel, 90.0);
    }
    glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL );

    glPushMatrix();
        glutSolidTeapot(0.2);
        glPushMatrix();
            glTranslatef(0.2, 0, 0);
            glRotatef(angle[0], 0.0f, 0.0f, 1.0f);
            glTranslatef(0.2, 0, 0);
            glutSolidTeapot(0.2);

        glPushMatrix();
            glTranslatef(0.2, 0, 0);
            glRotatef(angle[1], 0.0f, 0.0f, 1.0f);
            glTranslatef(0.2, 0, 0);
            glutSolidTeapot(0.2);
        glPopMatrix();
      glPopMatrix();


      glPushMatrix();
            glTranslatef(-0.2, 0, 0);
            glRotatef(angle[2], 0.0f, 0.0f, 1.0f);
            glTranslatef(-0.2, 0, 0);
            glutSolidTeapot(0.2);

        glPushMatrix();
            glTranslatef(-0.2, 0, 0);
            glRotatef(angle[3], 0.0f, 0.0, 1.0f);
            glTranslatef(-0.2, 0, 0);
            glutSolidTeapot(0.2);
        glPopMatrix();
      glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
    if(key=='1') ID=1;
    if(key=='2') ID=2;
    if(key=='3') ID=3;
    if(key=='4') ID=4;
    if(key=='5') ID=5;

    if(key=='s'|| key=='S'||key=='w'|| key=='W'){
        if(fout==NULL) fout=fopen("output.txt", "w+");
        fprintf(fout, "%.2f %.2f %.2f %.2f\n",angle[0], angle[1], angle[2], angle[3]);

    if(key=='r'|| key=='R'){
        if(fin==NULL) fin=fopen("output.txt", "r");
        fscanf(fin, "%f %f %f %f",&angle[0], &angle[1], &angle[2], &angle[3]);
        glutPostRedisplay();
    }
}
#include "CMP3_MCI.h"
#include <windows.h>
#include <mmsystem.h>
CMP3_MCI myMP3;
void mouse(int button, int state, int x, int y)
{
    oldX = x; oldY = y;
    if(button == GLUT_RIGHT_BUTTON)
    PlaySound("Shot.wav",NULL,SND_ASYNC);
}
void motion(int x, int y)
{
    angle[ID]+= x-oldX;
    oldX=x;
    glutPostRedisplay();
}


const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

/* Program entry point */

int main(int argc, char *argv[])
{
    myMP3.Load("123.mp3");
    myMP3.Play();
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
///    glutReshapeFunc(resize);
/// glutDisplayFunc(display);
///    glutKeyboardFunc(key);
///    glutIdleFunc(idle);

    glClearColor(1,1,1,1);
///    glEnable(GL_CULL_FACE); ///   glCullFace(GL_BACK);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

    glutMainLoop();

    return EXIT_SUCCESS;
}




沒有留言:

張貼留言