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
Graphics CS 3540 Graphics in games • None • ASCII art (so called...) • Images – Bitmaps – Vector • 2D animation • Video • 3D animation 3D Graphics Commonly used libraries: • OpenGL www.opengl.org • Microsoft DirectX http://msdn.microsoft.com/directx Many other libraries, wrappers, and game engines are built on one or both of these libraries. OpenGL vs. • Open standard • Provides interface to hardware acceleration • Portable • Widely used in commercial and noncommercial games • No native image or 3D model file format • GLUT "OpenGL is the only truly open, vendor-neutral, multiplatform graphics standard." http://www.opengl.org/about/overview/ DirectX • Proprietary • Provides interface to hardware acceleration • Only on Microsoft platforms • Widely used in commercial and noncommercial games • Native .x 3D model format, loaders for common bitmap formats • Windows API • Direct Play, Direct X Audio, etc. Fun with 3D libraries... Both OpenGL and DirectX are complicated libraries and learning how to use them well is not easy. DirectX, in particular, has gone through major changes that obsolete many earlier books and web sites. Microsoft's documentation can also change in confusing ways. Making a game with 3D graphics Suppose that I want to make a 3D game. How do I do that? We'll look at how do that with some references to Unity and other game engines and libraries. Nine easy steps 1. Find or make 3D models 2. Initialize software and hardware 3. Load models 4. Add models to world 5. Lights 6. Camera 7. Action 8. Interaction 9. This, that, and the other Unity makes these steps much easier, but it’s good to know at least a little bit about what’s happening behind the scenes. 1. Find or make 3D models Finding models • Google (what else?) • Call in favors from your artistic friends (you have lots of those, right?) • Borrow them from other games (but only if it's legal/ethical) Start with boxes! Making models • Blender 3D blender.org • Sketchup www.sketchup.com • Sculptris pixologic.com/sculptris • Various commercial packages, most of which are very expensive 3D model formats • Bad news: There are no "standard" formats for 3D model files that are comparable to JPEG or PNG. • Worse news: Converting between 3D file formats is a pain and the model will probably not look the same after conversion. 3D model formats • Text or binary? • What documentation is available? – Is a file format specification available? • How complicated is the file format? • What software can read/write the format? – Modeling software – Conversion software – Loaders and game engines • Does it include animation? 3D model formats • .fbx – Used by AutoDesk and others • .dae – COLLADA www.khronos.org/collada/ plus many more . obj - Wavefront (an ASCII format) • .maya Maya (3D modeling software) • .dxf - created for AutoCAD by AutoDesk fileformat.info .obj Wavefront • Wavefront (which merged with Alias, which was bought by AutoDesk) • Text format • Relatively simple • No animation • Blender can read/write, Unity can load File formats in Unity • Can read .FBX, .dae, .3DS, .dxf, and .obj files • Natively imports Maya files • “Natively” imports Blender files (via FBX) http://unity3d.com/support/document ation/Manual/HOWTOimportObject.html 2. Initialize software and hardware DirectX • One book has eight fun-filled pages on how to initialize DirectX, MSDN has a lot more than that... • Many options, choosing the wrong options can leave you with a blank screen • Beware of COM • Make sure you deallocate, close, destroy everything you allocate, open, or create if you want to avoid memory leaks 2. Initialize software and hardware OpenGL • Setup is relatively easy if you use GLU and GLUT, but can still be tricky Encapsulate! • Set-up code can be messy, so hide the details. • Encapsulation makes it easier to port to another OS or another graphics library. 3. Load models Choices: • Write your own loader – Not easy since 3D file formats are complicated and often not well documented • Use someone else's source code – Hard to say how well it will work • Use a game engine – Only an option if the engine supports your format Components of 3D models • Vertices - points in 3D space • Edges - connect vertices • Faces/Polygons – often triangles, sometimes quads • Meshes • Normals - Which way is up? (or out?) – Used for shading More components of models • Hierarchies - parent/child • Colors • Textures – Combinations of other materials – Images - often stored in separate file – Must be mapped to mesh • Bones • Animation - key frames 4. Add model to the world • Left-handed or Right-handed? • Location, size, and orientation • Parent-child hierarchy Left-handed vs. Right-handed y y left-handed x z x z right-handed "The right-handed system is universally accepted as the standard in mathematics and the physical sciences." wikipedia but... 3DS max vs. DirectX Unity is left-handed Lost in Space(s) • Model space (or local coordinates) – Coordinates are relative to local origin – Used to create model • World space – Coordinates are relative to global origin – Used to place models within the world • Screen space – Projected into 2D plane Local vs. global coordinates The front turrets have the same local coordinates for both instances of the model. y z x y The front turrets will have different global coordinates for each instance of the model. y z How do the x, y, and z global coordinates compare for these two instances of the bot model? z x x Parent/Child hierarchy When the bot turns, the turret should turn also, but sometimes the turret should turn when the bot doesn't. • Frames • Scene graph • Submeshes Adding models in Unity Some links to the Unity User Guide • Importing Assets • Prefabs Lights! Camera! Action! LIGHTING Some material in this section comes from Introduction to Game Programming, edited by Steve Rabin Questions • How much light is incident on a surface? • From which direction does the light come? • How is the light absorbed, reemitted, and reflected? • Which outgoing rays reach the eye of the viewer? Answers (or at least ways to find the answers) • Forward tracing – trace every photon emitted by a light source and figure out where it goes – photon mapping: high quality but extremely slow • Backward tracing – trace a hypothetical photon that hit the eye to see which object it came from – raytracing: practical, but not for real time rendering Answers (or at least ways to find the answers) • "Middle-out" evaluation – "Given this bit of surface, how much light came from these sources of light and ended up hitting the eye?" * – Looks at triangle currently being drawn – Real time rendering has to compromise between speed and quality •p. 471, Introduction to Game Programming, •edited by Steve Rabin Kinds of lighting We'll look at how different kinds of lighting affect this scene. Note: These pictures were made in Blender, not with real-time rendering. Ambient light • Light that is not attributed to any particular source • Can be a reasonable approximation for some multi-light situations, especially indoors • Unity: Edit->Render Settings What happens if your scene has too much ambient light? Too much ambient light Diffuse lighting Specular lighting Hemisphere lighting Sun + sky + ground direct sunlight diffracted sunlight reflected sunlight Useful approximation for some outdoor scenes Environment map • Approximates reflections • Cube map: – Put cube representing environment around the object being rendered. – Trace vector from eye to object, reflect from object, and see where it strikes the cube. • Same idea as hemisphere lighting but can contain images or more varied colors Shadows • Can be done with ray-tracing. • Unity can do shadows Types of lights Spot Point Directional Properties of lights • • • • Type: point, spot, directional Position: as for any object Color: color of light Range: how far the light shines Properties of spotlights Same as point lights, plus: • Direction • Falloff • Attenuation • Radius of inner and outer cones Lights in Unity • Point, directional, spot • Shadows (only in the Pro version) • Ambient light: Edit->Render Settings https://docs.unity3d.com/Manual/Lighting.html Particle systems https://en.wikipedia.org/wiki/Particle_system http://natureofcode.com/book/chapter-4-particle-systems/ http://buildnewgames.com/particle-systems/ Particle systems in Unity Public domain images from https://en.wikipedia.org/wiki/Particle_ system Lights! Camera! Action! POINT OF VIEW and RENDERING From local to output coordinates Untransformed Vertex local coordinates Output Transformed Vertex World Transformation Matrix place object in world View Transformation Matrix Projection Matrix choose point of view viewing frustum Programming Role-Playing Games with DirectX, by Jim Adams The Matrix (actually, lots of matrices) Matrices are used for transformations: • Translate – move from one place to another • Rotate – center of rotation is important • Scale – center of scaling is important Using a matrix to transform a vertex • Vertex is represented as three coordinates – vector from origin to location of vertex – 1x3 matrix • Multiply vertex matrix by transformation matrix to get new vertex matrix x' y' = z' 2 0 0 0 2 0 0 0 2 x y z What transformation does this represent? Combining transformations • Transformations can be combined by multiplying matrices together • Combine matrices, then multiply each vertex in object by combined matrix. – More efficient than multiplying each vertex by each of the transformation matrices. Note: In order to represent translation with matrix multiplication you have to add an extra coordinate. You will sometimes see vertices represented by 4element vectors, and 4x4 transformation matrices. From local to world coordinates Untransformed Vertex local coordinates World Transformation Matrix place object in world Output Transformed Vertex Projection Matrix View Transformation Matrix Placing an object in the world • Models usually have their center at the origin (0, 0, 0) in local coordinates. • To place a model in the world, the coordinates of all vertices in the model have to be transformed (translated, rotated, and/or scaled). • Usually the world transformation is kept in the (C++) object for the model and then applied each time the model is rendered. Animation using a model's world transformation • By changing the world transformation each frame, you can animate a model: move, rotate, scale, or some combination • Since the world transformation applies to the whole model, this kind of animation affects the whole model also. DirectX HRESULT IDirect3DDevice8::SetTransform( D3DTRANSFORMSTATETYPE State, // D3DTSWORLD CONST D3DMATRIX *pMatrix) // World matrix to set • Call SetTransform before drawing each model • DirectX will apply pMatrix to each vertex in the model • pMatrix will usually be some combination of translation, rotation and scaling • Game engines (e.g. Irrlicht) will provide easier ways of setting the location, orientation, and size of models. Programming Role-Playing Games with DirectX, by Jim Adams From world to view coordinates Untransformed Vertex Output Transformed Vertex World Transformation Matrix place object in world View Transformation Matrix Projection Matrix choose point of view Choosing a point of view DirectX HRESULT IDirect3DDevice8::SetTransform( D3DTRANSFORMSTATETYPE State, // D3DTS_VIEW CONST D3DMATRIX *pMatrix) // View matrix to set • Call SetTransform with D3DTS_VIEW parameter. • Only has to be done once (until view is changed). • How do you make the view matrix (pMatrix)? Programming Role-Playing Games with DirectX, by Jim Adams DirectX Making a view matrix D3DXMATRIX matView; D3DXVECTOR3 vecVP, vecTP, vecUp(0.0f, 1.0f, 0.0f); vecVP.x = XPos; vecVP.y = YPos; vecVP.z = ZPos; vecTP.x = vecTP.y = vecTP.z = 0.0f; D3DXMatrixLookatLH(&matView, &vecVP, &vecTP, &vecUp); • vecVP is the coordinates of the viewpoint – Location of the viewer's eye or a camera • vecTP is the coordinates of the target – what the camera is pointing at • vecUp tells the orientation of the camera Programming Role-Playing Games with DirectX, by Jim Adams OpenGL void draw(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(xO, yO, zO, xref, yref, zref, Vx, Vy, Vz); gModel.draw(); glFlush(); } xO, yO, zO specify the location of the viewpoint (camera) xref, yref, zref specify the location of the target (look-at point) Vx, Vy, Vz specify the orientation of the camera glMatrixMode is similar to SetTransform in DirectX Cameras • Game engines usually provide a camera class with methods to change its location and orientation. • Our (former) textbook defines a camera class (cCamera) as part of the Graphics Core. From view to projection Untransformed Vertex World Transformation Matrix View Transformation Matrix Output Transformed Vertex Projection Matrix viewing frustum choose point of view The viewing frustum viewing frustum far clipping plane near clipping plane Objects that are outside the viewing frustum will not be rendered. Unity: You can set the clipping planes for a camera in the Inspector. From view to output Untransformed Vertex World Transformation Matrix View Transformation Matrix Output Transformed Vertex output to screen Projection Matrix viewing frustum Output • DirectX provides various functions for drawing primitives and meshes. • These functions require some set up, and use the current settings for transformations. • DirectX primitives: D3DPT_POINTLIST, D3DPT_LINELIST, D3DPT_LINESTRIP, D3DPT_TRIANGLELIST, D3DPT_TRIANGLESTRIP, D3DPT_TRIANGLEFAN Lights! Camera! Action! ANIMATION Remember... The Matrix? The same matrices that are used to place objects in the world can be used to move and resize objects: • Translate – move from one place to another • Rotate – center of rotation is important • Scale – center of scaling is important Character Animation • • • • Morphing Animation Interpolation Skeletal Animation Kinematics and Inverse Kinematics Morphing animation • Quake is the classic example of a game that uses morphed animation. MD2 file format: • A set of named animations • Each animation contains a sequence of frames • Each frame contains a mesh http://tfc.duke.free.fr/old/models/md2.htm Question If I am writing a game that uses the MD2 file format, and I want a high frame rate, does that mean that my MD2 file is going to be huge? Keyframes • Only store the most important ("key") frames in the MD2 file. • When animating the model, figure out the intermediate frames by comparing the positions of corresponding vertices. • This is called interpolation Interpolation Filling in between keyframes For each coordinate of each vertex: xs = starting coordinate value xf = final coordinate value picture from www.w3.org How do we calculate an intermediate value? Interpolating character poses picture from www.sarge-sdk.com Skeletal animation Model contains: • bones connected to make a skeletal structure • skin: textured mesh • mapping from skin to bones: vertex weighting If you move a bone, what skin moves with it? picture from Blender documentation Skeletal animation • Game program animates character by moving bones--skin will move accordingly • File formats: • Milkshape (.ms3d), DirectX (.x) picture from Blender documentation Skeletal animation Skeletal animation can also make use of key frames and interpolation, but they will be specified in terms of bones rather than in terms of vertices in a mesh.