# UNIT- I 2D PRIMITIVES. Line and Curve Drawing Algorithms.

• Published on
25-Dec-2015

• View
223

• Download
1

Embed Size (px)

Transcript

<ul><li> Slide 1 </li> <li> UNIT- I 2D PRIMITIVES </li> <li> Slide 2 </li> <li> Line and Curve Drawing Algorithms </li> <li> Slide 3 </li> <li> Line Drawing y = m. x + b m = (y end y 0 ) / (x end x 0 ) b = y 0 m. x 0 x0x0 y0y0 x end y end </li> <li> Slide 4 </li> <li> DDA Algorithm if |m|1 y k+1 = y k + 1 x k+1 = x k + 1/m x0x0 y0y0 x end y end x0x0 y0y0 x end y end </li> <li> Slide 5 </li> <li> DDA Algorithm #include inline int round (const float a) { return int (a + 0.5); } void lineDDA (int x0, int y0, int xEnd, int yEnd) { int dx = xEnd - x0, dy = yEnd - y0, steps, k; float xIncrement, yIncrement, x = x0, y = y0; if (fabs (dx) &gt; fabs (dy)) steps = fabs (dx);/* |m|=1 */ xIncrement = float (dx) / float (steps); yIncrement = float (dy) / float (steps); setPixel (round (x), round (y)); for (k = 0; k &lt; steps; k++) { x += xIncrement; y += yIncrement; setPixel (round (x), round (y)); } </li> <li> Slide 6 </li> <li> Bresenhams Line Algorithm xkxk ykyk x k+1 y k+1 y dudu dldl xkxk x k+1 ykyk y k+1 </li> <li> Slide 7 </li> <li> Bresenhams Line Algorithm #include /* Bresenham line-drawing procedure for |m| xEnd) { x = xEnd; y = yEnd; xEnd = x0; } else { x = x0; y = y0; } setPixel (x, y); while (x &lt; xEnd) { x++; if (p &lt; 0) p += twoDy; else { y++; p += twoDyMinusDx; } setPixel (x, y); } </li> <li> Slide 8 </li> <li> Circle Drawing Pythagorean Theorem: x 2 + y 2 = r 2 (x-x c ) 2 + (y-y c ) 2 = r 2 (x c -r) x (x c +r) y = y c r 2 - (x-x c ) 2 xcxc ycyc r (x, y) </li> <li> Slide 9 </li> <li> Circle Drawing change x change y </li> <li> Slide 10 </li> <li> Circle Drawing using polar coordinates x = x c + r. cos y = y c + r. sin change with step size 1/r r (x, y) (x c, y c ) </li> <li> Slide 11 </li> <li> Circle Drawing using polar coordinates x = x c + r. cos y = y c + r. sin change with step size 1/r use symmetry if &gt;45 0 r (x, y) (x c, y c ) (x, y) (x c, y c ) 45 0 (y, x)(y, -x) (-x, y) </li> <li> Slide 12 </li> <li> Midpoint Circle Algorithm f(x,y) = x 2 + y 2 - r 2 </li> <li> Inside-Outside test When is a point an interior point? Odd-Even Rule Draw conceptually a line from a specified point to a distant point outside the coordinate space; count the number of polygon edges that are crossed if odd =&gt; interior if even =&gt; exterior Note! The vertices! </li> <li> Slide 67 </li> <li> Text Representation: * bitmapped (raster) + fast - more storage - less good for styles/sizes * outlined (lines and curves) + less storage + good for styles/sizes - slower </li> <li> Slide 68 </li> <li> Other output primitives * pattern (to fill an area) normally, an n x m rectangular color pixel array with a specified reference point * polymarker (marker symbol) a character representing a point * (polyline) a connected sequence of line segments </li> <li> Slide 69 </li> <li> Attributes Influence the way a primitive is displayed Two main ways of introducing attributes: 1) added to primitives parameter list e.g. setPixel(x, y, color) 2) a list of current attributes (to be updated when changed) e.g setColor(color); setPixel(x, y); </li> <li> Slide 70 </li> <li> Attributes for lines Lines (and curves) are normally infinitely thin type dashed, dotted, solid, dot-dashed, pixel mask, e.g. 11100110011 width problem with the joins; line caps are used to adjust shape pen/brush shape color (intensity) </li> <li> Slide 71 </li> <li> Lines with width Line caps Joins </li> <li> Slide 72 </li> <li> Attributes for area fill fill style hollow, solid, pattern, hatch fill, color pattern tiling </li> <li> Slide 73 </li> <li> Tiling Tiling = filling surfaces (polygons) with a rectangular pattern </li> <li> Slide 74 </li> <li> Attributes for characters/strings style font (typeface) color size (width/height) orientation path spacing alignment </li> <li> Slide 75 </li> <li> Text attributes </li> <li> Slide 76 </li> <li> Text attributes, contd </li> <li> Slide 77 </li> <li> Slide 78 </li> <li> Slide 79 </li> <li> Color as attribute Each color has a numerical value, or intensity, based on some color model. A color model typically consists of three primary colors, in the case of displays Red, Green and Blue (RGB) For each primary color an intensity can be given, either 0-255 (integer) or 0-1 (float) yielding the final color 256 different levels of each primary color means 3x8=24 bits of information to store </li> <li> Slide 80 </li> <li> Color representations Two different ways of storing a color value: 1) a direct color value storage/pixel 2) indirectly via a color look-up table index/pixel (typically 256 or 512 different colors in the table) </li> <li> Slide 81 </li> <li> Color Look-up Table </li> <li> Slide 82 </li> <li> Antialiasing Aliasing the fact that exact points are approximated by fixed pixel positions Antialiasing = a technique that compensates for this (more than one intensity level/pixel is required) </li> <li> Slide 83 </li> <li> Antialiasing, a method A polygon will be studied (as an example). Area sampling (prefiltering): a pixel that is only partly included in the exact polygon, will be given an intensity that is proportional to the extent of the pixel area that is covered by the true polygon </li> <li> Slide 84 </li> <li> Area sampling P = polygon intensity B = background intensity f = the extent of the pixel area covered by the true polygon pixel intensity = P*f + B*(1 - f) Note! Time consuming to calculate f </li> <li> Slide 85 </li> <li> Topics Clipping Cohen-Sutherland Line Clipping Algorithm </li> <li> Slide 86 </li> <li> Clipping Why clipping? Not everything defined in the world coordinates is inside the world window Where does clipping take place? OpenGL does it for you BUT, as a CS major, you should know how it is done. Model Viewport Transformation Clipping </li> <li> Slide 87 </li> <li> Line Clipping int clipSegment(p1, p2, window) Input parameters: p1, p2, window p1, p2: 2D endpoints that define a line window: aligned rectangle Returned value: 1, if part of the line is inside the window 0, otherwise Output parameters: p1, p2 p1 and/or p2s value might be changed so that both p1 and p2 are inside the window </li> <li> Slide 88 </li> <li> Line Clipping Example Line RetVal Output AB BC CD DE EA o P1 o P2 o P3 o P4 o P1 o P2 o P3 o P4 </li> <li> Slide 89 </li> <li> Cohen-Sutherland Line Clipping Algorithm Trivial accept and trivial reject If both endpoints within window trivial accept If both endpoints outside of same boundary of window trivial reject Otherwise Clip against each edge in turn Throw away clipped off part of line each time How can we do it efficiently (elegantly)? </li> <li> Slide 90 </li> <li> Cohen-Sutherland Line Clipping Algorithm Examples: trivial accept? trivial reject? window L1 L2 L3 L4 L5 L6 </li> <li> Slide 91 </li> <li> Cohen-Sutherland Line Clipping Algorithm Use region outcode </li> <li> Slide 92 </li> <li> Cohen-Sutherland Line Clipping Algorithm outcode (x &lt; Window.left) outcode (y &gt; Window.top) outcode (x &gt; Window.right) outcode (y &lt; Window.bottom) </li> <li> Slide 93 </li> <li> Cohen-Sutherland Line Clipping Algorithm Both outcodes are FFFF Trivial accept Logical AND of two outcodes FFFF Trivial reject Logical AND of two outcodes = FFFF Cant tell Clip against each edge in turn Throw away clipped off part of line each time </li> <li> Slide 94 </li> <li> Cohen-Sutherland Line Clipping Algorithm Examples: outcodes? trivial accept? trivial reject? window L1 L2 L3 L4 L5 L6 </li> <li> Slide 95 </li> <li> Cohen-Sutherland Line Clipping Algorithm int clipSegment(Point2&amp; p1, Point2&amp; p2, RealRect W) do if(trivial accept) return 1; else if(trivial reject) return 0; else if(p1 is inside) swap(p1, p2) if(p1 is to the left) chop against the left else if(p1 is to the right) chop against the right else if(p1 is below) chop against the bottom else if(p1 is above) chop against the top while(1); </li> <li> Slide 96 </li> <li> Cohen-Sutherland Line Clipping Algorithm A segment that requires 4 clips </li> <li> Slide 97 </li> <li> Cohen-Sutherland Line Clipping Algorithm How do we chop against each boundary? Given P1 (outside) and P2, (A.x,A.y)=? </li> <li> Slide 98 </li> <li> Cohen-Sutherland Line Clipping Algorithm Let dx = p1.x - p2.xdy = p1.y - p2.y A.x = w.r d = p1.y - A.y e = p1.x - w.r d/dy = e/dx p1.y - A.y = (dy/dx)(p1.x - w.r) A.y = p1.y - (dy/dx)(p1.x - w.r) = p1.y + (dy/dx)(w.r - p1.x) As A is the new P1 p1.y += (dy/dx)(w.r - p1.x) p1.x = w.r Q: Will we have divided-by-zero problem? </li> <li> Slide 99 </li> <li> UNIT-II THREE- DIMENSIONAL CONCEPTS </li> <li> Slide 100 </li> <li> 3D VIEWING </li> <li> Slide 101 </li> <li> 3D Viewing-contents Viewing pipeline Viewing coordinates Projections View volumes and general projection transformations clipping </li> <li> Slide 102 </li> <li> 3D Viewing World coordinate system(where the objects are modeled and defined) Viewing coordinate system(viewing objects with respect to another user defined coordinate system) Scene coordinate system(a viewing coordinate system chosen to be at the centre of a scene) Object coordinate system(a coordinate system specific to an object.) </li> <li> Slide 103 </li> <li> 3D viewing Simple camera analogy is adopted </li> <li> Slide 104 </li> <li> 3D viewing-pipeline </li> <li> Slide 105 </li> <li> 3D viewing Defining the viewing coordinate system and specifying the view plane </li> <li> Slide 106 </li> <li> 3D viewing First pick up a world coordinate position called the view reference point. This is the origin of the VC system Pick up the +ve direction for the Z v axis and the orientation of the view plane by specifying the view plane normal vector N. Choose a world coordinate position and this point establishes the direction for N relative to either the world or VC origin. The view plane normal vector is the directed line segment. steps to establish a Viewing coordinate system or view reference coordinate system and the view plane </li> <li> Slide 107 </li> <li> 3D viewing steps to establish a Viewing coordinate system or view reference coordinate system and the view plane Some packages allow us to choose a look at point relative to the view reference point. Or set up a Left handed viewing system and take the N and the +ve Z v axis from the viewing origin to the look- at point. </li> <li> Slide 108 </li> <li> 3D viewing steps to establish a Viewing coordinate system or view reference coordinate system and the view plane We now choose the view up vector V. It can be specified as a twist angle about Z v axis. Using N,V U can be specified. Generally graphics packages allow users to choose a position of the view plane along the Z v axis by specifying the view plane distance from the viewing origin. The view plane is always parallel to the X v Y v plane. </li> <li> Slide 109 </li> <li> 3D viewing To obtain a series of views of a scene we can keep the view reference point fixed and change the direction of N or we can fix N direction and move the view reference point around the scene. </li> <li> Slide 110 </li> <li> 3D viewing (a) Invert Viewing z Axis (b) Translate Viewing Origin to World Origin (c) Rotate About World x Axis to Bring Viewing z Axis into the xz Plane of the World System (d) Rotate About the World y Axis to Align the Two z Axes (e) Rotate About the World z Axis to Align the Two Viewing Systems Transformation from world to viewing coordinate system M wc,vc =R z R y R x.T </li> <li> Slide 111 </li> <li> What Are Projections? Picture Plane Objects in World Space Our 3-D scenes are all specified in 3-D world coordinates To display these we need to generate a 2-D image - project objects onto a picture plane </li> <li> Slide 112 </li> <li> Converting From 3-D To 2-D Projection is just one part of the process of converting from 3-D world coordinates to a 2-D image Clip against view volume Project onto projection plane Transform to 2-D device coordinates 3-D world coordinate output primitives 2-D device coordinates </li> <li> Slide 113 </li> <li> Types Of Projections There are two broad classes of projection: Parallel: Typically used for architectural and engineering drawings Perspective: Realistic looking and used in computer graphics Perspective Projection Parallel Projection </li> <li> Slide 114 </li> <li> Taxonomy Of Projections </li> <li> Slide 115 </li> <li> Types Of Projections There are two broad classes of projection: Parallel: preserves relative proportions of objects accurate views of various sides of an object can be obtained does not give realistic representations of the appearance of a 3D objective. Perspective: produce realistic views but does not preserve relative proportions projections of distant objects are smaller than the projections of objects of the same size that are closer to the projection plane. </li> <li> Slide 116 </li> <li> Parallel Projections Some examples of parallel projections Orthographic Projection(axonometric) Orthographic oblique </li> <li> Slide 117 </li> <li> Parallel Projections Some examples of parallel projections Isometric projection for a cube The projection plane is aligned so that it intersects each coordinate axes in which the object is defined (principal axes) at the same distance from the origin. All the principal axes are foreshortened equally. </li> <li> Slide 118 </li> <li> Parallel Projections Transformation equations for an orthographic parallel projections is simple Any point (x,y,z) in viewing coordinates is transformed to projection coordinates as Xp=XYp=Y </li> <li> Slide 119 </li> <li> Parallel Projections Transformation equations for oblique projections is as below. Oblique projections </li> <li> Slide 120 </li> <li> Parallel Projections Transformation equations for oblique projections is as below. Oblique projections An orthographic projection is obtained when L1=0. In fact the effect of the projection matrix is to shear planes of constant Z and project them on to the view plane. Two common oblique parallel projections: Cavalier and Cabinet </li> <li> Slide 121 </li> <li> Parallel Projections Oblique projections 2 common oblique parallel projections: Cavalier projection Cabinet projection All lines perpendicular to the projection plane are projected with no change in length. They are more realistic than cavaliar Lines perpendicular to the viewing surface are projected at one-half their length. </li> <li> Slide 122 </li> <li> Perspective Projections visual effect is similar to human visual system... has 'perspective foreshortening size of object varies inversely with distance from the center of projection. angles only remain intact for faces parallel to projection plane. </li> <li> Slide 123 </li> <li> Perspective Projections Where u varies from o to 1 </li> <li> Slide 124 </li> <li> Perspective Projections </li> <li> Slide 125 </li> <li> If the view plane is the UV plane itself then Zvp=0. The projection coordinates become Xp=X(Zprp/(Zprp-Z))=X(1/(1-Z/Zprp)) Yp=Y(Zprp/(Zprp-Z))=Y(1/(1-Z/Zprp)) If the PRP is selected at the viewing cooridinate origin then Zprp=0 The projection coordinates become Xp=X(Zvp/Z) Yp=Y(Zv...</li></ul>