Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Graphics Graphics Output Primitives By : Dhaval Shah.

Similar presentations


Presentation on theme: "Computer Graphics Graphics Output Primitives By : Dhaval Shah."— Presentation transcript:

1 Computer Graphics Graphics Output Primitives By : Dhaval Shah

2 Coordinate Frames Line Drawing Algorithms Line Drawing Algorithms: Order Dependence Circle Algorithms Ellipse Algorithms Pixel Addressing Area Fill Methods Plane Equations OpenGL Area Fill Vertex Arrays Pixel Array Primitives Character Handling

3 Coordinate Frame In geometry, a coordinate frame is a system which uses one or more numbers, or coordinates, to uniquely determine the position of a point or other geometric element on a manifold such as Euclidean space The use of a coordinate system allows problems in geometry to be translated into problems about numbers and vice versa; this is the basis of analytic geometry.  Common coordinate systems are  Number line 1.2 Cartesian coordinate system > 1.3 Polar coordinate system 1.4 Cylindrical and spherical coordinate systems 1.5 Homogeneous coordinate system 1.6 Other commonly used systems

4 Screen Coordinate Screen Coordinate System - This 2D coordinate system refers to the physical coordinates of the pixels on the computer screen, based on current screen resolution. Example :Diagram of Screen Coordinate system

5 Explanation x, y coordinates are respectively the horizontal and vertical addresses of any pixel or addressable point on a computer display screen. The x coordinate is a given number of pixels along the horizontal axis of a display starting from the pixel (pixel 0) on the extreme left of the screen. The y coordinate is a given number of pixels along the vertical axis of a display starting from the pixel (pixel 0) at the top of the screen. Together, the x and coordinates locate any specific pixel location on the screen. x and y coordinates can also be specified as values relative to any starting point on the screen or any subset of the screen such as an image. On the Web, each clickable area of an image map is specified as a pair of and y coordinates relative to the upper left-hand corner of the image

6 Points (0,0) (maxx,maxy) CRT The electron beam is turned on to illuminate the phosphor at the selected location (x, y) where 0 ≤ x ≤ maxx 0 ≤ y ≤ maxy setpixel(x, y, intensity) – loads an intensity value into the frame-buffer at (x, y). getpixel(x, y) – retrieves the current frame-buffer intensity setting at position (x, y).

7

8 Lines Analog devises, such as a random-scan display or a vector plotter, display a straight line smoothly from one endpoint to another. Linearly varying horizontal and vertical deflection voltages are generated that are proportional to the required changes in the x and y directions to produce the smooth line.

9 Digital devices display a straight line by plotting discrete coordinate points along the line path which are calculated from the equation of the line. Screen locations are referenced with integer values, so plotted positions may only approximate actual line positions between two specific endpoints. A computed line position of (10.48, 20.51) will be converted to pixel position (10, 21). This rounding of coordinate values to integers causes lines to be displayed with a stairstep appearance (the “jaggies”). Particularly noticeable on systems with low resolution. To smooth raster lines, pixel intensities along the line paths must be adjusted.

10 Line Drawing Algorithms
Cartesian equation: y = mx + c where m – slope c – y-intercept x1 y1 x2 y2

11 The Digital Differential Analyzer (DDA) Algorithm
means that for a unit (1) change in x there is m-change in y. Do not use y = 3x + 1 to calculate y. Use m x y 1 4 2 7 3 10 13 5 16 i.e. y = 3x m = 3 means that for a unit (1) change in y there is 1/m change in x.

12 The DDA Method Uses differential equation of the line : m
If slope |m|  1 then increment x in steps of 1 pixel and find corresponding y-values. If slope |m|  1 then increment y in steps of 1 pixel and find corresponding x-values. step through in x step through in y

13 The DDA Method Desired line (xi+1,round(yi+m)) (xi+1,yi+m) (xi,yi)

14 if |m|  1 xi+1 = xi + 1 yi+1 = yi + m if |m|  1 yi+1 = yi + 1
if slope m  0 if |m|  1 xi+1 = xi + 1 yi+1 = yi + m if |m|  1 yi+1 = yi + 1 xi+1 = xi + 1/m Right Right Left Left

15 Proceeding from right-endpoint to left-endpoint if slope m  0
if |m|  1 xi+1 = xi - 1 yi+1 = yi - m if |m|  1 yi+1 = yi - 1 xi+1 = xi - 1/m Right Right Left Left

16 if |m|  1 xi+1 = xi + 1 yi+1 = yi + m if |m|  1 yi+1 = yi - 1
if slope m < 0 if |m|  1 xi+1 = xi + 1 yi+1 = yi + m if |m|  1 yi+1 = yi - 1 xi+1 = xi - 1/m Left Left Right Right

17 Proceeding from right-endpoint to left-endpoint if slope m  0
if |m|  1 xi+1 = xi - 1 yi+1 = yi - m if |m|  1 yi+1 = yi + 1 xi+1 = xi + 1/m Left Left Right Right

