WEEK14
Work01
第一步:和過去相同開啟新專案,選擇Console application,在選擇C++
第二步:撰寫程式碼
//------------------------------------------------------程式碼-------------------------------------------------
#include <stdio.h>
FILE *fout=NULL,*fin=NULL;//檔案的指標
int main(int argc,char **argv)
{
if(fout==NULL) fout=fopen("output.txt","w+");
//如果fout為空指標,觸發條件
//讓fout輸出新的文件,"w+"寫入
fprintf(fout,"Hello World");
}
//--------------------------------------------------------------------------------------------------------------
Work02
//------------------------------------------------------程式碼-------------------------------------------------
#include <stdio.h>
#include <GL/glut.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("output.txt","w+");
fprintf(fout,"%d %d\n",x,y);
}
int main(int argc,char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("3D");
glutMotionFunc(motion);
glutDisplayFunc(display);
glutMainLoop();
}
//--------------------------------------------------------------------------------------------------------------
Work03
接下來,利用輸出的滑鼠移動數據檔案,將位置輸入到茶壺座標,讓茶壺移動
//------------------------------------------------------程式碼-------------------------------------------------
#include <stdio.h>
#include <GL/glut.h>
FILE *fout=NULL,*fin=NULL;//檔案的指標
int nowX=150,nowY=150;//讀取檔案位置的座標點
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();//stack push
glTranslatef((nowX-150)/150.0 , -(nowY-150)/150.0,0);
//視窗初始設定大小是300x300,所以中心座標(150,150),因此減掉150,讓範圍在-150到150之間
//因為視窗的實際座標是1到-1之間,因此在除以150.0,讓座標在1.0到-1.0之間的float(浮點)數
glutSolidTeapot(0.3);
glPopMatrix();//stack pop
glutSwapBuffers();
}
void motion(int x,int y)
{
if(fout==NULL) fout=fopen("output.txt","w+");
fprintf(fout,"%d %d\n",x,y);
}
void keyboard(unsigned char key, int X, int y)
{
if(key=='a')//當鍵盤輸入'a'時,觸發下方條件
{
if(fin==NULL) fin=fopen("output.txt","r");
fscanf(fin, "%d %d", &nowX, &nowY);
}
glutPostRedisplay();//告知電腦/GLUT,有空時要進行畫面重製
}
int main(int argc,char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("3D");
glutMotionFunc(motion);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
//--------------------------------------------------------------------------------------------------------------















沒有留言:
張貼留言