| 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 1. |
| 40 |
* |
| 41 |
* This example builds an obelisk using the Face Set node. |
| 42 |
*------------------------------------------------------------*/ |
| 43 |
|
| 44 |
#include <cstdlib> |
| 45 |
#include <Inventor/Xt/SoXt.h> |
| 46 |
#include <Inventor/Xt/viewers/SoXtExaminerViewer.h> |
| 47 |
#include <Inventor/nodes/SoCoordinate3.h> |
| 48 |
#include <Inventor/nodes/SoFaceSet.h> |
| 49 |
#include <Inventor/nodes/SoMaterial.h> |
| 50 |
#include <Inventor/nodes/SoNormal.h> |
| 51 |
#include <Inventor/nodes/SoNormalBinding.h> |
| 52 |
#include <Inventor/nodes/SoSeparator.h> |
| 53 |
#include <Inventor/nodes/SoVertexProperty.h> |
| 54 |
|
| 55 |
////////////////////////////////////////////////////////////// |
| 56 |
// CODE FOR The Inventor Mentor STARTS HERE |
| 57 |
|
| 58 |
// Eight polygons. The first four are triangles |
| 59 |
// The second four are quadrilaterals for the sides. |
| 60 |
static float vertices[28][3] = |
| 61 |
{ |
| 62 |
{ 0, 30, 0}, {-2,27, 2}, { 2,27, 2}, //front tri |
| 63 |
{ 0, 30, 0}, {-2,27,-2}, {-2,27, 2}, //left tri |
| 64 |
{ 0, 30, 0}, { 2,27,-2}, {-2,27,-2}, //rear tri |
| 65 |
{ 0, 30, 0}, { 2,27, 2}, { 2,27,-2}, //right tri |
| 66 |
{-2, 27, 2}, {-4,0, 4}, { 4,0, 4}, { 2,27, 2}, //front quad |
| 67 |
{-2, 27,-2}, {-4,0,-4}, {-4,0, 4}, {-2,27, 2}, //left quad |
| 68 |
{ 2, 27,-2}, { 4,0,-4}, {-4,0,-4}, {-2,27,-2}, //rear quad |
| 69 |
{ 2, 27, 2}, { 4,0, 4}, { 4,0,-4}, { 2,27,-2} //right quad |
| 70 |
}; |
| 71 |
|
| 72 |
// Number of vertices in each polygon: |
| 73 |
static int32_t numvertices[8] = {3, 3, 3, 3, 4, 4, 4, 4}; |
| 74 |
|
| 75 |
// Normals for each polygon: |
| 76 |
static float norms[8][3] = |
| 77 |
{ |
| 78 |
{0, .555, .832}, {-.832, .555, 0}, //front, left tris |
| 79 |
{0, .555, -.832}, { .832, .555, 0}, //rear, right tris |
| 80 |
|
| 81 |
{0, .0739, .9973}, {-.9972, .0739, 0},//front, left quads |
| 82 |
{0, .0739, -.9973}, { .9972, .0739, 0},//rear, right quads |
| 83 |
}; |
| 84 |
|
| 85 |
SoSeparator * |
| 86 |
makeObeliskFaceSet() |
| 87 |
{ |
| 88 |
SoSeparator *obelisk = new SoSeparator(); |
| 89 |
obelisk->ref(); |
| 90 |
|
| 91 |
#ifdef IV_STRICT |
| 92 |
// This is the preferred code for Inventor 2.1 |
| 93 |
|
| 94 |
// Using the new SoVertexProperty node is more efficient |
| 95 |
SoVertexProperty *myVertexProperty = new SoVertexProperty; |
| 96 |
|
| 97 |
// Define the normals used: |
| 98 |
myVertexProperty->normal.setValues(0, 8, norms); |
| 99 |
myVertexProperty->normalBinding = SoNormalBinding::PER_FACE; |
| 100 |
|
| 101 |
// Define material for obelisk |
| 102 |
myVertexProperty->orderedRGBA.setValue(SbColor(.4,.4,.4).getPackedValue()); |
| 103 |
|
| 104 |
// Define coordinates for vertices |
| 105 |
myVertexProperty->vertex.setValues(0, 28, vertices); |
| 106 |
|
| 107 |
// Define the FaceSet |
| 108 |
SoFaceSet *myFaceSet = new SoFaceSet; |
| 109 |
myFaceSet->numVertices.setValues(0, 8, numvertices); |
| 110 |
|
| 111 |
myFaceSet->vertexProperty.setValue(myVertexProperty); |
| 112 |
obelisk->addChild(myFaceSet); |
| 113 |
|
| 114 |
#else |
| 115 |
// Define the normals used: |
| 116 |
SoNormal *myNormals = new SoNormal; |
| 117 |
myNormals->vector.setValues(0, 8, norms); |
| 118 |
obelisk->addChild(myNormals); |
| 119 |
SoNormalBinding *myNormalBinding = new SoNormalBinding; |
| 120 |
myNormalBinding->value = SoNormalBinding::PER_FACE; |
| 121 |
obelisk->addChild(myNormalBinding); |
| 122 |
|
| 123 |
// Define material for obelisk |
| 124 |
SoMaterial *myMaterial = new SoMaterial; |
| 125 |
myMaterial->diffuseColor.setValue(.4, .4, .4); |
| 126 |
obelisk->addChild(myMaterial); |
| 127 |
|
| 128 |
// Define coordinates for vertices |
| 129 |
SoCoordinate3 *myCoords = new SoCoordinate3; |
| 130 |
myCoords->point.setValues(0, 28, vertices); |
| 131 |
obelisk->addChild(myCoords); |
| 132 |
|
| 133 |
// Define the FaceSet |
| 134 |
SoFaceSet *myFaceSet = new SoFaceSet; |
| 135 |
myFaceSet->numVertices.setValues(0, 8, numvertices); |
| 136 |
obelisk->addChild(myFaceSet); |
| 137 |
#endif |
| 138 |
|
| 139 |
obelisk->unrefNoDelete(); |
| 140 |
return obelisk; |
| 141 |
} |
| 142 |
|
| 143 |
// CODE FOR The Inventor Mentor ENDS HERE |
| 144 |
////////////////////////////////////////////////////////////// |
| 145 |
|
| 146 |
int |
| 147 |
main(int, char **argv) |
| 148 |
{ |
| 149 |
// Initialize Inventor and Xt |
| 150 |
Widget myWindow = SoXt::init(argv[0]); |
| 151 |
if (myWindow == NULL) exit(1); |
| 152 |
|
| 153 |
SoSeparator *root = new SoSeparator; |
| 154 |
root->ref(); |
| 155 |
|
| 156 |
root->addChild(makeObeliskFaceSet()); |
| 157 |
|
| 158 |
SoXtExaminerViewer *myViewer = |
| 159 |
new SoXtExaminerViewer(myWindow); |
| 160 |
myViewer->setSceneGraph(root); |
| 161 |
myViewer->setTitle("Face Set: Obelisk"); |
| 162 |
myViewer->show(); |
| 163 |
myViewer->viewAll(); |
| 164 |
|
| 165 |
SoXt::show(myWindow); |
| 166 |
SoXt::mainLoop(); |
| 167 |
} |
| 168 |
|