18 Simple DDA Line Algorithm {Based on the parametric equation of a line}
Procedure DDA(X1,Y1,X2,Y2 :Integer); Var Length, I :Integer; X,Y,Xinc,Yinc :Real; Begin Length := ABS(X2 - X1); If ABS(Y2 - Y1) > Length Then Length := ABS(Y2-Y1); Xinc := (X2 - X1)/Length; Yinc := (Y2 - Y1)/Length; X := X1; Y := Y1; DDA (digital differential analyzer) creates good lines but it is too time consuming due to the round function and long operations on real values. For I := 0 To Length Do Begin Plot(Round(X), Round(Y)); X := X + Xinc; Y := Y + Yinc End {For} End; {DDA}

19 Bresenham Line Algorithm A more efficient approach
Basis of the algorithm: From start position decide A or B next A B Start position

20 Bresenham Line Algorithm
True line ti si For a given value of x one pixel lies at distance ti above the line, and one pixel lies at distance si below the line

21 Bresenham Line Algorithm
Decision parameter di = (si - ti) If di  0, then closest pixel is below true line (si smaller) If di  0, then closest pixel is above true line (ti smaller) We must calculate the new values for di as we move along the line.

22 Example: At x1 : At x2 : At x3 : True line Start pixel at (x0,y1) x1
s1 = dy t1 = dx - dy d1 = (si - ti) = dy - (dx - dy) = 2dy - dx but 2dy  dx  di  0  y stays the same hence next pixel is at (x1,y1) True line 4dy 3dy At x2 : s2 = 2dy t2 = dx - 2dy d2 = (s2 – t2) = 2dy - (dx - 2dy) = 4dy - dx Suppose d2  0  y is incremented hence next pixel is at (x2,y2) 2dy dy At x3 : s3 = 3dy - dx t2 = 2dx - 3dy d3 = (s2 – t3) = 6dy - 3dx  0 so y stays the same hence next pixel is at (x3,y2)

23 In General For a line with gradient ≤ 1 For a line with gradient  1
d0 = 2dy – dx if di  0 then yi+1 = yi di+1 = di + 2dy if di ≥ 0 then yi+1 = yi + 1 di+1 = di + 2(dy – dx) xi+1 = xi + 1 For a line with gradient  1 d0 = 2dx – dy if di  0 then xi+1 = xi di+1 = di + 2dx if di ≥ 0 then xi+1 = xi + 1 di+1 = di + 2(dx – dy) yi+1 = yi + 1 Note: For |m| ≤ 1 the constants 2dy and 2(dy-dx) can be calculated once, so the arithmetic will involve only integer addition and subtraction.

24 Example – Draw a line from (20,10) to (30,18)
dx = 10 dy = 8 initial decision d0 = 2dy – dx = 6 Also 2dy = 16, 2(dy – dx) = -4 i di (xi+1,yi+1) (21,11) (22,12) (23,12) (24,13) (25,14) (26,15) (27,16) (28,16) (29,17) (30,18) 19 18 17 16 15 14 13 12 11 10 20 21 22 23 24 25 26 27 28 29 30 31 32

25 void LineBres(int x0, int y0, int x1, int y1) // line for |m| < 1 { int dx = abs(x1 – x0), dy = abs(y1 – y0); int d = 2 * dy – dx, twoDy = 2 * dy, twoDyMinusDx = 2 * (dy – dx); int x, y; if (x0 > x1) { // determines which point to use as start, which as end x = x1; y = y1; x1 = x0; } else { x = x0; y = y0; setPixel(x,y); while (x < x1) { x++; if (d < 0) d += twoDy; y++; d += twoDyMinusDx; setPixel(x, y);

26 Pixel addressing When an object is scan converted into the frame buffer, the input description is transformed to pixel coordinates. So, the displayed image may not correspond exactly with the relative dimensions of the input object. To preserve the specified geometry of world objects, we need to compensate for the mapping of mathematical input points to finite pixel area, we use one of the two ways:

27 Pixel addressing Adjust the dimensions of displayed objects to account for the amount of overlap of pixel areas with the object boundaries. (i.e. a rectangle with 40 cm width, will be displayed in 40 pixel) 2) Map world coordinates onto screen positions between pixels, so that we align objects boundaries with pixel boundaries instead of pixel centers.

28 Pixel addressing Screen Grid Coordinates:
An alternative to addressing display positions in terms of pixel centers is to reference screen coordinates with respect to the grid of horizontal and vertical pixel boundary lines spaced one unit a part.

29 Pixel addressing Screen coordinate position is then the pair of integer values identifying a grid intersection position between two pixels. For example, the mathematical line path for a polyline with screen endpoints (0, 0), (5, 2), and (1,4) is shown beside.

30 Pixel addressing With the coordinate origin at the lower left of the screen, each pixel area can be referenced by the integer grid coordinates of its lower left corner. The following figure illustrates this convention for an 8 by 8 section of a raster, with a single illuminated pixel at screen coordinate position (4, 5).

31 Pixel addressing In general, we identify the area occupied by a pixel with screen coordinates ( x, y) as the unit square with diagonally opposite corners at (x, y) and ( x + 1, y + 1 ). This pixel addressing scheme has several advantages: It avoids half-integer pixel boundary It facilitates precise object representations. Simplifies the processing involved in many scan conversion algorithms and in other raster procedures

