2019年3月14日 星期四

06160123_Week4電腦圖學

🔺電腦圖學2019-03-17 Week04

(1)主題:移動

(2)示範:GlTranslatef( x , y , z )

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

> 開啟 Window > Transformation.exe 


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




#開啟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>
void display(){

    glColor3f( 150/255.0 , 5/255.0 ,120/255.0 ); //上色
    glPushMatrix();
    glTranslatef(0.3,0.3,0);
    glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week04_06160123");
    glutDisplayFunc(display);

    glutMainLoop();
}





做一點改變,讓茶壺跟著滑鼠移動
更改程式碼:


#include <GL/glut.h>
float x=0, y=0;///滑鼠motion
void display(){
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//如果沒有這行程式就不會跟著滑鼠移動,會跑出很多茶壺,程式就會錯誤
    glPushMatrix();///備份矩陣
        glTranslatef( (x-150)/150.0, -(y-150)/150.0, 0);
        glutSolidTeapot(0.3);
        glColor3f( 150/255.0 , 5/255.0 ,120/255.0 );
    glPopMatrix();///還原矩陣,才不會再下次有舊的殘值
    glutSwapBuffers();
}

void motion(int nowX,int nowY){
    ///滑鼠motion
    x=nowX; y=nowY;///滑鼠motion
}///滑鼠motion

int main(int argc, char**argv){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week04_06160123");
    glutDisplayFunc(display);
    glutMotionFunc(motion);///滑鼠motion
    glutMainLoop();
}



#錯誤版本 (會跑出多茶壺)





#正確版本( 會跟著滑鼠移動 )
有glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

2 則留言: