Top Banner
Do it yourself Do it yourself 3D Games with 3D Games with OpenGL OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email: [email protected] Web: http://www.iit.bme.hu/~szirmay
79

Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Do it yourselfDo it yourself3D Games with OpenGL3D Games with OpenGL

Szirmay-Kalos László

Dept. of Control Engineering and Information Technology

Budapest University of Technology

email: [email protected]

Web: http://www.iit.bme.hu/~szirmay

Page 2: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Demo 1: The goalDemo 1: The goal

sgame/SpaceGame I

Page 3: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

3D 3D GamesGames

Self EnemyAI

Field objects

Page 4: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Virtual realityVirtual reality

virtual worldavatar

control

rendering interaction

Page 5: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Subtasks of GamesSubtasks of Games

Rendering the scene from the point of view of the avatar (virtual self)

Controlling the avatar through the input devices (keyboard, mouse)

Controlling the „intelligent” virtual objects (AI)

Simulating the physical world

Page 6: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Libraries for input and outputLibraries for input and output

rendering

virtual worldmodell

input

simulation

OpenGL

Windows + GLUT

Page 7: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Interaction schemesInteraction schemes

printfscanf

printfscanf

var1

var2

Program driven:event - variable correspondance: PC

event

state

Eventhandler

Event driven:event - variablecorrespondance: state

Page 8: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Input/Output managementInput/Output management

Operatingsystem

WindowsGLUT

main

DisplayFunc

KeyboadFunc

IdleFunc

OpenGLgraphicshardware

application

initializationcallback registration

callbacksSpecialFunc

Page 9: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

OpenGLOpenGL

Rendering API (library)– Rendering of 2D and 3D objects– follows the drawing state concept– Image operations

Independent of the windowing and operation systems– Window management should be done

separately

Page 10: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

glX, wgl glu

OpenGLOpenGL

hw

application

gl

GLUT

Xwindow, MS-Windows

Window system

Windowingbridge

Convenience,tessellators

Window management

Page 11: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

OpenGL syntax (C API)OpenGL syntax (C API)

glVertex3dv( … )

# of parameters2 - (x, y)3 - (x, y, z), (R, G, B)4 - (x, y, z, h) (R, G, B, A)

Data type b - byteub - unsigned bytes - shorti - intf - floatd - double

Vector or scalarv - vector - scalar

part of OpenGL API

Page 12: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

InitializationInitialization

#include <GL/gl.h>#include <GL/glu.h>#include <GL/glut.h>

void main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitWindowSize(600, 600); // initialization glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH); glutCreateWindow( "Space Game" );

glutDisplayFunc( DisplayFunc ); // callback registration glutIdleFunc( IdleFunc ); glutKeyboardFunc( KeyboardFunc ); glutSpecialFunc( SpecialKeyboardFunc ); glutMainLoop();}

R,G,B - R,G,B - Z

Page 13: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Demo 2: Demo 2: Single and double bufferingSingle and double buffering

single:sgame/Files/sgamesing.exe

double: sgame/Files/sgame.exe

Page 14: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Double buffering for animationDouble buffering for animation

frame buffer

1.

frame buffer

2.

monitor

glClear(GL_COLOR_BUFFER_BIT);drawing…glutSwapBuffers( );

draw

display

Page 15: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Rendering with OpenGLRendering with OpenGL

world definition modell-view transformperspective transform

1325628 1325628

1.2.

clipping z-buffering display

Page 16: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Why do we need Why do we need perspective transform?perspective transform?

What is seen in a pixel?

perspective transform

x1z1

x2z2

X = d = d

y1z1

y2z2Y = d = d

X = x1 = x2

Y = y1 = y2

Page 17: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Definition of transformationsDefinition of transformations

Tmodell Tview Tperspective

Tmodellview

xyz

XYZ

move, rotatescaleobjects

eye positionviewing directionview up

field of viewaspect ratiofront-back clipping

TviewportXP

YP

Page 18: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Elementary transformationsElementary transformations glTranslate: r’ = r + p glScale: x’= Sx x; y’= Sy y; z’ = Sz z

glRotate (ex. Rotation around axis z):

r’ = rSx 0 0

0 Sy 0

0 0 Sz

r’ = rcos sin

-sin cos

Page 19: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Homogeneous coordinatesHomogeneous coordinates Translation cannot fit into a 3x3 matrix

– Let us work with 4x4 matrices

