顯示具有 09160765_林渼莛 標籤的文章。 顯示所有文章
顯示具有 09160765_林渼莛 標籤的文章。 顯示所有文章

2022年6月14日 星期二

電腦圖學筆記week17

 筆記

Step

程式環境 : week01 (freeglut,GLUT專案)

                   week10 (OpenCV的設定)

                   week16 (改CBP的working_dir)

點線面顏色 : 10行程式碼  display()

                        main()前3行設定,最後一行glutMainLoop()

#include <GL/glut.h>
 void display()
 {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,0);
    glutSolidTeapot(0.3);

    glutSwapBuffers();
 }
 int main(int argc, char *argv[])//main()主函式 進階版
 {
    glutInit(&argc,argv);//把參數送給glutInit初始化
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);//雙緩衝區+3D深度功能
    glutCreateWindow("第02週的程式喔!!");//開GLUT視窗

    glutDisplayFunc(display);//顯示用的函式

    glutMainLoop();
 }

打光 : 8行 + 10多行

const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

讀入3D模型 : 模型在工作目錄,glm.h、glm.cpp加進去 #include "glm.h" 再 GLMmodel * body;     再...

貼圖 : OpenCV 及 myTexture範例,還有圖,而且畫glmDraw(pmodel , GLM_TEXTURE  GLM_SMOOTH);

int myTexture(char * filename)
{
    IplImage * img = cvLoadImage(filename); ///OpenCV讀圖
    cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
    glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
    GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
    glGenTextures(1, &id); /// 產生Generate 貼圖ID
    glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
    return id;
}

攝影機與運鏡 : 透視投影法 glyPerspective,配上glutReshapeFunc()再上gluLookAt()

void reshape(int w,int h){///不能 整數除
    float ar = (float) w/(float) h;
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);///3D變2D
    glLoadIdentity();
    gluPerspective(60,ar,0.1,100);

    glMatrixMode(GL_MODELVIEW);///3D Model+view
    glLoadIdentity();
    gluLookAt(0,0,3, ///eye
              0,0,0, ///center看哪裡
              0,1,0);///up向量
}

兩張貼圖 : Gundam(鋼彈)、木紋、宣告2個整數 GLuint tex1、text2; 在main()裡面
                   text1 = myTexture("data/Diffuse.jpg");
                   text2 = myTexture("data/wood.png");
在 display() 裡
glBindTexture(GL_TEXTUTE_2D, text1) 及 glBindTexture(GL_TEXTURE_2D, text2) 來切換

把全部的模型讀入 : 改寫 myReadGundam() 改寫 myReadOne() 及 myReadAll();

TRT : 必須要有中心點,才能把每個部位放好,才能正確轉,才能掛在對的地方
           可以使用 mouse motion 來知道值在哪裡
           要經常註解程式,才能找到 T-R-T的值

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

2022年6月7日 星期二

電腦圖學筆記week16

 筆記

Step1-1 alpha內插公式 : alpha: `0.0 ~ 1.0`

                                         `angle = alpha*新 + (1-alpha)*舊`

    ex: alpha: 0 =>舊

          alpha: 0.5 =>半新半舊

          alpha: 1 =>新

    使用Excel or Google Spreadsheet來做練習

Step1-2 用week15_angles_TRT_again程式拿來改

    1. 建新專案 File-New-Project  -> GLUT  ->week16_interpolation 內插

    2. 複製程式 -> 執行 -> 按s會存1行 -> mouse motion改動作(4次) -> 按s存檔

        (原本失敗的結果 : 按r會讀到動作,但不連續動作)

接著,需來改程式

1. void myInterplate(float alpha)


#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");///檔案跳行

}

float NewAngle[20],OldAngle[20];

void myRead()

{

    if(fout!=NULL){fclose(fout); fout=NULL;}

    if(fin==NULL) fin=fopen("file.txt","r");

    for(int i=0;i<20;i++)

    {

        OldAngle[i] = NewAngle[i];

        fscanf(fin, "%f", &NewAngle[i] );

        ///fscanf(fin,"%f",&angle[i]);

    }

    glutPostRedisplay();///重劃畫面

}

