2024年5月30日 星期四

電腦圖學week15

 ##week15-1_PlaySound

1.安裝freeglut

2.File-New-Project,GLUT專案,week15-1_PlaySound

```cpp

#include <windows.h>///第一行

```

int main(...)

{

    PlaySound("檔名.wav",,NULL,SND_ASYNC);

}


增加程式

```

    PlaySound("C://Do.wav",NULL,SND_SYNC);///沒有A同步等待

    PlaySound("C://Re.wav",NULL,SND_SYNC);

    PlaySound("C://Mi.wav",NULL,SND_SYNC);

    PlaySound("C://Fa.wav",NULL,SND_SYNC);

    PlaySound("C://Sol.wav",NULL,SND_SYNC);

    PlaySound("C://mykbeat.wav",NULL,SND_ASYNC);///有A 不同步/不等待

```

要將音檔拖到C槽以便讀取路徑


更新程式:讓背景音有節奏聲 

```cpp

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

{

    switch (key)

    {

        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;


///後面int main()函式裡的音樂要註解掉







 ##week15-2_PlaySound_empty

1..File-New-Project,GLUT專案,week15-2_PlaySound_empty

2.把程式碼全刪

3.把你的 工作執行目錄,設成小數點.



增加程式


```cpp

#include <windows.h>

int main()

{

    PlaySound("C://Do.wav",NULL,SND_SYNC);///要等

}




 ##week15_3_CMP3_MAI

1..File-New-Project,GLUT專案,week15_3_CMP3_MAI

2.請把CMP3_MCI.h和sone.mp3和freeglut.sull都放到專案目錄裡,很像之前glm.h

3.把你的 工作執行目錄,設成小數點.


更改程式:

```cpp

#include "CMP3_MCI.h" //雙引號

CMP3_MCI myMP3;//宣告執行物件

```

int main(int argc, char *argv[])

{

    myMP3.Load("song.mp3");

    myMP3.Play();

```





 ##week15_4_mouse_motion_translate_rotate

1..File-New-Project,GLUT專案,week15_4_mouse_motion_translate_rotate

2.貼上11行程式碼

3.目標:先能移動、旋轉、陣列


```cpp

void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

        glTranslatef(teapotX,teapotY,0);///移動

        glutSolidTeapot(0.3);

    glPopMatrix();

    glutSwapBuffers();

}

int oldX=0,oldY=0;///設定初始值

void mouse(int button,int state,int x,int y){

    oldX=x;

    oldY=y;

}

void motion(int x,int y){

    teapotX += (x-oldX)/150.0;

    teapotY -= (y-oldY)/150.0;

    oldX=x;///更新座標

    oldY=y;

    glutPostRedisplay();

}

int main(int argc, char *argv[])

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week15_4");

    glutDisplayFunc(display);

    glutMouseFunc(mouse);///記得加這個才會畫出來

    glutMotionFunc(motion);


    glutMainLoop();

}





紀錄移動位置:

```cpp

#include <GL/glut.h>

#include <stdio.h>

```

void motion(int x,int y){

    teapotX += (x-oldX)/150.0;

    teapotY -= (y-oldY)/150.0;

    printf("glTranslatef(%.3f,%.3f,0);\n",teapotX,teapotY);

```




繞著中心轉動:

#include <GL/glut.h>

#include <stdio.h>

float angleX[10]={},angleY[10]={};

float teapotX=0,teapotY=0;

void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glutSolidSphere(0.02,30,30);///秀出中心點


    glPushMatrix();

        ///glTranslatef(teapotX,teapotY,0);///註解掉不移動

        glRotatef(angleX[0],0,0,1);///旋轉

        glTranslatef(-0.520,-0.133,0);

        glutSolidTeapot(0.3);

    glPopMatrix();

    glutSwapBuffers();

}

int oldX=0,oldY=0;

void mouse(int button,int state,int x,int y){

    oldX=x;

    oldY=y;

}


void motion(int x,int y){

    ///teapotX += (x-oldX)/150.0;///註解掉

    ///teapotY -= (y-oldY)/150.0;

    ///printf("glTranslatef(%.3f,%.3f,0);\n",teapotX,teapotY);

    angleX[0]+=(x-oldX);

    oldX=x;

    oldY=y;

    glutPostRedisplay();

}


int main(int argc, char *argv[])

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week15_4");

    glutDisplayFunc(display);

    glutMouseFunc(mouse);

    glutMotionFunc(motion);


    glutMainLoop();

}



沒有留言:

張貼留言