[r’, 1] = [r, 1] = [r A + p, 1]

a11 a12 a13 0a21 a22 a23 0

a31 a32 a33 0

p1 p2 p3 1

A

p

[r’,1] = (...([r,1] Tn) Tn-1)... T1) = [r,1] (TnTn-1... T1)

Page 20: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Definition of transformationsDefinition of transformations

world definition

glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION); glLoadIdentity( );gluPerspective(fov,w/h,front,back);

glMatrixMode(GL_MODELVIEW); glLoadIdentity( );gluLookAt(eyex, eyey, eyez,

lookatx, lookaty, lookatz, vupx, vupy, vupz);

glTranslatef(px, py, pz); glRotatef(angle, axx, axy, axz);

eye

lookat

fov

w

h

w

h

Page 21: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Scene Definition OpenGLScene Definition OpenGLglBegin(GL_TRIANGLES);

glColor3f( 1, 1, 0 ); glVertex3f( x11, y11, z11 );glVertex3f( x12, y12, z12 );glVertex3f( x13, y13, z13 );

glColor3f( 0, 1, 0 ); glVertex3f( x21, y21, z21 );glVertex3f( x22, y22, z22 );glVertex3f( x23, y23, z23 );

glEnd();

x11,y11,z11

x12,y12,z12

x13,y13,z13

glColor4f(R,G,B,A)

Page 22: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

TexturingTexturing

Page 23: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Texture Mapping in OpenGLTexture Mapping in OpenGL

glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, texture_id); // which textureglBegin(GL_TRIANGLES);glTexCoord2f(u1, v1); glVertex3f(x1, y1, z1);glTexCoord2f(u2, v2); glVertex3f(x2, y2, z2);glTexCoord2f(u3, v3); glVertex3f(x3, y3, z3);glEnd();glDisable(GL_TEXTURE_2D);

(u1, v1)

(u2, v2)

(u3, v3)

x1,y1,z1

x2,y2,z2

x3,y3,z3

Page 24: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Definition of TexturesDefinition of TexturesglEnable(GL_TEXTURE_2D);glGenTextures(1, &texture_id);glBindTexture(GL_TEXTURE_2D, texture_id);

// load imageint width, height;unsigned char * image;image = LoadImage( filename, &width, &height );

// store textureglTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image );

Page 25: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Images, image formatsImages, image formats Head:

– type, size (width, height) – bits-per-pixel, indexed-true color, lookup table

Body:– width x height number of pixels: (R,G,B) or idx– Encoding, compression (run length, LZW, Huffmann,

FFT, wavelet) Standard formats:

– TARGA, BMP, GIF, JPEG, TIFF, PCX

Page 26: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

TARGA (.TGA)TARGA (.TGA)002

9 zeros

lower byte of widthupper byte of widthlower byte of heightupper byte of height

Bits-per-pixel: 2432

Body

Head: Body: b1g1r1

b2g2r2

Page 27: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Definition of surfaces as Definition of surfaces as triangle meshes: Tessellationtriangle meshes: Tessellation1. Find a parametric equation:

x(u,v) = x0 + r cos 2u sin v y(u,v) = y0 + r sin 2u sin vz(u,v) = z0 + r cos v u,v [0,1]

2. Select points in the unit rectangle

Page 28: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Quadrics: SphereQuadrics: Sphere// definitionGLUquadricObj * quadric;quadric = gluNewQuadric( );gluQuadricTexture(quadric, GL_TRUE);

// drawglBindTexture(GL_TEXTURE_2D, texture_id);gluSphere(quadric, R, 16, 10);

Page 29: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Rotating Earth: initRotating Earth: initGLUquadricObj * quadric;unsigned int earth_texture;

void Init( ) { quadric = gluNewQuadric( ); gluQuadricTexture(quadric, GL_TRUE);

glGenTextures(1, &earth_texture); glBindTexture(GL_TEXTURE_2D, earth_texture_id); glTexParameteri(GL_TEXTURE_2D,

GL_TEXTURE_MIN_FILTER, GL_NEAREST);

image = LoadImage(”earth.tga”, &width, &height ); glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);}

Page 30: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

GLUT timer controlGLUT timer controllong time;

void IdleFunc( ) { // idle call back

float old_time = time; time = glutGet( GLUT_ELAPSED_TIME );

float dt = time - old_time;

AnimateIt( dt ); DrawIt( );

}

