OUTPUT PRIMITIVES CEng 477 Introduction to Computer Graphics METU, 007
Recap: The basic forward projection pipeline: MCS Model Model Modeling Transformations M M 3D World Scene Viewing Transformations V 3D View Scene Model M3 WCS VCS P Clip Normalize D/3D Device Scene Rasterization Projection NDCS D Image DCS SCS
Screen vs. World coordinate sstems Objects positions are specified in a Cartesian coordinate sstem called World Coordinate Sstem which can be three dimensional and real-valued. Locations on a video monitor are referenced in integer screen coordinates. Therefore object definitions has to be scan converted to discrete screen coordinate locations to be viewed on a video monitor.
Specification of a D WCS in OpenGL glmatrimode (GL_PROJECTION); glloadidentit (); gluorthod (min, ma, min, ma); Objects that are specified within these coordinate limits will be displaed within the OpenGL window.
Output Primitives Graphic SW and HW provide subroutines to describe a scene in terms of basic geometric structures called output primitives. Output primitives are combined to form comple structures Simplest primitives Point (piel) Line segment
Scan Conversion Converting output primitives into frame buffer updates. Choose which piels contain which intensit value. Constraints Straight lines should appear as a straight line primitives should start and end accuratel Primitives should have a consistent brightness along their length The should be drawn rapidl
OpenGL Point Functions glbegin (GL_POINTS); glvertei(50, 00); glvertei(75, 50); glvertei(00, 00); glend();
OpenGL Line Functions glbegin (GL_LINES); p3 glverteiv(p); glverteiv(p); glverteiv(p3); glverteiv(p4); glverteiv(p5); glend(); p p5 p4 p
OpenGL Line Functions glbegin (GL_LINE_STRIP); p3 glverteiv(p); glverteiv(p); glverteiv(p3); glverteiv(p4); glverteiv(p5); glend(); p p5 p4 p
OpenGL Line Functions glbegin (GL_LINE_LOOP); p3 glverteiv(p); glverteiv(p); glverteiv(p3); glverteiv(p4); glverteiv(p5); glend(); p p5 p4 p
Line Drawing Algorithms Simple approach: sample a line at discrete positions at one coordinate from start point to end point, calculate the other coordinate value from line equation (slope-intercept line equation). = m + b or b = m m end m = end start start Is this correct? If m>, If m, increment and find increment and find
Digital Differential Analzer Simple approach: too man floating point operations and repeated calculations Calculate from for a value m m m for, 0 m for, m m
DDA Is faster than directl implementing =m+b. No floating point multiplications. We have floating point additions onl at each step. But what about round-off errors? Can we get rid of floating point operations completel?
Bresenham's Line Algorithm DDA: Still floating point operations =m+b Assume m If alread at ( ( ( +, +,, ) if + ) if ), choices : d d lower lower d > d upper upper + d upper d lower = m( d lower + ) + b d d dupper = m( d upper lower = ( = + ) = m( + ) = + b + ) + b + m( + ) b + + +3 m = Δ Δ = end end start start define p = Δ( d lower c = Δ + Δ(b ) d upper ) = Δ Δ + c independent from piel position
choose else choose 0 if + < < upper lower p d d ) ( ) ( p p c p Δ Δ = + Δ Δ = + + + + + + at step +: ) ( p p Δ Δ + = + = + + + 0 if p was negative if p was positive to calculate p 0 at the starting piel position ( 0, 0 ) p c b b c c p Δ Δ = Δ Δ Δ + Δ = Δ Δ = + Δ Δ = + Δ Δ = ) ( 0 0 0 0 0 0 0 0
Bresenham s Line-Drawing Algorithm Input: two line end points ( 0, 0 ) and ( end draw ( 0, 0 ) p Δ-Δ; 0, end ) while < end + + if p 0 choose + ; p + p +Δ else choose + + +; p + p + Δ draw ( +, + ) -Δ + p p +
Eample from the tetboo Using Bresenham s algorithm digitize the line with endpoints (0,0) and (30,8)
Eample continued... 8 0 0 5 30
Plotted piels 8 0 0 5 30
Circle Generation Circles can be approimated b a set of straight lines. But, how man lines do we need for an acceptable representation? How do we determine end points of lines?
Circle Drawing in OpenGL Routines for drawing circles or ellipses are not included in the OpenGL core librar. GLU (OpenGL Utilit) librar has some routines for drawing spheres, clinders, B- splines. Rational B-splines can be used to displa circles and ellipses.
Circle Generation 0 0 unit steps in 0 r 0 r Computationall comple Non uniform spacing Polar coordinates: r cos c r sin c
Fied angular step size to have equall spaced points r cos r cos d r sin r sin d r cos cosd r sin sin d cosd sin d r sin cosd r cos sin d cosd sin d fied d so compute cosd and sin d initiall
Computation can be reduced b considering (-,) (,) smmetr of circles: Still too comple, multiplications, trigonometric calculations (-,) (-,-) (-,-) (,-) Bresenham's circle generation algorithm involves simple integer operations (comparing squares of piel separation distances) Midpoint Circle Algorithm avoids squaring and generates the same piels as Bresenhams s algorithm. (,) (,-)
Midpoint Circle Algorithm Consider the second octant. Increment, decide on select which of piels, ( +, ) or ( +, -) are closer to the circle b evaluating the circle function at the midpoint.
f (, ) p f, p f, p p p p = 0 0 if if 4 4 on the circle choose inside the circle choose 4 r = + r > 0 if outside the circle choose < r r r 4
p = p + ( + ) + ( + ) ( + ) + + where + is either or depending on the sign of p. if p < 0 p + = p + + 3 if p 0 p + = p + + 5 computing p 0 at ( 0, 0 ) = (0,r) p 0 = f (, r = + ( r = 5 4 r ) ) r if r is integer p 0 = r
Midpoint Circle Algorithm Input: radius r and circle center ( c, c ) draw(0+ c,r+ c ) (add c and c before plotting) p r; 0; r; while < if p < 0 choose + ; p + p + +3 else choose + ; p + p + + 5 + + draw ( + + c, + + c ) + ; +; p p +
if p < 0 choose + ; p + p + +3 else choose + ; p + p + + 5 0 ; 0 ; r 0 plot (0,0) p 0 9 choose E plot,0 p 9 3 4 choose E plot,0 p 4 4 3 3 choose SE plot 3,9 p 3 6 8 5 4 choose E plot 4,9 p 4 8 3 7 choose SE plot 5,8 p 7 0 6 5 6 choose SE plot 6,7 p 6 4 5 9 choose SE plot 7,6 - - - -
Ellipse Generation Similar to circle generation with mid-point. Inside test. Different formula for points up to the tangent =-, slope<. (0,b) to tangent: increment find tangent to (a,0): decrement find Mid-point algorithm is applicable to other polnomial equations: Parabola, Hperbola b =- a a b