2020年6月10日 星期三

week16

week16

Alpha
0 26
0.1 37.1
0.2 48.2
0.3 59.3
0.4 70.4
0.5 81.5
0.6 92.6
0.7 103.7
0.8 114.8
0.9 125.9
1 137

a3=0.1*137+(1-a3)*26


---------------------------------------------------------------------------------------------------------------------
程式碼

#include <GL/glut.h>
#include <stdio.h>

FILE * fout=NULL;
FILE * fin=NULL;       宣告檔案指標
int angle[10]={0,0,0,0,0, 0,0,0,0,0};
int angleID=0;
int oldX, oldY;


void display();
void motion(int x, int y);
void mouse(int button, int state, int x, int y);
void keyboard(unsigned char key, int x, int y);
void saveAll();


void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);     白色
    glutSolidTeapot(0.2);  身體

    glPushMatrix();    右側
        glTranslatef(0.2, 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.2, 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();                      右側
    glutSwapBuffers();
}

void mouse(int button, int state, int x, int y)
{            備份(old)   mouse位置
           oldX=x; oldY=y; 
}
          交給函式來做事
void saveAll()     (自己寫的函式)全部存檔
{        裡面宣告,不會讓外面記起來
    if(fout==NULL) fout=fopen("motion.txt", "w+");     開檔
    for(int i=0; i<10; i++){                                                for迴圈,把陣列全用
         printf(      "%d ", angle[i]);                                     印畫面
        fprintf(fout, "%d ", angle[i]);                                   寫檔案
    }
     printf(      "\n");         跳行
    fprintf(fout, "\n");      跳行
}
void motion(int x, int y)
{      滑動,更新 angleID對應的angle[]
    angle[angleID] += (x-oldX);
    oldX=x;
    (自己寫的函式)全部存檔
    glutPostRedisplay();
}
void 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 讀檔
    }
}
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;
    if(key=='s') saveAll();
                           調好動作,存檔
    if(key=='r'){   撥放
        readAll();
        glutPostRedisplay();
    }
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week14 file");

    glutDisplayFunc(display);
    glutMouseFunc(mouse);                   備份(old)mouse位置
    glutMotionFunc(motion);                 滑動,更新 angleID對應的angle[]
    glutKeyboardFunc(keyboard);          鍵盤,選angleID
    glutMainLoop();
}

---------------------------------------------------------------------------------------------------------------------

沒有留言:

張貼留言