2018年5月25日 星期五

Week13_陳示珮

第十三週

(1) 主題 : 寫檔、讀檔
(2) 主題 : 控制關節
(3) 主題 : 動作內插



實作一   練習開檔案














(1) 打開codeblock ,新增新的文件檔

 步驟 : File→New→Empty file
















(2) 儲存文件檔,並將格式改成.cpp

步驟 : File →Save file as→檔名.cpp
















(3) 開啟檔案 output.txt會出現在桌面

程式碼 :

#include <stdio.h>
int main()
{
    FILE * fout = fopen("output.txt","w+");
    ///FILE的指標 FILE開檔 檔名 要寫write新增檔案
    printf("Hello World");
}


















(4)將訊息寫入output.txt中

程式碼 :

#include <stdio.h>
int main()
{
    FILE * fout = fopen("output.txt","w+");
    ///FILE的指標 FILE開檔 檔名 要寫write新增檔案
    ///printf("Hello World");
    fprintf(fout,"Hello World");
}




實作二    利用GLUT檔讀檔














(1)開啟glut檔,點一下白色視窗 ,第一次按s開檔案,第二次按後開始儲存位子

程式碼 :

#include <GL/glut.h>
#include <stdio.h>///要檔案
FILE * fout=NULL;///宣告檔案的指標,一開始是空的
float angle=60, angle2=90, angle3=60;///準備3個角度的值
void keyboard(unsigned char key, int x, int y)
{
    if(key=='s' || key=='S' || key=='w' || key=='W'){
        if(fout==NULL) fout=fopen("output.txt", "w+");
        ///上面這行,只有第一次按s鍵,才會還沒有值,才fopen
        ///因為第二次按s鍵,fout名花有主,不會再重覆開檔案
        printf("%.3f %.3f %.3f\n", angle,angle2,angle3);
        fprintf(fout, "%.3f %.3f %.3f\n", angle,angle2,angle3);///3個角度的值,印到檔案去
    }
}

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

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glutSwapBuffers();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");

    ///glutReshapeFunc(resize);
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    ///glutKeyboardFunc(key);
    ///glutIdleFunc(idle);

    glClearColor(1,1,1,1);
    ///glEnable(GL_CULL_FACE);
    ///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);

    glutMainLoop();

    return EXIT_SUCCESS;
}
















(2) 剛儲存的資料會存在 freeglut/bin中的output.txt

















(3) 畫茶壺,移動關節到想要的位子,按s即可儲存位子

程式碼 :

#include <GL/glut.h>
#include <stdio.h>///要檔案
FILE * fout=NULL;///宣告檔案的指標,一開始是空的
float angle=60, angle2=90, angle3=60;///準備3個角度的值
void keyboard(unsigned char key, int x, int y)
{
    if(key=='s' || key=='S' || key=='w' || key=='W'){
        if(fout==NULL) fout=fopen("output.txt", "w+");
        ///上面這行,只有第一次按s鍵,才會還沒有值,才fopen
        ///因為第二次按s鍵,fout名花有主,不會再重覆開檔案
        printf("%.3f %.3f %.3f\n", angle,angle2,angle3);
        fprintf(fout, "%.3f %.3f %.3f\n", angle,angle2,angle3);
    }///3個角度的值,印到檔案去
}
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 };

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    ///畫東西
    glPushMatrix();///像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面
        glutSolidTeapot(0.3);///身體

        glPushMatrix();///像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面
            glTranslatef(0.4, 0, 0);///(3) 掛在右邊
            glRotatef(angle, 0,0,1);///(2) angle的轉動角度,對Z軸
            glTranslatef(0.4, 0, 0);///(1) 往右移,讓手把在中間
            glutSolidTeapot(0.3); ///右手臂
        glPopMatrix();///像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面
    glPopMatrix();///像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面
    glutSwapBuffers();
}
void motion(int x, int y)
{
    angle = x;///角度會隨mouse motion而改值
    glutPostRedisplay();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");

    ///glutReshapeFunc(resize);
    glutDisplayFunc(display);
    glutMotionFunc(motion);///加上motion事件

    glutKeyboardFunc(keyboard);
    ///glutKeyboardFunc(key);
    ///glutIdleFunc(idle);

    glClearColor(1,1,1,1);
    ///glEnable(GL_CULL_FACE);
    ///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);

    glutMainLoop();

    return EXIT_SUCCESS;
}


















(4) 加入r, 執行後先按一下白色視窗 開始移動茶壺並按s儲存
完成後先關掉白色視窗 再關黑色視窗


