59
Game Programming Game Programming (Mapping) (Mapping) 2013. Spring

Game Programming (Mapping) 2013. Spring

Embed Size (px)

Citation preview

Page 1: Game Programming (Mapping) 2013. Spring

Game ProgrammingGame Programming(Mapping)(Mapping)

Game ProgrammingGame Programming(Mapping)(Mapping)

2013. Spring

Page 2: Game Programming (Mapping) 2013. Spring
Page 3: Game Programming (Mapping) 2013. Spring
Page 4: Game Programming (Mapping) 2013. Spring
Page 5: Game Programming (Mapping) 2013. Spring
Page 6: Game Programming (Mapping) 2013. Spring
Page 7: Game Programming (Mapping) 2013. Spring
Page 8: Game Programming (Mapping) 2013. Spring
Page 9: Game Programming (Mapping) 2013. Spring

Texture Mapping

■ The problem Colors, normals, etc. are only specified at vertices. How do we add detail between vertices?

■ Solution Specify the details in an image (the texture) and specify how

to apply the image to the geometry (the map)

■ Works for shading parameters other than color, as we shall see The basic underlying idea is the mapping

Page 10: Game Programming (Mapping) 2013. Spring

Basic Mapping

■ The texture lives in a 2D space Parameterize points in the texture with 2 coordinates: (s,t) These are just what we would call (x,y) if we were talking

about an image, but we wish to avoid confusion with the world (x,y,z)

■ Define the mapping from (x,y,z) in world space to (s,t) in texture space

■ With polygons: Specify (s,t) coordinates at vertices Interpolate (s,t) for other points based on given vertices

Page 11: Game Programming (Mapping) 2013. Spring

Basic Mapping

Page 12: Game Programming (Mapping) 2013. Spring

I assume you recall…

■ Texture sampling (aliasing) is a big problem Mipmaps and other filtering techniques are the solution

u

v

d

Page 13: Game Programming (Mapping) 2013. Spring

GL_NEAREST GL_LINEAR Mipmappping

I assume you recall…

Page 14: Game Programming (Mapping) 2013. Spring

I assume you recall…

■ The texture value for points that map outside the texture image can be generated in various ways Repeat, Clamp, …

■ Width and height of texture images is constrained (powers of two, sometimes must be square)

Wrap S : GL_CLAMPWrap T : GL_CLAMP

Wrap S : GL_CLAMPWrap T : GL_REPEAT

Wrap S : GL_REPEATWrap T : GL_CLAMP

Wrap S : GL_REPEATWrap T : GL_REPEAT

Page 15: Game Programming (Mapping) 2013. Spring

Textures in Games

■ The game engine provides some amount of texture support

■ Artists are supplied with tools to exploit this support They design the texture images They specify how to apply the image to the object

■ Commonly, textures are supplied at varying resolutions to support different hardware performance Note that the texture mapping code does not need to be

changed - just load different sized maps at run time

■ Textures are, without doubt, the most important part of a game’s look

Page 16: Game Programming (Mapping) 2013. Spring

Example Texture Tool

Page 17: Game Programming (Mapping) 2013. Spring

Standard Pipeline

Page 18: Game Programming (Mapping) 2013. Spring

Multitexturing■ Some effects are easier to implement if multiple textures can be applied

Future lectures: Light maps, bump maps, shadows, …

Key-frameKey-framemodelmodelgeometrygeometry

Key-frameKey-framemodelmodelgeometrygeometry DecalDecal

skinskinDecalDecalskinskin

BumpBumpskinskinBumpBumpskinskin

GlossGlossskinskinGlossGlossskinskin

WOW!WOW!ResultResultWOW!WOW!ResultResult

++++

Page 19: Game Programming (Mapping) 2013. Spring

Packing Textures

■ Problem The limits on texture width/height make it inefficient to store

many textures For example: long, thin objects

■ Solution Artists pack the textures for many objects into one image

• The texture coordinates for a given object may only index into a small part of the image

• Care must be taken at the boundary between sub-images to achieve correct blending

• Mipmapping is restricted

Page 20: Game Programming (Mapping) 2013. Spring

Combining Textures

Page 21: Game Programming (Mapping) 2013. Spring

Texture Matrix

