➽課前範例
- 網址:http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
- 下載 [data][win32][glut32.dll] 三個檔案
- [windows]解壓縮>>開啟[windows]資料夾>>將[data]壓縮裡的資料夾和[glut32.dll]拉入[windows]資料夾
- 開啟 Transformation.exe
- glTranslatef(x,y,z); //移動
- x ←左右→
- y ↑上下↓
- z 前後
- glRotatef(r,x,y,z); //旋轉
- r 旋轉大小
- x,y,z 軸的方向 右手定理(右手比個讚👍,拇指為軸的方向)

➽ Motion 函式
- 同之前先下載freegult>>將lib裡的libfreeglut.a複製一份並改名libglut32.a
- 開啟Codeblocks>>開啟new project>>OpenGL
- 最主程式加入
- glutMotionFunc(motion);///自己寫的函式,目標讓滑鼠移動時讓圖照著轉動
- 並在 display 函式上寫入 motion 函式
- float myAngle=0;///準備放滑鼠角度
- void motion(int x, int y)
- {
- myAngle = x;///讓滑鼠的x當我們要的轉動的角度
- glutPostRedisplay();///請GLUT記得去更新display 函式
- }
- 將 display 函式裡的 a 改成自己訂的變數(myAngle)
- const double a = myAngle;///將滑鼠角度指定給 display函式
- 程式碼如下:
- #include <GL/glut.h>
- float myAngle=0;
- void motion(int x, int y)
- {
- myAngle = x;
- glutPostRedisplay();
- }
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- glRotatef(myAngle,0,0,1);///使茶壺對z軸做旋轉
- glutSolidTeapot(0.3);
- glPopMatrix();
- glutSwapBuffers();
- }
- int main(int argc, char *argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("Week04!!!");
- glutDisplayFunc(display);
- glutMotionFunc(motion);
- glutMainLoop();
- }

❊問題:無法連續轉動
➽茶壺+Motion旋轉+Mouse函式(紅色為更改或新增的程式)
- #include <GL/glut.h>
- float myAngle=0, oldX=0;
- void motion(int x, int y)
- {
- myAngle += (x-oldX);
- oldX=x;
- glutPostRedisplay();
- }
- void mouse(int button, int state, int x, int y)
- {
- oldX=x;
- }
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- glRotatef(myAngle,0,0,1);
- glutSolidTeapot(0.3);
- glPopMatrix();
- glutSwapBuffers();
- }
- int main(int argc, char *argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("Week04!!!");
- glutDisplayFunc(display);
- glutMotionFunc(motion);
- glutMouseFunc(mouse);
- glutMainLoop();
- }





沒有留言:
張貼留言