2022年6月6日 星期一

09163076 week15

 1.settings>Linker settings>Add 

2.週使用#include <mmsystem.h>

這週教另一種用法#include <windows.h>,結果一樣

#include <windows.h>

int main()

{

    PlaySound("badbadbad.wav",NULL,SND_SYNC);

}

3.根據上一步驟底下錯誤訊息找到程式執行目錄,將音檔丟進去,更改程式碼後執行,即可聽到聲音

#include <windows.h>

int main()

{

    PlaySound("07042111.wav",NULL,SND_SYNC);

}

4.比較ASYNC與SYNC,ASYNC會讓程式同時執行,SYNC會一行一行執行

ASYNC輸入數字後聲音就停止

5.寫一個可以使用mp3檔案撥放音檔的程式

下載CMP3_MCI.h,將其與mp3音檔放在程式執行目錄

6.複製上週 week14_TRT_angle_write_and_read程式碼,將myWrite();註解掉,

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;

}

7.將機器人擺好動作後按's'鍵存檔,案'r'會讀一行,執行一個動作


8.找到此程式執行目錄中的file記事本,複製剛才s鍵存檔的內容貼上數次,再執行程式案r鍵機器人就能重複做動作

9.修改上個作業的程式碼

#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 == '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){

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

    }

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

            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.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);

            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("week14 TRT angle write");

     glutMouseFunc(mouse);

     glutMotionFunc(motion);

     glutKeyboardFunc(keyboard);

     glutDisplayFunc(display);

     glutMainLoop();

}

到teams下載gundam glm.cpp glm.h,將gundam資料夾中的data和glm.cpp  glm.h拉到week15_homework_gundam_parts資料夾,現在工作目錄在(in C:\Users\asus\Desktop\freeglut\bin),
把它放在程式碼的目錄
a.在.cbp的檔案裡找到working_dir="in C:\Users\asus\Desktop\freeglut\bin",改成working_dir=".",存檔
b.將freeglut裡的freeglut.dll複製到week14_TRT_angle_write的資料夾裡
執行程式










沒有留言:

張貼留言