void myInterpolate(float alpha){

    for(int i=0;i<20;i++){

        angle[i]= alpha * NewAngle[i] + (1-alpha) * OldAngle[i];

    }

}

int  t =0;

void keyboard(unsigned char key,int x,int y)

{

    if(key=='p'){

        if(t%30==0) myRead();

        myInterpolate( (t%30)/30.0 );///介於0.0~1.0

        glutPostRedisplay();

        t ++;

    }

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

 }


Step2-1

#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");///檔案跳行
}
float NewAngle[20],OldAngle[20];
void myRead()
{
    if(fout!=NULL){fclose(fout); fout=NULL;}
    if(fin==NULL) fin=fopen("file.txt","r");
    for(int i=0;i<20;i++)
    {
        OldAngle[i] = NewAngle[i];
        fscanf(fin, "%f", &NewAngle[i] );
        ///fscanf(fin,"%f",&angle[i]);
    }
    glutPostRedisplay();///重劃畫面
}
void myInterpolate(float alpha){
    for(int i=0;i<20;i++){
        angle[i]= alpha * NewAngle[i] + (1-alpha) * OldAngle[i];
    }
}
///int t=0;
void timer(int t){
    if(t%50==0) myRead();
    myInterpolate((t%50)/50.0);
    glutPostRedisplay();
    glutTimerFunc(20,timer,t+1);
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='p'){
        myRead();
        glutTimerFunc(0,timer,0);
        ///if(t%30==0) myRead();
        ///myInterpolate( (t%30)/30.0 );///介於0.0~1.0
        ///glutPostRedisplay();
        ///t++;
    }
    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();
 }

接著,讓動畫可以不用按p就會自動播放,並讓它變得更流暢 (只需要加入幾行程式碼就好)

Step2-2 利用課本範例,講解 gluLookAt (eye、center、up)

    eye : 攝影機的位置 / center : 攝影機中心點的位置 / up : 攝影機底部的位置

Step3-1 

    1. 建新專案 File-New-Project -> GLUT -> week16_camera_projection_gluLookAt

    2. 備份177行範例,要改造裡面的程式

    3. aspect ratio 長寬比

       ex : 1920x1080 、 1280x720 、 640x480 、16:9、 4:3

#include <GL/glut.h>

void reshape(int w,int h){

    float ar = (float) w/(float) h;

    glViewport(0,0,w,h);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    gluPerspective(60,ar,0.1,100);


    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

    gluLookAt(0,0,3, ///eye

              0,0,0, ///center看哪裡

              0,1,0);///up向量

}

void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glutSolidTeapot(1);

    glutSwapBuffers();

}

int main(int argc,char**argv)

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week16 camera");

    glutDisplayFunc(display);

    glutReshapeFunc(reshape);

    glutMainLoop();

}


Step3-2 滑鼠可拖曳 新加的程式碼

#include <GL/glut.h>
void reshape(int w,int h){
    float ar = (float) w/(float) h;
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60,ar,0.1,100);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0,0,3, ///eye
              0,0,0, ///center看哪裡
              0,1,0);///up向量
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(1);
    glutSwapBuffers();
}
void motion(int x,int y){
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt((x-150)/150.0,(y-150)/150.0,3, ///eye
              0,0,0, ///center看哪裡
              0,1,0);///up向量
    glutPostRedisplay();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week16 camera");
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
}

2022年5月31日 星期二

電腦圖學筆記week15

筆記 

Step1-1 PlaySound() 更詳細解說

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

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

    2. #inlcude <windows.h> 本週改這

    3. //PlaySound("檔名wav", NULL , SND_ASYNC); //上週 不等待/不同步

    4. PlaySound ("檔名.wav", NULL , SND_SNYC); //本週、等待、同步

    5. 注意工作執行目錄 working_dir (in C:\ . . . . .)

#include <windows.h>

#include <stdio.h>

int main()

{

    printf("PlaySound()之前\n");

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

    printf("PlaySound()之後\n");

}

但執行錯誤,因還沒有在settings->compiler加入winmm

