200 likes | 513 Views
3D Game Programming Lab2-2D game example. Goal. Familiar with GLUT game process Load images I/O control Animation. Image examples. Read Image. RGBApixmap pic; pic.readBMPFile (“filename.bmp");. Draw image (set position). In display() 方法一 : glRasterPos2i(x, y); pic.blend (); 方法 二 :
E N D
Goal • Familiar with GLUT game process • Load images • I/O control • Animation
Read Image • RGBApixmappic; • pic.readBMPFile(“filename.bmp");
Draw image (set position) • In display() 方法一: glRasterPos2i(x, y); pic.blend(); 方法二: pic.blendTex(x, y);
Draw image (scale) void blendTex(intx, inty, float scalex=1.0f, float scaley=1.0f); • pic.blendTex(picX, picY, 2.5, 1);
Draw image (scale) void blendTex(intx, inty, float scalex=1.0f, float scaley=1.0f); • pic.blendTex(picX, picY, -1, 1);
Draw image (scale) void blendTex(intx, inty, float scalex=1.0f, float scaley=1.0f); • pic.blendTex(picX+ pic.w(), picY, -1, 1);
Draw image (rotate) void blendTexRotate (int x, int y, float scalex=1.0f, float scaley=1.0f, float angle=0.0f) 除了最後一個參數其他都與之前相同,angle為旋轉角度,會以圖片為中心的z軸作逆時針旋轉,z軸為x cross y,所以為垂直螢幕向外射出:
Draw image (rotate) void blendTexRotate (intx, inty, float scalex=1.0f, float scaley=1.0f, float angle=0.0f) • pic.blendTexRotate(x, y, 1, 1, 90); 以圖片中心點 做旋轉
Change State void SpecialKeys(intkey, int x, int y) { switch(key) { case GLUT_KEY_LEFT: picX-= 5; if (whichPic==0) whichPic=1; else whichPic=0; DirectState=1; break; case GLUT_KEY_RIGHT: picX+= 5; if (whichPic==0) whichPic=1; else whichPic=0; DirectState=0; break; } } void display() { … if (DirectState==0) {//向右 pic[whichPic].blendTex(picX, picY, 1, 1); } else {//向左 int offset = pic[whichPic].nCols;//圖的寬度 pic[whichPic].blendTex(picX+offset, picY, -1, 1); //調整x位置,並以x=0為軸翻轉影像 } … }
Time Function • void glutTimerFunc(unsigned int msecs, void (*func)(int value), int value); 時間間隔 (milliseconds) 要呼叫的函式名稱 要傳入函式的變數
Time Function 使用時呼叫 jump(0) • glutTimerFunc( A, B, C ); • A = 時間間隔 (milliseconds) • B= 要呼叫的Function • C= 要傳入的變數 • GLUT每隔A毫秒 就會呼叫一次B(C)函式 • 以jump function為例: • glutTimerFunc(100,jump,i); • 表示每隔100單位時間,呼叫一次 jump (i)
Practice • Back and Forth n/2 steps n steps change DirectState n/2 steps
Challenge • Fly y = H * sin (x) H W
Super Challenge • Fly Randomly y = 50 * sin (x) + (rand() % maxChange)
去背製作範例 • 首先找一張將背景透明的圖片(檔案格式為 .gif 或 .png),接著用Photoshop等繪圖軟體把該圖片與我們選的特定顏色之背景做結合,以下我們用淡藍色(232, 248, 248)舉例: