2020年6月16日 星期二

week16_LSD's HW

1.紀錄動作下來(save&read)

#include <GL/glut.h>
int angle[10]={0,0,0,0,0, 0,0,0,0,0};
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
        glColor3f(1,1,1);///白色
glutSolidTeapot(0.3);///body

glPushMatrix();///右邊
            glTranslatef(0.3, 0,0);
            glRotatef(angle[0], 0,0,1);///轉動
            glTranslatef(0.2, 0,0);///最重要,旋轉中心要放在中心

            glColor3f(1,0,0);///紅色
            glutSolidTeapot(0.2);///右上手臂
            glPushMatrix();///右下手臂
                glTranslatef(0.2, 0,0);
                glRotatef(angle[1], 0,0,1);
                glTranslatef(0.2, 0,0);

                glColor3f(1,1,0);///黃
                glutSolidTeapot(0.2);
            glPopMatrix();
glPopMatrix();

glPushMatrix();///左邊
            glTranslatef(-0.3, 0,0);
            glRotatef(angle[2], 0,0,1);///轉動
            glTranslatef(-0.2, 0,0);///最重要,旋轉中心要放在中心

            glColor3f(0,1,0);///綠色
            glutSolidTeapot(0.2);///左上手臂
            glPushMatrix();
                glTranslatef(-0.2, 0,0);
                glRotatef(angle[3], 0,0,1);
                glTranslatef(-0.2, 0,0);

                glColor3f(0,0,1);///藍色
                glutSolidTeapot(0.2);///左下手臂
            glPopMatrix();
glPopMatrix();

glPopMatrix();
glutSwapBuffers();
}
int angleID=0;///要動哪個關節
int oldX=0, oldY=0;
#include <stdio.h>
FILE * fout=NULL;///檔案是NULL
FILE * fin=NULL;///File Input (fin)
void readAll()///TODO5: 把程式碼拉出來, 變成函式 readAll();
{
    if(fin==NULL) fin=fopen("motion.txt", "r" );///T開檔案
    for(int i=0; i<10; i++){
        ///scanf(  "%d", &a[i]);
        fscanf(fin,"%d", &angle[i]);///讀入
    }
}
void timer(int t)///TODO5: 用timer自動readAll()再更新
{
    glutTimerFunc(20, timer, t+1);///再註冊下一個時間點 20ms 之後
    readAll();
    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;///TODO2: 要動的關節
    if(key=='r'){///讀檔案
        readAll();
        glutPostRedisplay();///更新畫面/重畫
    }else if(key=='p'){///Play播放
        glutTimerFunc(20, timer, 0);///要用timer來自動播放
    }
    else if(key=='s'){
        void saveAll();
        saveAll();
    }
}
void mouse(int button, int state, int x, int y)
{
    oldX=x; oldY=y;///mouse按下去時, 記得位址
}
void saveAll()///全部存起來
{///(第1次是NULL,要把檔案開起來, 以後就不用再開它了...)
    if(fout==NULL) fout=fopen("motion.txt", "w+");///檔案是NULL
    for(int i=0; i<10; i++){
        printf("%d ", angle[i]);
        fprintf(fout, "%d ", angle[i]);
    }
    printf("\n");
    fprintf(fout, "\n");
}
void motion(int x, int y)
{
    angle[angleID] += (x-oldX);///讓關節動起來
    oldX=x;
    ///saveAll();///全部存起來
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week14 file");

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);keyboard運作
glutMouseFunc(mouse);
glutMotionFunc(motion);

glutMainLoop();
}
2.更改
(做第二次更改,利用alpha內插,讓新舊角度可以順利的內插出值並更新畫面
把關節數目變2倍時,要修改對應的陣列angle及oldAngle newAngle還有for迴圈,讓它可以移動加旋轉)
#include <GL/glut.h>
int angle[10]={0,0,0,0,0, 0,0,0,0,0};
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
        glColor3f(1,1,1);///白色
glutSolidTeapot(0.3);///body

glPushMatrix();///右邊
            glTranslatef(0.3, 0,0);
            glRotatef(angle[0], 0,0,1);///轉動
            glTranslatef(0.2, 0,0);///最重要,旋轉中心要放在中心

            glColor3f(1,0,0);///紅色
            glutSolidTeapot(0.2);///右上手臂
            glPushMatrix();///右下手臂
                glTranslatef(0.2, 0,0);
                glRotatef(angle[1], 0,0,1);
                glTranslatef(0.2, 0,0);

                glColor3f(1,1,0);///黃
                glutSolidTeapot(0.2);
            glPopMatrix();
