2023年5月17日 星期三

sheeba - week14 - 切換關節

 Step01-1

建立GLUT專案   命名為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)///step01-1你的timer函式,做對應動作
{
    glutTimerFunc(500, timer, t+1);
    angle += 90;
    glutPostRedisplay();
}
int main(int argc, char ** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week14");

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

Step01-2

建立新GLUT專案   命名為week14-2_timer_play

使茶壺按下空白鍵就開始轉動


Step02-1

建立GLUT專案   命名為week14-3_timer_alpha_interpolation

加入alpha內插公式後   使茶壺隨著滑鼠按壓拖曳旋轉



///week14-3_timer_alpha_interpolation
#include <GL/glut.h>
float angle=0, oldAngle=0, newAngle=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)///step01-1你的timer函式,做對應動作
{
    if(t<100) glutTimerFunc(500, timer, t+1);///設定下一個鬧鐘
    float alpha = t/100.0;///step02-1 alpha介於0.00~1.00之間
    angle = newAngle * alpha + (1-alpha) * oldAngle;///step02-1 alpha內插公式
    glutPostRedisplay();///重畫畫面
}
void motion(int x,int y)///step02-1
{
    angle = x;///step02-1即時更新角度
    glutPostRedisplay();///step02-1重畫畫面
}
void mouse(int button, int state, int x, int y)///step02-1
{
    if(state==GLUT_DOWN) oldAngle = x;///step02-1按下去
    if(state==GLUT_UP) newAngle = x;///step02-1放開
    glutPostRedisplay();///step02-1重畫畫面
}
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);///step02-1按滑鼠右鍵表示起點,放開表示終點
glutMotionFunc(motion);///step02-1當mouse在motion時,即時更新畫面
glutKeyboardFunc(keyboard);///step01-2用keyboard()

glutMainLoop();
}

Step03-1

控制中心點   使關節圍繞中心點旋轉



///week12-4_keyboard_mouse要用鍵盤滑鼠操控茶壺
///存檔會在工作執行目錄working dir
///在Codeblocks Project-Properties裡面的build targets可以設定
///把它從Desktop\freeglut\bin改成.(現在目錄的意思)DEBUG和Release都要設定才會將.cbp檔存檔
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"///week13 step02-1
GLMmodel * head = NULL;///week13 step02-1
GLMmodel * body = NULL;
GLMmodel * uparmR = NULL;
GLMmodel * lowarmR = NULL;
int show[4] = {1,1,1,1};
int ID = 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;
    ///if(key=='0') show[0] = !show[0];///week13 step03-1
    ///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;///step02-2一開始檔案沒有開,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();
        ///glTranslatef(teapotX,teapotY,0);
        ///glutSolidTeapot(0.3);
        glScalef(0.2, 0.2, 0.2);///week13 step02-2
        if(body==NULL)///week13 step02-1
        {
            head = glmReadOBJ("model/head.obj");
            body = glmReadOBJ("model/body.obj");///week13 step02-1
            uparmR = glmReadOBJ("model/uparmR.obj");
            lowarmR = glmReadOBJ("model/lowarmR.obj");
            ///glmUnitize(body);///week13 step02-1
        }
        if(ID==0) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[0]) glmDraw(head,GLM_MATERIAL);///week13 step03-1

        if(ID==1) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[1]) glmDraw(body,GLM_MATERIAL);
        glPushMatrix();
            glTranslatef(-1.186666, +0.466667, 0);///week14_step03-1反過來
            glRotatef(angle,0,0,1);///week14_step03-1TRT建出來
            ///glRotatef(angle,0,0,1);
        glTranslatef(1.186666, -0.466667, 0);///week14_step03-1的結果

            if(ID==2) glColor3f(1,0,0);
            else glColor3f(1,1,1);
            if(show[2]) glmDraw(uparmR,GLM_MATERIAL);
        glPopMatrix();

        if(ID==3) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[3]) glmDraw(lowarmR,GLM_MATERIAL);
    glPopMatrix();
    glColor3f(0,1,0);
    glutSolidTeapot(0.02);
    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_step03-1
    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;
        ///if(fout==NULL) fout = fopen("file4.txt", "w");
        ///fprintf(fout, "%f %f\n", teapotX, teapotY);
    }
    display();
}
///void keyboard(unsigned char key, int x, int y)
///{
    ///if(fin==NULL)
    ///{
        ///fclose(fout);
        ///fin = fopen("file4.txt","r");
    ///}
    ///fscanf(fin, "%f %f", &teapotX, &teapotY);
    ///display();
///}
int main(int argc, char ** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week12");

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

Step03-2



///week12-4_keyboard_mouse要用鍵盤滑鼠操控茶壺
///存檔會在工作執行目錄working dir
///在Codeblocks Project-Properties裡面的build targets可以設定
///把它從Desktop\freeglut\bin改成.(現在目錄的意思)DEBUG和Release都要設定才會將.cbp檔存檔
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"///week13 step02-1
GLMmodel * head = NULL;///week13 step02-1
GLMmodel * body = NULL;
GLMmodel * uparmR = NULL;
GLMmodel * lowarmR = NULL;
int show[4] = {1,1,1,1};
int ID = 3;///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;
    ///if(key=='0') show[0] = !show[0];///week13 step03-1
    ///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;///step02-2一開始檔案沒有開,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();
        ///glTranslatef(teapotX,teapotY,0);
        ///glutSolidTeapot(0.3);
        glScalef(0.2, 0.2, 0.2);///week13 step02-2
        if(body==NULL)///week13 step02-1
        {
            head = glmReadOBJ("model/head.obj");
            body = glmReadOBJ("model/body.obj");///week13 step02-1
            uparmR = glmReadOBJ("model/uparmR.obj");
            lowarmR = glmReadOBJ("model/lowarmR.obj");
            ///glmUnitize(body);///week13 step02-1
        }
        if(ID==0) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[0]) glmDraw(head,GLM_MATERIAL);///week13 step03-1

        if(ID==1) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[1]) glmDraw(body,GLM_MATERIAL);
        glPushMatrix();
            glTranslatef(-1.186666, +0.466667, 0);///week14_step03-1反過來
            glRotatef(angle,0,0,1);///week14_step03-1TRT建出來
            ///glRotatef(angle,0,0,1);
        glTranslatef(1.186666, -0.466667, 0);///week14_step03-1的結果

            if(ID==2) glColor3f(1,0,0);
            else glColor3f(1,1,1);
            if(show[2]) glmDraw(uparmR,GLM_MATERIAL);
        glPopMatrix();

        if(ID==3) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[3]) glmDraw(lowarmR,GLM_MATERIAL);

    glPopMatrix();

    glColor3f(0,1,0);
    glutSolidTeapot(0.02);
    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_step03-1
    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;
        ///if(fout==NULL) fout = fopen("file4.txt", "w");
        ///fprintf(fout, "%f %f\n", teapotX, teapotY);
    }
    display();
}
///void keyboard(unsigned char key, int x, int y)
///{
    ///if(fin==NULL)
    ///{
        ///fclose(fout);
        ///fin = fopen("file4.txt","r");
    ///}
    ///fscanf(fin, "%f %f", &teapotX, &teapotY);
    ///display();
///}
int main(int argc, char ** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week12");

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





沒有留言:

張貼留言