2019年5月23日 星期四

06160123_Week14電腦圖學

🔺電腦圖學2019-05-23 Week14


(1)主題:計時器Timer
    glutTimerFunc( timer, 1/30, 口 );

(2)主題:內插公式(在每個時間點算出一個值)

(3)主題:動畫

(4)期末作品

用程式碼做補間動畫

#GLUT

1、Download Freeglut package

2、將檔案解壓縮後複製一個libfreeglut.a檔並重新命名為libglut32.a

3、開啟Code Blocks>新增一個新的專案>點選Glut project

4、命名專案>選擇資料存放區(C:\Users\user\Downloads\freeglut\lib)>Finish

5、於左側project開啟程式碼>執行














刪除全部程式碼

到小葉老師的網站 http://120.125.89.81/ 
download以下檔案

(1) 貼上程式碼 畫出一個白茶壺

#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);
    glutInitWindowSize(500,500);///變大了
    glutCreateWindow("week 14 timer");

    glutDisplayFunc(display);
    glutMainLoop();
}


(2) downloadstep00-2.cpp 

加入一條播放聲音的程式碼
PlaySoundA("bazooka.wav", NULL, SND_ASYNC);
使茶壺跳出時,能聽到槍聲的音效

#include <GL/glut.h>
#include <mmsystem.h>///Now (1)
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glutSolidTeapot( 0.3 );

    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    PlaySoundA("bazooka.wav", NULL, SND_ASYNC);///Now (2)
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(500,500);///變大了
    glutCreateWindow("week 14 timer");

    glutDisplayFunc(display);
    
    glutMainLoop();
}



(3) downloadstep00-3.cpp

加入時間計時器
glutTimerFunc(3000, timer, 0);
使程式計算時間,3秒播放一次槍聲音效

#include <GL/glut.h>
#include <mmsystem.h>///Now (1)
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot( 0.3 );
    glutSwapBuffers();
}
void timer(int t)///NOW timer!!!!
{
    PlaySoundA("bazooka.wav", NULL, SND_ASYNC);///Now (2) ///NOW timer!!!!
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(500,500);///變大了
    glutCreateWindow("week 14 timer");

    glutDisplayFunc(display);
    glutTimerFunc(3000, timer, 0);///NOW timer!!!!

    glutMainLoop();
}


(4) downloadstep00-4.cpp

加入時間計時器,並印出秒數
秒數會配合槍聲音效
頭3秒後播放第一次,之後每秒就播放一次

glutTimerFunc(1000, timer, t+1 );/
printf("timer(t) 的 t是 %d\n", t);


#include <GL/glut.h>
#include <mmsystem.h>///Now (1)
#include <stdio.h>///printf()
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot( 0.3 );
    glutSwapBuffers();
}
void timer(int t)///NOW timer!!!!
{
    glutTimerFunc(1000, timer, t+1 );///NOW timer!!!! 起床要播下一個鬧鐘
    printf("timer(t) 的 t是 %d\n", t);///順便印一下現在的t是多少
    PlaySoundA("bazooka.wav", NULL, SND_ASYNC);///Now (2) ///NOW timer!!!!
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(500,500);///變大了
    glutCreateWindow("week 14 timer");

    glutDisplayFunc(display);
    glutTimerFunc(3000, timer, 0);///NOW timer!!!!

    glutMainLoop();
}


/
(5) download step03.cpp

利用內插法的原理,計算A至B點每秒移動的差距
即可做出由A移動至B點的茶壺

#include <GL/glut.h>
#include <mmsystem.h>///Now (1)
float angle=0;
float nowX=0;///NOW2
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        ///glRotatef(angle, 0,0,1);
        glTranslatef(nowX, 0, 0);///NOW2
        glutSolidTeapot( 0.3 );
    glPopMatrix();
    glutSwapBuffers();
}
void timer(int t)///NOW timer!!!!
{
    /// 1000 ms = 1 sec,  30 ms = 1/33 sec , 33 fps
    glutTimerFunc(30, timer, t+1 );///NOW timer!!!! 起床要播下一個鬧鐘
    float alpha = (t%33) / 33.0;///NOW2 alpha 內插
    nowX = alpha* 1 + (1-alpha)* -1 ;///NOW2

    angle += 1;
    glutPostRedisplay();///系統重繪畫面
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(500,500);///變大了
    glutCreateWindow("week 14 timer");

    glutDisplayFunc(display);
    glutTimerFunc(1000, timer, 0);///NOW timer!!!!

    glutMainLoop();
}



