# Week15
step01-1 使用PlaySound(檔名,NULL,SND_SYNC);,講解參數和對應的lib檔
-先做一個開場白,先放一個不存在的檔,利用PlaySound()去播放。
-記得要先去CodeBlocks/Settings/Compiler 左邊add上winmm
-但因為檔案不存在,所以只會嘟一聲,表示錯誤訊息
```
#include <windows.h>//#include <mmsystem.h>也行
int main()
{
PlaySound("badbadbad.wav",NULL,SND_SYNC);
}
```
放個存在的音檔,去播放
```
#include <windows.h>
int main()
{
PlaySound("07042111.wav",NULL,SND_SYNC);
}
```
-先下載一個wav檔案
-然後,放進cpp這個檔的工作目錄裡
-然後,改程式碼(wav檔案的檔案名稱)
-就可以發出怪怪的聲音
step01-2 SND_SYNC和SND_ASYNC的差別
-SND_SYNC:要等待同步
-SND_ASYNC:不用等待,不同步
以下為ASYNC實作小程式:音檔會持續播放,直到你輸入數值,才會停住。
```
#include <windows.h>
#include <stdio.h>
int main()
{
printf("現在在PLaySound()前\n");
PlaySound("07042111.wav",NULL,SND_ASYNC);
printf("現在在PlaySound()後\n");
int N;
scanf("%d",&N);
}
```
step01-3 播放mp3檔
-MP3檔案小 vs WAV檔案大
-把CMP3_MCI.h檔 和 mp3檔放在cpp工作目錄檔裡
```
#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);
}
```
step02-1 接續上周的程式 week14_angles_TRT_write_and_read
-打開新的GLUT專案,取名為week15_angles_TRT_again
-從github複製上周的完整程式week14_angles_TRT_write_and_read
沒有動畫一次只能動一個關節,我們要修改這個程式,讓他更酷一點。
沒有動畫一次只能動一個關節,我們要修改這個程式,讓他更酷一點。
擺完動作再存檔,每做一個動作再存檔。
可以從存檔裡的第一個動作,移到動作二,以此類推。
-motion裡不需要一直存檔,myWrite()註解掉
-所以,我們加上鍵盤偵測,如果按下特定的鍵,就會做myWrite()的動作
-操作方法:
-先擺好動作,然後按s一次,做存檔
-擺好第一個動作,再做第二個動作>>>以此類推
-最後,每按一次r,模型會做一個動作。。以此類推下去
!!上周的程式碼有錯誤,所以執行怪怪的。
-正確的myWrite()
```
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]);//2f後面要有一個空格
}
printf("\n");
fprintf(fout,"\n");
}
```
step02-2 如果模型要做重複的動作
-去freeglut/bin裡的file.text複製貼上重複的動作,file.text就是執行的動作檔
step03-1 講解回家作業(連續上個程式,要讀入鋼彈去做動作)
-打開新的GLUT專案,取名為week15_homework_gundam_parts
-先做要讀入模型的前置動作:
-glm.cpp&glm.h和freeglut.dull和gundam的data資料夾放到專案的工作目錄
-Add Files/glm.cpp
-加入讀入模型的程式打進去
-我們要從display()裡面去改,新的display()裡加上模型的程式
-working_dir要注意(如果錯了,到工作目錄的cbp檔,working_dir改成".")>>Reload,但不要再重複儲存cpp檔
現在模型還沒有貼圖和打光之類的,只是最簡單的顏色。
```
#include <GL/glut.h>
#include "glm.h"
#include <stdio.h>//為了printf,fprintf,fopen,fclose
GLMmodel * pmodel =NULL;
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]);
}
glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='r')myRead();
if(key=='s')myWrite();
if(key=='0')angleID=0;
if(key=='1')angleID=1;
if(key=='2')angleID=2;
if(key=='3')angleID=3;
}
void mouse(int button,int state,int x,int y)//mosue按下去
{
oldX=x;
}
void motion(int x,int y)
{
angle[angleID]+=(x-oldX);
///myWrite();
oldX=x;
glutPostRedisplay();
}
void display()//今日精華
{
}
void displayNotParts()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(pmodel==NULL)
{
pmodel=glmReadOBJ("data/Gundam.obj");
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel,90);
}
glmDraw(pmodel,GLM_SMOOTH);
glutSwapBuffers();
}
void displayold()
{
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 );
glutCreateWindow("week15 angles TRT again");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc( display );
glutMainLoop();
}
```
沒有留言:
張貼留言