Title: Bringing it all together
1Bringing it all together
- Bryan Duggan
- Hugh McAtamney
2Summary
- We can now use
- Vectors
- To represent position and orientation
- Represent a camera
- Vector arithmetic
- To move things
- Matrices
- To translate
- To rotate
- To scale
- To program the rendering pipeline
- DirectX
- To create a surface for drawing
- To create and draw textured vertex buffers
3Whats left
- Meshes
- Game entities
- Collision detection
- Then we can build a complete world to play with
- The world Textured polygons, vertex buffers
- The player The camera
- Daleks/projectiles - Mesh
- Ammunition - Mesh
4Mesh
- A mesh is a
- File
- Memory representation (vertex buffer etc)
- Loader
- It holds
- Vertices
- Indexes
- Normals
- Textures
- Materials
- Animation
- We can create a mesh in a 3D Editor
53D Editors
- Milkshape
- 3D Studio Max
- Maya
- GMax
6X Files
- In Direct X, meshes are stored with a .x
extension - Can be binary or text format
- We can use a 3D editor to export to the .x file
format - There is a .x file viewer with the DirectX SDK
7xof 0303txt 0032 // DirectX - from
MilkShape3D Frame dalekFrame
FrameTransformMatrix
1.000000,0.000000,0.000000,0.000000,
0.000000,1.000000,0.000000,0.000000,
0.000000,0.000000,1.000000,0.000000,
0.000000,0.000000,0.000000,1.000000
Mesh dalekMesh 15872
0.0774927.0544702.468911,
0.1152847.0469692.468383,
0.1152847.0208392.466543,
0.0674677.0303272.467211,
0.0875177.0786102.470611,
8Other formats
- FarCry
- Quake
- Half Life 1/2
- 3DS
- Etc.
9Mesh concepts
- Subsets
- Meshes are divided into subsets parts of the
mesh with different materials textures - Materials
- Determine how the light interacts with the subset
10Mesh API Calls
- To create a mesh from a file
- ID3DXBuffer adjBuffer 0
- ID3DXBuffer mtrlBuffer 0
- DWORD numMtrls 0
- hr D3DXLoadMeshFromX(
- _meshName.c_str(),
- D3DXMESH_MANAGED,
- device,
- adjBuffer,
- mtrlBuffer,
- 0,
- numMtrls,
- _mesh)
11Mesh API Calls
- mtrlBuffer points to a buffer for materials (used
for lighting) and texture file names - We then have to iterate through this buffer and
load any required textures
12In code
- for(int i 0 i lt numMtrls i)
-
- // the MatD3D property doesn't have an ambient
value set - // when its loaded, so set it now
- mtrlsi.MatD3D.Ambient mtrlsi.MatD3D.Diffuse
- // save the ith material
- _materials.push_back( mtrlsi.MatD3D )
- // check if the ith material has an associative
texture - if( mtrlsi.pTextureFilename ! 0 )
-
- // yes, load the texture for the ith subset
- IDirect3DTexture9 tex 0
- D3DXCreateTextureFromFile(
- device,
- mtrlsi.pTextureFilename,
- tex)
13Optimise the mesh
- hr _mesh-gtOptimizeInplace(
- D3DXMESHOPT_ATTRSORT
- D3DXMESHOPT_COMPACT
- D3DXMESHOPT_VERTEXCACHE,
- (DWORD)adjBuffer-gtGetBufferPointer(),
- 0, 0, 0)
14Drawing the mesh
- We iterate through the subsets and draw each one
with the appropriate material and texture - for(int i 0 i lt _materials.size() i)
-
- Device-gtSetMaterial( _materialsi )
- Device-gtSetTexture(0, 0)
- Device-gtSetTexture(0, _texturesi)
- _mesh-gtDrawSubset(i)
-
15Other useful Mesh API calls
- D3DXCreateBox
- D3DXCreateSphere
- D3DXCreateCylinder
- D3DXCreateTeapot
- D3DXCreatePolygon
- D3DXCreateTorus
16Bounding volumes
- D3DXComputeBoundingBox
- D3DXComputeBoundingSphere
- Do these return coordinates in local space or
world space?
17In code
- BYTE v 0
- _mesh-gtLockVertexBuffer(0, (void)v)
- D3DXComputeBoundingBox(
- (D3DXVECTOR3)v,
- _mesh-gtGetNumVertices(),
- D3DXGetFVFVertexSize(_mesh-gtGetFVF()),
- _min,
- _max)
- _mesh-gtUnlockVertexBuffer()
18In code
- void MeshWrappercomputeBoundingSphere()
-
-
- // Now compute the bounding rectangle
- BYTE v 0
- _mesh-gtLockVertexBuffer(0, (void)v)
- D3DXComputeBoundingSphere(
- (D3DXVECTOR3)v,
- _mesh-gtGetNumVertices(),
- D3DXGetFVFVertexSize(_mesh-gtGetFVF()),
- _center,
- _radius)
- _mesh-gtUnlockVertexBuffer()
19As we are good OO games programmers
- We add a MeshWrapper class to the framework
- Extends DrawableEntity
- Encapsulate all this messy and repetitive code
- Simply call setMeshName, before calling setup
- This leads us onto
20OO
- Computer games programmers need to be good OO
thinkers - Games are complicated
- Games need to be fast
- Most games naturally fit the OO paradigm
(European OO) - Problems in games (such as asset management etc.
etc. etc.) can be solved using design patterns
(USA OO) - Games APIs (DirectX/OpenGL) are written in C/C
- Games are written in C
- Complex C code can be visualised in UML
21Games are written in C
- Almost universally!!
- Why
- Access to native code (DirectX/OpenGL)
- OO
- Speed
- Portability?
22Games engines
- Written in C
- Are supported by embedded scripting languages,
such as Lua - Flexibility
- Ability to create MODs
- Ability to expose C objects/APIs
23Dalek World design
- Based on the functionality of the Dalek World
game what should our design be? - Existing classes
- Direct3dBase
- DrawableEntity
- BoundableEntity
- MeshWrapper
- Wall
- World
- DalekWorld
- What else do we need?
24(No Transcript)
25New classes
- MoveableEntity
- Has position, orientation (look)
- Can move (walk, strafe)
- Can look (yaw, roll, pitch)
- Has speed
- Camera
- As above
- But can also generate a view matrix
- Projectile
- As MoveableEntity
- Has a mesh, so it can draw itself
- But can be fired and hit things
- GameEntity
- As MoveableEntity
- But also has a MeshWrapper, so it can draw itself
26New classes
- Ammo
- As above
- But also rotates, respawns, can be picked up
- AutonomousGameEntity
- As GameEntity
- But can make decisions and move autonomously, (to
come later) - Dalek
- As above
- But also can fire projectiles
- Can also pick up ammo
- Has health and ammo stats
27UML Class diagram