■ Normally, the texture coordinates given at vertices are interpolated and directly used to index the texture

■ The texture matrix applies a homogeneous transform to the texture coordinates before indexing the texture

■ What use is this?

Page 22: Game Programming (Mapping) 2013. Spring

Animating Texture (method 1)

■ Loading a texture onto the graphics card is very expensive

■ But once there, the texture matrix can be used to “transform” the texture For example, changing the translation can select different

parts of the texture

■ If the texture matrix is changed from frame to frame, the texture will appear to move on the object

■ This is particularly useful for things like flame, or swirling vortices, or pulsing entrances, …

Page 23: Game Programming (Mapping) 2013. Spring

Projective Texturing

■ The texture should appear to be projected onto the scene, as if from a slide projector

■ Solution: Equate texture coordinates with world coordinates Think about it from the projector’s point of view: wherever a

world point appears in the projector’s view, it should pick up the texture

Use a texture matrix equivalent to the projection matrix for the projector – maps world points into texture image points

■ Details available in many places

Page 24: Game Programming (Mapping) 2013. Spring

Environment Mapping

■ Environment mapping produces reflections on shiny objects

■ Texture is transferred in the direction of the reflected ray from the environment map onto the object

■ Reflected ray: R=2(N·V)N-V■ What is in the map?

Object

ViewerReflected ray

Environment Map

Page 25: Game Programming (Mapping) 2013. Spring

Environment Maps

■ The environment map may take one of several forms: Cubic mapping Spherical mapping (two variants) Parabolic mapping

■ Describes the shape of the surface on which the map “resides”

■ Determines how the map is generated and how it is indexed

■ What are some of the issues in choosing the map?

Page 26: Game Programming (Mapping) 2013. Spring

Environment Mapping

■ Cube mapping is the norm nowadays

x

y

z

neye

Page 27: Game Programming (Mapping) 2013. Spring

Environment Mapping

www.debevec.org

Need For Speed Underground

Far Cry

Page 28: Game Programming (Mapping) 2013. Spring

Light Maps

■ Speed up lighting calculations by pre-computing lighting and storing it in maps Allows complex illumination models to be used in generating

the map (eg shadows, radiosity) Used in complex rendering algorithms (Radiance), not just

games

■ Issues: How is the mapping determined? How are the maps generated? How are they applied at run-time?

Page 29: Game Programming (Mapping) 2013. Spring

Example

www.flipcode.com

Call

of

du

ty

Page 30: Game Programming (Mapping) 2013. Spring

Example

Nearest interpolation Linear interpolation

What type of lighting (diffuse, specular, reflections) can the map store?

Page 31: Game Programming (Mapping) 2013. Spring

Example

No light maps With light maps

Page 32: Game Programming (Mapping) 2013. Spring

Applying Light Maps

■ Use multi-texturing hardware First stage: Apply color texture map Second stage: Modulate with light map

• Actually, make points darker with light map• DirectX allows you to make points brighter with texture

Page 33: Game Programming (Mapping) 2013. Spring

Fog Maps

■ Dynamic modification of light-maps■ Put fog objects into the scene■ Compute where they intersect with geometry and

paint the fog density into a dynamic light map Use same mapping as static light map uses

■ Apply the fog map as with a light map Extra texture stage

Page 34: Game Programming (Mapping) 2013. Spring

Fog Map Example

Page 35: Game Programming (Mapping) 2013. Spring

Bump Mapping

■ Texture values perturb surface normals

geometry Bump mapStores heights: can derive normals

+Bump mapped geometry

=

Page 36: Game Programming (Mapping) 2013. Spring

Dot Product bump mapping

■ Store normal vectors in the bump map■ Apply the bump map using the dot3 operator

Takes a dot product

www.nvidia.com

Page 37: Game Programming (Mapping) 2013. Spring

Normal Mapping

DOOM 3

James Hastings-Trew

Page 38: Game Programming (Mapping) 2013. Spring

Environment Bump Mapping

■ Perturb the environment map lookup directions with the bump map

Far Cry

Nvidia

Page 39: Game Programming (Mapping) 2013. Spring

Displacement Mapping

■ Normal Mapping Problem Doesn’t take into account geometric surface depth

• Does not exhibit parallax• No self-shadowing of the surface• Coarse silhouettes expose the actual geometry being

