0% found this document useful (0 votes)
59 views12 pages

1 Polygons

This document discusses OpenGL primitives for rendering quadrilaterals, quadrilateral strips, and general polygons. It explains that quads are four-sided polygons, quad strips connect multiple quads, and general polygons can have any number of sides, as long as the vertices all lie in the same plane and the polygon is convex. It also describes how to render non-convex polygons by subdividing them into convex polygons and using edge flags to hide interior edges.

Uploaded by

BaiJP
Copyright
© © All Rights Reserved
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)
59 views12 pages

1 Polygons

This document discusses OpenGL primitives for rendering quadrilaterals, quadrilateral strips, and general polygons. It explains that quads are four-sided polygons, quad strips connect multiple quads, and general polygons can have any number of sides, as long as the vertices all lie in the same plane and the polygon is convex. It also describes how to render non-convex polygons by subdividing them into convex polygons and using edge flags to hide interior edges.

Uploaded by

BaiJP
Copyright
© © All Rights Reserved
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/ 12

Quads, Quad Strips & Polygons

OpenGL
Learning Outcomes

• Have used Quadrilaterals, Quadrilateral strips and general purpose polygons.

• Understand the planar and convex rules for polygon construction

• 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.

• Some hardware provides for acceleration of other shapes as well, and


programmatically, using a general-purpose graphics primitive might be
simpler.

• These OpenGL primitives provide for rapid specification of:

• 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();

• All four corners of the ! glutSwapBuffers();


}
quadrilateral must lie in a
plane (no bent quads).

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

• Two important rules when


using many polygons to
construct a complex surface

(1) All polygons must be


planar. That is, all the vertices
of the polygon must lie in a
single plane. The polygon
cannot twist or bend in
space.

A good reason to use triangles. No triangle can ever


be twisted so that all three points do not line up in a
plane.

7
(2) Convex Rule
(2) A polygon’s edges must not
intersect, and the polygon must
be convex.

A polygon intersects itself if


any two of its lines cross.

Convex means that the


polygon cannot have any
indentions. A useful test of a convex polygon
is to draw some lines through it. If
any given line enters and leaves
• These restrictions allow the polygon more than once, the
OpenGL to use very fast
polygon is not convex.
algorithms for rendering these
polygons
8
Subdivision and Edges

• Even though OpenGL can draw only convex polygons,


there’s still a way to create a non-convex polygon: by
arranging two or more convex polygons together.

• E.G. a four-point star - obviously not convex and thus


violates OpenGL’s rules for simple polygon
construction.

• However, one can be composed of six separate


triangles, which are legal polygons.

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.

• However, if you use glPolygonMode to


switch to an outline drawing, it is distracting
to see all those little triangles making up
some larger surface area.

• OpenGL provides a special flag called an


edge flag to address those distracting edges.

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);

OpenGL which line segments are glVertex2f(20.0f, -40.0f);! !


considered border lines (lines that go glVertex2f(60.0f, -20.0f);
glEdgeFlag(GL_FALSE);
around the border of your shape) and which glVertex2f(20.0f, 0.0f);
ones are not (internal lines that shouldn’t be 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

You might also like