Week05
主題: 電腦圖學之父 Ivan Sutherland (Sketchpad)
老師介紹電腦圖學之父
主題: 移動、旋轉、縮放、矩陣
1.至 jsyeh.org/3dgc10 下載
data.zip =>解壓縮放到windows
windows.zip =>解壓縮 => 開啟Transfomation.exe
下面區域可以交換兩行程式碼,會有自轉公轉的效果
2-1. 鍵盤函式 glutKeyboardFunc()
新增一個keyboard函式,讓按下鍵盤時,能夠得知當下滑鼠的座標
#include <GL/glut.h>
#include <stdio.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BITS);
glColor3f(1,1,0);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
void keyboard(unsigned char key,int x,int y)
{
printf("你按下了 %c 在 %d %d 座標\n",key,x,y);
}
int main (int argc,char**argv)
{
glutInit( &argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week04 Rotate");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
2-2.鍵盤+滑鼠操作, 模仿 Maya/Unity介面
再新增一個新專案week05_keyboard_mouse_motion,接續上個程式week05_keyboard 變成 keyboard+mouse+motion
2-3.加入一個motion函式讓茶壺能夠跟著滑鼠移動
void motion(int mouseX,int mouseY)
{
x+=(mouseX-oldX); y+=(mouseY-oldY);
oldX =mouseX; oldY= mouseY;
display();
}
2-4.加入mouse函式,因為上面程式碼執行後茶壺每次都會重原點開始移動所以要解決這個問題
將滑鼠當前位置設為oldX、Y就可以解決這個問題了
void mouse(int bitton,int state,int mouseX,int mouseY)
{
oldX= mouseX; oldY= mouseY;
}
2-5.將茶壺放大縮小
void motion(int mouseX,int mouseY)
{
if( mouseX-oldX >0) scale*= 1.01;
if( mouseX-oldX <0) scale*= 0.99;
///x+=(mouseX-oldX); y+=(mouseY-oldY);
oldX =mouseX; oldY= mouseY;
display();
}
沒有留言:
張貼留言