2018年6月25日 星期一

Week13_蔡明憲

練習寫檔、讀檔

 程式碼 :

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


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

沒有留言:

張貼留言