| 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 2. |
| 40 |
* |
| 41 |
* This example creates an IndexedFaceSet. It creates |
| 42 |
* the first stellation of the dodecahedron. |
| 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/SoIndexedFaceSet.h> |
| 50 |
#include <Inventor/nodes/SoMaterial.h> |
| 51 |
#include <Inventor/nodes/SoMaterialBinding.h> |
| 52 |
#include <Inventor/nodes/SoSeparator.h> |
| 53 |
|
| 54 |
////////////////////////////////////////////////////////////// |
| 55 |
// CODE FOR The Inventor Mentor STARTS HERE |
| 56 |
|
| 57 |
// |
| 58 |
// Positions of all of the vertices: |
| 59 |
// |
| 60 |
static float vertexPositions[12][3] = |
| 61 |
{ |
| 62 |
{ 0.0000, 1.2142, 0.7453}, // top |
| 63 |
|
| 64 |
{ 0.0000, 1.2142, -0.7453}, // points surrounding top |
| 65 |
{-1.2142, 0.7453, 0.0000}, |
| 66 |
{-0.7453, 0.0000, 1.2142}, |
| 67 |
{ 0.7453, 0.0000, 1.2142}, |
| 68 |
{ 1.2142, 0.7453, 0.0000}, |
| 69 |
|
| 70 |
{ 0.0000, -1.2142, 0.7453}, // points surrounding bottom |
| 71 |
{-1.2142, -0.7453, 0.0000}, |
| 72 |
{-0.7453, 0.0000, -1.2142}, |
| 73 |
{ 0.7453, 0.0000, -1.2142}, |
| 74 |
{ 1.2142, -0.7453, 0.0000}, |
| 75 |
|
| 76 |
{ 0.0000, -1.2142, -0.7453}, // bottom |
| 77 |
}; |
| 78 |
|
| 79 |
// |
| 80 |
// Connectivity, information; 12 faces with 5 vertices each }, |
| 81 |
// (plus the end-of-face indicator for each face): |
| 82 |
// |
| 83 |
|
| 84 |
static int32_t indices[72] = |
| 85 |
{ |
| 86 |
1, 2, 3, 4, 5, SO_END_FACE_INDEX, // top face |
| 87 |
|
| 88 |
0, 1, 8, 7, 3, SO_END_FACE_INDEX, // 5 faces about top |
| 89 |
0, 2, 7, 6, 4, SO_END_FACE_INDEX, |
| 90 |
0, 3, 6, 10, 5, SO_END_FACE_INDEX, |
| 91 |
0, 4, 10, 9, 1, SO_END_FACE_INDEX, |
| 92 |
0, 5, 9, 8, 2, SO_END_FACE_INDEX, |
| 93 |
|
| 94 |
9, 5, 4, 6, 11, SO_END_FACE_INDEX, // 5 faces about bottom |
| 95 |
10, 4, 3, 7, 11, SO_END_FACE_INDEX, |
| 96 |
6, 3, 2, 8, 11, SO_END_FACE_INDEX, |
| 97 |
7, 2, 1, 9, 11, SO_END_FACE_INDEX, |
| 98 |
8, 1, 5,10, 11, SO_END_FACE_INDEX, |
| 99 |
|
| 100 |
6, 7, 8, 9, 10, SO_END_FACE_INDEX, // bottom face |
| 101 |
}; |
| 102 |
|
| 103 |
// Colors for the 12 faces |
| 104 |
static float colors[12][3] = |
| 105 |
{ |
| 106 |
{1.0, .0, 0}, { .0, .0, 1.0}, {0, .7, .7}, { .0, 1.0, 0}, |
| 107 |
{ .7, .7, 0}, { .7, .0, .7}, {0, .0, 1.0}, { .7, .0, .7}, |
| 108 |
{ .7, .7, 0}, { .0, 1.0, .0}, {0, .7, .7}, {1.0, .0, 0} |
| 109 |
}; |
| 110 |
|
| 111 |
// Routine to create a scene graph representing a dodecahedron |
| 112 |
SoSeparator * |
| 113 |
makeStellatedDodecahedron() |
| 114 |
{ |
| 115 |
SoSeparator *result = new SoSeparator; |
| 116 |
result->ref(); |
| 117 |
|
| 118 |
#ifdef IV_STRICT |
| 119 |
// This is the preferred code for Inventor 2.1 |
| 120 |
|
| 121 |
// Using the new SoVertexProperty node is more efficient |
| 122 |
SoVertexProperty *myVertexProperty = new SoVertexProperty; |
| 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 |
myVertexProperty->materialBinding = SoMaterialBinding::PER_FACE; |
| 128 |
|
| 129 |
// Define coordinates for vertices |
| 130 |
myVertexProperty->vertex.setValues(0, 12, vertexPositions); |
| 131 |
|
| 132 |
// Define the IndexedFaceSet, with indices into |
| 133 |
// the vertices: |
| 134 |
SoIndexedFaceSet *myFaceSet = new SoIndexedFaceSet; |
| 135 |
myFaceSet->coordIndex.setValues(0, 72, indices); |
| 136 |
|
| 137 |
myFaceSet->vertexProperty.setValue(myVertexProperty); |
| 138 |
result->addChild(myFaceSet); |
| 139 |
|
| 140 |
#else |
| 141 |
// Define colors for the faces |
| 142 |
SoMaterial *myMaterials = new SoMaterial; |
| 143 |
myMaterials->diffuseColor.setValues(0, 12, colors); |
| 144 |
result->addChild(myMaterials); |
| 145 |
SoMaterialBinding *myMaterialBinding = new SoMaterialBinding; |
| 146 |
myMaterialBinding->value = SoMaterialBinding::PER_FACE; |
| 147 |
result->addChild(myMaterialBinding); |
| 148 |
|
| 149 |
// Define coordinates for vertices |
| 150 |
SoCoordinate3 *myCoords = new SoCoordinate3; |
| 151 |
myCoords->point.setValues(0, 12, vertexPositions); |
| 152 |
result->addChild(myCoords); |
| 153 |
|
| 154 |
// Define the IndexedFaceSet, with indices into |
| 155 |
// the vertices: |
| 156 |
SoIndexedFaceSet *myFaceSet = new SoIndexedFaceSet; |
| 157 |
myFaceSet->coordIndex.setValues(0, 72, indices); |
| 158 |
result->addChild(myFaceSet); |
| 159 |
#endif |
| 160 |
|
| 161 |
result->unrefNoDelete(); |
| 162 |
return result; |
| 163 |
} |
| 164 |
|
| 165 |
// CODE FOR The Inventor Mentor ENDS HERE |
| 166 |
////////////////////////////////////////////////////////////// |
| 167 |
|
| 168 |
int |
| 169 |
main(int, char **argv) |
| 170 |
{ |
| 171 |
// Initialize Inventor and Xt |
| 172 |
Widget myWindow = SoXt::init(argv[0]); |
| 173 |
if (myWindow == NULL) exit(1); |
| 174 |
|
| 175 |
SoSeparator *root = makeStellatedDodecahedron(); |
| 176 |
root->ref(); |
| 177 |
|
| 178 |
SoXtExaminerViewer *myViewer = |
| 179 |
new SoXtExaminerViewer(myWindow); |
| 180 |
myViewer->setSceneGraph(root); |
| 181 |
myViewer->setTitle("Indexed Face Set: Stellated Dodecahedron"); |
| 182 |
myViewer->show(); |
| 183 |
myViewer->viewAll(); |
| 184 |
|
| 185 |
SoXt::show(myWindow); |
| 186 |
SoXt::mainLoop(); |
| 187 |
} |
| 188 |
|