glPopMatrix();

glPushMatrix();///左邊
            glTranslatef(-0.3, 0,0);
            glRotatef(angle[2], 0,0,1);///轉動
            glTranslatef(-0.2, 0,0);///最重要,旋轉中心要放在中心

            glColor3f(0,1,0);///綠色
            glutSolidTeapot(0.2);///左上手臂
            glPushMatrix();
                glTranslatef(-0.2, 0,0);
                glRotatef(angle[3], 0,0,1);
                glTranslatef(-0.2, 0,0);

                glColor3f(0,0,1);///藍色
                glutSolidTeapot(0.2);///左下手臂
            glPopMatrix();
glPopMatrix();

glPopMatrix();
glutSwapBuffers();
}
int angleID=0;///TODO2:要動哪個關節
int oldX=0, oldY=0;///TODO2:
#include <stdio.h>///TODO3: //TODO4:記得要移高一點哦!
FILE * fout=NULL;///TODO3: 檔案是NULL
FILE * fin=NULL;///TODO4: File Input (fin)
void readAll()///TODO5: 把程式碼拉出來, 變成函式 readAll();
{
    if(fin==NULL) fin=fopen("motion.txt", "r" );///TODO4: 開檔案
    for(int i=0; i<10; i++){
        ///scanf(  "%d", &a[i]);
        fscanf(fin,"%d", &angle[i]);///TODO4: 讀入
    }
}
int oldAngle[10];///新的會變成舊的///week162
int newAngle[10];///先讀到新的///week162
void timer(int t)///TODO5: 用timer自動readAll()再更新
{
    glutTimerFunc(20, timer, t+1);///再註冊下一個時間點 20ms 之後
    if(t==0){
        if(fin==NULL) fin=fopen("motion.txt","r");///week162
        for(int i=0;i<10;i++){///week162
            fscanf(fin,"%d",&newAngle[i]);///week162
        }
    }
    float alpha = (t%10)/10.0;///week162讓alpha介於0.0-1.0中間
    if( t%10==0 ){///每個進入橙色區域,就再做一次調整
        for(int i=0;i<10;i++){///week162
            oldAngle[i] = newAngle[i];///week162
            fscanf(fin,"%d",&newAngle[i]);///week162
        }
    }
    for(int i=0;i<10;i++){///week162
        angle[i] = alpha*newAngle[i] + (1-alpha)*oldAngle[i];///week162真的內插
    }
    ///readAll();
    glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
    if(key=='0') angleID=0;///TODO2: 要動的關節
    if(key=='1') angleID=1;///TODO2: 要動的關節
    if(key=='2') angleID=2;///TODO2: 要動的關節
    if(key=='3') angleID=3;///TODO2: 要動的關節
    if(key=='r'){///TODO4: 我們要讀檔案囉!!!!
        readAll();///TODO5:
        glutPostRedisplay();///TODO4: 更新畫面/重畫
    }else if(key=='p'){///TODO5: Play播放
        glutTimerFunc(20, timer, 0);///要用timer來自動播放
    }else if(key=='s'){///week161
        void saveAll();///week161
        saveAll();///week161
    }
}
void mouse(int button, int state, int x, int y)
{
    oldX=x; oldY=y;///mouse按下去時, 記得在哪裡
}
void saveAll()///全部存起來
{///(第1次是NULL,要把檔案開起來, 以後就不用再開它了...)
    if(fout==NULL) fout=fopen("motion.txt", "w+");///檔案是NULL
    for(int i=0; i<10; i++){
        printf("%d ", angle[i]);
        fprintf(fout, "%d ", angle[i]);
    }
    printf("\n");
    fprintf(fout, "\n");
}
void motion(int x, int y)
{
    angle[angleID] += (x-oldX);///讓關節動起來
    oldX=x;
    ///saveAll();///全部存起來
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week14 file");

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);///keyboard運作
glutMouseFunc(mouse);///mouse
glutMotionFunc(motion);///motion

glutMainLoop();

}

沒有留言:

張貼留言