Debian Codegraph @

apps/examples/Mentor/CXX/05.3.TriangleStripSet.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*--------------------------------------------------------------
38 * This is an example from the Inventor Mentor,
39 * chapter 5, example 3.
40 *
41 * This example creates a TriangleStripSet. It creates
42 * a pennant-shaped flag.
43 *------------------------------------------------------------*/
44
45 #include <cstdlib>
46 #include <Inventor/Xt/SoXt.h>
47 #include <Inventor/Xt/viewers/SoXtExaminerViewer.h>
48 #include <Inventor/nodes/SoCoordinate3.h>
49 #include <Inventor/nodes/SoMaterial.h>
50 #include <Inventor/nodes/SoMaterialBinding.h>
51 #include <Inventor/nodes/SoSeparator.h>
52 #include <Inventor/nodes/SoShapeHints.h>
53 #include <Inventor/nodes/SoTriangleStripSet.h>
54
55 //////////////////////////////////////////////////////////////
56 // CODE FOR The Inventor Mentor STARTS HERE
57
58 //
59 // Positions of all of the vertices:
60 //
61 static float vertexPositions[40][3] =
62 {
63 { 0, 12, 0 }, { 0, 15, 0},
64 {2.1, 12.1, -.2 }, { 2.1, 14.6, -.2},
65 { 4, 12.5, -.7 }, { 4, 14.5, -.7},
66 {4.5, 12.6, -.8 }, { 4.5, 14.4, -.8},
67 { 5, 12.7, -1 }, { 5, 14.4, -1},
68 {4.5, 12.8, -1.4 }, { 4.5, 14.6, -1.4},
69 { 4, 12.9, -1.6 }, { 4, 14.8, -1.6},
70 {3.3, 12.9, -1.8 }, { 3.3, 14.9, -1.8},
71 { 3, 13, -2.0 }, { 3, 14.9, -2.0},
72 {3.3, 13.1, -2.2 }, { 3.3, 15.0, -2.2},
73 { 4, 13.2, -2.5 }, { 4, 15.0, -2.5},
74 { 6, 13.5, -2.2 }, { 6, 14.8, -2.2},
75 { 8, 13.4, -2 }, { 8, 14.6, -2},
76 { 10, 13.7, -1.8 }, { 10, 14.4, -1.8},
77 { 12, 14, -1.3 }, { 12, 14.5, -1.3},
78 { 15, 14.9, -1.2 }, { 15, 15, -1.2},
79
80 {-.5, 15, 0 }, { -.5, 0, 0}, // the flagpole
81 { 0, 15, .5 }, { 0, 0, .5},
82 { 0, 15, -.5 }, { 0, 0, -.5},
83 {-.5, 15, 0 }, { -.5, 0, 0}
84 };
85
86
87 // Number of vertices in each strip.
88 static int32_t numVertices[2] =
89 {
90 32, // flag
91 8 // pole
92 };
93
94 // Colors for the 12 faces
95 static float colors[2][3] =
96 {
97 { .5, .5, 1 }, // purple flag
98 { .4, .4, .4 }, // grey flagpole
99 };
100
101 // Routine to create a scene graph representing a pennant.
102 SoSeparator *
103 makePennant()
104 {
105 SoSeparator *result = new SoSeparator;
106 result->ref();
107
108 // A shape hints tells the ordering of polygons.
109 // This insures double sided lighting.
110 SoShapeHints *myHints = new SoShapeHints;
111 myHints->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE;
112 result->addChild(myHints);
113
114 #ifdef IV_STRICT
115 // This is the preferred code for Inventor 2.1
116
117 // Using the new SoVertexProperty node is more efficient
118 SoVertexProperty *myVertexProperty = new SoVertexProperty;
119
120 // Define colors for the strips
121 for (int i=0; i<2; i++)
122 myVertexProperty->orderedRGBA.set1Value(i, SbColor(colors[i]).getPackedValue());
123 myVertexProperty->materialBinding = SoMaterialBinding::PER_PART;
124
125 // Define coordinates for vertices
126 myVertexProperty->vertex.setValues(0, 40, vertexPositions);
127
128 // Define the TriangleStripSet, made of two strips.
129 SoTriangleStripSet *myStrips = new SoTriangleStripSet;
130 myStrips->numVertices.setValues(0, 2, numVertices);
131
132 myStrips->vertexProperty.setValue(myVertexProperty);
133 result->addChild(myStrips);
134
135 #else
136 // Define colors for the strips
137 SoMaterial *myMaterials = new SoMaterial;
138 myMaterials->diffuseColor.setValues(0, 2, colors);
139 result->addChild(myMaterials);
140 SoMaterialBinding *myMaterialBinding = new SoMaterialBinding;
141 myMaterialBinding->value = SoMaterialBinding::PER_PART;
142 result->addChild(myMaterialBinding);
143
144 // Define coordinates for vertices
145 SoCoordinate3 *myCoords = new SoCoordinate3;
146 myCoords->point.setValues(0, 40, vertexPositions);
147 result->addChild(myCoords);
148
149 // Define the TriangleStripSet, made of two strips.
150 SoTriangleStripSet *myStrips = new SoTriangleStripSet;
151 myStrips->numVertices.setValues(0, 2, numVertices);
152 result->addChild(myStrips);
153 #endif
154
155 result->unrefNoDelete();
156 return result;
157 }
158
159 // CODE FOR The Inventor Mentor ENDS HERE
160 //////////////////////////////////////////////////////////////
161
162 int
163 main(int, char **argv)
164 {
165 // Initialize Inventor and Xt
166 Widget myWindow = SoXt::init(argv[0]);
167 if (myWindow == NULL) exit(1);
168
169 SoSeparator *root = makePennant();
170 root->ref();
171
172 SoXtExaminerViewer *myViewer =
173 new SoXtExaminerViewer(myWindow);
174 myViewer->setSceneGraph(root);
175 myViewer->setTitle("Triangle Strip Set: Pennant");
176 myViewer->show();
177 myViewer->viewAll();
178
179 SoXt::show(myWindow);
180 SoXt::mainLoop();
181 }
182