260 likes | 422 Views
CGMB214: Introduction to Computer Graphics. Topic 4 Graphics Output Primitives (I). Objectives. To be able to define the term output primitives To be able to define and create points and lines To understand line-drawing algorithms and line function. Output Primitives.
E N D
CGMB214: Introduction to Computer Graphics Topic 4 Graphics Output Primitives (I)
Objectives To be able to define the term output primitives To be able to define and create points and lines To understand line-drawing algorithms and line function
Output Primitives “where scene been represented by basic geometric structure which map into rectangular grid of pixel” In short: Basic objects that create computer drawn pictures (points, lines, circles etc) Pixel: little rectangles with same size and shape at each screen points Image: is a rectangular grid or array of pixels.
Output Primitives • The simplest form of output primitives are: • Points • Straight Lines • These primitives are defines in the graphics library (high level) • Learn the device-level algorithm for displaying 2D output primitives
Coordinate Reference Frames • Screen coordinate • Pixel position in the frame buffer • Absolute coordinate • The actual position within the coordinate in use • Relative coordinate • Coordinate position as an offset from the last position that was referenced (current position)
Points Accomplished by converting a single coordinate position by an application program into appropriate operations for output device in use (i.e. monitor) Electron beam is turned on to illuminate the screen phosphor depending on the display technology (Random-scan/Raster-scan)
OpenGL Point Function Default colour for primitives is white and the default point size is equal to the size of 1 pixel. Use glVertex*(); to state the coordinate value of a single position, where (*) determine the spatial dimension and data type of the coordinate value. glVertex must be placed in between glBegin (**); and glEnd();, where (**) refers to the kind of output primitive to be displayed
OpenGL Point Function glBegin(GL_POINTS); glVertex2i(40,50); glEnd(); The type of output primitive to be displayed The coordinate of the output primitive created Two dimensional, Integer data type
Lines Most common 2D primitive - done 100s or 1000s of times each frame Even 3D wire-frames are eventually 2D lines! Optimized algorithms contain numerous tricks/techniques that help in designing more advanced algorithms
Lines Converting a line segment Consider a line segment with endpoint (0,4) and (21,21) . Which pixel do we select?
Lines Naïve line conversion The naïve method select every pixel that the mathematical line segment intercept.
Lines • Requirements: • Must compute integer coordinates of pixels which lie on or near a line or circle. • Pixel level algorithms are invoked hundreds or thousands of times when an image is created or modified – must be fast! • Lines must create visually satisfactory images. • Lines should appear straight • Lines should terminate accurately • Lines should have constant density • Line algorithm should always be defined.
Line Equation To create a line, an equation is used y = mx + c where: m is slope c is y intercept For example, given 2 points P1 (5,2) and P2 (10,12)
Line Equation In raster scan system, it will check for every pixel → need to calculate for all y for x = 5 until x = 10 y Plot P1 and P2 Calculate m and c Plot points from P1 to P2, based on x = 5 until x = 10 12 x = 6, y = 4 x = 7, y = 6 x = 8, y = 8 x = 9, y = 10 0 As you can see, the points are not connected, therefore the nearest pixel to the point is taken -8 x 0 5 10
Digital Deferential Algorithm A scan conversion line algorithm based on calculating either △x or△y, where △x = x2 – x1 and △y = y2 - y1 m = △y/ △x For line with positive slope greater than1 For line with positive slope smaller than1
Digital Deferential Algorithm For example, draw the line between P1(5,2) and P2(10,5) using DDA △x = 5, △y= 3, m = 3/5 m is positive and smaller than 1, therefore use k starts at 0 y0 = 2 y0 will take the value of y for P1
Digital Deferential Algorithm Since we are moving on y axis, we are going to move 5 steps in x axis (△x = 5) y1 is the point of y at x1 The value of y1 is then rounded to the nearest pixel position.
Digital Deferential Algorithm Move x, 1 step towards P2, plot y based on the rounded value of y1 Repeat the process until you reach P2 Plot P1 and P2 3 y 3 7 6 4 5 4 3 4 2 5 6 7 8 9 10 11 x
Digital Deferential Algorithm • Advantages: • Faster than using y = mx +c • Eliminates multiplication (mx) by using screen characteristics, i.e. incrementing pixel • Disadvantages: • Floating point arithmetic is slower than integer arithmetic • Rounding down take time • Long lines can drift from the true line due to floating point round-off errors
Bresenham’s Line Algorithm Input the two line endpoints and store the left endpoint in (x0,y0) Load (x0,y0) into the frame buffer; that is, plot the first point. Calculate constants x, y, 2y, and 2y - 2x, and obtain the starting value for the decision parameter as P0 = 2y - x At each Xk along the line, starting at k = 0, perform the following test: If Pk < 0, the next point to plot is (xk + 1, yk) and Pk+1 = Pk + 2y Otherwise, the next point to plot is (xk + 1, yk + 1) and Pk+1 = Pk + 2y - 2x Repeat step 4, x times.
Bresenham’s Line Algorithm Given P1(5,2) and P2(10,5). Draw a line from P1 to P2 using Bresenham’s algorithm: △x = 5, △y= 3 P0 = 2△y-△x = 2(3) – 5 = 1 If Pk > 0, the next point to plot is (Xk + 1, Yk + 1) Therefore, plot (6,3)
Bresenham’s Line Algorithm △x=5, △y=3 P0=2△y-△x=2(3)-5=1 P0+1=P0+2△y-2△x=1+2(3)-2(5) =-3 P1+1=P1+2△y=-3+2(3) = 3 P2+1=P2+6-10=-1 P3+1=P3+6=5 P0 > 1, therefore (Xk + 1, Yk + 1) And use Pk+1 = Pk + 2y - 2y Plot (6,3) Plot (7,3) P0 < 1, therefore (xk + 1, yk) Plot (8,4) And use Pk+1 = Pk + 2y Plot (9,4) Stop when the value of Pk is the same as the value of y for P2
Bresenham’s Line Algorithm △x=5, △y=3 P0=2△y-△x=2(3)-5=1 P0+1=P0+2△y-2△x=1+2(3)-2(5) =-3 P1+1=P1+2△y=-3+2(3) = 3 P2+1=P2+6-10=-1 P3+1=P3+6=5 y 7 Plot (6,3) 6 5 4 Plot (7,3) 3 2 Plot (8,4) 5 6 7 8 9 10 11 x Plot (9,4)
Bresenham’s Line Algorithm • Advantages • Accurate and efficient – incremental integer calculations • Can be adapted to draw circles and curves. • Generalization: • Increment (x or y) depending on slope (m) starting endpoints (left or right) special case (y=0 or x =0)
16 15 14 13 12 11 10 9 6 11 7 8 9 10 12 13 14 15 16 17 Optimisation Speed can be increased even more by detecting cycles in the decision variable. These cycles correspond to a repeated pattern of pixel choices. The pattern is saved and if a cycle is detected it is repeated without recalculating.