2023年5月4日 星期四

劉 Week12 電腦圖學

 Week 12 主題:檔案File

第一堂課

建立一個空白檔案week12_1 printf_fprintf

#include <stdio.h>
int main(void)
{
    ///檔案指標 fout = fopen("檔名", "模式");
    FILE * fout = fopen("file.txt", "w");
    printf("Hello world\n");
    fprintf(fout, "Hello world在檔案裡\n");
    return 0;
}








建立一 個新的空白檔案Week12-2_fscanf.

#include <stdio.h>
int main()
{
    char line[20];
    ///scanf("%s", line);
    FILE * fin = fopen("file.txt", "r");
    fscanf(fin, "%s\n", line);
    printf("讀到了:%s\n", line);
    fscanf(fin, "%s\n", line);
    printf("讀到了:%s\n", line);
}







建立一 個新的空白檔案Week12-3

#include <stdio.h>
int main()
{
    int a[10] = {10,20,30,40,50,60,70,80,90,100};
    FILE * fout = fopen("file3.txt", "w");
    for(int i=0; i<10; i++){
        fprintf(fout, "%d ", a[i] );
        printf("%d ", a[i] );
    }
    fclose(fout);

    int b[10];
    FILE * fin = fopen("file3.txt","r");
    for(int i=0; i<10; i++){;
    fscanf(fin, "%d", &b[i]);
    printf("%d",b[i]);
}
fclose(fin);
}







第二堂課

建立GLUT專案week12-4 _keyboard_mouse



#include <stdio.h>
#include <GL/glut.h>
float teapotX = 0, teapotY = 0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(teapotX, teapotY, 0);
        glutSolidTeapot( 0.3 );
    glPopMatrix();
    glutSwapBuffers();
}

void mouse (int button, int state, int x, int y)
{
    teapotX = (x-150)/150.0;
    teapotY = (150-y)/150.0;
    display();
}
int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
   glutCreateWindow("week12");

   glutDisplayFunc(display);
   glutMouseFunc(mouse);

   glutMainLoop();
}




#include <stdio.h>
#include <GL/glut.h>
float teapotX = 0, teapotY = 0;
FILE * fout = NULL;
FILE * fin = NULL;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(teapotX, teapotY, 0);
        glutSolidTeapot( 0.3 );
    glPopMatrix();
    glutSwapBuffers();
}

void mouse (int button, int state, int x, int y)
{
    teapotX = (x-150)/150.0;
    teapotY = (150-y)/150.0;
    if(state==GLUT_DOWN){
        if(fout==NULL) fout = fopen("file4.txt", "w");

        fprintf(fout, "%f %f\n", teapotX, teapotY);
    }
    display();
}
void keyboard(unsigned char key, int x, int y)
{
   if(fin==NULL){
      fclose(fout);
      fin = fopen("file4.txt", "r");
   }
    fscanf(fin, "%f%f", &teapotX, &teapotY);
    display();
}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
   glutCreateWindow("week12");

   glutDisplayFunc(display);
   glutMouseFunc(mouse);

   glutMainLoop();
}



第三堂課

將codeblock的project的Debug和Release的路徑改成.



再將freeglut->lib->中的freeglut.dll

移到week12-4 _keyboard_mouse資料夾中













沒有留言:

張貼留言