課堂作業 1 : 讀檔
1. 開啟一個新專案
2. 撰寫程式碼
#include <stdio.h>FILE *fout=NULL, *fin=NULL;
int main(int argc, char**argv)
{
if (fout==NULL)
fout=fopen("output.txt", "w+");
fprintf(fout,"Hello World\n");
return 0;
}
3. 執行程式時會自動開啟一個檔案(output)
4. 將output檔用notepad++開啟 Hello World印在這個檔案裡
課堂作業 2 : 利用glut把mouse motion 存起來
1. 改成glut專案
2. 程式碼
#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("3D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
3. 放入freeglut.dll
4. 執行 滑鼠點選移動
5. 開啟motion檔 確認滑鼠數值有讀入檔案
6. 數值確認讀入檔案
課堂作業 3 : 讀檔 按'r'茶壺
1. 撰寫程式
#include <GL/glut.h>
#include <stdio.h>
FILE *fout=NULL, *fin=NULL;
int nowX=0, nowY=0;//oldX=0, oldY=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("3D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
2. 執行檔案 : 滑鼠按住移動(讀檔)
3. 確認檔案有讀入
4. 在執行一次檔案 (按住鍵盤"r"鍵 讓茶壺跟著之前滑鼠讀入的數值移動)
課堂作業 4 :
















沒有留言:
張貼留言