Debian Codegraph @

apps/examples/Mentor/CXX/05.5.Binding.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 5.
40 *
41 * This example illustrates a variety of ways to bind
42 * materials to a polygon object.
43 * Three cases of a switch statement show various ways of
44 * binding materials to objects.
45 * The object used for all three examples is the stellated
46 * dodecahedron from an earlier example in this chapter.
47 *------------------------------------------------------------*/
48
49 #include <cstdlib>
50 #include <Inventor/Xt/SoXt.h>
51 #include <Inventor/Xt/viewers/SoXtExaminerViewer.h>
52 #include <Inventor/nodes/SoCoordinate3.h>
53 #include <Inventor/nodes/SoIndexedFaceSet.h>
54 #include <Inventor/nodes/SoMaterial.h>
55 #include <Inventor/nodes/SoMaterialBinding.h>
56 #include <Inventor/nodes/SoSeparator.h>
57
58 // Positions of all of the vertices:
59 static float vertexPositions[12][3] =
60 {
61 { 0.0000, 1.2142, 0.7453}, // top
62
63 { 0.0000, 1.2142, -0.7453}, // points surrounding top
64 {-1.2142, 0.7453, 0.0000},
65 {-0.7453, 0.0000, 1.2142},
66 { 0.7453, 0.0000, 1.2142},
67 { 1.2142, 0.7453, 0.0000},
68
69 { 0.0000, -1.2142, 0.7453}, // points surrounding bottom
70 {-1.2142, -0.7453, 0.0000},
71 {-0.7453, 0.0000, -1.2142},
72 { 0.7453, 0.0000, -1.2142},
73 { 1.2142, -0.7453, 0.0000},
74
75 { 0.0000, -1.2142, -0.7453}, // bottom
76 };
77
78 // Connectivity, information; 12 faces with 5 vertices each },
79 // (plus the end-of-face indicator for each face):
80
81 static int32_t indices[72] =
82 {
83 1, 2, 3, 4, 5, SO_END_FACE_INDEX, // top face
84
85 0, 1, 8, 7, 3, SO_END_FACE_INDEX, // 5 faces about top
86 0, 2, 7, 6, 4, SO_END_FACE_INDEX,
87 0, 3, 6, 10, 5, SO_END_FACE_INDEX,
88 0, 4, 10, 9, 1, SO_END_FACE_INDEX,
89 0, 5, 9, 8, 2, SO_END_FACE_INDEX,
90
91 9, 5, 4, 6, 11, SO_END_FACE_INDEX, // 5 faces about bottom
92 10, 4, 3, 7, 11, SO_END_FACE_INDEX,
93 6, 3, 2, 8, 11, SO_END_FACE_INDEX,
94 7, 2, 1, 9, 11, SO_END_FACE_INDEX,
95 8, 1, 5,10, 11, SO_END_FACE_INDEX,
96
97 6, 7, 8, 9, 10, SO_END_FACE_INDEX, // bottom face
98 };
99
100 // Colors for the 12 faces
101 static float colors[12][3] =
102 {
103 {1.0, .0, 0}, {.0, .0, 1.0}, {0, .7, .7}, { .0, 1.0, 0},
104 { .7, .7, 0}, {.7, .0, .7}, {0, .0, 1.0}, { .7, .0, .7},
105 { .7, .7, 0}, {.0, 1.0, .0}, {0, .7, .7}, {1.0, .0, 0}
106 };
107
108 // Routine to create a scene graph representing a dodecahedron
109 SoSeparator *
110 makeStellatedDodecahedron()
111 {
112 SoSeparator *result = new SoSeparator;
113 result->ref();
114
115 #ifdef IV_STRICT
116 // This is the preferred code for Inventor 2.1
117
118 // Using the new SoVertexProperty node is more efficient
119 SoVertexProperty *myVertexProperty = new SoVertexProperty;
120
121 // The material binding.
122 myVertexProperty->materialBinding = SoMaterialBinding::PER_FACE;
123
124 // Define colors for the faces
125 for (int i=0; i<12; i++)
126 myVertexProperty->orderedRGBA.set1Value(i, SbColor(colors[i]).getPackedValue());
127
128 // Define coordinates for vertices
129 myVertexProperty->vertex.setValues(0, 12, vertexPositions);
130
131 // Define the IndexedFaceSet, with indices into
132 // the vertices:
133 SoIndexedFaceSet *myFaceSet = new SoIndexedFaceSet;
134 myFaceSet->coordIndex.setValues(0, 72, indices);
135
136 myFaceSet->vertexProperty.setValue(myVertexProperty);
137 result->addChild(myFaceSet);
138
139 #else
140 // The material binding node.
141 SoMaterialBinding *myBinding = new SoMaterialBinding;
142 myBinding->value = SoMaterialBinding::PER_FACE;
143 result->addChild(myBinding);
144
145 // Define colors for the faces
146 SoMaterial *myMaterials = new SoMaterial;
147 myMaterials->diffuseColor.setValues(0, 12, colors);
148 result->addChild(myMaterials);
149
150 // Define coordinates for vertices
151 SoCoordinate3 *myCoords = new SoCoordinate3;
152 myCoords->point.setValues(0, 12, vertexPositions);
153 result->addChild(myCoords);
154
155 // Define the IndexedFaceSet, with indices into
156 // the vertices:
157 SoIndexedFaceSet *myFaceSet = new SoIndexedFaceSet;
158 myFaceSet->coordIndex.setValues(0, 72, indices);
159 result->addChild(myFaceSet);
160 #endif
161
162 result->unrefNoDelete();
163 return result;
164 }
165
166 int
167 main(int argc, char **argv)
168 {
169 int whichBinding = 0;
170
171 if (argc > 1) whichBinding = atoi(argv[1]);
172 if (whichBinding > 2 || whichBinding < 0 || argc == 1) {
173 fprintf(stderr, "Argument must be 0, 1, or 2\n");
174 fprintf(stderr, "\t0 = PER_FACE\n");
175 fprintf(stderr, "\t1 = PER_VERTEX_INDEXED\n");
176 fprintf(stderr, "\t2 = PER_FACE_INDEXED\n");
177 exit(1);
178 }
179
180 // Initialize Inventor and Xt
181 Widget myWindow = SoXt::init(argv[0]);
182 if (myWindow == NULL) exit(1);
183
184 SoSeparator *root = makeStellatedDodecahedron();
185 root->ref();
186
187 #ifdef IV_STRICT
188 // Get the indexed face set for editing
189 SoIndexedFaceSet *myIndexedFaceSet;
190 myIndexedFaceSet = (SoIndexedFaceSet *) root->getChild(0);
191
192 // Get the SoVertexProperty node for editing the material binding
193 SoVertexProperty *myVertexProperty;
194 myVertexProperty = (SoVertexProperty *)
195 myIndexedFaceSet->vertexProperty.getValue();
196
197 //////////////////////////////////////////////////////////////
198 // CODE FOR The Inventor Mentor STARTS HERE (Inventor 2.1)
199
200 // Which material to use to color the faces
201 // half red & half blue
202 static int32_t materialIndices[12] = {
203 0, 0, 0, 0, 0, 0,
204 1, 1, 1, 1, 1, 1,
205 };
206
207 switch(whichBinding) {
208 case 0:
209 // Set up binding to use a different color for each face
210 myVertexProperty->materialBinding = SoMaterialBinding::PER_FACE;
211 break;
212 case 1:
213 // Set up binding to use a different color at each
214 // vertex, BUT, vertices shared between faces will
215 // have the same color.
216 myVertexProperty->materialBinding = SoMaterialBinding::PER_VERTEX_INDEXED;
217 break;
218 case 2:
219 myVertexProperty->materialBinding = SoMaterialBinding::PER_FACE_INDEXED;
220 myIndexedFaceSet->materialIndex.setValues(
221 0, 12, materialIndices);
222 break;
223 }
224
225 // CODE FOR The Inventor Mentor ENDS HERE
226 //////////////////////////////////////////////////////////////
227
228 #else // old style
229 // Get the material binding node for editing
230 SoMaterialBinding *myBinding;
231 myBinding = (SoMaterialBinding *) root->getChild(0);
232
233 // Get the indexed face set for editing
234 SoIndexedFaceSet *myIndexedFaceSet;
235 myIndexedFaceSet = (SoIndexedFaceSet *) root->getChild(3);
236
237 //////////////////////////////////////////////////////////////
238 // CODE FOR The Inventor Mentor STARTS HERE
239
240 // Which material to use to color the faces
241 // half red & half blue
242 static int32_t materialIndices[12] = {
243 0, 0, 0, 0, 0, 0,
244 1, 1, 1, 1, 1, 1,
245 };
246
247 switch(whichBinding) {
248 case 0:
249 // Set up binding to use a different color for each face
250 myBinding->value = SoMaterialBinding::PER_FACE;
251 break;
252 case 1:
253 // Set up binding to use a different color at each
254 // vertex, BUT, vertices shared between faces will
255 // have the same color.
256 myBinding->value = SoMaterialBinding::PER_VERTEX_INDEXED;
257 break;
258 case 2:
259 myBinding->value = SoMaterialBinding::PER_FACE_INDEXED;
260 myIndexedFaceSet->materialIndex.setValues(
261 0, 12, materialIndices);
262 break;
263 }
264
265 // CODE FOR The Inventor Mentor ENDS HERE
266 //////////////////////////////////////////////////////////////
267 #endif
268
269
270 SoXtExaminerViewer *myViewer =
271 new SoXtExaminerViewer(myWindow);
272 myViewer->setSceneGraph(root);
273 myViewer->setTitle("Material Bindings");
274 myViewer->show();
275 myViewer->viewAll();
276
277 SoXt::show(myWindow);
278 SoXt::mainLoop();
279 }