Step01:
1.電腦圖學之父 伊凡·蘇澤蘭(Ivan Edward Sutherland)博士論文中提出的Sketchpad程式
2.他的指導老師:克勞德·向農(終極機器:外表平淡無奇,只是在一側有一個開關,彈一下開關,盒蓋就會打開,一個機械手會伸出來;將開關復原,機械手就縮回盒子。)
Step02:
1.swap translate/rotate(把2行程式交換,會有公轉和自轉效果)
Step03:
1.先寫入
#include<GL/glut.h>
void Display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,0);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main(int argc,char**argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week04_rotate");
glutDisplayFunc(Display);
glutMainLoop();
}
2.加入函式keyboard()
void keyboard(unsigned char key,int x,int y){
printf("你按下了%c在%d%d 座標\n",key,x,y);
}
3.在main()加入
glutKeyboardFunc(keyboard);
4.執行後發現mouse可以拖曳茶壺移動
Step04:
1.加入mouse(),motion()函式
void mouse(int button,int key,int x,int y){
}
void motion(int x,int y){
}
2.在main()加入
glutMouseFunc(mouse);
glutMotionFunc(motion);
Step05:
1.設定
float x=0,y=0,z=0;
int oldx=0,oldy=0;
2.改寫motion()
void motion(int mouseX,int mouseY){
x+=(mouseX-oldx);
y+=(mouseY-oldy);
oldx=mouseX;oldy=mouseY;
Display();
}
3.Display()加入glutPushMatrix(),glutPopMatrix()並改寫
glPushMatrix();
glTranslatef((x-150)/150.0,-(y-150)/150.0,z);
glColor3f(1,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
Step05:
1.y在Display()加入glClearColor(0.5,0.5,0.5,1); ///R,G,B,A,A:半透明功能,尚未開啟
2.mouse()加入oldx=mouseX;oldy=mouseY;///為了解決瞬移問題,改用正確方法
Step06:
1.改寫mouse()
加入
if(mouseX-oldx>0) scale*=1.01;
if(mouseX-oldx<0) scale*=0.99;
把x+=(mouseX-oldx);
y+=(mouseY-oldy);註解
2.在Display()加入glScalef(scale,scale,scale);
3.執行發現可放大縮小
沒有留言:
張貼留言