2023年5月17日 星期三

ww-week14-切換關節-切換移動、旋轉-計時器、內插動作

1.計時器、內插動作

1-1.專案1: 
先把freeglut再裝一次,小葉老師上課軟體 / freeglut檔案複製到桌面 / 將libfreeglut.a複製貼上後改成libglut32.a/codeblocks開啟新專案:week14-1_timer

#include <GL/glut.h>
float angle =0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
glutSwapBuffers();

}
void timer(int t)///1-1 你的timer函式,做對應動作
{
    glutTimerFunc(500,timer,t+1);///1-1
    angle += 90;///1-1增加90度
    glutPostRedisplay();///1-1重畫畫面
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week14");

glutDisplayFunc(display);
glutTimerFunc(2000,timer,0);///1-1設定timer函式
    glutMainLoop();
}

1-2.專案2: 

開新專案:week14-2_timer_play/用keyboard()函式來控制開始

#include <GL/glut.h>
float angle =0;
void display() 
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
glutSwapBuffers();
}
void timer(int t)
{
    glutTimerFunc(500,timer,t+1);
    angle += 90;///1-1增加90度
    glutPostRedisplay();
}
void keyboard(unsigned char key ,int x, int y)///1-2按下任何鍵開始播放
{
    glutTimerFunc(0,timer,0);
}

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week14");

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);///1-2用keyboard()
///glutTimerFunc(2000,timer,0);
    glutMainLoop();
}

2.切換移動、旋轉

2-1專案3:用excel畫出下圖表格/等等用程式寫


開新專案:week14-3_timer_alpha_interpolation/ 劃出動畫,按下去表示起點,任意鍵開始play/備份 

※裡面增加mouse()來處理按下去、放開來的對應動作;motion()負責即時更新角度;timer()裡用100格的內插,所以t小於100時,持續設定下一次timer,等待的時間不要太久;用alpha來算角度的值

#include <GL/glut.h>

float angle =0,oldAngle=0,newAngle=0;///2-1

void display(){

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

        glRotatef(angle,0,0,1);

        glutSolidTeapot(0.3);

    glPopMatrix();

    glutSwapBuffers();

}

void timer(int t){

    if(t<100)glutTimerFunc(50,timer,t+1);///2-1在100之內,設定下一個鬧鐘

    float alpha =t/100.0;///2-1 alpha介於0.00~1.00之間

    angle = newAngle*alpha + (1-alpha)*oldAngle;///2-1內插公式

    glutPostRedisplay();

}

void motion(int x,int y)///2-1 

{

    angle = x;///即時更新角度

    glutPostRedisplay();///重畫畫面

}

void mouse(int botton,int state,int x,int y)///2-1 

{

    if(state==GLUT_DOWN) oldAngle=x;///按下去

    if(state==GLUT_UP) newAngle=x;///放開來

}

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

    glutTimerFunc(0,timer,0);

}

int main(int argc, char *argv[]){

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

glutCreateWindow("week14");

glutDisplayFunc(display);

glutMouseFunc(mouse);///2-1按下去,表示起點

glutMotionFunc(motion);///2-1當mouse在motion時,即時更新畫面

glutKeyboardFunc(keyboard);

    glutMainLoop();

}

3.切換關節

3-1.裝git,clone下我們的專案2023graphicsa

在codeblocks 開啟2023Grahhicsa 的Final_Project / file / open 開啟.cbp檔/加一個ID用來選擇部位選到的變紅色/save everything備份


#include <stdio.h>

#include <GL/glut.h>

#include "glm.h"

GLMmodel * head =NULL;

GLMmodel * uparmR =NULL;

GLMmodel * lowarmR =NULL;

GLMmodel * body =NULL;

int show[4]={1,1,1,1};///week14 3-1

int ID = 0;///week14 3-1 //0頭 1身體 2上手臂 3下手臂

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

    if(key=='0') ID = 0;///week14 3-1

    if(key=='1') ID = 1;///week14 3-1

    if(key=='2') ID = 2;///week14 3-1

    if(key=='3') ID = 3;///week14 3-1

    ///if(key=='0') show[0] = !show[0];

    ///if(key=='1') show[1] = !show[1];

    ///if(key=='2') show[2] = !show[2];

    ///if(key=='3') show[3] = !show[3];

    glutPostRedisplay();

}

FILE * fout=NULL;

FILE * fin =NULL;

float teapotX=0,teapotY=0;

float angle=0,angle2=0,angle3=0;

void display(){

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

        glScalef(0.2,0.2,0.2);

        if(body==NULL){

            head = glmReadOBJ("model/head.obj");

            body = glmReadOBJ("model/body.obj");

            uparmR = glmReadOBJ("model/uparmR.obj");

            lowarmR = glmReadOBJ("model/lowarmR.obj");

        }

        if(ID==0)glColor3f(1,0,0);///week14 3-1

        else glColor3f(1,1,1);///week14 3-1

        if(show[0]) glmDraw(body, GLM_MATERIAL);

        if(ID==1)glColor3f(1,0,0);///week14 3-1

        else glColor3f(1,1,1);///week14 3-1

        if(show[1]) glmDraw(head, GLM_MATERIAL);

        glPushMatrix();

            glTranslatef(teapotX,teapotY,0);

            if(ID==2)glColor3f(1,0,0);///week14 3-1

            else glColor3f(1,1,1);///week14 3-1

            if(show[2]) glmDraw(uparmR, GLM_MATERIAL);

        glPopMatrix();

        if(ID==3)glColor3f(1,0,0);///week14 3-1

        else glColor3f(1,1,1);///week14 3-1

        if(show[3]) glmDraw(lowarmR, GLM_MATERIAL);

    glPopMatrix();

    glutSwapBuffers();

}

