2D Drawing
2D Drawing
Graphics Programming
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
Cyan (0,1,1)
Image: Pixel maps to color
White (1,1,1)
Yellow (1,1,0)
Blue (0,0,1)
Red (1,0,0)
B
Budditha Hettige
20
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
Budditha Hettige
23
Example:
Shading.cpp
Budditha Hettige
24