1 / 66

OpenGL Shadow

OpenGL Shadow. Content . Shadowing using Stencil Buffer Shadowing using Projective Texture Shadow map Shadow volume. Shadows are important …. It is usually better to have an inaccurate shadow than none at all. (area) light source. occluder. penumbra. umbra. receiver. shadow.

glenys
Télécharger la présentation

OpenGL Shadow

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OpenGL Shadow

  2. Content • Shadowing using Stencil Buffer • Shadowing using Projective Texture • Shadow map • Shadow volume

  3. Shadows are important … It is usually better to have an inaccurate shadow than none at all

  4. (area) light source occluder penumbra umbra receiver shadow Terminology Occludee (receiver) Hard Shadow Soft Shadow

  5. Soft Shadows (Ref) • Point light generates hard shadows only • Soft shadows are NOT generated by low pass filters • Soft shadows are generally preferable, if possible.

  6. Planar Projection Shadow (xz plane) l y v: any pt on model v x shadow p

  7. l v p n p:n•x+d=0 Planar Projection (general)

  8. Planar Projection Shadow • Problems [& Solutions] • Z-fighting [polygon offset, stencil] • Shadow fall outside of the receiver [stencil] • Semi-transparent shadows [blending + stencil, ensuring each pixel is drawn at most once] • Planar receiver only • Need to render each frame (But for static objects, the shadows are view-independent) [render the shadow into texture]

  9. Shadow polygons are blended with the floor, one-by-one Repetitive blending … To prevent this from happening, use stencil buffer When a polygon is drawn, increment the stencil count (therefore, the area is not drawn again) Remark glStencilFunc(GL_EQUAL, 1, 0xFF); glStencilOp (GL_KEEP, GL_KEEP, GL_INCR);

  10. Stencil Setting

  11. Projective Texture Shadow

  12. Overview In the display callback • Render occluders to projective shadow texture (with FBO) • Set up texture unit 1 for projective (shadow) texture; render receiver • Render occluder to complete the scene

  13. Steps • Terrain: triangle strip • FBO for render to shadow texture • Render projective (shadow) texture

  14. 0 [i,j] [i-1,j] 1 Set normal at 2 [i-1,j+1] x [i,j] 3 4 2 3 4 0 1 [i,j+1] [i-1,j+1] z Terrain (trianglestrip) [0,0] [0,1] [1,0] [1,1] [2,0] [2,1] …[4,0] [4,1]

  15. Render to Shadow Texture • Render occluder (unlit) as black objects • Clear color set to (1,1,1)

  16. Projective Shadow Texture • Set texture environment to modulate • Shadowed part: dark (modulated by GREY) • Unshadowed part: unchanged (modulated by WHITE) • Use the same projective parameters (fovy, …) as in render to texture

  17. Tricks If you see something like this, the artifact comes from projective texture clamping: the shadow touches the texture border. It can be alleviated by increasing the fov or raising the light position This is normal!

  18. Shadow Map Primer Image-based technique by Lance Williams (1978) Amenable to h/w implementation

  19. World Space Coordinate Transformation Note the convention on X and X-1

  20. Texgen (eye-linear) Ee: transformed plane equations We do not use texture matrix T for now Plight: projection matrix for light frustum S: scale-bias matrix from [-1,1] to [0,1] [s,t,r,q]T is actually [x,y,z,w]T (scale-biased clip coordinates)

  21. EyeLight Depth Map Comparison • Render from eye’s point of view • “Convert” the eye coordinates to light space via texgen facility ObjectLight

  22. EyeLight Summary

  23. Zls > Zsm: in shadow After converting to light space • Compare with depth texture: • Use comparison result (0 or 1) in fragment shading (can be bilinearly filtered): use the result directly to modulate the diffuse and specular intensity

  24. GL_ARB_shadow (ref) This comparison produces a boolean texture value • Require depth texture extension • Enable shadow comparison • glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE); • Not in shadow if r<=texture • glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL); • Shadow comparison should generate an INTENSITY result (or ALPHA result) • glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY) • Set alpha test to discard false comparisons • glAlphaFunc(GL_GEQUAL, 0.99f); • glEnable(GL_ALPHA_TEST);

  25. From GL_ARB_SHADOW

  26. Generate shadow map render the scene from light position Lighting, texturing, color buffer writes OFF Copy depth buffer content to a depth texture Note: shadow map is view independent (only need to be updated if scene or light moved) Ambient pass Render the scene from camera with only ambient lighting Diffuse Pass Set up necessary manipulations for coordinate transformation (eye coordinate to light coordinate) Set up shadow map parameters (compare_mode, compare_func, deptht texture_mode) Render the scene with lights on; the shadow map will generate a boolean texture result based on shadow test. The test will set the alpha values of the fragment. [in shadow a = 0, otherwise, a = 1] The alpha test will let the lit (unshadowed) fragment pass, covering the original ambient fragments. Final color is ambient pass + full colora [multitexture] Set shadowmap at the fourth texture unit Use the other three for original textures Implementation

  27. Fov for light need to be the same as te fov of camera? No. The shadow comparison is solely based on depth texture and the converted clip coordinates in light coordinate system. The projection matrices (where fov plays a part) are of no importance. Aspect ratio of shadow map need to be the same as the aspect ratio of the window? No. (similar reason as in the left.) The projection matrices (where aspect ratio plays a part) are of no importance. FAQ

  28. The artifact on the base is because the shadow map coverage contains only part of the base. It can be remedied by increasing the fov of the light frustum (but sacrificing the depth texture resolution) Trouble Shooting fov=90 fov=45

  29. Using Shaders… • Texgen is not performed • The inverse of viewing matrix need to be computed directly (see projective texture) • Only two passes are required • First pass: generate shadow map • Second pass: render the shadowed and lit part (one pass less than fixed functionality) • Remove unwanted artifact (more precise control by rendering shadowed part only within light frustum)

  30. Bias(1)

  31. Bias(2)

  32. Bias(3)

  33. Alias(1)

  34. Alias(2)

  35. Issues • Bias factor • Depth in shadow map often slightly lower than that of the viewer • Aliasing due to point-sampling • Percentage closer filtering: instead of single point sample, take four closest. • Bilinearly interpolate the results (in/out shadow) to calculate the lighting • Provide pseudo-soft shadows

  36. Shadow Map • Advantage: • Simple • Shadow map is view independent • Disadvantage: • Shadow distance from light position may appear blocky • Storage • Light source in the view volume?

  37. Need stencil buffer! Shadow Volume Geometry-based technique first proposed by Frank Crow in 1977

  38. Point light [illustration here based on point light] Silhouette: the border of front/back facing polygons w.r.t. light source

  39. Z-pass Algorithm

  40. Pitfalls of Z-Pass Figure 6: When eye point is within the shadow volume, depth-pass stencil operation fails

  41. Carmack’s reverse (Z-fail) 1

More Related