2022年5月31日 星期二

Amber's note

主題:聲音

延續上週,繼續教聲音

<step1>設定settings/Linker settings/新增winmm(咒語)



<測試>寫出下面程式碼,測試系統可以正常執行

             上週教#include <mmsystem.h>

               這週教另一種用法#include <windows.h>


<step2>下載其他音檔測試,07042111.wav為音檔名



<注意>1.一定要是.wav檔,不可以是mp3檔

            2.音檔必須放在跟專案工作目錄一樣的地方

<比較ASYNC與SYNC>ASYNC會讓程式同時執行,SYNC會一行一行執行



<使用mp3檔播放>因為mp3(壓縮)的檔案比wav(沒壓縮)小所以我們試著讀mp3檔 ,

                                要include "CMP3_MCI.h"檔案、檔案可以在teams下載,放到同個專案目錄下

<TRT again>繼續教上週的動畫,修改上週 week14_TRT_angle_write_and_read程式碼,將myWrite();註解掉,將keyboard函式裡的 

Kuo's Graphic_Note_Week15

 Step 1 : Using CodeBlocks to make Joints of Model Rotated.

    1-1. Coding


    1-2. Build & Run

Final Code:
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
int angleID=0;
FILE*fout=NULL,*fin=NULL;
void myWrite(){
    if(fin!=NULL){
        fclose(fin);
        fin=NULL;
    }
    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);
    glColor3f(1,1,1);///white
    glRectf(0.3, 0.5, -0.3, -0.2);///body
    glPushMatrix();
        glTranslatef(0.3,0.5,0);///hanging on (0.5,0.5)
        glRotatef(angle[0],0,0,1);///Rotate
        glTranslatef(-0.3,-0.4,0);///Translate Rotating point to O(0,0)
        glColor3f(1,0,0);///red arms
        glRectf(0.3,0.5,0.8,0.3);///arms
        glPushMatrix();
            glTranslatef(0.8,0.4,0);
            glRotatef(angle[1],0,0,1);
            glTranslatef(-0.8,-0.4,0);
            glColor3f(0,1,0);///green
            glRectf(0.8,0.5,1.1,0.3);
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.3,0.5,0);///hanging on (0.5,0.5)
        glRotatef(angle[2],0,0,1);///Rotate
        glTranslatef(0.3,-0.4,0);///Translate Rotating point to O(0,0)
        glColor3f(1,0,0);///red arms
        glRectf(-0.3,0.5,-0.8,0.3);///arms
        glPushMatrix();
            glTranslatef(-0.8,0.4,0);
            glRotatef(angle[3],0,0,1);
            glTranslatef(0.8,-0.4,0);
            glColor3f(0,1,0);///green
            glRectf(-0.8,0.5,-1.1,0.3);
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
}
int main(int ac,char**av,char**ev){
    glutInit(&ac,av);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("Week14 TRT");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

2022年5月30日 星期一

week15

 1.讀入聲音

#include <windows.h>

int main()

{

    PlaySound("bad.wav", NULL, SND_SYNC);///先讀入一個空檔案

}

settings->compiler->Linker settings->add"winmm"



執行出錯誤的音效後,電腦就能讀聲音了
下載聲音,將音檔放入資料夾

#include <windows.h>
int main()
{
    PlaySound("07042111.wav", NULL, SND_SYNC);
}

2.輸入數字後聲音停止



#include <windows.h>
int main()
{
    printf("現在在PlaySound前\n");
    PlaySound("07042111.wav", NULL, SND_ASYNC);///SND_SYNC要等待同步
    printf("現在在PlaySound後\n");            ///SND_ASYNC不用等待、不同步
    int N;
    scanf("%d", &N);///等你輸入數字,輸入後聲音停止
}


3.

將CMP3_MCI.h、mp3拉進資料夾

#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);///等你輸入數字,輸入後聲音停止
}

4.讀入上禮拜程式week14_TRT_angle_write_and_read,並修改
   要能擺好動作再進行存檔
   將原本motion()中的myWrite()註解掉 (使其可以不會一直寫檔)
   在keyboard()中增加一個鍵存檔->按 s ,啟動 myWrite函式,擺好動作後存檔
   存取後,按 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();
}


5.全白的鋼彈模型
把glm.h、glm.cpp、gundam的data放到程式目錄
專案右鍵把glm.cpp加進來
用Notepad++把.cbp檔案中的 working_dir 改為 working_dir="."並存檔

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

6.分割顯示的機器人
把設定好的模型檔拉到data
設 myReadOne函式讀取模型檔
再設定一個新的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();
}

week15👻

  一.開啟openGL專案

1.程式碼:

#include<stdio.h>

int main(int argc,char**argv)

{

    FILE*fout=NULL;

    fout=fopen("file.txt","w+");///檔案的指標 fout output用

    printf("Hello World\n");///開檔,使用w+模式

    fprintf(fout,"Hello World\n");

}


2.修改執行目錄位置(修改week15_file裡的Option working_dir位置改成".")



2.老師給的程式碼


程式碼:

