2022/3/15 第四周
主題:旋轉
1.下載/執行範例
(1) https://jsyeh.org/3dcg10/ ,下載win32、data 並且解壓縮
(2) 執行transformation.exe : 右上角按右鍵可以換模型、正下方拉滑鼠上下可以調整數值
glRotatef( 角度, x , y , z )

@大拇哥是軸 , 右手大拇哥以外的四隻指頭,決定旋轉方向
ex1:
(數字是正的)
旋轉軸y的方向(大拇哥往上指) : 車頭向右、左轉
(數字是負的)
旋轉軸y的方向(大拇哥往下指) : 車頭向左、右轉
ex2:
(數字是正的)
旋轉軸x的方向(大拇哥往右邊指) : 車身向前、後倒
(數字是負的)
旋轉軸x的方向(大拇哥往左邊指) : 車身後倒、向前
ex3:
(數字是正的)
旋轉軸z的方向(大拇哥往自己指) : 以車身中心 向左、右轉
(數字是負的)
旋轉軸z的方向(大拇哥往螢幕指) : 以車身中心 向右、左轉
2.寫z軸程式
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(90,0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week04 Rotate");
glutDisplayFunc(display);
glutMainLoop();
}
3.滑鼠左右拉可以旋轉茶壺 (設一個angle變數)
#include <GL/glut.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();///備份矩陣
glRotatef(angle,0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();
}
void motion(int x, int y)
{
angle = x;
display();///重畫畫面
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week04 Rotate");
glutDisplayFunc(display);
glutMotionFunc(motion);///mouse motion 動
glutMainLoop();
}
4.將滑鼠點下去的x的值定錨! (因為x值一直改變,所以會亂跳)#include <GL/glut.h>
float angle=0 ,oldx=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();///備份矩陣
glRotatef(angle,0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
glutSwapBuffers();
}
void motion(int x, int y)
{
angle += (x-oldx); ///angle加上剛剛點的x值減掉舊的x值
oldx=x;
display();///重畫畫面
}
void mouse(int button, int state, int x, int y)
{
oldx=x;///定錨
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week04 Rotate");
glutDisplayFunc(display);
glutMotionFunc(motion);///mouse motion 動
glutMouseFunc(mouse);///mouse按下去、放開來
glutMainLoop();
}
沒有留言:
張貼留言