2020年3月25日 星期三

ELF

Transformation.exe

glTranslatef    移動     (x,y,z)
    glRotatef    旋轉     (旋轉量,x,y,z)
      glScalef    縮放     (x,y,z)

glRotatef適用安培右手定則

右手比讚,大拇指指向軸的方向
四指旋轉的方向 ➜ glRotate的旋轉方向

glut預設函式,改成滑鼠轉動X軸旋轉






(無修正)寫出茶壺,讓滑鼠X軸移動時,茶壺對Z軸做旋轉

#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_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week04");
    glutDisplayFunc(display);

    glutMotionFunc(motion);


    glutMainLoop();


}


有Bug,同一個座標位置,茶壺旋轉位置一樣。





(有修正)寫出茶壺,讓滑鼠X軸移動時,茶壺對Z軸做旋轉


#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_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week04");
    glutDisplayFunc(display);

    glutMotionFunc(motion);
    glutMouseFunc(mouse);

    glutMainLoop();


}



設定oldX紀錄舊的移動結束座標位置,
讓程式從上一個座標開始移動在加上後來的移動,
就不會有Bug。



沒有留言:

張貼留言