1 Polygons
1 Polygons
OpenGL
Learning Outcomes
• Know how to turn off edges when rendering apparently convex polygons.
2
Other Primitives
• Triangles are the preferred primitive for object composition because most
OpenGL hardware specifically accelerates triangles, but they are not the only
primitives available.
• quadrilaterals
• quadrilateral strips
• general-purpose polygons
3
Quads
• If you add one more side to
a triangle, you get a
quadrilateral, or a four-
sided figure.
• OpenGL’s GL_QUADS
void renderScene(void)
primitive draws a four- {
sided polygon. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);! ! ! ! ! !
glVertex3f(-50.0f, 0.0f, 0.0f);!! ! !
• This quad has a clockwise glVertex3f( 0.0f, 50.0f, 0.0f);!! ! !
glVertex3f( 50.0f, 0.0f, 0.0f);!! ! !
winding. glVertex3f( 0.0f, -50.0f, 0.0f);!! !
! glEnd();
4
Quad Strip
• As you can for triangle strips, you can
specify a strip of connected quadrilaterals
with the GL_QUAD_STRIP primitive.
• Eg. a quad strip specified by six vertices.
• Note that these quad strips maintain a
clockwise winding.
void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUAD_STRIP);! ! ! ! ! !
glVertex3f(-50.0f, -50.0f, 0.0f);! ! ! !
glVertex3f( -50.0f, 50.0f, 0.0f);! !
glVertex3f( 0.0f, -50.0f, 0.0f);!
glVertex3f( 0.0f, 50.0f, 0.0f);!! ! !
glVertex3f( 50.0f, -50.0f, 0.0f);! !
glVertex3f( 50.0f, 50.0f, 0.0f);!!
! glEnd();
! glutSwapBuffers();
} 5
General Polygons
• The final OpenGL primitive is the
GL_POLYGON, which you can use to draw a
polygon having any number of sides.
• Eg a polygon consisting of five vertices.
• Polygons, like quads, must have all vertices
on the same plane.
void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);! ! ! ! ! !
glVertex3f(-50.0f, 50.0f, 0.0f);! ! ! !
glVertex3f( 20.0f, 50.0f, 0.0f);
glVertex3f( 50.0f, 30.0f, 0.0f);
glVertex3f( 50.0f, -50.0f, 0.0f);!
glVertex3f(-50.0f, -50.0f, 0.0f);
! glEnd();
! glutSwapBuffers();
} 6
Polygon Construction Rules (1) Planar Rule
7
(2) Convex Rule
(2) A polygon’s edges must not
intersect, and the polygon must
be convex.
9
! glBegin(GL_TRIANGLES);
glVertex2f(-20.0f, 0.0f);
glVertex2f(20.0f, 0.0f);
glVertex2f(0.0f, 40.0f);
glVertex2f(-20.0f,0.0f);
glVertex2f(-60.0f,-20.0f);
glVertex2f(-20.0f,-40.0f);
glVertex2f(-20.0f,-40.0f);
glVertex2f(0.0f, -80.0f);
glVertex2f(20.0f, -40.0f);! !
glVertex2f(20.0f, -40.0f);! !
glVertex2f(60.0f, -20.0f);
glVertex2f(20.0f, 0.0f);
glVertex2f(-20.0f, 0.0f);
glVertex2f(-20.0f,-40.0f);
glVertex2f(20.0f, 0.0f);
glVertex2f(-20.0f,-40.0f);
glVertex2f(20.0f, -40.0f);
glVertex2f(20.0f, 0.0f);
!
! glEnd();
10
• When the polygons are filled, you won’t be
able to see any edges and the figure will
seem to be a single shape onscreen.
11
glBegin(GL_TRIANGLES);
glEdgeFlag(GL_FALSE);
glVertex2f(-20.0f, 0.0f);
glEdgeFlag(GL_TRUE);
glVertex2f(20.0f, 0.0f);
glVertex2f(0.0f, 40.0f);
glVertex2f(-20.0f,0.0f);
glVertex2f(-60.0f,-20.0f);
glEdgeFlag(GL_FALSE);
glVertex2f(-20.0f,-40.0f);
glEdgeFlag(GL_TRUE);
glVertex2f(-20.0f,-40.0f);
glVertex2f(0.0f, -80.0f);
glEdgeFlag(GL_FALSE);
• By setting and clearing the edge flag as you glVertex2f(20.0f, -40.0f);! !
specify a list of vertices, you inform glEdgeFlag(GL_TRUE);
visible). glEdgeFlag(GL_FALSE);
glVertex2f(-20.0f, 0.0f);
• The glEdgeFlag function takes a single glVertex2f(-20.0f,-40.0f);
parameter that sets the edge flag to True or glVertex2f(20.0f, 0.0f);
False. When the function is set to True, any glVertex2f(-20.0f,-40.0f);
vertices that follow mark the beginning of a glVertex2f(20.0f, -40.0f);
glVertex2f(20.0f, 0.0f);
boundary line segment glEdgeFlag(GL_TRUE);
! glEnd(); 12