顯示具有 Week14 標籤的文章。 顯示所有文章
顯示具有 Week14 標籤的文章。 顯示所有文章

2019年6月10日 星期一

喬巴的補交作業

計時器Timer



#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("fart.wav", NULL, SND_ASYNC);///Now (2) ///NOW timer!!!! } ➜去wav檔案下載尋找檔案 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(); }


內插公式

























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


動畫



2019年5月24日 星期五

World.Execute(Me); Week14

TIMER
1.
2.
3.
4.
5.
6.
7.
8.
#include <GL/glut.h>
#include "glm.h"
#include <stdio.h>
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel21 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel31 = NULL;
FILE * fout=NULL;///(1) ///NOW4
FILE * fin =NULL;///(1) ///NOW5


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 light2_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light2_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light2_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light2_position[] = { 0.0f, 0.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];
float oldAngle[20];///NOW4
float newAngle[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);                         //備份矩陣
        glRotatef(1, 1,1,1);///NOW2 ///(2)旋轉關節1
        if (!pmodel) {                              ///now要有3D Model
        pmodel = glmReadOBJ("data/body.obj");    ///now要有3D Model
        if (!pmodel) exit(0);                       ///now要有3D Model
        glmUnitize(pmodel);                         ///now要有3D Model
        glmFacetNormals(pmodel);                    ///now要有3D Model
        glmVertexNormals(pmodel, 90.0);             ///now要有3D Model
        }                                           ///now要有3D Model
        glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL); ///now要有3D Model
    glPopMatrix();                          //還原矩陣

    glPushMatrix();
        glTranslatef(-0.35,0.38,0 );
        glScalef(0.25,0.25,0.25);                         //備份矩陣
        glRotatef(angle[1],1,0,0);///NOW2 ///(2)旋轉關節1
        glTranslatef(0.2, -0.5,0 );
        if (!pmodel2) {                              ///now要有3D Model
        pmodel2 = glmReadOBJ("data/lefthand.obj");    ///now要有3D Model
        if (!pmodel2) exit(0);                       ///now要有3D Model
        glmUnitize(pmodel2);                         ///now要有3D Model
        glmFacetNormals(pmodel2);                    ///now要有3D Model
        glmVertexNormals(pmodel2, 90.0);             ///now要有3D Model
        }                                           ///now要有3D Model
        glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL); ///now要有3D Model
    glPopMatrix();

    glPushMatrix();
        glTranslatef(0.35,0.38,0 );
        glScalef(0.25,0.25,0.25);                         //備份矩陣
        glRotatef(angle[2], 1,0,0);///NOW2 ///(2)旋轉關節1
        glTranslatef(-0.2, -0.5,0 );
        if (!pmodel21) {                              ///now要有3D Model
        pmodel21 = glmReadOBJ("data/righthand.obj");    ///now要有3D Model
        if (!pmodel21) exit(0);                       ///now要有3D Model
        glmUnitize(pmodel21);                         ///now要有3D Model
        glmFacetNormals(pmodel21);                    ///now要有3D Model
        glmVertexNormals(pmodel21, 90.0);             ///now要有3D Model
        }                                           ///now要有3D Model
        glmDraw(pmodel21, GLM_SMOOTH | GLM_MATERIAL); ///now要有3D Model
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.15, -0.1,0 );
        glScalef(0.7,0.7,0.7);                         //備份矩陣
        glRotatef(angle[3], 1,0,0);///NOW2 ///(2)旋轉關節1
        glTranslatef(0, -0.25,0 );
        if (!pmodel3) {                              ///now要有3D Model
        pmodel3 = glmReadOBJ("data/leftleg.obj");    ///now要有3D Model
        if (!pmodel3) exit(0);                       ///now要有3D Model
        glmUnitize(pmodel3);                         ///now要有3D Model
        glmFacetNormals(pmodel3);                    ///now要有3D Model
        glmVertexNormals(pmodel3, 90.0);             ///now要有3D Model
        }                                           ///now要有3D Model
        glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL); ///now要有3D Model
    glPopMatrix();

    glPushMatrix();
        glTranslatef(0.15, -0.1,0 );
        glScalef(0.7  ,0.7,0.7);                         //備份矩陣
        glRotatef(angle[4], 1,0,0);///NOW2 ///(2)旋轉關節1
        glTranslatef(0, -0.25,0 );
        if (!pmodel31) {                              ///now要有3D Model
        pmodel31 = glmReadOBJ("data/rightleg.obj");    ///now要有3D Model
        if (!pmodel31) exit(0);                       ///now要有3D Model
        glmUnitize(pmodel31);                         ///now要有3D Model
        glmFacetNormals(pmodel31);                    ///now要有3D Model
        glmVertexNormals(pmodel31, 90.0);             ///now要有3D Model
        }                                           ///now要有3D Model
        glmDraw(pmodel31, GLM_SMOOTH | GLM_MATERIAL); ///now要有3D Model
    glPopMatrix();

    glutSwapBuffers();
}

