# Game Programming 09 OGRE3D Lighting/shadow in Action

## Presentation on theme: "Game Programming 09 OGRE3D Lighting/shadow in Action"— Presentation transcript:

Game Programming 09 OGRE3D Lighting/shadow in Action
2010년 2학기 디지털콘텐츠전공

Rendering in Video Games
Depth-Buffered Triangle Rasterization Virtual Scene Virtual Camera Various Light Sources Visual Properties Solving the Rendering Equation (Shading Equation)

Rendering in OGRE3D Depth-Buffered Triangle Rasterization
Virtual Scene  createScene() Virtual Camera  createCamera()/ createViewport() Various Light Sources  createScene() Visual Properties  material Solving the Rendering Equation (OGRE3D engine)

What does make a scene look real?

OGRE3D Lighting Three types of lighting in OGRE3D
Point (Ogre::Light::LT_POINT) Point light sources emit light from them in every direction. Spotlight (Ogre::Light::LT_SPOTLIGHT) A spotlight works exactly like a flashlight does. Directional (Ogre::Light::LT_DIRECTIONAL) Directional light simulates far-away light that hits everything in the scene from a direction.

Creating a Light Creating a Light Setting a type Setting a position
SceneManager::createLight(“name”) Setting a type Ogre::Light::setType( … ) LT_POINT, LT_DIRECTIONAL, LT_SPOTLIGHT Setting a position Ogre::Light::setPosition( Ogre::Vector3(x,y,z) ) Ogre::Light* pointLight = mSceneMgr->createLight("pointLight"); pointLight->setType(Ogre::Light::LT_POINT); pointLight->setPosition(Ogre::Vector3(0, 150, 250));

Setting a Light Setting the color Setting the direction
Ogre::Light::setDiffuseColour (Ogre::ColourValue(r,g,b)) Ogre::Light::setSpecularColour (Ogre::ColourValue(r,g,b)) Setting the direction Ogre::Light::setDirection( Ogre::Vector3(x,y,z)) Setting the spot light property Ogre::Light::setSpotlightRange (inner_angle, outer_angle) angle: Ogre::Degree(angle) Ogre::Light* directionalLight = mSceneMgr->createLight("directionalLight"); directionalLight->setType(Ogre::Light::LT_DIRECTIONAL); directionalLight->setDiffuseColour(Ogre::ColourValue(.25, .25, 0)); directionalLight->setSpecularColour(Ogre::ColourValue(.25, .25, 0)); directionalLight->setDirection(Ogre::Vector3( 0, -1, 1 ));

one of the most challenging aspects of 3D rendering still very much an active area of research. There are many techniques to render shadows. None is perfect and they all come with advantages and disadvantages

How to draw a shadow? Off-line algorithm Real-time algorithm

The simplest technique to create shadows from an object. Two-pass algorithm Draw the object Draw the shadow Problem: Only on a planar surface

Shadow Mapping Testing whether a pixel is visible from the light source by comparing it to a depth image of the light source's view Depth image is stored as a texture  Texture-based shadow Visualization of the depth map projected onto the scene Scene rendered from the light view Scene from the light view, depth map. Depth map test failures Scene with shadow mapping

Aliasing How to solve the problem: Increasing the resolution of the texture Projective shadow map single shadow map pixel

Stencil buffer Stencil buffer is like a mask.

Typical use of a stencil buffer
Drawing a reflection effect Drawing a planar shadow 참고자료:

Although we can create volumes for every triangle in the occluders, we only need the silhouette. Different types of volume for different types of lights

Shadow Volume concept All the objects positioned within a shadow volume are hidden from the light source and are thus in either full or partial shadow. A silhouette edge can more generally be considered as an outline or edge separating a front- and back-facing surface.

Stencil Shadow Volume Render the Scene without lights and keep the z-buffer. Fragments with non-zero stencil values are considered to be in shadow. The generation of the values in the stencil buffer : Render front face of shadow volume. If depth test passes, increment stencil value Render back face of shadow volume. If depth test passes, decrement stencil value Render the lit area where the stencil buffer value is 0

Stencil Shadow Pros Very accurate and robust Nearly artifact-free
Faceting near the silhouette edges is the only problem Work for point lights and directional lights equally well Low memory usage

Stencil Shadow Cons Too accurate — hard edges Very fill-intensive
Need a way to soften Very fill-intensive Scissor and depth bounds test help Significant CPU work required Silhouette determination Building shadow volumes Hard shadow Soft shadow

Avoid any long shadows (e.g. a very low sun position) Culling distant objects beforehand Shadow volume accuracy Don’t put any object too close to a light source Mesh edge list Edge list is necessary for constructing the shadow volume Official exporter and tools will generate it automatically If you use your own mesh, you can generate it by calling Mesh::buildEdgeList Visible silhouette edge Silhouette edge can be very marked. Especially, the mudulative method is more vulnerable with 2 or more light sources Additive lights do not suffer from this as badly because each light is masked individually

Adding each light one by one For this reason, rendering should be divided into several passes: Ambient pass Diffuse/specular pass Decal pass