Title: CAP4730: Computational Structures in Computer Graphics
1CAP4730 Computational Structures in Computer
Graphics
2D Basics, Line Drawing, and Clipping
- Chapter 3 Hearn Baker
- Portions obtained from Leonard McMillans COMP136
Notes - www.cs.unc.edu/mcmillan/comp136/Lecture 6
2Definitions
- 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 objects shape with geometric
primitives - Span of primitives are defined by the API
- What are some types?
- Lines, Triangles, Quadrics, Conic sections,
Curved surfaces
3Two Dimensional Images
Y
- 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
Y Axis
(0,0)
X Axis
X
4Partition the space into pixels
1. 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, lets talk about low level
calls 4. What about 2D vs 3D?
Y
(2,7)
(9,7)
(2,1)
(9,1)
X
Screen Coordinates references to frame buffer
locations
Q True or Flase Screen Coordinates World
Coordinates
5Pixels
- ?glSetPixel(?)
- ?glGetPixel(?)
- Scan line number y
- Column number x
6Absolute and Relative Coordinate Specifications
Y
- 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
(0,6)
(7,0)
(2,1)
(0,-6)
7Specifying a World Coordinate System in OpenGL
Y
X
gluOrtho2D (xmin, xmax, ymin, ymax) What should
our xmin, xmax, ymin, ymax values be? Equivalent
to the size of the framebuffer
8What is a pixel
From a geometry point of view, a pixel is a
point. Q Where is (2,1)?
Q What is a pixel? A square or a point?
3
2
1
2
1
3
4
5
0
9But when we think about images, a pixel is a
rectangle. Q Where is (2,1)? A. The center of
a pixel
2
1
0
2
1
0
3
4
10Basic 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()
- Lets draw points in our assignment 1
- Next up? Lines
11Draw a line from 0,0 to 4,2
How do we choose between 1,0 and 1,1? What would
be a good heuristic?
(4,2)
2
1
(0,0)
0
2
1
0
3
4
12What are lines composed of?Write
glBegin(GL_LINES)
(4,2)
2
1
(0,0)
0
2
1
0
3
4
13What we are working with
V1 (6,8)
V2 (13,8)
V0 (6,2)
V3 (13,2)
- We are still dealing with vertices
- Draws a line between every pair of vertices
- glBegin(GL_LINES)
- glVertex2i(6,2)
- glVertex2i(6,8)
- glEnd()
14Lets draw a triangle
(0,2)
(4,2)
2
1
(2,0)
0
2
1
0
3
4
15Consider a translation
(-0.2,2)
(3.8,2)
2
1
(1.8,0)
0
2
1
0
3
4
16The Ideal Line
(17,8)
What do we want?
- Continuous appearance
- Uniform thickness and brightness
- Pixels near the ideal line are on
- Speed
(2,2)
Discretization - converting a continuous signal
into discrete elements. Scan Conversion -
converting vertex/edges information into pixel
data for display
17Slope-Intercept Method
- From algebra y mx b
- m slope b y intercept Lets write some
code
class Point public int x, y int
r,g,b unsigned byte framebufferIMAGE_WIDTHIM
AGE_HEIGHT3
DrawLine (Point point1, Point point2)
18Slope-Intercept Method
- From algebra y mx b
- m slope b y intercept Lets write some
code
DrawLine (Point point1, Point point2) m(point2.
y-point1.y) / (point2.x-point2.x) bpoint1.y
(-point1.x) m for ipoint1.x to
point2.x SetPixel(i , round(mib)), pixel1.r,
pixel1.g, pixel1.b
SetPixel(int x, int y, int r, int g, int
b) framebuffer(y IMAGE_WIDTHx) 3
0r framebuffer(y IMAGE_WIDTHx) 3
1g framebuffer(y IMAGE_WIDTHx) 3
2b
19Example 1 Point1 V(2,2) C(255,102,0) Point2
V(17,8) C(255,102,0) What if colors were
different?
(0,9)
(17,8)
(2,2)
(0,0)
(18,0)
20How do we change the framebuffer?
Whats the index into GLubyte framebuffer?
Point is 9,5
21Example
(0,9)
(0,0)
(18,0)
What are the problems with this method? Slopegt1
22Revised Slope Intercept
DrawLine (Point point1, Point point2) m(point2.
y-point1.y) / (point2.x-point2.x) bpoint1.y
(-point1.x) m if (mgt1) for ipoint1.x to
point2.x SetPixel(i , round(imb)) else
for ipoint1.y to point2.y SetPixel(i ,
round(i-b/m)) Which one should we use if
m1? What is the cost per pixel?
23Optimization (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) jpoint1.y
(-point1.x) m 0.5 for ipoint1.x to
point2.x SetPixel(i , (int)jm))
24Bresenhams 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?
25- What you need to know about Bresenham LDA
- Why we use it
- Major idea of integer-izing a decision point
- How this reduces things to just integer form.
(0,9)
(17,8)
(2,2)
(0,0)
(18,0)
26Recap
- 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?
27Other Primitive Drawing Solutions
- What other shapes might we want to draw quickly?
- Circles (and thus) Ovals
- Curves
- Fill?