2022年6月7日 星期二

AhFatKr的電腦圖學筆記week16

 #week16

##step01-1

alpga內插公式: alpha: `0.0~ 1.0`

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

ex.

    alpha: 0 => 舊

    alpha:0.5 =>半新半舊

    alpha: 1=> 新

使用Excel or Google Spreadsheet 來做


#step01-2

用程式試試看 week15_angles_TRT_again拿來改

1.File-New-Project,GLUT,week16_interpolation 內插

2.複製程式,執行,按s會存一行,mouse motion改動作4次,按s存檔

3.原本失敗的結果,按r會讀到動作但不連續,關掉


接下來要改造程式

1.void myInterplate(float alpha)

```
///a
#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]);///檔案印出來
    }///這裡老師沒有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;
float alpha=0;
void keyboard(unsigned char key,int x,int y){
    if(key=='p'){///Play
        if(t%30==0)myRead();
        myInterpolate((t%30)/30.0);///介於0.0~1.0
        glutPostRedisplay();
        t++;
    }
    if(key=='s') myWrite();///調好動作才存檔
    if(key=='r') myRead();
    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重畫畫面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);///(3)把手臂掛回身體
        glRotatef(angle[0],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);

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

        glPushMatrix();///左半部
        glTranslatef(-0.3,0.4,0);///(3)把手臂掛回身體
        glRotatef(angle[2],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);

        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();
}```
##step02-1
接下來讓動畫可以不用按P就自動播放,並讓它變得更流暢只需要加入幾行程式碼就好!
##step02-2
利用課本上的projection來幫助我們理解照相機的運作方式


##step03-1
1.File-Bew-Projec,GLUT,week16_camera_projection_gluLookAt
2.輩分177行範例,要改造裡面的程式
3.aspect ratio 長寬比 ex.1920x1080, 1280x720, 640*480, 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);///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();
}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week16 camera");;
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);///範例是用resize
    glutMainLoop();
}
```





沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

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