Page 31: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Rotating Earth: animate, drawRotating Earth: animate, drawvoid AnimateIt( float dt ) { angle += rot_speed * dt; if (angle > 360.0) angle -= 360.0;}

void DrawIt( ) { glClearColor(0, 0, 0, 0); // R,G,B,A glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glPushMatrix( ); glRotatef( angle, 0, 0, 1 ); glBindTexture(GL_TEXTURE_2D, earth_texture); gluSphere( quadric, 1.0, 16, 10 ); glPopMatrix( ); glutSwapBuffers( );}

Page 32: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Demo 3: Rotating EarthDemo 3: Rotating Earth

sgame/earth/earth1.exe

Page 33: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Earth Revolves around Earth Revolves around the Sun the Sun

dist

rot_angle

rev_angle

void AnimateIt( float dt ) { rot_angle += rot_speed * dt; rev_angle += rev_speed * dt;}

void DrawIt( ) { glPushMatrix( ); glRotatef(rev_angle, 0, 0, 1); glTranslatef(dist, 0, 0 ); glRotatef(rot_angle, 0, 0, 1); glBindTexture(GL_TEXTURE_2D, earth_texture); gluSphere(quadric, 1, 16, 10); glPopMatrix( ); glutSwapBuffers( );}

Page 34: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Demo 3: Rotating Earth Demo 3: Rotating Earth revolves around the Sunrevolves around the Sun

sgame/earth/earth.exe

Page 35: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Creating the spaceCreating the spacevoid DrawIt( ) { glBindTexture(GL_TEXTURE_2D, space_texture);

glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex3f(-SS, -SS, -SS);

glTexCoord2f(0, 1); glVertex3i(-SS, SS, -SS);

glTexCoord2f(1, 1); glVertex3i( SS, SS, -SS);

glTexCoord2f(1, 0); glVertex3i( SS, -SS, -SS); ... glEnd();}

SS,SS,SS

-SS,-SS,-SS

Page 36: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

GamesGames

virtual worldavatar

control

rendering interaction

AnimateIt(dt), DrawIt()ControlIt(dt)

Page 37: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Game ObjectGame Object ControlIt:

– interacts with the others, ”thinks” and sets the available controls (e.g. rockets)

InteractIt: – communication between two objects

AnimateIt: – moves to the next position and orientation

DrawIt: – renders the object with OpenGL

Page 38: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Simulation loop (Game loop)Simulation loop (Game loop)

void IdleFunc( ) { // idle call back float old_time = time; time = glutGet( GLUT_ELAPSED_TIME ); float dt = time - old_time;

avatar -> ProcessInput( ); world -> Control( dt ); world -> Animate( dt );

glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); avatar -> SetCameraTransform(); world -> Draw(); glutSwapBuffers( );}

dt

Page 39: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Game dataflowGame dataflow

InputProcessing:

KeyboardFunc,SpecialFunc

input

Simulate:IdleFunc

virtualworld

Render

Page 40: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Representation of the Representation of the virtual worldvirtual world

Dynamic objects (kill) Different object types (heterogeneous collection) Linked list (hierarchical structures, trees)

ship1 ship2avatar

space sun

world

bulletexplosion

Join: add new element

Page 41: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

SpaceshipSpaceship Complex geometry

– quadrilateral mesh Complex texture Physical animation

– forces (gravitation, rockets)– collisions

Behaviour (AI)– control rockets

avoid collisions, escape from avatar, chase avatar

Page 42: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Building a ship: Building a ship: Polygon modelling in MayaPolygon modelling in Maya

Page 43: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Extruding Extruding

Page 44: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Extruding (still)Extruding (still)

Page 45: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Extruding (once more)Extruding (once more)

Page 46: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Extruding (final)Extruding (final)

Page 47: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Smoothing by SubdivisionSmoothing by Subdivision

Page 48: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

= 1/2 + 1/16 + 1/16

Subdivision surfacesSubdivision surfaces= 1/4

= 1/4 + 1/4

Page 49: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Subdivision smoothing: Subdivision smoothing: 1 level1 level

Page 50: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Subdivision smoothing: Subdivision smoothing: 2 levels2 levels

Page 51: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

TexturingTexturing

modelltexture

Page 52: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Definition of the parameterizationDefinition of the parameterizationin Mayain Maya

(0,0)(1,1)

Page 53: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Definition of the parameterizationDefinition of the parameterization