32 Pixel addressing Notes:
1)      The previous algorithms for drawing line, circle, …etc are still valid when applied to input positions expressed as screen grid coordinates. 2)      The decision parameter Pk is a measure of screen grid separation differences rather than separation differences from pixel centers.

33 Pixel addressing A circle of radius 5 and center position (10, 10), for instance, would be displayed by the midpoint circle algorithm using screen grid coordinate positions. But the plotted circle has a diameter of 11, To plot the circle with the defined diameter of 10, we can modify the circle algorithm to shorten each pixel scan line and each pixel column.

34 Pixel addressing

35 Pixel addressing

36 Order Dependency Is this image correct? Probably not
Polygons are rendered in the order they pass down the pipeline Blending functions are order dependent

37 Opaque and Translucent Polygons
Suppose that we have a group of polygons some of which are opaque and some translucent How do we use hidden-surface removal? Opaque polygons block all polygons behind them and affect the depth buffer Translucent polygons should not affect depth buffer Render with glDepthMask(GL_FALSE) which makes depth buffer read-only Sort polygons first to remove order dependency

38 Clipping Operations Clipping Clip window Applications of clipping
Identify those portions of a picture that are either inside or outside of a specified region of space. Clip window The region against which an object is to be clipped. The shape of clip window Applications of clipping World-coordinate clipping

39 Clipping Operations Viewport clipping Types of clipping
It can reduce calculations by allowing concatenation of viewing and geometric transformation matrices. Types of clipping Point clipping Line clipping Area (Polygon) clipping Curve clipping Text clipping Point clipping (Rectangular clip window)

40 Line Clipping Possible relationships between line positions and a standard rectangular clipping region Before clipping after clipping

41 Line Clipping Possible relationships
Completely inside the clipping window Completely outside the window Partially inside the window Parametric representation of a line x = x1 + u(x2 - x1) y = y1 + u(y2 - y1) The value of u for an intersection with a rectangle boundary edge Outside the range 0 to 1 Within the range from 0 to 1

42 Cohen-Sutherland Line Clipping
Region code A four-digit binary code assigned to every line endpoint in a picture. Numbering the bit positions in the region code as 1 through 4 from right to left.

43 Cohen-Sutherland Line Clipping
Bit values in the region code Determined by comparing endpoint coordinates to the clip boundaries A value of 1 in any bit position: The point is in that relative position. Determined by the following steps: Calculate differences between endpoint coordinates and clipping boundaries. Use the resultant sign bit of each difference calculation to set the corresponding bit value.

44 Cohen-Sutherland Line Clipping
The possible relationships: Completely contained within the window 0000 for both endpoints. Completely outside the window Logical and the region codes of both endpoints, its result is not 0000. Partially

45 Splitting Concave Polygons
Identify a concave polygon Calculating the cross product of successive edge vectors. If the z component of some cross product is positive while others have a negative, it is concave.

46 Splitting Concave Polygons
Vector method Calculate the edge-vector cross product in a counterclockwise order. If any z component turns out to be negative The polygon is concave. Split it along the line of the first edge vector in the cross- product pair.

47 Splitting Concave Polygons

48 Polygon Clipping

49 Sutherland-Hodgeman Polygon Clipping
Processing the polygon boundary as a whole against each window edge Processing all polygon vertices against each clip rectangle boundary in turn

50 Sutherland-Hodgeman Polygon Clipping
Pass each pair of adjacent polygon vertices to a window boundary clipper There are four cases:

51 Sutherland-Hodgeman Polygon Clipping
Intermediate output vertex list Once all vertices have been processed for one clip window boundary, it is generated. The output list of vertices is clipped against the next window boundary. It can be eliminated by a pipeline of clipping routine. Convex polygons are correctly clipped. If the clipped polygon is concave Split the concave polygon

52 Sutherland-Hodgeman Polygon Clipping
v3 v2 v1

53 Weiler-Atherton Polygon Clipping
Developed as a method for identifying visible surfaces It can be applied with arbitrary polygon-clipping region. Not always proceeding around polygon edges Sometimes follows the window boundaries For clockwise processing of polygon vertices For an outside-to-inside pair of vertices, follow the polygon boundary. For an inside-to-outside pair of vertices, follow the window boundary in clockwise direction.

54 Weiler-Atherton Polygon Clipping

55 Other Clipping Curve clipping Text clipping
Use bounding rectangle to test for overlap with a rectangular clip window. Text clipping All-or-none string-clipping All-or-none character-clipping Clip the components of individual characters

56 Circle Generating Algorithms
Circles and ellipses are common components in many pictures. Circle generation routines are often included in packages.

57 Circle Equations rSin) rCos) Polar form x = rCos
y = rSin (r = radius of circle) P=(rCos, rSin) rSin) rCos) x y r

58 Drawing a circle Disadvantages
 = 0° while ( < 360°) x = rCos y = rSin setPixel(x,y)  =  + 1° end while Disadvantages To find a complete circle  varies from 0° to 360° The calculation of trigonometric functions is very slow.

59 Cartesian form Use Pythagoras theorem x2 + y2 = r2 x r y y x r

60 Circle algorithms Disadvantages: Not all pixel filled in
Step through x-axis to determine y-values Disadvantages: Not all pixel filled in Square root function is very slow

