2023年5月17日 星期三

電腦圖學紀錄 切換關節

 GLUT專案

先把freeglut丟到桌面設定好

week14-1_timer

#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotated(angle,0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void timer(int t)///timer函式
{
    glutTimerFunc(500,timer,t+1);///設下一個鬧鐘
    angle+=90;///增加90度
    glutPostRedisplay();///重畫畫面
}

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

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


一直轉90度



week14-2_timer_play

#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotated(angle,0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void timer(int t)///timer函式
{
    glutTimerFunc(500,timer,t+1);///設下一個鬧鐘
    angle+=90;///增加90度
    glutPostRedisplay();///重畫畫面
}
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);
    glutKeyboardFunc(keyboard);///用keyboard()
    ///glutTimerFunc(2000,timer,0);///設定timer函式
    glutMainLoop();
}


按一下鍵盤就會開始一直旋轉90度


#include <stdio.h>
#include <GL/glut.h>

#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * uparmR = NULL;
GLMmodel * lowarmR = NULL;
int show[4]={1,1,1,1};///用show[i]決定要不要顯示
int ID=0;///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];
    ///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; ///一開始,檔案沒有開, NULL
FILE * fin = NULL; ///要讀檔用的指標, 一開始也是 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);///week13-02-2
        if(body==NULL){
            head=glmReadOBJ("model/head.obj");
            body=glmReadOBJ("model/body.obj");
            uparmR=glmReadOBJ("model/uparmR.obj");
            lowarmR=glmReadOBJ("model/lowarmR.obj");
            ///glmUnitize(body);///week13-02-1這行之後會改
        }
        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();
            glTranslated(teapotX, teapotY, 0);

            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();
    glutSwapBuffers();
}
int oldX=0, oldY=0;///week13-03-2
void motion(int x, int y){///week13-03-2
    teapotX += (x -oldX)/150.0;///week13-03-2
    teapotY -= (y -oldY)/150.0;///week13-03-2
    oldX=x;
    oldY=y;
    printf("glTranslatef(%f, %f, 0);\n",teapotX,teapotY);
    glutPostRedisplay();///week13-03-2
}

void mouse(int button, int state, int x, int y)
{
    if(state==GLUT_DOWN){
        oldX = x;///teapotX = (x-150)/150.0;
        oldY = y;///teapotY = (150-y)/150.0;
        angle = x;
        ///printf("glTranslatef(%f, %f, 0);\n", teapotX, teapotY);
        ///if(fout==NULL) fout = fopen("file4.txt", "w"); ///step02-2 沒開檔,就開
        ///fprintf(fout, "%f %f\n", teapotX, teapotY); ///step02-2 要再存座標
    }
    display();
}
///void keyboard(unsigned char key, int x, int y) ///step02-2 keyboard函式
///{
    ///if(fin==NULL){ ///step02-2 如果檔案還沒 fopen(), 就開它
        ///fclose(fout); ///前面mouse會開fout指標, 所以要關掉
        ///fin = fopen("file4.txt", "r"); ///step02-2 沒開檔,就開
   /// }
    ///fscanf(fin, "%f %f", &teapotX, &teapotY); ///step02-2 真的讀檔
    ///display(); ///step02-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); ///step02-2 keyboard要做事囉(開檔、讀檔)

    glutMainLoop();
}



按0123分別可以顯示出不同部位



#include <stdio.h>
#include <GL/glut.h>

#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * uparmR = NULL;
GLMmodel * lowarmR = NULL;
int show[4]={0,0,1,0};///用show[i]決定要不要顯示
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];
    ///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; ///一開始,檔案沒有開, NULL
FILE * fin = NULL; ///要讀檔用的指標, 一開始也是 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);///week13-02-2
        if(body==NULL){
            head=glmReadOBJ("model/head.obj");
            body=glmReadOBJ("model/body.obj");
            uparmR=glmReadOBJ("model/uparmR.obj");
            lowarmR=glmReadOBJ("model/lowarmR.obj");
            ///glmUnitize(body);///week13-02-1這行之後會改
        }
        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();
            glTranslated(teapotX, teapotY, 0);

            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;///week13-03-2
void motion(int x, int y){///week13-03-2
    teapotX += (x -oldX)/150.0;///week13-03-2
    teapotY -= (y -oldY)/150.0;///week13-03-2
    oldX=x;
    oldY=y;
    printf("glTranslatef(%f, %f, 0);\n",teapotX,teapotY);
    glutPostRedisplay();///week13-03-2
}

void mouse(int button, int state, int x, int y)
{
    if(state==GLUT_DOWN){
        oldX = x;///teapotX = (x-150)/150.0;
        oldY = y;///teapotY = (150-y)/150.0;
        angle = x;
        ///printf("glTranslatef(%f, %f, 0);\n", teapotX, teapotY);
        ///if(fout==NULL) fout = fopen("file4.txt", "w"); ///step02-2 沒開檔,就開
        ///fprintf(fout, "%f %f\n", teapotX, teapotY); ///step02-2 要再存座標
    }
    display();
}
///void keyboard(unsigned char key, int x, int y) ///step02-2 keyboard函式
///{
    ///if(fin==NULL){ ///step02-2 如果檔案還沒 fopen(), 就開它
        ///fclose(fout); ///前面mouse會開fout指標, 所以要關掉
        ///fin = fopen("file4.txt", "r"); ///step02-2 沒開檔,就開
   /// }
    ///fscanf(fin, "%f %f", &teapotX, &teapotY); ///step02-2 真的讀檔
    ///display(); ///step02-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); ///step02-2 keyboard要做事囉(開檔、讀檔)

    glutMainLoop();
}


