Procedural modeling and shadow mappingCSE 167: Computer graphics •Procedural modeling –Height...
Transcript of Procedural modeling and shadow mappingCSE 167: Computer graphics •Procedural modeling –Height...
Procedural modeling and shadow mapping
Computer Graphics
CSE 167
Lecture 15
CSE 167: Computer graphics
• Procedural modeling
– Height fields
– Fractals
• L-systems
– Shape grammar
• Shadow mapping
CSE 167, Winter 2020 2Based on slides courtesy of Jurgen Schulze
3D modeling
• Definition: creating 3D objects/scenes and defining their appearance (texture etc.)
• Previous methods covered in this course– Triangle meshes– Curves– Surface patches
• Interactive modeling– Manually place vertices and/or control points
• Realistic scenes are extremely complex models containing millions or billions of primitives
CSE 167, Winter 2020 3
3D modeling
• Data driven modeling– Scan model geometry from
real world examples• Use 3D scanners or similar
devices
• Use photographs as textures
• Procedural modeling– Construct 3D models
and/or textures algorithmically
CSE 167, Winter 2020 4
Photograph Rendering[Levoy et al.]
Procedural modeling
• Wide variety of techniques for algorithmic model creation
• Used to create models too complex (or tedious) to build manually– Terrain, clouds– Plants, ecosystems– Buildings, cities
• Usually defined by a small set of data, or rules, that describes the overall properties of the model– Example: tree defined by branching
properties and leaf shapes
• Model is constructed by an algorithm– Often includes randomness to add variety– For example, a single tree pattern can be
used to model an entire forest
CSE 167, Winter 2020 5
[Deussen et al.]
Procedural modeling
• Example: No Man’s Sky– Players are free to perform within the entirety of a procedurally generated
deterministic open world universe, which includes over 18 quintillion planets. Through the game's procedural generation system, planets have their own ecosystems with unique forms of flora and fauna, and various sentient alien species may engage the player in combat or trade within planetary systems.
https://www.youtube.com/watch?v=nLtmEjqzg7M
CSE 167, Winter 2020 6
Procedural modeling
• Use some sort of randomness to make models more interesting, natural, less uniform
• Pseudorandom number generation algorithms– Produce a sequence of (apparently) random numbers based on some initial
seed value– rand() generates random number between 0 and 1
• Pseudorandom sequences are repeatable, as one can always reset the sequence– srand(seed) initializes the random number generator– If the seed value is changed, a different sequence of numbers will be
generated– Non-repeatable sequences can be generated with
srand((unsigned)time(NULL));
• Note: rand() is not recommended for serious random-number generation needs. It is recommended to use C++11's random number generation facilities to replace rand(). See documentation for <random>
CSE 167, Winter 2020 7
Procedural modeling
• Height fields
• Fractals
– L-systems
• Shape grammar
CSE 167, Winter 2020 8
Height fields
• Landscapes are often constructed as height fields
• Regular grid on the ground plane
• Store a height value at each point
• Can store large terrain in memory – No need to store all grid coordinates: inherent connectivity
• Shape terrain by operations that modify the height at each grid point
• Height can be generated from grayscale values– Allows using image processing tools to create terrain
height
CSE 167, Winter 2020 9
Midpoint displacement algorithm
• Random midpoint displacement algorithm (one-dimensional)
• Similar for triangles, quadrilaterals
CSE 167, Winter 2020 10
Result: Mountain Range
Step 1
Step 2
Step 3
Start with single horizontal line segment.Repeat for sufficiently large number of times{
Repeat over each line segment in scene{
Find midpoint of line segment.Displace midpoint in Y by random amount. Reduce range for random numbers.
} }
Step 0
Diamond square algorithm
• Begins with a 2D array of width and height 2n + 1• Four corner points must be set to initial values• Iteratively perform diamond and square steps
– Diamond step• For each square in the array, set the midpoint of that square to be the average
of the four corner points plus a random value.
– Square step• For each diamond in the array, set the midpoint of that diamond to be the
average of the four corner points plus a random value
• At each iteration, reduce the magnitude of the random value
CSE 167, Winter 2020 11
Diamond square algorithm
https://www.youtube.com/watch?v=9HJKrctqIJI
CSE 167, Winter 2020 12
Fractals
• Definition: a curve or geometric figure, each part of which has the same statistical character as the whole
• Fractals are useful in modeling naturally occurring structures (e.g., eroded coastlines, snowflakes) in which similar patterns recur at progressively smaller scales, and in describing partly random or chaotic phenomena such as crystal growth, fluid turbulence, and galaxy formation
CSE 167, Winter 2020 13
From Wikipedia
Mandelbrot set
• Z and C are complex numbers
• Initialize Z with 0 + 0i• Pick any C and iterate Z
• If C is diverging to infinity, then it is not part of the Mandelbrot set; otherwise, it is
CSE 167, Winter 2020 14
Demo: http://www.scale18.com/canvas2.html
Fractals
• The Hardest Mandelbrot Zoom in 2017 –New record, 750,000,000 iterations
https://www.youtube.com/watch?v=aSg2Db3jF_4
CSE 167, Winter 2020 15
Fractals
• A fractal with a boxlike shape found by Tom Lowe in 2010• Defined in a similar way to the Mandelbrot set• Can be defined in any number of dimensions, typically
drawn in three dimensions for illustrative purposeshttps://www.youtube.com/watch?v=0clz6WLfWaY
CSE 167, Winter 2020 16
Fractals
• Trip inside a 3D fractal (Kleinian) GPU realtimerendering
https://www.youtube.com/watch?v=XIzScwydxOE
CSE 167, Winter 2020 17
Fractal landscapes
• Add textures, material properties and use a nice rendering algorithm
• Example: Terragen Free (no cost version)https://planetside.co.uk/
CSE 167, Winter 2020 18http://planetside.co.uk/terragen-image-gallery/
L-systems
• Developed by biologist Aristid Lindenmayer in 1968 to study growth patterns of algae
• Defined by grammar
– V alphabet, set of symbols that can be replaced (variables)– S set of symbols that remain fixed (constants)– ω string of symbols defining initial state– P production rules
• Stochastic L-system– If there is more than one production rule for a symbol,
then randomly choose one
CSE 167, Winter 2020 19
Turtle interpretation for L-systems
• Origin: functional programming language Logo– Dialect of Lisp– Designed for education: drove a mechanical turtle as an output device
• Turtle interpretation of strings– State of turtle defined by (x, y, α) for position and heading– Turtle moves by step size d and angle increment δ
• Sample Grammar– F: move forward a step of length d
New turtle state: (x’, y’, α)x’ = x + d cos αy’ = y + d sin αA line segment between points (x, y) and (x’, y’) is drawn
– + Turn left by angle δ. Next state of turtle is (x, y, α + δ)Positive orientation of angles is counterclockwise
– − Turn right by angle δ. Next state of turtle is (x, y, α - δ)
CSE 167, Winter 2020 20
Example: Sierpinski triangle
• Variables: A, B– Both mean draw forward
• Constants: + , -– Turn left by 60 degrees, right by 60 degrees
• Start: A• Rules: (A→B-A-B), (B→A+B+A)
CSE 167, Winter 2020 21
2 iterations 4 iterations
6 iterations 8 iterations
Example: fern
• Variables: X, FX: no drawing operationF: move forward
• Constants: +, −, [, ]+: turn left-: turn right[ : push current position and angle onto stack] : pop stack and set current position and angle to stack values
• Start: X• Rules: (X → F-[[X]+X]+F[+FX]-X),(F → FF)
CSE 167, Winter 2020 22
[Wikipedia]
Examples
http://www.kevs3d.co.uk/dev/lsystems/
CSE 167, Winter 2020 23
Fractal trees
• “Real-time 3D Plant Structure Modeling by L-System with Actual Measurement Parameters” by Rawin Viruchpintuand Noppadon Khiripet– Model trunk and branches as cylinders– Change color from brown to green at certain level of recursion
CSE 167, Winter 2020 24Dragon Curve TreeSource: Allen Pike
Shape grammar
• Shape Rules
– Defines how an existing shape can be transformed
• Generation Engine
– Performs the transformations
• Working Area
– Displays created geometry
CSE 167, Winter 2020 25
Example: Coca-Cola bottle
26
Evolution of Coca-Cola bottles
Division of a Coca-Cola bottleCSE 167, Winter 2020
Example: Coca-Cola bottle
• Shape computation for two existing Coca-Cola bottles
CSE 167, Winter 2020 27
Source: Chau et al.: “Evaluation of a 3D Shape Grammar Implementation”, Design Computing and Cognition'04, pp. 357-376
Real-Time Procedural Modeling
• Chaos Theory by DMA
– Best 4k Intro at Assembly 2011 in Finland
https://www.youtube.com/watch?v=2aQtgPv84wE
• Uncovering Static by Fairlight & Alcatraz
– Best 64k Intro at Assembly 2011 in Finland
https://www.youtube.com/watch?v=6sJfMmBtG0k
• More demos at http://awards.scene.org
CSE 167, Winter 2020 28
Shadow mapping
Shadows
• Give additional cues on scene lighting
30CSE 167, Winter 2020
Shadows
• Contact points
• Depth cues
CSE 167, Winter 2020 31
Shadows
• Realism
CSE 167, Winter 2020 32
Without self-shadowing With self-shadowing
Terminology
Umbra: fully shadowed region
Penumbra: partially shadowed region
CSE 167, Winter 2020 33
(area) light source
receiver shadow
occluder
umbra
penumbra
Hard and soft shadows
• Point and directional lights lead to hard shadows, no penumbra
• Area light sources lead to soft shadows, with penumbra
CSE 167, Winter 2020 34
point directional area
umbra penumbra
Hard and soft shadows
CSE 167, Winter 2020 35
Hard shadow from
point light source
Soft shadow from
area light source
Shadows for interactive rendering
• Hard shadows only in this course
– Soft shadows are difficult to compute in interactive graphics
• Two most popular techniques
– Shadow mapping
– Shadow volumes
• Many variations, subtleties
• Active research area
CSE 167, Winter 2020 36
Shadow mapping
• A scene point is lit by the light source if visible from the light source
• Determine visibility from light source by placing a camera at the light source position and rendering the scene from there
CSE 167, Winter 2020 37
Scene points are lit if
visible from light source
Determine visibility from
light source by placing camera
at light source position
• First Pass
– Render scene by placing camera at light source position
– Store depth image (shadow map)
Depth image as seen
from light source
depth value
CSE 167, Winter 2020 38
Two pass algorithm
• Second Pass
– Render scene from camera position
– At each pixel, compare distance to light source with value in shadow map
• If distance is larger, then pixel is in shadow
• If distance is smaller or equal, then pixel is lit
CSE 167, Winter 2020 39
Final image with shadows
vb is in
shadow pixel seen
from eye vb
Two pass algorithm
Shadow map look-up
• Need to transform each point from object space to shadow map
• Shadow map texture coordinates are in [0,1]2
• Transformation from object to shadow map coordinates
• T is called texture matrix
• After perspective projection we have shadow map coordinates
CSE 167, Winter 2020 40
(0,0)
(1,1)
Light space
Object space
Shadow map
Shadow map look-up
• Transform each vertex to normalized frustum of light
• Pass s,t,r,q as texture coordinates to rasterizer
• Rasterizer interpolates s,t,r,q to each pixel
• Use projective texturing to look up shadow map– This means, the texturing unit automatically computes (s/q, t/q, r/q, 1)
– (s/q, t/q) are shadow map coordinates in [0,1]2
– r/q is depth in light space
• Shadow depth test: compare shadow map at (s/q, t/q) to r/q
CSE 167, Winter 2020 41
GLSL specifics
• In application– Store matrix T in OpenGL texture matrix
– Set using glMatrixMode(GL_TEXTURE)
• In vertex shader– Access texture matrix through predefined uniform gl_TextureMatrix
• In fragment shader– Declare shadow map as sampler2DShadow
– Look up shadow map using projective texturing withvec4 texture2DProj(sampler2D, vec4)
CSE 167, Winter 2020 42
Implementation specifics
• When doing a projective texture look up on a sampler2DShadow, the depth test is performed automatically
– Return value is (1,1,1,1) if lit
– Return value is (0,0,0,1) if shadowed
• Simply multiply result of shading with current light source with this value
CSE 167, Winter 2020 43
Shadow mapping
• Demo
http://www.paulsprojects.net/opengl/shadowmap/shadowmap.html
CSE 167, Winter 2020 44
Shadow mapping
• Tutorial
http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/
CSE 167, Winter 2020 45
Shadow maps
• Issues
– Sampling problems
– Limited field of view (of shadow map)
– Z-fighting
CSE 167, Winter 2020 46
Sampling problems
• Shadow map pixel may project to many image pixels– Stair-stepping artifacts
CSE 167, Winter 2020 47
Sampling problems
• Solutions– Increase resolution of shadow map
• Not always sufficient
– Split shadow map into several tiles
– Modify projection for shadow map rendering
• Light space perspective shadow maps (LiSPSM) http://www.cg.tuwien.ac.at/research/vr/lispsm/
– Combination of splitting and LiSPSM
• Basis for most commercial implementationsCSE 167, Winter 2020 48
• What if a scene point is outside the field of view of the shadow map? field of
view
Limited field of view
CSE 167, Winter 2020 49
• What if a scene point is outside the field of view of the shadow map?
– Use six shadow maps, arranged in a cube
• Requires a rendering pass for each shadow map
shadow
maps
CSE 167, Winter 2020 50
Limited field of view
Z-fighting
• Depth values for points visible from light source are equal in both rendering passes
• Because of limited resolution, depth of pixel visible from light could be larger than shadow map value
• Need to add bias in first pass to make sure pixels are lit
Camera image
Shadow map
Image
pixels
Shadow map
pixels Pixel is
considered
in shadow!
Depth
of pixel
Depth of
shadow map
CSE 167, Winter 2020 51
Z-fighting
• Add bias when rendering shadow map
– Move geometry away from light by small amount
• Finding correct amount of bias is tricky
CSE 167, Winter 2020 52
Correct bias Not enough bias Too much bias
Z-fighting
CSE 167, Winter 2020 53
Just right
Not enough bias Too much bias
Shadow mapping
• Directional light sources– Shadow mapping can be done with orthographic projection in step
one when creating the shadow map
• More informationhttp://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/http://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix/orthographic-projection-matrix
CSE 167, Winter 2020 54
Shadow mapping
• Resources– Overview (lots of links)
http://www.realtimerendering.com/
– Basic shadow mapshttp://en.wikipedia.org/wiki/Shadow_mapping
– Avoiding sampling problems in shadow mapshttp://www.comp.nus.edu.sg/~tants/tsm/tsm.pdf
http://www.cg.tuwien.ac.at/research/vr/lispsm/
– Faking soft shadows with shadow mapshttp://people.csail.mit.edu/ericchan/papers/smoothie/
CSE 167, Winter 2020 55