2021年6月9日 星期三

林裕宸電圖week16

1.開啟15週的檔案

2.原本使用timer隨時間改變讓手臂動 現在利用滑鼠的座標來讓手臂改變位置

原理是:

按下滑鼠時紀錄x座標 設定為OldX,滑鼠移動為持續更新的x座標,新的x座標減按下時的OldX座標(x-OldX),這位移量為手臂的位移。

程式碼如下

#include <stdio.h>

#include <GL/glut.h>

float angle[20]={}, diff=2;///Step05

int angleID=0; ///Step05

int oldX=0;

void timer( int t ){


}

void mouse(int button, int state, int x, int y){

    oldX = x;///當mouse按下去時記下位置

}

void motion(int x,int y){///當motion動

    angle[angleID] += x-oldX;///用x-oldx

    oldX = x;///再更新位置

    glutPostRedisplay();

}

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

    if(key=='0') angleID=0; ///Step05

    if(key=='1') angleID=1; ///Step05

    if(key=='2') angleID=2; ///Step05

    if(key=='3') angleID=3; ///Step05

}

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

        glutSolidTeapot( 0.3 );///身體

        glPushMatrix();///左半邊

            glTranslatef(-0.3,0,0);

            glRotatef(angle[0], 0,0,1);///旋轉 ///Step05

            glTranslatef(-0.3,0,0);///(1)把關節旋轉中心放到畫面中心

            glutSolidTeapot( 0.3 );///左手臂(沒有重疊)

            glPushMatrix();

                glTranslatef(-0.3,0,0);

                glRotatef(angle[1], 0,0,1);///旋轉 ///Step05

                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[3], 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("week16 file");


    glutMouseFunc(mouse);

    glutMotionFunc(motion);

    glutKeyboardFunc(keyboard);///Step05 整合很多關節,會去改angleID

    glutTimerFunc(0, timer, 0);///Step04-2 設第1個timer做動畫

    glutDisplayFunc(display);

    glutMainLoop();

}

可用數字鍵1234來決定要控制的關節



