2. 如何用滑鼠移動物體
1. 打開GLUT專案
2. 打開此專案的 main.cpp
3. 程式碼分析 (在display函式裡)
glPushMatrix();
glTranslated(-2.4,1.2,-6); ///d : double
glRotated(60,1,0,0); ///對 1,0,0 (旋轉軸 x軸)轉 60度
glRotated(a,0,0,1); ///對 0,0,1 (旋轉軸 z軸)一直旋轉 a度
glColor3f(1,1,0);
glutSolidTeapot(1);
glutSolidSphere(1,slices,stacks); ///畫圖函式
glPopMatrix();


5. 加上茶壺

移動/轉動 會套用到後面的全部物件
移動
glTranslated(-2.4,1.2,-6); ///d : double
旋轉
6. 改變旋轉軸

補充 : 右手座標系統 從銀幕射出來 右手逆時針轉
不同旋轉比較
不同旋轉軸

同旋轉軸,但不同方向

非x, y, z 旋轉軸
縮放
放大 縮小

不等比例放大/縮小

Translation matrix

Push
備份矩陣
Pop
還原矩陣
如何用滑鼠移動
#include <GL/glut.h>
#include <stdio.h>
float dx, dy, dz=0;
int oldX, oldY, oldZ=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(dx, dy, dz); ///移動
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers(); /// vs. glFlush();
}
void motion(int x, int y) ///用來拖曳移動時
{
dx += (x-oldX)/150.0;
dy += -(y-oldY)/150.0; ///座標上下顛倒加負號
oldX = x;
oldY = y;
glutPostRedisplay(); ///貼便利貼 請電腦要重畫畫面
}
void mouse(int button, int state, int x, int y)
{
if(state==GLUT_DOWN){oldX=x; oldY=y;} ///滑鼠按下去
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); ///double配buffer single配flush
glutCreateWindow("week04");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}

用滑鼠拖曳茶壺
沒有留言:
張貼留言