Okay, you have learned … 2D Graphics Pipeline

„ OpenGL Graphics processing consists of many stages: „ Viewport and World Window setup

main() { glViewport(0,0,300,200); Object Modeling Object glMatrixMode(GL_PROJECTION); World Coordinates glLoadIndentity(); Local Coordinates transformation gluOrtho2D(-1,1,-1,1); glBegin(GL_QUADS); glColor3f(1,1,0); glVertex2i(-0.5,-0.5); glVertex2i(+0.5,0); glVertex2i(+0.5,+0.5); ( page) glVertex2i(-0.5,+0.5); glEnd(); }

2D Graphics Pipeline (2) Clipping and Rasterization

„ OpenGL does these for you – no explicit OpenGL functions needed for doing clipping and rasterization Clipping window to Object Applying Object „ Clipping – Remove objects that are outside the viewport World Coordinates world window subset mapping world window

„ Rasterization (scan conversion) – Convert high Simple 2D Drawing Pipeline level object descriptions to pixel in the frame buffer

Object Display Rasterization Screen coordinates

1 2D Point Clipping 2D Line Clipping

„ Determine whether a „ Determine whether a (xmax, ymax) point (x,y) is inside or (xmax, ymax) line is inside, outside, or outside of the world partially inside window? „ If a line is partially If (xmin <= x <= xmax) inside, we need to and (ymin <= y <= ymax) (xmin, ymin) display the inside (xmin, ymin) segment then the point (x,y) is inside else the point is outside

Trivial Accept Case Trivial Reject Case

(Xmax, Ymax) „ Lines that are clearly inside „ Lines that are clearly outside the world the world window - what are p1 p1 they? window – what are they?

p2 Xmin <= P1.x, P2.x <=xmax ƒ p1.x, p2.x <= Xmin OR Ymin <=P1.y, P2.y <= Ymax (Xmin, Ymin) ƒ p1.x, p2.x >= Xmax OR p2 ƒ p1.y, p2.y <= ymin OR ƒ p1.y, p2.y >= ymax

2 Non-Trivial Cases Non-trivial case clipping

„ Lines that cannot be trivially „ Compute the line/window p2 rejected or accepted boundary edges intersection

„ One point inside, one point „ There will be four outside intersections, but only one or two are on the window p1 „ Both points are outside, but not “trivially” outside edges „ These two points are the „ Need to find the line end points of the desired segments that are inside line segment

Rasterization (Scan Conversion) Rasterization Algorithms

„ Convert high-level description „ A fundamental function to pixel colors in the frame buffer „ Determine the pixels’ colors, illuminations, textures, etc.

„ Implemented by graphics hardware

„ Rasterization algorithms

Viewport „ Lines Rasterization Transformation „ Circles

„ Triangles

„ Polygons

3 Rasterize Lines Line Drawing Algorithm (1)

„ Why learn this? 8 7 Line: (3,2) -> (9,6) „ Understand the discrete nature of computer graphics 6 5 ? „ Write pure device independent graphics 4 programs (Palm graphics) 3 2 „ Become a graphics system developer 1

0 1 2 3 4 5 6 7 8 9 10 11 12

Line Drawing Algorithm (2) Line Drawing Algorithm (3)

„ Slope-intercept line equation Given the line equation y = mx + b, and end points (x0,y0) (x1, y1) „ Y = mx + b Walk through the line: starting at (x0,y0) „ Given two end points (x0,y0), (x1, y1), If we choose the next point in the line as X = x0 + Δx how to compute m and b? Y = ?

Y = y0 + Δx* m (x1,y1) (x1,y1) m = (y1-y0) / (x1 – x0) = y0 + Δx* (dy/dx) dy = dy / dx dy (x0,y0) (x0,y0) dx dx b = y1 – m * x1

4 Line Drawing Algorithm (4) Line Drawing Algorithm (5)

(x1,y1) X = x0 Y = y0 „ How about a line like this? Illuminate pixel (x, int(Y))

X = x0 + 1 Y = y0 + 1 * m Can we still increment X by 1 at each Step? Illuminate pixel (x, int(Y)) The answer is No. Why? X = X + 1 Y = Y + 1 * m We don’t get enough samples Illuminate pixel (x, int(Y)) How to fix it ?

… Increment Y (x0, y0)

Until X == x1

Line Drawing Algorihtm (6) Line Drawing Algorithm (7)

X = x0 Y = y0 (x1,y1) Illuminate pixel (x, int(Y)) „ The above is the simplest line drawing

Y = y0 + 1 X = x0 + 1 * 1/m algorithm „ Illuminate pixel (x, int(Y)) Not very efficient „ Optimized algorithms such integer DDA Y = Y + 1 X = X + 1 /m and Bresenhan algorithm (section 8.10) Illuminate pixel (x, int(Y)) are typically used

… „ Not the focus of this course

(x0,y0) Until Y == y1

5