3.
開啟txt檔並儲存座標 儲存座標之後可做動畫
#include <stdio.h>
#include <GL/glut.h>
float angle[20]={}, diff=2;///Step05
int angleID=0; ///Step05
int oldX=0;
FILE * fout = NULL;
void timer( int t ){

}
void mouse(int button, int state, int x, int y){
    oldX = x;///當mouse按下去時記下位置
}
void motion(int x,int y){///當motion動
    angle[angleID] += x-oldX;///用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){ ///Step05
    if(key=='2') angleID=0; ///Step05
    if(key=='1') angleID=1; ///Step05
    if(key=='3') angleID=2; ///Step05
    if(key=='4') angleID=3; ///Step05
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glutSolidTeapot( 0.3 );///身體
        glPushMatrix();///左半邊
            glTranslatef(-0.3,0,0);
            glRotatef(angle[0], 0,0,1);///旋轉 ///Step05
            glTranslatef(-0.3,0,0);///(1)把關節旋轉中心放到畫面中心
            glutSolidTeapot( 0.3 );///左手臂(沒有重疊)
            glPushMatrix();
                glTranslatef(-0.3,0,0);
                glRotatef(angle[1], 0,0,1);///旋轉 ///Step05
                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[3], 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("week16 file");

    glutMouseFunc(mouse);///老鼠函數
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);///Step05 整合很多關節,會去改angleID
    glutTimerFunc(0, timer, 0);///Step04-2 設第1個timer做動畫
    glutDisplayFunc(display);
    glutMainLoop();
}
播放被記錄的影片
#include <stdio.h>
#include <GL/glut.h>
float angle[20]={}, diff=2;///Step05
int angleID=0; ///Step05
int oldX=0;
FILE * fout = NULL;
FILE * fin = NULL;///檔案指標fin
void timer( int t ){
    glutTimerFunc(10, timer, t+1);///設定影格 播放影片
    if( fin==NULL ) fin = fopen( "angle.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;///當mouse按下去時記下位置
}
void motion(int x,int y){///當motion動
    angle[angleID] += x-oldX;///用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){ ///Step05
    if(key=='2') angleID=0; ///Step05
    if(key=='1') angleID=1; ///Step05
    if(key=='3') angleID=2; ///Step05
    if(key=='4') angleID=3; ///Step05
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glutSolidTeapot( 0.3 );///身體
        glPushMatrix();///左半邊
            glTranslatef(-0.3,0,0);
            glRotatef(angle[0], 0,0,1);///旋轉 ///Step05
            glTranslatef(-0.3,0,0);///(1)把關節旋轉中心放到畫面中心
            glutSolidTeapot( 0.3 );///左手臂(沒有重疊)
            glPushMatrix();
                glTranslatef(-0.3,0,0);
                glRotatef(angle[1], 0,0,1);///旋轉 ///Step05
                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[3], 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("week16 file");

    glutMouseFunc(mouse);///老鼠函數
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);///Step05 整合很多關節,會去改angleID
    glutTimerFunc(0, timer, 0);///Step04-2 設第1個timer做動畫
    glutDisplayFunc(display);
    glutMainLoop();
}
紀錄動作 然後讀檔
#include <stdio.h>
#include <GL/glut.h>
float angle[20]={}, diff=2;///Step05
int angleID=0; ///Step05
int oldX=0;
FILE * fout = NULL;
FILE * fin = NULL;///檔案指標fin
void timer( int t ){
///    glutTimerFunc(10, timer, t+1);///設定影格 播放影片
///    if( fin==NULL ) fin = fopen( "angle.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;///當mouse按下去時記下位置
}
void motion(int x,int y){///當motion動
    angle[angleID] += x-oldX;///用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){ ///Step05
    if(key=='2') angleID=0; ///Step05
    if(key=='1') angleID=1; ///Step05
    if(key=='3') angleID=2; ///Step05
    if(key=='4') angleID=3; ///Step05
    if(key=='s'){
        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'){
        if( fin==NULL ) fin = fopen( "angle.txt", "r");
        for(int i=0; i<20; i++)fscanf( fin, "%f", &angle[i] );
        glutPostRedisplay();
    }
    if(key=='p'){

    }
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glutSolidTeapot( 0.3 );///身體
        glPushMatrix();///左半邊
            glTranslatef(-0.3,0,0);
            glRotatef(angle[0], 0,0,1);///旋轉 ///Step05
            glTranslatef(-0.3,0,0);///(1)把關節旋轉中心放到畫面中心
            glutSolidTeapot( 0.3 );///左手臂(沒有重疊)
            glPushMatrix();
                glTranslatef(-0.3,0,0);
                glRotatef(angle[1], 0,0,1);///旋轉 ///Step05
                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[3], 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("week16 file");

    glutMouseFunc(mouse);///老鼠函數
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);///Step05 整合很多關節,會去改angleID
    glutTimerFunc(0, timer, 0);///Step04-2 設第1個timer做動畫
    glutDisplayFunc(display);
    glutMainLoop();
}
成品
#include <stdio.h>
#include <GL/glut.h>
float angle[20]={}, diff=2;///Step05
float angleOld[20]={}, angleNew[20];
int angleID=0; ///Step05
int oldX=0;
FILE * fout = NULL;
FILE * fin = NULL;///檔案指標fin
void timer( int t ){
    glutTimerFunc(30, timer, t+1);///設定影格 播放影片
    if( t%30==0 ){
        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%300)/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;///當mouse按下去時記下位置
}
void motion(int x,int y){///當motion動
    angle[angleID] += x-oldX;///用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){ ///Step05
    if(key=='2') angleID=0; ///Step05
    if(key=='1') angleID=1; ///Step05
    if(key=='3') angleID=2; ///Step05
    if(key=='4') angleID=3; ///Step05
    if(key=='s'){
        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'){
        if( fin==NULL ) fin = fopen( "angle.txt", "r");
        for(int i=0; i<20; i++)fscanf( fin, "%f", &angle[i] );
        glutPostRedisplay();
    }
    if(key=='p'){

    }
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glutSolidTeapot( 0.3 );///身體
        glPushMatrix();///左半邊
            glTranslatef(-0.3,0,0);
            glRotatef(angle[0], 0,0,1);///旋轉 ///Step05
            glTranslatef(-0.3,0,0);///(1)把關節旋轉中心放到畫面中心
            glutSolidTeapot( 0.3 );///左手臂(沒有重疊)
            glPushMatrix();
                glTranslatef(-0.3,0,0);
                glRotatef(angle[1], 0,0,1);///旋轉 ///Step05
                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[3], 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("week16 file");

    glutMouseFunc(mouse);///老鼠函數
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);///Step05 整合很多關節,會去改angleID
    glutTimerFunc(0, timer, 0);///Step04-2 設第1個timer做動畫
    glutDisplayFunc(display);
    glutMainLoop();
}






沒有留言:

張貼留言

距地表面160 Week11

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