Presentation is loading. Please wait.

Presentation is loading. Please wait.

Announcements Always the alias rather than either of us directly for anything – Better.

Similar presentations


Presentation on theme: "Announcements Always the alias rather than either of us directly for anything – Better."— Presentation transcript:

1 Announcements Always email the cs195utas@cs.brown.edu alias rather than either of us directly for anything course-relatedcs195utas@cs.brown.edu – Better for TAs (know what’s going on, can discuss before responding) – Better for you (faster response time) – Exception: grade questions BGD meeting Saturday 8:30p in the CIT lobby Test your handins on department machines! – If you didn’t have to do LIBS += -lGLU in your.pro file and #include to use GLU, you used the wrong Qt version (we use 4.8) – Windows doesn’t have case sensitive filenames, Linux does #include, NOT #include, NOT #include – QtCreator is very lightweight, good for X forwarding

2 World Representation Not the U.N.! Spatial Organization Warmup Weeklies Tips for Minecraft 1

3 Ideal 3D game environment Easy to generate – Algorithm/world-editing tools Dynamic – Easy, inexpensive to modify Accurate collision detection Beautiful, high detail – Inexpensive to render Compact representation – Faster to load from disk or network AI can easily navigate all areas

4 The harsh reality Tradeoffs between detail and object count Choices depend on platform, game mechanics – Much higher detail where player is likely to focus Examples – Car models vs. background trees in racing games – Dynamic MMO worlds do not have compact representations

5 World representation Design goals determine the best representation to use Easy to generate – Some data structures better for algorithms/editors than others Dynamic – How much rebuilding must be done when the world is modified? Accurate collision detection – Optimize for fewer, more detailed models vs. many low detail models Compact representation – Space vs. time tradeoff

6 Basic world representation Polygon soup – Most basic representation – Unordered collection of polygons (no connectivity/hierarchy) – Easy to generate – Difficult for collision detection (hard to tell where solid objects are) Polygon mesh – Polygons share vertices – Ensures closed mesh – Good for representing world – Not good for game objects (usually need simpler representation)

7 World Representation Octreerific! Spatial Organization Warmup Weeklies Tips for Minecraft 1

8 Spatial organization Standard collision detection is O(n 2 ) – Too slow for large game worlds Solution: spatially organize to discard far-away data – Other game objects – Pieces of static geometry Many approaches – Bounding volumes – Bounding volume hierarchy – Uniform grid – Hierarchical grid – Octree – BSP tree – Portal-based environment

9 Bounding volumes Wrap objects in simple geometry (bounding volumes) to speed up collisions – If objects are far apart, their bounding volumes won’t collide, and the detailed test is not needed Doesn’t solve O(n 2 ) runtime alone

10 Bounding volume hierarchy (BVH) Parent volumes contain child volumes Sibling volumes may overlap (not spatial partitioning) Speeds up collisions between dynamic entities Also speeds up rendering

11 BVH construction Top-down construction easier than bottom-up Start with a volume containing all objects Find a partition via some heuristic Sort objects into two sub- volumes and discard partition Repeat recursively

12 BVH construction buildBVH(node, objects): // fit bounding volume of node to objects... if numObjects <= minObjectsPerLeaf: node.objects = objects else: // partition objects according to a heuristic... buildBVH(node.left, leftObjects) buildBVH(node.right, rightObjects)

13 BVH construction heuristics Difficult to choose good partitioning axes Goals – Minimize node volume – Keep branches balanced – Minimize sibling overlap Strategies – Median-cut (divide into equal-size parts with respect to a selected axis) – Minimize sum of child volumes – Maximize separation of child volumes

14 BVH queries collideBVH(object, volume): if volume.isLeaf: for object2 in volume.children: collide(object, object2) else: for child in volume.children: collideBVH(object, child)

15 Uniform grid Speeds up collisions between dynamic entities Axis-aligned cube cells (voxels) – Each cell has list of objects May be dense (array) or sparse (hash-based) Each frame: update cells for objects that moved

16 Uniform grid Pros – Simple to generate and modify – Good for static and dynamic objects – O(1) access to neighbors Cons – Not appropriate for objects of greatly varying sizes – Dense representation wastes memory in empty regions

17 Hierarchical grid Hierarchy of differently-sized uniform grids – N levels, cells at top level are big enough to cover bounding box of largest game object – Each sub-level’s cells are half the scale Inserting objects – Find the level where cells are big enough to fit it fully – In that level, insert into the cell that contains its center 1D example on right 4B 3D 2AE 1CF ABCDFE

18 Hierarchical grid queries Traverse over all levels Test cells containing object’s bounding box and neighboring cells 1D example: colliding object E 4B 3D 2AE 1CF ABCDFE 4B 3D 2AE 1CF

19 Octree Axis-aligned hierarchical partitions Hierarchical grid with one top- level cell Pros: – Simple construction for static scenes – Easy collision tests: all AABBs Cons – Traversing tree is expensive if the tree is deep or unbalanced

20 Case study: Cube Engine Open-source networked FPS engine Uses octree to represent world elegantly – Vertices of leaf nodes can be moved – Ramps, heightfields, curved surfaces

21 Binary space partitioning (BSP) tree Hierarchy of planar half-spaces – Each node has a plane, divides the space of the parent in two – Models solid vs. empty space – Leaves represent convex polytope Some solid, some empty Some have infinite area/volume Works best for indoor environments (flat, man-made surfaces) Used by original Doom and Quake – Still used today (Source, Unreal, Call of Duty IW engine) a b c d e f g h b a c d e f g h

22 BSP tree construction Recursively pick a triangle according to a heuristic, split scene along that plane Tree needs to be balanced for good performance a bceg b a c d e f g h dfh

