Animation and Games Development 242-515, Semester 1, 2014-2015 5. Game Architecture • Objective o to discuss some of the main game architecture elements, rendering, and the game loop 242-515 AGD: 5. Game Arch. 1 Overview 1. 2. 3. 4. Simple Game Architecture More Detailed Game Architecture Graphics/Rendering Game Programming 242-515 AGD: 5. Game Arch. 2 1. Simple Game Architecture Audio Input Rendering Engine Game Logic AI Network Game World Data 3 • I will focus on the graphics and maths techniques used inside the "Rendering Engine" box. • These include: o basic 3D graphics theory o 3D graphics programming with the JMonkeyEngine game engine o 3D rendering algorithms underlying 3D game engines o shader programming (maybe) 4 2. More Detailed Game Architecture 242-515 AGD: 5. Game Arch. 5 2.1. Core Systems • Elements used in most games: startup and shutdown file IO (loaders, writers, datafile parsers) input controls (keyboard, mouse, touch) window management • minimize, maximize, full-screen, resolution o maths (e.g. vectors, matrices) o data structures (e.g. linked lists) o o o o • In JMonkeyEngine, many of these come from the standard Java libraries 242-515 AGD: 5. Game Arch. 6 2.2. Resources (Assets) • These can be separated into game media (stuff visible to the player) and data resources (data used internally by the game). • Media Resources: o o o o o images / textures audio video shaders 3D models 242-515 AGD: 5. Game Arch. • Data Resources: o user settings • player items • high score table o game settings • fonts • physics map o platform settings • graphics and audio capabilities 7 2.3. Third-Party Libraries • The game engine will often reuse existing libraries: o GUI (in JMonkeyEngine (JME): Nifty GUI) o Physics (in JME: jBullet, a Java port of the Bullet Physics library) o skeletons (in JME: support for Blender joints) o AI o visual effects( in JME: OpenGL shaders, particle system) o terrain (in JME: the Ogre3D dotScene format) o scene graph (so important, that it's a part of the core in JME) 242-515 AGD: 5. Game Arch. 8 2.4. What is a Scene Graph? • A scene graph stores all the entities or objects in the scene, and the spatial relationship between them. 242-515 AGD: 5. Game Arch. 9 • The scene graph data structure simplifies the creation of a scene by the user, and also: o makes it easier to transform groups of objects • e.g. translate, rotate them o makes it easier to determine when objects are colliding and are visible to each other o makes it easier to display a scene with different Levels of Detail (LOD) 242-515 AGD: 5. Game Arch. 10 2.5. Visual Effects (in JME) • Particles o smoke, fire, explosions, etc. • Post processing / filter effects o reflective water, light scattering, fog o shadow mapping o High Dynamic Range (HDR) rendering • increased contrast for greater detail o ambient occlusion (blocking) o depth of field blur o etc. 242-515 AGD: 5. Game Arch. 11 2.6. Physics (in JME) • Java binding to the Bullet physics library o a collision detection and rigid body dynamics library • http://bulletphysics.org/wordpress/ • Features include: o o o o o o o collisions, gravity, forces mesh-accurate collision shapes rigid body dynamics for vehicles and characters physical joints and hinges Blender integration Ragdoll physics Multi-threaded physics 242-515 AGD: 5. Game Arch. 12 3. Graphics/Rendering • Almost every game engine utilizes either OpenGL or DirectX for its graphics processing and rendering. • JMonkeyEngine uses OpenGL. http://www.opengl.org/ http://msdn.microsoft.com/en-US/directx 3.1. OpenGL • A hardware-independent API, implemented on many different platforms. • Several hundred functions, with many language bindings. • An accepted industry standard: • evolves slowly • currently at version 4.2 • Widely used for: • games, modeling, scientific visualization, etc. 14 3.2. DirectX • DirectX is a Microsoft API providing direct access to hardware. • Only for Windows, currently at version 11.1 • DirectX components: o o o o o Direct Graphics – 2D and 3D graphics DirectInput – interface to input devices DirectAudio – play sound and music DirectPlay – communication across networks DirectShow – multimedia support 15 3.3. OpenGL vs. DirectX • OpenGL is aimed at 3D graphics only; DirectX is a more complete game development API. • OpenGL is portable across platforms; DirectX is only for Windows. 16 3.4. What is Rendering? render • Rendering is the process of converting a 3D scene into a 2D picture (raster image) on the computer screen: o The 3D scene is composed of 3D models (geometries). o A model is composed of 3D graphics primitives oe.g. triangles, quadrilaterals 17 4. Game Programming • Most games consist of an startup phase, a game loop, and a shutdown phase: • A game loop consists of three stages: 242-515 AGD: 5. Game Arch. 18 4.1. A Detailed Game Flowchart Start Display Startup GUI Initialization: Load assets Load levels … Read player input Shutdown: store scores close files End game over? Update game state, using: Collision Detection, Game AI, Physics, etc Render (draw) game frame 4.2. Frame Rates • An important element missing from the basic game loop is the need to keep the rate of frame drawing constant. • A frame rate is the average number of frames drawn by the game loop each second. • Typical frame rates: o 30 fps (i.e. 30 frames drawn in 1 second) o 50 fps • The frame rate should stay constant so that the user sees the game updating at a fixed rate. 242-515 AGD: 5. Game Arch. 20 4.3. Frame Rate Problem • The frame rate should be the same on fast and slow computers. • The problem is that a fast machine will carry out "updating" and "rendering" faster than a slow machine, and so the frame rate will be faster. • We must change the game loop so that the frame rate isn't affected by a computer's speed. 242-515 AGD: 5. Game Arch. 21 • One way to fix this speed problem is to add a "wait" stage in the loop: run stop Startup Input Update Shutdown game loop Wait Render delays the loop so the frame rate is not too fast 242-515 AGD: 5. Game Arch. 22 • Waiting deals with a loop that is too fast, but what about a loop that is too slow? o e.g. because the computer is slow • One solution is to skip the rendering (drawing) stage, making the loop faster: run stop Startup Input Update no Shutdown Do we have extra time? yes Wait 242-515 AGD: 5. Game Arch. Render game loop 23 • The good news is that JMonkeyEngine deals with maintaining a constant frame rate. • We only have to write the "update" code for the loop, not the "rendering" or timing parts, which are dealt with by the engine. 242-515 AGD: 5. Game Arch. 24