int oldX;///NOW
void mouse(int button, int state, int x, int y)///NOW
{
    oldX = x;///NOW
}
void motion(int x, int y)///NOW
{
    angle[angleID] += (x-oldX);///NOW ///NOW3
    oldX = x;///NOW

    for(int i=0; i<20; i++){///NOW4
        printf( "%.1f ", angle[i]);///NOW4
    }
    printf("\n");///NOW4
    display();///NOW
}

void timer(int t)///NOW3
{
    glutTimerFunc(33, timer, t+1);///NOW3 ///NOW4 快一點
    if(t%30==0){///NOW4 每 30frame要讀新資料
        if(fin==NULL) fin=fopen("motion.txt", "r");///NOW3
        for(int i=0; i<20; i++){///NOW3
            oldAngle[i] = newAngle[i]; ///NOW4
            fscanf(fin, "%f", &newAngle[i]);///NOW4 新資料
        }
    }
    float alpha = (t%30)/30.0;///NOW4 今天第1節有寫過它,內插
    for(int i=0;i<20;i++){///NOW4
        angle[i] = alpha*newAngle[i] + (1-alpha)*oldAngle[i];///NOW4
    }
    glutPostRedisplay();///NOW4
}

void keyboard(unsigned char key, int x, int y)///NOW3
{
    if(key=='1') angleID=1;///NOW3
    if(key=='2') angleID=2;///NOW3
    if(key=='3') angleID=3;///NOW3
    if(key=='4') angleID=4;///NOW3
    if(key=='w' || key=='W'){///NOW4 小寫的 w
        if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW4
        for(int i=0; i<20; i++){///NOW4
            fprintf( fout, "%.1f ", angle[i]);///NOW4
        }
        printf("\n");///NOW4
        fprintf(fout, "\n");///NOW4
    }
    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
        }
    }
    if(key=='t' || key=='p'){///NOW3 ///啟動timer 或是 play
        glutTimerFunc(33, timer, 0);///NOW3 ///NOW4 變快了
        if(fin==NULL) fin=fopen("motion.txt", "r");///NOW3
        for(int i=0; i<20; i++){///NOW3
            fscanf(fin, "%f", &newAngle[i]);///NOW3 ///NOW4 新角度
        }
    }
    glutPostRedisplay();///和 display()很像,但更好 ///NOW5
}

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

    glutDisplayFunc(display);
    glutIdleFunc(display);
