電腦圖學
(1)主題:移動
(2)示範:glTranslatef(x,y,z)
TODO:首先先到http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/ 這個網站
下載:windows.zip解壓縮並下載\windows\Transformation.exe
data.zip 解壓縮並下載\windows\data\模型
glut32.dll 下載\windows\glut32.dll
之後便會出現這個畫面
上述步驟好了後,便可開啟上面的Transformation.exe檔
圖片如下
接下來在Google Chrome下載freeglut(window的mingw版)
https://www.transmissionzero.co.uk/software/freeglut-devel/ 並把他解壓縮到桌面
之後再把解壓縮的freeglut資料夾打開來,並找到lib資料夾
再把lib裡面的libfreeglut.a複製成libglut32.a)
顯示出的畫面如下
再開啟Codeblocks 創立glut project
連結Week01的動作,如果產生此畫面表示創立成功
接下來就開始打程式碼,使我們要的畫面顯示出來
圖片如下
接下來的步驟,是要讓這個茶壺隨著滑鼠移動起來
程式碼如下
#include <GL/glut.h>
float x=0, y=0;///滑鼠motion
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();///備份矩陣
glTranslatef((x-150)/150.0,-(y-150)/150.0,0);///滑鼠motion
glutSolidTeapot(0.3);
///glVertex2f((x-150)/150.0,-(y-150)/150.0,0);
glPopMatrix();///還原矩陣,才不會在下次有舊的殘值
glutSwapBuffers();
}
void motion(int nowX, int nowY)///滑鼠motion
{ ///滑鼠motion
x=nowX; y=nowY;///滑鼠motion
display();///滑鼠motion 馬上更新
} ///滑鼠motion
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week04");
glutDisplayFunc(display);
glutMotionFunc(motion); ///滑鼠motion
glutMainLoop();
}
沒有留言:
張貼留言