2IV60 Computer graphics set 7: Basic Geometric Modeling Jack van Wijk TU/e Geometric modeling 1 The world is full of all kind of objects: Trees, people, cars, housed, clouds, rocks, waves, pencil sharpeners, fire, mountains, plants, … How can we describe these, such that they are - easy to enter; - easy to process; - easy to display? Complex problem, HUGE topic! Geometric modeling 2 Input: • by user - preferably on a high level, customized to application • by scanning objects - laser scanners: points - medical scanners: voxels Geometric modeling 3 User Instructions, specifications conversion triangles Scanner 3D points, voxels conversion graphics pipeline image Smooth Curves (2D) Two standard methods: • Parametric: X(t) = (x(t), y(t)) • Implicit: f(x, y) = 0 H&B A-8,9:772-774 Parametric description line 1 Given point P and vector V: X(t) = P + Vt y Given two points P en Q: X(t) = P + (Q P)t , or = P(1t) + Qt Segment: tmin t tmax t Q P V x H&B A-8,9:772-774 Parametric description curve • X(t) = (x(t), y(t)) • Drawing a curve in a simple way, approximate with a polyline: MoveTo(X(0)); for i := 1 to N do LineTo(X(iDt)); H&B A-8,9:772-774 Drawing a curve with OpenGL 1 X(float t):Point; { P[0]= …; P[1]=…; P[2]=…; return P; }; DrawX(); { N = 12; // use symbolic name for the number of segments int i; float dt := (tmax – tmin)/N; glBegin(GL_LINE_STRIP); // GL_LINE_LOOP if closed curve for (i = 0; i <= N; i++) // bounds here: 0 - N glVertex3fv(X(tmin + i*dt)); glEnd(); } H&B 4-5:82-83 Drawing a curve with OpenGL 2 • Using too few points: – jagged appearance; • Using too many points: – slow, – fat appearance (many segments per pixel). H&B 4-5:82-83 Drawing a curve with OpenGL 3 • Decide on #points: – characteristics shape; – size of shape on screen. – beforehand or adjust automatically. H&B 4-5:82-83 Implicit description line • (X P).N = 0 with N.V = 0 (N: normal vector) • Also: ax+by+c=0 y N V P x H&B A-8,9:772-774 Implicit description curve f=1 f=0 f >0 f =-1 f =-2 f <0 H&B A-8,9:772-774 Circle Parametric : ( x, y ) (r cos 2t , r sin 2t ), 0 t 1 y r x Implicit (or nonparametric) : x y r 0 2 2 2 H&B A-8,9:772-774 Curves (3D) Two standard methods: • Parametric: X(t) = (x(t), y(t), z(t)) • Implicit: f(x, y, z) = 0 and g(x, y, z) = 0 Intersection of two surfaces H&B A-8,9:772-774 Circle in 3D Parametric : ( x, y, z ) (r cos 2t , r sin 2t ,0), z y 0 t 1 r Implicit : x x y r 0 and z0 2 2 2 H&B A-8,9:772-774 Surfaces • Polyhedra • Parametric surfaces • Implicit surfaces H&B A-8,9:772-774 Polyhedra • Set of polygons that describe the surface of an object • Often only triangles (hardware!) • Many variations for storage (zie H&B 4-7) • Often additional information per vertex (color, normal, texture, …) H&B 13-1:418 Curved surfaces • Parametric: X(u,v) = (x(u,v), y(u,v), z(u,v)) • Implicit: f(x, y, z) = 0 H&B 13-3:421-422 Sphere 1 z Parametric : y x(u , v) r cos(2u ) sin( v) y (u , v) r sin( 2u ) sin( v) z (u , v) r cos(v) 0 u, v 1 x u constant, v varies v constant, u varies H&B 13-4:422-424 Sphere 2 z Display : y Make N N facets with vertices : X(ui ,v j ), X(ui 1,v j ), X(ui ,v j 1 ), X(ui 1,v j 1 ), x with ui i / N , v j j / N , u constant, v varies v constant, u varies 0 i, j N 1 v u H&B A-8,9:807-808 Sphere 3 z Implicit descriptio n : y f ( x, y , z ) x 2 y 2 z 2 r 2 0 Test if punt ( x, y, z ) is inside object : x f ( x, y, z ) 0 : Yes f ( x, y, z ) 0 : No Again, f divides space in two parts. H&B 13-4:422-424 Partial Derivatives 1 Standard derivative for function of one variable : df ( x) f ( x Dx) f ( x) f ' ( x) lim Dx 0 dx Dx Partial derivative s for function of two variables : f ( x, y ) f ( x Dx, y ) f ( x, y ) f x ( x, y ) lim Dx 0 x Dx f ( x, y ) f ( x, y Dy ) f ( x, y ) f y ( x, y ) lim Dy 0 y Dy H&B A10:774 Partial Derivatives 2 Example : f ( x, y ) 2 x xy 3 y For the partial derivative to x, consider y as constant, and apply standard differenti ation to x : f ( x, y ) 4x y x For the partial derivative to y, consider x as constant, and apply standard differenti ation to y : f ( x, y ) x 9 y2 x H&B A10:774 2 3 Normal on surface 1 Given : Parametric curve P(u ) ( x(u ), y (u ), z (u )) u Tangent vector : dx(u ) dy(u ) dz(u ) T(u ) , , du du du T(u) P(u) Note :if u is time, then T(u ) is a velocity vector. H&B A-10:774-775 Normal on surface 2 Given : Parametric surface.P (u , v) ( x(u , v), y (u , v), z (u , v)) Tangent vector in u - direction : x(u , v) y (u , v) z (u , v) Tu (u , v) , , u u u Tv(u,v) u Tu(u,v) P(u,v) Tangent vector in v - direction : x(u , v) y (u , v) z (u , v) Tv (u , v) , , v v v v H&B A-10:774-775 Normal on surface 3 Tangent vector in u - direction : x(u , v) y (u , v) z (u , v) Tu (u , v) , , N u u u Tangent vector in v - direction : Tv(u,v) u Tu(u,v) x(u , v) y (u , v) z (u , v) Tv (u , v) , , v v v P(u,v) Tu (u , v) and Tv (u , v) are in tangent plane of surface. Normal to surface : N(u , v) Tu (u , v) Tv (u , v) v H&B A-10:774-775 Normal on surface 4 Example cylinder w ith radius r and height h, with 0 u , v 1 : z x(u , v) r cos(2u ) y (u , v) r sin( 2u ) z (u , v) hv Tv Tu (u , v) x(u , v) / u , y (u , v) / u , z (u , v) / u 2r sin( 2u ), 2r cos(2u ), 0 Tv (u , v) x(u , v) / v, y (u , v) / v, z (u , v) / v 0, 0, h N(u , v) Tu (u , v) Tv (u , v) 2rh cos(2u ), 2r sin( 2u ), 0 2rhcos(2u ), sin( 2u ), 0 v N Tu x y u H&B A-10:774-775 Normal on surface 5 Example sphere : x(u , v) r cos(2u ) sin( v) y (u , v) r sin( 2u ) sin( v) z (u , v) r cos(v) Tu (u , v) 2r sin( 2u ) sin( v), 2r cos(2u ) sin( v), 0 Tv (u , v) r cos(2u ) cos(v), r sin( 2u ) cos(v), r sin( v) 2 2 r 2 cos(2u ) sin 2 (v) 2 2 2 Tu (u , v) Tv (u , v) 2 r sin( 2u ) sin (v) 2 2 2 2 2 2 2 r sin ( 2 u ) sin( v ) cos( v ) 2 r cos ( 2 u ) sin( v ) cos( v ) x(u , v) 2 2 r sin( v) y (u , v) z (u , v) H&B A-10:774-775 Normal on surface 6 Given : Implicit surface f ( x, y, z ) 0 Normal vector : N ( x , y , z ) f ( x , y , z ) ( is grad) f f f , , x y z direction of fastest increase f N(x,y,z) (x,y,z) f(x,y,z)=0 H&B A-10:774-775 Normal on surface 7 f >0 f=0 f <0 H&B A-10:774-775 Normal on surface 8 Example cylinder, radius r and height h, with 0 u , v 1 : z f ( x, y , z ) x 2 y 2 r 2 0 N x f f f N( x, y, z ) , , x y z (2 x,2 y,0) y H&B A-10:774-775 Quadratic surfaces 1 Quadratic surfaces: Second degree equation f ( x, y, z ) 0 z ax2 by 2 cz 2 dxy exz fyz gx hy iz j 0 Example : cylinder : f ( x, y, z ) x y r 0 2 2 2 x y sphere : f ( x, y, z ) x 2 y 2 z 2 r 2 0 cone : f ( x, y , z ) x 2 y 2 z 2 0 Can also easily be described parametrically H&B 13-4:422-424 Quadratic surfaces 2 Ellipsoid : scaled sphere (mentos, cigar) sphere : f ( x, y , z ) x 2 y 2 z 2 r 2 0 ellipsoid : x y z f ( x, y , z ) 1 0 a b c 2 2 2 z c x a b y H&B 13-4:422-424 Quadratic surfaces 3 Torus : donut, car tyre (degree 4 surface) In x 0 plane : f ( x, y, z ) ( y ra ) 2 z 2 r 2 0 f ( x, y, z ) ( x 2 y 2 ra ) 2 z 2 r 2 0 Generic : z as y (x, y, z) r ra (0, y, z) y as y as x as H&B 13-4:422-424 Drawing surfaces with OpenGL 1 • Quadratic surfaces: GLU and GLUT offer some basic functions: – – – – glutSolidSphere(r, nLon, nLat); glutSolidCone(rbase, rheight, nLon, nLat); glutSolidTeapot(size); gluSphere, gluCylinder, • Teapot? – Google: Utah teapot; – Google: Stanford bunny. H&B 13-6:425-431 Drawing surfaces with OpenGL 1 • Quadratic surfaces: GLU and GLUT offer some basic functions: – – – – glutSolidSphere(r, nLon, nLat); glutSolidCone(rbase, rheight, nLon, nLat); glutSolidTeapot(size); gluSphere, gluCylinder, • Alternative: Define your own, custom versions, based on standard way to render parametric surfaces. H&B 13-6:425-431 Drawing surfaces with OpenGL 2 X(float u, v):Point; { P[0]= …; P[1]=…; P[2]=…; return P; }; DrawX(); { NU, NV = 12; // use symbolic names int i, j; float du := (umax – umin)/NU; float dv := (vmax – vmin)/NV; for numbers of points again for (j = 0; j < NV; j++) { glBegin(GL_QUAD_STRIP); for (i = 0; i <= NU; j++) { glVertex3fv(X(umin + i*du, vmin + j*dv)); glVertex3fv(X(umin + i*du, vmin + (j+1)*dv)); } glEnd(); } Drawing surfaces with OpenGL 3 Many variations possible – using triangle strips; – inserting normals, shading info, texture, ... Selecting the number of facets: – too few: jagged appearance; – too many: slow; – decide beforehand or automatically, based on size on screen and curvature surface. H&B 4-5:82-83 Next… • We now know how to define basic objects. • Stay tuned for splines: arbitrary curves and curved surfaces. • But first: let’s consider illumination and shading.