drawn

■ Displacement Mapping Displace actual positions from Heightfield Map

Page 40: Game Programming (Mapping) 2013. Spring

Displacement Mapping (Result)

■ Displacement Offset

Page 41: Game Programming (Mapping) 2013. Spring

Billboards

■ Billboards are texture-mapped polygons, typically used for things like trees Image-based rendering method where

complex geometry (the tree) is replaced with an image placed in the scene (the textured polygon)

■ The texture has alpha values associated with it: 1 where the tree is, and 0 where it isn’t So you can see through the polygon in

places where the tree isn’t

Page 42: Game Programming (Mapping) 2013. Spring

Planar Reflections (Flat Mirrors)

■ Use the stencil buffer, color buffer and depth buffer■ Basic idea:

We need to draw all the stuff around the mirror We need to draw the stuff in the mirror, reflected, without

drawing over the things around the mirror

■ Key point: You can reflect the viewpoint about the mirror to see what is seen in the mirror, or you can reflect the world about the mirror

Page 43: Game Programming (Mapping) 2013. Spring

Reflecting Objects

■ If the mirror passes through the origin, and is aligned with a coordinate axis, then just negate appropriate coordinate

■ Otherwise, transform into mirror space, reflect, transform back

MirrorWall

Page 44: Game Programming (Mapping) 2013. Spring

Reflection Example

The stencil buffer after the second pass

The color buffer after the second pass – the reflected scene cleared outside the stencil

Page 45: Game Programming (Mapping) 2013. Spring

Reflection Example

The color buffer after the final pass

Page 46: Game Programming (Mapping) 2013. Spring

So what game did you play recently ?

Page 47: Game Programming (Mapping) 2013. Spring

Why Shadows?

■ Shadows tell us about the relative locations and motions of objects

Page 48: Game Programming (Mapping) 2013. Spring

Facts about Shadows

■ Shadows can be considered as areas hidden from the light source Suggests the use of hidden surface algorithms

■ For scenes with static lights and geometry, the shadows are fixed Can pre-process such cases Cost is in moving lights or objects

■ Point lights have hard edges, and area lights have soft edges

Page 49: Game Programming (Mapping) 2013. Spring

Ground Plane Shadows

■ Shadows cast by point light sources onto planes are an important case that is relatively easy to compute Shadows cast by objects (cars,

players) onto the ground

■ Accurate if shadows don’t overlap Can be fixed, but not well

(xp,yp,zp)

(xsw,ysw,zsw)

L(directional light)

Page 50: Game Programming (Mapping) 2013. Spring

Shadows in Light Maps

■ Static shadows can be incorporated into light maps When creating the map, test for shadows by ray-casting to

the light source - quite efficient

■ Area light sources should cast soft shadows Interpolating the texture will give soft shadows, but not

good ones, and you loose hard shadows Sampling the light will give better results: Cast multiple

rays to different points on the area light, and average the results

Should still filter for best results

Page 51: Game Programming (Mapping) 2013. Spring

Soft Shadow Example

Page 52: Game Programming (Mapping) 2013. Spring

Shadows in Games

Grand Theft Auto

Metal Gear

Megaman

Page 53: Game Programming (Mapping) 2013. Spring

Projective Shadows

■ Create a texture (dark on white) representing the appearance of the occluder as seen by the light Game programmers frequently call this a shadow map Can create it by “render to texture” with the light as viewpoint

■ Use projective texturing to apply it to receivers■ Works if the appearance of the occluder from the light is

reasonably constant■ Requires work to identify occluders and receivers

Page 54: Game Programming (Mapping) 2013. Spring

Shadow Volumes

■ A shadow volume for an object and light is the volume of space that is shadowed That is, all points in the volume are in shadow for that

light/object pair

■ Creating the volume: Find silhouette edges of shadowing object as seen by the

light source Extrude these edges away from the light, forming polygons Clip the polygons to the view volume

Page 55: Game Programming (Mapping) 2013. Spring
Page 56: Game Programming (Mapping) 2013. Spring
Page 57: Game Programming (Mapping) 2013. Spring

Shadow Volumes

Page 58: Game Programming (Mapping) 2013. Spring

Shadow Volume

Page 59: Game Programming (Mapping) 2013. Spring