顯示具有 09160102_鄭煒霖 標籤的文章。 顯示所有文章
顯示具有 09160102_鄭煒霖 標籤的文章。 顯示所有文章

2022年5月31日 星期二

一定是大拇指的啦

 Step 1 : 播放聲音

1.1 File-New-Empty File 命名為week15-1_PlaySound

1.2 程式碼

#include <windows.h>

#include <stdio.h>

int main()

{

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

    PlaySound("badbad.wav",NULL,SND_SYNC);///假設一個badbad.wav做測試

   ///檔案不存在會有小的錯誤提示音

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

}

1.3 設定咒語
setting -> Linker settings -> Add -> winmm ->  OK

紅色代表要執行的步驟,綠色是指之前opencv的設定,下載完.wav檔後要放在程式碼資夾

step 2 : AND_SYNC  、  SND_ASYNC
2.1 Do Re Me 三音
#include <windows.h>
#include <stdio.h>
int main()
{
    PlaySound("do.wav" , NULL , SND_ASYNC);///ASYNC不等待直接執行
    PlaySound("re.wav" , NULL , SND_ASYNC);///ASYNC不等待直接執行
    PlaySound("me.wav" , NULL , SND_ASYNC);///ASYNC不等待直接執行
}
2.2 輸入數字決定音樂
#include <windows.h>
#include <stdio.h>
int main()
{
    PlaySound("07042111.wav" , NULL , SND_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);
    }
}

step 3 : mp3檔
3.1  File-New-Empty File 命名為week15-3_MP3
3.2 到moodle下載CMP3_MCI.h 放到跟程式碼同資料夾內
3.3 程式碼
#include <stdio.h>
#include "CMP3_MCI.h"
CMP3_MCI mp3;
int main()
{
    mp3.Load("07042111.mp3");
    mp3.Play();

    printf("隨便等你輸入數字,不然程式就要結束囉");
    int n;
    scanf("%d",&n);
}

2022年5月24日 星期二

一定是大拇指的啦

 step 1 : fprintf 讀寫檔案

1.1 先設立資料夾在桌面 命名為week14-1_ fprintf

1.2  open-empty file 存檔為week14-1.cpp

1.3 程式碼

#include <stdio.h>

int main()

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

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

    printf(     "Hello world\n");

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

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

}

1.4 資料夾內會有四個檔案


step 2 : fprintf fscanf 

21 先設立資料夾在桌面 命名為week14-1_ fprintf_fscanf

2.2  open-empty file 存檔為week14-2.cpp

2.3 程式碼

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

}


step 3 : 結合上週many_TRT

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

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

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

#include <GL/glut.h>

#include <stdio.h>///printf用

float angle[4] , oldx=0;///angle變陣列以致可以放更多選轉角度

int angleID=0;///0號關節 1號關節

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

    }

}

void keyboard(unsigned char key , int x  , int y)///根據按數字鍵多少決定第幾個關節要轉動

{ ///按數字鍵決定要改變哪個angleID

    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);///angle變成陣列

    oldx=x;

    mywrite();///執行用

    glutPostRedisplay();///請glut重新re display

}

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);///旋轉 對Z軸轉 ///angle[0] 第一個值

        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);;///(3)拉回到右上角

            glRotatef(angle[1],0,0,1);///(2)旋轉角度  ///angle[1] 第二個值

            glTranslatef(-0.7,-0.4,0);///(1)將軸心放到世界中心

            glColor3f(0,1,10);

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

        glPopMatrix();

    glPopMatrix();


///左手跟右手的X軸相反,所以要加負號

    glPushMatrix();

        glTranslatef(-0.3,0.4,0);///

        glRotatef(angle[2],0,0,1);///旋轉 對Z軸轉  ///angle[2] 第三個值

        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);///(3)拉回到左上角

            glRotatef(angle[3],0,0,1);///(2)旋轉角度  ///angle[3] 第四個值

            glTranslatef(0.7,-0.4,0);///(1)將軸心放到世界中心

            glColor3f(0,1,10);

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

    glutMouseFunc(mouse);

    glutMotionFunc(motion);

    glutDisplayFunc(display);

    glutMainLoop();

}