※用Excel做內插法
Ans = alpha * future + (1-alpha) * old)


(6) download 同學的機器人06160451_before.zip
這是不會存檔的程式碼

貼上程式碼:

#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel21 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel31 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel41 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel51 = NULL;

const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

float angle[20];
int angleID=1;

void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    ///身體
    glPushMatrix();
        glTranslatef(0, 0.3,0 );
        glScalef(0.5,0.5,0.5);

            if (!pmodel) {
                pmodel = glmReadOBJ("data/06160451.obj");
            if (!pmodel) exit(0);
                glmUnitize(pmodel);
                glmFacetNormals(pmodel);
                glmVertexNormals(pmodel, 90.0);
            }
        glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
        ///右上臂
        glPushMatrix();
            glTranslatef(0.45,0.3,0 );
            glScalef(0.5,0.5,0.5);
            glRotatef(angle[1], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);

            glPushMatrix();
            glTranslatef(0,-1.1,0 );
            glScalef(1,1,1);
            glRotatef(angle[2], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();
            glTranslatef(-0.45,0.3,0 );
            glScalef(0.5,0.5,0.5);
            glRotatef(angle[3], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);

            glPushMatrix();
            glTranslatef(0,-1.1,0 );
            glScalef(1,1,1);
            glRotatef(angle[4], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
        glPopMatrix();

        glPushMatrix();
            glTranslatef(0.2,-1.05,0 );
            glScalef(0.5,0.5,0.5);
            glRotatef(angle[5], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);


            glPushMatrix();
                glTranslatef(0,-1.2,0 );
                glScalef(1,1,1);
                glRotatef(angle[6], 0,0,1);
                glTranslatef(0,-0.7, 0);
                if (!pmodel2) {
                    pmodel2 = glmReadOBJ("data/123.obj");
                    if (!pmodel2) exit(0);
                    glmUnitize(pmodel2);
                    glmFacetNormals(pmodel2);
                    glmVertexNormals(pmodel2, 90.0);
                }
                glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
            glPopMatrix();

        glPushMatrix();
            glTranslatef(-0.2,-1.05,0 );
            glScalef(0.5,0.5,0.5);
            glRotatef(angle[7], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);


            glPushMatrix();
                glTranslatef(0,-1.2,0 );
                glScalef(1,1,1);
                glRotatef(angle[8], 0,0,1);
                glTranslatef(0,-0.7, 0);
                if (!pmodel2) {
                    pmodel2 = glmReadOBJ("data/123.obj");
                    if (!pmodel2) exit(0);
                    glmUnitize(pmodel2);
                    glmFacetNormals(pmodel2);
                    glmVertexNormals(pmodel2, 90.0);
                }
                glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
            glPopMatrix();

    glPopMatrix();

    glutSwapBuffers();
}
int oldX=0;
void mouse(int button, int state, int x, int y)///NOW2
{
    oldX = x;
}
#include <stdio.h>
void motion(int x, int y)
{
    angle[angleID] += x-oldX;
    oldX = x;
    for(int i=0;i<20;i++){
    printf(" %.2f ", angle[i]);
    }
    printf("\n");
    display();
}


void keyboard(unsigned char key, int x, int y)
{
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
    if(key=='4') angleID=4;
    if(key=='5') angleID=5;
    if(key=='6') angleID=6;
    if(key=='7') angleID=7;
    if(key=='8') angleID=8;
}

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

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glClearColor(1,1,1,1);
    glCullFace(GL_BACK);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
    glutKeyboardFunc(keyboard);///NOW3
    glutMouseFunc(mouse);///NOW2
    glutMotionFunc(motion);///NOW2
    glutDisplayFunc(display);

    glutMainLoop();
}



(7) download 同學的機器人06160451_after.zip
這是會存檔的程式碼

貼上程式碼:

#include <stdio.h>///NOW for fprintf()
#include <GL/glut.h>
#include "glm.h"

FILE * fout = NULL;///NOW for fprintf()
FILE * fin = NULL;///NOW for fscanf()

GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel21 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel31 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel41 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel51 = NULL;

const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

float angle[20];
int angleID=1;