23 BSP tree querying To test if a point is inside or outside: – Start at root – Visit front child if point in front of plane, otherwise visit back child – If the point is in front of a leaf node, it is outside, otherwise it is inside b a c d e f g h a b e f c d g h a b e f

24 BSP tree ray traversal Given ray (P,D) and root If P is behind plane – Recursively traverse back node – Hit-test polygons on plane – Recursively traverse front node if D N > 0 If P is in front of plane – Recursively traverse front node – Hit-test polygons on plane – Recursively traverse back node if D N < 0 Front Back D P

25 BSP tree pros & cons Pros: – Because it’s a binary tree, most tests O(log n) – Easy collision detection and raycasting Cons: – Very expensive to build (optimal is NP-complete!) – Not suitable for dynamic objects – Numeric stability can be tricky

26 Constructive solid geometry (CSG) Boolean set operations on 3D solids Useful for level building Level editors for games like Unreal, Quake II – Source engine level editor (Hammer) and Unreal editor still based on CSG Subtraction Intersection Union

27 CSG implementation Elegantly handled by merging BSP trees A and B – Split polygons in A by planes in B, get A' – Split polygons in B by planes in A, get B' Union (A | B) – Remove polygons in A' that are inside B – Remove polygons in B' that are inside A Subtraction (A - B) – Remove polygons in A' that are inside B – Remove polygons in B' that are outside A – Flip orientation of polygons in B' Intersection (A & B) – Remove polygons in A' that are outside B – Remove polygons in B' that are outside A A B A B A B

28 Problems using BSP trees with CSG Coplanar polygons (neither inside nor outside) Extra geometry due to BSP splits T-junctions

29 Case study: Unreal engine Originally used CSG via BSP merging exclusively – Palette of solid “brushes” (cube, cylinder, stairway) – World is initially solid, rooms are subtracted Modern versions use BSP just for rough level shape – Use static meshes for details – Avoids BSP+CSG problems

30 Portal-based environment Models empty space, carves world up into non- overlapping convex polyhedra – Shared faces between two polyhedra are “portals” – Appropriate for indoor environments The key: no obstruction from any point to any other point inside a polyhedron Convex polyhedra Room with portals marked

31 Constructing portal environments Model using polyhedra directly – Usually in an editor (tedious to create) Or just use leaves of BSP tree as polyhedra – May result in lots of tiny portals (inefficient)

32 Portal ray traversal method Start in one polyhedron Trace ray to face – If solid, stop – If portal, continue in neighbor 1 2 3 4 5

33 Dynamic visibility set w/portals Determine set of objects visible from within each polyhedron on the fly Recursively clip against portal bounds

34 Portal-based rendering example Draw room 1 – Contains player Draw room 2 – Seen from portal 1 Draw room 3 – Seen from portal 2 Don’t draw room 4 – Not seen from portal 3 Player View frustum 1 2 3 4

35 Portal pros and cons Pros – Easy collision detection and raytracing – Can be used for dynamic objects (unlike BSP trees) – Portals can point to non-adjacent polyhedra Mirrors, teleportation, changes in size Cons – Implementation is complex – Hardware can do visible surface determination better now Hierarchical z-buffer occlusion culling

36 Case study: Descent engine One of the first actually 3D games – Fly with 6 degrees of freedom through tight hallways Used deformed cubes as polyhedra – Easy to edit, manipulate empty space directly – Portal-based render led to super-efficient software rendering

37 World Representation Stay a while and listen! Spatial Organization Warmup Weeklies Tips for Minecraft 1

38 Overview First major project, three checkpoints Week 1 – Build a world with textures and terrain Week 2 – Make rendering more efficient and scalable – Add a player with collision detection and response Week 3 – Add simplistic enemies – Also ability to add and remove blocks – Stream in chunks as the player moves Beyond… – Play your own version of Minecraft!

39 Minecraft world format Two-layer hierarchical grid – 1x1x1 blocks grouped into chunks Class project: 32x32x32-block chunks The real game: 32x128x32-block chunks Huge number of blocks, memory usage is problematic – Each block holds one byte specifying its type – Determine textures, transparency, etc. from type – Original game uses a second byte for lighting data Why did Notch (creator) choose this data structure?

40 Terrain generation Noise function mapping 2D point in horizontal plane to terrain height – Why is rand() a bad idea? Terrain will be too noisy; completely disjoint – Octave noise: sum layers of interpolated noise at different scales – Look into simplex noise or Perlin noise for more information For true 3D noise (overhangs, caves, etc.), just use a 3D noise function – Block solid iff noise value at its center greater than some threshold – But bias by the height so that there’s solid ground rather than infinite rolling noise high and low

41 Rendering Only draw faces that neighbor an empty cell Use a glBegin()/glEnd() block to draw the faces as textured quads – Like last week Rendering will still be slow with lots of blocks – Next week, you will learn how to make the rendering much faster with VBOs!

42 C++ tip of the week Struct alignment On a 32-bit machine, a Foo instance will occupy 12 bytes – By default, struct data members are word-aligned – Reading/writing aligned memory is faster What if we want to reduce memory usage? struct Foo { char m1; char m2; short m3; };

43 C++ tip of the week C++ compilers allow you to pack structs Useful for network buffers and large data sets We strongly recommend using this for your Minecraft chunk structure // gcc struct Foo {... } __attribute__((packed)); // msvc #pragma pack(push, 1) struct Foo {... }; #pragma pack(pop)

44 World Representation Pretty boring this week! Spatial Organization Warmup Weeklies Tips for Minecraft 1

45 Examples of things you can talk about Anything extra you did on top of requirements Any interesting choices or design decisions you made Any unusual problems you encountered Bugs that you can’t seem to track down


Download ppt "Announcements Always the alias rather than either of us directly for anything – Better."

Similar presentations


Ads by Google