2020年5月27日 星期三

Week 14 _


- Draw -


 ❖ 茶壺跟著滑鼠移動,利用數字鍵切換部位 

#include <GL/glut.h>
int angle[10]={0,0,0,0,0, 0,0,0,0,0};
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glutSolidTeapot(0.2);

    glPushMatrix();
        glTranslatef(0.2, 0,0 );
        glRotatef(angle[0], 0,0,1);
        glTranslatef(0.2, 0,0 );

        glColor3f(1,0,0);
        glutSolidTeapot(0.2);

        glPushMatrix();
            glTranslatef(0.2, 0,0);
            glRotatef(angle[1], 0,0,1);
            glTranslatef(0.2, 0,0);

            glColor3f(1,1,0);
            glutSolidTeapot(0.2);

        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.2, 0,0 );
        glRotatef(angle[2], 0,0,1);
        glTranslatef(-0.2, 0,0 );

        glColor3f(0,1,0);
        glutSolidTeapot(0.2);

        glPushMatrix();
            glTranslatef(-0.2, 0,0);
            glRotatef(angle[3], 0,0,1);
            glTranslatef(-0.2, 0,0);

            glColor3f(0,0,1);
            glutSolidTeapot(0.2);

        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int angleID=0;
int oldX, oldY;
void mouse(int button, int state, int x, int y)
{
    oldX=x; oldY=y;
}
void motion(int x, int y)
{
    angle[angleID] += (x-oldX);
    oldX=x;
    glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
    if(key=='0') angleID=0;
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week14 file");

    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}

 ❖ 自動存檔、顯示數值 


#include <stdio.h>   ❖ 新增整個函式 
FILE * fout=NULL;
void saveAll()
{
    if(fout==NULL) fout=fopen("motion.txt", "w+");
    for(int i=0; i<10; i++)
    {
         printf(      "%d ", angle[i]);
        fprintf(fout, "%d ", angle[i]);
    }
     printf(      "\n");
    fprintf(fout, "\n");
}

void motion(int x, int y)
{
    angle[angleID] += (x-oldX);
    oldX=x;
    saveAll();   ❖ 新增這行 
    glutPostRedisplay();
}

 ❖ 按r會重畫畫面 

if(key=='r') ❖ 新增整個迴圈 
{
    if(fin==NULL) fin=fopen("motion.txt","r");
    for(int i=0;i<10;i++)
    {
        fscanf(fin,"%d",&angle[i]);
    }
    glutPostRedisplay();
}


#include <GL/glut.h>
#include <stdio.h>  ///TODO3: 外掛 
///全域變數(global variable)最好放在最前面,放中間不好!!!!
FILE * fout=NULL;  ///TODO3 
FILE * fin=NULL; ///TODO4: 宣告檔案指標 
int angle[10]={0,0,0,0,0, 0,0,0,0,0};
int angleID=0;
int oldX, oldY;

 /// C/C++因為函式使用前, 要先宣告or定義, 有時候會順序不對 
void display();
void motion(int x, int y);
void mouse(int button, int state, int x, int y);
void keyboard(unsigned char key, int x, int y);
void saveAll();
///可以在程式的最前面, 把全部的函式的外形, 先宣告一次
///(不要寫完整的定義)

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glutSolidTeapot(0.2);

    glPushMatrix();
        glTranslatef(0.2, 0,0 );
        glRotatef(angle[0], 0,0,1);
        glTranslatef(0.2, 0,0 );

        glColor3f(1,0,0);
        glutSolidTeapot(0.2);

        glPushMatrix();
            glTranslatef(0.2, 0,0);
            glRotatef(angle[1], 0,0,1);
            glTranslatef(0.2, 0,0);

            glColor3f(1,1,0);
            glutSolidTeapot(0.2);

        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.2, 0,0 );
        glRotatef(angle[2], 0,0,1);
        glTranslatef(-0.2, 0,0 );

        glColor3f(0,1,0);
        glutSolidTeapot(0.2);

        glPushMatrix();
            glTranslatef(-0.2, 0,0);
            glRotatef(angle[3], 0,0,1);
            glTranslatef(-0.2, 0,0);

            glColor3f(0,0,1);
            glutSolidTeapot(0.2);
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}

void mouse(int button, int state, int x, int y)
///TODO2:備份(old)mouse位置 
    oldX=x; oldY=y; ///TODO2:備份(old)mouse位置 
}

void saveAll() ///TODO3:  (自己寫的函式)全部存檔 
{       
    if(fout==NULL) fout=fopen("motion.txt", "w+"); ///TODO3:開檔 
    for(int i=0; i<10; i++)
    {  ///TODO3: for迴圈,把陣列全用 
         printf(      "%d ", angle[i]); ///TODO3: 印畫面 
        fprintf(fout, "%d ", angle[i]); ///TODO3: 寫檔案 
    }
     printf(      "\n");  ///TODO3: 跳行, 畫面不會亂 
    fprintf(fout, "\n");  ///TODO3: 跳行, 畫面不會亂 
}
void motion(int x, int y)
///TODO2:滑動,更新 angleID對應的angle[] 
    angle[angleID] += (x-oldX);
    oldX=x;
    saveAll();  ///TODO3: (自己寫的函式)全部存檔 
    glutPostRedisplay();  ///TODO2:重畫畫面 
}
void readAll()
{
    if(fin==NULL) fin=fopen("motion.txt", "r");  ///TODO4 開檔 
    for(int i=0; i<10; i++)
    {
        fscanf(fin, "%d", &angle[i]);///TODO4 讀檔
    }
}
void keyboard(unsigned char key, int x, int y)
{///TODO2: 鍵盤,選angleID
    if(key=='0') angleID=0;
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
    if(key=='r'){
        readAll();
        glutPostRedisplay();///TODO4 重畫
    }
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week14 file");

    glutDisplayFunc(display);
    glutMouseFunc(mouse);///TODO2:備份(old)mouse位置
    glutMotionFunc(motion);///TODO2:滑動,更新 angleID對應的angle[]
    glutKeyboardFunc(keyboard);///TODO2: 鍵盤,選angleID
    glutMainLoop();
}


沒有留言:

張貼留言