///OpenGL設定
    glClearColor(1,1,1,1);
    //glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glutKeyboardFunc(keyboard);///NOW3
    glutMouseFunc(mouse);///NOW
    glutMotionFunc(motion);

    glEnable(GL_LIGHT0);
    /*glEnable(GL_LIGHT1);*/
    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);

   /* glLightfv(GL_LIGHT1, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT1, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT1, 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);

    glutMainLoop();
}

Tokoyami_world.exe_week14

我們來計時吧(唐突)(?)

總而言之,今天的主題是「計時器(Timer)」!一個相當重要的東西。總之我們先開始吧~~

首先我們要下載fr......


接著我們開始來做吧~~

首先,這次的程式碼如下......


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

    glutSwapBuffers();
}
void timer(int t)///NOW (2)
{
    glutTimerFunc(1000, timer, 0);
    PlaySoundA("Illuminati Confirmed.wav", NULL, SND_ASYNC);
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(900,900);
    glutCreateWindow("week14 timer");

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

    glutMainLoop();
}

來播播看吧~~

真是奇妙的音樂......?

*Illuminati Confirmed Sound*


反正成功了,阿哈哈......我們繼續吧~~

接著我們讓它能夠一直重複地讓茶壺動作吧

這次的程式碼如下......


#include <GL/glut.h>
#include <mmsystem.h>///NOW (0)發聲音
#include <stdio.h>///NOW3 for printf()
float angle=0;///NOW4
float nowX=0;///NOW5
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///NOW4
        ///glRotatef(angle, 0, 0, 1);///NOW4
        glTranslatef(nowX, 0, 0);///NOW5
        glutSolidTeapot( 0.3 );///NOW4
    glPopMatrix();///NOW4
    glutSwapBuffers();
}
void timer(int t)///NOW (2)
{/// 1000 ms = 1秒,   33ms = 0.033秒 => 33*30=1000,  30fps
    glutTimerFunc(33, timer, t+1);///NOW4 ///NOW3 一起床,馬上撥下一個鬧鐘, 時間才會準
    printf("現在的 timer(t)的t是: %d\n", t);///NOW3
    float alpha = (t%30) / 30.0;///變成 alpha: 0.0 ~ 1.0  ///NOW5
    nowX = alpha*1.0 + (1-alpha)* -1.0;///NOW5

    glutPostRedisplay();///NOW4 要更新畫面
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(500,500);///大一點
    glutCreateWindow("week14 timer");

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

    glutMainLoop();
}

來播播看吧


可以播放!而且計時器也確實有在運作!

接著就來更進一步來嘗試吧

這是最後的結果......

#include <stdio.h>///NOW1 for fprintf()
FILE * fout=NULL;///NOW1 for fopen() + fprintf()
FILE * fin =NULL;///NOW2 for fopen() + fscanf()
#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];///NOW1
float oldAngle[20];///NOW4
float newAngle[20];///NOW4
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 timer(int t)///NOW3
{
    glutTimerFunc(33, timer, t+1);///NOW3 ///NOW4 快一點
    if(t%30==0){///NOW4 每 30frame要讀新資料
        if(fin==NULL) fin=fopen("motion.txt", "r");///NOW3
        for(int i=0; i<20; i++){///NOW3
            oldAngle[i] = newAngle[i]; ///NOW4
            fscanf(fin, "%f", &newAngle[i]);///NOW4 新資料
        }
    }
    float alpha = (t%30)/30.0;///NOW4 今天第1節有寫過它,內插
    for(int i=0;i<20;i++){///NOW4
        angle[i] = alpha*newAngle[i] + (1-alpha)*oldAngle[i];///NOW4
    }
    glutPostRedisplay();///NOW4
}

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'){///NOW1
        if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW1
        for(int i=0; i<20; i++){///NOW1
            fprintf(fout, "%.1f ", angle[i]);///NOW1
        }///NOW1
        fprintf(fout, "\n");///NOW1
    }
    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
        }
    }
    if(key=='t' || key=='p'){///NOW3 ///啟動timer 或是 play
        glutTimerFunc(33, timer, 0);///NOW3 ///NOW4 變快了
        if(fin==NULL) fin=fopen("motion.txt", "r");///NOW3
        for(int i=0; i<20; i++){///NOW3
            fscanf(fin, "%f", &newAngle[i]);///NOW3 ///NOW4 新角度
        }
    }
    glutPostRedisplay();///NOW2 請讀到angle[i]之後, 重畫畫面
}

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

成果為......

幫我稱10秒!_week14

第一節課是計時器的練習

 glutTimerFunc(1000, timer, 0);
 1000為1秒  後面的0是一個變數

下列程式能讓茶壺不停的旋轉
#include <GL/glut.h> #include <mmsystem.h>///NOW (0)發聲音 #include <stdio.h>///NOW3 for printf() float angle=0;///NOW4 void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix();///NOW4 glRotatef(angle, 0, 0, 1);///NOW4 glutSolidTeapot( 0.3 );///NOW4 glPopMatrix();///NOW4 glutSwapBuffers(); } void timer(int t)///NOW (2) { glutTimerFunc(30, timer, t+1);///NOW4 ///NOW3 一起床,馬上撥下一個鬧鐘, 時間才會準 printf("現在的 timer(t)的t是: %d\n", t);///NOW3 angle = t;///NOW4 glutPostRedisplay();///NOW4 要更新畫面 } int main(int argc, char**argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(500,500);///大一點 glutCreateWindow("week14 timer"); glutDisplayFunc(display); glutTimerFunc(1000, timer, 0);///NOW (1) glutMainLoop(); }


第二節課在教內差公式

設定 alpha值 float alpha = (t%30)/30.0; ///變成alpha:0.0~1.0
內差公式:nowX = alpha*1.0+(1-alpha)* -1.0;
可以利用內差公式來做動畫
    glPushMatrix();///NOW4
        glTranslatef(nowX, 0, 0);///NOW4
        glutSolidTeapot( 0.3 );///NOW4
    glPopMatrix();///NOW4


如何把檔案帶到其他地方開起來
把cbp檔案用notepad++打開
並且把<Option working_dir="." /> 連結位子處改成.
<Add directory="C:/Users/user/Desktop/freeglut/include" /> 改成自己名稱的位子



最後下面這是能用計時器+內差公式做出動畫的程式
#include <stdio.h>///NOW1 for fprintf() FILE * fout=NULL;///NOW1 for fopen() + fprintf() FILE * fin =NULL;///NOW2 for fopen() + fscanf() #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];///NOW1 float oldAngle[20];///NOW4 float newAngle[20];///NOW4 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 timer(int t)///NOW3 { glutTimerFunc(33, timer, t+1);///NOW3 ///NOW4 快一點 if(t%30==0){///NOW4 每 30frame要讀新資料 if(fin==NULL) fin=fopen("motion.txt", "r");///NOW3 for(int i=0; i<20; i++){///NOW3 oldAngle[i] = newAngle[i]; ///NOW4 fscanf(fin, "%f", &newAngle[i]);///NOW4 新資料 } } float alpha = (t%30)/30.0;///NOW4 今天第1節有寫過它,內插 for(int i=0;i<20;i++){///NOW4 angle[i] = alpha*newAngle[i] + (1-alpha)*oldAngle[i];///NOW4 } glutPostRedisplay();///NOW4 } 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'){///NOW1 if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW1 for(int i=0; i<20; i++){///NOW1 fprintf(fout, "%.1f ", angle[i]);///NOW1 }///NOW1 fprintf(fout, "\n");///NOW1 } 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 } } if(key=='t' || key=='p'){///NOW3 ///啟動timer 或是 play glutTimerFunc(33, timer, 0);///NOW3 ///NOW4 變快了 if(fin==NULL) fin=fopen("motion.txt", "r");///NOW3 for(int i=0; i<20; i++){///NOW3 fscanf(fin, "%f", &newAngle[i]);///NOW3 ///NOW4 新角度 } } glutPostRedisplay();///NOW2 請讀到angle[i]之後, 重畫畫面 } 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(); }