Shadows in Computer Graphics. by Björn Kühl im/ve University of Hamburg, Germany

Similar documents
Shadow Mapping. Render Scene and Access the Depth Texture. What is Projective Texturing? About Projective Texturing (1) About Projective Texturing (3)

Computergrafik. Matthias Zwicker. Herbst 2010

Computer Graphics Shadow Algorithms

Last Time. Why are Shadows Important? Today. Graphics Pipeline. Clipping. Rasterization. Why are Shadows Important?

Shadow Algorithms. CSE 781 Winter Han-Wei Shen

Last Time. Reading for Today: Graphics Pipeline. Clipping. Rasterization

For Intuition about Scene Lighting. Today. Limitations of Planar Shadows. Cast Shadows on Planar Surfaces. Shadow/View Duality.

Real-Time Shadows. Last Time? Today. Why are Shadows Important? Shadows as a Depth Cue. For Intuition about Scene Lighting

What for? Shadows tell us about the relative locations. Vienna University of Technology 2

Lecture 17: Shadows. Projects. Why Shadows? Shadows. Using the Shadow Map. Shadow Maps. Proposals due today. I will mail out comments

CMSC427 Advanced shading getting global illumination by local methods. Credit: slides Prof. Zwicker

Real-Time Shadows. Computer Graphics. MIT EECS Durand 1

Shadows. Real-Time Hard Shadows. Collected by Ronen Gvili. Most slides were taken from : Fredu Durand Stefan Brabec

