2017年4月25日 星期二

Week10_嗯對,這是賴小沫的

 1_Maya匯出OBJ模型以及obj&mtl程式碼講解

jsyeh.org/3dcg10 下載source.zip,windows.zip,data.zip,glut32.dll
 
windows.zip解壓縮,將data資料夾與glut32.dll拉進windows資料夾

 
開啟Transformation.exe

 
按右鍵可以更改模型
 
將模型切換成soccerball
 
開啟data資料夾,soccerball.obj&soccerball.mtl按右鍵以Notepad++開啟

soccerball.mtl:

色彩設定的數據

soccerball.obj:

 
v:glVertex3f(x,y,z);
 
vn:glNormal3f(nx,ny,nz);



glBegin(GL_POLYGON);   //f開頭的字,面,臉,face,facet
    glColor3f(r,g,b);
    glTexCoord2f(tx,ty);   //vertex texture coordinate ,vt開頭的貼圖座標tx,ty
    glNormal3f(nx,ny,nz);   //vertex normal ,vn開頭的nx,ny,nz
    glVertex3f(x,y,z);   //obj file ,v開頭的x,y,z
glEnd();
 

 2_glm讀模型&畫

利用老師傳送的myGLMsample.zip解壓縮
※記得將freeglut下載在桌面,才不會無法執行
 
開啟04160011_hw1.cbp
 
可以由" glmReadOBJ("模型路徑/名稱.obj"); "讀取不同的模型檔

按下F9 Build&執行
 
可以從myGLMsample資料夾中的data資料夾查看可以換成什麼模型
 

 3_NeHe射擊遊戲

Google搜尋NeHe,開啟官網
 
點選第一個網站,會進到NeHe的網頁,
點進右邊分類的Lessons 31-35

選擇Lesson 32

往下搜尋載點

下載第一個C++版本即可

將剛剛下載下來的檔案解壓縮

執行Lesson32.exe不要用全螢幕開啟

用滑鼠點擊目標物,
左下位置會顯示目前的Level 中下位置會顯示得分數 右下會顯示目前還能夠miss的數量
 

 4_WAV音效.MP3音樂

先將待會要播放的音檔複製到myGLMsample資料夾的data資料夾裡面
 
新增mouse函式來播放音樂,新增控制指令如下:
 
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
 
GLMmodel* pmodel = NULL;
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        angle++;
        glRotatef(angle, 0,1,0);
        if (!pmodel) {
        pmodel = glmReadOBJ("data/soccerball.obj");
        if (!pmodel) exit(0);
        glmUnitize(pmodel);
        glmFacetNormals(pmodel);
        glmVertexNormals(pmodel, 90.0);
        }
        glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix();
    glutSwapBuffers();
}

const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
 
#include <mmsystem.h>  //(1)Multimedia system
void mouse(int button,int state,int x,int y)
{   //(2)mouse function, use PlaySound
    PlaySound("C:\\Users\\user\\Desktop\\myGLMsample\\data\\Shot.wav",NULL,SND_ASYNC);
    //(3)PlaySound的路徑要記得用\\或/
}
 
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
    glutCreateWindow("Yes, 3D Model Here");

    glutMouseFunc(mouse);
 
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glClearColor(1,1,1,1);

    //glEnable(GL_CULL_FACE);
    //glCullFace(GL_BACK);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);
 
    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
 
    glutMainLoop();
}

按下F9 Build&執行,滑鼠點擊即可聽到聲音
 

 5_Keyboard

從FB社團中下載music.zip的壓縮檔

解壓縮後會看到Do — So的音檔

將上面提到的五個音檔複製到myGLMsample資料夾中

延續使用剛剛的程式碼,並且新增keyboard函式,利用數字鍵播放不同的音檔
 
#include <mmsystem.h>  //(1)Multimedia system
void keyboard(unsigned char key,int x,int y)
{   //(5)Add keyboard for Do/Re/Mi/Fa/So
    if(key=='1') PlaySound("Do.wav",NULL,SND_ASYNC);
    if(key=='2') PlaySound("Re.wav",NULL,SND_ASYNC);
    if(key=='3') PlaySound("Mi.wav",NULL,SND_ASYNC);
    if(key=='4') PlaySound("Fa.wav",NULL,SND_ASYNC);
    if(key=='5') PlaySound("So.wav",NULL,SND_ASYNC);
}

void mouse(int button,int state,int x,int y)
{   //(2)mouse function, use PlaySound
    PlaySound("C:\\Users\\user\\Desktop\\myGLMsample\\data\\Shot.wav",NULL,SND_ASYNC);
    //(3)PlaySound的路徑要記得用\\或/
}
 
int main(int argc, char**argv)
{
    ...

    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutDisplayFunc(display);
    glutIdleFunc(display);
 
    ...
}

按下F9 Build&執行,按下數字鍵1~5即可聽到聲音

沒有留言:

張貼留言