Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
INTRODUCTION TO COMPUTER G RAPHI C S CIS 736 Computer Graphics Lecture 22 of 42 Video Games and BSP 1 of 2 Monday, 06 March 2006 Reading: Hardware Rendering (shader_lecture) Adapted with permission from slides by Andy van Dam and Kevin Egan W. H. Hsu http://www.kddresearch.org avd November 4, 2003 VSD 1/46 INTRODUCTION TO COMPUTER G RAPHI C S Object-Precision Algorithms Historically first approaches • Roberts ’63 - hidden line removal – compare each edge with every object - eliminate invisible edges or parts of edges. • Complexity: worse than O(n2) since each object must be compared with all edges • A similar approach for hidden surfaces: – each polygon is clipped by the projections of all other polygons in front of it – invisible surfaces are eliminated and visible sub-polygons are created – SLOW, ugly special cases, polygons only avd November 4, 2003 VSD 39/46 INTRODUCTION TO COMPUTER G RAPHI C S 3-D Depth-Sort Algorithm (Newell, Newell, and Sancha, based on work by Schumacher) • Handles errors/ambiguities of Z-sort: • Summary of algorithm 1. Initially, sort by smallest Z 2. Resolve ambiguities: (a) Compare X extents (b) Compare Y extents (c) Is P entirely on one side of Q? (d) Is Q entirely on one side of P? (e) Compare X-Y projections (Polygon Intersection) (f) Swap or split polygons 3. Scan convert back to front avd November 4, 2003 VSD 40/46 INTRODUCTION TO COMPUTER G RAPHI C S 3-D Depth-Sort Algorithm Advantages • Fast enough for simple scenes • Fairly intuitive Disadvantages • Slow for even moderately complex scenes • Hard to implement and debug • Lots of special cases avd November 4, 2003 VSD 41/46 INTRODUCTION TO COMPUTER G RAPHI C S Binary Space Partitioning (1/5) (Fuchs, Kedem, and Naylor, based on work by Schumacher) • Provides spatial subdivision and draw order • Divide and conquer: • avd – to display any polygon correctly, display all polygons on “far” (relative to viewpoint) side of polygon, then that polygon, then all polygons on polygon’s “near” side. – but how to display polygons on one side correctly? Choose one polygon and process it recursively! Trades off view-independent preprocessing step (extra time and space) for low run-time overhead each time view changes November 4, 2003 VSD 42/46 INTRODUCTION TO COMPUTER G RAPHI C S Binary Space Partitioning (BSP) Trees (2/5) • Perform view-independent step once each time scene changes: – recursively subdivide environment into a hierarchy of half-spaces by dividing polygons in a half-space by the plane of a selected polygon – build a BSP tree representing this hierarchy – each selected polygon is the root of a sub-tree • An example: BSP-0: Initial Scene avd November 4, 2003 VSD 43/46 INTRODUCTION TO COMPUTER G RAPHI C S Binary Space Partitioning (BSP) Trees (3/5) BSP-1: Choose any polygon (e.g., polygon 3) and subdivide others by its plane, splitting polygons when necessary BSP-2: Process front sub-tree recursively avd November 4, 2003 VSD 44/46 INTRODUCTION TO COMPUTER G RAPHI C S Binary Space Partitioning (BSP) Trees (4/5) BSP-3: Process back sub-tree recursively BSP-4: An alternative BSP tree with polygon 5 at the root avd November 4, 2003 VSD 45/46 INTRODUCTION TO COMPUTER G RAPHI C S Binary Space Partitioning (BSP) Trees (5/5) • Perform BSP tree view-dependent step once each time viewpoint changes: – – – the nodes can be visited in back-to-front order using a modified in-order walk of the tree. at any node, the far side of a node’s polygon is the side that the viewpoint is not in very fast! Quake III uses this for occlusion culling and to speed up intersection testing void BSP_displayTree(BSP_tree* tree) { if ( tree is not empty ) if ( viewer is in front of root ) { BSP_displayTree(tree->backChild); displayPolygon(tree->root); BSP_displayTree(tree->frontChild) } else { BSP_displayTree(tree->frontChild); /* ignore next line if back-face culling desired */ displayPolygon(tree->root); BSP_displayTree(tree->backChild) } } BSP applet : http://symbolcraft.com/graphics/bsp/index.html avd November 4, 2003 VSD 46/46 INTRODUCTION TO COMPUTER Game Genres Action Adventure Educational Strategy • Real time (RTS) and turned based RPG • CRPG, MMORPG Puzzle Shooter • First Person Shooter (FPS) Sports Platformer Racing Simulation Fighting The Action Hybrids: Action/Adventure, Action/RPG G RAPHI C S INTRODUCTION TO COMPUTER G RAPHI C S What is a game engine? The core software component Car analogy The game engine… • Takes input • Computes physics (collisions, projectiles, etc.) • Plays sounds • Simulates AI • Draws stuff! A good game engine is independent of the game type Writing a good game engine takes years so most developers license engines • Current: Quake III, Unreal Tournament • Future: Source, Doom III, Unreal Warfare • Ever notice that a lot of games look alike? Well, what’s a game then? • Gameplay • Levels, music, artwork, story, etc. INTRODUCTION TO COMPUTER G RAPHI C S Basic Game Loop while (!gameover) { get input process input simulate physics and AI redraw } As CS123 graduates, you can all do this • Sceneview: level file format and (bad) scene data • Camtrans: Takes care of camera and math • Modeler: User manipulations • Intersect: Basics of collision • You can hack together a FPS without too much effort structure INTRODUCTION TO COMPUTER G RAPHI C S Sound Stereo doesn’t cut it anymore – you need positional audio. Positional audio increases immersion The Old: Vary volume as position changes The New: Head-Related Transfer Functions (HRTF) for 3d positional audio with 2-4 speakers Games use: • Dolby 5.1: requires lots of speakers • Creative’s EAX: “environmental audio” • Aureal’s A3D: probably the best positional audio • DirectSound3D: Microsoft’s answer • OpenAL: open, cross-platform API INTRODUCTION TO COMPUTER G RAPHI C S Physics Sick of seeing the same death animation every time? Enter “rag doll physics.” Real physics is computationally expensive, so lots of approximations (like bounding boxes) are made. Realism/Performance tradeoff Most explosions and projectiles are scripted, not simulated “Good” physics engines are now a requirement for a game engine • Unreal and Source use HAVOK • Doom III rolled its own and will even have per-polygon hit detection! Realistic physics != good game (Trespasser) Can use spacial data structure to speed up collision tests as well Particle systems Demo: Stair and Truck Dismount INTRODUCTION TO COMPUTER G RAPHI C S AI And you thought graphics was hacky! The Old: Simple FSMs, Crash ‘n’ Turn The New: Glorified FSMs (scripting, waypoints, goals…), A* Creating an AI that will kick your butt is child’s play. Believable AI is very difficult. One of the easiest ways to get into the industry • Download a SDK, make a bot, get hired. AI continues to get better as people “read the literature” and the GPU lightens the load on the CPU INTRODUCTION TO COMPUTER G RAPHI C S Black and White (2000) The most advanced AI seen in a video game to date. God game where you have to train your avatar to behave. Unfortunately it’s not that great of a game… INTRODUCTION TO COMPUTER G RAPHI C S Networking Packets will get lost. The internet is unpredictable. Use non-blocking I/O and multithreading TCP/IP is too slow! • Too much error correction • Waits for ACK before sending next packet • Almost all games use UDP instead Since we’re going to use UDP, it’s up to us to do the error correction. No one really had a good solution until… John Carmack brought fast paced action gaming to modems with QuakeWorld (1996) and set up the standard client/server model still used today. INTRODUCTION TO COMPUTER G RAPHI C S The Client/Server Model for Games The server simulates the game at X Hz and is the authoritative state of the game. It sends packets to the clients. The multiple clients are all playing the game at different speeds (> X usually) and sending information to the server. Problem: The client is updating at a different rate than the server. Solution: Interpolation! Problem: What if there is a hiccup and we miss a packet from the server? Solution: Extrapolation and reconstruction from delta compression INTRODUCTION TO COMPUTER G RAPHI C S I thought this was a graphics class? Cheating is “avoided” by the server containing the only real state of the system and having it make all of the big decisions (i.e. deaths aren’t extrapolated) If you thought this was hard, what about MMORPGs that must support a few thousand players at once on the same server? Asheron’s Call has the most impressive networking system: all servers are the same world. Yikes! • Fun Fact: Turbine was founded by Brown graduates and Andy was chairman of the board for a time! INTRODUCTION TO COMPUTER G RAPHI C S Single Instruction Multiple Data (SIMD) The real way vector algebra is done Exploit data parallelism Cram vectors (four 32-bit floats) into 128-bit registers and do things in one instruction instead of four x86: MMX (ints), SSE (floats), SSE2 (doubles) PPC: AltiVec PS2’s EE has two VPUs, VU0 and VU1 • Btw, the PSone and PS2 are MIPS based. CS31 comes in handy after all! Xbox has a PIII => SSE Gamecube has a PPC but doesn’t have full AltiVec Hardware T&L does this too INTRODUCTION TO COMPUTER G RAPHI C S Camera When games made the transition from 2d to 3d, a good camera system became essential Golden Rule: The camera should not interfere with gameplay. • Your camera system should be defined by the gameplay you’re trying to achieve. A good camera is very hard to achieve • Some control must be given to the user, but how much? Three main techniques: 1. First Person 2. Third Person – 3. Fixed, Free, or Follow “Isometric” Personally, I’d like to see a second person camera system INTRODUCTION TO COMPUTER First Person Popularized by id software Dominates PC gaming Good for: • FPS • Immersive storytelling Bad for: • Platformers* (%^!&ing jumping puzzles!) • Strategy games *see the next slide G RAPHI C S INTRODUCTION TO COMPUTER G RAPHI C S Excellence in First Person Metroid Prime is an exception to the platformer rule Transition to third person fixed or follow when in ball mode INTRODUCTION TO COMPUTER G RAPHI C S Third Person Most prevalent choice, especially on consoles Fixed: Camera doesn’t move. Ever. • Resident Evil series, Final Fantasy VII-IX Follow: The camera does move to follow the player but camera movement is very restricted • Sports games, Crash Bandicoot, Panzer Dragoon, Devil May Cry • Most common of the three types Free: The camera doesn’t always follow in a linear manner. This flavor gives the user the most control over the camera. • Super Mario 64, MMORPGs • Hardest mode to get right More restriction is good for the developers, but bad for the player Selection/targeting is difficult INTRODUCTION TO COMPUTER G RAPHI C S Excellence in Third Person Super Mario 64 had the first legitimate third person camera system in a 3d environment. Games are still copying it today. Further refined in The Legend of Zelda: Ocarina of Time Read about it in Game Programming Gems II And yet some how they screwed it up in Super Mario Sunshine… INTRODUCTION TO COMPUTER G RAPHI C S “Isometric” Video games have been using isometric projection for ages. It all started in 1982 with Q*Bert and Zaxxon which were made possible by advances in raster graphics hardware Still in use today when you want to see things in the distance as well as things close up (e.g. strategy, simulation games) Technically most games today aren’t isometric and are instead axonometric, but people still call them isometric to avoid learning a new word. Other inappropriate terms used for axonometric views are “2.5D” and “three-quarter.” INTRODUCTION TO COMPUTER G RAPHI C S Issue: Clipping Planes Have you ever played a video game and all of the sudden some object pops up in the background (e.g. a tree in a racing game)? That’s the object coming inside the far clip plane. The old hack to keep you from noticing the pop-up is to add fog in the distance. A classic example of this is from Turok: Dinosaur Hunter Now all you notice is fog and how little you can actually see. This practically defeats the purpose of an outdoor environment! And you can still see pop-up from time to time. Thanks to fast hardware and level of detail algorithms, we can push the far plane back now and fog is much less prevalent. However this hurts Z precision and can lead to Z-fighting. Putting the near clip plane back a bit can help recover some precision INTRODUCTION TO COMPUTER G RAPHI C S Issue: Orientation Using Euler angles (pitch, yaw, spin) can lead to Gimbal lock and poor interpolation Quaternions solve the problem • 4d complex numbers • 3d dimensional vector + scalar component • Spherical linear interpolation (slerp!) • axis-angle representation is equivalent • I covered this in a section so I won’t go over it all again Scripting camera paths is also complicated • The path is typically a spline and the camera orientation is interpolated along the path INTRODUCTION TO COMPUTER G RAPHI C S Texturing Nothing is more important than texture performance and quality. Textures are used for absolutely everything. Fake shading Fake detail Fake effects Fake geometry • Geometry is expensive – you gotta store it, transform it, light it, clip it… bah! Use them in ways they aren’t supposed to be used • An image is just an array after all If it weren’t for textures, we’d be stuck with big Gouraud shaded polys! Quick hardware texture review • Interpolation is linear in 1/z INTRODUCTION TO COMPUTER G RAPHI C S Multipass Rendering In 123, everything we’ve done has been in one pass but in reality you won’t get anywhere with that. Multipass rendering gives you flexibility and better realism An early version of Quake 3 did this: (1-4: Accumulate bump map) 5: Diffuse lighting 6: Base texture (7: Specular lighting) (8: Emissive lighting) (9: Volumetric effects) (10: Screen flashes) Multitexturing is the most important part of multipass rendering (remember all of those texture regs?) INTRODUCTION TO COMPUTER G RAPHI C S Billboards A billboard is a flat object that faces something • There are lots of different billboarding methods, but we’ll stick with the easiest, most used one Take a quad and slap a texture on it. Now we want it to face the camera. How do we do that? (Hint: you just did it in modeler) Bread and butter of older 3d games and still used extensively today • Monsters (think Doom) • Items • Impostors (LOD) • Text • HUDs (sometimes) • Faked smoke, fire, explosions, particle effects, halos, etc. • #*$&ing lens flares Bad news: Little to no shading INTRODUCTION TO COMPUTER G RAPHI C S Aliasing when scaling up Bilinear Filtering (a.k.a. Bilinear Interpolation) • Interpolate horizontally by the decimal part of u and vertically interpolate the horizontal components by the decimal part of v x = floor(u) a=u-x y = floor(v) b=v–y T(u,v) = (1 – a)[(1 – b)T(x, y) + bT(x, y + 1)] + a[(1 – b)T(x + 1, y) + bT(x + 1, y + 1)] = (1 – a)(1 – b)T(x, y) + a(1 – b)T(x + 1, y) + (1 – a)bT(x, y + 1) + abT(x + 1, y + 1) This is essentially what you did in filter when scaling up Hardware can do this almost for free and I can’t think of a card that doesn’t do it by default Not so free in a software engine INTRODUCTION TO COMPUTER G RAPHI C S Mipmapping Mip = multum in parvo (many in a small place) Solves the aliasing problem when scaling down It’s possible for more than one texel may cover the area of a pixel (edges of objects, objects in the distance…). We could find all texels that fall under that pixel and blend them, but that’s too much work This problem causes temporal aliasing Will bilinear filtering help? Will it solve the problem? Solution: more samples per pixel or lower the frequency of the texture • Mipmapping solves the problem by taking the latter approach Doing this in real time is too much work so we’ll precompute • Take the original texture and reduce the area by 0.25 until we reach a 1 x 1 texture • Use a good filter and gamma correction when scaling • If we use a Gaussian filter, this is called a Gaussian pyramid “Predict” how bad the aliasing is to determine which mipmap level to use How much more memory are we using? Can potentially increase texture performance (Lars story) Cards do mipmapping and bilinear filtering by default. A good deal of console games don’t do mipmapping, why? n n i 0 j 0 (2ni )2 4 j n 1 4n 1 3 4 1 4n 1 1 4 3 (2n ) 2 3 4n 3 4 1 1 3 3 INTRODUCTION TO COMPUTER Problem Solved… G RAPHI C S INTRODUCTION TO COMPUTER G RAPHI C S We’re good. A little too good. We got rid of aliasing, but now everything is too blurry! Let’s take more samples. Take a sample from the mipmap level above and below the current one and do bilinear filtering on the current mipmap level => Trilinear Filtering Trilinear filtering makes it look a little better but we’re still missing something… If we’re going to take even more samples we better be taking them correctly. Key observation: suppose we take a pixel and backwards map it onto a texture. Is the pixel always a nice little square* with sides parallel to the texture walls? NO! Bilinear and trilinear filtering are isotropic because they sample the same distance in all directions. Now we’re going to sample more where it is actually needed * Of course, a pixel is NOT a tiny little square. But let’s suppose it is… INTRODUCTION TO COMPUTER G RAPHI C S Anisotropic Filtering Anisotropic = not isotropic (surprise). Also called aniso or AF for short. There are a couple of aniso algorithms that don’t use mipmapping but our cards already do mipmapping really well so we’ll build off of that. When the pixel is backwards mapped, the longest side of the quad determines the line of anisotropy and we’ll take a hojillion samples along that line across mipmaps. Aniso and FSAA are the two big features of today’s modern cards • ATI and NVIDIA have different algorithms that they guard secretively and continue to improve/screw up We could be taking up to 128 samples per pixel! This takes serious bandwidth. This is orders of magnitude more costly than bilinear (4) or trilinear (6) filtering. Pictures! INTRODUCTION TO COMPUTER G RAPHI C S Aniso Rules (1/3) richleader.com INTRODUCTION TO COMPUTER Aniso Rules (2/3) Serious Sam extremetech.com G RAPHI C S INTRODUCTION TO COMPUTER G RAPHI C S Aniso Rules (3/3) Serious Sam extremetech.com INTRODUCTION TO COMPUTER G RAPHI C S Texture Generation Who needs artists? Procedural Texturing • Use a random procedure to generate the colors • Perlin noise (not just for color) • Good for wood, marble, water, fire… • Unreal Tournament did it quite a bit Texture Synthesis • No games use this to my knowledge • Efros & Leung, Ashikhmin use neighborhood statistics • Cohen (Siggraph 2003) has a much faster tile based method