CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained...

27
CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: www.cs.unc.edu/~mcmillan/comp136/ Lecture 6 2D Basics, Line Drawing, and Clipping

Transcript of CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained...

Page 1: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

CAP4730: Computational Structures in Computer Graphics

Chapter 3 Hearn & BakerPortions obtained from Leonard McMillan’s COMP136

Notes:www.cs.unc.edu/~mcmillan/comp136/Lecture 6

2D Basics, Line Drawing, and Clipping

Page 2: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

Definitions• CG API – Computer Graphics

Application Programming Interface (OpenGL, DirectX)

• Graphics primitives – functions in the API that describe picture components

• How could we describe an object?– Typically focus on object shape

– Define an object’s shape with geometric primitives

– Span of primitives are defined by the API

– What are some types?• Lines, Triangles, Quadrics, Conic sections,

Curved surfaces

Page 3: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

Two Dimensional Images

• Use Cartesian coordinates• We label the two axes as

– X (horizontal)– Y (vertical)

• Origin is in the lower left• How big is the space?

– So what is the image we see on a screen?

– We call this space the world coordinate system

X Axis

Y

Axis

(0,0) +X

+Y

Page 4: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

Partition the space into pixels1. Define a set of points (vertices) in 2D space.

2. Given a set of vertices, draw lines between

consecutive vertices.

3. If you were writing OpenGL yourself, let’s

talk about low level calls

4. What about 2D vs 3D?

Screen Coordinates – references to frame buffer locations

Q: True or Flase: Screen Coordinates == World Coordinates

+X

+Y

(2,1)

(2,7)

(9,1)

(9,7)

Page 5: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

Pixels

• ?=glSetPixel(?)

• ?=glGetPixel(?)

• Scan line number – y

• Column number – x

Page 6: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

Absolute and Relative Coordinate Specifications

• Absolute coordinates – location specified as a relationship to the origin

• Relative coordinates – location specified as a relationship to other points– Good for pen/plotters– Publishing/layout– Allows for a very object

oriented approach

• For this class we will always use absolute coordinates

+Y

(2,1)

(0,6)

(0,-6)

(7,0)

Page 7: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

Specifying a World Coordinate System in OpenGL

+X

+Y

gluOrtho2D (xmin, xmax, ymin, ymax)What should our xmin, xmax, ymin, ymax values be?

Equivalent to the size of the framebuffer

Page 8: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

What is a “pixel”From a geometry point of view, a pixel is a point. Q: Where is (2,1)?

2

2

1

10 3 4

3

5

Q: What is a pixel? A square or a point?

Page 9: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

But when we think about images, a pixel is a rectangle.

Q: Where is (2,1)? A. The center of a pixel

1

2

0

10 3 4

2

Page 10: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

Basic OpenGL Point Structure

• In OpenGL, to specify a point:– glVertex*();

• In OpenGL, some functions require both a dimensionality and a data type– glVertex2i(80,100), glVertex2f(58.9, 90.3)– glVertex3i(20,20,-5), glVertex3f(-2.2,20.9,20)

• Must put within a ‘glBegin/glEnd’ pair– glBegin(GL_POINTS);– glVertex2i(50,50);– glVertex2i(60,60);– glVertex2i(60,50);– glEnd();

• Let’s draw points in our assignment #1• Next up? Lines

Page 11: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

Draw a line from 0,0 to 4,2

2

2

1

10

0

3 4

How do we choose between 1,0 and 1,1? What would be a good heuristic?

(0,0)

(4,2)

Page 12: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

What are lines composed of?Write glBegin(GL_LINES)

2

2

1

10

0

3 4

(0,0)

(4,2)

Page 13: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

What we are working with

V0: (6,2)

V1: (6,8)

V3: (13,2)

V2: (13,8)

• We are still dealing with vertices• Draws a line between every pair of vertices• glBegin(GL_LINES);• glVertex2i(6,2);• glVertex2i(6,8);• glEnd();

Page 14: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

Let’s draw a triangle

(2,0)

(4,2)(0,2)

