0% found this document useful (0 votes)
81 views

2D Drawing

The document discusses 2D graphics programming in OpenGL including drawing primitive types like points, lines, triangles, and polygons. It covers specifying colors and shading models as well as details of line, polygon, and color functionality.

Uploaded by

Budditha Hettige
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

2D Drawing

The document discusses 2D graphics programming in OpenGL including drawing primitive types like points, lines, triangles, and polygons. It covers specifying colors and shading models as well as details of line, polygon, and color functionality.

Uploaded by

Budditha Hettige
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

CSC 307 1.

Graphics Programming

Budditha Hettige Department of Statistics and Computer Science

Graphics Programming

2D Drawing

Budditha Hettige

Primitive Types
GL_POINTS GL_LINES GL_TRIANGLES GL_TRIANGLE_STRIP GL_QUAD_STRIP GL_LINE_STRIP GL_LINE_LOOP GL_QUADS GL_POLYGON GL_TRIANGLE_FAN
Budditha Hettige

Points
point is represented by a set of floatingpoint numbers called a vertex
glBegin(GL_POINTS); glVertex2f(0.0, 0.0); glVertex2f(0.0, 3.0); glVertex2f(4.0, 3.0); glVertex2f(6.0, 1.5); glVertex2f(4.0, 0.0); glEnd();

Budditha Hettige

Lines (GL_LINES)
Pairs of vertices interpreted as individual line segments

Budditha Hettige

Lines (GL_LINE_STRIP)
Series of connected line segments

Budditha Hettige

Lines (GL_LINE_LOOP)
Same as GL_LINE_STRIP, with a segment added between last and first vertices

Budditha Hettige

Line Details
Specify lines with different widths and lines
void glLineWidth(GLfloat width);

Stippled Lines
void glLineStipple(GLint factor, GLushort pattern); glEnable(GL_LINE_STIPPLE);

Budditha Hettige

Example
glEnable(GL_LINE_STIPPLE); glLineStipple(1, 0x0101); /* dotted */ glLineStipple(1, 0x00FF); /* dashed */ glLineStipple(1, 0x1C47); /* dash/dot/dash */
glEnable(GL_LINE_STIPPLE); glLineStipple(1, 0x0101); glBegin(GL_LINE_LOOP); glVertex2f(-0.5, -0.5); glVertex2f(-0.5, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, -0.5); glEnd();
Budditha Hettige

Triangles (GL_TRIANGLES)
Triples of vertices interpreted as triangles

Budditha Hettige

10

OpenGL 2D-Drawing
glBegin(GL_TRIANGLES);
glVertex2f(-0.20f, 0.0f); glVertex2f(0.20f, 0.0f); glVertex2f(0.0f, 0.40f); glVertex2f(-0.20f, 0.0f); glVertex2f(-0.60f,-0.20f); glVertex2f(-0.20f,-0.40f); glVertex2f(-0.20f,-0.40f); glVertex2f(0.0f, -0.80f); glVertex2f(0.20f, -0.40f); glVertex2f(0.20f, -0.40f); glVertex2f(0.60f, -0.20f); glVertex2f(0.20f, 0.0f); glVertex2f(-0.20f, 0.0f); glVertex2f(-0.20f,-0.40f); glVertex2f(0.20f, 0.0f); glVertex2f(-0.20f,-0.40f); glVertex2f(0.20f, -0.40f); glVertex2f(0.20f, 0.0f);

glEnd();
Budditha Hettige

11

Triangles (GL_TRIANGLE_STRIP)
Linked strip of triangles

Budditha Hettige

12

Triangles (GL_TRIANGLE_FAN)
Linked fan of triangles

Budditha Hettige

13

Quad-(GL_QUADS)
Quadruples of vertices interpreted as foursided polygons

Budditha Hettige

14

Quads (GL_QUAD_STRIP)
Linked strip of quadrilaterals

Budditha Hettige

15

Polygon (GL_POLYGON)
Boundary of a simple, convex polygon

Budditha Hettige

16

Polygon Details
Points, Outlines, or Solids
void glPolygonMode(GLenum face, GLenum mode); Face
GL_FRONT_AND_BACK, GL_FRONT, or GL_BACK

Mode
GL_POINT, GL_LINE, or GL_FILL

Budditha Hettige

17

Stippling Polygons
filled polygons are drawn with a solid pattern
void glPolygonStipple(const GLubyte *mask);

Example
Polygons.cpp
glEnable(GL_POLYGON_STIPPLE); glPolygonStipple(fly); glRectf(125.0, 25.0, 225.0, 125.0); GLubyte fly[] = { 0x00, 0x00, 0x00, 0x00, 0x00,... };

Budditha Hettige

18

OpenGL 2D-Drawing
void display(void) { glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 0.0, 0.0); // red glBegin(GL_POLYGON); glVertex2f(-0.20f, 0.50f); glVertex2f(0.20f, 0.50f); glVertex2f(0.50f, 0.20f); glVertex2f(0.50f, -0.20f); glColor3f (0.0, 0.0, 1.0); // blue glVertex2f(0.20f, -0.50f); glVertex2f(-0.20f, -0.50f); glVertex2f(-0.50f, -0.20f); glVertex2f(-0.50f, 0.20f); glEnd(); glFlush (); }
Budditha Hettige

19

RGB Color Space


G
Green (1,0,0)

Cyan (0,1,1)
Image: Pixel maps to color

White (1,1,1)

Yellow (1,1,0)

Blue (0,0,1)

Blk Magenta (1,0,1)

Red (1,0,0)

B
Budditha Hettige

20

Common Composite Colors

Budditha Hettige

21

Colors
glColor3f(1.0, 0.0, 0.0);
The current RGB color is red: full red, no green, no blue. RGB Display Modes

Budditha Hettige

22

Specifying a Color and a Shading Model


RGBA mode, use the glColor*() command to select a current color.
void glColor3{b s i f d ub us ui}(TYPE r, TYPE g, TYPE b); void glColor4{b s i f d ub us ui}(TYPE r, TYPE g, TYPE b, TYPE a); void glColor3{b s i f d ub us ui}v(const TYPE *v); void glColor4{b s i f d ub us ui}v(const TYPE *v);

Budditha Hettige

23

Specifying a Shading Model


single color (flat shading) many different colors (smooth shading, also called Gouraud shading) void glShadeModel(GLenum mode);
GL_SMOOTH (the default) or GL_FLAT. glShadeModel(GL_SMOOTH);

Example:
Shading.cpp

Budditha Hettige

24

You might also like