2018年3月19日 星期一

WEEK04-移動.旋轉.縮放

1.試 glRoatatef (角度, x軸, y軸, z軸)
       (OpenGL)(float)      ↳     ↳      ↳各種旋轉軸
















2.期中考考題
























3.練習旋轉的程式
























#include <stdio.h>
#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///備份矩陣
        glRotatef(angle, 0,0,1);///旋轉angle度
        glutSolidTeapot(0.3);
    glPopMatrix();///還原矩陣
    glutSwapBuffers();
    printf("%.1f\n", angle);
    angle++;///每次角度加1
}
int main(int argc, char*argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week04 transformation");
    glutIdleFunc(display);///很閒/Idle,去重畫
    glutDisplayFunc(display);
    glutMainLoop();

}



4.利用滑鼠拖曳,旋轉
























#include <stdio.h>
#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///備份矩陣
        glRotatef(angle, 0,0,1);///旋轉angle度
        glutSolidTeapot(0.3);
    glPopMatrix();///還原矩陣
    glutSwapBuffers();
    ///printf("%.1f\n", angle);
    ///angle++;///每次角度加1
}
void mouse(int button, int state, int x, int y)
{

}
void motion(int x, int y)
{///Now: 當mouse在拖曳motion時,
    angle=x;///Now: 去改變angle值
    glutPostRedisplay();///請重面重畫
}
int main(int argc, char*argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week04 transformation");
    glutMouseFunc(mouse);///Now: mouse
    glutMotionFunc(motion);///Now: motion
    ///Now: glutIdleFunc(display);///很閒/Idle,去重畫
    glutDisplayFunc(display);
    glutMainLoop();
}

沒有留言:

張貼留言