2024年5月30日 星期四

C~電腦圖學日誌 Week15

Week15


week15-1_PlaySound


1. 安裝freeglut,複製libfreeglut.a改成libglut32.a


2. 開新專案week15-1_PlaySound
    將音檔放在C槽
```cpp

#include <windows.h>    ///一定要在第1行
int main(int argc, char *argv[])
{
    PlaySound("C:/mykbeat.wav", NULL, SND_ASYNC);
}
```cpp


3. 比較SND_SYNC    VS    SND_ASYNC
///NULL空指標 (代表可以決定音樂要套在哪個物件上)
PlaySound("C:/Do.wav", NULL, SND_SYNC); ///沒有A同步等待(當下只有播音樂
PlaySound("C:/mykbeat.wav", NULL, SND_ASYNC);///有A不同步/不等待(播音樂加上執行後面的程式



week15-2_PlaySound_empty

1. 精簡版


2. 改成SND_ASYNC一執行就結束(因為後面沒程式了)
    ///可以用scanf/(輸入)來卡著
    


    ///把檔案都放在同目錄
    ///改設定和把音檔、freeglut.dll放入同資料夾

改week15-1_PlaySound

1. 新增案鍵控制Do Re Mi
    ///  改成預設目錄

2. 註解掉原本的
Do Re Mi



week15-3_CMP3_MCI

1. 開新專案week15-3_CMP3_MCI

2. CMP3_MCI.h、sone.mp3、freeglut.dll放同目錄    並改目錄


3. 使用
CMP3_MCI.h來讀音樂與播放

#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. 開新專案week15-4_mouse_motion_translate_rotate

2. 用GLUT    11行程式來改
#include <GL/glut.h>
float angleX[10]={}, angleY[10]={};
float teapotX=0, teapotY=0;///
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_mouse_motion_translate_rotate");
    glutDisplayFunc(display);

    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}



3.輸出移動後茶壺的XY座標 
#include <stdio.h>
printf("glTranslatef(%.3f, %.3f, 0):\n", teapotX, teapotY);


4. 新增茶壺轉動點 

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();
}

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();
}

Github





沒有留言:

張貼留言