step 4 :做動畫
4.1  開新專案 命名為week_angle_fprintf_fscanf
4.2 貼上剛剛的程式碼 新增紅色的程式碼///發現有問題可以去freeglut/bin/file.txt查看
#include <GL/glut.h>
#include <stdio.h>
float angle[4] , oldx=0;///angle變陣列以致可以放更多選轉角度
int angleID=0;///0號關節 1號關節
FILE * fout = NULL ,  *fin = 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] );///檔案印出來
    }
    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)///根據按數字鍵多少決定第幾個關節要轉動
{ ///按數字鍵決定要改變哪個angleID
    if (key=='r') myRead();///按著r可以重播畫面
    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);///angle變成陣列
    oldx=x;
    myWrite();
    glutPostRedisplay();///請glut重新re display
}
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);///旋轉 對Z軸轉 ///angle[0] 第一個值
        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);;///(3)拉回到右上角
            glRotatef(angle[1],0,0,1);///(2)旋轉角度  ///angle[1] 第二個值
            glTranslatef(-0.7,-0.4,0);///(1)將軸心放到世界中心
            glColor3f(0,1,10);
            glRectf(0.7 , 0.5 , 1.0 , 0.3);///右上手臂
        glPopMatrix();
    glPopMatrix();

///左手跟右手的X軸相反,所以要加負號
    glPushMatrix();
        glTranslatef(-0.3,0.4,0);///
        glRotatef(angle[2],0,0,1);///旋轉 對Z軸轉  ///angle[2] 第三個值
        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);///(3)拉回到左上角
            glRotatef(angle[3],0,0,1);///(2)旋轉角度  ///angle[3] 第四個值
            glTranslatef(0.7,-0.4,0);///(1)將軸心放到世界中心
            glColor3f(0,1,10);
            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_many_TRT");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}
4.3 更改freeglut/bin 位置
4.3.1 week14_angle_fprintf_fscanf 右鍵使用notepad++開啟
4.3.2 將紅色框框內working_dir 中的 " "內改寫成.

4.3.3 存檔後回codeblock  會有reload project 要按Yes
4.3.4 桌面freeglut/bin/freeglut.dll 這個檔案要複製貼上到當前的資料夾
(兩個是綁在一起的 否則無法執行)

step 5 : 計時器
5.1 開新專案 命名為week14_timer
5.2 程式碼(一次設定好)
#include <stdio.h>
#include <GL/glut.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");
    ///一次設3次
    glutTimerFunc(1000,timer,1);///第一秒 1 
    glutTimerFunc(2000,timer,2);///第二秒 2 
    glutTimerFunc(3000,timer,3);///第三秒 3 
    glutDisplayFunc(display);
    glutMainLoop();
}
5.3 程式碼(自動設定+1)
#include <stdio.h>
#include <GL/glut.h>
void timer(int t)
{
    printf("起床,現在時間 : %d\n",t);
    glutTimerFunc(1000,timer,t+1);///自動加1
}
void display()
{

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

    glutTimerFunc(5000,timer,0);///第5秒開始執行
    glutDisplayFunc(display);
    glutMainLoop();
}
5.4程式碼(會秒會發出聲音)
#include <stdio.h>
#include <GL/glut.h>
#include <mmsystem.h>///do wav 用
void timer(int t)
{
    printf("我起床囉: %d\n",t);
    PlaySound("do.wav",NULL,SND_ASYNC);
    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();
}

2022年5月17日 星期二

一定是大拇指的啦

 Step 1 : 四邊形

1.1開啟專案 ,命名為week13_rect_TRT

1.2 新增四邊形當身體

#include <GL/glut.h>


void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glRectf( 0.3,0.5,-0.3,-0.5);///四邊形

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

}


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

1.4 調整手臂軸心
#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(-0.3,-0.4,0);///(1)把手臂選轉中心放世界中心
                                                ///將原本軸心(0.3,0.4) 拉到(0,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();
}

1.5 手臂旋轉45度
#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();    
        glRotatef(45,0,0,1);///(2)旋轉 對Z軸轉45度
        glTranslatef(-0.3,-0.4,0);///(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();
}

