Top Banner
CSE 381 – Advanced Game Programming Terrain Management
41
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSE 381 – Advanced Game Programming Terrain Management.

CSE 381 – Advanced Game ProgrammingTerrain Management

Page 2: CSE 381 – Advanced Game Programming Terrain Management.

Think of it as a textured blanket

Page 3: CSE 381 – Advanced Game Programming Terrain Management.

Terrain

Obviously a rendering challenge

Also a collision detection challenge

Requires a huge amount of data for outside game environments

Usually pre-lit – what does this mean?

Typically integrated with:sky

water

sun

Page 4: CSE 381 – Advanced Game Programming Terrain Management.

Terrain Characteristics

Terrain characteristics:fidelity

how realistic is it?for model & textures

spreaddegree to which areas of the terrain are unique

freedomhow much are the player’s movements restricted by

the terrain?

Want real earth terrain data?http://edcwww.cr.usgs.gov

Page 5: CSE 381 – Advanced Game Programming Terrain Management.

Terrain Modeling

Some game engines have terrain managerswith their own GUI terrain generation tools

with their own rendering optimizations

Terrain building as modelscan also be positioned, rotated, etc. as one

Page 6: CSE 381 – Advanced Game Programming Terrain Management.

Terrain Cover

Texturing provides this

Typical textures:grass, flowers, dirt, pebbles, rocks, moss, sand, stone,

etc.

Most engines support terrain texture blendingWhat’s that?

good for transition areas at borders of differing terrain

Page 7: CSE 381 – Advanced Game Programming Terrain Management.

When making Terrain Textures

avoid making distinctive marksWhy?

avoid making textures with patternsWhy?

Remember, these will be repeated over an area

Page 8: CSE 381 – Advanced Game Programming Terrain Management.

How do you think terrain data is stored?

Height mapsWhat’s that?

a 2D image that stores topographical info for a square map region

different shades of gray represent different elevations

brightness of a pixel denotes its elevation

Page 9: CSE 381 – Advanced Game Programming Terrain Management.

More on height maps

Each pixel represents an area’s elevationex: 1 pixel is a square meter

black may be the base elevation

pure white may be the other extreme

values in between are scaled

Ex: 8 bits per pixel means 256 possible elevations

Page 10: CSE 381 – Advanced Game Programming Terrain Management.

Using Height Maps

Terrain generator reads a height map file and creates the appropriate geometryHow?

Each pixel represents a tile (square of terrain)

For each tilea vertex is generated at appropriate elevation

two triangles are generated connecting new vertex to bordering vertices

Some generators will add more verticesWhy?

Interpolation (more on this later)

Page 11: CSE 381 – Advanced Game Programming Terrain Management.

Terrain Units

WU – world unitsrefers to measurement scale for a game world

basic WU is one inch

Example:level tile is 10 X 10 units (meters or otherwise)

100 tiles X 100 tiles

level is 1000 X 1000

NOTE: each tile may have many trianglesagain, depending on interpolation

Page 12: CSE 381 – Advanced Game Programming Terrain Management.

Water

Think of as its own level objectHas a position, scale, rotationTypically specified using a single rectangle

can fit into non rectangular terrain contours

For camera out of water:typically water drawn using multiple textures

one for shallows, one for deepcan be blended via depth gradient

effects then appliedE.g., specular highlights

For camera under water:other effects like fog, viscosity, waves, etc.

Page 13: CSE 381 – Advanced Game Programming Terrain Management.

Terrain Rendering

What do we need to know?how to store the data

how to load it

how to render it

how to detect collisions with it. Why?Nothing should go below the terrain

Page 14: CSE 381 – Advanced Game Programming Terrain Management.

Height Maps Revisited

Height maps are images

When a level is loaded, how might we store terrain data such that it can be efficiently:rendered

used in collision detection calculations

Page 15: CSE 381 – Advanced Game Programming Terrain Management.

2 Terrain Management Options

1. Static Option - Load height map into a mesh (or meshes) and treat terrain as any other mesh

easy to implement

typically divided into clumps

2. Dynamic Option - Load height map data into data structure, each frame, use dynamically changing mesh based on location of frustum

that’s a bit trickier

Page 16: CSE 381 – Advanced Game Programming Terrain Management.

Clumps

What’s a clump?a chunk of terrain

subset of a level’s full terrain

What’s the benefit of managing clumps?more efficient rendering/management of terrain

Why?problem reduction

Page 17: CSE 381 – Advanced Game Programming Terrain Management.

Clump Construction Example

Suppose we are loading a 256 x 256 height map

Construct terrain as 16 x 16 clumps

How many clumps is that?256

How many vertices per height map pixels?depends on interpolation (more on this in a moment)

Page 18: CSE 381 – Advanced Game Programming Terrain Management.

How do we use the clumps?

Each frame, select only the visible clumps for rendering

How do we do that?frustum culling is one way

but there’s a simpler way

clumps are cells in the 2D grid

in what cell is the camera?render that clump

in which direction is the look at vector?render those clumps

know the max clump distance that can be viewed

Page 19: CSE 381 – Advanced Game Programming Terrain Management.

Dynamic Terrain Generation

Height map is a 2D grid

At start of level:build a recyclable mesh to store dynamic terrain

Each frame:determine where on the grid the camera is

fill dynamic terrain with visible portions of grid

Page 20: CSE 381 – Advanced Game Programming Terrain Management.

What’s the point of interpolation?

Rounding terrain

How?add more vertices to the grid

Simple algorithmadd 2 between each pair of vertices

what height?make them closer to nearest existing vertex than 1/3

of height difference. Why?

Page 21: CSE 381 – Advanced Game Programming Terrain Management.

