2019年3月21日 星期四

06160123_Week5電腦圖學

🔺電腦圖學2019-03-21 Week05

(1)主題:旋轉Rotation

(2)實作:GlRotatef( angle , x , y , z )

(3)滑鼠轉,模型轉



TODO:點名:Veyon Config > 服務 >啟動服務
TODO:到小葉老師的網站3D Graphics download資料
download:window.zip  解壓縮 至window 
                    data.zip    解壓縮 data拉至window
                   glut32.dll   解壓縮 glut32.dll移至window資料夾

> 開啟 Window > Transformation.exe 


       
> 開啟後會看見預設的模型Porsche ( 點按右鍵可更換model )


glRotate( angle , x , y , z  )的原則就像安培右手定理



#開啟CodeBlocks GLUT

1、Search Freeglut > Download Freeglut3.0.0 MSVC Package Download freeglut 3.0.0 for MinGW

2、將檔案解壓縮後複製一個libfreeglut.a檔並重新命名為libglut32.a

3、開啟Code Blocks>新增一個新的專案>點選Glut project

4、命名專案>選擇資料存放區(C:\Users\user\Downloads\freeglut\lib)>Finish

5、於左側project開啟程式碼>執行


把程式碼全部刪光光


-

打上 新的程式碼:

>「讓茶壺自轉」

#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);//今天新教的!!!!Idex閒閒沒事,就重畫
    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();
    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);//今天新教的!!!!Idex閒閒沒事,就重畫
    glutMotionFunc(motion);//現在要加新的!!!!
    glutMainLoop();//如果忘了寫glutMainLoop() 或是忘了寫圓括號(),就會離開

}




-

新增( 紅色標示 )及修改( 刪除線標示 )一些程式碼,讓茶壺點擊周圍使小黑視窗顯示坐標及所使用之滑鼠鍵

左鍵 : 0
中鍵 : 1
右鍵 : 2
中鍵前推 : 3
中鍵後推 : 4

#include <GL/glut.h>
#include <stdio.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();
    glutSwapBuffers();
}
void  motion(int x,int y){///現在要加新的!!!!
///當你的mouse去做drag作motion動作時,
    angle=x;///現在要加新的!!!!(2)mouse移動
    display();///現在要加新的!!!!
}
void mouse(int button , int state , int x,int y){///現在要加新的!!!!
///要把大項放進冰箱:(1)mouse按下去 (3)mouse放開來
    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);///今天新教的!!!!Idex閒閒沒事,就重畫
    glutMotionFunc(motion);///現在要加新的!!!!
    glutMouseFunc(mouse);///現在要加新的!!!!

    glutMainLoop();///如果忘了寫glutMainLoop() 或是忘了寫圓括號(),就會離開
}


沒有留言:

張貼留言