SlideShare a Scribd company logo
CAP4730: Computational 
Structures in Computer Graphics 
2D Basics, Line Drawing, and 
Clipping 
Chapter 3 Hearn & Baker 
Portions obtained from Leonard McMillan’s COMP136 
Notes: 
www.cs.unc.edu/~mcmillan/comp136/Lecture 6
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
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 
Y 
Axis 
(0,0) +X
Partition 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, let’s 
talk about low level calls 
4. What about 2D vs 3D? 
(9,7) 
(9,1) 
+X 
+Y 
(2,7) 
(2,1) 
Screen Coordinates – references to frame buffer locations 
Q: True or Flase: Screen Coordinates == World Coordinates
Pixels 
• ?=glSetPixel(?) 
• ?=glGetPixel(?) 
• Scan line number – y 
• Column number – x
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 
(0,6) 
(2,1) 
(7,0) 
(0,-6)
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
From a geometry point of view, a pixel is a point. Q: Where is (2,1)? 
What is a “pixel” 
3 
2 
2 
1 
0 1 3 4 
5 
Q: What is a pixel? A square or a point?
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 
0 1 3 4 
2
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
Draw a line from 0,0 to 4,2 
2 
How do we choose between 1,0 and 1,1? What would be a good heuristic? 
2 
1 
0 1 
0 
3 4 
(0,0) 
(4,2)
What are lines composed of? 
Write glBegin(GL_LINES) 
2 
2 
1 
0 1 
0 
3 4 
(0,0) 
(4,2)
What we are working with 
V1: (6,8) 
V0: (6,2) 
V2: (13,8) 
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();
Let’s draw a triangle 
(0,2) (4,2) 
(2,0) 
2 
2 
1 
0 1 
0 
3 4
Consider a translation 
(-0.2,2) (3.8,2) 
(1.8,0) 
2 
2 
1 
0 1 
0 
3 4
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?
Slope-Intercept Method 
• From algebra: y = mx + b 
– m = slope b = y intercept Let’s write some code 
class Point 
{ 
public: 
int x, y; 
int r,g,b; 
}; 
unsigned byte framebuffer[IMAGE_WIDTH*IMAGE_HEIGHT*3]; 
DrawLine (Point point1, Point point2) 
{ 
}
Slope-Intercept Method 
• From algebra: y = mx + b 
– m = slope b = y intercept Let’s write some code 
DrawLine (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;}
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,9) 
(0,0) (18,0)
How do we change the 
framebuffer? 
(17,8) 
(0,9) What’s the index into GLubyte framebuffer[]? Point is 9,5 
(2,2) 
(0,0) (18,0)
Example: 
(7,9) 
(12,0) 
(0,9) 
Example 2: 
Point1 V:(7,9 ) C:( 0,255,0) 
Point2 V:(12,0) C:(0,255,0) 
(0,0) (18,0) 
What are the problems with this method? Slope>1
Revised Slope Intercept 
DrawLine (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?
Optimization (DDA Algorithm) 
• Since we increment y by the same amount, 
we can also inline rounding: 
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));} 
• New cost: one floating point addition, one 
integer addition, one cast.
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?
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,9) 
(0,0) (18,0)
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?
Other Primitive Drawing Solutions 
• What other shapes might we want to draw 
quickly? 
– Circles (and thus) Ovals 
– Curves 
• Fill?

More Related Content

What's hot

C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]
MomenMostafa
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 
Bab 4 dan bab 5 algebra and trigonometry
Bab 4 dan bab 5 algebra and trigonometryBab 4 dan bab 5 algebra and trigonometry
Bab 4 dan bab 5 algebra and trigonometry
CiciPajarakan
 
Numerical analysis multivariable unconstrained
Numerical analysis  multivariable unconstrainedNumerical analysis  multivariable unconstrained
Numerical analysis multivariable unconstrained
SHAMJITH KM
 