https://sound-effects.bbcrewind.co.uk/search 尋找想要的wav檔

下載後移到執行資料夾

Step1-2 PlaySound()

    1.File-New-empty File再改檔名.cpp

    2. 寫入

        #include <windows.h>

        #include <stdio.h>

        int main(){

            PlaySound("do.wav",NULL,SND_SYNC);///ASYNC不等待

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

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

        }

    3. wav檔移到執行資料夾    

    4. 記得寫成SYNC(寫ASYNC若不等待會出錯

        #include <windows.h>

        #include <stdio.h>

        int main(){

            PlaySound("NHU05082251.wav",NULL,SND_ASYNC);///不等待執行較快,適合互動

            while(1){

                printf("輸入數字: ");

                int N;

                scanf("%d",&N);

                if(N==1)PlaySound("do.wav",NULL,SND_SYNC);///ASYNC不等待

                if(N==2)PlaySound("re.wav",NULL,SND_SYNC);

                if(N==3)PlaySound("mi.wav",NULL,SND_SYNC);

                }

           }


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

              PlaySound()只能用大/原始的WAV檔,只要1行


             MP3檔比較麻煩,很多行。

             Moodle可下載 CMP3_MCI.h

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

    1. 在Moodle 下載 CMP3_MCT.h放在同目錄

    2. 程式碼 #include "CMP3_MCT.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("07071034.wav");

    mp3.Play();

    printf("輸入數字程式卡住");

    int N;

    scanf("%d",&N);

}


Step2-2 接續上周的進度加強印象,將動畫相關的動作編輯進行改良。

              接續上周的week14_angles_TRT_write_and_read

    0. File-New-Project ,GLUT專案, week15_angles_TRT_again

    1. copy 上周的程式碼,改成 week15_angles並試跑: mouse motion, keyboard '0' '1' '2' '3' 換關           節,最後案 'r' 長按,可慢慢讀入資料

    2. 發現問題: 不像動畫,只是拉某一個關節(很慢)

    3. mrWrite() 會寫一行, myRead()會讀一行

    4. 不能把myWrite() 放 motion

    5. 把動作對應的關節全部調好後,才存動作

    6. 擺好動作時,按 's' 存動作

    7. 執行時,存幾個動作, 'r' 可讀入

    8. 事後在工作執行目錄 file.txt 可以大量copy 動作,重複,再重新跑程式

Step3-1 機器人擺動作

    1. 需要 3D Model(glm.h / glm.cpp) (.obj / .mtl / .jpg ...)

    2. 把模型切成很多塊(因整塊就無法擺動關節) 分別讀入

    3. TRT的程式,才能轉動

    4. keyboard() 切換關節, mouse motion() 轉動那一個關節

        (1) File-New-Project ,GLUT 專案,week15_hw_gundam_parts

        (2) 工作執行目錄 working_dir不太好,要改

        (3) 用 Notepad++把week15_hw_gundam_parts.cbp(CodeBlocks Project)改裡面的working_dir

        (4) 把glm.h、glm.cpp、gundam的data目錄,全部放在程式目錄

        (5) 在CodeBlocks左邊

2022年5月24日 星期二

電腦圖學筆記week14

筆記

電腦圖學 Week14 

    1.寫檔、讀檔

    2.關節、做動畫

    3.計時器 glutTimerFunc (時間,timer,參數t)

    4.播放聲音


Step1-1 為了記錄動畫的動作,我們需要寫檔。fopen()開檔,fprintf0()寫檔,fclose()關檔

    寫檔 File Output (記錄)

    0. File-New-Empty File,存檔成 week14-1_fprintf 目錄裡的 week14-1.cpp

 #include <stdio.h>

int main()

{///檔案指標  fout  開啟檔案(檔名 , 寫)

    FILE * fout = fopen("file.txt", "w+");

    printf(     "Hello world\n");

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

    fclose(fout);///關閉檔案

}


    1. fopen() 開啟檔案

    2. printf() => fprintf() File Output

    3. fclose() 關閉檔案