61 Circle Algorithms Use 8-fold symmetry and only compute pixel positions for the 45° sector. 45° (x, y) (y, x) (-x, y) (y, -x) (x, -y) (-x, -y) (-y, x) (-y, -x)

62 Bresenham’s Circle Algorithm
Consider only 45° ≤  ≤ 90° General Principle The circle function: and

63 Bresenham’s Circle Algorithm
p1 p3 yi D(si) D(ti) yi - 1 p2 r xi xi + 1 After point p1, do we choose p2 or p3?

64 Bresenham’s Circle Algorithm
Define: D(si) = distance of p3 from circle D(ti) = distance of p2 from circle i.e. D(si) = (xi + 1)2 + yi2 – r [always +ve] D(ti) = (xi + 1)2 + (yi – 1)2 – r2 [always -ve] Decision Parameter pi = D(si) + D(ti) so if pi < 0 then the circle is closer to p3 (point above) if pi ≥ 0 then the circle is closer to p2 (point below)

65 The Algorithm y0 = r p0 = [12 + r2 – r2] + [12 + (r-1)2 – r2] = 3 – 2r
x0 = 0 y0 = r p0 = [12 + r2 – r2] + [12 + (r-1)2 – r2] = 3 – 2r if pi < 0 then yi+1 = yi pi+1 = pi + 4xi + 6 else if pi ≥ 0 then yi+1 = yi – 1 pi+1 = pi + 4(xi – yi) + 10 Stop when xi ≥ yi and determine symmetry points in the other octants xi+1 = xi + 1

66 Example r = 10 p0 = 3 – 2r = -17 Initial point (x0, y0) = (0, 10) i pi
9 8 7 6 5 4 3 2 1 i pi xi, yi -17 (0, 10) 1 -11 (1, 10) 2 -1 (2, 10) 3 13 (3, 10) 4 -5 (4, 9) 5 15 (5, 9) 6 9 (6, 8) 7 (7,7)

67 Advantages of Bresenham circle
Only involves integer addition, subtraction and multiplication There is no need for squares, square roots and trigonometric functions

68 Ellipse-Generating Algorithms
Ellipse – A modified circle whose radius varies from a maximum value in one direction (major axis) to a minimum value in the perpendicular direction (minor axis). P=(x,y) F1 F2 d1 d2 The sum of the two distances d1 and d2, between the fixed positions F1 and F2 (called the foci of the ellipse) to any point P on the ellipse, is the same value, i.e. d1 + d2 = constant

69 Ellipse Properties Expressing distances d1 and d2 in terms of the focal coordinates F1 = (x1, x2) and F2 = (x2, y2), we have: Cartesian coordinates: Polar coordinates: ry rx

70 Ellipse Algorithms Symmetry between quadrants
Not symmetric between the two octants of a quadrant Thus, we must calculate pixel positions along the elliptical arc through one quadrant and then we obtain positions in the remaining 3 quadrants by symmetry (x, y) (-x, y) (x, -y) (-x, -y) rx ry

71 Ellipse Algorithms Decision parameter: 1 Slope = -1 rx ry 2

72 Ellipse Algorithms 1 Slope = -1 rx ry 2 Starting at (0, ry) we take unit steps in the x direction until we reach the boundary between region 1 and region 2. Then we take unit steps in the y direction over the remainder of the curve in the first quadrant. At the boundary therefore, we move out of region 1 whenever

73 Midpoint Ellipse Algorithm
yi yi-1 xi xi+1 xi+2 Midpoint Assuming that we have just plotted the pixels at (xi , yi). The next position is determined by: If p1i < 0 the midpoint is inside the ellipse  yi is closer If p1i ≥ 0 the midpoint is outside the ellipse  yi – 1 is closer

74 Decision Parameter (Region 1)
At the next position [xi = xi + 2] OR where yi+1 = yi or yi+1 = yi – 1

75 Decision Parameter (Region 1)
Decision parameters are incremented by: Use only addition and subtraction by obtaining At initial position (0, ry)

76 Region 2  Decision parameter:
Over region 2, step in the negative y direction and midpoint is taken between horizontal pixels at each step. yi yi-1 xi xi+1 xi+2 Midpoint Decision parameter: If p2i > 0 the midpoint is outside the ellipse  xi is closer If p2i ≤ 0 the midpoint is inside the ellipse  xi + 1 is closer

77 Decision Parameter (Region 2)
At the next position [yi+1 – 1 = yi – 2] OR where xi+1 = xi or xi+1 = xi + 1

78 Decision Parameter (Region 2)
Decision parameters are incremented by: At initial position (x0, y0) is taken at the last position selected in region 1

79 Midpoint Ellipse Algorithm
Input rx, ry, and ellipse center (xc, yc), and obtain the first point on an ellipse centered on the origin as (x0, y0) = (0, ry) Calculate the initial parameter in region 1 as At each xi position, starting at i = 0, if p1i < 0, the next point along the ellipse centered on (0, 0) is (xi + 1, yi) and otherwise, the next point is (xi + 1, yi – 1) and and continue until

