1 / 15

Lecture 5 Rendering lines

Lecture 5 Rendering lines. Steps of line rendering Scan-conversion for line segments A1 tutorial. Description of a line in WC A line is projected to view plane and clipped by clipping window. It becomes a line segment, with two ends within the clipping window

elga
Download Presentation

Lecture 5 Rendering lines

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 5 Rendering lines Steps of line rendering Scan-conversion for line segments A1 tutorial CP411 Computer Graphics Fall 2007 Wilfrid Laurier University

  2. Description of a line in WC • A line is projected to view plane and clipped by clipping window. It becomes a line segment, with two ends within the clipping window • Line segment is transformed to normalized coordinate system • Line segment is mapped to screen coordinates, then converted to integers in view port ordinate system of the display • A line segment in SC is a graphics output primitives. It needs to be converted to pixels in SC by scan-conversion or rasterization algorithm. i.e. to set up pixels in the frame buffer. Each pixel position (x, y) has a location in frame buffer setPixel(x, y) is to set the color value at pixel (x, y) 1. Line-Drawing Steps CP411 Computer Graphics

  3. (x2,y2) (x2,y2) (x1,y1) (x1,y1) CP411 Computer Graphics Fall 2007 Wilfrid Laurier University

  4. Scan conversion • From SC description to pixels by scan conversion • Transforming the continuous into this discrete by sampling. Can never be ideal. • The best we can do is a discrete approximation of an ideal line. CP411 Computer Graphics

  5. The Basic idea of drawing lines • Let (x0, y0) and (x1, y1) be the screen coordinates of the two end points of a line segment. x0 x1 • Step through the x-coordinate from x0 to x1. Set the pixels closest to the line. (x2,y2) (x1,y1) CP411 Computer Graphics

  6. Important line qualities • Continuous appearance • Uniform thickness and brightness • Turn on the pixels nearest the ideal line • How fast is the line generated • Preferred algorithms • Use integers rater than floating point • Use addition, subtraction rather than multiplication and division • Use iteration or recurrence • Use increment CP411 Computer Graphics

  7. Simple Line Algorithm I int x0= , y0= , x1= , y1= ; int dx = x1 - x0; int dy = y1 - y0; setPixel(x0, y0); if (dx != 0) { float m = (float) dy / (float)dx; float b = y0 - m*x0; dx = (x1 > x0) ? 1 : -1; while (x0 != x1) { x0 += dx; y0 = Math.round(m*x0 + b); setPixel(x0, y0); } } y = m x + b m = (y1-y0)/(x1-x0) x = x0, x0+1, …, x1 y = mx + b CP411 Computer Graphics

  8. The above algorithm only works well when the angle between the line and the x-axis, , is less than 45o • The left figure is resulted for a line with  = 70o . The pixels on the line are too sparsely set. A more desirable one is shown on the right.  = 70o CP411 Computer Graphics

  9. Algorithm I improvement int x0 = , y0=,x1= , y1=..; int dx = x1 - x0; int dy = y1 - y0; setPixel(x0, y0); if (Math.abs(dx) > Math.abs(dy)) { float m = (float) dy / (float) dx; float b = y0 - m*x0; dx = (dx < 0) ? -1 : 1; while (x0 != x1) { x0 += dx; setPixel(x0, Math.round(m*x0 + b)); } } else if (dy != 0) { float m = (float) dx / (float) dy; float b = x0 - m*y0; dy = (dy < 0) ? -1 : 1; while (y0 != y1) { y0 += dy; setPixel(Math.round(m*y0 + b), y0); }} CP411 Computer Graphics

  10. d y dx=1 Algorithm II – by increment, remove multiplication int x0 = …, y0 = … , x1 = …, y1 = …; int x = x0; float y = y0, dy = (y1 – y0) / (x1 – x0); for (x = x0; x  x1; ++x) { setPixel( x, round(y) ); y = y + dy; } Round operation is slow as well CP411 Computer Graphics

  11. Algorithm III • error is the distance between the centre of a pixel and the line) int x0 = …, y0 = … , x 1 …, y1 = …; int x, y = y0; float error = 0.0, dy = (y1 – y0) / (x1 – x0); for (x = x0; x  x1; ++x) { setPixel( x, y ); error = error + dy; if (error > 0.5) { ++y; error = error - 1.0; } } CP411 Computer Graphics

  12. Algorithm III _ improvement • The floating point arithmetic can be entirely eliminated by scaling up error and y by 2(x2-x1) int x0 = …, x =1 …, y0 = … , y1 = …; int x, y = y0, dx = 2*(x1 – x0), dy = 2*(y1 – y0), error = 0; for (x = x1; x  x1; ++x) { setPixel( x, y ); error = error + dy; if (error > dx/2) { ++y; error = error - dx; } } CP411 Computer Graphics

  13. Algorithm III _improvement • The program runs even faster if we offset error by dx/2 • int x0 = …, x1 = …, y0 = … , y1 = …; • int x, y = y0, dx = 2*(x1 – x0), dy = 2*(y1 – y0), error = -dx / 2; • for (x = x0; x  x1; ++x) { • setPixel( x, y ); • error = error + dy; • if (error > 0) { ++y; error = error - dx; } • } CP411 Computer Graphics

  14. Algorithm V The Bresenham’s line drawing algorithm Combine the operations error = error + dy and error = error – dx into one when error > 0; int x0 = …, x1 = …, y0 = … , y1 = …; int x, y = y0, dx = 2*(x1 – x0), dy = 2*(y1 – y0), error = -dx / 2 - dy , dymx = dy - dx; for (x = x1; x  x2; ++x) { setPixel( x, y ); if (error < 0) error = error + dy; else { ++y; error = error + dymx; } } CP411 Computer Graphics

  15. void lineBres (int x0, int y0, intxEnd, intyEnd) { intdx = fabs (xEnd - x0), dy = fabs(yEnd - y0); int p = 2 * dy - dx; inttwoDy = 2 * dy, twoDyMinusDx = 2 * (dy - dx); int x, y; /* Determine which endpoint to use as start position. */ if (x0 > xEnd) { x = xEnd; y = yEnd; xEnd = x0; } else { x = x0; y = y0; } setPixel (x, y); while (x < xEnd) { x++; if (p < 0) p += twoDy; else { y++; p += twoDyMinusDx; } setPixel (x, y); } } CP411 Computer Graphics

More Related