Step1-2 讀檔 File Input

    0. File-New-EMpty File,存檔到 week14-2_fprintf_fscanf 目錄裡的 week14-2.cpp

    1. 打剛剛 week14-1.cpp拿來用

    2 . 另外一組 `FILE * fin = fopen("檔名", "r");

    3. scanf() => fscanf() File Input

    4. fclose()

#include <stdio.h>

int main()

{///檔案指標  fout  開啟檔案(檔名 , 寫)

    FILE * fout = fopen("file.txt", "w+");

    fprintf(fout,"3.1415926\n");///預設數字

    fclose(fout);///關閉檔案


    float angle=0;

    FILE * fin = fopen("file.txt", "r");

    fscanf(fin , "%f", &angle);///&符號要記得

    printf("讀到了角度:%f",angle);

    fclose(fin);

}


Step1-3 把上週的程式 week13_rect_many_TRT 拿來改

    1. 先設立資料夾在桌面 命名為week14-3_ angle_fprintf

    2. 開新專案 命名為week14-3.cpp

    3. 貼上上週many_TRT程式碼,並新增紅色程式碼

#include<GL/glut.h>

#include<stdio.h>


float angle[20] ,oldX=0;

int angleID=0;


FILE * fout=NULL;

void 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


}

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;


}

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();///請GLUT重畫畫面

}


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);///(3)整體移位

        glRotatef(angle[0],0,0,1);///(2)

        glTranslatef(-0.3,-0.4,0);///(1)把軸心放在正中心

        glColor3f(1,0,0);///紅

        glRectf(0.3,0.5,0.7,0.3);///上手臂


            glPushMatrix();

                glTranslatef(0.7,0.4,0);///(3)整體移位

                glRotatef(angle[1],0,0,1);///(2)

                glTranslatef(-0.7,-0.4,0);///(1)把軸心放在正中心


                glColor3f(0,1,0);///綠

                glRectf(0.7,0.5,1.0,0.3);///下手臂

            glPopMatrix();

    glPopMatrix();


     glPushMatrix();///左邊

        glTranslatef(-0.3,0.4,0);///(3)整體移位

        glRotatef(angle[2],0,0,1);///(2)

        glTranslatef(0.3,-0.4,0);///(1)把軸心放在正中心

        glColor3f(1,0,0);///紅

        glRectf(-0.3,0.5,-0.7,0.3);///上手臂


            glPushMatrix();

                glTranslatef(-0.7,0.4,0);///(3)整體移位

                glRotatef(angle[3],0,0,1);///(2)

                glTranslatef(0.7,-0.4,0);///(1)把軸心放在正中心


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

    glutInitWindowSize(600,600);

    glutCreateWindow("week13 rect TRT");


    glutMouseFunc(mouse);

    glutMotionFunc(motion);

    glutKeyboardFunc(keyboard);

    glutDisplayFunc(display);


    glutMainLoop();

}


Step2-1 開始做動畫

    0. File-New-Project,GLUT專案 week14_angles_fprintf_fscanf

    1. copy前一個版本的程式來修改

    2. 要寫void myRead()

    3. keyboard()裡,按下 ' r ' 呼叫 myRoad()

#include <stdio.h>
FILE * fin =NULL;
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=='0') angleID=0;
    if(key=='1') angleID=1;
}

Step2-2 為啥產生的檔案file.txt放在奇怪的目錄 C:\Users\user\Desktop\freeglut\gin ,將它放在程式專案的那個目錄!

0. What! 好奇怪!!! file.txt放在奇怪的目錄

1. Why? 原來是歷史餘毒 GLUT專案 需要 freeglut.dll 所以 working_dir被設到 freeglut\bin 裡面

2. How? 在 .cbp CodeBlocks Project 檔裡, 有 working_dir的設定 工作執行的目錄

3. 使用 Notepad++ 把 .cbp 的 working_dir="....." 改 working_dir="." 小數點

4. Notepad++存檔後, CodeBlocks Reload它, 便成功了!!!!

5. 小心 歷史餘毒 freeglut.dll 要再修正, 放到week14_angles_fprintf_fscanf 程式專案的同目錄, 再執行時, 便可以 mouse motion 動動動, 按r重播,而且file.txt 也放在你的程式專案目錄囉!!!


沒裝 Notepad++ 的, 快去下載台灣人 Don Ho 侯今吾先生寫的 Notepad++

Step3-1 Timer

    0. 看電腦按R的速度

    1. 寫程式

#include <GL/glut.h>
#include <stdio.h>
void timer(int t){
    printf("起床, 現在時間: %d\n", t);
}
void display(){

}
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week14 timer");

    glutTimerFunc(1000, timer, 1);
    glutTimerFunc(2000, timer, 2);
    glutTimerFunc(3000, timer, 3);
    glutTimerFunc(4000, timer, 4);
    glutTimerFunc(5000, timer, 5);
    glutDisplayFunc(display);
    glutMainLoop();
}
    2. 動畫30秒,程式呼叫程式
#include <GL/glut.h>
#include <stdio.h>
void timer(int t){
    printf("起床, 現在時間: %d\n", t);
    glutTimerFunc(1000, timer, t+1);
}
void display(){

}
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week14 timer");

    glutTimerFunc(5000, timer, 0);
    glutDisplayFunc(display);
    glutMainLoop();
}

播放聲音 PlaySound()

    0. 準備好聲音檔

    1. 繼續改 week14_timer

    2. #incloude <Mmsystem.h>

    3. playsound("檔名")

2022年5月17日 星期二

電腦圖學筆記week13

筆記

Step1-1 glRectf (x1 , y1 , x2 , y2) 方塊
    1. File-New-Project , GLUT專案 , week13_rect_TRT
    2. 貼上GLUT 10行程式,不用茶壺
    3.把手臂接到身體

#include<GL/glut.h>

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(x,y,z);(3)把手臂掛回身體

        ///glRotatef(angle,0,0,1);(2)旋轉 對Z軸轉

        ///glTranslatef(x2,y2,z2);(1)把手臂的旋轉中心,放中心

        glColor3f(1,0,0);//紅色的

        glRectf(0.3,0.5,0.7,0.3);

    glPopMatrix();

    glutSwapBuffers();

}

int main(int argc, char**argv)

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    //glutInitWindowSize(600,600);

    glutCreateWindow("week13 rect TRT");



    glutDisplayFunc(display);



    glutMainLoop();

}

Step1-2 再把紅色的手臂加上去

#include <GL/glut.h>
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(); 把手臂放回身體
        ///glRotatef();    旋轉
        ///glTranslatef(); 手臂的旋轉中心
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect TRT");
    glutDisplayFunc(display);
    glutMainLoop();
}

Step1-3 先把旋轉中心放在正中心
             ---> 把 (0.3 0.4) 移到 (0 0)
                    glTranslatef (-0.3, -0.4, 0);

#include <GL/glut.h>
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(); ///把手臂放回身體
        ///glRotatef();    ///旋轉
        glTranslatef(-0.3,-0.4,0); ///手臂的旋轉中心

        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect TRT");
    glutDisplayFunc(display);
    glutMainLoop();
}

Step1-4 把手臂旋轉45度

#include <GL/glut.h>
float angle=45;
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(); ///把手臂放回身體
        glRotatef(angle,0,0,1);    ///旋轉
        glTranslatef(-0.3,-0.4,0); ///手臂的旋轉中心

        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect TRT");
    glutDisplayFunc(display);
    glutMainLoop();
}

Step1-5 把手臂放回身體正確位置

#include <GL/glut.h>
float angle=45;
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,1);    ///旋轉
        glTranslatef(-0.3,-0.4,0); ///手臂的旋轉中心

        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect TRT");
    glutDisplayFunc(display);
    glutMainLoop();
}

Step2-1 讓手臂能隨著滑鼠移動去旋轉

#include <GL/glut.h>
float angle=45,oldX=0;
void mouse(int Button, int state, int x, int y){
    oldX= x;
}
void motion(int x,int y){
    angle += (x-oldX);
    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,1);    ///旋轉
        glTranslatef(-0.3,-0.4,0); ///手臂的旋轉中心

        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect TRT");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

Step2-2 要開新的專案 week13_rect_TRT_TRT 做出更多的關節
    1. File-New-Project , GLUT專案 ,  week_rect_TRT_TRT
    2.把前面的程式拿來用
    3.要新增的地方=>第二個關節

#include <GL/glut.h>
float angle=0,oldX=0;
void mouse(int Button, int state, int x, int y){
    oldX= x;
}
void motion(int x,int y){
    angle += (x-oldX);
    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,1);    ///旋轉
        glTranslatef(-0.3,-0.4,0); ///手臂的旋轉中心
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);
        glPushMatrix();
       ////glTranslatef(x,y,z); ///把手臂放回身體
        ///glRotatef(angle,0,0,1);    ///旋轉
        ///glTranslatef(x2,y2,z2); ///手臂的旋轉中心
            glColor3f(0,1,0);
            glRectf(0.7,0.5,1,0.3);
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect TRT");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

Step2-3 調整手臂的中心位置並旋轉
             (把上手臂的旋轉註解掉,因會很難看出下手臂在旋轉的樣子)

#include <GL/glut.h>
float angle=0,oldX=0;
void mouse(int Button, int state, int x, int y){
    oldX= x;
}
void motion(int x,int y){
    angle += (x-oldX);
    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,1);    ///旋轉
        glTranslatef(-0.3,-0.4,0); ///手臂的旋轉中心
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);

        glPushMatrix();
            ////glTranslatef(x,y,z); ///把手臂放回身體
            glRotatef(angle,0,0,1);    ///旋轉
            glTranslatef(-0.7,-0.4,0); ///手臂的旋轉中心
            glColor3f(0,1,0);
            glRectf(0.7,0.5,1,0.3);
        glPopMatrix();
    glPopMatrix();


    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect TRT");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

Step2-4 把手臂放回身體,並把上手臂旋轉的註解消掉

#include <GL/glut.h>
float angle=0,oldX=0;
void mouse(int Button, int state, int x, int y){
    oldX= x;
}
void motion(int x,int y){
    angle += (x-oldX);
    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,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,0,0,1);    ///旋轉
            glTranslatef(-0.7,-0.4,0); ///手臂的旋轉中心
            glColor3f(0,1,0);
            glRectf(0.7,0.5,1,0.3);
        glPopMatrix();
    glPopMatrix();


    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect TRT");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

Step2-5 新增一個新專案week_13_rect_many_TRT
             1.增加一個左手,記得負號(鏡射)

#include <GL/glut.h>
float angle=0,oldX=0;
void mouse(int Button, int state, int x, int y){
    oldX= x;
}
void motion(int x,int y){
    angle += (x-oldX);
    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,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,0,0,1);    ///旋轉
            glTranslatef(-0.7,-0.4,0); ///手臂的旋轉中心
            glColor3f(0,1,0);
            glRectf(0.7,0.5,1,0.3);
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.3,0.4,0); ///把手臂放回身體
        glRotatef(angle,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,0,0,1);    ///旋轉
            glTranslatef(0.7,-0.4,0); ///手臂的旋轉中心
            glColor3f(0,1,0);
            glRectf(-0.7,0.5,-1,0.3);
        glPopMatrix();
    glPopMatrix();


    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect TRT");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

Step3-1 讓所有關節都有角度,大概20個角度
              1.設float angle[20] ,把下面angle改成 angle[0]、angle[1]......
              2.再設定一個int angleID=0;
                 angle[angleID]--->當angleID=0時,angle[0] 以此類推
              3.建立一個void keyboard函式
              4.在main函式裡面新增 glutKeyboardFunc(keyboard)
                  --->才能用鍵盤操作關節

#include <GL/glut.h>
float angle[20],oldX=0;
int angleID=0;
void mouse(int Button, int state, int x, int y){
    oldX= x;
}
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;
}
void motion(int x,int y){
    angle[angleID] += (x-oldX);
    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.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.3);
        glPopMatrix();
    glPopMatrix();


    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect TRT");

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

VERY BEAUTIFUL, VERY POWERFUL

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