Page 54: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Creating a texture for the shipCreating a texture for the ship

Page 55: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:
Page 56: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Textured shipTextured ship

Page 57: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Animate: using NewtonAnimate: using Newton’s laws’s lawsforcem

void Ship :: AnimateIt( float dt ) { acceleration = force/m; velocity += acceleration * dt; position += velocity * dt;}

void Ship :: DrawIt() { glPushMatrix( ); glTranslatef(position.x, position.y, position.z); glBegin( GL_QUADS ); ... ; glEnd( ); glPopMatrix();}

velocity

position

Page 58: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Orientation ControlOrientation Control

void Ship :: DrawIt() { glPushMatrix( ); glTranslatef(position.x, position.y, position.z);

Vector modell_head( 0, 0, 1 ); Vector world_head = velocity.UnitVector(); Vector rotate_axis = modell_head % world_head;

float cos_rotate_angle = world_head * modell_head; glRotatef( acos(cos_rotate_angle)* 180 / M_PI, rotate_axis.x,rotate_axis.y,rotate_axis.z); glBegin( GL_QUADS ); ... ; glEnd( ); glPopMatrix( ); }

modell_head

world_head = velocity.UnitVector();

Page 59: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Ship :: ControlItShip :: ControlItvoid Ship :: ControlIt( float dt ) { force = Vector(0, 0, 0); Interact( world );}

ship1 ship2avatar

space sun

world

bulletexplosion

void Ship::InteractIt( GameObject * object )

Page 60: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Ship: InteractItShip: InteractItvoid Ship :: InteractIt( GameObject * object ) { if ( object->GetType( ) == PLANET ) {

}

if ( object->GetType( ) == AVATAR ) {

}}

F = f m·M r2

avatar avatar

bullet

aiming angle

Page 61: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Ship: InteractItShip: InteractIt

if ( object->GetType( ) == PLANET ) { Planet * planet = (Planet *)object; Vector dist = planet->position - position; float r = dist.Length(); force += dist * f * mass * planet->mass /r/r/r;

if ( r < planet->Radius() * 2 )force += (position - planet->position)/r;

if ( r < planet->Radius()+BoundingRadius() )Kill();

}

F = f m·M r2

Page 62: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Ship: InteractItShip: InteractIt

if ( object->GetType( ) == AVATAR ) { Avatar * avatar = (Avatar *)object; Vector goal = avatar -> position - position; force += goal * 0.05;

cosaim = velocity.UnitVector()*goal.UnitVector(); if (cosaim > 0.9) {

world-> Join( new Bullet(position, velocity)); }}

avatar avatar

bullet

aim

Page 63: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Collision detection between Collision detection between two slow objectstwo slow objects

dist = obj1.position - obj2.positionmin = obj1.BoundingRadius() + obj2.BoundingRadius()if (dist.Length() < min) Collision!

given t

Problem with fast objects

t

t + t

Page 64: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

BulletBullet Seemingly complex geometry Similar look from all directions Easier to define its image

Hit detection = fast collision detectiontransparent

Page 65: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

BillboardsBillboards Single semi-transparent texture on an

oriented quadrilateral

pos

pos

QUAD

QUAD

Tmodell Tview Tperspectivexyz

XYZ

positionorientation

camera positioncamera orientation

Page 66: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Bullet :: DrawItBullet :: DrawItvoid Bullet :: DrawIt() {

Set transformation to compensate the rotation of the camera transform

glEnable(GL_BLEND); // transparency glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);

glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex2f(-size, -size); glTexCoord2f(1, 0); glVertex2f(size, -size); glTexCoord2f(1, 1); glVertex2f(size, size); glTexCoord2f(0, 1); glVertex2f(-size, size); glEnd();

glDisable(GL_BLEND);}

Page 67: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

How can we store How can we store transparent textures?transparent textures?

Some image file formats allow not only RGB triplets, but also RGBA quadruples

Store the texture into two images– image 1: rgb– image 2: a gray-scale image of alpha

Page 68: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Compensate camera Compensate camera transformtransform

float viewMatrix[16];glGetFloatv(GL_MODELVIEW_MATRIX, viewMatrix);

glMultMatrixf( viewMatrix );

0001tx, ty, tz

Rotation invert

pos.x, pos.y, pos.z

Page 69: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Collision detection with fast objects: Collision detection with fast objects:

ray-tracingray-tracing

