顯示具有 09160756_陳彥呈 標籤的文章。 顯示所有文章
顯示具有 09160756_陳彥呈 標籤的文章。 顯示所有文章

2022年6月23日 星期四

 WEEK16 天氣晴

1. alpha 內插公式: 0.0 ~ 1.0

➡ angle=alpha*新角度+(1-alpha)*舊角度

利用excel來練習



2-1.複製上週 week15-angle-TRT-again 程式碼 

檔名: week16-interpolation

改善按r之後動作不連續的狀況,利用alpha內插法

程式碼:


4.改視角,aspect ratio:長寬比(寬:長)

#include <GL/glut.h>

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向量

}

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glutSolidTeapot(1);

    glutSwapBuffers();

}

void motion(int x,int y)

{

    glMatrixMode(GL_MODELVIEW);///3D Model+View

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


    glutDisplayFunc(display);

    glutMotionFunc(motion);

    glutReshapeFunc(reshape);///初始視角

    glutMainLoop();

}



2022年6月7日 星期二

 WEEK15 天氣陰

1-1. CodeBlocks / File / Empty File 檔名:week15-1_PlaySound.cpp

1-2

程式碼:


1-3要在 Setting / Compiler / Linker Settings /Add:wnmm

1-4 在 https://sound-effects.bbcrewind.co.uk/search 下載聲音

把下載的聲音放到執行目錄


PS還沒進入迴圈前,會是動物07074095.wav的聲音,按下1,2,時,便會進入迴圈,且不

用等待音檔撥放完,就可以按下一個數字撥放其他音檔。

3-1 wav (檔案較大)v.s mp3(檔案較小),PlaySound()只能用原始的wav檔

3-2 到moodle下載 CMP3_MCI.h 放在同目錄中 檔名:week15-mp3.cpp的位置



每調好一個動作按s存檔,再按r就可以了,也可以到freeglut/bin/file.txt


完整程式碼:

#include <GL/glut.h>

#include <stdio.h>

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(" %.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 mouse(int Button, int state, int x, int y){

    oldX= x;

}

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

    if(key=='s') myWrite(); ///調好動作再存檔

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

    glutCreateWindow("week15-angles-TRT-again");

    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutMotionFunc(motion);

    glutDisplayFunc(display);

    glutMainLoop();

}




 WEEK14 天氣陰

1.寫檔

開啟codeblocks --> File/Empty file 檔名:week14-1

程式碼:



2.讀檔

程式碼:


3.開啟codeblocks 新增新專案,檔名:week14-angle-fprintf

複製week13-rect-many-TRT程式碼

程式碼:

完整程式碼:

#include <GL/glut.h>
#include <stdio.h>;
float angle[20],oldx=0;
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");///myWrite中加入,每呼叫一次,小黑視窗跳行
    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=='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);
    glRectd(0.3,0.5,-0.3,-0.5);///身體
    glPushMatrix();
        glTranslatef(0.3,0.4,0);///把手臂掛回身體
        glRotatef(angle[0],0,0,1);///對Z軸旋轉
        glTranslatef(-0.3,-0.4,0);///把手臂的旋轉中心,放中心
        glColor3f(1,0,0);
        glRectd(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);///綠色手臂
            glRectd(0.7,0.5,1.0,0.3);
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.3,0.4,0);///把手臂掛回身體
        glRotatef(angle[2],0,0,1);///對Z軸旋轉
        glTranslatef(0.3,-0.4,0);///把手臂的旋轉中心,放中心
        glColor3f(1,0,0);
        glRectd(-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);///綠色手臂
            glRectd(-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");

    glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMotionFunc(motion);///呼叫前面motion函式
    glutMouseFunc(mouse);///呼叫前面mouse函式
    glutKeyboardFunc(keyboard);///呼叫前面的keyboard函式

    glutMainLoop();
}







2022年5月24日 星期二

 Week13 天氣晴


1.    畫一個長方形(glRectd(0.3,0.5,-0.3,-0.5))

