3D Computer Games Peter Wonka pwonka@gmail.com Doom Peter Wonka, ASU 101 1 Unreal Tournament Peter Wonka, ASU 101 2 How to implement a Computer Game? Computer Games need to be fast at least 60 image per second (the monitor refresh rate) You need an efficient programming language You have to learn C++ You cannot use Java, Visual Basic, C# for critical routines. Note: A mixture of languages can be used but right now the performance critical parts are dominated by C++ in the industry Efficient Programming Language is not enough We need an efficient architecture Regular Intel / AMD CPU is too slow Peter Wonka, ASU 101 3 Computation Speed How can we make computation faster? Idea: Parallel Computing In the last years most speed gains come from massive parallel computation Graphics hardware is a cheap parallel “supercomputer” that can work with your PC You need to study computer architecture and parallel computing to build fast hardware You need to study computer architecture and parallel computing to implement fast algorithms! Fast implementation is not always intuitive and requires detailed system level knowledge Peter Wonka, ASU 101 4 NVidia Architecture / DirectX 10 16 Sets of 8 “streaming processors” (SP) 128 processors are a lot faster than one Peter Wonka, ASU 101 5 Why is graphics hardware so fast? Same small program is run for a large amount of data Restricted memory access for reading memory Very restricted memory access for writing to memory Peter Wonka, ASU 101 6 How to control the game? Keyboard, Mouse, Trackball You need a good library to manage input You need to know about operating systems Peter Wonka, ASU 101 7 How to store a scene? How to model a scene? Professional 3D software (e.g. Maya, 3D Max) Design problem, not taught in computer science! How to store a scene? Super Simple First Try (no colors) class point { float x,y,z; }; class triangle { point v1, v2, v2; }; class object { DynamicArray< triangle > TriangleArray; }; v1 v2 Peter Wonka, ASU 101 v3 8 How to draw a scene? Super Simple Rendering Algorithm (Wireframe) For each object o For each triangle t in o.TriangleArray v1projected = project t.v1 onto image plane v2projected = project t.v2 onto image plane v3projected = project t.v3 onto image plane Draw line from v1projected to v2projected Draw line from v2projected to v1projected Draw line from v3projected to v1projected Peter Wonka, ASU 101 9 How to project a point on an image plane? Points in 3D are encoded as vectors in a 4D vector space. v1 = (x,y,z,1), e.g. v1 = (7, 5, 3, 1) You need to understand vectors and vector spaces Why do you need 4 coordinates and why is the fourth coordinate always 1? These coordinates are called homogenous coordinates Why will not be answered in this class y v1 z Peter Wonka, ASU 101 x 10 How to project a point on an image plane? Points are projected using matrix multiplication: v1Projected = ProjectionMatrix * v1 ProjectionMatrix is defined by a virtual camera in the scene Virtual Camera 3 Virtual Camera 1 v1 Virtual Camera 2 Peter Wonka, ASU 101 11 How to transform objects in the scene? Most important transformations: Translation and Rotation You need matrix vector multiplication again Simply multiply all vertices of an object Below: Rotation around the z-axis with angle theta x' cos y ' sin z' 0 1 0 y x Peter Wonka, ASU 101 z 12 sin cos 0 0 0 0 x 0 0 y . 1 0 z 0 1 1 More Transformations How to compound transformations? You need matrix - matrix multiplication How to invert a transformations? You need to compute inverse matrices How to determine which transformations can be inverted? You need the concept of singular matrices Summary You need to implement fast code in C++ You need to learn Linear Algebra!! Peter Wonka, ASU 101 13 Shooting in Games? Your character shoots (along a straight line) What is hit? Super Simple Algorithm construct a straight line l CharacterPosition ShootingDi rection for each object o for each triangle t in o.TriangleArray temp_dist / temp_loc Intersect( t, l ) if (hit_distance < smallest_distance) hit_dist / hit_location = temp_dist / temp_loc Peter Wonka, ASU 101 14 Shooting in Games Problem: Algorithm is inefficient If the scene has 1M triangles we wait for a very long time Solution: Use hierarchical data structures Octree (quadtree is 2d version) Kd-tree BSP-tree We need knowledge about algorithms and (spatial) data structures Peter Wonka, ASU 101 15 Quadtrees Quatree is a spatial data structure Root node encloses a quadratic (rectangular space) Every internal node has four children Space is subdivided regularly root 0 1 0 Peter Wonka, ASU 101 16 2 1 3 2 3 Better Intersection Top down Front to back Recursive algorithm 4 3 2 1 Peter Wonka, ASU 101 17 How to create nice Shading Effects? Idea: Create a rough 3D models with Triangles Pretend the surfaces are smooth and have details You need to use (Vector) Calculus, Differential Geometry, and Geometric Modeling Peter Wonka, ASU 101 18 How to compute nice lighting effects? Problem: What color has a point x in the image? During the game: there is not time Solution: Precomputation Example Formulation: E ( x) L( x ) cos( N x , )d x x Peter Wonka, ASU 101 Translation: The color (light energy) of the point E(x) is the sum of all incoming light energy in a hemisphere of x You need Calculus 19 Integration? Good news: no analytic computation required nice and simple methods exist Peter Wonka, ASU 101 20 Integration Low dimensional Integrals trapezoidal integration gaussian quadrature Higher dimensional Integrals f ( x)dx f ( x1 ,..., xn )dx1...dxn Monte Carlo integration Randomized Algorithms Monte Carlo – use randomness but results depends on the sequence of random numbers Las Vegas – use randomness but always give the same answer in the end (differ in speed) Peter Wonka, ASU 101 21 Monte Carlo Estimator Algorithm Idea: b Question : f ( x) Pick N random variables Xi Sample function at Xi Compute weighted average a Question: How to exactly compute the average? a Peter Wonka, ASU 101 b 22 Monte Carlo Estimator b Question : f ( x) a Need : supply of uniform random variables X i [a, b] ba N Monte Carlo Estimator:FN f (Xi ) N i 1 How can we show that the estimate is “correct”? What does it mean to be correct? You need to learn Probability and Statistics Peter Wonka, ASU 101 23 Monte Carlo Estimator Proof Idea: Show that the expected value is the value of the integral Proof details are beyond the scope of this lecture, just to give you an idea: b E ( FN ) f ( x) a Proof : b a N ba N E[ FN ] E f ( X i ) E f ( X i ) N N i 1 i 1 ba N ba N b E[ f ( X i )] f ( x) p ( x)dx a N i 1 N i 1 1 N N b i 1 a b f ( x)dx f ( x)dx Peter Wonka, ASU 101 a 24 Monte Carlo Estimator Extends easily to multiple dimensions Still very simple to implement x1 y1 z1 Question : f(x,y,z)dzdydx x0 y0 z0 Multidimen sional Samples : X i ( xi , yi , zi ) 1 e.g. p( X ) const ( x1 x0 )( y1 y0 )( z1 z0 ) ( x1 x0 )( y1 y0 )( z1 z0 ) N f (Xi) FN N i 1 Peter Wonka, ASU 101 25 How to cheat in games? Multiplayer Online Games Cheating Ideas Modify the rendering of the game so you can see through walls Send incorrect movement updates for your opponents Use a bot You connect to a bot server, the bot connects to the real game bot filters your interaction You move, but the bot automatically shoots with “perfect” aim Game companies need to prevent cheating You need to learn about Networking Security Artificial Intelligence Peter Wonka, ASU 101 26 How to animate fire, water, smoke? You need PDEs = partial differential equations E.g. Navier Stokes Equations Peter Wonka, ASU 101 27 Summary Implementation skills and algorithms are important for computer games Mathematics is also important (especially if you want to have a very good job) Peter Wonka, ASU 101 28 Faculty Peter Wonka (Computer Graphics) Gerald Farin (Geometric Modeling) Greg Nielson (Scientific Visualization) Ashish Amresh (Computer Games) Peter Wonka, ASU 101 29