2019年3月14日 星期四

電腦圖學 Week04

電腦圖學

  1. 主題:移動
  2. 示範:glTranslatef(x,y,z)
  3. 主題:滑鼠

TODO:www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10
下載:windows.zip 下載\windows\Transformation.exedata.zip 
下載\windows\data\模型glut32.dll 
下載\windows\glut32.dll

先去網站下載data,win32,glut32.dll


然後解壓縮把data放進去windows裡面


然後開啟Transformation.exe



開啟CodeBlocks GLUT
Download Freeglut3.0.0 MSVC Package
將檔案解壓縮後複製一個libfreeglut.a檔並重新命名為libglut32.a
開啟Code Blocks>新增一個新的專案>點選Glut 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_06160326");
    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_06160326");
    glutDisplayFunc(display);
    glutMotionFunc(motion);///滑鼠motion
    glutMainLoop();
}


加glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

沒有留言:

張貼留言