graphics cns 3540. graphics in games none ascii art (so called...) images –bitmaps –vector 2d...
Post on 20-Dec-2015
223 Views
Preview:
TRANSCRIPT
Graphics in games• None• ASCII art (so called...)• Images
– Bitmaps– Vector
• 2D animation• Video• 3D animation
3D GraphicsCommonly used libraries:• OpenGL www.opengl.org
• Microsoft DirectXhttp://msdn.microsoft.com/directx
Many other libraries, wrappers, and game engines are built on one or both of these libraries.
OpenGL vs. DirectX
• Open standard• Provides interface to hardware acceleration
• Portable• Widely used in commercial and non-commercial games
• No native image or 3D model file format
• GLUT
• Proprietary• Provides interface to hardware acceleration
• Only on Microsoft platforms
• Widely used in commercial and non-commercial games
• Native .x 3D model format, loaders for common bitmap formats
• Windows API• Direct Play, Direct X Audio, etc.
"OpenGL is the only truly open, vendor-neutral, multiplatform graphics standard."
http://www.opengl.org/about/overview/
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 with3D graphics
Suppose that I want to make a 3D display for UV Bots (which I do...)
How do I do that?
We'll look at how do that with some references to the book's game engine and Irrlicht (irrlicht.sourceforge.net)
Nine easy stepsNine moderat
ely difficult steps
1. Find or make 3D models2. Initialize software
and hardware3. Load models4. Add models to world5. Lights6. Camera7. Action8. Interaction9. This, that, and the
other
Nine frustrating steps that will make you wish you were using Klingon pain sticks instead and will make you want to go into something simple like neurosurgery...
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 ethical)
Making models• Blender 3D
blender.org
• Milkshape 3D• Wings 3D
www.wings3d.com
• 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
• .3ds - Original 3DS max format• .max - Newer 3DS max format• .x - Direct X• .dxf - created for AutoCAD by AutoDesk
• .obj - Wavefront (an ASCII format)
• .mdl - Quakeplus many morefileformat.info
.x DirectX• Both text and binary formats• DirectX SDK has loaders• Includes animation• Specification is available but is not for the faint-hearted
• Various importers, exporters, converters, but many are flaky
.obj Wavefront• Wavefront (which merged with Alias, which was bought by AutoDesk)
• Text format• Relatively simple• Blender can read/write, Irrlicht can load
• No animation
.ms3d Milkshape• Binary format• Includes animation• Reading and writing:
– Chumbalum's web site (www.swissquake.ch/chumbalum-soft) has C++ code to load .ms3d files
– Irrlicht can load– Blender cannot read or write
2. Initialize software and hardwareDirectX
• Our 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 hardwareOpenGL
• 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 modelsChoices:• 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
x
y
z
y
x
z
left-handed
right-handed
"The right-handed system is universally accepted as the standard in mathematics and the physical sciences."
wikipedia
but... 3DS max vs. DirectX
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
y
x
z
Local vs. global coordinates
y
xz
y
xz
The front turrets have the same local coordinates for both instances of the model. The front turrets will have
different global coordinates for each instance of the model.
How do the x, y, and z global coordinates compare for these two instances of the bot model?
Parent/Child hierarchyWhen the bot turns, the turret should turn also, but sometimes the turret should turn when the bot doesn't.
• Frames• Scene graph• Submeshes
Lights! Camera! Action!
Some material in this section comes fromIntroduction to Game Programming, edited by Steve Rabin
LIGHTING
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 lightingWe'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
What happens if your scene has too much ambient light?
Hemisphere lighting
Sun + sky + ground
Useful approximation for some outdoor scenes
direct sunlightdiffracted sunlightreflected sunlight
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
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
From local to output coordinates
Programming Role-Playing Games with DirectX,by Jim Adams
UntransformedVertex
WorldTransformation
MatrixView
TransformationMatrix
ProjectionMatrixOutput
TransformedVertex
local coordinates place object in
world
choose point of view
viewing frustum
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'
=xyz
2 0 00 2 00 0 2
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 4-element vectors, and 4x4 transformation matrices.
From local to world coordinates
UntransformedVertex
WorldTransformation
MatrixView
TransformationMatrix
ProjectionMatrixOutput
TransformedVertex
local coordinates place object in
world
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
• 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.
HRESULT IDirect3DDevice8::SetTransform(D3DTRANSFORMSTATETYPE State, // D3DTSWORLDCONST D3DMATRIX *pMatrix) // World matrix to set
Programming Role-Playing Games with DirectX, by Jim Adams
From world to view coordinates
UntransformedVertex
WorldTransformation
MatrixView
TransformationMatrix
ProjectionMatrixOutput
TransformedVertex
place object in world
choose point of view
DirectX
• Call SetTransform with D3DTS_VIEW parameter.
• Only has to be done once (until view is changed).
• How do you make the view matrix (pMatrix)?
HRESULT IDirect3DDevice8::SetTransform(D3DTRANSFORMSTATETYPE State, // D3DTS_VIEWCONST D3DMATRIX *pMatrix) // View matrix to set
Programming Role-Playing Games with DirectX, by Jim Adams
DirectX
Making a view matrix
• 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
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);
OpenGLvoid 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();}
glMatrixMode is similar to SetTransform in DirectX
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
Cameras
• Game engines usually provide a camera class with methods to change its location and orientation.
• Our textbook defines a camera class (cCamera) as part of the Graphics Core.
From view to projection
UntransformedVertex
WorldTransformation
MatrixView
TransformationMatrix
ProjectionMatrixOutput
TransformedVertex
choose point of view
viewing frustum
The viewing frustum
near clipping plane
far clippingplane
viewing frustum
Objects that are outside the viewing frustum will not be rendered.
From view to output
UntransformedVertex
WorldTransformation
MatrixView
TransformationMatrix
ProjectionMatrixOutput
TransformedVertex
output to screen
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
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 meshhttp://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
InterpolationFilling in between keyframes
For each coordinate of each vertex:xs = starting coordinate value
xf = final coordinate value
How do we calculate an intermediate value?
picture from www.w3.org
Skeletal animationModel contains:• bones connected to make a skeletal structure
• skin: textured mesh• mapping from skin to bones:vertex weightingIf 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
top related