80 Midpoint Ellipse Algorithm
(x0, y0) is the last position calculated in region 1. Calculate the initial parameter in region 2 as At each yi position, starting at i = 0, if p2i > 0, the next point along the ellipse centered on (0, 0) is (xi, yi – 1) and otherwise, the next point is (xi + 1, yi – 1) and Use the same incremental calculations as in region 1. Continue until y = 0. For both regions determine symmetry points in the other three quadrants. Move each calculated pixel position (x, y) onto the elliptical path centered on (xc, yc) and plot the coordinate values x = x + xc , y = y + yc

81 Example rx = 8 , ry = 6 2ry2x = 0 (with increment 2ry2 = 72)
2rx2y = 2rx2ry (with increment -2rx2 = -128) Region 1 (x0, y0) = (0, 6) i pi xi+1, yi+1 2ry2xi+1 2rx2yi+1 -332 (1, 6) 72 768 1 -224 (2, 6) 144 2 -44 (3, 6) 216 3 208 (4, 5) 288 640 4 -108 (5, 5) 360 5 (6, 4) 432 512 6 244 (7, 3) 504 384 Move out of region 1 since 2ry2x > 2rx2y

82 Example Region 2 (x0, y0) = (7, 3) (Last position in region 1) i pi
xi+1, yi+1 2ry2xi+1 2rx2yi+1 -151 (8, 2) 576 256 1 233 (8, 1) 128 2 745 (8, 0) - Stop at y = 0 6 5 4 3 2 1 7 8

83 Mathematical equation of a line
v t > 0 p0 t = 0 t < 0

84 Line Equations Slope-intercept line equation: where: yend y0 x0 xend
Let’s quickly review the equations involved in drawing lines Slope-intercept line equation: x y y0 yend xend x0 where:

85 Lines & Slopes The slope of a line (m) is defined by its start and end coordinates The diagram below shows some examples of lines and their slopes m = 0 m = -1/3 m = -1/2 m = -1 m = -2 m = -4 m = ∞ m = 1/3 m = 1/2 m = 1 m = 2 m = 4

86 A Very Simple Solution We could simply work out the corresponding y coordinate for each unit x coordinate Let’s consider the following example: x y (2, 2) (7, 5) 2 7 5

87 A Very Simple Solution (cont…)
5 4 3 2 1 1 2 3 4 5 6 7

88 A Very Simple Solution (cont…)
x y (2, 2) (7, 5) 2 3 4 5 6 7 First work out m and b: Now for each x value work out the y value:

89 A Very Simple Solution (cont…)
Now just round off the results and turn on these pixels to draw our line 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8

90 A Very Simple Solution (cont…)
However, this approach is just way too slow In particular look out for: The equation y = mx + b requires the multiplication of m by x Rounding off the resulting y coordinates We need a faster solution

91 Raster line For now, we consider only 1-pixel width lines.
The screen pixel mesh is represented in different forms Raster NE M1 P (xp, yp) E .91.

92 Display the line on a Raster Monitor
This process digitizes the line into a set of discrete integer positions that, in general, only approximates the actual line path. the graphics system must first project the endpoints to integer screen coordinates and determine the nearest pixel positions along the line path between the two endpoints. The line color is loaded into the frame buffer at the corresponding pixel coordinates. Reading from the frame buffer, the video controller plots the screen pixels. This process digitizes the line into a set of discrete integer positions that, in general, only approximates the actual line path

93 What is a “pixel” Q: What is a pixel? A square or a point?
From a geometry point of view, a pixel is a point. Q: Where is (2,1)? 3 2 1 1 2 3 4 5

94 But when we think about images, a pixel is a rectangle.
Q: Where is (2,1)? A. The center of a pixel 2 1 1 2 3 4

95 Using Open-GL Files: .h, .lib, .dll
The entire folder gl is placed in the Include directory of Visual C++ The individual lib files are placed in the lib directory of Visual C++ The individual dll files are placed in C:\Windows\System32

96 Using Open-GL (2) Includes:
<windows.h> <gl/gl.h> <gl/glu.h> <gl/glut.h> <gl/glui.h> (if used) Include in order given. If you use capital letters for any file or directory, use them in your include statement also.

97 Using Open-GL (3) Changing project settings: Visual C++ 6.0
Project menu, Settings entry In Object/library modules move to the end of the line and add glui32.lib glut32.lib glu32.lib opengl32.lib (separated by spaces from last entry and each other) In Project Options, scroll down to end of box and add same set of .lib files Close Project menu and save workspace

98 Using Open-GL (3) Changing Project Settings: Visual C++ .NET 2003
Project, Properties, Linker, Command Line In the white space at the bottom, add glui32.lib glut32.lib glu32.lib opengl32.lib Close Project menu and save your solution

99 Getting Started Making Pictures
Graphics display: Entire screen (a); windows system (b); [both have usual screen coordinates, with y-axis down]; windows system [inverted coordinates] (c)

100 Example: A Rectangle moveTo(4, 4); //move to starting corner
lineTo(-2, 4); lineTo(-2, -2); lineTo(4, -2); lineTo(4, 4); //close the square

101 Drawing Aligned Rectangles
glRecti (GLint x1, GLint y1, GLint x2, GLint y2); // opposite corners; filled with current color; later rectangles are drawn on top of previous ones

