2022年3月21日 星期一

09160074 week05

 ●3/21 主題Rotate

 ★安培右手定則

    旋轉軸就像買鹹蘇雞的竹籤,我們研究了 0,1,0 的轉動, 研究了 0,-1,0 的轉動,研究了 1,0,0的轉動,也研究了0,0,1的轉動,總之,利用神奇的安培右手,就可以知道東西是怎麼繞軸轉動
X軸
Y軸
Z軸
接下來是奇怪的旋轉軸1,1,0,,是有點斜的軸,那它會怎麼轉呢 就像當兵背值星帶斜斜的

●旋轉茶壺

 接續上次茶壺程式碼,把Translated改成Rotatef
    利用 glutMotionFunc()把mouse motion對應的函式準備好void motion(int x, int y) 會有mouse在動的時候的座標。修改好angle後,便再呼叫一次display()去照著angle來轉動
●程式碼

#include <GL/glut.h>
float angle=0;
void display()
    {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glPushMatrix();
                glRotated(angle,0,0,1);
                glColor3f(1,1,0);
                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("week05_Rotate");
            glutDisplayFunc(display);
            glutMotionFunc(motion);
            glutMainLoop();
            return 0;
    }

剛剛的motion()在轉動時,有點不連續,因為它只是讓angle=x做瞬間動作。想要做出比較好的連續動作,需要用到一個冷笑話「把大象放到冰箱裡去」把冰箱門打開、大象放進去、把冰箱門關起來。利用mouse()及motion()函式,配合 angle += x-oldX知道mouse多移動多少
●●程式碼
#include <GL/glut.h>
float angle=0,oldx=0;
void display()
    {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glPushMatrix();
                glRotated(angle,0,0,1);
                glColor3f(1,1,0);
                glutSolidTeapot( 0.3 );
            glPopMatrix();
            glutSwapBuffers();
    }
void mouse(int button,int state,int x,int y)
{
    oldx=x;
}
void motion(int x,int y)
{
    angle+=(x-oldx);
    oldx=x;
    display();
}
int main(int argc, char**argv)
    {
            glutInit( &argc,argv );
            glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
            glutCreateWindow("week05_Rotate");
            glutDisplayFunc(display);
            glutMouseFunc(mouse);
            glutMotionFunc(motion);
            glutMainLoop();
            return 0;
    }

●滑鼠事件進階



沒有留言:

張貼留言