Open GL Super Bible:Drawing in 3D: Lines, Points, and Polygons
To access the contents, click the chapter and section titles.
Open GL Super Bible
(Publisher: Macmillan Computer Publishing)
Author(s): Waite group Press
ISBN: 1571690735
Publication Date: 08/01/96
Previous
Table of Contents
Next
Subdivision and Edges
Even though OpenGL can only draw convex polygons, therełs still a way to create a nonconvex polygonby arranging two or more convex polygons together. For example, letłs take a four-point star as shown in Figure 6-31. This shape is obviously not convex and thus violates OpenGLłs rules for simple polygon construction. However, the star on the right is composed of six separate triangles, which are legal polygons.
Figure 6-31 A nonconvex four-point star made up of six triangles
When the polygons are filled, you wonłt be able to see any edges and the figure will seem to be a single shape on screen. However, if you use glPolygonMode to switch to an outline drawing, it would be distracting to see all those little triangles making up some larger surface area.
OpenGL provides a special flag called an edge flag for this purpose. By setting and clearing the edge flag as you specify a list of vertices, you inform OpenGL which line segments are considered border lines (lines that go around the border of your shape), and which ones are not (internal lines that shouldnłt be visible). The glEdgeFlag() function takes a single parameter that sets the edge flag to True or False. When set to True, any vertices that follow mark the beginning of a boundary line segment. Listing 6-11 shows an example of this from the STAR example program on the CD.
Listing 6-11 Example usage of glEdgeFlag from the STAR program
// Begin the triangles
GlBegin(GL_TRIANGLES);
glEdgeFlag(bEdgeFlag);
glVertex2f(-20.0f, 0.0f);
glEdgeFlag(TRUE);
glVertex2f(20.0f, 0.0f);
glVertex2f(0.0f, 40.0f);
glVertex2f(-20.0f,0.0f);
glVertex2f(-60.0f,-20.0f);
glEdgeFlag(bEdgeFlag);
glVertex2f(-20.0f,-40.0f);
glEdgeFlag(TRUE);
glVertex2f(-20.0f,-40.0f);
glVertex2f(0.0f, -80.0f);
glEdgeFlag(bEdgeFlag);
glVertex2f(20.0f, -40.0f);
glEdgeFlag(TRUE);
glVertex2f(20.0f, -40.0f);
glVertex2f(60.0f, -20.0f);
glEdgeFlag(bEdgeFlag);
glVertex2f(20.0f, 0.0f);
glEdgeFlag(TRUE);
// Center square as two triangles
glEdgeFlag(bEdgeFlag);
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);
glEdgeFlag(TRUE);
// Done drawing Triangles
glEnd();
The Boolean variable bEdgeFlag is toggled on and off by a menu option to make the edges appear and disappear. If this flag is True, then all edges are considered boundary edges and will appear when the polygon mode is set to GL_LINES. In Figures 6-32a and 6-32b you can see the output from STAR, showing the wireframe star with and without edges.
Figure 6-32a STAR program with edges enabled
Figure 6-32b STAR program without edges enabled
Summary
Wełve covered a lot of ground in this chapter. At this point you can create your 3D space for rendering, and you know how to draw everything from points and lines to complex polygons. Wełve also shown you how to assemble these two dimensional primitives as the surface of three-dimensional objects.
We encourage you to experiment with what you have learned in this chapter. Use your imagination and create some of your own 3D objects before moving on to the rest of the book. Youłll then have some personal samples to work with and enhance as you learn and explore new techniques throughout the book.
Here Comes the Tank/Robot SimulationBeginning with this chapter, we will begin constructing a tank and robot simulator as a supplementary example (found on the CD). The goal of this simulation is to have both the tank and robot roam around in a virtual landscape, allowing for viewpoints from the tankłs or robotłs perspective. The tank/robot simulator is not explained as part of the text, but the simulation will be gradually enhanced using the techniques presented in each chapter. You can start now and view some of the objects that will exist in the virtual world of our tank and robot. Observe and study how these objects are composed entirely of the primitives from this chapter.
Previous
Table of Contents
Next
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.
Wyszukiwarka
Podobne podstrony:
166 T S 1998 petrol NO163 166I CSK 166 09 1SHSpec 166 6206C28 Rudiments2010 03, str 166 16928 (166)więcej podobnych podstron