2

2

1

10

0

3 4

Page 15: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

Consider a translation

(1.8,0)

(3.8,2)(-0.2,2)

2

2

1

10

0

3 4

Page 16: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

The Ideal Line

• Continuous appearance

• Uniform thickness and brightness

• Pixels near the ideal line are “on”

• Speed

(2,2)

(17,8)

Discretization - converting a continuous signal into discrete elements.

Scan Conversion - converting vertex/edges information into pixel data for display

What do we want?

Page 17: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

Slope-Intercept Method• From algebra: y = mx + b

– m = slope b = y intercept Let’s write some codeclass Point

{

public:

int x, y;

int r,g,b;

};

unsigned byte framebuffer[IMAGE_WIDTH*IMAGE_HEIGHT*3];

DrawLine (Point point1, Point point2)

{

}

Page 18: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

Slope-Intercept Method• From algebra: y = mx + b

– m = slope b = y intercept Let’s write some codeDrawLine (Point point1, Point point2){

m=(point2.y-point1.y) / (point2.x-point2.x);

b=point1.y + (-point1.x) * m;

for i=point1.x to point2.x

SetPixel(i , round(m*i+b)), pixel1.r, pixel1.g, pixel1.b;}

SetPixel(int x, int y, int r, int g, int b){

framebuffer[(y * IMAGE_WIDTH+x) * 3 + 0]=r;

framebuffer[(y * IMAGE_WIDTH+x) * 3 + 1]=g;

framebuffer[(y * IMAGE_WIDTH+x) * 3 + 2]=b;}

Page 19: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

Example 1:

Point1 V:(2,2) C:(255,102,0)

Point2 V:(17,8) C:(255,102,0)

What if colors were different?

(17,8)

(2,2)

(0,0) (18,0)

(0,9)

Page 20: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

How do we change the framebuffer?

(17,8)

(2,2)

(0,0) (18,0)

(0,9) What’s the index into GLubyte framebuffer[]? Point is 9,5

Page 21: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

Example:

(7,9)

(12,0)

Example 2:

Point1 V:(7,9 ) C:( 0,255,0)

Point2 V:(12,0) C:(0,255,0)

(0,0) (18,0)

(0,9)

What are the problems with this method? Slope>1

Page 22: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

Revised Slope InterceptDrawLine (Point point1, Point point2){

m=(point2.y-point1.y) / (point2.x-point2.x);

b=point1.y + (-point1.x) * m;

if (m>1)

{

for i=point1.x to point2.x

SetPixel(i , round(i*m+b));}

}

else

{

for i=point1.y to point2.y

SetPixel(i , round(i-b/m));}

}

Which one should we use if m=1?

What is the cost per pixel?

Page 23: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

Optimization (DDA Algorithm)

• Since we increment y by the same amount, we can also inline rounding:

• New cost: one floating point addition, one integer addition, one cast.

DrawLine (Point point1, Point point2){

m=(point2.y-point1.y) / (point2.x-point2.x);

j=point1.y + (-point1.x) * m + 0.5;

for i=point1.x to point2.x

SetPixel(i , (int)j+=m));}

Page 24: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

Bresenham’s Line Drawing

• In general:– Addition and Subtraction are faster than

Multiplication which is faster than Division– Integer calculations are faster than Floating

point

• Made all math just integers (Section 3.5)

• How?

Page 25: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

What you need to know about Bresenham LDA

1) Why we use it

2) Major idea of integer-izing a decision point

3) How this reduces things to just integer form.

(17,8)

(2,2)

(0,0) (18,0)

(0,9)

Page 26: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

Recap• DDA/Line Intercept Algorithm

– Slope Intercept y = mx + b

– Easy to implement

– Slow

• Bresenham– No floating point math

– Fast

• Why do we spend so much time optimizing this?

Page 27: CAP4730: Computational Structures in Computer Graphics Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: mcmillan/comp136/Lecture.

Other Primitive Drawing Solutions

• What other shapes might we want to draw quickly?– Circles (and thus) Ovals– Curves

• Fill?