*#include <GL/glut.h>
#include <stdio.h>///要檔案
FILE * fout=NULL;
FILE * fin=NULL;//宣告檔案的指標,一開始是空的
float angle=60, angle2=90, angle3=60;///準備3個角度的值
void keyboard(unsigned char key, int x, int y)
{
    if(key=='s' || key=='S' || key=='w' || key=='W'){
        if(fout==NULL) fout=fopen("output.txt", "w+");
        ///上面這行,只有第一次按s鍵,才會還沒有值,才fopen
        ///因為第二次按s鍵,fout名花有主,不會再重覆開檔案
        printf("%.3f %.3f %.3f\n", angle,angle2,angle3);
        fprintf(fout, "%.3f %.3f %.3f\n", angle,angle2,angle3);
    }///3個角度的值,印到檔案去
    if(key=='r'||key=='R')
    {
        if(fin==NULL) fin=fopen("output.txt", "r");
        fscanf(fin, "%f %f %f", &angle,&angle2,&angle3);
        glutPostRedisplay();
    }
}
///打光的陣列之前全部刪掉, 陣列不要刪
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 };

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///畫東西
    glPushMatrix();///像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面
        glutSolidTeapot(0.3);///身體

        glPushMatrix();///像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面
            glTranslatef(0.4, 0, 0);///(3) 掛在右邊
            glRotatef(angle, 0,0,1);///(2) angle的轉動角度,對Z軸
            glTranslatef(0.4, 0, 0);///(1) 往右移,讓手把在中間
            glutSolidTeapot(0.3); ///右手臂
        glPopMatrix();///像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面
    glPopMatrix();///像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面
    glutSwapBuffers();
}
void motion(int x, int y)
{
    angle[now] = x;/// 角度會隨mouse motion而改值
    glutPostRedisplay();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");


    ///glutReshapeFunc(resize);
    glutDisplayFunc(display);
    glutMotionFunc(motion);/// 加上motion事件

    glutKeyboardFunc(keyboard);
    ///glutKeyboardFunc(key);
    ///glutIdleFunc(idle);

    glClearColor(1,1,1,1);
    ///glEnable(GL_CULL_FACE);
    ///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);

    glutMainLoop();

    return EXIT_SUCCESS;
}

















(4) 可到freeglut/bin查看 剛剛的位子是否存入output.txt
關掉執行檔後再執行一次 ,按一下白色視窗 ,接著按r 即可重播剛剛儲存的位子




















(5) 加入手臂手軸 儲存位子

鍵盤搭配滑鼠來操控關節 例如:按著1再用滑鼠操控關節要移動的位子

*#include <GL/glut.h>
#include <stdio.h>///要檔案
FILE * fout=NULL;///宣告檔案的指標,一開始是空的
FILE * fin=NULL;/// 宣告(input)檔案的指標,一開始是空的
float angle[20];///準備20個角度的值 (陣列如果宣告在 外面,default都是0)
int now=0;///用keyboard的數字鍵,來切換關節
void keyboard(unsigned char key, int x, int y)
{
    if(key=='1') now=1;///用keyboard的數字鍵,來切換關節
    if(key=='2') now=2;
    if(key=='3') now=3;
    if(key=='4') now=4;
    if(key=='5') now=5;
    if(key=='6') now=6;
    if(key=='7') now=7;
    if(key=='8') now=8;
    if(key=='s' || key=='S' || key=='w' || key=='W'){
        if(fout==NULL) fout=fopen("output.txt", "w+");
        ///上面這行,只有第一次按s鍵,才會還沒有值,才fopen
        ///因為第二次按s鍵,fout名花有主,不會再重覆開檔案
        ///printf("%.3f %.3f %.3f\n", angle,angle2,angle3);
        ///fprintf(fout, "%.3f %.3f %.3f\n", angle,angle2,angle3);
        for(int i=0;i<20;i++){
            printf("%.3f ", angle[i]);///把每一個陣列裡的角度,都寫一次
            fprintf(fout, "%.3f ", angle[i]);///  把每一個陣列裡的角度,都寫一次
        }
        printf("\n");///最後要印個跳行,才不會畫面亂掉
        fprintf(fout,"\n");

    }///3個角度的值,印到檔案去
    if(key=='r' || key=='R'){
        if(fin==NULL) fin=fopen("output.txt", "r");///第一次讀檔時,要先開檔
        for(int i=0;i<20;i++){
            fscanf(fout, "%.3f ", &angle[i]);///把每一個陣列裡的角度,都讀一次
            printf("%.3f ", angle[i]);
        }
        glutPostRedisplay();///重畫畫面
    }
}
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 };

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///畫東西
    glPushMatrix();///像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面
        glutSolidTeapot(0.3);///身體

        glPushMatrix();///像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面
            glTranslatef(0.4, 0, 0);///掛在右邊
            glRotatef(angle[1], 0,0,1);/// angle的轉動角度,對Z軸
            glTranslatef(0.4, 0, 0);///往右移,讓手把在中間
            glutSolidTeapot(0.3); ///右手臂
            glPushMatrix();
                glTranslatef(0.4, 0, 0);
                glRotatef(angle[2], 0,0,1);
                glTranslatef(0.4, 0, 0);
                glutSolidTeapot(0.3); ///右手肘
            glPopMatrix();
        glPopMatrix();///像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面

        glPushMatrix();///像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面
            glTranslatef(-0.4, 0, 0);
            glRotatef(angle[3], 0,0,1);
            glTranslatef(-0.4, 0, 0);
            glutSolidTeapot(0.3); ///左手臂
            glPushMatrix();
                glTranslatef(-0.4, 0, 0);
                glRotatef(angle[4], 0,0,1);
                glTranslatef(-0.4, 0, 0);
                glutSolidTeapot(0.3); ///左手肘
            glPopMatrix();
        glPopMatrix();///像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面

    glPopMatrix();///像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面
    glutSwapBuffers();
}
void motion(int x, int y)
{
    angle[now] = x;/// 角度會隨mouse motion而改值
    glutPostRedisplay();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");


    ///glutReshapeFunc(resize);
    glutDisplayFunc(display);
    glutMotionFunc(motion);/// 加上motion事件

    glutKeyboardFunc(keyboard);
    ///glutKeyboardFunc(key);
    ///glutIdleFunc(idle);

    glClearColor(1,1,1,1);
    ///glEnable(GL_CULL_FACE);
    ///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);

    glutMainLoop();

    return EXIT_SUCCESS;
}



沒有留言:

張貼留言