2023年5月18日 星期四

m week14

 week14

step01-1

安裝git

開啟2023graphicsb

開新的GLUT專案 week14-timer
放在 2023graphicsb

freeglut選在2023graphicsb的final_project 底下的


輸入程式碼
#include <GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot( 0.3 );
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week14");

    glutDisplayFunc(display);

    glutMainLoop();
}
增加程式碼讓它旋轉
#include <GL/glut.h>
float angle = 0;
void timer(int t)
{
    glutTimerFunc(500, timer, t+1);
    angle +=90;
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle, 0, 0, 1);
        glutSolidTeapot( 0.3 );
        glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week14");

    glutTimerFunc(3000, timer, 0);
    glutDisplayFunc(display);

    glutMainLoop();
}


step01-2

開新的專案week14-2_timer_play
按任意鍵在play
複製week14-timer的程式碼
並修改程式碼
程式碼
#include <GL/glut.h>
float angle = 0;
void timer(int t)
{
    glutTimerFunc(33, timer, t+1);
    angle +=3;
    glutPostRedisplay();
}
void keyboard(unsigned char key , int x, int y)
{
    glutTimerFunc(0, timer, 0);
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle, 0, 0, 1);
        glutSolidTeapot( 0.3 );
        glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week14");

    glutKeyboardFunc(keyboard);
    ///glutTimerFunc(3000, timer, 0);
    glutDisplayFunc(display);

    glutMainLoop();
}

step02-1

打開excel
內插動作
輸入數值

先把檔案被分到雲端

step02-2

開新的專案 week14-3_timer_alpha_interpolation
alpha內插,用mouse控制,開始的位置、結束的位置,在play
複製 week14-2_timer_play 的程式碼
修改程式碼
#include <GL/glut.h>
float angle = 0, newAngle = 0, oldAngle = 0;
float oldX = 0;
void timer(int t)
{
    if(t<100) glutTimerFunc(33, timer, t+1);
    float alpha = t/100.0;
    angle = alpha*newAngle + (1-alpha)*oldAngle;
    glutPostRedisplay();
}
void keyboard(unsigned char key , int x, int y)
{
    glutTimerFunc(0, timer, 0);
}
void mouse(int button, int state, int x, int y)
{
    if(state==GLUT_DOWN) oldAngle = angle;
    if(state==GLUT_UP) newAngle = angle;
    oldX = x;
    glutPostRedisplay();
}
void motion(int x, int y)
{
    angle += x-oldX;
    oldX = x;
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle, 0, 0, 1);
        glutSolidTeapot( 0.3 );
        glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week14");

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    ///glutTimerFunc(3000, timer, 0);
   glutDisplayFunc(display);

    glutMainLoop();
}

step03-1

打開final_project
秀關節顏色
修改程式碼
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * rightarm = NULL;
GLMmodel * righthand = NULL;
int show[4] = {1,1,1,1};
int ID = 2;
float teapotX = 0, teapotY = 0;
FILE * fout = NULL;
FILE * fin = NULL;
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();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    if(head==NULL){
        head = glmReadOBJ("model/head.obj");
        body = glmReadOBJ("model/body.obj");
        rightarm = glmReadOBJ("model/rightarm.obj");
        righthand = glmReadOBJ("model/righthand.obj");
        ///glmUnitize(head);
    }
    glPushMatrix();
        glScalef(0.3, 0.3, 0.3);
        glPushMatrix();
            glTranslatef(teapotX, teapotY, 0);
            
            if(ID==0) glColor3f(1,0,0);
            else glColor3f(1,1,1);
            if (show[0]) glmDraw(head, GLM_MATERIAL);
        glPopMatrix();
        if(ID==1) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if (show[1]) glmDraw(body, GLM_MATERIAL);

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

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

    glPopMatrix();
    glutSwapBuffers();
}
int oldX=0, oldY=0;
void  mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN){
        oldX = x;
        oldY = y;
    }
}
void motion(int x, int y){
    teapotX += (x - oldX)/150.0;
    teapotY -= (y - oldY)/150.0;
    oldX = x;
    oldY = y;
    glutPostRedisplay();
}
int main(int argc,char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week13");

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

    glutMainLoop();
}

step03-2

修改程式碼
讓關節轉動

#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * rightarm = NULL;
GLMmodel * righthand = NULL;
int show[4] = {1,1,1,1};
int ID = 2;
int angle = 0;
float teapotX = 0, teapotY = 0;
FILE * fout = NULL;
FILE * fin = NULL;
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();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    if(head==NULL){
        head = glmReadOBJ("model/head.obj");
        body = glmReadOBJ("model/body.obj");
        rightarm = glmReadOBJ("model/rightarm.obj");
        righthand = glmReadOBJ("model/righthand.obj");
        ///glmUnitize(head);
    }
    glPushMatrix();
        glScalef(0.3, 0.3, 0.3);
        glPushMatrix();
            //glTranslatef(teapotX, teapotY, 0);

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

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

        glPushMatrix();
            ///glTranslatef(teapotX, teapotY, 0);
            glTranslatef(+1.300000, +0.540000, 0);
            glRotatef(angle, 0, 0, 1);
            glTranslatef(-1.300000, -0.540000, 0);

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


            glPushMatrix();
                ///glTranslatef(teapotX, teapotY, 0);
                glTranslatef(1.939999, 0.020000, 0);
                glRotatef(angle, 0, 0, 1);
                glTranslatef(-1.939999, -0.020000, 0);
                if(ID==3) glColor3f(1,0,0);
                else glColor3f(1,1,1);
                if (show[3]) glmDraw(righthand, GLM_MATERIAL);
            glPopMatrix();
        glPopMatrix();

    glPopMatrix();
    glColor3f(0,1,0);
    glutSolidTeapot( 0.02 );
    glutSwapBuffers();
}
int oldX=0, oldY=0;
void  mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN){
        oldX = x;
        oldY = y;
    }
}
void motion(int x, int y){
    teapotX += (x - oldX)/150.0*3;
    teapotY -= (y - oldY)/150.0*3;
    printf("glTranslatef(%f, %f, 0);\n", teapotX, teapotY);
    angle += x-oldX;
    oldX = x;
    oldY = y;
    glutPostRedisplay();
}
int main(int argc,char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week14");

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

    glutMainLoop();
}

擷取最後一行程式碼
作為關節的旋轉中心
然後貼上 glTranslatef
然後在第一個glTranslatef的正負號顛倒




沒有留言:

張貼留言