- 播放聲音(新的)
2. Settings中的 Compiler...視窗 linker settings要增加咒語 winmm
3. 因為沒有把.wav檔拉近對應的資料夾,因此只有錯誤訊息的提示音
4. 所以要把音檔拉進資料夾才能成功發出音樂聲
///#incluude <mmsystem.h> ///上週教的
#include <windows.h> ///另一種寫法
int main()
{ ///不存在的檔案 ///上周教的是SND_ASYNC
///PlaySound("bad.wav",NULL, SND_SYNC);
PlaySound("07042111.wav",NULL, SND_SYNC);
}
- 播放聲音(比較)
2. 前一個 SND_SYNC 改為 SND_ASYNC
3. SND_ASYNC 的不同在於不用等待,播放後就不理了
4. 增加的 scanf 為等待你輸入數字(卡住不要結束),輸入後即結束程式(結束聲音)
#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);///等待輸入數字
}
- 播放聲音(mp3問題)
1. 新增一個空白檔案
2. 希望能播放 mp3 (檔案小、有壓縮) wav (檔案大、原始資料)
3. 將 CMP3_MCI.h 檔放入資料夾,這樣才能順利播放
4. 再將mp3檔也放入資料夾
5. scanf 同樣為卡住程式,使其不要結束
#include "CMP3_MCI.h"
#include <stdio.h>
CMP3_MCI mp3;
int main()
{
mp3.Load("JJK.mp3");
mp3.Play();
printf("現在正在播放音樂\n");
int N;
scanf("%d", &N);
}
- 播放動畫
1. 新增空白專案
2. 複製上週的 Week14_TRT_angle_write_and_read 程式碼
3. 要能擺好動作再進行存檔
4. 將原本motion()中的myWrite()註解掉 (使其可以不會一直寫檔)
5. 在keyboard()中,增加一個按鍵存檔(按下 s ,召喚 myWrite函式,寫擺好動作後檔)
6. 存取好後,按下 r 就可以讀取並回復原之前儲存的動作,關掉後重開再按 r 也可以做出動作
#include <GL/glut.h>
#include <stdio.h> ///為了 printf , fprintf, fopen, fclose
float angle[20], oldX=0; ///角度歸零
int angleID=0; ///0為第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");
///少了fclose,因為不想印一行就結束,要多行一點
}
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 == '0') angleID=0; ///預設
if(key == '1') angleID=1;
if(key == '2') angleID=2;
if(key == '3') angleID=3;
if(key == 'r') myRead();
if(key == 's') myWrite(); ///save
}
void mouse(int button, int state, int x, int y)
{
oldX= x;
}
void motion(int x, int y)
{
angle[angleID] += (x-oldX);
///myWrite(); ///沒必要一直寫檔
oldX= x;
glutPostRedisplay();
}
void display()
{
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.4, 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.4, 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_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week15_angles_TRT_again");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display); ///不放Idle
glutMainLoop();
}
- Gundam顯示出來
1. 新增空白專案
2. 將 glm.h glm.cpp 及 gundam 的data資料夾放進程式目錄
3. 專案 按右鍵Add File...把glm.cpp加進來
4. 用Notepad++把.cbp檔案中的 working_dir 改為 working_dir="."並存檔再Reload
5. 接著執行,就可以看到一個完整的全白gundam了
#include <GL/glut.h>
#include <stdio.h> ///為了 printf , fprintf, fopen, fclose
#include "glm.h"
GLMmodel * pmodel = NULL;
float angle[20], oldX=0; ///角度歸零
int angleID=0; ///0為第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");
///少了fclose,因為不想印一行就結束,要多行一點
}
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 == '0') angleID=0; ///預設
if(key == '1') angleID=1;
if(key == '2') angleID=2;
if(key == '3') angleID=3;
if(key == 'r') myRead();
if(key == 's') myWrite(); ///save
}
void mouse(int button, int state, int x, int y)
{
oldX= x;
}
void motion(int x, int y)
{
angle[angleID] += (x-oldX);
///myWrite(); ///沒必要一直寫檔
oldX= x;
glutPostRedisplay();
}
void display()
{
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.4, 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.4, 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_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week15_angles_TRT_again");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display); ///不放Idle
glutMainLoop();
}
- 機器人(分割顯示)
1. 把分割好的模型檔拉進data資料夾中
2. 設定一個 myReadOne函式來讀取模型檔
3. 再設定一個新的display()來畫出分割過的部位
#include <GL/glut.h>
#include <stdio.h> ///為了 printf , fprintf, fopen, fclose
#include "glm.h"
GLMmodel * pmodel = NULL;
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * bot = NULL;
GLMmodel * arm1 = NULL;
GLMmodel * arm2 = NULL;
GLMmodel * hand1 = NULL;
GLMmodel * hand2 = NULL;
float angle[20], oldX=0; ///角度歸零
int angleID=0; ///0為第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");
///少了fclose,因為不想印一行就結束,要多行一點
}
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 == '0') angleID=0; ///預設
if(key == '1') angleID=1;
if(key == '2') angleID=2;
if(key == '3') angleID=3;
if(key == 'r') myRead();
if(key == 's') myWrite(); ///save
}
void mouse(int button, int state, int x, int y)
{
oldX= x;
}
void motion(int x, int y)
{
angle[angleID] += (x-oldX);
///myWrite(); ///沒必要一直寫檔
oldX= x;
glutPostRedisplay();
}
GLMmodel * myReadOne(char * filename){
GLMmodel * one = NULL;
if(one == NULL){
one = glmReadOBJ(filename);
glmUnitize(one);
glmFacetNormals(one);
glmVertexNormals(one, 90);
}
return one;
}
void display()///最新的display
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(head==NULL) head= myReadOne("data/head.obj");
if(body==NULL) body= myReadOne("data/body.obj");
glmDraw(head, GLM_SMOOTH);
glutSwapBuffers();
}
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.4, 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.4, 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_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week15_angles_TRT_again");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutDisplayFunc(display); ///不放Idle
glutMainLoop();
}








沒有留言:
張貼留言