2022年5月30日 星期一

09163051 上課筆記 Week15

1.PlaySound 複習 

先到設定Settings - compiler - Linker settings - add - winmm 進行設定(聲音相關) 



開新的C++專案,
1.使用  PlaySound("X.wav",NULL,SND_SYNC);  

#include<windows.h>
int main()
{
    PlaySound("badbadbad.wav",NULL,SND_SYNC);
}
會出現錯誤聲音,因為badbadbad.wav不存在.

2.播放自己想放的聲音:
把自己要播放的聲音放到cbp所在的資料夾內,再把badbadbad.wav改成音樂檔案的名稱.

3.SND_SYNC,SND_ASYNC 介紹:
    SND_SYNC:要等待同步
    SND_ASYNC:不用等待,不同步

#include<windows.h>
#include <stdio.h>
int main()
{
    int n;
    printf("PLaySound()前\n");
    PlaySound("07042111.wav",NULL,SND_SYNC);
    printf("PLaySound()後\n");
    scanf("%d",&n);
}
這樣就得等到聲音播放完畢才能做動作.

4.播放Mp3檔案
**mp3檔案比WAV檔案還小**
先把CMP3_MCI.h 跟 mp3放在 cbp目錄資料夾.

#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);
}

2.接續上周的程式 week14_TRT_angle_write_and_read

1.開新GLUT project: week15_angles_TRT_again 並把上週程式碼丟進去.

2.目前程式只能一次動一個關節,修改程式碼後讓他能夠擺完動作後存檔,每做一個動作存一個檔.


#include <GL/glut.h>
#include <stdio.h>
#include <iostream>
using namespace std;
float angle[20],oldx=0;
int angleID=0;
FILE * fout=NULL;
FILE * 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("flie.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=='w')myWrite();
}
void mouse(int button,int state ,int x,int y){
    oldx=x;
}
void motion(int x,int y){
    angle[angleID]+=(x-oldx);
    //myWrite(); 當需要write的時候再按下w即可.
    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();///right
        glTranslatef(0.3, 0.5, 0);
        glRotatef( angle[0], 0,0,1 );//2.旋轉他
        glTranslatef(-0.3,-0.4, 0);//1.把旋轉中心放中心點
        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();///left
        glTranslatef(-0.3, 0.5, 0);
        glRotatef( angle[2], 0,0,1 );//2.旋轉他
        glTranslatef(0.3,-0.4, 0);//1.把旋轉中心放中心點
        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);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("Week15 angles TRT again");

    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);
    glutMouseFunc(mouse);/////mouse
    glutMotionFunc(motion);//配合motion
    glutMainLoop();
}

3.機器人跳舞 

開新GLUT project: week15_homework_gundan_parts 並把上面程式碼丟進去.

1.新增一個display() function, 舊的display()=>displayOld()
#include <GL/glut.h>
#include <stdio.h>

#include "glm.h"
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 == 's') myWrite();
    if(key == 'r') myRead();
    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)
{
    oldX= x;
}
void motion(int x, int y)
{
    angle[angleID] += (x-oldX);
    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);//       手肘掛在關節上
            glRotatef(angle[1], 0, 0, 1);//    旋轉
            glTranslatef(-0.8, -0.4, 0);//     把手肘旋轉中心放到中間
            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);//      手肘掛在關節上
            glRotatef(angle[3], 0, 0, 1);//   旋轉
            glTranslatef(0.8, -0.4, 0);//      把手肘旋轉中心放到中間
            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);
    glutMainLoop();
}

##這段程式會輸出無顏色的gundam.##

先更改cbp內部的      working_dir 有兩段.


把freeglut.dll丟入資料夾內,再把之前的鋼彈程式及物件丟入本cbp資料夾內.(glm.cpp,glm.h,鋼彈的data).
Add files 把glm.cpp引入專案內.









沒有留言:

張貼留言