2016年5月24日 星期二

Week14 蔡宜真


課堂作業1

建立一個貝殼專案

















打入程式碼:

#include <stdio.h>
FILE *fout=NULL, *fin=NULL;
int main(int argc,char**argv)
{

    if(fout==NULL) fout=fopen("output.txt","w++");
    ///printf("Hello World\n");
    fprintf(fout, "Hello world\n");
}
執行,會產生一個txt檔案
                                                                                 














課堂作業2

build option =>  加入 include.lib 和 5個咒語



















打入程式碼:
#include <GL/glut.h>
#include <stdio.h>

FILE *fout=NULL, *fin=NULL;
void display()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}



void motion(int x, int y)
{
    if(fout==NULL)
        fout=fopen("motion.txt","w+");
    fprintf( fout, "%d %d\n",x,y);
    printf("%d %d\n",x,y);
}




int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
    glutCreateWindow("hello");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}
執行後,移動滑鼠=> 跑出數據後關閉


















數據會存入txt 檔

















課堂作業3

程式碼:

#include <GL/glut.h>
#include <stdio.h>

FILE *fout=NULL, *fin=NULL;
int nowX=0,nowY=0;
void display()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
      glTranslatef((nowX-150)/150.0,-(nowY-150)/150.0,0);
      glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}



void motion(int x, int y)
{
    if(fout==NULL)
        fout=fopen("motion.txt","w+");
    fprintf( fout, "%d %d\n",x,y);
    printf("%d %d\n",x,y);
}

void keyboard(unsigned char key,int x,int y)
{
  if(key=='r'){
    if(fin==NULL)fin=fopen("motion.txt","r");
    fscanf( fin,"%d %d\n",&nowX,&nowY);
    printf("%d %d\n",nowX,nowY);
  }

  glutPostRedisplay();///請電腦貼個公告,有空就display

}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
    glutCreateWindow("hello");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}

執行,用滑鼠點螢幕會跑出數據
關閉在執行,按r=>會執行剛剛的移動數據










沒有留言:

張貼留言