10. 3D Graphics 10.1 Introduction 10 2 B 10.2 Basics i off 3D G Graphics hi 10.3 Hierarchical Modeling 10.4 Projection 10 5 Visibility 10.5 10.6 Illumination 10 7 T 10.7 Texturing t i Thomas Ertl Graphical-Interactive Systems WS 10/11 1 Course Structure Introduction Human cognition and perception (aspects of human actions and perception) Color perception and color models Graphical p output p devices Raster graphics 2D graphics Interaction GUI: foundation, basics GUI: toolkits,, development p Usability engineering 3D computer t graphics: hi objects, transformations, visibility, illumination, shading Thomas Ertl Graphical-Interactive Systems WS 10/11 2 10. 3D Graphics 10.1 Introduction 10 2 B 10.2 Basics i off 3D G Graphics hi 10.3 Hierarchical Modeling 10.4 Projection 10 5 Visibility 10.5 10.6 Illumination 10 7 T 10.7 Texturing t i Thomas Ertl Graphical-Interactive Systems WS 10/11 3 10.1 Introduction 3D computer graphics Thomas Ertl Graphical-Interactive Systems WS 10/11 4 10.1 Introduction 3D computer graphics • 3D objects: surface modeling modeling, freeform surfaces vs vs. polygons • Curves and surfaces: Polynomials, Polynomials Bezier Bezier, de Casteljau • Triangle meshes: indexed face sets, edge lists, triangle strips • Pipeline of geometric transformations: p j , vertex transformation byy homogenous g affine-projective, 4x4 matrices, transformation of normals • Hierarchical modeling: matrix stacks, scene graphs Thomas Ertl Graphical-Interactive Systems WS 10/11 5 10.1 Introduction 3D computer graphics • Projection – Perspective division, homogeneous coordinates – Mapping in direction of the projection: numerical problems and near/far clipping planes, field-of-view – Normalization transformation: view frustum in a unit cube – Clipping in homogeneous coordinates – Classification of projections: perspective - parallel (orthogonal, oblique) Thomas Ertl Graphical-Interactive Systems WS 10/11 6 10.1 Introduction 3D computer graphics • Visibility – Culling vs. image-space methods, backface culling – Depth sorting, painter's painter s algorithm, BSP trees – Z-buffer method, scanline interpolation of z values • Illumination – Lightsource – object, diffuse/specular reflections – Illumination models: local – global (ray tracing, radiosity), physics h i b based/software-simulation d/ ft i l ti – heuristic/hardwareh i ti /h d accelerated – Phong g model: ambient, diffuse, specular, p material colors – Shading methods: flat, Gouraud, Phong Thomas Ertl Graphical-Interactive Systems WS 10/11 7 10.1 Introduction 3D computer graphics • Textures – – – – – General mapping methods and multi-textures Texture sources, mapping, dimension Texture coordinates, parameterization of standard objects Texture filtering, mipmapping Perspectively correct interpolation Thomas Ertl Graphical-Interactive Systems WS 10/11 8 10. 3D Graphics 10.1 Introduction 10 2 B 10.2 Basics i off 3D G Graphics hi 11 Geometric Modeling 10.3 Hierarchical Modeling 10 4 Projection 10.4 10.5 Visibility 10 6 Ill 10.6 Illumination i ti 10.7 Texturing Thomas Ertl Graphical-Interactive Systems WS 10/11 9 10.2 Basics of 3D Graphics 3D world with objects geometry primitive j subsystem object raster subsystem color image • Objects – Points, Points curves, curves surfaces surfaces, volumes – Boundary representation (Brep): surface around an object • Surface modeling – Freeform surfaces • Polynomial splines • For example: Bézier patches, B-splines – Polygons • Piecewise Pi i linear li approximation i ti • Problems with non-flat polygons → split p into triangles g Thomas Ertl Graphical-Interactive Systems WS 10/11 10 10.2 Basics of 3D Graphics Parametric polynomial curves n + 1 points Î interpolation polynomials of degree n, oscillate for high order spline = several curves with continuous/smooth connection piecewise cubic + smooth connection approximating cubic Bézier curve Thomas Ertl Graphical-Interactive Systems WS 10/11 11 10.2 Basics of 3D Graphics 3D models d l in i computer t graphics: hi triangle ti l meshes h v2 • Geometry: coordinates of the nodes (vertices) 1 2 • Topology: v3 connection of vertices by edges P1 P2 v1 5 • Data structures 4 3 – Indexed face set: list of nodes + list of indices V = (v 1,v 2 ,v 3 ,v 4 ) P1 = (1,4,2) P2 = (2,4,3 ) v4 – List of vertices + list of edges g + indexed vertex indices V = (v 1,v 2 ,v 3 ,v 4 ) E1 = (v 1,v 2 ) E 2 = (v 2 ,v 3 ) P1 = (E1, E 4 , E5 ) – Complex pointer structures (winged edge) Thomas Ertl Graphical-Interactive Systems WS 10/11 12 10.2 Basics of 3D Graphics Ti Triangle l strips ti v0 v2 v4 The normal vector points outward: vertices are counter-clockwise . v1 v3 v5 ( v0 v1 v2 v3 v4 v0 v1 v2 v2 v1 v3 v2 v3 v4 v4 v3 v5 ) = Swap v5 Thomas Ertl Graphical-Interactive Systems WS 10/11 13 10.2 Basics of 3D Graphics G Graphics hi pipeline: i li geometry t ttransformation f ti object coordinates di t model transformation affine modelview transform world coordinates view transformation affine eye coordinates normalization t transformation f ti (projective) Thomas Ertl Graphical-Interactive Systems WS 10/11 normalized clip li coordinates clipping li i rasterization t i ti 14 10.2 Basics of 3D Graphics 4x4 4 4 matrices ti • Affine transformation in homogeneous coordinates P ′ = M (4×4 )P rotation shearing scaling projection ⎛ ⎛. ⎜⎜ ⎜ ⎜. ⎜ ⎜. ⎜⎜ ⎝ ⎝ (. . .⎞ ⎟ . .⎟ . . ⎟⎠ . .) ⎛.⎞ ⎞ ⎜ ⎟⎟ ⎜.⎟ ⎟ ⎜.⎟ ⎟ ⎝ ⎠⎟ 1 ⎟⎠ ⎛x⎞ ⎜ ⎟ ⎜y⎟ ⎜z⎟ ⎜ ⎟ ⎜ 1⎟ ⎝ ⎠ translation homogenization Thomas Ertl Graphical-Interactive Systems WS 10/11 15 10.2 Basics of 3D Graphics Example E l off a 4x4 4 4 matrix ti • Rotation around the z axis ⎛ ⎛ cos ϕ − sin ϕ 0 ⎞ 0⎞ ⎜⎜ ⎟ ⎟ cos ϕ 0 ⎟ 0⎟ ⎜ ⎜ sin ϕ ⎜⎜ 0 ⎟ 0⎟ 0 1 ⎝ ⎠ ⎟⎟ ⎜⎜ 0 0 0 1 ⎝ ⎠ • Column vectors are images of the unit (basis) vectors • Affine mapping in 3D needs the image of 4 points in space (tetrahedron) (t t h d ) • 4 (points) x 3 (coordinates) = 3 x 3 (matrix) + 1 x 3 (translation) Thomas Ertl Graphical-Interactive Systems WS 10/11 16 10.2 Basics of 3D Graphics T Transforming f i th the normall vector t ⎛x⎞ ⎜ ⎟ ⎜y ⎟ point P = ⎜ ⎟ is on plane E : z ⎜ ⎟ ⎜ 1⎟ ⎝ ⎠ ⎛a⎞ ⎜ ⎟ ⎜b⎟ normal n = ⎜ ⎟ c ⎜ ⎟ ⎜d ⎟ ⎝ ⎠ ( ax + by + cz + d = 0 nT ⋅ P = 0 P′ = M ⋅P ) n T M −1 ⋅ M P = 0 n T ⋅ M −1 ⋅ (M ⋅ P ) = n ′ T ⋅ P ′ = 0 ( ) P ⎛x⎞ ⎜ ⎟ y (a b c d )⎜⎜ ⎟⎟ = 0 z ⎜ ⎟ ⎜ 1⎟ ⎝ ⎠ nT ( ⇒ n ′ = M −1 Thomas Ertl Graphical-Interactive Systems WS 10/11 ) T ⋅n 17 10.2 Basics of 3D Graphics Transforming the normal vector • Transformation rule: ( n ′ = M −1 ) T ⋅n • Affine Affi ttransformation f ti off normall vectors: t – By the transposed inverse matrix – 3x3 sub-block without translation/projection Thomas Ertl Graphical-Interactive Systems WS 10/11 18 10. 3D Graphics 10.1 Introduction 10 2 B 10.2 Basics i off 3D G Graphics hi 10.3 Hierarchical Modeling 10.4 Projection 10 5 Visibility 10.5 10.6 Illumination 10 7 T 10.7 Texturing t i Thomas Ertl Graphical-Interactive Systems WS 10/11 19 10.3 Hierarchical Modeling y y x Identity double chair x y R(180°) double x 4 combo table y TS1 TS2 x double Thomas Ertl Graphical-Interactive Systems WS 10/11 TV1 4 combo TV2 4 combo TT room 20 10.3 Hierarchical Modeling S Scene graph h (DAG = di directed t d acyclic li graph) h) room TV1 TV2 Transformation of chair n in world coordinates: (TV1 R180 TS2) R TS1 n o I TS2 Transformation of chair o in world coordinates: TT table chair (TV1 R180 TS1) Thomas Ertl Graphical-Interactive Systems WS 10/11 21 10.3 Hierarchical Modeling Matrix M t i stacks t k • Efficiency requires the re-use of matrices – Matrix M t i stacks t k – Top row: current transformation matrix Load(I) Push Mult(TV1) Push Mult(R180) Push Mult(TS1) Chair I I I TV1 I TV1 I TV1 R I I I TV1 I TV1 I TV1 R I TV1 R I TV1 I I I TV1 I TV1 I I I Thomas Ertl Graphical-Interactive Systems WS 10/11 I TV1 R I TV1 R TS1 Pop ... I TV1 R 22 10. 3D Graphics 10.1 Introduction 10 2 B 10.2 Basics i off 3D G Graphics hi 10.3 Hierarchical Modeling 10.4 Projection 10 5 Visibility 10.5 10.6 Illumination 10 7 T 10.7 Texturing t i Thomas Ertl Graphical-Interactive Systems WS 10/11 23 10.4 Projections Projection P j ti vs. Perspective P ti • Projection: “One dimension is lost” (e.g., straight line Æ point) • Perspective: “Parallel lines meet in a point” – Projective mapping not affine, but any affine mapping is projective • Weaker invariants – Not p preserved: distances,, angles, g , areas – Incidence is preserved (membership of a point on a line) – Cross ratio is preserved: x 2 − x0 x3 − x1 x 2 − x1 x3 − x0 Thomas Ertl Graphical-Interactive Systems WS 10/11 24 10.4 Projections Projection P j ti vs. Perspective P ti • Projective geometry in 2D: – D Duality lit off points i t and d straight t i ht lilines • One straight line through 2 points • Intersection of 2 straight lines: one point – Parallel lines intersect at infinity – Infinite points lie on infinity line • In 3D: duality of points and planes • A projection is defined f by the image off five f 3D points – 5 x 3 = 4 x 4 –1 elements – 4x4 matrix – 1 component (homogeneous) Thomas Ertl Graphical-Interactive Systems WS 10/11 25 10.4 Projections 2D perspective ti projection j ti • Center of projection • Projection plane ⎛x ⎞ x′ x x " =" ⎜⎜ h ⎟⎟ = ⇒ x′ = z +1 D z+D ⎝ wh ⎠ D z Normallyy z' = 0, but we need z' to calculate the visibility z′ = ⎛z ⎞ z " =" ⎜⎜ h ⎟⎟ z D +1 ⎝ wh ⎠ ⎛ x h ⎞ ⎛ 1 0 0 ⎞⎛ x ⎞ ⎜ ⎟ ⎜ ⎟⎜ ⎟ ⎜ zh ⎟ = ⎜ 0 1 0 ⎟⎜ z ⎟ ⎜ w ⎟ ⎜ 0 1 D 1 ⎟⎜ 1 ⎟ ⎝ h⎠ ⎝ ⎠⎝ ⎠ Thomas Ertl Graphical-Interactive Systems WS 10/11 (x, z ) “screen” (x ′, z′) x D eye 26 10.4 Projections Perspective projection • • Shearing z in w-direction Homogeneous matrices include projections ⎛ x ⎞ ⎛ 1 0 0 ⎞⎛ x ⎞ ⎜ ⎟ ⎜ ⎟⎜ ⎟ ⎜ z ⎟ = ⎜ 0 1 0 ⎟⎜ z ⎟ ⎜ w ⎟ ⎜ 0 1 D 1 ⎟⎜ 1 ⎟ ⎝ ⎠ ⎝ ⎠⎝ ⎠ w C B' eye B 1 A' A a ⎛ 1 a⎞ ⎟⎟ ⎜⎜ ⎝ 0 1⎠ 1 shear homogeneous division C' –D 0 A: far, far in front of the eye B: just before the eye C: behind the eye eye z Î closer together Î further to negative values Î to positive values Ζ∞ Thomas Ertl Graphical-Interactive Systems WS 10/11 27 10.4 Projections • Non-linearity N li it off z transformation t f ti z‘ 0 eye –D z strongly nonlinear Æ Numerical problem (overflow / underflow) • Range restriction in z: – near / far clipping planes Thomas Ertl Graphical-Interactive Systems WS 10/11 28 10.4 Projections E Example l off a 2D projection j ti y eye z near (screen) eye f far y' –1 1 z' Thomas Ertl Graphical-Interactive Systems WS 10/11 29 10.4 Projections IIn 3D: 3D normalization li ti ttransformation f ti • View frustum Æ unit cube y far clipping plane z near x clipping plane eye • View frustum often specified by – Field of view (FOV): opening angle – Aspect ratio (screen width/screen height) Thomas Ertl Graphical-Interactive Systems WS 10/11 30 10.4 Projections Clipping Cli i iin homogeneous h coordinates di t • Advantage: clipping against unit cube is easier than against view frustum in world/eye coordinates • But: We must clip in homogeneous coordinates – Otherwise: Oth i wrong segments t – Issue: negative w values – Lines Li th thatt are contained t i d iin b both th cones w e.g. − 1 < w > 0 and − w < x < w x < +1 w w < 0 and w < x < −w Thomas Ertl Graphical-Interactive Systems WS 10/11 x 31 10.4 Projections Clipping Cli i iin homogeneous h coordinates di t • First (projective) normalization transformation • Then clipping in (homogeneous) ( ) clip coordinates • Then (perspective) division by w results in normalized d i coordinates device di t (3D unit it cube) b ) • Then projection to screen coordinates (draw at position x, x y, y use z for visibility) (But: sometimes pixel-based scissoring on modern graphics hardware) Thomas Ertl Graphical-Interactive Systems WS 10/11 32 10.4 Projections Cli i iin homogeneous Clipping h coordinates di t w A C'' C' C B 1 A' eye B' 0 z sshear ea correct segment: A´C´ wrong segment: A´C´´ homogeneous division view frustum Thomas Ertl Graphical-Interactive Systems WS 10/11 33 10.4 Projections E Exterior/interior i /i i line li segments Y B A A' Z wrong interior segment eye B' correct exterior segment screen Thomas Ertl Graphical-Interactive Systems WS 10/11 34 10.4 Projections H Homogeneous window-edge i d d coordinates di t x > −1 x + w = WECL (P ) > 0 w x R : < 1 w − x = WECR (P ) > 0 w L: ( (assuming g that w > 0) I t Intersecti tion off L(α ) = P1 + α (P2 − P1 ) WECL (L(α )) = x1 + α (x 2 − x1 ) + w 1 + α (w 2 − w 1 ) = 0 ⇒α = w 1 + x1 WECL (P1 ) = (w1 + x1 ) − (w 2 + x2 ) WECL (P1 ) − WECL (P2 ) Thomas Ertl Graphical-Interactive Systems WS 10/11 35 10.4 Projections Standard St d d viewing i i conditions diti (e.g., ( OpenGL) O GL) • Eye is in origin (eye coordinates) • View in direction off negative z axis • Specified by – E Eye point i – Line of sight – Up vector • Model transform + view transform = model-view matrix Thomas Ertl Graphical-Interactive Systems WS 10/11 36 10.4 Projections C Computing ti th the ttransformation f ti matrix: t i example l • z In 2D: based on image of four projected points ⎛0⎞ ⎛0⎞ ⎜ ⎟ ⎜ ⎟ R ⎜ 0 ⎟ = k1⎜ − 1⎟ ⎜ 1⎟ ⎜0⎟ ⎝ ⎠ ⎝ ⎠ n (0,0 ) eye at −∞ ⎛ 0 ⎞ ⎛0⎞ ⎜ ⎟ ⎜ ⎟ R ⎜ − n ⎟ = k 2 ⎜ − 1⎟ ⎜ 1 ⎟ ⎜1⎟ ⎝ ⎠ ⎝ ⎠ (− n,− n ) (n,− n ) (− f ,−f ) ⎛ f ⎞ ⎛1⎞ ⎜ ⎟ ⎜ ⎟ R ⎜ − f ⎟ = k 3 ⎜1⎟ ⎜ 1⎟ ⎜1⎟ ⎝ ⎠ ⎝ ⎠ ⎛ 1⎞ ⎛ 1⎞ ⎜ ⎟ ⎜ ⎟ R⎜ 0 ⎟ = k 4 ⎜ 0 ⎟ ⎜0⎟ ⎜0⎟ ⎝ ⎠ ⎝ ⎠ o q x (f ,−f ) p z‘ (− 1,1) p q x direction remains the same o x‘ (1− , 1) n Thomas Ertl Graphical-Interactive Systems WS 10/11 37 10.4 Projections • In I 2D: 2D 12 equations ti ffor 12 unknown k values l • Goal: 3x3 matrix – 1 and coefficients k1, k2, k3, k4 ⎛1 0 ⎜ − (f + n ) R = ⎜0 f −n ⎜⎜ −1 0 ⎝ 0 ⎞ − 2fn ⎟ ⎟ f −n ⎟ 0 ⎟⎠ • In 3D with asymmetric y frustum (left/right, ( g , top/bottom) p ) ⎛ 2n ⎜ ⎜r −l ⎜ 0 R =⎜ ⎜ ⎜ 0 ⎜ ⎝ 0 0 2n t −b 0 0 Thomas Ertl Graphical-Interactive Systems WS 10/11 r +l r −l t +b t −b − (f + n ) f −n −1 ⎞ 0 ⎟ ⎟ 0 ⎟ ⎟ − 2fn ⎟ ⎟ f −n ⎟ 0 ⎠ 38 10.4 Projections Cl Classification ifi ti off projections j ti Planar geometric projections P ll l Parallel Orthographic Top P Perspective ti Oblique One-point Cabinet Axonometric Cavalier Front elevation Side elevation Two-point Three point Three-point Isometric Dimetric Trimetric Thomas Ertl Graphical-Interactive Systems WS 10/11 39 10.4 Projections Classification Cl ifi ti off projections j ti • Perspective projection – P Parallel ll l straight t i ht lines li only l stay t parallel ll l if th they lilie on a plane l parallel to the projection plane – All the other straight g lines ((of the same direction)) will intersect in vanishing point(s) (1,2,3-point perspectives) • Parallel projection – All projection “rays” are parallel (i.e., only a single direction of projection) – Used for engineering and architecture because they can be used for measurements Thomas Ertl Graphical-Interactive Systems WS 10/11 40 10.4 Projections Parallel P ll l projection j ti • Orthographic – O Orthogonal th l to t projection j ti plane l – Multi-view orthographic: projection plane orthogonal g or p parallel to the p principal p axes – Axonometric multi-view orthographic • Projection plane is not aligned with coordinate di t system t • Isometric: same contraction along each axis,, same angle g (120°) ( ) • Dimetric: angles between two of the principal axes are equal • Trimetric: T i t i th three diff differentt angles l axonometric DOP: direction of projection VPN: view plane normal Thomas Ertl Graphical-Interactive Systems WS 10/11 41 10.4 Projections Parallel P ll l projection j ti • Oblique – C Cavalier li projection j ti (45° - no contraction) – Cabinet projection p j (~63° - contraction 1/2) oblique DOP: direction of projection VPN: view plane normal Thomas Ertl Graphical-Interactive Systems WS 10/11 42 10.4 Projections P Parallel ll l projections j ti Thomas Ertl Graphical-Interactive Systems WS 10/11 43 10.4 Projections P Perspective ti projections: j ti vanishing i hi points i t Thomas Ertl Graphical-Interactive Systems WS 10/11 44 10. 3D Graphics 10.1 Introduction 10 2 B 10.2 Basics i off 3D G Graphics hi 10.3 Hierarchical Modeling 10.4 Projection 10 5 Visibility 10.5 10.6 Illumination 10 7 T 10.7 Texturing t i Thomas Ertl Graphical-Interactive Systems WS 10/11 45 10.5 Visibility Visible Vi ibl line/surface li / f computation t ti • Determination of the visible surfaces (visibility) • Image-space approach (z-buffer) ( ff ) – Compare pixels’ depths • Object-space Obj t approach h (d (depth th sort) t) – Compare polygons and intersect them • “Culling” Culling – View frustum culling (clipping) – Occlusion culling – Backface culling • Remove invisible backfaces (approx. one half of all faces f convex objects) for bj ) • Calculate normal vector; dot product with direction of projection p j (z ( component p > 0)) Thomas Ertl Graphical-Interactive Systems WS 10/11 46 10.5 Visibility H l in Halos i li line d drawings i [1] Thomas Ertl Graphical-Interactive Systems WS 10/11 47 10.5 Visibility Depth D th sortt List priority algorithms • Sorts S all polygons from f the front f to back • Draw from back to front (painter’s algorithm), e.g. in i P Postscript t i t • Problems – Sorting according to which z? (center point?) – Cycles cannot be sorted M P P MQ Q eye 2 eye 1 Thomas Ertl Graphical-Interactive Systems WS 10/11 48 10.5 Visibility Depth D th sort: t b bounding di box b / z extent t t 1. Sort all polygons according to smallest (furthest) z 2. Test whether z extents overlap 3. Min/max test based on axis-aligned bounding boxes 4. Test overlap in x-y plane 5. Possibly partition the objects Thomas Ertl Graphical-Interactive Systems WS 10/11 49 10.5 Visibility BSP trees t (binary (bi space partitioning) titi i ) • Sorting the objects according to partitioning planes (oriented half spaces) • The drawing order is given by traversing – Compare eye point with orientation of partitioning plane – First, paint all objects behind • Take object polygons as planes and split other objects BSP tree x 1023 1 Q dt Quadtree 2 < y y 3 0 > 0 4 x 1023 Thomas Ertl Graphical-Interactive Systems WS 10/11 y < > < > 3 1 2 1 2 3 4 4 50 10.5 Visibility Begin with level A Sort according to normal direction (+/-) (+/ ) Split half spaces Determine D t i object bj t nextt to the eye Sort from front to back (or in reverse order) Thomas Ertl Graphical-Interactive Systems WS 10/11 51 10.5 Visibility • Select S l t polygon l planes l as separating ti planes l • Partition intersected objects Thomas Ertl Graphical-Interactive Systems WS 10/11 52 10.5 Visibility Z–buffer Z b ff (image-space (i approach) h) • Compute z value for each pixel and compare with the depth from pixels already drawn Æ needs additional z-buffer (16-32 bits) 1 2 2 3 3 3 4 4 4 4 1 2 3 4 0 2 4 4 0 5 4 4 6 5 5 4 4 4 3 3 3 3 6 5 4 4 Thomas Ertl Graphical-Interactive Systems WS 10/11 53 10.5 Visibility Z-buffer Z b ff algorithm l ith Initialize • Framebuffer ff Å background color • z_buffer Å z (of far clipping plane) (+1 in OpenGL) foreach polygon (in arbitrary order) foreach pixel(x, pixel(x y) in projected polygon z = z(x, y) Å from interpolation if z >= > z_buffer(x, z buffer(x y) then (GL_LESS (GL LESS in OpenGL) z_buffer(x, y) = z framebuffer(x, a ebu e ( , y) = p pixel(x, e ( , y) Thomas Ertl Graphical-Interactive Systems WS 10/11 54 10.5 Visibility Z-buffer Z b ff algorithm l ith Disadvantages: • Storage S requirements • z aliasing (especially for 16 bit buffers) • Transparency requires sorting • Much unnecessary work for scenes with high depth complexity l it ((overdraw) d ) Thomas Ertl Graphical-Interactive Systems WS 10/11 55 10.5 Visibility Scanline S li interpolation i t l ti • In normalized vertex coordinates y y1 scanline ys r1 ra y2 r −r r1 − r2 = a 1 y1 − y 2 y s − y1 rp xp rb x r2 y3 r3 incremental: rp +1 = rp + Δx Thomas Ertl Graphical-Interactive Systems WS 10/11 ra = r1 − (r1 − r2 ) y1 − y s y1 − y 2 rb = r1 − (r1 − r3 ) y1 − y s y1 − y 3 rp = rb − (rb − ra ) xb − x p x b − xa rb − ra x b − xa 56 10. 3D Graphics 10.1 Introduction 10 2 B 10.2 Basics i off 3D G Graphics hi 10.3 Hierarchical Modeling 10.4 Projection 10 5 Visibility 10.5 10.6 Illumination 10 7 T 10.7 Texturing t i Thomas Ertl Graphical-Interactive Systems WS 10/11 57 10.6 Illumination • Li Lighting hti effects ff t essential ti l ffor 3D iimpression i (i (in 2D images) • Interaction of – Light sources (intensity, color, position...) – Objects (material properties properties, orientation wrt the light source) • Types of reflection – Specular p reflection • Angle of incidence = angle of reflection – Diffuse Diff reflection fl ti • Same reflection into all directions Thomas Ertl Graphical-Interactive Systems WS 10/11 eye ϕϕ 58 10.6 Illumination Lambert L b t reflection fl ti • Perfectly diffuse reflection N ϑ L dA´ ϑ dA effectively effecti el illuminated ill minated ssurface rface dA dA´ = dA cos ϑ calculating intensity I ~ cos ϑ Thomas Ertl Graphical-Interactive Systems WS 10/11 59 10.6 Illumination Reflection R fl ti coefficient ffi i t • Real objects often reflect diffuse and specular light simultaneously: “glossy” glossy surfaces • Material described by diffuse and specular reflection coefficients Thomas Ertl Graphical-Interactive Systems WS 10/11 60 10.6 Illumination eye Reflection R fl ti models d l • Local reflection: – C Consider id only l directly di tl visible i ibl (point- ) light sources (as seen from the object ) – No further reflections diff se/spec lar diffuse/specular eye • Global reflection: – Consider multiply reflected light reflective Thomas Ertl Graphical-Interactive Systems WS 10/11 61 10.6 Illumination Illumination Ill i ti calculation l l ti • Global models: – R Ray ttracing i (according to Whitted): • Some p point light g sources • Several perfectly reflecting reflections • Sharp Sh shadow h d edges d – Radiosity: • All surfaces emit and/or reflect light diffusely • Soft shadow edges • Indirect lighting Thomas Ertl Graphical-Interactive Systems WS 10/11 62 10.6 Illumination Ray tracing Radiosity Thomas Ertl Graphical-Interactive Systems WS 10/11 63 10.6 Illumination Illumination Ill i ti calculation l l ti • Physics-based illumination models – C Compute t physical h i l quantities titi ((radiant di t flflux, radiance) di ) – Physical material laws: interaction between light and matter – Complex numerical simulation • Hardware-accelerated graphics (e.g. OpenGL) – “Arbitrary” y units – Heuristic, local illumination models (Phong) – “Faking” realism: textures Thomas Ertl Graphical-Interactive Systems WS 10/11 64 10.6 Illumination Phong ill Ph illumination i ti • 3 components of intensity: I = Ia + Id + Is – A Ambient: bi t • Environment light from all directions • “Simulates” Simulates multiple scattering / multiple reflection – Diffuse: Lambert law – Specular: Not perfectly reflecting N ϑ L • Diffuse component Id = k d ⋅ IL ⋅ cos ϑ = k d ⋅ IL ⋅ (N • L ) intensity of light source diffuse reflection coefficient Thomas Ertl Graphical-Interactive Systems WS 10/11 65 10.6 Illumination Phong ill Ph illumination i ti • Specular component – Extended E t d d hi highlights hli ht – Perfect reflection only towards direction R: α R + L = N ⋅ (N • L ) ⋅ 2 – Strongly decreasing values for other directions • Model according g to cosnα (p (perfectlyy reflecting: g n → ∞)) Is = k s ⋅ IL ⋅ cos n α = k s ⋅ IL ⋅ (R • V ) n eye N R V Thomas Ertl Graphical-Interactive Systems WS 10/11 α L ϑ ϑ 66 10.6 Illumination Blinn-Phong Bli Ph ill illumination: i ti halfway h lf vector t • Alternative for the specular component n Is = k s ⋅ IL ⋅ cos n α = k s ⋅ IL ⋅ (R • V ) by using the halfway vector H = (α ≈ β ) N H eye L +V L +V Is = k s ⋅ IL ⋅ (N • H ) n β R L V α ϑ ϑ Thomas Ertl Graphical-Interactive Systems WS 10/11 67 10.6 Illumination Blinn-Phong Bli Ph illumination ill i ti • In OpenGL additional aspects vertex color = material emission + global ambient light + ∑ attenuate (ambient + diffus + specular ) ⋅ spotlight LQ I λ = OEλ + Iaλ ⋅ Oaλ ( + ∑ faiλ ⋅ Iai λ ⋅ Oaλ + max (N • L i ,0 ) ⋅ Idi λ ⋅ Od λ i + (N • H i < 0 || N • L i < 0 ? 0 : N • H i ) ⋅ Isi λ ⋅ Osλ n ) • Instead of scalar reflection factors ka, kd, ks – Frequency-dependent material colors Oaλ, Odλ, Osλ – Diffuse Diff reflection fl i (usually) ( ll ) has h the h color l off the h surface f – Specular reflection (usually) has the color of the source of light (except for metals) Thomas Ertl Graphical-Interactive Systems WS 10/11 68 10.6 Illumination Shading Sh di • Illumination calculation in world or in eye coordinates • Iff illumination is only available at polygon vertices (not ( for each pixel) – How can we compute the pixel color from the vertex color? Æ Shading! • Flat shading g – Constant color in the triangle (Æ Mach bands) e.g., one normal vector for each triangle • Gouraud shading (smooth shading) . – Interpolation of the vertex colors • Phong Ph shading h di – Calculation of the illumination for every pixel Thomas Ertl Graphical-Interactive Systems WS 10/11 69 10.6 Illumination Gouraud G d (smooth) ( th) shading h di • Scanline interpolation of the vertex color (RGB) • Lighting computation in the geometry stage • Normal at vertices by weighted average from the surrounding di ttriangles i l N • Supported in (RGB) standard OpenGL (RGB) N (RGB) (RGB) N Thomas Ertl Graphical-Interactive Systems WS 10/11 70 10.6 Illumination Gouraud G d (smooth) ( th) shading h di • Problems: – B Bumps are reduced d d / removed d – No highlights inside the polygons – Creases / sharp edges have to be modeled by using several normal vectors per vertex – If all normal vectors are equal: flat shading Thomas Ertl Graphical-Interactive Systems WS 10/11 71 10.6 Illumination Phong shading Ph h di • Lighting for each pixel Æ Needs N d normall vector t att each h pixel i l • Scanline interpolation of the three components of the vertex normal,, then normalization to unit length g • High quality but high computational costs – 3 components, normalization, lighting per pixel in eye coordinates in rasterization stage – Not in standard OpenGL N – Fragment/pixel shader • Acceleration: – Interpolate scalar products – No normalization – Highlight textures N N Thomas Ertl Graphical-Interactive Systems WS 10/11 72 10.6 Illumination Shading Sh di problems bl • Smooth surfaces but non-smooth edges – At silhouette ilh tt lilines • Vertex inconsistencies – T-vertex T vertex (hanging node) • Linear interpolation only correct for triangles – Color depends on orientation Æ split into triangles 1 4 2 4 3 1 2 3 Thomas Ertl Graphical-Interactive Systems WS 10/11 73 10. 3D Graphics 10.1 Introduction 10 2 B 10.2 Basics i off 3D G Graphics hi 10.3 Hierarchical Modeling 10.4 Projection 10 5 Visibility 10.5 10.6 Illumination 10 7 T 10.7 Texturing t i Thomas Ertl Graphical-Interactive Systems WS 10/11 74 10.7 Texturing Simple Si l illumination ill i ti • No lighting • Constant C colors (flat shading) • Polygons P l • • • • Parallel light Diff Diffuse reflection fl ti Smooth shading Freeform surfaces Thomas Ertl Graphical-Interactive Systems WS 10/11 75 10.7 Texturing IImproved d ill illumination i ti • Parallel light • Specular S reflection f • Phong shading • Several local light sources • Different materials Thomas Ertl Graphical-Interactive Systems WS 10/11 76 10.7 Texturing Texturing T t i • 2D image texture • Bump mapping • Reflection textures Thomas Ertl Graphical-Interactive Systems WS 10/11 77 10.7 Texturing Motivation M ti ti • Realistic look: fine structures on surfaces • Combining C bi i pictures i and d geometry y z x geometry screen image t s vertices geometry pipeline rasterizer image g Thomas Ertl Graphical-Interactive Systems WS 10/11 pixel pipeline 78 10.7 Texturing Texturing: T t i modulation d l ti off iimage synthesis th i process • Reflection properties – Color, C l reflection fl ti coefficients, ffi i t ttransparency – Classical texture mapping • Illumination – Environment mapping / reflection mapping – Shadow mapping, pp g, illumination mapping pp g • Geometry – Displacement mapping: move vertices • Normal vectors – Bump mapping / normal mapping Thomas Ertl Graphical-Interactive Systems WS 10/11 79 10.7 Texturing E Examples l • Solid texturing • Bump mapping Reflection mapping Thomas Ertl Graphical-Interactive Systems WS 10/11 80 10.7 Texturing Multi-textures M lti t t • Complex optical effects • Combining C a number off textures Thomas Ertl Graphical-Interactive Systems WS 10/11 81 10.7 Texturing Texture T t sources • Images (e.g., photos, simulations, videos, ...) – – – – – Simple Si l acquisition i iti Limited resolution High memory requirements Attention: “frozen illumination” 1D images (color tables) and 3D images (volumes) • Algorithms (shaders) – Non-trivial programming – Unlimited resolution, anti-aliasing possible – Any algorithm Thomas Ertl Graphical-Interactive Systems WS 10/11 82 10.7 Texturing Dimensionality Di i lit off textures t t • Texture as a discretely sampled mapping • nD texture defines f the domain: – – – – 1D: parameter of a line, height, temperature,... 2D: parameter of a surface (u,v u v), ) direction (θ (θ,ϕ), ϕ) ... 3D: point in space (solid texture), direction vector,... 4D: lines in space p ((reflection)) • Image space: dimensionality of data values: – – – – 1D: parameter for reflection (kd): intensity/luminance 2D: intensity and transparency 3D: color, normal vector, reflection vector 4D color/vector 4D: l / t and d ttransparency Thomas Ertl Graphical-Interactive Systems WS 10/11 83 10.7 Texturing Texture T t mapping i • Point coordinates (x,y,z) • Surface S f coordinates (u,v) ( ) (e.g., ( Bézier patch)) • Texture coordinates (s,t,r), sometimes (u,v) • Direction vector (R, N, H,...) • Function of the above parameters (in a shader) – Projection onto other surfaces (sphere, cylinder, box,...) Thomas Ertl Graphical-Interactive Systems WS 10/11 84 10.7 Texturing F Forward d / backward b k d mapping i Backward mapping is standard in graphics hardware Thomas Ertl Graphical-Interactive Systems WS 10/11 85 10.7 Texturing C Compound d fforward d mapping i (2 (2-level l l mapping) i ) Thomas Ertl Graphical-Interactive Systems WS 10/11 86 10.7 Texturing Texture T t coordinates di t • Texture parameter generally in [0,1] – T Texture t coordinates di t att the th vertices ti off a surface f • Calculate of the texture coordinates of a pixel – Need a parameterization inside the surface • Triangle (linear scanline interpolation) • Rectangle (bilinear interpolation) s = a*b*s4 + (1-a)*b*s3 + a*(1-b)*s2 + (1-a) *(1-b)*s1 (s3, t3) 1 b 0 ( s1, t1) 0 Thomas Ertl Graphical-Interactive Systems WS 10/11 (s4, t4) p (s2, t2) a 1 87 10.7 Texturing Standard St d d parameterizations t i ti • Cylinder coordinates (φ,z) • Polar coordinates (φ,θ) ( • Analog for cone, torus, … Thomas Ertl Graphical-Interactive Systems WS 10/11 88 10.7 Texturing St d d parameterizations Standard t i ti Thomas Ertl Graphical-Interactive Systems WS 10/11 89 10.7 Texturing T t Texture coordinates di t Thomas Ertl Graphical-Interactive Systems WS 10/11 90 10.7 Texturing Texture T t wrapping i • Continuation of a texture beyond [0,1] clamp/ clamp clamp/ repeat repeat/ p clamp repeat/ repeat Thomas Ertl Graphical-Interactive Systems WS 10/11 91 10.7 Texturing Texture T t filtering filt i • Magnification: – Few texels are mapped to pixel many pixels – Nearest N t neighbor i hb sampling li • Take the nearest texel – Bilinear Bili iinterpolation t l ti texture • Interpolate between the four next texels • Use fractional coordinates (floating-point computations) • Results in smoother images Thomas Ertl Graphical-Interactive Systems WS 10/11 92 10.7 Texturing T t Texture filtering: filt i magnification ifi ti nearest neighbor bilinear Thomas Ertl Graphical-Interactive Systems WS 10/11 93 10.7 Texturing Texture T t filtering filt i • Minification: – Many texels are mapped to few pixels pixel – Footprint F t i t – Aliasing: • A high-frequency hi h f signal i l texture is sampled at low frequency – Filtering: • Average over (many) corresponding pixels • Too expensive to be done during runtime Thomas Ertl Graphical-Interactive Systems WS 10/11 94 10.7 Texturing T t Texture filtering: filt i minification i ifi ti / magnification ifi ti pixel center pixel center nearest texel 4 texels for f bilinear interpolation Thomas Ertl Graphical-Interactive Systems WS 10/11 95 10.7 Texturing MipMapping Mi M i • Simple prefiltering of textures • Multum In Parvo (MIP) ( ) • Store recursively textures of half sizes (1/3 memory increase) i ) • Filtering/averaging for each block of 2x2 texels Thomas Ertl Graphical-Interactive Systems WS 10/11 96 10.7 Texturing MipMapping Mi M i • Use texture resolution so that – Texelsize(n) T l i ( ) < pixel's i l' extent t t on ttexture t < ttexturesize(n+1) t i ( 1) • Reconstruction – Trillinear interpolation of the 8 nearest texels v d v u d u Thomas Ertl Graphical-Interactive Systems WS 10/11 97 10.7 Texturing Mi M MipMapping i No MipMapping Thomas Ertl Graphical-Interactive Systems WS 10/11 With MipMapping 98 10.7 Texturing T t Texture iinterpolation t l ti Wrong interpolation in screen space p0 viewpoint Line with parameters p0,p1 at vertices 0.5 • (p0 + p1) view plane p1 1 [3] Thomas Ertl Graphical-Interactive Systems WS 10/11 99 10.7 Texturing Perspectively P ti l correctt interpolation i t l ti • Nonlinear z transformation can generate texture distortions • Linear interpolation preserves division ratios • Projection does not • Correct: hyperbolic (rational) interpolation Thomas Ertl Graphical-Interactive Systems WS 10/11 100 10.7 Texturing Di id iin 1, Divide 1 4 4, 64 64... parts t Thomas Ertl Graphical-Interactive Systems WS 10/11 101 10.7 Texturing P Perspective ti texture t t interpolation i t l ti Object-affine j spaces p Objectt Obj space World space p Screen-affine spaces div by 1/w T t Texture space Homogeneous texture space (s,t,1) (s/w,t/w,1/w) div by w Homogeneous Screen space screen space (x,y,z x y z,1) 1) (xw,yw,zw,w) • Linear interpolation off s/w, / t/w / and 1/w / • Divide per pixel: s/w by 1/w Thomas Ertl Graphical-Interactive Systems WS 10/11 102 10.7 Texturing Texture T t ffunctions ti How is the texture color applied to the pixel? • Replace: replace the pixel's color with the texture's color • Modulate: M d l t scale the pixel's color (illumination) • Blend: interpolate between the pixel's and the texture's color • Decal: replace the pixel's color with alpha blending Thomas Ertl Graphical-Interactive Systems WS 10/11 103 10.7 Texturing O OpenGL GL Pipeline Pi li Thomas Ertl Graphical-Interactive Systems WS 10/11 104 References [1] Foley, James; van Dam, Andries; Feiner, Steven K.; Hughes, John: Computer Graphics, Addison Wesley, 1997 [2] Watt, Alan: 3D-Computergraphik, Addison Wesley, 3. Auflage, 2002 [3] Hill, F.S. JR, Computer Graphics – Using OpenGL, 2nd Edition, Prentice Hall,, 1990 Thomas Ertl Graphical-Interactive Systems WS 10/11 105