STEP1:理解Rotate的原理
理解Rotate程式利用X,Y,Z軸座標轉動圖案的原理,如果換成大拇指比讚的3D圖案或許會比較助於理解喔!
STEP2:用滑鼠控制3D圖案轉動
程式碼:
float myAngle=0;///變數的宣告
const double a = myAngle;///把角度塞進去
void motion(int x,int y)///mouse motion現在動到哪裡了哩!?
{
myAngle=x;///挖ㄟ角度,丟系x的值喔
}
glutMotionFunc(motion);///加了mouse mtion的函式,可捕捉mouse的動態ㄎㄎ^^
STEP3:讓茶壺可以動出MAYA風格的轉動
程式碼:
float myAngle=0;///TODO:宣告挖ㄟ角度變數
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(myAngle,0,0,1 );///todo:對z軸做旋轉
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int oldx=0;///(0)宣告變數
void mouse(int button,int state, int x,int y)
{
if(state==GLUT_DOWN)oldx = x;///(1)按下去時要記得哩低斗!
}
void motion(int x,int y)
{
myAngle += (x-oldx);///todo:挖ㄟ角度等下拿來用
oldx = x;
display();///todo:每次做動作,就重畫畫面
}///Q:如再MAYA裡把3D模型轉動?(1)mouse按下去 (2)mouse動 (3)mouse放開
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week04 rotate");
glutMotionFunc(motion);///todo:加motion函式
glutMouseFunc(mouse);
glutDisplayFunc(display);
glutMainLoop();
}



沒有留言:
張貼留言