2022年5月31日 星期二

豪哥的電腦圖學作業week15

 電腦圖學筆記week15

step01-1

PlaySound() 更詳細解說

0.New Empty File,存檔成week15-1_PlaySound.cpp

1.#include<mmsystem.h>上週教的

2.#include<windows.h>這週教的

3.PlaySound( " 檔名.wav" , NULL,SND_ASYNC );//上週

4.PlaySound( "檔名.wav" , NULL,SND_SYNC );//這週

5.Setting-Complier Setting,Linker 加入 winmm

6.下載個wav檔放進程式碼目錄

#include <windows.h>
#include <stdio.h>
int main()
{
    printf("PlaySound()之前\n");
    PlaySound("badbad.wav",NULL,SND_SYNC);
    printf("PlaySound()之後\n");
}
可播放音樂

stoe01-2
0.New Empty File,存成week15-2_SND_SYNC_SND_ASYNC

#include <windows.h>
#include <stdio.h>
int main()
{
    PlaySound("badbad.wav",NULL,SND_ASYNC);///ASYNC不等待
    while(1){
        printf("請輸入數字: ");
        int N;
        scanf("%d",&N);
        if(N==1) PlaySound("do.wav",NULL,SND_ASYNC);
        if(N==2) PlaySound("re.wav",NULL,SND_ASYNC);
        if(N==3) PlaySound("mi.wav",NULL,SND_ASYNC);
    }
}///最後一行,就沒了

ASYNC互動效果比較好

step02-1

WAV (大/原始)vs. MP3(小/有壓縮)

PlaySound只能用WAV檔

0. New Empty File ,week15-3_mp3.cpp

1.在moodle下載 CMP3_MCI.h

2.程式碼 #include "CMP3_MCI.h"

3.宣告 CMP3_MCI mp3;

4.mp3.Load("檔名.mp3");

5.mp3.Play();

#include <stdio.h>

#include "CMP3_MCI.h"

CMP3_MCI mp3;


int main()

{

    mp3.Load("badbad.mp3");

    mp3.Play();


    printf("隨便等你輸入數字,程式就卡住囉: ");

    int N;

    scanf("%d",&N);

}


step02-2
0.開新GLUT專案 week15_angles_TRT_again
1.複製上週程式,試跑2.不像動畫 動作不自然
3.myWrite()會寫一行 myRead()會讀一行
4.不能把myWrite()放motion()
5.關節動作調好再存檔
6.所以擺完動作後再按s存檔

///week15修改自week14_angles_fprintf
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
int angleID=0;
FILE * fout=NULL, * fin = NULL;
void myWrite()
{///每呼叫一次myWrite
    if(fout == NULL)fout=fopen("file.txt","w+");

    for(int i = 0;i<20;i++)
    {
        printf("%.1f ",angle[i]);///小黑印出來
        fprintf(fout,"%.1f ",angle[i]);///檔案印出來
    }///這裡老師沒有fclose
    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();///調好動作才save存檔
    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);
    ///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.5);

    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.7,0.3);
        glPushMatrix();
            glTranslatef(0.7,0.4,0);
            glRotatef(angle[1],0,0,1);
            glTranslatef(-0.7,-0.4,0);
            glColor3f(0,1,0);
            glRectf(0.7,0.5,1.0,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.7,0.3);

            glPushMatrix();
                glTranslatef(-0.7,0.4,0);
                glRotatef(angle[3],0,0,1);
                glTranslatef(0.7,-0.4,0);
                glColor3f(0,1,0);
                glRectf(-0.7,0.5,-1.0,0.3);
            glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
 }
 int main(int argc, char *argv[])
 {
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week15_angles_TRT_again");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);

    glutMainLoop();
 }


step03-1
機器人擺動
1.開新GLUT專案 week15_hw_gundam_parts
2.工作執行目錄 working.dir 不太好, 要改到現在程式的目錄
3.用Notepad++編輯 cbp檔的路徑,working dir 路徑改成小數點,把freeglut.dll放進目錄



沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

一.     一樣先安裝且設定好freeglut,OpecCV, 開啟CodeBlocks建立新專案 week11_gundam,                 把 MyGundam.zip下載解壓縮後的data資料夾放到freeglut/bin裡面 把week09_openc...