Procedural Modeling and Shadow Mapping
Total Page:16
File Type:pdf, Size:1020Kb
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 2018 2 Based 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 CSE 167, Winter 2018 3 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 Photograph Rendering [Levoy et al.] – Construct 3D models and/or textures algorithmically CSE 167, Winter 2018 4 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 [Deussen et al.] – For example, a single tree pattern can be used to model an entire forest CSE 167, Winter 2018 5 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 Procedural modeling • Height fields • Fractals – L‐systems • Shape grammar CSE 167, Winter 2018 7 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 Midpoint displacement algorithm Start with single horizontal line segment. Repeat for sufficiently large number of times • { Random midpoint Repeat over each line segment in scene { displacement algorithm Find midpoint of line segment. Displace midpoint in Y by random amount. Reduce range for random numbers. (one‐dimensional) } } • Similar for triangles, quadrilaterals Step 0 Step 1 Step 2 Step 3 Result: Mountain Range CSE 167, Winter 2018 9 Source: http://gameprogrammer.com/fractal.html#midpoint 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 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 From Wikipedia CSE 167, Winter 2018 11 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 Demo: http://www.scale18.com/canvas2.html CSE 167, Winter 2018 12 Fractals • 3D Mandelbrot Zoom https://www.youtube.com/watch?v=0clz6WLfWaY CSE 167, Winter 2018 13 Fractals • Trip inside a 3D fractal (Kleinian) GPU realtime rendering https://www.youtube.com/watch?v=XIzScwydxOE CSE 167, Winter 2018 14 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 15 http://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 2018 16 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 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) 2 iterations 4 iterations 6 iterations 8 iterations CSE 167, Winter 2018 18 Example: fern • Variables: X, F X no drawing operation F move forward • Constants: +, − + Turn left 25 degrees − Turn right 25 degrees [Wikipedia] • Start: X [ corresponds to saving the • Rules: current values for position and angle (push), which are (X → F[−X][X]F[−X]+FX),(F → FF) restored when the corresponding ] is executed CSE 167, Winter 2018(pop) 19 Examples http://www.kevs3d.co.uk/dev/lsystems/ CSE 167, Winter 2018 20 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 Dragon Curve Tree CSE 167, Winter 2018 21 Source: 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 2018 22 Example: Coca‐Cola bottle Evolution of Coca‐Cola bottles CSE 167, Winter 2018 23 Division of a Coca‐Cola bottle Example: Coca‐Cola bottle • Shape computation for two existing Coca‐Cola bottles Source: Chau et al.: “Evaluation of a 3D Shape Grammar Implementation”, Design Computing and Cognition'04, pp. 357‐376 CSE 167, Winter 2018 24 Example: procedural buildings • Demo fr‐041: debris by Farbrausch, 2007 https://www.youtube.com/watch?v=wqu_IpkOYBg&hd=1 Single, 177 KB EXE file! http://www.farbrausch.de/ CSE 167, Winter 2018 25 Shadow mapping Shadows • Give additional cues on scene lighting CSE 167, Winter 2018 27 Shadows • Contact points • Depth cues CSE 167, Winter 2018 28 Shadows • Realism Without self-shadowing With self-shadowing CSE 167, Winter 2018 29 Terminology Umbra: fully shadowed region Penumbra: partially shadowed region (area) light source occluder penumbra umbra receiver shadow CSE 167, Winter 2018 30 Hard and soft shadows • Point and directional lights lead to hard shadows, no penumbra • Area light sources lead to soft shadows, with penumbra point directional area umbra penumbra CSE 167, Winter 2018 31 Hard and soft shadows Hard shadow from Soft shadow from point light source area light source CSE 167, Winter 2018 32 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 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 Determine visibility from Scene points are lit if light source by placing camera visible from light source CSE 167, Winter 2018at light source position 34 Two pass algorithm • First Pass depth value – Render scene by placing camera at light source position – Store depth image (shadow map) Depth image as seen CSE 167, Winter 2018from light source 35 Two pass algorithm • Second Pass – Render scene from camera position vb is in shadow pixel seen – At each pixel, compare from eye vb 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 Final image with shadows CSE 167, Winter 2018 36 Shadow map look‐up • Need to transform each