2016年5月23日 星期一

week14和鋼鐵人一起拯救世界a柏廷

TODO1:讀取檔案

1.創建貝殼專案


2.語言設定選擇C++


3.路徑設定


4.環境設定不用更改



5.程式碼




6.資料夾會出現一個 文字檔 會顯示讀取的內容。



#include <stdio.h>
FILE *fout=NULL,*fin=NULL;   ///指標給空值
int main(int argc,char **argv)
{
    if(fout==NULL) fout=fopen("output.txt","w+");  ///w+ 為可讀可寫,若無檔案會新增,若有檔案則會覆蓋。
    fprintf(fout,"Hello LBT");
}


TODO2  滑鼠紀錄座標

1. 環境變數設定,將五個咒語打進來


2.設定 freeglut 的include 路徑


3.設定 freeglut 的lib 路徑


4.程式碼


5.執行結果,可在視窗移動滑鼠,會紀錄移動座標


6. 座標存取的檔案



#include <GL/glut.h>
#include <stdio.h>
FILE *fout=NULL,*fin=NULL;   ///指標給空值
int i=0;
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("output.txt","w+");  ///w+ 為可讀可寫,若無檔案會新增,若有檔案則會覆蓋。
    fprintf(fout,"%d:%d,%d \n",i++,x,y);
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("3D");
    glutDisplayFunc(display);
    glutMotionFunc(motion);

    glutMainLoop();
}


TODO3 :讀取移動紀錄







#include <GL/glut.h>
#include <stdio.h>
FILE *fout=NULL,*fin=NULL;   ///檔案的指標給空值
int nowX=150,nowY=150;  ///等一下讀取檔案,把mouse motion 讀回來
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix(); ///保護
        glTranslatef((nowX-150)/150.0,-(nowY-150)/150.0,0);
        ///因為視窗約300*300,而這視窗正中心為原點(0,0),在除以150,就是介於1~-1的浮點數
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void motion(int x,int y)
{
    if(fout==NULL) fout=fopen("output.txt","w+");
    ///w+ 為可讀可寫,若無檔案會新增,若有檔案則會覆蓋。
    fprintf(fout,"%d %d \n",x,y);
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='r')   ///按小寫的r,會去讀一行/一組x y
    {
        if(fin==NULL) fin=fopen("output.txt","r");
        fscanf(fin,"%d %d",&nowX,&nowY);
    }
    glutPostRedisplay(); ///電腦貼個 post-it便利貼,告訴glut有空要重畫畫面
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("3D");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);

    glutMainLoop();
}

TODO4: 理解老師的程式碼




沒有留言:

張貼留言