2024年5月30日 星期四

從0到0.01的電腦圖學 Week15

本週主題

(1) 主題:音樂、音效

(2) mouse 移動模型

(3) 期末作品

1. GLUT程式1:PlaySound()

在程式最上面先放上#include <windows.h>

接著在main中寫 PlaySound("音樂檔案路徑/音樂檔案", NULL,SND_ASYNC)

第三參數有兩個下法:
SND_ASYNC - 不同步(不等待)
SND_SYNC  -  同步

2. GLUT程式2:.mp3的使用

在程式最上面放上
#include "CMP3_MCI.h"
CMP3_MCI myMP3;

接著在main中寫
myMP3.Load("音樂檔案路徑/音樂檔案");
myMP3.Play();

3. GLUT程式3-1:利用滑鼠移動物件

#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.5);
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-4");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
4. GLUT程式3-2:利用程式找出座標點
#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.5);
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-4");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}

5. GLUT程式3-3:利用程式找出座標點,然後沿茶壺嘴轉動
#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); ///show the center
        ///move the teapot to center find the coordinate glTranslatef(-0.860, -0.220, 0);
glPushMatrix();
        glRotatef(angleX[0],0,0,1);
        ///glTranslatef(teapotX,teapotY,0);
        glTranslatef(-0.860, -0.220, 0);
        glutSolidTeapot(0.5);
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-4");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}


沒有留言:

張貼留言