2016年3月21日 星期一

week05

本周主題:
(1)旋轉 移動
(2)mouse 旋轉
(3)階層式旋轉
(4)階層式移動

網址:jsyeh.org/3dcg10


下載data.zip & window.zip & glut32.dll(上周相同)



開看看transformation 試試看
(按右鍵可以reset~~)
translate 移動
rotate 旋轉
scale 縮放



讓茶壺水平旋轉
先打開openGL


freeglut 並複製網址


打上程式碼
#include <GL/glut.h>
float rotX=0;
void display()
{
    glClearColor(1,1,1,1);
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();///備份Matrix (Matrix裡有translate, rotate, scale的量值)
        glRotatef(rotX, 0,1,0);
        glColor3f(1,0,0);
        glutSolidTeapot(0.3);
    glPopMatrix();///還原Matrix 回到剛剛push的樣子
    glutSwapBuffers();
}
void motion(int x,  int y)
{
    rotX=x;
    display();///glutPostRedisplay();
}
int main(int argc,  char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);///double buffers兩倍記憶體
    glutCreateWindow("hello 3D");

    glutDisplayFunc(display);
    glutMotionFunc(motion);

    glutMainLoop();
}
得到水平茶壺~~

讓茶壺垂直水平旋轉
改程式碼(加上rotY)
#include <GL/glut.h>
float rotX=0,   rotY=0;
void display()
{
    glClearColor(1,1,1,1);
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();///備份Matrix (Matrix裡有translate, rotate, scale的量值)
        glRotatef(rotY, 1,0,0);///Y的移動量,讓他對X軸轉
        glRotatef(rotX, 0,1,0);///X的移動量,讓他對Y軸轉
        glColor3f(1,0,0);
        glutSolidTeapot(0.3);
    glPopMatrix();///還原Matrix 回到剛剛push的樣子
    glutSwapBuffers();
}
void motion(int x,  int y)
{
    rotX=x;  rotY=y;///同時拿來用
    display();///glutPostRedisplay();
}
int main(int argc,  char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);///double buffers兩倍記憶體
    glutInitWindowSize(300,300); glutInitWindowPosition(600,0);
    glutCreateWindow("hello 3D");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}

得到可以水平垂直的茶壺~~

沒有留言:

張貼留言