39
© Machiraju/Zhang/Möller © Machiraju/Zhang/Möller Line Drawing Foundations of Computer Graphics Torsten Möller

Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Line Drawing

Foundations of Computer Graphics

Torsten Möller

Page 2: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Rendering Pipeline

Hardware

Modelling Transform Visibility

Illumination +Shading

ColorPerception,Interaction

Texture/Realism

Page 3: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Reading

Angel – Chapter 8.8-8.10

Page 4: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Today

Recap - the graphics pipeline Rasterizing lines

brute-force DDA Bresenham

Rasterizing quadrics Anti-aliasing

Page 5: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

From the modeling stage to image formation Pipelined approach helps increase system

throughput Throughput: rate at which data flows through

the system In a pipeline, data processing in subsequent

blocks can be done in parallel Especially effective when the same sequence of

operations are to be performed on large quantity of data – true in graphics

The graphics pipeline

Page 6: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Vertex processor Per-vertex operations (vertices define

objects/primitives) Two main functionalities:

Coordinate transformations Color computation at each vertex (shading models)

Objects/geometry first defined in their own coordinate systems, then transformed into a world space — modeling transformation

Then objects are transformed from world space into the camera coordinate system — viewing transformation and projection transformation

Page 7: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Clipping and primitive assembly

Model a finite field of vision Remove objects outside a

finite clipping volume Need to be done on a primi- tive by primitive basis,

not on vertices Output is a set of primitives

whose projections can appear in the image

Page 8: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Rasterizer The rasterizer converts a primitive into a set

of fragments A fragment stores per pixel information for its

associated primitive, later used to determine whether the fragment contributes to pixel color and to compute the pixel color Raster/pixel location (in the frame buffer) Depth, e.g., to determine whether this fragment

“survives” Interpolated attributes, e.g., color and texture

coordinate, from the previous stages Alpha value (for blending) Window ID, etc.

Page 9: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Fragment processing Performs per-fragment operations Main function is to compute the color at a pixel,

using information stored in the fragments, e.g., depth, alpha, texture coordinates; can also add in fog and other effects

A (programmable) fragment shader is a program that performs the processing which replaces the OpenGL fixed functionality, e.g., simply using interpolated attributes

Similar to a vertex shader (per-vertex operations) Shader programs typically have limited

instruction set

Page 10: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Drawing or rasterization of primitives first. Consider lines and polygons.

Where are we at now?

Page 11: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Line drawing in OpenGL

glColor3f(1.0, 1.0, 1.0);

glLineWidth(2.0);

glBegin(GL_LINES);

glVertex2f(0.5, 0.5);

glVertex2f(0.5, 0.5);

glVertex2f(0.5, 0.5);

glVertex2f(0.5, 0.5);

glEnd();

glBegin(GL_LINE_STRIP);

glBegin(GL_LINE_LOOP);

There is no GL_CIRCLE

Page 12: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Assumptions Transformation, clipping, projection

already done Primitives to rasterize are actually inside

the screen Work with 2D screen coordinates with

square pixels

(0, 0)

(N, M)

Page 13: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Primitives

Polygons explicit curves

lines, quadrics (conic sections) parametric curves/surfaces

Curves, Surfaces implicit description, e.g. x2+y2-1=0 Misc

particle systems/points, fractals

Page 14: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Explicit Descriptions

Given - an explicit equationy = f(x)

cannot get multiple values of y no infinite slope (vertical line) axis dependent (y depends on x) specify one variable and resolve for

the other

Page 15: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Lines

Given - an explicit line equationy = mx+n

Which pixels to set?http://bert.stuy.edu/pbrooks/graphics/demos/BresenhamDemo.htm

Page 16: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Scan Converting Lines Compute the coordinates of pixels that

lies on or near an ideal, infinitely thin line imposed on a 2D raster grid

Assumptions line will be 1 pixel thick and will approximate

an infinitely fine line pixels represented as disjoint circles, centred

on a grid (unless specified otherwise) integer coordinates of endpoints pixel on or off (2 states)