int oldX=0,oldY=0;

void motion (int x,int y){

    teapotX += (x-oldX)/150.0;

    teapotY-= (y-oldY)/150.0;

    oldX = x;

    oldY = y;

    printf("glTranslatef(%f,%f,0);\n",teapotX,teapotY);

    glutPostRedisplay();

}

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

    if(state==GLUT_DOWN){

        oldX = x;///week13-4teapotX = (x-150)/150.0;

        oldY = y;///week13-4teapotY = (150-y)/150.0;

        angle =x;

        ///if(fout==NULL)fout =fopen("file4.txt","w");week12

        ///fprintf(fout,"%f %f\n",teapotX,teapotY);

    }

    display();

}

int main(int argc, char *argv[]){

glutInit(&argc, argv);

glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH);

glutCreateWindow("week14");

glutDisplayFunc(display);

glutMouseFunc(mouse);

glutMotionFunc(motion);

glutKeyboardFunc(keyboard);

        glutMainLoop();

}

3-2切換移動、旋轉:建立一個Teapot當中心點/
先移動到中心點/
複製移動座標,貼回程式碼,再加入TRT讓上手臂可以旋轉並掛回去身體/
/開啟下手臂的顯示,移動到中心點後複製座標,並且加入TRT(外層上手臂的旋轉可以先關閉)
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head =NULL;
GLMmodel * uparmR =NULL;
GLMmodel * lowarmR =NULL;
GLMmodel * body =NULL;
int show[4]={1,1,1,1};///week14 3-2
int ID = 3;///week14 3-2 //選擇機關:0頭 1身體 2上手臂 3下手臂
void keyboard(unsigned char key,int x, int y){
    if(key=='0') ID = 0;
    if(key=='1') ID = 1;
    if(key=='2') ID = 2;
    if(key=='3') ID = 3;
    glutPostRedisplay();
}
FILE * fout=NULL;
FILE * fin =NULL;
float teapotX=0,teapotY=0;
float angle=0,angle2=0,angle3=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glScalef(0.2,0.2,0.2);
        if(body==NULL){
            head = glmReadOBJ("model/head.obj");
            body = glmReadOBJ("model/body.obj");
            uparmR = glmReadOBJ("model/uparmR.obj");
            lowarmR = glmReadOBJ("model/lowarmR.obj");
        }
        if(ID==0)glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[0]) glmDraw(head, GLM_MATERIAL);
        if(ID==1)glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[1]) glmDraw(body, GLM_MATERIAL);

        glPushMatrix();
            glTranslatef(-1.200356,+0.466667,0);///week14-3-2
            //glTranslatef(teapotX,teapotY,0);用來找移動值 
            glRotatef(angle,0,0,1);///week14-3-2 TRT建出來
            glTranslatef(1.200356,-0.466667,0);///week14-3-2
            if(ID==2)glColor3f(1,0,0);
            else glColor3f(1,1,1);
            if(show[2]) glmDraw(uparmR, GLM_MATERIAL);

            glPushMatrix();
                glTranslatef(-1.946666,0.126667,0);///week14-3-3
                glRotatef(angle,0,0,1);///week14-3-2 TRT建出來
                glTranslatef(1.946666,-0.126667,0);///week14-3-2
                if(ID==3)glColor3f(1,0,0);
                else glColor3f(1,1,1);
                if(show[3]) glmDraw(lowarmR, GLM_MATERIAL);
            glPopMatrix();
        glPopMatrix();


    glPopMatrix();

    glColor3f(0,1,0);///week14-3-2放個小茶壺在中心當參考點
    glutSolidTeapot(0.02);///week14-3-2
    glutSwapBuffers();
}
int oldX=0,oldY=0;
void motion (int x,int y){
    teapotX += (x-oldX)/150.0;
    teapotY-= (y-oldY)/150.0;
    oldX = x;
    oldY = y;
    angle= x;///week14-3-2旋轉用
    printf("glTranslatef(%f,%f,0);\n",teapotX,teapotY);
    glutPostRedisplay();
}
void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN){
        oldX = x;
        oldY = y;
        angle =x;
    }
    display();
}
//void keyboard(unsigned char key ,int x,int y)///2keyboard函式
//{
//    if(fin==NULL){///2如果檔案還沒fopen(),就開他
//        fclose(fout);///2前面的mouse會開fout 指標,所以要關掉
//        fin = fopen("file4.txt","r");///2開檔
//    }
//    fscanf(fin,"%f %f",&teapotX,&teapotY);///2讀檔
//    display();///2重畫畫面
//}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week14");

glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
    glutMainLoop();
}


git指令
cd desktop
git clone https://github.com/你的帳號/2023graphicsa
cd 2023graphicsa
加入檔案
git add . 
git status

git config --global user.email "jsyeh@mail.mcu.edu.tw"

git config --global user.name "jsyeh"

git commit -m "week10"

git push


沒有留言:

張貼留言