2020年3月25日 星期三

鄧芷豪你不要把安培的右手打斷好嗎

Transformasion


安培右手定理
右手大拇指是軸
物件會往手掌方向旋轉

讓茶壺旋轉
#include <GL/glut.h>

float myAngle=0;
void motion(int x,int y)
{
    myAngle = x;
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(myAngle,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);
    glutMainLoop();
}



因為旋轉不會從上一次旋轉結束的位置開始,所以稍微修改一下


#include <GL/glut.h>

float myAngle=0,oldX=0;
void motion(int x,int y)
{
    myAngle += (x-oldX);
    oldX=x;
    glutPostRedisplay();
}
void mouse(int button,int state,int x,int y)
{
    oldX=x;
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(myAngle,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);
    glutMainLoop();
}

沒有留言:

張貼留言