Integer programming branch and bound
Integer programming   branch and boundInteger programming   branch and bound
Integer programming branch and bound
Alejandro Angulo
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
Oye Tu
 

What's hot (19)

C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]
 
Array
ArrayArray
Array
 
operators in c++
operators in c++operators in c++
operators in c++
 
graphical method
graphical method graphical method
graphical method
 
INTRODUCTION TO MATLAB session with notes
  INTRODUCTION TO MATLAB   session with  notes  INTRODUCTION TO MATLAB   session with  notes
INTRODUCTION TO MATLAB session with notes
 
Linear algebra application in linear programming
Linear algebra application in linear programming Linear algebra application in linear programming
Linear algebra application in linear programming
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity
 
Bab 4 dan bab 5 algebra and trigonometry
Bab 4 dan bab 5 algebra and trigonometryBab 4 dan bab 5 algebra and trigonometry
Bab 4 dan bab 5 algebra and trigonometry
 
Numerical analysis multivariable unconstrained
Numerical analysis  multivariable unconstrainedNumerical analysis  multivariable unconstrained
Numerical analysis multivariable unconstrained
 
Integer programming branch and bound
Integer programming   branch and boundInteger programming   branch and bound
Integer programming branch and bound
 
User Defined Functions in C Language
User Defined Functions   in  C LanguageUser Defined Functions   in  C Language
User Defined Functions in C Language
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
Dynamic programming1
Dynamic programming1Dynamic programming1
Dynamic programming1
 
arrays
arraysarrays
arrays
 
Comparitive Analysis of Algorithm strategies
Comparitive Analysis of Algorithm strategiesComparitive Analysis of Algorithm strategies
Comparitive Analysis of Algorithm strategies
 
Python Lecture 4
Python Lecture 4Python Lecture 4
Python Lecture 4
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
 
Operators
OperatorsOperators
Operators
 

Viewers also liked

Lecture 2d point,curve,text,line clipping
Lecture   2d point,curve,text,line clippingLecture   2d point,curve,text,line clipping
Lecture 2d point,curve,text,line clipping
avelraj
 
Bresenham's line algo.
Bresenham's line algo.Bresenham's line algo.
Bresenham's line algo.
Mohd Arif
 
Hearn and Baker 2 D transformations
Hearn and Baker 2 D transformations   Hearn and Baker 2 D transformations
Hearn and Baker 2 D transformations
Taher Barodawala
 
Line drawing algo.
Line drawing algo.Line drawing algo.
Line drawing algo.
Mohd Arif
 
Clipping Algorithm In Computer Graphics
Clipping Algorithm In Computer GraphicsClipping Algorithm In Computer Graphics
Clipping Algorithm In Computer Graphics
student(MCA)
 
applications of computer graphics
applications of computer graphicsapplications of computer graphics
applications of computer graphics
Aaina Katyal
 

Viewers also liked (16)

Clipping in Computer Graphics
Clipping in Computer GraphicsClipping in Computer Graphics
Clipping in Computer Graphics
 
Lecture 2d point,curve,text,line clipping
Lecture   2d point,curve,text,line clippingLecture   2d point,curve,text,line clipping
Lecture 2d point,curve,text,line clipping
 
2.2. interactive computer graphics
2.2. interactive computer graphics2.2. interactive computer graphics
2.2. interactive computer graphics
 
2.1. fundamental of computer graphics
2.1. fundamental of computer graphics2.1. fundamental of computer graphics
2.1. fundamental of computer graphics
 
Cs580
Cs580Cs580
Cs580
 
Bresenham's line algo.
Bresenham's line algo.Bresenham's line algo.
Bresenham's line algo.
 
COMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUALCOMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUAL
 
Polygon clipping
Polygon clippingPolygon clipping
Polygon clipping
 
Bresenham Line Drawing Algorithm
Bresenham Line Drawing AlgorithmBresenham Line Drawing Algorithm
Bresenham Line Drawing Algorithm
 