拖動紅色的可以整塊移動



#include <stdio.h>
#include <GL/glut.h>

#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * uparmR = NULL;
GLMmodel * lowarmR = NULL;
int show[4]={0,0,1,0};///用show[i]決定要不要顯示
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];
    ///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; ///一開始,檔案沒有開, NULL
FILE * fin = NULL; ///要讀檔用的指標, 一開始也是 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);///week13-02-2
        if(body==NULL){
            head=glmReadOBJ("model/head.obj");
            body=glmReadOBJ("model/body.obj");
            uparmR=glmReadOBJ("model/uparmR.obj");
            lowarmR=glmReadOBJ("model/lowarmR.obj");
            ///glmUnitize(body);///week13-02-1這行之後會改
        }
        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.200000, +0.453333, 0);
            glRotatef(angle, 0, 0, 1);
            ///glTranslatef(teapotX, teapotY, 0);
            glTranslatef(1.200000, -0.453333, 0);

            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;
    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;///teapotX = (x-150)/150.0;
        oldY = y;///teapotY = (150-y)/150.0;
        angle = x;
        ///printf("glTranslatef(%f, %f, 0);\n", teapotX, teapotY);
        ///if(fout==NULL) fout = fopen("file4.txt", "w"); ///step02-2 沒開檔,就開
        ///fprintf(fout, "%f %f\n", teapotX, teapotY); ///step02-2 要再存座標
    }
    display();
}
///void keyboard(unsigned char key, int x, int y) ///step02-2 keyboard函式
///{
    ///if(fin==NULL){ ///step02-2 如果檔案還沒 fopen(), 就開它
        ///fclose(fout); ///前面mouse會開fout指標, 所以要關掉
        ///fin = fopen("file4.txt", "r"); ///step02-2 沒開檔,就開
   /// }
    ///fscanf(fin, "%f %f", &teapotX, &teapotY); ///step02-2 真的讀檔
    ///display(); ///step02-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); ///step02-2 keyboard要做事囉(開檔、讀檔)

    glutMainLoop();
}



以一個點為中心旋轉






#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * uparmR = NULL;
GLMmodel * lowarmR = NULL;
int show[4]={1,1,1,1};///用show[i]決定要不要顯示
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;

    glutPostRedisplay();
}
FILE * fout = NULL; ///一開始,檔案沒有開, NULL
FILE * fin = NULL; ///要讀檔用的指標, 一開始也是 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);///week13-02-2
        if(body==NULL){
            head=glmReadOBJ("model/head.obj");
            body=glmReadOBJ("model/body.obj");
            uparmR=glmReadOBJ("model/uparmR.obj");
            lowarmR=glmReadOBJ("model/lowarmR.obj");
            ///glmUnitize(body);///week13-02-1這行之後會改
        }
        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.200000, +0.453333, 0);
            glRotatef(angle, 0, 0, 1);
            ///glTranslatef(teapotX, teapotY, 0);
            glTranslatef(1.200000, -0.453333, 0);

            if(ID==2) glColor3f(1,0,0);///選定的,設紅色
            else glColor3f(1,1,1);
            if (show[2]) glmDraw(uparmR, GLM_MATERIAL);
            glPushMatrix();
            glTranslatef(-1.946666, +0.180001, 0);
            //glTranslatef(teapotX, teapotY, 0);
            glRotatef(angle, 0, 0, 1);
            glTranslatef(1.946666, -0.180001, 0);
            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);///放小茶壺在中心點當參考
    glutSolidTeapot(0.02);///放小茶壺在中心點當參考
    glutSwapBuffers();
}
int oldX=0, oldY=0;///week13-03-2
void motion(int x, int y){
    teapotX += (x -oldX)/150.0;
    teapotY -= (y -oldY)/150.0;
    oldX=x;
    oldY=y;
    angle = x;
    printf("glTranslatef(%f, %f, 0);\n",teapotX,teapotY);
    glutPostRedisplay();

}

void mouse(int button, int state, int x, int y)
{
    if(state==GLUT_DOWN){
        teapotX = (x-150)/150.0;///teapotX = (x-150)/150.0;
        teapotY = (150-y)/150.0;///teapotY = (150-y)/150.0;
        angle = x;
        if(fout==NULL) fout = fopen("file4.txt", "w"); ///step02-2 沒開檔,就開
        fprintf(fout, "%f %f\n", teapotX, teapotY); ///step02-2 要再存座標
    }
    display();
}
///void keyboard(unsigned char key, int x, int y) ///step02-2 keyboard函式
///{
    ///if(fin==NULL){ ///step02-2 如果檔案還沒 fopen(), 就開它
        ///fclose(fout); ///前面mouse會開fout指標, 所以要關掉
        ///fin = fopen("file4.txt", "r"); ///step02-2 沒開檔,就開
   /// }
    ///fscanf(fin, "%f %f", &teapotX, &teapotY); ///step02-2 真的讀檔
    ///display(); ///step02-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); ///step02-2 keyboard要做事囉(開檔、讀檔)

    glutMainLoop();
}



整隻手臂都會一起轉



快咳死了 可惡



沒有留言:

張貼留言