610 likes | 743 Views
CA 302 Computer Graphics and Visual Programming. Aydın Öztürk aydin.ozturk @ ege.edu.tr http://www. ube.ege.edu.tr/~ozturk. Graphics Output Primitives. 1. Introduction. Output Primitives: Point positions and line segments are the simplest geometric pirimitives.
E N D
CA 302 Computer Graphics and VisualProgramming Aydın Öztürk aydin.ozturk@ege.edu.tr http://www.ube.ege.edu.tr/~ozturk Graphics OutputPrimitives
1. Introduction Output Primitives: Point positions and line segments are the simplest geometric pirimitives. Other geometric pirimitives: Circles, conicsections, spline curves and surfaces
2. Coordinate Reference Frame To describe a picture first we define a world coordinate reference frame (2D or 3D). Next, we describe the positions of the objects in world coordinates. These coordinate positions along with other information such as color are stored . Objects are then displayed by passing the scene information to the viewing routines.
2. Coordinate Reference Frame Locations on a video monitorarereferenced in integerscreencoordinateswhichcorrespondtopixelpositions in theframebuffer. 5 1 4 3 2 1 1 0 0 1 2 3 4 5
2. Coordinate Reference Frame Once pixel positions have been identified for an object, the appropriate color values must be stored in the frame buffer. For this purpose we assume that we have the low-level Procedures setPixel (x,y) getPixel (x,y, color)
3. Specifying a 2D World-Coordinate System in OpenGL The OpenGl commands for setting up a 2D Cartesianreference frame are glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluOrtho2D (xmin, xmax, ymin, ymax) We add an identity matrix before the projection matrix to ensure that the coordinate values were not accumulated with any previously computed values.
4. OpenGLPointFunctions We use the following OpenGl function to state the coordinate values of a single point glBegin (GL_POINTS); glVertex* ( ); glEnd; where (*) indicates that suffix codes are required for this function.
4. OpenGLPointFunctions (cont.) Alternatively we can specify the coordinate values as int point1 [ ]={50, 100}; int point2 [ ]={75, 150}; int point3 [ ]={100, 200}; and call the OpenGL function to plot these points as(v is for vector) glBegin (GL_POINTS); glVertex2iv (point1); glVertex2iv (point2); glVertex2iv (point3); glEnd;
4. OpenGLLineFunctions (cont.) glBegin (GL_LINES); glVertex2iv (point1); glVertex2iv (point2); glVertex2iv (point3); glEnd; p3 p3 p1 p1 p1 p5 p5 p3 p4 p4 p4 p2 p2 p2 GL_LINE_STRIP GL_LINE_LOOP GL_LINES
5. LineDrawingAlgorithms The line equation For a given interval the corrresponding y -interval can be computed as yend y0 xend x0
5.1 DDA Algorithm 5 TheDigitalDifferntialAnalyzer(DDA) is scan-conversionalgorithmbased on calculatingeitheror . 4 3 2 1 0 0 1 2 3 4 5 5 4 3 2 1 0 0 1 2 3 4 5
5.1 DDA Algorithm(cont.) Form<1 : Form>1: yk xk
5.1 DDA Algorithm(cont.) Since m is a real number, each calculated x or y values must be rounded to the integer. • DDA is a fast algorithm. • Accumulation of round-off error in successive additions of the increments can couse a serios problem.
5.2 Bresenham’sLineAlgorithm Bresenham’sLineAlgorithmusesonlyinrementalintegercalculations. Whensampling at unitxintervals, wedecideaboutwhich of thetwopixelpositions is closertothetruelinepath at each step. yk+3 yk+3 y=mx+b yk+2 yk+1 yk xkxk+1 xk+2 xk+3 xk+4 xk+5
5.2 Bresenham’sLineAlgorithm(cont.) yk+1 dupper y dlower yk xk+1
5.2 Bresenham’sLineAlgorithm(cont.) To determine which of the two pixels is closest to the line path, a test is made based on the following quantity: or equivalently where
5.2 Bresenham’sLineAlgorithm(cont.) • The sign of pk is the same as the sign since • c is a constant and eliminated in the recursive calculations. • Weplot the lower pixel if p <0 otherwise we plot the upper pixel.
5.2 Bresenham’sLineAlgorithm(cont.) We can calculate pk recursively as follows:
5.2 Bresenham’sLineAlgorithm(cont.) Finding the inital value p0
5.2 Bresenham’sLineAlgorithm(cont.) The algorithm 1. Input the line end poins and store (x0, y0). 2.Set the color for frame-buffer position (x0, y0); (i.e. Plot the firs point). 3. Calculate the constants and find . 4. At each xk starting at k=0, perform the following test: If , then the next point to plot is Otherwise the next point to plot is 5.Perform step 4 times.
5.3 LineDrawingUsingParametricLineEquations A parametric line equation in 2D that goes through points P0= (x0, y0) and P1= (x1, y1) can be written as
5.3 LineDrawingUsingParametricLineEquations A nice property of this algorithm is that it works whether or not x1>x2. An incremental pseudo code for this algorithm is
6. Circle-GeneratingAlgorithms A point(x,y ) on a circle can be expressedbytheequation (x,y) r θ yc xc For a given x, the corresponding y-value can be calculated as
6.1. Midpoint Circle Algorithm To apply the mid-point method we define Assuming that the point (xk, yk) was plotted, we determine whether the next pixel at position (xk+1, yk) or at position (xk+1, yk). yk yk-1 Midpoint xk xk+1 xk+2
6.1. MidpointCircleAlgorithm The decision function is defined as A recursive expression for the next position at position xk+1+1= xk+2 is
6.1. MidpointCircleAlgorithm or where yk+1 is either yk or yk-1depending on the sign of pk .
6.1. MidpointCircleAlgorithm The initial decision parameter is obtained as follows: If r is defined as an integer, p0can be raunded to p0=1-r
6.1. MidpointCircleAlgorithm The algorithm: 1. Input radius r and circle center ( xc yc) then set the coordinates for the first point as (( x0 y0)=(0, r). 2. Calculate the initial value of the decision parameter as 3.At each xk position, starting at k=0, perform the following test: if pk<0, the next point is at ( xk+1, yk) and otherwise the next point is at ( xk+1, yk-1) and
6.1. MidpointCircleAlgorithm The algorithm: 4. Determine symmetry points in the other oktants. 5. Move each point onto the circle centered at (xc, yc). 6. Repeat steps 3 until x≥y.
7. Fill-AreaPrimitives Another useful costruct, besides points, straight-line segments, and curves for describing components of a picture is an area that is filled with some solid color or pattern. A picture component of this type is referred to as fill area. Mostly, fill areas are used to describe solid objects.
7.1. PolygonFill-Areas Polygon Classification: Convex polygon: All interior angles are ≤1800. Concave polygon: A polygon that is not convex
7.1. PolygonFill-Areas Identifying Concave Polygons • A Concave poligon has at least one interior angle greater than 1800 . • Extension of some edges of a concave polygon intersect other edges. • Some pair of interior points in a concave polygon produces a line segment that intesects the polygon boundary
E5 • E4 <180o • E6 • E3 >180o • E2 • E1 Polygon Fill • Convex Concave
7.1. PolygonFill-Areas Splitting Concave Polygons • Once we have identified a concave poligon we can split it into a set of convex polygons. • This can be accomplished using edge vectors and edge cross products. • We first need to form the edge vectors Ek= Vk+1-Vk . • Next we calculate cross products of successive edge vectors in order around the poligon perimeter. • If the z-component of some cross products is positive while other cross products have a negative z-component, the polygon is concave.
E5 E4 E6 E3 E2 E1 7.1. PolygonFill-Areas Splitting A Concave Poligon (E1× E2)z>0 (E2× E3)z>0 (E3× E4)z<0 (E4× E5)z>0 (E5× E6)z>0
7.1. PolygonFill-Areas Example E5 E6 E4 E2 E3 E1
7.1. Polygon Fill-Areas Splitting of a Polygon into Triangles • OpenGL guarentees correct rendering of polygons only if they are convex. A triangle is a convex polygon. • But a polygon which has >3 vertices can be concave. • So, we must tesselate a given polygon into triangles.The GLU library has a tesselator.
7.1. PolygonFill-Areas: Inside-OutsideTesting Wewantfill inside of a polygonwithconstantcolor • Flat and Convexpolygons areguarenteedto be renderedcorrectlybyOpenGL. • Fornon-flatpolygons: • We can workwiththeirprojections. • Orwe can usefirstthreeverticestodetermine a flatplanetousefortheinterrior. • Fornon-simpleflatpolygons, wemustdecidehowtodeterminewhether a givenpoint is inside oroutside of thepolygon .
Odd-even rule (easy implementation) crossing, odd parity rule, even-odd rule Crossed odd edges: inside point Crossed even edges : outside point Non-zero winding rule Clocwise edges : +1 Counterclocwise edges : –1 Don’t fill while total 0 7.1. PolygonFill-Areas: Inside-OutsideTesting
7.2. Polygon Tables VERTEX TABLE EDGE TABLE POLYGON-SURFACE TABLE V1: x1 y1 z1 V2: x2 y2 z2 V3: x3 y3 z3 V4: x4 y4 z4 V5: x5 y5 z5 E1 : V 1, V 2 E2 : V 2, V 3 E3 : V 3, V 1 E4 : V 3, V 4 E5 : V 4, V 5 E6 : V 5, V 1 S1 : E1, E 2, E3 S2 : E 3, E4, E5, E6 V1 E6 E1 E3 S1 S2 V5 V3 E2 E5 E4 V2 V4
7.2. Polygon Tables E6 E1 E3 S1 S2 V3 E2 E5 E4 Edge Table for the surfaces V5 V2 V4
7.3. Planes (x, y, z)
7.3.1 PlaneEquations (x, y, z)
7.3.2 Front and BackPolygonfaces • For any point (x, y, z) ona plane we have • For any point (x, y, z) behind the plane we have • For any point (x, y, z) is infront of the plane we have
7.3.2 Front and BackPolygonfaces • Orientation of a polygon surface can be described with the normal vector for the plane. • The cartesian coordinates of the normal is (A, B, C). N y V1 V2 V3 x z
7.3.2 Front and BackPolygonfaces • The elements of normal vector can also be obtained using a vector cross product. • In a rigthanded Cartesian system if we select the vertex positions V1, V2 , V3 taken in the counter clockwise order when viewing from outside the object toward the inside we have N y V1 V2 V3 x z
7.3.2 Front and BackPolygonfaces • For any pointP on the plane, the plane equation can be written as
7.3.2 Front and BackPolygonfaces Example: Find the plane equation for the points V1=(1, 2, 3), V2=( 4, 3, 8), V3=(8, 13, 16) First, de define the vectors on in the plane as P1= V2- V1 = ( 3, 1, 5) P2= V3- V1 = ( 7, 11, 13) and find the normal vector as N= P1×P2 = (-29, -4, 19) Next, using one of the points in the plane (e.g. V1), N•V1 = -D = 20 we obtain the plain equatin as -29X -4Y+19Z-20=0
7.4 OpenGLPolygonFill-AreaFunctions • By default, a polygon interior is displayed in a solid color. • In OPENGL , a fill area must be specified as a convex polygon. • A polygon vertex list must contain at least three vertices. glBegin (GL_POLYGON); glvertex2iv (p1); glvertex2iv (p2); glvertex2iv (p3); glvertex2iv (p4); glvertex2iv (p5); glvertex2iv (p6); glEnd( ) p5 p6 p4 p1 p2 p3