#include <stdio.h>
#include <GL/glut.h>
float angle=0,diff=2;
void timer(int t){
    glutTimerFunc(30,timer,t+1);
    angle+=diff;
    if(angle>90)diff=-2;
    if(angle<0)diff=+2;
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        ///glutSolidTeapot( 0.3 );
        glPushMatrix();
            glTranslatef(-0.3,0,0);///掛在正確的地方
            glRotatef(angle,0,0,1);///(2)旋轉
            glTranslatef(-0.3,0,0);///(1)把關節放到畫面中心
            glutSolidTeapot( 0.3 );///左手臂
            glPushMatrix();
                ///glutSolidTeapot( 0.3 );
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main( int argc, char ** argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week15 file");
    glutTimerFunc(0,timer,0);
    glutDisplayFunc(display);
    glutMainLoop();
}


程式碼:
#include <stdio.h>///可割可棄
#include <GL/glut.h>
float angle=0,diff=2;
void timer(int t){
    glutTimerFunc(30,timer,t+1);
    angle+=diff;
    if(angle>90)diff=-2;
    if(angle<0)diff=+2;
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glutSolidTeapot( 0.3 );///身體
        glPushMatrix();
            glTranslatef(-0.3,0,0);
            glRotatef(angle,0,0,1);
            glTranslatef(-0.3,0,0);
            glutSolidTeapot( 0.3 );///左手臂(重疊了)
            glPushMatrix();
                glTranslatef(-0.3,0,0);
                glRotatef(angle,0,0,1);
                glTranslatef(-0.3,0,0);
                glutSolidTeapot( 0.3 );///左手肘(重疊了)
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main( int argc, char ** argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week15 file");
    glutTimerFunc(0,timer,0);
    glutDisplayFunc(display);
    glutMainLoop();
}


3.整合多個關節
程式碼:
#include <stdio.h>///可割可棄
#include <GL/glut.h>
float angle[20]={},diff=2;
int angleID=0;
void timer(int t){
    glutTimerFunc(30,timer,t+1);
    angle[angleID]+=diff;
    if(angle[angleID]>90)diff=-2;
    if(angle[angleID]<0)diff=+2;
    glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y){
    if(key=='1')angleID=1;
    if(key=='0')angleID=0;
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glutSolidTeapot( 0.3 );///身體
        glPushMatrix();
            glTranslatef(-0.3,0,0);
            glRotatef(angle[0],0,0,1);
            glTranslatef(-0.3,0,0);
            glutSolidTeapot( 0.3 );///左手臂(重疊了)
            glPushMatrix();
                glTranslatef(-0.3,0,0);
                glRotatef(angle[1],0,0,1);
                glTranslatef(-0.3,0,0);
                glutSolidTeapot( 0.3 );///左手肘(重疊了)
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main( int argc, char ** argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week15 file");
    glutKeyboardFunc(keyboard);
    glutTimerFunc(0,timer,0);
    glutDisplayFunc(display);
    glutMainLoop();
}







superidol week15

 1.新增一個空白檔案,去setting裡compiler找到linker setting 並新增winmm

2.下載老師給的wav,把聲音加進去

3.開新專案,帶入上週程式碼並新增,能讓物體看起來像動畫,每當擺好一個姿勢後按s(儲存),然後按r,物體就會回復之前的動作。

4.做出一個全白鋼彈的程式碼
#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 == '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(); 
}
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();
}

5.切割出鋼彈部分的程式碼。
#include <GL/glut.h>
#include <stdio.h>
#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;
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 == '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();
}
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();
}
GLMmodel * myReadOne(char * filename){
    GLMmodel * one = NULL;
    if(one == NULL){
        one = glmReadOBJ(filename);
        glmUnitize(one);
        glmFacetNormals(one);
        glmVertexNormals(one, 90);
    }
    return one;
}
void 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);
            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();
}








Week15

 1.新程式碼

2.Setting>compiler>linker seeting>add>輸入winmm>OK


3.如果有叮表示開啟失敗,把藥的音樂wav放到同一個資料夾

4練習SND_ASYNC


5.mp3播放

6.街上周程式碼,註解調Myrite()

7.在keyboard改美按一次s就存檔一次

8.開一個新檔,把glm.cpp.m加入資料夾,在檔案內加入glm.cpp更改目錄,改work dir="."

改display()





week15 跳舞

 1-1教新的程式碼


要變譯成功但沒有link成功
案Setting ->compiler->linker seeting->add->輸入winmm->OK

按下編譯
會有一聲"叮"(檔案開啟失敗的聲音)
將你要的音樂.wav檔名稱放到同資料夾,記得改程式裡的檔名

2-1
SND_ASYNC練習



3-1
mp3撥放
下載"CMP3_MCI.h"'裝到相同資料夾

4-1
接續上禮拜程式
目標,擺好動作再存一行
先改一行程式碼
註解掉Mywrite()
在keyboard裡
改成每案一下"S"存檔一次


5-1
開新檔案把剛寫的複製一次貼上
將glm.cpp、glm.m加進資料夾
在檔案內加入glm.cpp
更改工作檔案目錄
改成work dir="."
成功將檔案工作目錄改到自己的資料夾內
創建模型
GLMmodel *pmodel=NULL;

改掉display()