Hearn and Baker 2 D transformations
Hearn and Baker 2 D transformations   Hearn and Baker 2 D transformations
Hearn and Baker 2 D transformations
 
Geometric transformation 2d chapter 5
Geometric transformation 2d   chapter 5Geometric transformation 2d   chapter 5
Geometric transformation 2d chapter 5
 
Line drawing algo.
Line drawing algo.Line drawing algo.
Line drawing algo.
 
Clipping Algorithm In Computer Graphics
Clipping Algorithm In Computer GraphicsClipping Algorithm In Computer Graphics
Clipping Algorithm In Computer Graphics
 
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
 
Clipping
ClippingClipping
Clipping
 
applications of computer graphics
applications of computer graphicsapplications of computer graphics
applications of computer graphics
 

Similar to Primitives

CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdfCD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
RajJain516913
 
Output Primitives in Computer Graphics and Multimedia
Output Primitives in Computer Graphics and MultimediaOutput Primitives in Computer Graphics and Multimedia
Output Primitives in Computer Graphics and Multimedia
saranyan75
 
Computer Graphics and Multimedia Output primitives
Computer Graphics and Multimedia Output primitivesComputer Graphics and Multimedia Output primitives
Computer Graphics and Multimedia Output primitives
saranyan75
 
Matlab intro
Matlab introMatlab intro
Matlab intro
fvijayami
 
Signature recognition using clustering techniques dissertati
Signature recognition using clustering techniques dissertatiSignature recognition using clustering techniques dissertati
Signature recognition using clustering techniques dissertati
Dr. Vinayak Bharadi
 

Similar to Primitives (20)

Computer Graphics Unit 1
Computer Graphics Unit 1Computer Graphics Unit 1
Computer Graphics Unit 1
 
Computer graphics 2
Computer graphics 2Computer graphics 2
Computer graphics 2
 
raster algorithm.pdf
raster algorithm.pdfraster algorithm.pdf
raster algorithm.pdf
 
CG-Lecture3.pptx
CG-Lecture3.pptxCG-Lecture3.pptx
CG-Lecture3.pptx
 
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdfCD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
 
Study on Fundamentals of Raster Scan Graphics
Study on Fundamentals of Raster Scan GraphicsStudy on Fundamentals of Raster Scan Graphics
Study on Fundamentals of Raster Scan Graphics
 
computer graphics-C/C++-dancingdollcode
computer graphics-C/C++-dancingdollcodecomputer graphics-C/C++-dancingdollcode
computer graphics-C/C++-dancingdollcode
 
The Day You Finally Use Algebra: A 3D Math Primer
The Day You Finally Use Algebra: A 3D Math PrimerThe Day You Finally Use Algebra: A 3D Math Primer
The Day You Finally Use Algebra: A 3D Math Primer
 
Output Primitives in Computer Graphics and Multimedia
Output Primitives in Computer Graphics and MultimediaOutput Primitives in Computer Graphics and Multimedia
Output Primitives in Computer Graphics and Multimedia
 
Computer Graphics and Multimedia Output primitives
Computer Graphics and Multimedia Output primitivesComputer Graphics and Multimedia Output primitives
Computer Graphics and Multimedia Output primitives
 
4 CG_U1_M3_PPT_4 DDA.pptx
4 CG_U1_M3_PPT_4 DDA.pptx4 CG_U1_M3_PPT_4 DDA.pptx
4 CG_U1_M3_PPT_4 DDA.pptx
 
Cgm Lab Manual
Cgm Lab ManualCgm Lab Manual
Cgm Lab Manual
 
Rasterization.pptx
Rasterization.pptxRasterization.pptx
Rasterization.pptx
 
Templateless Marked Element Recognition Using Computer Vision
Templateless Marked Element Recognition Using Computer VisionTemplateless Marked Element Recognition Using Computer Vision
Templateless Marked Element Recognition Using Computer Vision
 
