2023年5月18日 星期四

森 Week14 電腦圖學 計時器內插

 Week14

1.先下載git,把之前的檔案雲端下載下來,建立一個project專案,




2.新增程式碼

#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();

}

執行結果如下


3.新增程式碼

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

{

    glutTimerFunc(0, timer, 0);

}

glutKeyboardFunc(keyboard);

把這行註解掉///glutTimerFunc(3000,timer,0);

4.更改程式碼如下


#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();

}

執行結果如下:


5.把final裡面的cbp檔在codeblocks中開啟,兩個檔案算成功,更改程式碼如下

#include <stdio.h>

#include <GL/glut.h>

#include "glm.h"

GLMmodel * head = NULL;

GLMmodel * body = NULL;

GLMmodel * right_arm = NULL;

GLMmodel * left_arm = 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");

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

        left_arm = glmReadOBJ("model/uparmL.obj");

    }

    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(right_arm, GLM_MATERIAL);


        if(ID==3) glColor3f(1,0,0);

        else glColor3f(1,1,1);

        if(show[3]) glmDraw(left_arm, 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();

}

執行結果如下圖


6.修改程式碼

#include <stdio.h>

#include <GL/glut.h>

#include "glm.h"

GLMmodel * head = NULL;

GLMmodel * body = NULL;

GLMmodel * right_arm = NULL;

GLMmodel * left_arm = NULL;

int show[4] ={1,1,1,1};

int ID = 2;

float teapotX = 0, teapotY = 0;

float angle = 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");

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

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

    }

    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(-1.360000, +0.360000, 0);

            glRotatef(angle, 0, 0, 1);

            glTranslatef(1.360000, -0.360000, 0);


            if(ID==2) glColor3f(1,0,0);

            else glColor3f(1,1,1);

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


            glPushMatrix();

                ///glTranslatef(teapotX,teapotY,0);

                glTranslatef(-1.959999, +0.080000, 0);

                glRotatef(angle, 0, 0, 1);

                glTranslatef(1.959999, -0.080000, 0);


                if(ID==3) glColor3f(1,0,0);

                else glColor3f(1,1,1);

                if(show[3]) glmDraw(left_arm, 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("week13");

    glutMotionFunc(motion);

    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);


    glutMainLoop();

}

成功如下圖



7.

8.

沒有留言:

張貼留言