下載 "data"、"win32" 及 "glut32.dll" 至桌面解壓縮。
打開 Transformation
Translate 移動
Rotate 旋轉
Scale 縮放
在黑色部分點選右鍵,點 [S] Swap translate/rotate (旋轉跟移動交換)
點選後 Translate ---> Rotate
Rotate Translate

沒交換前是在原地轉(圍繞著車子),交換後會在右邊(以往右邊為例子)旋轉--(連車子一起轉圈)。
課堂作業二
練習 : 茶壺旋轉 (用滑鼠來喔~~)
1. 開啟GLUT專案
2. 程式碼 :
#include <GL/glut.h>
float rotX=0;
void display()
{
glClearColor(1,1,1,1); //白色背景
glClear(GL_COLOR_BUFFER_BIT); //清背景
glPushMatrix(); //備份Matrix (Matrix裡面還會有translate, rotate, scale的量值)
glRotatef(rotX, 0,1,0); //X的移動量,讓它對Y軸旋轉
glColor3f(1,0,0);
glutSolidTeapot(0.3);
glPopMatrix(); //還原Matrix (回到剛剛push時的樣子) *push -->記住
glutSwapBuffers();
}
void motion(int x, int y)
{
rotX=x; //拿來旋轉 (角度)
display(); //gultPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); //double buffers兩倍記憶體
glutCreateWindow("hello3D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
結果如下 :
Q1. 什麼是 glPushMatrix();
glPopMatrix(); ?
A1: glPushMatrix(); 是記住自己的位置。
glPopMatrix(); 是回到之前記住的位置。
Q2. 什麼是 Buffer ? Double ?
A2: Buffer像是記憶體一樣,Double是雙倍的,Double Buffer在這例指的是雙倍記憶體,會先把輸入的先"記起來"不顯示,等到執行的時候再一起顯示出來。
課堂作業三
增強版本 :
#include <GL/glut.h>
float rotX=0, rotY=0;
void display()
{
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(rotY, 1,0,0); //Y的移動量,讓它對X軸旋轉
glRotatef(rotX, 0,1,0); //X的移動量,讓它對Y軸旋轉
glColor3f(1,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)
{
rotX=x; rotY=y; //拿來旋轉 (角度) , 同時拿來用。
display();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(300,300); glutInitWindowPosition(600,0); //調整視窗大小 *記得要在glutCreateWindow的上面 。
glutCreateWindow("hello3D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}

沒有留言:
張貼留言