Skies

• Add ambiance

• Add realism

• Add value

• Note:– Different genres use different approaches

• rpg vs. fps

• Inside or outside game?

Page 22: CSE 381 – Advanced Game Programming Terrain Management.

Approaches

• Skyplanes

• Skyboxes

• Skydomes

• Cloud Generation & rendering

Page 23: CSE 381 – Advanced Game Programming Terrain Management.

For all approaches

• Render in sky space

• Huh?

• Render first, with depth buffer off

• Use geometry that fits sky textures– minimize interpolation– this means sky geometry is much smaller than other

objects

Page 24: CSE 381 – Advanced Game Programming Terrain Management.

Skyplanes

• Simplest to model and texture

• Does not completely enclose camera

• Disadvantage: – End of plane is noticible– Doesn’t cover horizon

• Should only be used when mountains or other objects fills gaps

Page 25: CSE 381 – Advanced Game Programming Terrain Management.

Simplest Skyplane Approach

• Use 2 triangles– put them right over the camera– Ex: 1 y-unit above camera at all times– Move sky with camera

• Map sky texture onto geometry– again, minimize interpolation

• No minification or magnification

– What determines this? How you:• size it

• texture it

• position it

Page 26: CSE 381 – Advanced Game Programming Terrain Management.

Alternative: curved plane

• Shaped like a parachute

• Considerations:– Resolution (number of vertex rows/columns)– Radius– Height of peak– Repetitions of texture

Page 27: CSE 381 – Advanced Game Programming Terrain Management.

Creating the Geometry

• How can we create vertices that are:– evenly spaced– part of mesh centered over 0,0– in x&z range -planeSize/2 through planeSize/2

• Plane size = √((2*radius)2/2)

• Using Pythagorean theorem, in loop, calculate distance to determining y values

Page 28: CSE 381 – Advanced Game Programming Terrain Management.

Rendering Skyplanes

• Disable:– lighting– fog– depth testing

• To blend clouds with sky, enable OpenGL blending

Page 29: CSE 381 – Advanced Game Programming Terrain Management.

Sky Fading

• Note that the sky fades on the horizon, it doesn’t just get cut off

• How can we add this effect?

• For skyplane, add alpha value to all vertices:– those nearest center (0, ?, 0) fully opaque,– gradually + and – from center more transparent– use a linear, or better yet, exponential function

Page 30: CSE 381 – Advanced Game Programming Terrain Management.

Clouds with Volume

• How do we add 3Dish clouds?

• Use Layers of them – same and different textures– blend them

Page 31: CSE 381 – Advanced Game Programming Terrain Management.

Skyboxes

• Most common sky rendering technique

• Fully encloses camera

• Inexpensive

• Again, enclose camera in small box

• How does it work?– render distant scenery onto inside or cube

Page 32: CSE 381 – Advanced Game Programming Terrain Management.

Skybox Atlas

Page 33: CSE 381 – Advanced Game Programming Terrain Management.

Traditional Approach

• Render ordinary textured cube

• Align with the world axis– Centered around camera

• Render it first– Disable necessary features as before

• Size of box can be small– it should fit the textures

– make sure it’s in the frustum

Page 34: CSE 381 – Advanced Game Programming Terrain Management.

Skybox Rendering Steps

1. Clear the depth buffer

2. Disable depth test

3. Disable depth writes

4. Disable fog and lighting

5. Render the box

6. Enable depth test and writes

7. Enable lighting and fog

8. Draw the rest of the scene

Page 35: CSE 381 – Advanced Game Programming Terrain Management.

Skybox Edges

• Boxes have seams

• We don’t want those to show up

• Why would they?– mag/minification

• may sample nearby pixels

• border may be nearby

• results in poor interpolation

• Alternative, use texture coordinates that don’t start at edge, but one pixel off edge– or employ GL_CLAMP_TO_EDGE

– ignores edge texels in interpolation

Page 36: CSE 381 – Advanced Game Programming Terrain Management.

Does the skybox move?

• Not relative to the camera

• Camera can rotate, of course

• Skybox goes where ever the camera is

• Assumption is skybox art is of things very far away

Page 37: CSE 381 – Advanced Game Programming Terrain Management.

Generating Skybox Art

• Common to make a large, continuous texture– to wrap around sides, for example

• Break it up for texturing

• Good tools exist:– Skypaint by Gavin Bell

• paint seamless, panoramic 360 deg. Images

– Terragen by Planetside• generate sky boxes and terrain

Page 38: CSE 381 – Advanced Game Programming Terrain Management.

Skybox Layering

• Can provide more beautiful experiences

• Example: 3 layers– Outer layer for static background (e.g. stars)– Middle layer for dynamic stuff (e.g. clouds)– Inner layer for much closer stuff (e.g. mountains)

• Middle layer may use texture animation– Means moving textures across a geometry– How?

• Gradually adjusting texture coordinates

Page 39: CSE 381 – Advanced Game Programming Terrain Management.

Textures vs. Colors

• Note that skies transition from horizon colors to sunset colors and make a smooth transition

• Colored Vertex rendering can do this easily– more easily than texturing

• Option, use colors for outer layer, textures for inner layers

Page 40: CSE 381 – Advanced Game Programming Terrain Management.

Skydomes

• For even better sky rendering– Good for night-day transitions– Good for advanced effects– Uses one texture (for back layer)– Provides better terrain sky transitions

Page 41: CSE 381 – Advanced Game Programming Terrain Management.

Generating Vertices

• Use spherical equations– Polar coordinates– calculate evenly spaced points on globe

• For more on Skydomes and advanced sky rendering, see the end of Chapter 9