http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
照前幾個禮拜做的,下載win32/data/glut.dll並解壓縮

點開Transformation,右鍵可選擇不同物體
今天要教的是旋轉,透過安培右手定則了解旋轉規律

數值改變代表的意義

2.改寫程式
再利用前幾周教的重寫一個簡易版
#include <GL/glut.h>
float myAngle=0;///宣告角度變數
void motion(int x,int y)///TODO:滑鼠在動時有座標值
{glutMotionFunc(motion);
myAngle=x;///讓滑鼠的x座標成為旋轉軸
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);///要記得清不然會有殘影
glPushMatrix();
glRotatef(myAngle,0,0,1);///對z軸做旋轉
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week04");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
呈現出來的結果:

把程式轉動的改得更順暢
///如何轉動物體:1.滑鼠按下去 2.移動滑鼠 3.放開滑鼠
#include <GL/glut.h>
float myAngle=0,oldX=0;///宣告角度變數
void motion(int x,int y)///TODO:滑鼠在動時有座標值
{glutMotionFunc(motion);
myAngle+=(x-oldX);///myAngle=x;///讓滑鼠的x座標成為旋轉軸
oldX=x;///x變成轉動基準
glutPostRedisplay();
}
void mouse(int button,int state,int x,int y)
{
oldX=x;///按下去時把x位子記下來,讓x取代oldX
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(myAngle,0,0,1);///對z軸做旋轉
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Week04");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);///註冊mouse函式,按下去呼叫mouse()
glutMainLoop();
}

沒有留言:
張貼留言