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

Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Procedural modeling and shadow mapping

Computer GraphicsCSE 167

Lecture 15

Page 2: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

CSE 167: Computer graphics

• Procedural modeling– Height fields– Fractals

• L‐systems

– Shape grammar

• Shadow mapping

CSE 167, Winter 2018 2Based on slides courtesy of Jurgen Schulze

Page 3: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

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

CSE 167, Winter 2018 3

Page 4: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

3D modeling

• Realistic scenes are extremely complex models containing millions or billions of primitives

• 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 2018 4

Photograph Rendering[Levoy et al.]

Page 5: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

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 2018 5

[Deussen et al.]

Page 6: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

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) );

CSE 167, Winter 2018 6

Page 7: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Procedural modeling

• Height fields• Fractals

– L‐systems

• Shape grammar

CSE 167, Winter 2018 7

Page 8: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

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 2018 8

Page 9: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Midpoint displacement algorithm

• Random midpoint displacement algorithm (one‐dimensional)

• Similar for triangles, quadrilaterals

CSE 167, Winter 2018 9Source: http://gameprogrammer.com/fractal.html#midpoint

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

Page 10: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Diamond square algorithm• Begins with a 2D array of size 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 2018 10

Page 11: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

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 2018 11

From Wikipedia

Page 12: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

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 2018 12Demo: http://www.scale18.com/canvas2.html

Page 13: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Fractals

• 3D Mandelbrot Zoomhttps://www.youtube.com/watch?v=0clz6WLfWaY

CSE 167, Winter 2018 13

Page 14: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Fractals

• Trip inside a 3D fractal (Kleinian) GPU realtimerendering https://www.youtube.com/watch?v=XIzScwydxOE

CSE 167, Winter 2018 14

Page 15: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Fractal landscapes

• Add textures, material properties and use a nice rendering algorithm

• Example: Terragen Free (no cost version)http://www.planetside.co.uk/terragen/

CSE 167, Winter 2018 15http://planetside.co.uk/terragen-image-gallery/

Page 16: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

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 2018 16

Page 17: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

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 2018 17

Page 18: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

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 2018 18

2 iterations 4 iterations

6 iterations 8 iterations

Page 19: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Example: fern

• Variables: X, FX no drawing operationF move forward

• Constants: +, −+ Turn left 25 degrees− Turn right 25 degrees

• Start: X• Rules: (X → F[−X][X]F[−X]+FX),(F → FF)

CSE 167, Winter 2018 19

[Wikipedia]

[ corresponds to saving the current values for position and angle (push), which are 

restored when the corresponding ] is executed 

(pop)

Page 20: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Examples

http://www.kevs3d.co.uk/dev/lsystems/

CSE 167, Winter 2018 20

Page 21: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Fractal trees

• Tutorial for recursive generation of trees in 3D:http://web.comhem.se/solgrop/3dtree.htm– Model trunk and branches as cylinders– Change color from brown to green at certain level of recursion

CSE 167, Winter 2018 21Dragon Curve Tree Source: Allen Pike

Page 22: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

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 2018 22

Page 23: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Example: Coca‐Cola bottle

23

Evolution of Coca‐Cola bottles

Division of a Coca‐Cola bottleCSE 167, Winter 2018

Page 24: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Example: Coca‐Cola bottle

• Shape computation for two existing Coca‐Cola bottles

CSE 167, Winter 2018 24Source: Chau et al.: “Evaluation of a 3D Shape Grammar Implementation”, Design Computing and Cognition'04, pp. 357‐376

Page 25: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Example: procedural buildings

• Demo fr‐041: debris by Farbrausch, 2007https://www.youtube.com/watch?v=wqu_IpkOYBg&hd=1 Single, 177 KB EXE file!http://www.farbrausch.de/

CSE 167, Winter 2018 25

Page 26: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Shadow mapping

Page 27: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Shadows

• Give additional cues on scene lighting

27CSE 167, Winter 2018

Page 28: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Shadows

• Contact points• Depth cues

CSE 167, Winter 2018 28

Page 29: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Shadows

• Realism

CSE 167, Winter 2018 29Without self-shadowing With self-shadowing

Page 30: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Terminology

Umbra: fully shadowed regionPenumbra: partially shadowed region

CSE 167, Winter 2018 30

(area) light source

receiver shadow

occluder

umbra

penumbra

Page 31: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

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 2018 31

point directional area

umbra penumbra

Page 32: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Hard and soft shadows

CSE 167, Winter 2018 32

Hard shadow from point light source

Soft shadow fromarea light source

Page 33: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

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 2018 33

Page 34: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

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 2018 34

Scene points are lit if visible from light source

Determine visibility from light source by placing camera

at light source position

Page 35: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

• 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 2018 35

Two pass algorithm

Page 36: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

• 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 2018 36Final image with shadows

vb is in shadow pixel seen

from eye vb

Two pass algorithm

Page 37: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

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 2018 37

(0,0)

(1,1)

Light space

Object space

Shadow map

Page 38: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

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 2018 38

Page 39: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

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 2018 39

Page 40: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

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 2018 40

Page 41: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Shadow mapping

• Demohttp://www.paulsprojects.net/opengl/shadowmap/shadowmap.html

CSE 167, Winter 2018 41

Page 42: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Shadow mapping

• Tutorialhttp://www.opengl‐tutorial.org/intermediate‐tutorials/tutorial‐16‐shadow‐mapping/

CSE 167, Winter 2018 42

Page 43: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Shadow maps

• Issues– Sampling problems– Limited field of view (of shadow map)– Z‐fighting

CSE 167, Winter 2018 43

Page 44: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Sampling problems

• Shadow map pixel may project to many image pixels– Stair‐stepping artifacts

CSE 167, Winter 2018 44

Page 45: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

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 2018 45

Page 46: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

• What if a scene point is outside the field of view of the shadow map?

field ofview

Limited field of view

CSE 167, Winter 2018 46

Page 47: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

• 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

shadowmaps

CSE 167, Winter 2018 47

Limited field of view

Page 48: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

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 mapImagepixels

Shadow mappixels Pixel is

consideredin shadow!

Depth of pixel

Depth of shadow map

CSE 167, Winter 2018 48

Page 49: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

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 2018 49Correct bias Not enough bias Too much bias

Page 50: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Z‐fighting

CSE 167, Winter 2018 50

Just right

Not enough bias Too much bias

Page 51: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

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 2018 51

Page 52: Procedural modeling and shadow mappingCSE 167: Computer graphics • Procedural modeling – Height fields – Fractals • L‐systems – Shape grammar • Shadow mapping CSE 167,

Shadow mapping

• Resources– Overview (lots of links)

http://www.realtimerendering.com/– Basic shadow maps

http://en.wikipedia.org/wiki/Shadow_mapping– Avoiding sampling problems in shadow maps

http://www.comp.nus.edu.sg/~tants/tsm/tsm.pdfhttp://www.cg.tuwien.ac.at/research/vr/lispsm/

– Faking soft shadows with shadow mapshttp://people.csail.mit.edu/ericchan/papers/smoothie/

CSE 167, Winter 2018 52