CS 48/68 INTERACTIVE COMPUTER GRAPHICS Picking and Crves Week 6 David Breen Department of Compter Science Drexel University Based on material from Ed Angel, University of New Mexico
Objectives Picking Select objects from the display Introdce types of crves and srfaces Explicit Implicit Parametric Strengths and weaknesses Discss Modeling and Approximations Conditions Stability 2
Picking Identify a ser-defined object on the display In principle, it shold be simple becase the mose gives the position and we shold be able to determine to which object(s) a position corresponds Practical difficlties Pipeline architectre is feed forward, hard to go from screen back to world Complicated by screen being 2D, world is D How close do we have to come to object to say we selected it?
Three Approaches Hit list Most general approach bt most difficlt to implement Use back or some other bffer to store object ids as the objects are rendered Rectanglar maps Easy to implement for many applications Divide screen into rectanglar regions 4
Using another bffer and colors for picking For a small nmber of objects, we can assign a niqe color (often in color index mode) to each object We then render the scene to a color bffer other than the front bffer so the reslts of the rendering are not visible We then get the mose position and se glreadpixels() to read the color in the bffer we jst wrote at the position of the mose The retrned color gives the id of the object 5
Using Regions of the Screen Many applications se a simple rectanglar arrangement of the screen Example: paint/cad program tools drawing area mens Easier to look at mose position and determine which area of screen it is in that sing selection mode picking 6
Rendering Modes OpenGL can render in one of three modes selected by glrendermode(mode) GL_RENDER: normal rendering to the frame bffer (defalt) GL_FEEDBACK: provides list of primitives rendered bt no otpt to the frame bffer GL_SELECTION: Each primitive in the view volme generates a hit record that is placed in a name stack which can be examined later 7
Hit Record 8
Using Selection Mode Initialize name bffer Enter selection mode (sing mose) Render scene with ser-defined names (id#) Every object in view volme generates a hit Name stack processing always done Reenter normal render mode This operation retrns nmber of hits Examine contents of name bffer (hit records) Hit records inclde nmber of ids, depth information and ids on stack at moment of rendering 9
Selection Mode Fnctions glselectbffer(): specifies name bffer glinitnames(): initializes name bffer glpshname(id): psh id on name bffer glpopname(): pop top of name bffer glloadname(id): replace top name on bffer id is set by application to identify objects Can t be called inside glbegin/glend
Selection Mode and Picking In general, selection mode won t work for picking becase every primitive in the view volme will generate a hit Change the viewing parameters so that only those primitives near the crsor are in the altered view volme Use glpickmatrix
glpickmatrix() glpickmatrix(gldoble x, Gldoble y, Gldoble w, Gldoble h, Glint *vp) k Creates a projection matrix for picking that restricts drawing to a w x h area centered at (x,y) in the window coordinates within the viewport vp 2
Go to pick.c
Introdction to Crves
Escaping Flatland Until now we have worked with flat entities sch as lines and flat polygons Fit well with graphics hardware Mathematically simple Bt the world is not composed of flat entities Need crves and crved srfaces May only have need at the application level Implementation can render them approximately with flat primitives 5
Modeling with Crves data points approximating crve interpolating data point 6
What Makes a Good Representation? There are many ways to represent crves and srfaces Want a representation that is Stable Smooth Easy to evalate Mst we interpolate or can we jst come close to data? Do we need derivatives? 7
Explicit Representation Most familiar form of crve in 2D y=f(x) Cannot represent all crves Vertical lines Circles Extension to D y=f(x), z=g(x) The form z = f(x,y) defines a srface y z x y 8 x
Implicit Representation Two dimensional crve(s) g(x,y)= Mch more robst All lines ax+by+c= Circles x 2 +y 2 -r 2 = Three dimensions g(x,y,z)= defines a srface Intersect two srface to get a crve In general, we cannot solve for points that satisfy the eqation 9
Parametric Crves Separate eqation for each spatial variable x=x() y=y() z=z() For max min we trace ot a crve in two or three dimensions p( min ) p() p()=[x(), y(), z()] T p( max ) 2
Selecting Fnctions Usally we can select good fnctions not niqe for a given spatial crve Approximate or interpolate known data Want fnctions which are easy to evalate Want fnctions which are easy to differentiate Comptation of normals Connecting pieces (segments) Want fnctions which are smooth 2
Parametric Lines We can normalize to be over the interval (,) Line connecting two points p and p p()= p p()=(-)p +p Ray from p in the direction d p()=p +d p() = p p() = p d p()= p +d 22
Crve Segments After normalizing, each crve is written p()=[x(), y(), z()] T, In classical nmerical methods, we design a single global crve In compter graphics and CAD, it is better to design small connected crve segments p() join point p() = q() p() q() q() 2
Parametric Polynomial Crves N i j x( ) = cxi y( ) = c yj z() = c zk i= M j= If N=M=K, we need to determine (N+) coefficients Eqivalently we need (N+) independent conditions K " k k= Noting that the crves for x, y and z are independent, we can define each independently in an identical manner We will se the form where p can be any of x, y, z p() = K " c k k k= 24
Why Polynomials Easy to evalate Continos and differentiable everywhere Mst worry abot continity at join points inclding continity of derivatives p() q() join point p() = q() bt p () q () 25
Cbic Parametric Polynomials N=M=K=, gives balance between ease of evalation and flexibility in design p( ) = k = c k For coefficients to determine for each of x, y and z Seek for independent conditions for varios vales of reslting in 4 eqations in 4 nknowns for each of x, y and z Conditions are a mixtre of continity reqirements at the join points and conditions for fitting the data k 26
Designing Parametric Cbic Crves
Objectives Introdce the types of crves Interpolating Hermite Bezier B-spline Analyze their performance 28
29 Matrix-Vector Form c k k k = = ) p( " # % & = c c c c 2 c " # % & = 2 define c c T T = = ) p( then
Interpolating Crve p p p p 2 Given for data (control) points p, p,p 2, p determine cbic p() which passes throgh them Mst find c,c,c 2, c
Interpolating Mltiple Segments se p = [p p p 2 p ] T se p = [p p 4 p 5 p 6 ] T Get continity at join points bt not continity of derivatives
2 Interpolation Eqations apply the interpolating conditions at =, /, 2/, p =p()=c p =p(/)=c +(/)c +(/) 2 c 2 +(/) c p 2 =p(2/)=c +(2/)c +(2/) 2 c 2 +(2/) c p =p()=c +c +c 2 +c or in matrix form with p = [p p p 2 p ] T p=ac " # % & ' ( ) * +, ' ( ) * +, ' ( ) * +, ' ( ) * +, ' ( ) * +, ' ( ) * +, = 2 2 2 2 2 A
Interpolation Matrix Solving for c we find the interpolation matrix MI = ' A = & ' %' 5.5 9 4.5 9 ' 22.5.5 ' 4.5 8 '.5 # ' 4.5 4.5 " c=m I p Note that M I does not depend on inpt data and can be sed for each segment in x, y, and z
Blending Fnctions Rewriting the eqation for p() p()= T c= T M I p = b() T p where b() = [b () b () b 2 () b ()] T is an array of blending polynomials sch that p() = b ()p + b ()p + b 2 ()p 2 + b ()p b () = -4.5(-/)(-2/)(-) b () =.5 (-2/)(-) b 2 () = -.5 (-/)(-) b () = 4.5 (-/)(-2/) 4
Blending Fnctions These fnctions are not monotonic Hence the interpolation polynomial will wiggle 5
Other Types of Crves and Srfaces How can we get arond the limitations of the interpolating form Lack of smoothness Discontinos derivatives at join points We have for conditions (for cbics) that we can apply to each segment Use them other than for interpolation Need only come close to the data 6
Hermite Form p () p () p() p() Use two interpolating conditions and two derivative (tangent) conditions per segment Ensres continity and first derivative continity between segments 7
Eqations Interpolating conditions are the same at ends p() = p = c p() = p = c +c +c 2 +c Differentiating we find p () = c +2c 2 + 2 c Evalating at end points p () = p = c p () = p = c +2c 2 +c 8
9 Matrix Form c q " # % & = " # % & = 2 p' p' p p Solving, we find c=m H q where M H is the Hermite matrix " # % & ' ' ' ' = 2 2 2 MH
4 Blending Polynomials p() = b() T q " # % & ' + ' + ' + ' = 2 2 2 2 2 2 2 ) b( Althogh these fnctions are smooth, the Hermite form is not sed directly in Compter Graphics and CAD becase we sally have control points bt not derivatives However, the Hermite form is the basis of the Bezier form
Parametric and Geometric Continity We can reqire the derivatives of x, y,and z to each be continos at join points (parametric continity) Alternately, we can only reqire that the tangents of the reslting crve be continos (geometry continity) The latter gives more flexibility as we have need satisfy only two conditions rather than three at each join point 4
Example Here the p and q have the same tangents at the ends of the segment bt different derivatives Generate different Hermite crves This techniqes is sed in drawing applications 42
Bezier and Spline Crves
Objectives Introdce the Bezier crves Derive the reqired matrices Introdce the B-spline and compare it to the standard cbic Bezier 44
Bezier s Idea In graphics and CAD, we do not sally have derivative data Bezier sggested sing the same 4 data points as with the cbic interpolating crve to approximate the derivatives in the Hermite form 45
Approximating Derivatives p located at =/ p p 2 p 2 located at =2/ p p p # p '() " p'() " 2 / / p slope p () slope p () p p 46
Eqations Interpolating conditions are the same p() = p = c p() = p = c +c +c 2 +c Approximating derivative conditions p () = (p - p ) = c p () = (p - p 2 ) = c +2c 2 +c Solve for linear eqations for c=m B p 47
48 Bezier Matrix " # % & ' ' ' ' = 6 MB p() = T M B p = b() T p blending fnctions
Blending Fnctions b( ) = & ( ' ) # 2 ( ' ) 2 2 ( ' ) % " Note that all zeros are at and which forces the fnctions to be smooth over (,) 49
Bernstein Polynomials The blending fnctions are a special case of the Bernstein polynomials b kd ( ) = d k( d k) ( ) d k These polynomials give the blending polynomials for any degree Bezier form All zeros at and For any degree they all sm to They are all between and inside (,) k 5
Convex Hll Property The properties of the Bernstein polynomials ensre that all Bezier crves lie in the convex hll of their control points Hence, even thogh we do not interpolate all the data, we cannot be too far away Bezier crve p p 2 convex hll p p 5
Analysis Althogh the Bezier form is mch better than the interpolating form, we have the derivatives are not continos at join points Can we do better? Go to higher order Bezier More work Derivative continity still only approximate Spported by OpenGL Apply different conditions Tricky withot letting order increase 52
B-Splines Basis splines: se the data at p=[p i-2 p i- p i p i- ] T to define crve only between p i- and p i Allows s to apply more continity conditions to each segment For cbics, we can have continity of fnction, first and second derivatives at join points Cost is times as mch work for crves Add one new point each time rather than three For srfaces, we do 9 times as mch work 5
54 Cbic B-spline " # % & ' ' ' ' = 6 4 MS p() = T M S p = b() T p
55 Blending Fnctions " # % & ' + + + ' ' = 2 2 2 6 4 ) ( 6 ) b( convex hll property
Splines and Basis If we examine the cbic B-spline from the perspective of each control (data) point, each interior point contribtes (throgh the blending fnctions) to for segments We can rewrite p() in terms of the data points as p( ) = Bi ( ) defining the basis fnctions {B i ()} p i 56
57 Basis Fnctions 2 2 2 2 ) ( ) ( ) ( 2) ( ) ( 2 + + < " + + < " < " # # < " # # < % & ' # + + = i i i i i i i i i i b b b b Bi In terms of the blending polynomials
Generalizing Splines We can extend to splines of any degree Data and conditions to not have to given at eqally spaced vales (the knots) Nonniform and niform splines Can have repeated knots Can force spline to interpolate points Cox-deBoor recrsion gives method of evalation 58
NURBS Nonniform Rational B-Spline crves and srfaces add a forth variable w to x,y,z Can interpret it as weight to give more importance to some control data Can also interpret as moving to homogeneos coordinate Reqires a perspective division NURBS act correctly for perspective viewing Qadrics are a special case of NURBS 59
Every Crve is a Bezier Crve We can render a given polynomial sing the recrsive method if we find control points for its representation as a Bezier crve Sppose that p() is given as an interpolating crve with control points q p()= T M I q There exist Bezier control points p sch that p()= T M B p Eqating and solving, we find p=m B - M I 6
6 Matrices Interpolating to Bezier B-Spline to Bezier " # % & ' ' ' ' = ' 6 5 2 2 6 5 M M I B " # % & = ' 4 4 2 2 4 4 M M S B
Example These three crves were all generated from the same original data sing Bezier recrsion by converting all control point data to Bezier control points Bezier Interpolating B Spline 62
Drawing Bezier Crves in OpenGL
Basic Procedre Enable an evalator (glenable) For vertices, normals and colors Define Bezier parameters (glmapf) Evalate Bezier crve (glevalcoordf, or glmapgridf and glevalmesh) 64
Example GLfloat data[4][]; glenable(gl_map_vertex); glmapf(gl_map_vertex,.,.,, 4, data); glbegin(gl_line_strip); for (i = ; i <= 2; i++) glevalcoordlf(i / 2.); glend(); 65
D Evalator: glmapf() glmap{fd}(glenm entity, TYPE, TYPE, GLint stride, GLint order, TYPE* data); entity describes type entity GL_MAP_VERTEX_, GL_MAP_VERTEX_4, GL_MAP_COLOR_4, GL_MAP_NORMAL and is parameter range stride is nmber of variables between data points order of Bernstein polynomial ( more than degree) 66
Polynomial Order Has a Limit Each OpenGl implementation has a maximm Bernstein polynomial order glgetintergerv(gl_max_eval_order, &max_order); Gets maximm polynomial order e.g. 8,, 5, 67
Defining partitions: glmapgridf() glmapgridf(glint n, TYPE, TYPE ); Defines n eqally spaced partitions between parameters and Prodces n+ samples 68
Evalate Bezier at samples: glevalmesh() glevalmesh(glenm mode, GLint first, GLint last); Renders in mode (GL_LINE, GL_POINT) all enabled evalators from the first to last vales of defined by glmapgrid() 69
Example GLfloat data[4][]; glenable(gl_map_vertex); glmapf(gl_map_vertex,,,, 4, data); glmapgridf(2,, ); glevalmesh(gl_line,, 2); 7
Go to bezier.c