topic 4 graphics output primitives (i) cgmb214: introduction to computer graphics

26
TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

Upload: austen-king

Post on 03-Jan-2016

241 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

TOPIC 4GRAPHICS OUTPUT PRIMITIVES ( I )

CGMB214: Introduction to Computer Graphics

Page 2: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

Objectives

To be able to define the term output primitivesTo be able to define and create points and linesTo understand line-drawing algorithms and line

function

Page 3: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

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.

Page 4: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

Output Primitives

The simplest form of output primitives are:PointsStraight Lines

These primitives are defines in the graphics library (high level)

Learn the device-level algorithm for displaying 2D output primitives

Page 5: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

Coordinate Reference Frames

Screen coordinate Pixel position in the frame buffer

Absolute coordinate The actual position within the coordinate

in useRelative coordinate

Coordinate position as an offset from the last position that was referenced (current position)

Page 6: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

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)

Page 7: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

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

Page 8: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

OpenGL Point Function

glBegin(GL_POINTS);glVertex2i(40,50);

glEnd();

The type of output

primitive to be displayed

Two dimensional, Integer data

type

The coordinate of the output primitive created

Page 9: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

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

Page 10: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

Lines

Converting a line segment

Consider a line segment with endpoint (0,4) and (21,21) . Which pixel do we select?

Page 11: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

Lines

Naïve line conversion

The naïve method select every pixel that the mathematical line segment intercept.

Page 12: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

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.

Page 13: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

Lines

Line Drawing Algorithm

Line EquationDDA (Digital Differential Algorithm)

Bresenham’s Line

Algorithm

Page 14: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

Line Equation

To create a line, an equation is usedy = mx + c

where: m is slopec is y intercept

For example, given 2 points P1 (5,2) and P2 (10,12)

2

5

10

510

212

m 8)10(212 mxyc

Page 15: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

Line Equation

In raster scan system, it will check for every pixel → need to calculate for all y for x = 5 until x = 10

-8

0

12

0 5 10

1. Plot P1 and P22. Calculate m and c3. Plot points from P1

to P2, based on x = 5 until x = 10

x = 6, y = 4x = 7, y = 6x = 8, y = 8x = 9, y = 10

As you can see, the points are not connected, therefore the nearest pixel to the point is taken

y

x

Page 16: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

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/ △xFor line with positive slope greater than1

For line with positive slope smaller than1

mxx kk

11

myy kk 1

Page 17: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

Digital Deferential Algorithm

For example, draw the line between P1(5,2) and P2(10,5) using DDA

△x = 5, △y= 3, m = 3/5m is positive and smaller than 1, therefore

use

k starts at 0y0 = 2

myy kk 1

y0 will take the value of y for P1

Page 18: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

Digital Deferential Algorithm

0.55

34.4

5

3

4.45

38.3

5

3

8.35

32.3

5

3

2.35

36.2

5

3

6.25

32

5

3

414

313

212

111

010

yy

yy

yy

yy

yy1.Since we are moving on y axis, we are going to move 5 steps in x axis (△x = 5)

2.y1 is the point of y at x1

3.The value of y1 is then rounded to the nearest pixel position.

Page 19: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

Digital Deferential Algorithm

0.55

34.4

5

3

4.45

38.3

5

3

8.35

32.3

5

3

2.35

36.2

5

3

6.25

32

5

3

414

313

212

111

010

yy

yy

yy

yy

yy

7

6

5

4

3

2

5 6 7 8 9 10

11

Plot P1 and P2Move x, 1 step towards P2, plot y based on the rounded

value of y1

3

x

y

Repeat the process until you reach P2

3

4

4

Page 20: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

Digital Deferential Algorithm

Advantages:Faster than using y = mx +cEliminates 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

Page 21: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

Bresenham’s Line Algorithm

1. Input the two line endpoints and store the left endpoint in (x0,y0)

2. Load (x0,y0) into the frame buffer; that is, plot the first point.

3. Calculate constants x, y, 2y, and 2y - 2x, and obtain the starting value for the decision parameter as

P0 = 2y - x

4. At each Xk along the line, starting at k = 0, perform the following test:

5. If Pk < 0, the next point to plot is (xk + 1, yk) and

Pk+1 = Pk + 2y

6. Otherwise, the next point to plot is (xk + 1, yk + 1) and

Pk+1 = Pk + 2y - 2x

7. Repeat step 4, x times.

Page 22: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

Bresenham’s Line Algorithm

Given P1(5,2) and P2(10,5). Draw a line from P1 to P2 using Bresenham’s algorithm:

If Pk > 0, the next point to plot is (Xk + 1, Yk +

1) Therefore, plot

(6,3)

1. △x = 5, △y= 3

2. P0 = 2△y-△x = 2(3) – 5 = 1

Page 23: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

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

P0 < 1, therefore (xk +

1, yk) And usePk+1 = Pk + 2yStop when

the value of Pk is the same as the value of y for P2

Plot (6,3)

Plot (7,3)

Plot (8,4)

Plot (9,4)

Page 24: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

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

Plot (6,3)

Plot (7,3)

Plot (8,4)

Plot (9,4)

765432

5 6 7 8 9 10

11

x

y

Page 25: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

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)

Page 26: TOPIC 4 GRAPHICS OUTPUT PRIMITIVES (I) CGMB214: Introduction to Computer Graphics

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.

11 12 13 14 15 16 17

9

10

11

12

13

14

15

16

6 7 8 9 10