Page 17: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Scan Converting Lines (2) Desirable properties:

constant brightness (irrespective of length or orientation)

1 pixel per column (-1 <= slope <= 1), 1 pixel per row (slope > +/- 1)

as close to the ideal/as straight as possible allow control of pen, line and endpoint

styles drawn as smoothly as possible

(anti-aliasing) drawn as rapidly as possible

Page 18: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Lines - solutions

• Brute force– for each column xi compute the row

yi=round(mxi+n)

– very costly

• incremental! (Newtons idea)– we know yi - find yi+1

– yi+1= round(yi +m)

Page 19: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Basic – Brute force Find slope m = rise/run or y = mx + b increment x by 1 (xi); calculate yi = mxi + b pixel (xi, round(yi)) turned on simple, but inefficient:

floating point multiplication addition round

Page 20: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Incremental - DDA multiplication can

be eliminated:

called digital differential analyzer (DDA) - after mechanical device that solves differential equations by numerical methods

Drawbacks: floating point values (m,y) round operation special cases could be done more quickly

Page 21: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Lines - Bresenham Special case of Midpoint Line Algorithm uses only integer arithmetic & no

rounding idea is to provide the best-fit

approximation to a true line by minimizing the error (distance) to the true line

slope (rest is done with reflection)

Page 22: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller

Explicit line equation:

Implicit line equation:

Main idea - call on decision variable:

Lines - Bresenham (2)

Page 23: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

if di < 0, pick O, and:

if di > 0, pick NO, and:

if di = 0, pick either one, but consistently

Lines - Bresenham (3)

Page 24: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller

Lines - Bresenham (4)

Bresenham [Wikipedia]

dx=6dy=3

Page 25: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Lines - Bresenham (5)

Additional issues: - covers 0 <= m <= 1 case, other 7 octants: exchange x/y, mirror x and y Endpoint order: we want P0 to P1 to

look exactly the same as P1 to P0

Starting at edge of a clip rectangle: we must use the midpoint test result rather than computing intersections analytically if we are to ensure the correct sequence of pixels

Page 26: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Lines - Bresenham (4)

Page 27: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Quadrics - Circles

Circles, ellipsoids, etc.

divide in 8 quadrants What is special

about 8-foldsymmetry?

Page 28: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Quadrics - Circles (2) Midpoint Circle Algorithm

trace out arc in 2nd octant and draw the rest by symmetry

choose between E and SE pixel at each stage

Page 29: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Quadrics - Circles (3)

incremental - now have higher order

higher order difference system

Page 30: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Quadrics - Ellipses

Can a midpoint algorithm be developed for ellipses? What are the important differences from circles?

The result is Da Silva's algorithm (the idea is illustrated below):

Page 31: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Lines - Anti-Aliasing

A continuous (zero-width) line is “invisible” in a discrete grid.

Give line thickness - How? Signal Processing Approach:

line is high-frequency want to avoid aliasing what to do first?

Page 32: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Lines - Anti-Aliasing (2)

Bandlimit signal - how? Convolve with smoothing filter -

which?

Page 33: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Lines - Anti-Aliasing (3)

Box filter not all that good convolve it again - why? Then resample!

Page 34: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Lines - Anti-Aliasing (4)

Other alternatives? Can we improve this algorithm?

Page 35: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

General Anti-Aliasing: super-sampling

simple but expensive simply sample n times within a

pixel and average!

Page 36: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

remember …

Page 37: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Möller© Machiraju/Möller

Aliasing example

Page 38: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Möller© Machiraju/Möller

Point vs. supersampling

Page 39: Line Drawing - vda.univie.ac.atvda.univie.ac.at/Teaching/Graphics/19s/LectureNotes/14_lines_18w.pdf · lies on or near an ideal, infinitely thin line imposed on a 2D raster grid Assumptions

© Machiraju/Zhang/Möller© Machiraju/Zhang/Möller

Xiaolin Wu's line algorithm

Multi-sample AA: https://www.youtube.com/watch?v=hqi0114mwtY