102 Aspect Ratio of Aligned Rectangles
Aspect ratio = width/height

103 Area Fill Different types of Polygons Simple Convex Simple Concave
Non-simple : self-intersecting With holes Convex Concave Self-intersecting

104 Polygons A polygon is a many-sided planar figure composed of vertices and edges. A polygon is bounded (finite area) and closed (includes boundary). Vertices are represented by points (x,y). Edges are represented as line segments which connect two points, (x1,y1) and (x2,y2). P = { (xi , yi ) } i=1,n E3 (x3,y3) E2 (x2,y2) E1 (x1,y1)

105 Polygons: Complex vs Simple
A simple polygon – edges only intersect a vertices, no coincident vertices A complex polygon – edges intersect and/or coincident vertices A B C E F G H C E D A B B A C A B,E C D F

106 Simple Polygons: Convex and Concave
Convex Polygon - For any two points P1, P2 inside the polygon, all points on the line segment which connects P1 and P2 are inside the polygon. All points P = uP1 + (1-u)P2, u in [0,1] are inside the polygon provided that P1 and P2 are inside the polygon. Concave Polygon - A polygon which is not convex.

107 Filling Concave Polygons
Fill the polygon 1 scanline at a time Determine which pixels on each scanline are inside the polygon and set those pixels to the appropriate value. Look only for those pixels at which changes occur.

108 Scan-Line Algorithm For each scan-line: -Find the intersections of the scan line with all edges of the polygon. -Sort the intersections by increasing x-coordinate. -Fill in all pixels between pairs of intersections. Possible Problems 1. Horizontal edges ==> Ignore 2. Vertices ==> If local max or min, then count twice, else count once. (This is implemented by shortening one edge by one pixel.) 3. Calculating intersections is slow. 2 4 6 8 For scan-line number 7 the sorted list of x-coordinates is (1,3,7,9) Therefore fill pixels with x-coordinates 1-3 and 7-9.

109 Edge Coherence -Not all polygon edges intersect each scanline. -Many edges intersected by scanline yk will also be intersected by yk+1 -Nearby pixels on given edge and span have similar coordinates, attributes and equation components

110 “scan-line aligned” trapezoids
-break up problem into simpler ”scan-line aligned” trapezoids - only two side edges: left, right T5 T6 T4 T3 right edge of T2 T2 left edge of T2 T1

111 Edge Table -sort vertices by y coordinate building Edge Table (ET) ET ymax not needed D C yD,C F T5 yG GF G T6 T4 yE EF ED E T3 yB BC B H T2 yH HG T1 yA AH AB A y=0

112 Scan AET with AEL D C F G B H A
-scan up through ET and track Active Edge Table (AET). At each scan-line fill in span of pixel for each edge pair. ET AETk (k = 1..10) ymax D C [(DE,BC)] F [(GF,EF), (DE,BC)] T5 yG GF G T6 [(HG,EF), (DE,BC)] T4 yE EF ED E T3 [(HG,BC)] yB BC B H T2 [(HG,AB)] yH HG T1 [(AH,AB)] yA AH AB A y=0

113 How do we fill trapezoids?
-Need to generate pixel coordinates of left and right edges of edge pair-- (lx,ly) and (rx,ry) -Need to fill pixel in span between current G y3 y2 H y1 y0 lx1, lx2, lx3 rx1 rx2 rx3 rx0, lx0

114 B Rasterize Edge … yk+1 yk yk=0 xk xk+1 … xk=0 From y=mx + b we have:
xk+1= xk+ 1 / m = xk+ Δx / Δy fractions yuck! -So split into integer and fraction and look at fraction numerator: xk+1= xk+ (Δx div Δy) + (Δx mod Δy) / Δy integer fraction xk = xik + xnk / Δ y, xi is integer component xn is x’s fraction’s numerator B yk+1 yk yk=0 xk xk+1 xk=0

115 Rasterize Edge Δx=7 Δy=3 Δx div Δy= 2 Δx mod Δy= 1 B=(13,4)
Base Case: xi = x0 , xn = 0 Iteration Case: xi is integer component, xn is fraction numerator xi := xi + (Δx div Δy) xn := xn + (Δx mod Δy) if xn ≥ Δy then xi := xi + 1 xn := xn – Δy endif Warning: H&B doesn’t handle Δx > Δy Δx=7 Δy=3 Δx div Δy= 2 Δx mod Δy= 1 B=(13,4) k y xi xn x (=xi+xn/Δy) /3 /3 /3 (10,3) (8,2) A=(6,1)

116 Splitting Concave Polygons – Concept
Concave can be split into Convex Polygons – this simplifies Edge Table and Active Edge Table in polygon rasterizer (only one scan-line trapezoid is active at a time!) Identify: look for interior angle greater than 180º - cross product signs will differ i j k x0 y0 z0 x1 y1 z1 (E1 X E2)z > 0 E5 (E2 X E3)z > 0 E4 (E3 X E4)z < 0 E6 E3 (E4 X E5)z < 0 (E5 X E6)z > 0 E2 E1 (E6 X E1)z > 0 Y X