Cgm Lab Manual
Cgm Lab ManualCgm Lab Manual
Cgm Lab Manual
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
 
Matlab intro
Matlab introMatlab intro
Matlab intro
 
Signature recognition using clustering techniques dissertati
Signature recognition using clustering techniques dissertatiSignature recognition using clustering techniques dissertati
Signature recognition using clustering techniques dissertati
 
Comparison of Various Line Clipping Algorithm for Improvement
Comparison of Various Line Clipping Algorithm for ImprovementComparison of Various Line Clipping Algorithm for Improvement
Comparison of Various Line Clipping Algorithm for Improvement
 

Recently uploaded

Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
YibeltalNibretu
 

Recently uploaded (20)

Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 

Primitives

  • 1. CAP4730: Computational Structures in Computer Graphics 2D Basics, Line Drawing, and Clipping Chapter 3 Hearn & Baker Portions obtained from Leonard McMillan’s COMP136 Notes: www.cs.unc.edu/~mcmillan/comp136/Lecture 6
  • 2. 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
  • 3. 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 Y Axis (0,0) +X
  • 4. Partition 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, let’s talk about low level calls 4. What about 2D vs 3D? (9,7) (9,1) +X +Y (2,7) (2,1) Screen Coordinates – references to frame buffer locations Q: True or Flase: Screen Coordinates == World Coordinates
  • 5. Pixels • ?=glSetPixel(?) • ?=glGetPixel(?) • Scan line number – y • Column number – x
  • 6. 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 (0,6) (2,1) (7,0) (0,-6)
  • 7. 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
  • 8. From a geometry point of view, a pixel is a point. Q: Where is (2,1)? What is a “pixel” 3 2 2 1 0 1 3 4 5 Q: What is a pixel? A square or a point?
  • 9. 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 0 1 3 4 2
  • 10. 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
  • 11. Draw a line from 0,0 to 4,2 2 How do we choose between 1,0 and 1,1? What would be a good heuristic? 2 1 0 1 0 3 4 (0,0) (4,2)
  • 12. What are lines composed of? Write glBegin(GL_LINES) 2 2 1 0 1 0 3 4 (0,0) (4,2)
  • 13. What we are working with V1: (6,8) V0: (6,2) V2: (13,8) 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();
  • 14. Let’s draw a triangle (0,2) (4,2) (2,0) 2 2 1 0 1 0 3 4
  • 15. Consider a translation (-0.2,2) (3.8,2) (1.8,0) 2 2 1 0 1 0 3 4
  • 16. 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?
  • 17. Slope-Intercept Method • From algebra: y = mx + b – m = slope b = y intercept Let’s write some code class Point { public: int x, y; int r,g,b; }; unsigned byte framebuffer[IMAGE_WIDTH*IMAGE_HEIGHT*3]; DrawLine (Point point1, Point point2) { }
  • 18. Slope-Intercept Method • From algebra: y = mx + b – m = slope b = y intercept Let’s write some code DrawLine (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;}
  • 19. 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,9) (0,0) (18,0)
  • 20. How do we change the framebuffer? (17,8) (0,9) What’s the index into GLubyte framebuffer[]? Point is 9,5 (2,2) (0,0) (18,0)
  • 21. Example: (7,9) (12,0) (0,9) Example 2: Point1 V:(7,9 ) C:( 0,255,0) Point2 V:(12,0) C:(0,255,0) (0,0) (18,0) What are the problems with this method? Slope>1
  • 22. Revised Slope Intercept DrawLine (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?
  • 23. Optimization (DDA Algorithm) • Since we increment y by the same amount, we can also inline rounding: 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));} • New cost: one floating point addition, one integer addition, one cast.
  • 24. 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?
  • 25. 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,9) (0,0) (18,0)
  • 26. 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?
  • 27. Other Primitive Drawing Solutions • What other shapes might we want to draw quickly? – Circles (and thus) Ovals – Curves • Fill?