Real-Time Shadows. Last Time? Textures can Alias. Schedule. Questions? Quiz 1: Tuesday October 26 th, in class (1 week from today!

Robust Stencil Shadow Volumes. CEDEC 2001 Tokyo, Japan

Real-Time Shadows. Last Time? Schedule. Questions? Today. Why are Shadows Important?

Real-Time Shadows. MIT EECS 6.837, Durand and Cutler

Shadow Rendering EDA101 Advanced Shading and Rendering

Acknowledgement: Images and many slides from presentations by Mark J. Kilgard and other Nvidia folks, from slides on developer.nvidia.

Overview. A real-time shadow approach for an Augmented Reality application using shadow volumes. Augmented Reality.

Shadow Rendering. CS7GV3 Real-time Rendering

Optimized Stencil Shadow Volumes. Cass Everitt & Mark J. Kilgard

Volume Shadows Tutorial Nuclear / the Lab

VGP353 Week 2. Agenda: Assignment #1 due Introduce shadow maps. Differences / similarities with shadow textures Added benefits Potential problems

Real-time Shadow Mapping

Rasterization Overview

Shadow Volume History (1) Shadow Volumes. Shadow Volume History (2) Shadow Volume History (3) Shadow Volume Basics. Shadow Volume History (4)

Advanced Shading and Texturing

Shadows. COMP 575/770 Spring 2013

Shadows in the graphics pipeline

Shadow Techniques. Sim Dietrich NVIDIA Corporation

CS 5610 / Spring Depth Buffering and Hidden Surface Removal

CSE 167: Introduction to Computer Graphics Lecture #18: More Effects. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2016

Rendering. Converting a 3D scene to a 2D image. Camera. Light. Rendering. View Plane

In- Class Exercises for Shadow Algorithms

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

Shadows. Prof. George Wolberg Dept. of Computer Science City College of New York

Shadows. Shadows. Spatial relationship between objects. Shadows as depth cue. Spatial relationship between objects

An Interactive Introduction to OpenGL Programming

Lecture 19: OpenGL Texture Mapping. CITS3003 Graphics & Animation

Texture Mapping II. Light maps Environment Maps Projective Textures Bump Maps Displacement Maps Solid Textures Mipmaps Shadows 1. 7.

Real Time Reflections Han-Wei Shen

Texture Mapping. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

A Shadow Volume Algorithm for Opaque and Transparent Non-Manifold Casters

Screen Space Ambient Occlusion TSBK03: Advanced Game Programming

Advanced Shading I: Shadow Rasterization Techniques

Vincent Forest, Loïc Barthe, Mathias Paulin. IRIT-UPS-CNRS University of Toulouse, France

Computer Graphics. Shadows

CSE 167: Introduction to Computer Graphics Lecture #9: Visibility. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2018

Real-time Shadows in CG

Orthogonal Projection Matrices. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

CSE 167: Lecture #5: Rasterization. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

Shadows. Shadows. Thanks to: Frédo Durand and Seth Teller MIT. Realism Depth cue

Lecture 5 3D graphics part 3

Practical Shadow Mapping

Multi-View Soft Shadows. Louis Bavoil

Direct Rendering of Trimmed NURBS Surfaces

TDA362/DIT223 Computer Graphics EXAM (Same exam for both CTH- and GU students)

CEng 477 Introduction to Computer Graphics Fall 2007

TSBK03 Screen-Space Ambient Occlusion

Pipeline Operations. CS 4620 Lecture 10

Basic GPU techniques Josef Pelikán CGG MFF UK Praha.

Pipeline Operations. CS 4620 Lecture Steve Marschner. Cornell CS4620 Spring 2018 Lecture 11

Culling. Computer Graphics CSE 167 Lecture 12

Computer Graphics (CS 543) Lecture 10: Soft Shadows (Maps and Volumes), Normal and Bump Mapping

The Traditional Graphics Pipeline

Getting fancy with texture mapping (Part 2) CS559 Spring Apr 2017

Rendering Objects. Need to transform all geometry then

CSE 167: Introduction to Computer Graphics Lecture #11: Visibility Culling

Projection Matrix Tricks. Eric Lengyel

Programming Graphics Hardware

CS 432 Interactive Computer Graphics

Illumination and Geometry Techniques. Karljohan Lundin Palmerius

DOOM 3 : The guts of a rendering engine

Pipeline Operations. CS 4620 Lecture 14

The Graphics Pipeline

Hidden surface removal. Computer Graphics

Deferred Rendering Due: Wednesday November 15 at 10pm

Shadows. Shadows. Spatial relationship between objects. Shadows as depth cue. Spatial relationship between objects

Announcements. Written Assignment 2 is out see the web page. Computer Graphics

Visible-Surface Detection Methods. Chapter? Intro. to Computer Graphics Spring 2008, Y. G. Shin

Spotlight Shadow Mapping in OpenGL

Overview. By end of the week:

Practical Shadows. Outline

CHAPTER 3. Visualization of Vector Data. 3.1 Motivation

Computer Graphics with OpenGL ES (J. Han) Chapter VII Rasterizer

CS4620/5620: Lecture 14 Pipeline

Real-Time Non- Photorealistic Rendering

INF3320 Computer Graphics and Discrete Geometry

Pixels and Buffers. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Buffers and Processing Fragments

Today. Texture mapping in OpenGL. Texture mapping. Basic shaders for texturing. Today. Computergrafik

High-quality Shadows with Improved Paraboloid Mapping

Procedural modeling and shadow mapping. Computer Graphics CSE 167 Lecture 15

Werner Purgathofer

EECS 487: Interactive Computer Graphics

Programmable GPUs. Real Time Graphics 11/13/2013. Nalu 2004 (NVIDIA Corporation) GeForce 6. Virtua Fighter 1995 (SEGA Corporation) NV1

CSE 167: Introduction to Computer Graphics Lecture #4: Vertex Transformation

3D Rasterization II COS 426

Spring 2009 Prof. Hyesoon Kim

Transcription:

Shadows in Computer Graphics by Björn Kühl im/ve University of Hamburg, Germany

Importance of Shadows Shadows provide cues to the position of objects casting and receiving shadows to the position of the light sources

Importance of Shadows Shadows provide information about the shape of casting objects about the shape of shadow receiving objects

Shadow Tutorial Popular Shadow Techniques Implementation Details (object based) Shadow Mapping (image based) Comparison of both techniques

Popular Shadow Methods Shadows can be found in most new games Two methods for shadow generation are predominately used: Shadow Mapping Need for Speed Prince of Persia Doom3 F.E.A.R Prey

Figure: Screenshot from id's Doom3

introduction Frank Crow introduced his approach using shadow volumes in 1977. Tim Heidmann of Silicon Graphics was the first one who implemented Crow s idea using the stencil buffer.

Calculating Shadow Volumes With The CPU Calculate the silhouette: An edge between two planes is a member of the silhouette, if one plane is facing the light and the other is turned away. Figure: silhouette edge (left), a light source and an occluder (top right), and the silhouette of the occluder ( down right)

Calculating Shadow Volumes With The CPU The shadow volume should be closed By extruding the silhouette, the side surfaces of the shadow volume are generated The light cap is formed by the planes facing the light The dark cap is formed by the planes not facing the light which are transferred into infinity.

Calculating Shadow Volumes With The GPU Vertex shaders are specialized for transforming vertices... however, they can not create new vertices That is why the silhouette can not be extruded Idea: One-time enhancement of the object by additional vertices displacement of vertices which are turned away from the light source

Calculating Shadow Volumes With The GPU Replace all edges by degenerate rectangles. They are degenerate because they have no surface area The new rectangles consist of new vertices each with a position a normal, the same as the vertex of the adjacent plane The shadow volume is build by testing each vertex if it is facing the light. If it isn't facing the light, then it has to be extruded into infinity in the direction of the vector from the light to the vertex.

Calculating Shadow Volumes With The GPU All edges are replaced by degenerated rectangles By moving all vertices which don't face the light, the degenerated rectangles become real rectangles forming the side surfaces of the shadow volume.

Using the shadow volumes to calculate the real shadows

The Stencil Buffer Used like a real stencil to mask a scene. Examples: Cockpit ( mask only the windows to draw the landscape) Reflections ( mask the area where to draw the same scene mirrored) Shadows...

The Stencil Buffer Can draw geometry in it. Doing this will update the values in the stencil buffer. Can look up the value in the stencil buffer. Can use a comparison function ( among others <, >, =,!=) to then update the value in the stencil buffer draw the fragment discard the fragment

The Stencil buffer Figure: Screenshot from SimBins GTR2. The mirrors and windows are masked areas.

Depth-Pass vs. Depth-Fail To decide if a fragment lies in a shadow, two methods exist: 1. Depth-pass = z-pass 2. Depth-fail = Carmack s Reverse = z-fail Both methods calculate a value for each fragment in the stencil buffer

Depth-Pass 1. Disable writing to depth and color buffers 2. Render front face of shadow volume. If the depth test passes, increment the stencil value. 3. Render back face of shadow volume. If the depth test passes, decrement the stencil value.

Depth-Pass If the camera is located inside of a shadow volume, the entry in this volume is missed and the stencil value isn't incremented. This causes incorrect shadows.

Depth-Pass - OpenGL Stencil Configuration // disable writing to the color- and depth buffer: glcolormask(gl_false, GL_FALSE, GL_FALSE, GL_FALSE); gldepthmask(gl_false); // Configuration of the stencil function and the stencil operator: //1) first stencil test increment if drawing of the front faces is successful glenable(gl_stencil_test); glstencilfunc( GL_ALWAYS, 0, 0xffffffff); glstencilop( GL_KEEP, GL_KEEP, GL_INCR); glenable(gl_cull_face); glfrontface(gl_ccw); drawshadowvolume(); // at first only front faces // 3) second test decrement if drawing of the back faces is successful glstencilop(gl_keep, GL_KEEP, GL_DECR); glfrontface(gl_cw); drawshadowvolume(); // now the back faces

Depth-Fail / Carmack's Reverse 1. Disable writing to depth and color buffers 2. Render back face of shadow volume. If the depth test fails, increment the stencil value. 3. Render front face of shadow volume. If the depth test fails, decrement the stencil value.

Depth-Fail - If the camera is located inside of a shadow volume, the result is still correct! - The more robust method! (capping is needed!)

Near Clipping Plane Issues Both methods have problems if the shadow volume intersects a clipping plane. Depth-pass doesn't function, if the volume is intersected by the near plane.

Far Clipping Plane Issues Depth-fail doesn't function, if the volume is intersected by the far plane. Moving the far plane to infinity is the solution to the problem.

Depth-Fail Three approaches for moving the far clipping plane to infinity: 1. Direct manipulation of the projection matrix 2. Defining the kind of projection using a negative value for the distance to the far clipping plane 3. Using the extension NV_depth_clamp by nvidia

Depth-Pass vs. Depth-Fail Advantages: front & back caps are unnecessary less geometry speedier than depth-fail easier to implement Disadvantages: not applicable if the camera is located inside of a shadow volume near plane clipping issues not really robust Advantages: also applicable if the camera is located inside of a shadow volume More robust because the far plane clipping issue can be eliminated: by translating the nearclipping-plane to infinity the depth-clamping-extension (nvidia) assigns all geometry behind the far clipping plane with the maximal z-value Disadvantages: front & back caps necessary more geometry to render slower harder to implement

Shadow Mapping Figure: Screenshot of Gothic III

Shadow Mapping Concept Concept by Lance Williams 1978 1. Draw scene from light's viewpoint Store the depth values in a shadow texture 2. Draw scene from camera's viewpoint Determine each fragment's position relative to the light Compare the transformed depth value to the value in the depth map at position XY. If the value is greater than the value of the depth map then the fragment is covered from the light s perspective and therefore must be shadowed.

Shadow Mapping Render-to-Texture The depth values from light's viewpoint interpreted as b/w colors and the same scene from camera's viewpoint with shadows

Shadow Mapping Render-to-Texture: OpenGL Texture Configuration: // Create and bind a 2D texture: glgentextures(1, &shadowmaptexture); glbindtexture(gl_texture_2d, shadowmaptexture); // configure the texture: ( dimension, and type of content) glteximage2d( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, 500, 500, 0, GL_DEPTH_COMPONENT32, GL_FLOAT, NULL); // copy values from the depth buffer ( GL_DEPTH_COMPONENT) to the texture: glcopyteximage2d(gl_texture_2d, 0, GL_DEPTH_COMPONENT, 0, 0, framesizewidth, framesizeheight, 0); // alternatively copy rgb-values from the color buffer ( GL_RGBA) to the texture: glcopyteximage2d(gl_texture_2d, 0, GL_RGBA, 0, 0, framesizewidth, framesizeheight, 0);

Shadow Mapping Depth Comparison Draw scene from camera s viewpoint Determine each fragments position relative to the light (d') Compare the transformed depth value to the value in the depth map at position XY (d).

Shadow Mapping Configuration of the depth comparison in OpenGL using the CPU // Bind the shadow map as the current texture and activate texturing: glbindtexture(gl_texture_2d, shadowmaptexture); glenable(gl_texture_2d); //Enable shadow comparison gltexparameteri(gl_texture_2d,gl_texture_compare_mode_ A RB, GL_COMPARE_R_TO_TEXTURE); //Shadow comparison should be true if r<=texture gltexparameteri(gl_texture_2d, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL); //Shadow comparison should generate an INTENSITY result gltexparameteri(gl_texture_2d, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);

Shadow Mapping Configuration of the depth comparison in OpenGL using the GPU Performing the depth comparison in the fragment shader: uniform sampler2dshadow shadowmap; uniform float threshold; varying vec4 projcoord; void main () { // performing the depth comparison: float compresult = shadow2dproj(shadowmap, projcoord).z; // if both values are equal: compresult = 1.0 // otherwise compresult = 0.0; } // we can use the compresult to darken the color of the fragment gl_fragcolor = gl_color * compresult;

Shadow Mapping Switching Between The Coordinate Systems Determine the position of the fragment relative to the light source means transforming the eye-coordinates into clip-coordinates of the light source ( switch ).

Shadow Mapping Generation Of Texture-Coordinates relative to the display borders relative to the object borders

Shadow Mapping Generation of texture coordinates in OpenGL //Calculate texture matrix for projection //This matrix takes us from eye space to the light's clip space //It is postmultiplied by the inverse of the current view matrix when specifying texgen static MATRIX4X4 biasmatrix(0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f); //bias from [-1, 1] to [0, 1] MATRIX4X4 texturematrix = biasmatrix * lightprojectionmatrix * lightviewmatrix; //Set up texture coordinate generation. gltexgeni(gl_s, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); gltexgenfv(gl_s, GL_EYE_PLANE, texturematrix.getrow(0)); glenable(gl_texture_gen_s); gltexgeni(gl_t, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); gltexgenfv(gl_t, GL_EYE_PLANE, texturematrix.getrow(1)); glenable(gl_texture_gen_t); gltexgeni(gl_r, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); gltexgenfv(gl_r, GL_EYE_PLANE, texturematrix.getrow(2)); glenable(gl_texture_gen_r); gltexgeni(gl_q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); gltexgenfv(gl_q, GL_EYE_PLANE, texturematrix.getrow(3)); glenable(gl_texture_gen_q);

Shadow Mapping Polygon Offset With infinite resolution and unlimited precision pixels transformed into light's coordinate system & passing the visible test have the same depth value as the one in the depth texture. But in real life, resolution and precision are limited --> producing artefacts. We can avoid this by using a polygon offset before drawing to the shadow map. An appropriate polygon offset must be found for every individual scene.

Shadow Mapping Polygon Offset Figure: The same scene using three different polygon offsets. Polygon offset is too low (left), just right (middle), and too high( right).

Shadow Mapping Polygon Offset Polygon Offset is controlled through two parameters: factor and units. Factor scales the maximum depth slope of the polygon Units scales the smallest value guaranteed a resolvable difference in window coordinate depth values Polygon offset = m * factor + r * units This offset is added to every given z-value of each vertex.

Shadow Mapping percentage closest filtering without pcf with pcf

Shadow Mapping Back Projections Expected behavior of a projector Troublesome back projections

Comparison Of Techniques Stencil Shadow Volume + automatic self-shadows + omni-directional lights + no aliasing effects - dependent on scene's complexity, because additional geometry must be rendered. - soft shadows are not possible - consumes a lot of fillrate - clipping plane issues Shadow Mapping + automatic self-shadows + independent of scene complexity + no additional geometry to be rendered + soft shadows possible - aliasing effects caused by sampling errors - only directional lights - incorrect self-shadowing - incorrect back-projections

Literature Shadow Mapping Morgan McGuire GPU-Gems - Addison-Wesley Longman Ashu Rege: Shadow Considerations (paper) - developer.nvidia.com http://www.gamedev.net/columns/hardcore/shadowvolume/page1.asp http://msdn.microsoft.com/library/default.asp?url=/library/enus/directx9_c/shadowvolume_sample.asp Mark J. Kilgard - Shadow Mapping with Today s OpenGL Hardware ( Paper) developer.nvidia.com http://www.paulsprojects.net/tutorials/smt/smt.html OpenGL Shreiner,Woo,Neider - OpenGL Programming Guide The Official Guide to Learning OpenGL, Version 2, Addison-Wesley Longman Rost, Kessenich, Lichtenbelt OpenGL Shading Language, AddisonWesley Longman