2020年5月27日 星期三

Week14

Step 1. FILE檔案,fopen()開檔,fprintf()寫檔。
檔案先放桌面~
#include <stdio.h>
int main()
{///檔案指標 fout 是開啟檔(write...)
    FILE * fout=fopen("檔名.txt", "w+");

    printf("       Hello world \n");
    fprintf( fout,"Hello world in file\n");
}

我們加上for迴圈,印很多迴圈。
#include <stdio.h>
int main()
{///檔案指標 fout 是開啟檔(write...)
    FILE * fout=fopen("檔名2.txt", "w+");

    for(int i=0; i<20; i++){
    printf(       "Hello world %d\n",i);
    fprintf( fout,"Hello world in file %d\n",i);
    }
}
陣列的值,用for迴圈寫出來~
#include <stdio.h>
int angle[10];
int main()
{///檔案指標 fout 是開啟檔(write...)
    FILE * fout=fopen("motion.txt", "w+");

    for(int i=0; i<10; i++){///TODO2:for迴圈
    printf(       "%d ",angle[i]);
    fprintf( fout,"%d ",angle[i]);
    }
    printf("\n");
    fprintf(fout, "\n");
}

Step 2. 用GLUT檔做。
#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 main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week14 file");

    glutDisplayFunc(display);
    glutMainLoop();
}

紅色的部分代表加入的程式碼,使關節轉動。

我們讓關節轉動的時候順便加上座標位置。

沒有留言:

張貼留言