2019年3月21日 星期四

彥の圖學筆記 Week05

電腦圖學

2019-03-21 Week05

(1) 主題: 旋轉 Rotation


每次上課前必做的準備!!!!
進入小葉老師的網站下載範例(http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/)

> 下載05/04的Examples: [data]、[win32]、glut32.dll三個連結 > 將[win32]解壓縮
> 把[data]、glut32.dll、丟進[win32]的資料夾裡 > 點開範例Transformation.exe
(點橘色超連結字體下載)


(2) 實作: glRotatef()

程式碼(物件自動旋轉)

#include <GL/glut.h>
float angle=0;///今天新教的!!!!
void display()
{///沒有使用 glClear() 去清畫面,會有殘影...
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///Push備份保護Matrix
        glRotatef(angle, 0, 1, 0);///今天新教的!!!! 對Y軸,轉angle度
        glutSolidTeapot(0.3);
    glPopMatrix();///Pop還原Matrix
    glutSwapBuffers();
    angle+=3;///今天新教的!!!! 每次執行到這行,加一點點
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week05 rotate");
    glutDisplayFunc(display);
    glutIdleFunc(display);///今天新教的!!!! Idle閒閒沒事,就重畫
    glutMainLoop();
}



(3) 滑鼠旋轉、自動旋轉

程式碼(物件可使用滑鼠旋轉)

#include <GL/glut.h>
float angle=0;///今天新教的!!!!
void display()
{///沒有使用 glClear() 去清畫面,會有殘影...
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///Push備份保護Matrix
        glRotatef(angle, 0, 1, 0);///今天新教的!!!! 對Y軸,轉angle度
        glutSolidTeapot(0.3);
    glPopMatrix();///Pop還原Matrix
    glutSwapBuffers();
    ///angle+=3;//////今天新教的!!!! 每次執行到這行,加一點點
}
void motion(int x, int y)///現在要新加的!!!!
{///當你的mouse去drag作motion動作時,
    angle=x;///現在要新加的!!!!
    display();///現在要新加的!!!!
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week05 rotate");
    glutDisplayFunc(display);
    glutIdleFunc(display);///今天新教的!!!! Idle閒閒沒事,就重畫
    glutMotionFunc(motion);///現在要新加的!!!!
    glutMainLoop();///如果忘了寫 glutMainLoop() 或是忘了寫圓括號() 就會離開
}
程式碼(查看滑鼠鍵位數值)

#include <GL/glut.h>
float angle=0;///今天新教的!!!!
void display()
{///沒有使用 glClear() 去清畫面,會有殘影...
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///Push備份保護Matrix
        glRotatef(angle, 0, 1, 0);///今天新教的!!!! 對Y軸,轉angle度
        glutSolidTeapot(0.3);
    glPopMatrix();///Pop還原Matrix
    glutSwapBuffers();
    ///angle+=3;//////今天新教的!!!! 每次執行到這行,加一點點
}
void motion(int x, int y)///現在要新加的!!!!
{///當你的mouse去drag作motion動作時,
    angle=x;///現在要新加的!!!!
    display();///現在要新加的!!!!
}
#include <stdio.h>
void mouse(int button, int state, int x, int y)
{
    printf("%d %d %d %d\n", button, state, x, y);
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week05 rotate");
    glutDisplayFunc(display);
    glutIdleFunc(display);///今天新教的!!!! Idle閒閒沒事,就重畫
    glutMotionFunc(motion);///現在要新加的!!!!
    glutMouseFunc(mouse);
    glutMainLoop();///如果忘了寫 glutMainLoop() 或是忘了寫圓括號() 就會離開
}

沒有留言:

張貼留言