2019年5月16日 星期四

06160805的課堂筆記

電腦圖學
1.主題:檔案
2.主題:關節、擺動作
3.主題:複習
4.作業:機器人擺動作


開啟code blocks→File→New→Project→Console application


打上程式碼,可以在自己的資料夾上看到一個新檔案,打開資料夾,裡面可以看到輸出的東西

#include <stdio.h>
FILE * fout=NULL; ///宣告一個檔案指標
int main()
{ ///檔案寫黨
    fout=fopen("filename.txt","w+"); ///我們要開的檔案
    for(int i=0;i<20;i++){
        fprintf(fout,"Hello World\n"); ///印出來的檔案
    }
}


再加上一些程式碼,多加一個檔案

#include <stdio.h>
FILE * fout2=NULL; ///NOW2 寫另一個檔案
FILE * fin2=NULL; ///NOW2 讀另一個檔案
float angle[20]; ///NOW2 我們有20個關的角度
int main()
{ ///檔案寫黨
    fout2=fopen("motion.txt","w+"); ///NOW2 另一個有關節的檔案
    for(int i=0;i<20;i++){
        fprintf(fout2," %.2f ",angle[i]); ///另一個有關節的檔案
    }               ///加空格是因為怕數字黏起來
                 ///若沒有加空格,數字就會黏起來
             fprintf(fout2,"\n");
}



下載檔案


將檔案移至桌面並開啟


將檔案移至桌面並開啟


將lib打開


將此檔案複製並貼上


將貼上的檔案重新命名為libglut32.a


File→New→Project


選取 GLUT project


輸入名稱,將檔案儲存在桌面


將老師提供的程式碼貼上去後,紅色茶壺可以用滑鼠進行旋轉,顏色可以依照自己的喜好做修改

#include <GL/glut.h>
float angle[20];///NOW2
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glColor3f(1, 1, 1);///white
        glutSolidTeapot( 0.3 );///body
        glPushMatrix();
            glTranslatef(0.2, 0, 0);///NOW2 (3)掛上去
            glRotatef(angle[1], 0,0,1);///NOW2 ///(2)旋轉
            glTranslatef(0.15, 0, 0);///NOW2 (1)旋轉中心
            glColor3f(1, 0, 0);///red
            glutSolidTeapot( 0.2 );///right arm
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int oldX=0;///NOW2
void mouse(int button, int state, int x, int y)///NOW2
{///NOW2
    oldX = x;///NOW2
}
void motion(int x, int y)///NOW2
{///NOW2
    angle[1] += x-oldX;///NOW2
    oldX = x;///NOW2
    display();///NOW2
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);///可以開大一點的window
    glutCreateWindow("Week 13 angle motion file");
    glutMouseFunc(mouse);///NOW2
    glutMotionFunc(motion);///NOW2
    glutDisplayFunc(display);

    glutMainLoop();
}


將老師提供的程式碼貼上去後,執行的結果是按數字1,2,3,4,可以旋轉不同的茶壺角度