velocity

rel_velocity = velocity - vel2ray: position + rel_velocity·t

If (ray intersects bounding sphere first AND tintersect < dt) Collision!

hit_object = world->Intersect(position,velocity,t);

ship1 ship2 avatar space sun

world

bulletexplosion

position

vel2

Page 70: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Bullet :: ControlItBullet :: ControlItvoid Bullet :: ControlIt( float dt ) { GameObject * hit_object = world -> Intersect(position, velocity, t);

if ( t > 0 && t <= dt ) {Kill(); // bullet disappearsVector hit_point = position + velocity * t;int hit_type = hit_object -> GetType();if ( hit_type == AVATAR ) exit(1); // player is killedelse { world -> Join( new Explosion( hit_point ) ); if (hit_type == SHIP) hit_object -> Kill();}

} age += dt; if ( age > 10 ) Kill(); // bullet disappears}

Page 71: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

ExplosionExplosion Seemingly complex, randomly

animated structure

Similar look from all directions Collection of billboards

Particle system

Page 72: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Particle SystemsParticle Systems

position: position += velocity * dtvelocity: velocity += acceleration * dtacceleration: acceleration = force / weight

lifetimeage: age += dt; if (age > lifetime) Kill();

size, dsize: size += dsize * dt;weight, dweight: weight += dweight * dtcolor, dcolor: color += dcolor * dt

global force field(smoke)

randominitialvalues

Page 73: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Explosion settingsExplosion settings

position = Rand( center, 0.1 ); // initially focusedlifetime = Rand( 2.0, 1.0 );

size = 0.001; // initially smalldsize = Rand( 1.0, 0.5 ) / lifetime / 2.0;

velocity = Rand( Vector(0, 0, 0), 0.4 ); // symmetricacceleration = Rand( Vector(0, 0, 0), 0.4 );

// from yellow opaque animate to reddish transparent color = Rand( Color(1, 0.5, 0, 1), Color(0, 0.5, 0, 0) );dcolor = Color(0, -0.5, 0, -1) / lifetime / 2;

Rand(mean, var) mean

var

Page 74: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

AvatarAvatar

Keyboard controls its behavior:– ProcessInput

Its position and orientation will be the camera position and orientation before rendering– SetCameraTransform

Like a ship but is not drawn– Control: gravitation, bullet collision

Page 75: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Avatar :: SetCameraTransformAvatar :: SetCameraTransformAvatar :: SetCameraTransform( ) { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(position.x, position.y, position.z,

position.x + velocity.x, position.y + velocity.y, position.z + velocity.z,

up.x, up.y, up.z);}

eye

lookat

up = [0, 1, 0]

Page 76: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Keyboard ProcessingKeyboard Processing

KeyboardFuncSpecialFunc

input

IdleFunc:GameLoop

virtualworld

IsSpace, IsLeft, IsRight, IsUp, IsDown

Page 77: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Avatar :: ProcessInputAvatar :: ProcessInputAvatar :: ProcessInput( GLUTWindow * input ) { if ( input->IsSpace( ) )// shoot

world -> Join(new Bullet(position, velocity));

Vector head = velocity.UnitVector();

if ( input->IsUp() ) force += up * (-1); if ( input->IsDown() ) force += up; if ( input->IsLeft() ) force += up % head; if ( input->IsRight() ) force += head % up;}

Page 78: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Game Game EngineEngine

GameObjectposition, velocity, accelerationControlIt(float dt )AnimateIt(float dt)InteractIt( GameObject * o) DrawIt( )IntersectIt(Ray r, float& t)

MemberControl, Animate, DrawInteract, Intersect, Join

TexturedObject

next

AvatarProcessInput()

SetCameraTransform()

TextureLoad( char * fname)

BillBoardDrawIt()

ParticleSystemEmit(int n)

Particle

GLUTWindow

GameEngineDisplayFunc

IdleFuncKeyPress

world

avatar

500 C++ lines

Page 79: Do it yourself 3D Games with OpenGL Szirmay-Kalos László Dept. of Control Engineering and Information Technology Budapest University of Technology email:

Space GameSpace Game

TexturedObjectAvatar BillBoard

SpaceDrawIt

PlanetDrawIt

AnimateIt

SelfProcessInput

ControlItInteractIt

BulletControlIt

ShipDrawIt

InteractItControlIt

GameEngine

SpaceGame

350 C++ lines