3D Game Programming Lab2-2D game example

20
3D Game Programming Lab2-2D game example

description

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 (); 方法 二 : - PowerPoint PPT Presentation

Transcript of 3D Game Programming Lab2-2D game example

Page 1: 3D Game  Programming Lab2-2D game example

3D Game ProgrammingLab2-2D game example

Page 2: 3D Game  Programming Lab2-2D game example

Goal

• Familiar with GLUT game process• Load images• I/O control• Animation

Page 3: 3D Game  Programming Lab2-2D game example

Image examples

stand walk jump sun1 sun2

flyup flydown cat1 cat2

Page 4: 3D Game  Programming Lab2-2D game example

Read Image

• RGBApixmap pic;• pic.readBMPFile(“filename.bmp");

Page 5: 3D Game  Programming Lab2-2D game example

Draw image (set position)

• In display()方法一 :

glRasterPos2i(x, y);pic.blend();

方法二 :pic.blendTex(x, y);

Page 6: 3D Game  Programming Lab2-2D game example

Draw image (scale)void blendTex(int x, int y, float scalex=1.0f, float scaley=1.0f);

• pic.blendTex(picX, picY, 2.5, 1);

Page 7: 3D Game  Programming Lab2-2D game example

Draw image (scale)void blendTex(int x, int y, float scalex=1.0f, float scaley=1.0f);

• pic.blendTex(picX, picY, -1, 1);

Page 8: 3D Game  Programming Lab2-2D game example

Draw image (scale)void blendTex(int x, int y, float scalex=1.0f, float scaley=1.0f);

• pic.blendTex(picX + pic.w(), picY, -1, 1);

Page 9: 3D Game  Programming Lab2-2D game example

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 ,所以為垂直螢幕向外射出 :

Page 10: 3D Game  Programming Lab2-2D game example

Draw image (rotate)void blendTexRotate(int x, int y, float scalex=1.0f, float scaley=1.0f, float angle=0.0f)

• pic.blendTexRotate(x, y, 1, 1, 90);

以圖片中心點做旋轉

Page 11: 3D Game  Programming Lab2-2D game example

Change State

Page 12: 3D Game  Programming Lab2-2D game example

Change Statevoid SpecialKeys(int key, 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 為軸翻轉影像   } …}

Page 13: 3D Game  Programming Lab2-2D game example

Time Function• void glutTimerFunc(unsigned int msecs, void (*func)(int value), int value);

時間間隔 (milliseconds)

要呼叫的函式名稱

要傳入函式的變數

Page 14: 3D Game  Programming Lab2-2D game example

Time FunctionglutTimerFunc( A, B, C );(1)A = 時間間隔

(milliseconds)

(2)B = 要呼叫的Function

(3)C = 要傳入的變數 GLUT 每隔 A 毫秒 就會

呼叫一次 B(C) 函式

以 jump function 為例:glutTimerFunc(100,ju

mp,i); 表示每隔 100 單位時間,

呼叫一次 jump (i)

使用時呼叫 jump(0)

Page 15: 3D Game  Programming Lab2-2D game example

Practice

• Back and Forth

n steps

n/2 steps

n/2 steps changeDirectState

Page 16: 3D Game  Programming Lab2-2D game example

Challenge

• Fly

W

H

y = H * sin (x)

atvv

vtpp

at

ttt

tttt

an cceleratio force and epFor timest

Page 17: 3D Game  Programming Lab2-2D game example

Super Challenge

• Fly Randomly

y = 50 * sin (x) + (rand() % maxChange)

Page 18: 3D Game  Programming Lab2-2D game example

將指定的顏色變為透明色去除圖片的背景

Page 19: 3D Game  Programming Lab2-2D game example

去背製作範例• 首先找一張將背景透明的圖片 ( 檔案格式為 .gif

或 .png ) ,接著用 Photoshop 等繪圖軟體把該圖片與我們選的特定顏色之背景做結合,以下我們用淡藍色 (232, 248, 248) 舉例:

Page 20: 3D Game  Programming Lab2-2D game example

         Q&A