Computer Graphics Modelling in D Lecture School of EECS Queen Mar, Universit of London D primitives Digital line algorithms Digital circle algorithms Polgon filling CG - p.hao@qmul.ac.uk D primitives Line segments Ellipses and circles Polgons Curves Lines and Pollines Lines drawn between ordered points to create more comple forms called pollines. Same first and last point make closed polline or polgon. If it does not intersect itself, called simple polgon. Conve Polgons: For ever pair of points in the polgon, the line between them is full contained in the polgon. Concave Polgons: Not conve: some two points in the polgon are joined b a line not full contained in the polgon. Polline Polgon Conve polgon Concave polgon CG - p.hao@qmul.ac.uk CG - p.hao@qmul.ac.uk 4 Special polgons OpenGL polgon primitives v 4 v v 5 v v 4 v 4 v v 5 v 5 v6 v v 6 v v v 6 v v 8 v 7 v v 8 v 7 v v 8 v 7 Triangles Quads Polgon triangle square rectangle v v v 5 v v 4 v 5 v v 4 v 6 v 8 v v v 4 v 6 Triangle Strip v Triangle Fan v v v 5 Quad Strip v 7 CG - p.hao@qmul.ac.uk 5 CG - p.hao@qmul.ac.uk 6
Scan Converting Lines Mathematics of Lines Rasterization (scan-conversion) : turn D primitives into sets of piels Equation of a (D) line: a + b + c = 0 Direction: ( b, a) Normal vector: (a, b) ideal line Parametric equation of a segment [P0-P] : (t) = 0 + t*(-0) = (-t)*0 + t* (t) = 0 + t*(-0) = (-t)*0 + t*, where t in [0..] O For a line from (0, 0) to (, ), the line equation is = m + c where m is the slope of the line m = ( 0) / ( 0) c = 0 m*0 Note that we can restrict out attention to lines of slope between - and. CG - p.hao@qmul.ac.uk 7 CG - p.hao@qmul.ac.uk 8 Naive algorithm Clever approaches faster. start with the smallest of (0,0). compute corresponding value of. SetPiel(, round()) 4. increment and loop until reaching ma(,) Cost: float multiplication + float addition + round per loop O Use integer calculations Avoid divides and multiplies Use incremental computations Use spatial coherence CG - p.hao@qmul.ac.uk 9 CG - p.hao@qmul.ac.uk 0 Basic Incremental Algorithm Digital differential analzer (DDA) algorithm: void Line(int 0, int 0, int, int ) { int ; float d, d,, m; d = - 0; /* Floating */ d = - 0; /* Floating */ m = d/d; /* Floating division */ = 0; /* Floating */ for ( = 0; <=,++){ SetPiel(, round()); += m; /* Increment */ Cost: float add + round per loop CG - p.hao@qmul.ac.uk Midpoint Line Algorithm Line equation: ( 0)( 0) = ( 0)( 0) or F(,) = 0 F(,) = ( 0)( 0) ( 0)( 0) = ( 0) ( 0) ( 0)0+( 0)0 F(+, ) F(,) = ( 0) F(+, +) F(,) = ( 0) ( 0) F(+, +/) F(,) = ( 0) ( 0)/ d = ( 0) ( 0) If point P(,) drawn, the net point is either P(+,) or P(+,+) To decide which point, use the relative position of the midpoint M = (+, +/) with respect to the line The distance d can be computed incrementall with onl one or two integer adds per loop! CG - p.hao@qmul.ac.uk
Midpoint Line Algorithm Scan Converting Circles Bresenham's algorithm: void MidpointLine(int 0, int 0, int, int ) { int d,d,incre,incrne,d,,; d=-0; d=-0; d=*d-d; /* initial value of d */ incre=*d; /* increment for move to E */ incrne=*d-*d; /* increment for move to NE */ =0; =0; DrawPiel(,) /* draw the first piel */ while (<){ if (d<=0){ /* choose E */ d+=incre; ++; /* move E */ F(, )<0 else { /* choose NE */ NE d+=incrne; ++; ++; /* move NE */ SetPiel (,); (i, i) E Cost: integer add per piel F(, )>0 CG - p.hao@qmul.ac.uk F(, )=0 (i+, i+/) For a circle + = R, we could use the following algorithm: for (= R; <=R; ++) { SetPiel (, round(sqrt(r* R * )); SetPiel (, round(sqrt(r* R * )); However, this simple method is inefficient And the circle has large gaps for the value of close to R The large gaps can be avoided b using the parametric equations of the circle: = R cos(a), = R sin(a) However the inefficienc problems remain. CG - p.hao@qmul.ac.uk 4 R Eight-Wa Smmetr we can draw a circle smmetricall b eight-sections if we draw a piel (, ) then we can also draw (, ), (, ), (, ), (, ), (, ), (, ), (, ). Therefore, we onl need consider 45 O of a circle, from =0 and = to ==R/sqrt(). Midpoint Circle Algorithm Circle equation: + = R or F(,) = 0 F(,)= + R = 0 F(+, /) F(,) = + 5/4 d = + 5/4; d(0,r) = 5/4 R E move: F(+, /) F(+, /) = + d new = d old + + SE move: F(+, /) F(+, /) = ( ) + 5 d new = d old + ( ) + 5 CG - p.hao@qmul.ac.uk 5 CG - p.hao@qmul.ac.uk 6 Midpoint Circle Algorithm void MidpointCircle (int radius) { /* Assume integer radius. */ int,,d = 0; = radius; d = - radius; /* Initialization */ SetPiel(,); while (>) { if (d < 0) { /* Choose E */ d += * + ; ++; else { /* Choose SE */ d += *(-)+5; ++; --; SetPiel(,); all integer computations, shift and or adds per piel Filling Shapes Filling Shapes: Turn on all the piels on a raster displa that are INSIDE a mathematical shape. Questions before filling: Is the shape closed with a boundar? Which piel is inside and which is outside? What color/pattern should the shape be filled with? CG - p.hao@qmul.ac.uk 7 CG - p.hao@qmul.ac.uk 8
ODL DCS ODL DCS Filling Rectangles Filling Polgons If the rectangle is aligned with the and ais, then we can easil determine which piels lie inside the rectangle. for = min to ma for = min to ma SetPiel (, ) Use scan lines Edge coherence Finding intersections 4 0 8 6 4 0 0 4 6 8 0 4 CG - p.hao@qmul.ac.uk 9 CG - p.hao@qmul.ac.uk 0 Geometric Modelling Geometric Modelling Hierarch in Geometric Models Modelling transformations Surface modelling Solid Modelling Point-based Surface-based Constructive CG - p.hao@qmul.ac.uk CG - p.hao@qmul.ac.uk Hierarch in Geometric Models Comple objects can be built using applicationspecific atomic components. Smbolized b various tree structures or DAG (Directed Acclic Graph). QM Tree upperbod leg leg arm arm foot foot hand hand DAG upperbod leg arm hand foot Binar Tree upperbod arm leg hand arm foot leg hand foot Hierarch in Geometric Models If a child-sibling tree (binar tree) is emploed, preorder traversal can be used for rendering. Parent calls child and passes geometric parameters to child. QM Tree upperbod leg leg arm arm foot foot hand hand DAG Binar Tree upperbod upperbod leg arm leg arm foot hand arm foot leg hand hand foot CG - p.hao@qmul.ac.uk CG - p.hao@qmul.ac.uk 4 4
Modelling transformations Surface modelling Boundar Representations CG - p.hao@qmul.ac.uk 5 CG - p.hao@qmul.ac.uk 6 Representing Polgon Meshes Representing Polgon Meshes A polgon mesh is a collection of polgons or faces, which are together connected to form the skin of the object. Faces Edges, the boundar between faces Vertices, the boundaries between edges, or where three or more faces meet Normals, teture coordinates, colors, shading coefficients, etc Polgon Mesh Verte list locations of the vertices, geometric information Edge list indees into end vertices of edges, topological information Face list indees into vertices and normal lists, topological information Normal list directions of the normal vectors, orientation information. CG - p.hao@qmul.ac.uk 7 CG - p.hao@qmul.ac.uk 8 Representing Polgon Meshes Euler s Formula: V E + F = verte list and face list are enough. Polgon Mesh Eample A cube: Coordinates of Vertices (D,,, and z for each verte): -, # Verte 0 - -, # Verte - - -, # Verte - -, # Verte, # Verte 4 -, # Verte 5 - -, # Verte 6 -, # Verte 7 Lists of 6 Faces (vertices are referenced to the vertices above, - marks the end of the verte list of a face) :,, 0,, -, # Front face=verte,,0, 0,, 5, 4, -, # Right side 7, 6,,, -, # Left side 4, 5, 6, 7, -, # Back 0, 4, 7,, -, # Top,, 6, 5, - # Bottom CG - p.hao@qmul.ac.uk 9 CG - p.hao@qmul.ac.uk 0 5
Polgon Mesh Eample Plane Equations A House : Coordinates of Vertices : 0, # Verte 0 0 -, # Verte - 0 -, # Verte - 0, # Verte, # Verte 4 -, # Verte 5 - -, # Verte 6 -, # Verte 7 0 4 0 # Verte 8 List of Faces : 0,,,, -, 0,, 5, 4, -,,, 6, 5, -,,, 7, 6, -,, 0, 4, 7, -, 4, 5, 8, -, 5, 6, 8, -, 6, 7, 8, -, 7, 4, 8, - The equation of a plane is A + B + Cz + D = 0 Its normal vector is (A, B, C). Three points determine a plane: A B Cz D 0 A B Cz D 0 A B Cz D 0 A B Cz D 0 The plane equation: z A z B 0 z C z D z z 0 z z z z z z z + z z z z 0 z CG - p.hao@qmul.ac.uk CG - p.hao@qmul.ac.uk Relative location with a plane A Nonsense Object A B Cz D 0 0 0 outside on inside Perpendicular distance from a point (,, z) to a plane : d A B Cz D A B C CG - p.hao@qmul.ac.uk CG - p.hao@qmul.ac.uk 4 What do we mean b a solid object? Three-Levels of Modeling finite three dimensional rigid closed finitel describable with a determinable boundar World Phsical objects Solid Mathematical objects theor Point-set topolog Algebraic topolog Bod of mathematics Representation realization B-rep CSG Decomposition CG - p.hao@qmul.ac.uk 5 CG - p.hao@qmul.ac.uk 6 6
Regularized Boolean Set Operations Primitive instancing CG - p.hao@qmul.ac.uk 7 CG - p.hao@qmul.ac.uk 8 Sweep Representations Boundar Representations Z Z sweep sweep X Y Y X CG - p.hao@qmul.ac.uk 9 CG - p.hao@qmul.ac.uk 40 Spatial-Partitioning Representations Constructive Solid Geometr (CSG) Cell decomposition Spatial occupanc enumeration Octrees CSG Tree Binar Space Partition A B D B out C C D A in out in out CG - p.hao@qmul.ac.uk 4 CG - p.hao@qmul.ac.uk 4 7
Object Modelling Summar Voels Polgon Mesh Octree CSG Tree B-rep CG - p.hao@qmul.ac.uk 4 8