#include <GL/glut.h>
float angle[20];///NOW2
int angleID=1;///NOW3
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glColor3f(1, 1, 1);///white
        glutSolidTeapot( 0.3 );///body

        glPushMatrix();///右邊
            glTranslatef(0.2, 0, 0);///NOW2 (3)掛上去
            glRotatef(angle[1], 0,0,1);///NOW2 ///(2)旋轉
            glTranslatef(0.15, 0, 0);///NOW2 (1)旋轉中心
            glColor3f(1, 0, 0);///red
            glutSolidTeapot( 0.2 );///right arm
            glPushMatrix();
                glTranslatef(0.2, 0, 0);///NOW2 (3)掛上去
                glRotatef(angle[2], 0,0,1);///NOW2 ///(2)旋轉
                glTranslatef(0.15, 0, 0);///NOW2 (1)旋轉中心
                glColor3f(1, 0, 0);///red
                glutSolidTeapot( 0.2 );///right lower arm
            glPopMatrix();
        glPopMatrix();

        glPushMatrix();///左邊
            glTranslatef(-0.2, 0, 0);///NOW2 (3)掛上去
            glRotatef(angle[3], 0,0,1);///NOW2 ///(2)旋轉
            glTranslatef(-0.15, 0, 0);///NOW2 (1)旋轉中心
            glColor3f(1, 0, 0);///red
            glutSolidTeapot( 0.2 );///right arm
            glPushMatrix();
                glTranslatef(-0.2, 0, 0);///NOW2 (3)掛上去
                glRotatef(angle[4], 0,0,1);///NOW2 ///(2)旋轉
                glTranslatef(-0.15, 0, 0);///NOW2 (1)旋轉中心
                glColor3f(1, 0, 0);///red
                glutSolidTeapot( 0.2 );///right lower arm
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int oldX=0;///NOW2
void mouse(int button, int state, int x, int y)///NOW2
{///NOW2
    oldX = x;///NOW2
}
#include <stdio.h>///NOW3
void motion(int x, int y)///NOW2
{///NOW2
    angle[angleID] += x-oldX;///NOW3
    oldX = x;///NOW2
    for(int i=0;i<20;i++){///NOW3
        printf(" %.2f ", angle[i]);///NOW3
    }
    printf("\n");///NOW3
    display();///NOW2
}
void keyboard(unsigned char key, int x, int y)///NOW3
{///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
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);///可以開大一點的window
    glutCreateWindow("Week 13 angle motion file");
    glutKeyboardFunc(keyboard);///NOW3
    glutMouseFunc(mouse);///NOW2
    glutMotionFunc(motion);///NOW2
    glutDisplayFunc(display);

    glutMainLoop();
}



寫檔案

#include <GL/glut.h>
#include <stdio.h>///NOW_FILE (0)
float angle[20];///NOW2
int angleID=1;///NOW3
FILE * fout = NULL;///NOW_FILE (1)
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glColor3f(1, 1, 1);///white
        glutSolidTeapot( 0.3 );///body

        glPushMatrix();///右邊
            glTranslatef(0.2, 0, 0);///NOW2 (3)掛上去
            glRotatef(angle[1], 0,0,1);///NOW2 ///(2)旋轉
            glTranslatef(0.15, 0, 0);///NOW2 (1)旋轉中心
            glColor3f(1, 0, 0);///red
            glutSolidTeapot( 0.2 );///right arm
            glPushMatrix();
                glTranslatef(0.2, 0, 0);///NOW2 (3)掛上去
                glRotatef(angle[2], 0,0,1);///NOW2 ///(2)旋轉
                glTranslatef(0.15, 0, 0);///NOW2 (1)旋轉中心
                glColor3f(1, 0, 0);///red
                glutSolidTeapot( 0.2 );///right lower arm
            glPopMatrix();
        glPopMatrix();

        glPushMatrix();///左邊
            glTranslatef(-0.2, 0, 0);///NOW2 (3)掛上去
            glRotatef(angle[3], 0,0,1);///NOW2 ///(2)旋轉
            glTranslatef(-0.15, 0, 0);///NOW2 (1)旋轉中心
            glColor3f(1, 0, 0);///red
            glutSolidTeapot( 0.2 );///right arm
            glPushMatrix();
                glTranslatef(-0.2, 0, 0);///NOW2 (3)掛上去
                glRotatef(angle[4], 0,0,1);///NOW2 ///(2)旋轉
                glTranslatef(-0.15, 0, 0);///NOW2 (1)旋轉中心
                glColor3f(1, 0, 0);///red
                glutSolidTeapot( 0.2 );///right lower arm
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int oldX=0;///NOW2
void mouse(int button, int state, int x, int y)///NOW2
{///NOW2
    oldX = x;///NOW2
}

void motion(int x, int y)///NOW2
{///NOW2
    angle[angleID] += x-oldX;///NOW3
    oldX = x;///NOW2
    if(fout == NULL) fout = fopen("motion.txt", "w+");///NOW_FILE (2)
    for(int i=0;i<20;i++){///NOW3
        printf(" %.2f ", angle[i]);///NOW3
        fprintf(fout, " %.2f ", angle[i]);///NOW_FILE
    }
    printf("\n");///NOW3
    fprintf(fout, "\n");///NOW_FILE
    display();///NOW2
}
void keyboard(unsigned char key, int x, int y)///NOW3
{///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'){///NOW_FILE
        fout = fopen("motion.txt", "w+");///NOW_FILE (2)
    }
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);///可以開大一點的window
    glutCreateWindow("Week 13 angle motion file");
    glutKeyboardFunc(keyboard);///NOW3
    glutMouseFunc(mouse);///NOW2
    glutMotionFunc(motion);///NOW2
    glutDisplayFunc(display);

    glutMainLoop();
}


