2021年6月9日 星期三

week16

 

-利用上禮拜的程式碼
新增void mouse(); void motion();
設x,oldx
動畫按0123並配合滑鼠移動能讓關節移動 
-再motion裡面新增程式碼:


    if(fout==NULL) fout=fopen("angle.txt", "w+");
    for(int i=0; i<20;i++) fprintf(fout, "%.1f", angle[i]);
    fprintf(fout, "\n");
    for(int i=0;i<20;i++) printf("%.1f", angle[i]);
    printf("\n");

可以看到關節移動的角度
-播放動畫 FILE * fin = NULL;

再timer裡面新增動畫指令 
-再keyboard()裡面新增save,read,play
利用key==' '; 用鍵盤上的s可以存檔 
儲存位置後再讀檔,並利用鍵盤上的r讀檔
讀檔後就能撥放了


-再timer新增內插 讓動畫更順
設float angleOld[20]={}, angleNew[20]={};
更新angle, 利用alpha 0~1中間進行angle[i]= alpha*angleNew[i]+(1-alpha)*angleOld[i];

程式碼:

#include <stdio.h>

#include <GL/glut.h>
float angle[20]={}, diff=2;
float angleOld[20]={}, angleNew[20]={};
int angleID=0;
int oldx=0;
FILE * fout = NULL;
FILE * fin = NULL;///播放動畫
void timer(int t)
{
    glutTimerFunc(30,timer,t+1);
    if(t%30==0){///每10個frame,讀一筆新資料
        for(int i=0;i<20;i++) angleOld[i]=angleNew[i];
        if(fin==NULL) fin=fopen("angle.txt", "r");
        for(int i=0;i<20;i++) fscanf(fin, "%f", &angleNew[i]);
        ///讀新資料前先備份
    }
    float alpha=(t%30)/30.0;
    for(int i=0;i<20;i++){
        angle[i]= alpha*angleNew[i]+(1-alpha)*angleOld[i];
    }
    glutPostRedisplay();
}
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();

//    if(fout==NULL) fout=fopen("angle.txt", "w+");
//    for(int i=0; i<20;i++) fprintf(fout, "%.1f", angle[i]);
//    fprintf(fout, "\n");
//    for(int i=0;i<20;i++) printf("%.1f", angle[i]);
//    printf("\n");

}
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;
    if(key=='s'){///save
        if(fout==NULL) fout=fopen("angle.txt", "w+");
        for(int i=0; i<20;i++) fprintf(fout, "%.1f", angle[i]);
        fprintf(fout, "\n");
        for(int i=0;i<20;i++) printf("%.1f", angle[i]);
        printf("\n");

    }
    if(key=='r'){///read
        if(fin==NULL) fin=fopen("angle.txt", "r");
        for(int i=0;i<20;i++) fscanf(fin, "%f",&angle[i]);
        glutPostRedisplay();///重畫畫面
    }
    if(key=='p'){///play利用timer播放

    }
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glutSolidTeapot(0.3);
        glPushMatrix();///左
            glTranslatef(-0.3,0,0);
            glRotated(angle[0],0,0,1);
            glTranslatef(-0.3,0,0);
            glutSolidTeapot(0.3);
            glPushMatrix();
                glTranslatef(-0.3,0,0);
                glRotated(angle[0],0,0,1);
                glTranslatef(-0.3,0,0);
                glutSolidTeapot(0.3);
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();///右
        glTranslatef(+0.3,0,0);
        glRotatef(-angle[2],0,0,1);
        glTranslatef(+0.3,0,0);
        glutSolidTeapot(0.3);
            glPushMatrix();
                glTranslatef(+0.3,0,0);
                glRotatef(-angle[2],0,0,1);
                glTranslatef(+0.3,0,0);
                glutSolidTeapot(0.3);
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
}
int main( int argc, char ** argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("08160661");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutTimerFunc(0,timer,0);
    glutDisplayFunc(display);
    glutMainLoop();
}



沒有留言:

張貼留言

距地表面160 Week11

 #include "glm.h" GLMmodel* pmodel = NULL; void drawmodel(void) {     if (!pmodel) { pmodel = glmReadOBJ("data/porsche.obj...