1.6 調整手臂到右上角
#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(0.3,0.4,0);///(3)手臂調整到要的位置
        glRotatef(45,0,0,1);///(2)旋轉 對Z軸轉
        glTranslatef(-0.3,-0.4,0);///(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();
}

step 2 : 使用mouse、motion
2.1 增加mouse指令
#include <GL/glut.h>
float angle=45 , oldx=0;///設定選轉角度變數
void mouse(int button , int state , int x ,int y)  ///用滑鼠點擊
{
    oldx=x;
}
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);///旋轉 對Z軸轉 ///角度轉angle度
        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);///mouse用
    glutDisplayFunc(display);
    glutMainLoop();
}

2.2 增加motion指令
#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();///請glut重新re display
}
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);///旋轉 對Z軸轉
        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);///motion用
    glutDisplayFunc(display);
    glutMainLoop();
}
step 3 : 新增關節
3.1 開新專案 ,命名為week13_rect_TRT_TRT
3.2 複製week13_rect_TRT程式碼 貼上去剛剛新開的專案
3.3 貼上程式碼
///week13_rect_TRT_TRT
#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();///請glut重新re display
}
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);///旋轉 對Z軸轉
        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);///(3)拉回到右上角
            glRotatef(angle,0,0,1);///(2)旋轉角度
            glTranslatef(-0.7,-0.4,0);///(1)將軸心放到世界中心
            glColor3f(0,1,10);
            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_TRT");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

step 4 :  貼上左手
4.1 開新專案 ,命名為week13_rect_many_TRT
4.2 複製week13_rect_TRT程式碼 貼上去剛剛新開的專案
4.3貼上程式碼
///week13_rect_many_TRT
#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();///請glut重新re display
}
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);///旋轉 對Z軸轉
        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);;///(3)拉回到右上角
            glRotatef(angle,0,0,1);///(2)旋轉角度
            glTranslatef(-0.7,-0.4,0);///(1)將軸心放到世界中心
            glColor3f(0,1,10);
            glRectf(0.7 , 0.5 , 1.0 , 0.3);///右上手臂
        glPopMatrix();
    glPopMatrix();

///左手跟右手的X軸相反,所以要加負號
    glPushMatrix();
        glTranslatef(-0.3,0.4,0);///
        glRotatef(angle,0,0,1);///旋轉 對Z軸轉
        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);///(3)拉回到左上角
            glRotatef(angle,0,0,1);///(2)旋轉角度
            glTranslatef(0.7,-0.4,0);///(1)將軸心放到世界中心
            glColor3f(0,1,10);
            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_many_TRT");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}
step 5 : 新增更多角度
5.1 新增keyboard函數
///week13_rect_many_TRT
#include <GL/glut.h>
float angle[4] , oldx=0;///angle變陣列以致可以放更多選轉角度
int angleID=0;///0號關節 1號關節
void keyboard(unsigned char key , int x  , int y)///根據按數字鍵多少決定第幾個關節要轉動
{ ///按數字鍵決定要改變哪個angleID
    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);///angle變成陣列
    oldx=x;
    glutPostRedisplay();///請glut重新re display
}
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);///旋轉 對Z軸轉 ///angle[0] 第一個值
        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);;///(3)拉回到右上角
            glRotatef(angle[1],0,0,1);///(2)旋轉角度  ///angle[1] 第二個值
            glTranslatef(-0.7,-0.4,0);///(1)將軸心放到世界中心
            glColor3f(0,1,10);
            glRectf(0.7 , 0.5 , 1.0 , 0.3);///右上手臂
        glPopMatrix();
    glPopMatrix();

///左手跟右手的X軸相反,所以要加負號
    glPushMatrix();
        glTranslatef(-0.3,0.4,0);///
        glRotatef(angle[2],0,0,1);///旋轉 對Z軸轉  ///angle[2] 第三個值
        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);///(3)拉回到左上角
            glRotatef(angle[3],0,0,1);///(2)旋轉角度  ///angle[3] 第四個值
            glTranslatef(0.7,-0.4,0);///(1)將軸心放到世界中心
            glColor3f(0,1,10);
            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_many_TRT");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

