2024年5月30日 星期四

魚鱗☆゚.*・。電腦圖學 Week15

1. week15-1_PlaySound

(1)安裝freeglut

(2)打開新專案,命名為:week15-1_PlaySound

(3)第一行新增:#include <windows.h>,把 PlaySound("C:/mykbeat.wav",NULL,SND_ASYNC); 放在int main裡面

(4)把 mykbeat.wav 放在C槽裡,播放後就有聲音了

(5)把 Do.wav ~ Sol.wav 放入C槽中,加上

    PlaySound("C:/Do.wav",NULL,SND_SYNC);
    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);

##ASYNC : 不同步/不等待

##SYNC : 同步/等待

(6)播放後就會先從Do一個個播到Sol,最後再播mykbeat


2. week15-2_PlaySound_empty

(1)打開新專案,命名為:week15-2_PlaySound_empty

(2)把所有程式碼刪掉,加上程式碼:

#include <windows.h>
int main()
{
    PlaySound("C:/Do.wav",NULL,SND_SYNC);
}

(3)執行後就是簡單的播音樂的程式了

3. 回到 week15-1_PlaySound

##把 projects 的 properties 內的 Build Targets 內的 Execution working dir 改成 .

##然後把freeglut資料夾內的bin裡的 freeglut.dll 複製道專案資料夾中,這樣音檔路徑就不用打了

(1)在 static void 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;

這樣就能用鍵盤彈音符了


4. week15-3_CMP3_MCI

(1)打開新專案,命名為:week15-3_CMP3_MCI

(2)把 CMP3_MCI.h 、 sone.mp3 和 freeglut.dll 放進專案目錄中,把Execution working dir 改成 .

(3)在第一行加上程式碼:

#include "CMP3_MCI.h"
CMP3_MCI myMP3;

在int main中加入:

myMP3.Load("song.mp3");
myMP3.Play();

(4)播放後就有音效了


5. week15-4_mouse_motion_translate_rotate

(1)打開新專案,命名為:week15-4_mouse_motion_translate_rotate

(2)把所有程式碼刪掉,加上11行程式碼:

#include <GL/glut.h>
void display()
{
    
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
int main(int argc, char*argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week05");
    glutDisplayFunc(display);
    
    glutMainLoop();
}

(3)增加滑鼠移動的程式

#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_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week15");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}

(4)加上茶壺移動座標的程式

#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);
    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;
    printf("glTranslatef(%.3f, %.3f, 0);\n", teapotX, teapotY);
    oldX = x;
    oldY = y;
    glutPostRedisplay();
}
int main(int argc, char*argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week15");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}

(5)畫出中心點,能藉由中心點找出TRT的旋轉移動位置

#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);
        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);
    oldX = x;
    oldY = y;
    glutPostRedisplay();
}
int main(int argc, char*argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week15");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}

##記得複製座標 :glTranslatef(-0.500, -0.127, 0);

(6)加上TRT程式,並註解掉一些不需要的程式

#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.500, -0.127, 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_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week15");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}

6. 上傳Github



沒有留言:

張貼留言