1.主題: Rotate旋轉
https://jsyeh.org/3dcg10/ 下載
data.zip =>下載\windows\data\一堆3D模型
windows.zip=>下載\windows\transformation.exe
今天教旋轉,旋轉方向以旋轉軸為中心逆時鐘,可以用安培用手定則來理解
X軸為旋轉軸
Z軸為旋轉軸(與畫面垂直,雇用一點呈現
開新專案
命名week04_mouse_motion
打下列程式碼:
///
#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();
}
///
但有一個問題,每次滑鼠在移動茶壺的時候,都會重置最原本的座標
所以要改動
///
#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);
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();
}
///
沒有留言:
張貼留言