使用鍵盤「R」鍵,可讓茶壺旋轉

#include <GL/glut.h>
#include <stdio.h>///NOW_FILE (0)
float angle[20];///NOW2
int angleID=1;///NOW3
FILE * fout = NULL;///NOW_FILE (1)
FILE * fin = NULL;///NOW_FILE_READ (1)
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glColor3f(1, 1, 1);///white
        glutSolidTeapot( 0.3 );///body

        glPushMatrix();///右邊
            glTranslatef(0.2, 0, 0);///NOW2 (3)掛上去
            glRotatef(angle[1], 0,0,1);///NOW2 ///(2)旋轉
            glTranslatef(0.15, 0, 0);///NOW2 (1)旋轉中心
            glColor3f(1, 0, 0);///red
            glutSolidTeapot( 0.2 );///right arm
            glPushMatrix();
                glTranslatef(0.2, 0, 0);///NOW2 (3)掛上去
                glRotatef(angle[2], 0,0,1);///NOW2 ///(2)旋轉
                glTranslatef(0.15, 0, 0);///NOW2 (1)旋轉中心
                glColor3f(1, 0, 0);///red
                glutSolidTeapot( 0.2 );///right lower arm
            glPopMatrix();
        glPopMatrix();

        glPushMatrix();///左邊
            glTranslatef(-0.2, 0, 0);///NOW2 (3)掛上去
            glRotatef(angle[3], 0,0,1);///NOW2 ///(2)旋轉
            glTranslatef(-0.15, 0, 0);///NOW2 (1)旋轉中心
            glColor3f(1, 0, 0);///red
            glutSolidTeapot( 0.2 );///right arm
            glPushMatrix();
                glTranslatef(-0.2, 0, 0);///NOW2 (3)掛上去
                glRotatef(angle[4], 0,0,1);///NOW2 ///(2)旋轉
                glTranslatef(-0.15, 0, 0);///NOW2 (1)旋轉中心
                glColor3f(1, 0, 0);///red
                glutSolidTeapot( 0.2 );///right lower arm
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int oldX=0;///NOW2
void mouse(int button, int state, int x, int y)///NOW2
{///NOW2
    oldX = x;///NOW2
}

void motion(int x, int y)///NOW2
{///NOW2
    angle[angleID] += x-oldX;///NOW3
    oldX = x;///NOW2
    if(fout == NULL) fout = fopen("motion.txt", "w+");///NOW_FILE (2)
    for(int i=0;i<20;i++){///NOW3
        printf(" %.2f ", angle[i]);///NOW3
        fprintf(fout, " %.2f ", angle[i]);///NOW_FILE
    }
    printf("\n");///NOW3
    fprintf(fout, "\n");///NOW_FILE
    display();///NOW2
}
void keyboard(unsigned char key, int x, int y)///NOW3
{///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'){///NOW_FILE
        fout = fopen("motion.txt", "w+");///NOW_FILE (2)
    }
    if(key=='r'){
        if(fin==NULL) fin = fopen("motion.txt", "r");///NOW_FILE_READ (2)
        ///scanf("%d", &n);
        for(int i=0;i<20; i++){///NOW_FILE_READ
            fscanf(fin, "%f", &angle[i]);///NOW_FILE_READ
            printf(" %.2f ", angle[i]);///順便印出來看數字對不對
        }
    }
    glutPostRedisplay();///NOW_FILE_READ 和 display()很像
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);///可以開大一點的window
    glutCreateWindow("Week 13 angle motion file");
    glutKeyboardFunc(keyboard);///NOW3
    glutMouseFunc(mouse);///NOW2
    glutMotionFunc(motion);///NOW2
    glutDisplayFunc(display);

    glutMainLoop();
}






沒有留言:

張貼留言