程式碼:
    #include <GL/glut.h>
    void display()

    
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glRectd(0.3,0.5,-0.3,-0.5);
           glPushMatrix();
    }

    int main(int argc, char**argv)

    {
       glutInit( &argc, argv );
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

        glutInitWindowSize(600,600);
        glutCreateWindow("week13-rect-TRT");
    
        glutIdleFunc(display);
        glutDisplayFunc(display);

        glutMainLoop();
    }


1-2 加上手臂
程式碼:
  #include <GL/glut.h>
void display()

{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glRectd(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);
        glRectd(0.3,0.5,0.7,0.3);///手臂(2點分別為左上角&右上角)
    glPopMatrix();
    glutSwapBuffers();

}

int main(int argc, char**argv)

{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutInitWindowSize(600,600);
    glutCreateWindow("week13-rect-TRT");

    glutIdleFunc(display);
    glutDisplayFunc(display);

    glutMainLoop();
}


1-3 手臂旋轉
        
        程式碼:glTranslated(-0.3,-0.4,0);



1-4.旋轉45度+把整個手臂移動至右上方

程式碼:



1-5.用mouse,motion改變旋轉角度

完整程式碼:

#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);
    glRectd(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);
        glRectd(0.3,0.5,0.7,0.3);///手臂(2點分別為左上角&右上角)
    glPopMatrix();
    glutSwapBuffers();

}

int main(int argc, char**argv)

{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
  
    glutInitWindowSize(600,600);
    glutCreateWindow("week13-rect-TRT");
    glutIdleFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);

    glutMainLoop();
}


2-1.加一個綠色手臂

2.2.把TRT加上去



3-1.把左邊手臂加上,並且改掉x的正負號(因為鏡像)

4-1增加多個角度

PS 把所有X座標改成負的

完整程式碼:

#include <GL/glut.h>
float angle[20],oldx=0;
int angleID=0;///0號關節,1號關節...
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);
    oldx=x;
    glutPostRedisplay();///請glut重新整理
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glRectd(0.3,0.5,-0.3,-0.5);///身體
    glPushMatrix();
        glTranslatef(0.3,0.4,0);///把手臂掛回身體
        glRotatef(angle[0],0,0,1);///對Z軸旋轉
        glTranslatef(-0.3,-0.4,0);///把手臂的旋轉中心,放中心
        glColor3f(1,0,0);
        glRectd(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);///綠色手臂
            glRectd(0.7,0.5,1.0,0.3);
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.3,0.4,0);///把手臂掛回身體
        glRotatef(angle[2],0,0,1);///對Z軸旋轉
        glTranslatef(0.3,-0.4,0);///把手臂的旋轉中心,放中心
        glColor3f(1,0,0);
        glRectd(-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);///綠色手臂
            glRectd(-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");

    glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMotionFunc(motion);///呼叫前面motion函式
    glutMouseFunc(mouse);///呼叫前面mouse函式
    glutKeyboardFunc(keyboard);///呼叫前面的keyboard函式

    glutMainLoop();
}










 Week12天氣晴

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

2.打開 window/Transformation.exe

    glTranslatef( 0.9 , 0 , 0 );

    glRotatef( angle , 0 , 1 , 0 );

    (往右轉的藍色車子)

    glRotatef( angle , 0 , 1 , 0 );

    glTranslatef( 0.9 , 0 , 0 );

    (繞著中間轉彎)

   

3.對特定軸轉動練習

    (1) 120.125.80.50/GL

    (2) ctrl-R清空

    (3)畫身體 myDrawObject(0)

     畫手臂 myDrawObject(1)

    (4)可更改程式碼順序

    (5)按 angle= 再按空白建 會自動變成動畫\


4.用茶壺來旋轉

    



程式碼:

#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 );///小茶壼 想像它是手臂
        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();
        glPopMatrix();

        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 );///小茶壼 想像它是手臂
        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();
        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();
}

#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);///掛到右邊,把整個往右移動
        glRotatef(angle,0,0,1);
        glTranslatef(0.2,0,0);///把手臂的旋轉中心移至中心
        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();
}


   





VERY BEAUTIFUL, VERY POWERFUL

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