2022年5月10日 星期二

一定是大拇指的啦

    step 0 :下載模型

0.0  jsyeh.org/3dcg10 下載  data.zip  , windows.zip

0.1 data.zip =>下載/windows/data

      windows.zip => 下載/windows

      source.zip=>下載/windows

      執行windows/Transformation.exe

step 1 : 示範轉動方式

  1.1 在右邊轉動的藍色車子(自轉)

```

   glTranslaterf(0.9 , 0.0 , 0.0);///移動 在右邊

   glRotatef( 角度 , 0 , 1 ,0 );///旋轉

   畫一台藍色車子

```


   1.2 繞著中間轉彎的車子 (公轉)

```

   glRotatef( 角度 , 0 , 1 ,0 );///旋轉

   glTranslaterf(0.9 , 0.0 , 0.0);///移動 在右邊

   畫一台藍色車子

```


step 2 : 用另一個程式理解(TRT)

2.1 120.125.80.50/GL 有程式 (內含其中、練習)

2.2右下角點ToDraw , 左邊可以畫圖

2.3 Ctrl+R Reload可以清空圖

2.4 畫身體 叫myDrawObject(0)

2.5 畫手臂 叫myDrawObject(1)

2.6 拉動程式碼可以改變位置

2.7 按右上角 angle= 在按空白鍵 會自動變成動畫


step 3 : 新增新的功能

3.1 改成身體不動 手臂正常揮手

```

glPushMatrix();

        myDrawObject(0);///畫身體

        glRotatef( angle , 0 , 0 , 1) ;///轉下面整個東西

        glTranslatef( -0.3 , -0.19 , 0 );///往左下方移動(軸心放中心) 

        myDrawObject(1);///手臂(右上方)

plPopMatrix();

```

3.2 上面 T 擺好 ,手掛到身體右上角

```

myDrawObject(0);///畫身體

glPushMatrix();

        glTranslatef(0.29 , 0.31 , 0 ) ///往右上方移動

        glRotatef( angle , 0 , 0 , 1) ;///轉下面整個東西

        glTranslatef( -0.3 , -0.19 , 0 );///往左下方移動(軸心放中心) 

        myDrawObject(1);///手臂(右上方)

plPopMatrix();

```

step 4 : 實作

4.1 開新專案 命名為week12_TRT

4.2 貼上程式碼

#include <GL/glut.h>

void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

        glTranslatef(0 , 0, 0);

        glRotatef( angle , 0 , 0 ,1 );

        glTranslatef( 0 , 0 , 0);

        glutSolidTeapot(0.2);

    glPopMatrix();

    glutSwapBuffers();

}

int main(int argc, char **argv)

