2020年3月25日 星期三

欸你過來一下04

老師先讓我們看看大家的回家作業 > 再來講解等等要上課的內容
旋轉角度是用(右手大拇指)安培定律作為Y軸

glRotatef的4個數字分別代表( 旋轉角度 , Y軸 , X軸 , Z軸 );


/* 再次提醒 > J個不放到桌面會開不起來 */




今天在原本freeglut的程式碼中多加上這幾行
讓物件能夠跟著mouse動ㄘ動


茶壺可以轉動了 擴
#include <GL/glut.h>
float myAngel=0;
void motion(int x, int y)
{
    myAngel = x;
    glutPostRedisplay();
}///目標:上週跟上上週的程式10行,加上剛剛練習的6行
void display()
{
    glClear( GL_COLOR_BUFFER_BIT);
    glPushMatrix();
        glRotatef(myAngel, 0,0,1);///對Z軸做旋轉
        glutSolidTeapot( 0.3 );
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char ** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week04");

    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}

讓轉動更加自然 > 新增了mouse()

#include <GL/glut.h>
float myAngel=0, oldX=0;
void motion(int x, int y)
{
    myAngel += (x-oldX);
    oldX = x;///因為x退休了
    glutPostRedisplay();
}///目標:上週跟上上週的程式10行,加上剛剛練習的6行
void mouse(int button, int state, int x, int y)
{
    oldX = x;///按下滑鼠時,會把位置記起來 -> oldX
}
void display()
{
    glClear( GL_COLOR_BUFFER_BIT);
    glPushMatrix();
        glRotatef(myAngel, 0,0,1);
        glutSolidTeapot( 0.3 );
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char ** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week04");

    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);///mouse函式-按下去就會呼叫mouse()
    glutMainLoop();
}

沒有留言:

張貼留言