117 Splitting Concave Polygons
Edge Vector: Compute Edge Vectors (Ek = Vk+1- Vk) traversing polygon counter-clockwise. If cross product of consecutive edge is negative, then split polygon along the embedding line of first edge in cross product Rotational: put successive edges on x-axis. If next edge has negative x coordinate split polygon on x-axis. X Y E1 E2 E3 E4 E5 E6 E5 E4 E6 E3 X Y X Y E2 X Y E1

118 Splitting Convex Polygon to Triangles
Triangle rasterizing is even simpler. Only 3 edges to “sort”. Only fill in 1 or 2 scan-line aligned triangles. Pretty much what all graphics hardware does. Pick 3 vertices of convex polygon. Split off triangle. Repeat with remaining vertices. Continue until only 3 vertices left. A) B) C) v2 v2 v2 v3 v3 v1 v1 v3 v1 v4 v4 v4 v0 v0 v0

119 Objects using solid modeling
Building Realistic... Objects using solid modeling To make a realistic image... ...objects must be able to be combined One of the most popular ways for combining objects... ...is with Boolean set operators ...using union, difference, and intersection Boolean set operators are 3D equivalents of simple 2D

120 Objects using solid modeling
Building Realistic... Objects using solid modeling Two intersecting cubes Intersection operation Difference operation: Bottom-Top Difference operation: Top-Bottom Union operation

121 Objects using solid modeling
Building Realistic... Objects using solid modeling Using ordinary Boolean set operators, not all intersections form solid objects... ...they may instead form a plane, a line... Two intersecting cubes producing a solid Two intersecting cubes producing a plane Two intersecting cubes producing a line

122 Objects using solid modeling
Building Realistic... Objects using solid modeling • Or...they may instead form a point or be null Two intersecting cubes producing a point Two cubes producing a null set

123 Objects using solid modeling
Building Realistic... Objects using solid modeling Using regularized Boolean set operators, only solid objects or null sets are formed... ...let's look at the same set of examples using regularized operators: Two intersecting cubes producing a solid Two intersecting cubes producing a null set Two intersecting cubes producing a null set

124 Objects using solid modeling
Building Realistic... Objects using solid modeling Two intersecting cubes again producing a null set Two cubes producing a null set

125 Objects using solid modeling
Building Realistic... Objects using solid modeling Solid objects created with sweeps can be manipulated using Boolean set operations... ...by first converting the objects into boundary representations, spatial- partitioning representations, or constructive solid geometry Two simple sweeps of 2D objects (triangles) How these objects would look when overlapping The result of a union operation; it can no longer be thought of as a simple sweep

126 Data structure 2 basic spatial data models exist
vector: based on geometry of points lines Polygons raster: based on geometry of grid cells (images, bitmaps, DEMs)

127 Vector Data Model Points: represent discrete point features
each point location has a record in the table airports are point features each point is stored as a coordinate pair

128 Raster Data Model origin is set explicitly cell size is always known
cell references (row/column locations) are known cell values are referenced to row/column location values represent numerical phenomena or index codes for non-numerical phenomena

129 Raster Data Model A few different types of raster data
digital orthophoto digital elevation model (DEM)

130 Normal equation of plane
The normal is perpendicular to any line in the plane (p,q,r) n (x,y,z) R A (a,b,c) Cartesian equation of a plane o

131 6.1 Definitions Definition 1: A nonzero vector x is an eigenvector (or characteristic vector) of a square matrix A if there exists a scalar λ such that Ax = λx. Then λ is an eigenvalue (or characteristic value) of A. Note: The zero vector can not be an eigenvector even though A0 = λ0. But λ = 0 can be an eigenvalue. Example:

132 6.3 Eigenvectors Example 1 (cont.):
To each distinct eigenvalue of a matrix A there will correspond at least one eigenvector which can be found by solving the appropriate set of homogenous equations. If λi is an eigenvalue then the corresponding eigenvector xi is the solution of (A – λiI)xi = 0 Example 1 (cont.):

133 OpenGL Fill-Area Functions 1
glBegin(GL_POLYGON); // Specify what to draw, // a polygon // Geometric info via vertices: glVertex*(); // 1 glVertex*(); // // ... glEnd; glVertex[234][isfd] [234]: 2D, 3D, 4D [isfd]: integer, short, float, double For instance: glVertex2i(100, 25); H&B 4-4:81-82

134 OpenGL Fill-Area Functions 2
3 4 GL_POLYGON: convex polygon Concave polygons give unpredictable results. 1 5 3 2 5 4 1 H&B 4-8:94-99

135 OpenGL Fill-Area Functions 3
GL_TRIANGLES: sequence of triangles GL_TRIANGLE_STRIP: GL_TRIANGLE_FAN: 3 6 5 9 1 7 3 4 7 2 8 5 1 2 4 8 6 6 5 1 4 H&B 4-8:94-99 2 3

136 OpenGL Fill-Area Functions 4
GL_QUADS: sequence of quadrilaterals GL_QUAD_STRIP: strip of quadrilaterals 4 3 8 11 7 10 1 12 5 6 2 9 4 6 8 2 3 1 7 H&B 4-8:94-99 5