void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    ///身體
    glPushMatrix();
        glTranslatef(0, 0.3,0 );
        glScalef(0.5,0.5,0.5);

            if (!pmodel) {
                pmodel = glmReadOBJ("data/06160451.obj");
            if (!pmodel) exit(0);
                glmUnitize(pmodel);
                glmFacetNormals(pmodel);
                glmVertexNormals(pmodel, 90.0);
            }
        glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
        ///右上臂
        glPushMatrix();
            glTranslatef(0.45,0.3,0 );
            glScalef(0.5,0.5,0.5);
            glRotatef(angle[1], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);

            glPushMatrix();
            glTranslatef(0,-1.1,0 );
            glScalef(1,1,1);
            glRotatef(angle[2], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();
            glTranslatef(-0.45,0.3,0 );
            glScalef(0.5,0.5,0.5);
            glRotatef(angle[3], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);

            glPushMatrix();
            glTranslatef(0,-1.1,0 );
            glScalef(1,1,1);
            glRotatef(angle[4], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
        glPopMatrix();

        glPushMatrix();
            glTranslatef(0.2,-1.05,0 );
            glScalef(0.5,0.5,0.5);
            glRotatef(angle[5], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);


            glPushMatrix();
                glTranslatef(0,-1.2,0 );
                glScalef(1,1,1);
                glRotatef(angle[6], 0,0,1);
                glTranslatef(0,-0.7, 0);
                if (!pmodel2) {
                    pmodel2 = glmReadOBJ("data/123.obj");
                    if (!pmodel2) exit(0);
                    glmUnitize(pmodel2);
                    glmFacetNormals(pmodel2);
                    glmVertexNormals(pmodel2, 90.0);
                }
                glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
            glPopMatrix();

        glPushMatrix();
            glTranslatef(-0.2,-1.05,0 );
            glScalef(0.5,0.5,0.5);
            glRotatef(angle[7], 0,0,1);
            glTranslatef(0,-0.7, 0);
            if (!pmodel2) {
                pmodel2 = glmReadOBJ("data/123.obj");
            if (!pmodel2) exit(0);
                glmUnitize(pmodel2);
                glmFacetNormals(pmodel2);
                glmVertexNormals(pmodel2, 90.0);
            }
            glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);


            glPushMatrix();
                glTranslatef(0,-1.2,0 );
                glScalef(1,1,1);
                glRotatef(angle[8], 0,0,1);
                glTranslatef(0,-0.7, 0);
                if (!pmodel2) {
                    pmodel2 = glmReadOBJ("data/123.obj");
                    if (!pmodel2) exit(0);
                    glmUnitize(pmodel2);
                    glmFacetNormals(pmodel2);
                    glmVertexNormals(pmodel2, 90.0);
                }
                glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
            glPopMatrix();

    glPopMatrix();

    glutSwapBuffers();
}
int oldX=0;
void mouse(int button, int state, int x, int y)///NOW2
{
    oldX = x;
}
#include <stdio.h>
void motion(int x, int y)
{
    angle[angleID] += x-oldX;
    oldX = x;
    for(int i=0;i<20;i++){
    printf(" %.2f ", angle[i]);
    }
    printf("\n");
    display();
    ///motion裡面,就不要再存檔了.... (第13週一直存,但現在不要存,才不會亂亂的)
}


void keyboard(unsigned char key, int x, int y)
{
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
    if(key=='4') angleID=4;
    if(key=='5') angleID=5;
    if(key=='6') angleID=6;
    if(key=='7') angleID=7;
    if(key=='8') angleID=8;
    if(key=='w'){///只有按下'w'才存1組動作 (1行)
        if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW
        for(int i=0; i<20; i++){///NOW
            fprintf(fout, " %.1f ", angle[i]);///NOW
        }///NOW
        fprintf(fout, "\n");///NOW
    }///NOW
    if(key=='r'){///NOW2
        if(fin==NULL) fin=fopen("motion.txt", "r");///NOW2
        for(int i=0; i<20; i++){///NOW2
            fscanf(fin, "%f", &angle[i]);///NOW2
        }///NOW2
    }
    glutPostRedisplay();///NOW2 重畫畫面
}

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

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glClearColor(1,1,1,1);
    glCullFace(GL_BACK);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
    glutKeyboardFunc(keyboard);///NOW3
    glutMouseFunc(mouse);///NOW2
    glutMotionFunc(motion);///NOW2
    glutDisplayFunc(display);

    glutMainLoop();

}

沒有留言:

張貼留言