電腦圖學 2023-05-03 Week12
1. 主題: 檔案讀寫2. 主題: 鍵盤、滑鼠控制角度
3. 回家作業: 可控制關節角度的模型
Week12-1
1. 在CodeBlocks開一個的新的file,存在桌面檔名為week12-1.cpp
2. 加入以下程式碼
#include <stdio.h>
int main()
{
FILE*fout=fopen("file.txt","w");
printf("Hello World\n");
fprintf(fout,"Hello World在檔案裡\n");
}
3. 執行結果,小黑會出現Hello Word,桌面上的.txt檔中的字為Hello Word在檔案裡
Week12-2
1. 在CodeBlocks開新的GLUT
2. 加入以下程式碼
#include <stdio.h>
#include <GL/glut.h>
float teapotX=0,teapotY=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslated(teapotX,teapotY,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN)
{
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();
}3. 執行結果,茶壺會跟著滑鼠點擊的地方移動
Week12-5 MNK控制角度而且會記錄下來1. 繼續使用week12-4
2. 修改code
#include <stdio.h>
#include <GL/glut.h>
FILE*fout=NULL;
FILE*fin=NULL;
float teapotX=0,teapotY=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslated(teapotX,teapotY,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN)
{
teapotX=(x-150)/150.0;
teapotY=(150-y)/150.0;
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);
glutKeyboardFunc(keyboard);
glutMainLoop();
}3. 執行結果,先用滑鼠點擊紀錄茶壺位置,再按空白鍵回放之前紀錄的位置
Week12-6 更改工作執行目錄
沒有留言:
張貼留言