137 OpenGL Display lists 2 H&B 4-15 111-113
// Straightforward void drawRobot(); { // lots of glBegin, glVertex, glEnd calls } void drawScene(); { drawRobot(); glTranslate3f(1.0, 0.0, 0.0); } H&B

138 OpenGL Display lists 3 H&B 4-15 111-113
void drawRobot(); { // lots of glBegin, glVertex, glEnd calls } int rl_id; void init(); { rl_id = glGenLists(1); // get id for list glNewList(rl_id, GL_COMPILE); // create new list drawRobot(); // draw your object glEndList(); } // end of list void drawScene(); { glCallList(rl_id); // draw list glTranslate3f(1.0, 0.0, 0.0); glCallList(rl_id); // and again } H&B

139 OpenGL Display lists 4 H&B 4-15 111-113
First, get an id. Either some fixed constant, or get a guaranteed unique one: rl_id = glGenLists(1); // get id for list Next, create a display list with this id: glNewList(rl_id, GL_COMPILE); // create new list drawing commands; // draw something glEndList(); // end of list Finally, “replay” the list. Change the list only when the scene is changed: glCallList(rl_id); // draw list H&B

140 Vertex Arrays OpenGL provides a facility called vertex arrays that allows us to store array data in the implementation Six types of arrays supported Vertices Colors Color indices Normals Texture coordinates Edge flags We will need only colors and vertices

141 Initialization Using the same color and vertex data, first we enable
glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); Identify location of arrays glVertexPointer(3, GL_FLOAT, 0, vertices); glColorPointer(3, GL_FLOAT, 0, colors); data array data contiguous 3d arrays stored as floats

142 Sample code for drawing object
glDrawArrays(GL_TRIANGLES, 0, 3); uniform vec3 triangleColor; out vec4 outColor; void main() { outColor = vec4(triangleColor, 1.0); }

143 Definition: Bitmap Bitmapped images:
Photographs, computer paintings, videos Specific number of dots (or pixels) across and down (160 x 120, 800 x 600, 1400 x 1050) Dots spread out or squeeze together as image size changes Number of dots remains the same Photoshop, Paint, QuickTime

144 “Maps” of Dots

145 “Maps” of Dots

146 “Maps” of Dots

147 Bitmap Examples

148 Making Bitmaps Bigger 100% (900 x 983 dpi) 200% 500%

149 Raster Displays Images are composed of arrays of pixels displayed on a raster device. Two main ways to create images: Scan and digitize an existing image. Compute a value for each pixel procedurally. The result may be stored as a pixmap, a rectangular array of color values.

150 Manipulating Pixmaps Pixmaps may be stored in regular memory or in the frame buffer (off-screen or on-screen). Rendering operations that draw into the frame buffer change the particular pixmap that is visible on the display.

151 Pixmap Operations: Copying

152 Pixmap Operations: Copying
glReadPixels () reads a portion of the frame buffer into memory. glCopyPixels() copies a region in one part of the frame buffer into another region of the frame buffer. glDrawPixels() draws a given pixmap into the frame buffer. We can also copy a pixmap from memory to memory.

153 Scaling Pixmaps glPixelZoom(float sx, float sy);
Sets scale factors in x and y Any floating point values are allowed for sx and sy, even negative ones. The default values are 1.0. The scaling takes place about the current raster position, pt. Scale factors are applied to the image drawn from a pixmap, not to the pixmap.

154 Scaling Pixmaps (2) Roughly, the pixel in row r and column c of the pixmap will be drawn as a rectangle of width sx and height sy screen pixels, with lower left corner at screen pixel (pt.x + sx * r, pt.y + sy * c). More precisely, any screen pixels whose centers lie in this rectangle are drawn in the color of this pixmap pixel.

155 CG Hardware System Overview
Components of a CG system Raster displays Display devices: crt, etc. CG system architectures GPU Frame buffers Addressability and resolution “Scanning out” the image Color look up tables Input devices

156 Double Buffer Use two system buffers instead of one
A process can transfer data to or from one buffer while the operating system empties or fills the other buffer

157 Bitmap Character raster
Each character represented (stored) as a 2-D array – Each element corresponds to a pixel in a rectangular “character cell” – Simplest: each element is a bit (1=pixel on, 0=pixel off)

158 Stroke Character outline
Each character represented (stored) as a series of line segments – sometimes as more complex primitives Parameters needed to draw each stroke – endpoint coordinates for line segments

159 Characters Characteristics
Characteristics of Bitmapped Characters Each character in set requires same amount of memory to store Characters can only be scaled by integer scaling factors "Blocky" appearance Difficult to rotate characters by arbitrary angles Fast (BitBLT) Characteristics of Stroked Characters Number of stokes (storage space) depends on complexity of character Each stroke must be scan converted more time to display Easily scaled and rotated arbitrarily – just transform each stroke

160 References 1)Coordinate frame References 2) Line drawing algorithm 3)Circle and Ellipse drawing algorithm

161 References Pixel Addressing references Area fill methods Plane Equations

162 References OpenGL Area Fill Vertex array Pixel array


Download ppt "Computer Graphics Graphics Output Primitives By : Dhaval Shah."

Similar presentations


Ads by Google