2022年5月30日 星期一

xyt week15

 week15

1. 講解播放聲音的參數和對應的lib

  • 開啟CodeBlocks 檔名:week15-1_PlaySound.cpp
  • 到Setting-Compile-Linker Setting-Add "winmm"
  • 小黑視窗會播出錯誤音效

```c
///#include <mmsystem.h>//上週教
#include <windows.h>///另一種寫法,結果一樣
int main()
{           ///先放不存在的檔     上週教SND_ASYNC
    PlaySound("badbadbad.wav", NULL, SND_SYNC);
}///Compile成功,但是link失敗
/// .c .cpp =compile=> .o =link=> .exe
```

2. 播放想要的音效

  • 將wav檔存在CodeBlocks下方錯誤訊息中對應位置
  • 按下執行即可播放音樂

3. 比較 SND_SYNC 和 SND_ASYNC

  • SND_SYNC 要等待同步

  • SND_ASYNC 不等待,不同步

  • 開啟CodeBlocks 檔名:week15-2_SND_SYNC_SND_ASYNC

```c
#include <windows.h>
#include <stdio.h>///printf()
int main()
{
    printf("現在在PlaySound()前\n");
    PlaySound("07042111.wav", NULL, SND_ASYNC);
    printf("現在在PlaySound()後\n");
    int N;
    scanf("%d", &N);///等你輸入數字
}
```

  • 執行則播放音檔,若輸入數字則停止

4. 播放mp3

  • mp3檔案小,wav檔案大,但 mp3 有壓縮比較難
  • 開啟CodeBlocks 檔名:week15-3_mp3.cpp
  • 先下載CMP3_MCI.h
  • 執行後即可使用 mp3 的方式播放

```c
#include  "CMP3_MCI.h"
#include <stdio.h>
CMP3_MCI mp3;
int main()
{
    mp3.Load("07042111.mp3");
    mp3.Play();
    printf("現在正在播放羊的聲音\n");
    int N;
    scanf("%d", &N);

}
```


5. 接續上週 week14_angles_TRT_write_and_read

  • File-New-Project, GLUT檔
  • 複製上週程式碼
  • 原本 motion() 裡一直呼叫 myWrite() ,把它刪掉
  • keyboard函數 按下r 會讀進一行 按下 s 來儲存位置
  • 到freeglut - bin - file.txt 調整希望出現的動作






```c

#include <GL/glut.h>

#include <stdio.h> ///為了 printf, fprintf, fopen, fclose
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(       "%.2f", angle[i]);
        fprintf(fout, "%.2f", angle[i]);
    }
    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]);
    }
}
void keyboard( unsigned char key, int x, int y){
    if( key=='r' ) myRead();
    if( key=='s' ) myWrite();///save
    if( key=='0' ) angleID=0;
    if( key=='1' ) angleID=1;
    if( key=='2' ) angleID=2;
    if( key=='3' ) angleID=3;
}///用keyboard的按鍵來決定
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();
}
void display()
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glColor3f(1, 1, 1);///白色
    glRectf(0.3, 0.5, -0.3, -0.2);///身體,瘦身
    glPushMatrix();///掛的位置改變了
        glTranslatef(0.3, 0.5, 0);
        glRotatef(angle[0], 0, 0, 1);
        glTranslatef(-0.3, -0.4, 0);
        glColor3f(1, 0, 0);
        glRectf(0.3, 0.5, 0.8, 0.3);///上手臂
        glPushMatrix();
            glTranslatef(0.8, 0.4, 0);///(3) 把下手肘掛在關節上
            glRotatef(angle[1], 0, 0 ,1);///(2) 旋轉
            glTranslatef(-0.8, -0.4, 0);///(1) 把下手肘的旋轉中心放正中心
            glColor3f(0, 1, 0);///綠色的
            glRectf(0.8, 0.5, 1.1, 0.3);/// 再畫下手肘
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();///左半邊
        glTranslatef(-0.3, 0.5, 0);
        glRotatef(angle[2], 0, 0, 1);
        glTranslatef(+0.3, -0.4, 0);
        glColor3f(1, 0, 0);
        glRectf(-0.3, 0.5, -0.8, 0.3);///上手臂
        glPushMatrix();
            glTranslatef(-0.8, 0.4, 0);///(3) 把下手肘掛在關節上
            glRotatef(angle[3], 0, 0 ,1);///(2) 旋轉
            glTranslatef(+0.8, -0.4, 0);///(1) 把下手肘的旋轉中心放正中心
            glColor3f(0, 1, 0);///綠色的
            glRectf(-0.8, 0.5, -1.1, 0.3);/// 再畫下手肘
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutInitWindowSize(600, 600);
    glutCreateWindow("week15 TRT angles again");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc( display );
    glutMainLoop();
    return 0;
}
```
6. 機器人跳舞 期末作業講解

  • File-New-Project, GLUT檔 檔名:week15_homework_gundan_parts
  • 將glm.cpp、glm.h、freeglut.dll、gundam的data資料夾丟進week15_homework_gundan_parts
  • 在程式碼新增一個新的display函式,將舊的display改成 displayOld()
  • 複製 displayOld 的 glClear 和 glutSqapBuffers() 複製到新的


  • 右鍵week15_homework_gundam_parts , Add File 將glm.cpp放進去

  • 把week15_homework_TRT_again.cbp用nodepad++開啟

  • 將11、21行的working_dir引號裡改成"."
  • 即可跑出鋼彈模型



沒有留言:

張貼留言