{

    glutInit (&argc , argv);

    glutInitDisplayMode( GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week12 TRT");


    glutIdleFunc(display);

    glutDisplayFunc(display);

    glutMainLoop();

}

4.3把TRT 複製貼上去

4.4 加上rotate 、angle++ 讓它旋轉
       TRT順序為321
#include <GL/glut.h>
float angle=0 ;///角度
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(0 , 0, 0);///(3)掛到右邊(整個往右移動)
        glRotatef( angle , 0 , 0 ,1 );///(2)旋轉(向手臂一樣轉動)
        glTranslatef( 0 , 0 , 0);///(1)選轉中心放到中間
        glutSolidTeapot(0.2);
    glPopMatrix();
    glutSwapBuffers();
    angle+=0.1;///每次增加0.1度
}
int main(int argc, char **argv)
{
    glutInit (&argc , argv);
    glutInitDisplayMode( GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week12 TRT");

    glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMainLoop();
}

4.5 茶壺超人 
#include <GL/glut.h>
float angle=0 ;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);///白色
    glutSolidTeapot(0.3);///大的茶壺當身體
    glPushMatrix();
        glTranslatef(0.2 , 0, 0);///(3)掛到右邊(整個往右移動)
        glRotatef( angle , 0 , 0 ,1 );///(2)旋轉(向手臂一樣轉動)
        glTranslatef( 0.2 , 0 , 0);///(1)選轉中心放到中間
        glColor3f(1,0,0);///紅色
        glutSolidTeapot(0.2);///小的當手臂
    glPopMatrix();
    glutSwapBuffers();
    angle+=0.1;
}
int main(int argc, char **argv)
{
    glutInit (&argc , argv);
    glutInitDisplayMode( GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week12 TRT");

    glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMainLoop();
}
 
Step 5 : 回家作業

5.1 可自己件模型 或是上網查 ( 需有身體、手臂等等)

5.2 開新專案 命名為week12_TRT_TRT

5.3 範例 (身體+右手)

 ```

引用模型:

#include "glm.h"

GLMmodel * pmodel = NULL;

GLMmodel * pmodel = NULL;///上手臂

GLMmodel * pmodel = NULL;///下手軸

```

#include <GL/glut.h>

float angle=0 ;

void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glColor3f(1,1,1);

    glutSolidTeapot(0.3);///身體 ///glmDraw( pmodel  ,...)

    glPushMatrix();

        glTranslatef(0.2 , 0, 0);

        glRotatef( angle , 0 , 0 ,1 );

        glTranslatef( 0.2 , 0 , 0);

        glColor3f(1,0,0);

        glutSolidTeapot(0.2);///手臂 /// glmDraw(arm , ...)

        glPushMatrix();

            glTranslatef(0.2 , 0, 0);

            glRotatef( angle , 0 , 0 ,1 );

            glTranslatef( 0.2 , 0 , 0);

            glColor3f(1,0,0);

            glutSolidTeapot(0.2);///手肘  /// glmDraw(hand , ...)

        glPushMatrix();

    glPopMatrix();

    glutSwapBuffers();

    angle+=0.1;

}

int main(int argc, char **argv)

{

    glutInit (&argc , argv);

    glutInitDisplayMode( GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week12 TRT");


    glutIdleFunc(display);

    glutDisplayFunc(display);

    glutMainLoop();

}

5.3 範例 (身體+右手+左手) 

位置、角度改成負的(因為左右手對稱)

 ```

引用模型:

#include "glm.h"

GLMmodel * pmodel = NULL;

GLMmodel * pmodel = NULL;///上手臂

GLMmodel * pmodel = NULL;///下手軸

```

#include <GL/glut.h>

float angle=0 ;

void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);


    glColor3f(1,1,1);

    glutSolidTeapot(0.3);///身體 ///glmDraw( pmodel  ,...)


    glPushMatrix();///右邊的手臂手肘

        glTranslatef(0.2 , 0, 0);

        glRotatef( angle , 0 , 0 ,1 );

        glTranslatef( 0.2 , 0 , 0);

        glColor3f(1,0,0);

        glutSolidTeapot(0.2);///手臂 /// glmDraw(arm , ...)

        glPushMatrix();

            glTranslatef(0.2 , 0, 0);

            glRotatef( angle , 0 , 0 ,1 );

            glTranslatef( 0.2 , 0 , 0);

            glColor3f(1,0,0);

            glutSolidTeapot(0.2);///手肘  /// glmDraw(hand , ...)

        glPushMatrix();

    glPopMatrix();


    glPushMatrix();///左邊的手臂手肘

        glTranslatef(-0.2 , 0, 0);

        glRotatef( -angle , 0 , 0 ,1 );

        glTranslatef( -0.2 , 0 , 0);

        glColor3f(1,0,0);

        glutSolidTeapot(0.2);///手臂 /// glmDraw(arm , ...)

        glPushMatrix();

            glTranslatef(-0.2 , 0, 0);

            glRotatef( -angle , 0 , 0 ,1 );

            glTranslatef( -0.2 , 0 , 0);

            glColor3f(1,0,0);

            glutSolidTeapot(0.2);///手肘  /// glmDraw(hand , ...)

        glPushMatrix();

    glPopMatrix();


    glutSwapBuffers();

    angle+=0.1;

}

int main(int argc, char **argv)

{

    glutInit (&argc , argv);

    glutInitDisplayMode( GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week12_TRT_TRT");


    glutIdleFunc(display);

    glutDisplayFunc(display);

    glutMainLoop();

}


}

VERY BEAUTIFUL, VERY POWERFUL

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