移動/旋轉/縮放與矩陣(Matrix)
從網址下載: sorce , date , win32 ,glut32.dll ( jsyeh.org/3dcg10 )

解壓縮win32
把 data 打開,將裡面的資料夾複製到上面解壓縮後的window32資料夾
再將 glut32.dll 移至window32資料夾

按右鍵 點選Swap translate/rotate

glRotatef和glTranslatef作交換

看看差別在哪裡

差異:點選後的Translatef在下,旋轉的範圍較大,像是整個車子在鍋子裡,整個鍋子一起轉
(前者,以車子的中心旋轉,後者,車子繞著鍋子中心轉)。
課堂作業二
使茶壺可以左右轉



程式碼:
#include <GL/glut.h>
float rotX=0;
void display()
{
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(rotX,0,1,0);
glColor3f(1,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)
{
rotX=x;
display();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("hello3D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
課堂問題
Q1; 什麼是glPushMatrix();
glPopMatrix();
A:
和rush pop相同意義,push是儲存資料 pop則是提出資料互為相反的涵式
Q2: 什麼是 Buffers
Double
A:
buffer是電腦繪圖緩衝區,怕圖形呈現時不完整,所以在圖片顯示前會先在這繪製好圖形再一起呈現
double-buffer即為兩次緩衝,較為保險
使茶壺不只能單水平旋轉或垂直旋轉

程式碼:
#include <GL/glut.h>
float rotX=0,rotY=0; //同時能轉X也能轉Y
void display()
{
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(rotX,0,1,0);
glRotatef(rotY,1,0,0); //轉動X軸
glColor3f(1,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
rotX=x;
rotY=y; //代入滑鼠y值移動量
display();
}
int main (int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(400,400); //視窗大小glutInitWindowPosition(700,0); //視窗跳出之位置
glutCreateWindow("hello3D");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
沒有留言:
張貼留言