| 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 8, example 1. |
| 40 |
* |
| 41 |
* This example creates and displays a B-Spline curve. |
| 42 |
* The curve is order 3 with 7 control points and a knot |
| 43 |
* vector of length 10. One of its knots has multiplicity |
| 44 |
* 2 to illustrate a curve with a spike in it. |
| 45 |
*--------------------------------------------------------*/ |
| 46 |
|
| 47 |
#include <cstdlib> |
| 48 |
#include <Inventor/SoDB.h> |
| 49 |
#include <Inventor/SoInput.h> |
| 50 |
#include <Inventor/Xt/SoXt.h> |
| 51 |
#include <Inventor/Xt/viewers/SoXtExaminerViewer.h> |
| 52 |
#include <Inventor/nodes/SoBaseColor.h> |
| 53 |
#include <Inventor/nodes/SoCamera.h> |
| 54 |
#include <Inventor/nodes/SoComplexity.h> |
| 55 |
#include <Inventor/nodes/SoCoordinate3.h> |
| 56 |
#include <Inventor/nodes/SoDrawStyle.h> |
| 57 |
#include <Inventor/nodes/SoLightModel.h> |
| 58 |
#include <Inventor/nodes/SoMaterial.h> |
| 59 |
#include <Inventor/nodes/SoNurbsCurve.h> |
| 60 |
#include <Inventor/nodes/SoRotation.h> |
| 61 |
#include <Inventor/nodes/SoScale.h> |
| 62 |
#include <Inventor/nodes/SoSeparator.h> |
| 63 |
#include <Inventor/nodes/SoTranslation.h> |
| 64 |
|
| 65 |
static char *floorData = |
| 66 |
"#Inventor V2.0 ascii\n" |
| 67 |
"Separator {\n" |
| 68 |
" SpotLight {\n" |
| 69 |
" cutOffAngle 0.9\n" |
| 70 |
" dropOffRate 0.2\n" |
| 71 |
" location 6 12 2\n" |
| 72 |
" direction 0 -1 0\n" |
| 73 |
" }\n" |
| 74 |
" ShapeHints {\n" |
| 75 |
" faceType UNKNOWN_FACE_TYPE\n" |
| 76 |
" }\n" |
| 77 |
" Texture2Transform {\n" |
| 78 |
" #rotation 1.57\n" |
| 79 |
" scaleFactor 8 8\n" |
| 80 |
" }\n" |
| 81 |
" Texture2 {\n" |
| 82 |
" filename /usr/share/src/Inventor/examples/data/oak.rgb\n" |
| 83 |
" }\n" |
| 84 |
" NormalBinding {\n" |
| 85 |
" value PER_PART\n" |
| 86 |
" }\n" |
| 87 |
" Material { diffuseColor 1 1 1 specularColor 1 1 1 shininess 0.4 }\n" |
| 88 |
" DEF FloorPanel Separator {\n" |
| 89 |
" DEF FloorStrip Separator {\n" |
| 90 |
" DEF FloorBoard Separator {\n" |
| 91 |
" Normal { vector 0 1 0 }\n" |
| 92 |
" TextureCoordinate2 {\n" |
| 93 |
" point [ 0 0, 0.5 0, 0.5 2, 0.5 4, 0.5 6,\n" |
| 94 |
" 0.5 8, 0 8, 0 6, 0 4, 0 2 ] }\n" |
| 95 |
" Coordinate3 {\n" |
| 96 |
" point [ 0 0 0, .5 0 0, .5 0 -2, .5 0 -4, .5 0 -6,\n" |
| 97 |
" .5 0 -8, 0 0 -8, 0 0 -6, 0 0 -4, 0 0 -2, ]\n" |
| 98 |
" }\n" |
| 99 |
" FaceSet { numVertices 10 }\n" |
| 100 |
" BaseColor { rgb 0.3 0.1 0.0 }\n" |
| 101 |
" Translation { translation 0.125 0 -0.333 }\n" |
| 102 |
" Cylinder { parts TOP radius 0.04167 height 0.002 }\n" |
| 103 |
" Translation { translation 0.25 0 0 }\n" |
| 104 |
" Cylinder { parts TOP radius 0.04167 height 0.002 }\n" |
| 105 |
" Translation { translation 0 0 -7.333 }\n" |
| 106 |
" Cylinder { parts TOP radius 0.04167 height 0.002 }\n" |
| 107 |
" Translation { translation -0.25 0 0 }\n" |
| 108 |
" Cylinder { parts TOP radius 0.04167 height 0.002 }\n" |
| 109 |
" }\n" |
| 110 |
" Translation { translation 0 0 8.03 }\n" |
| 111 |
" USE FloorBoard\n" |
| 112 |
" Translation { translation 0 0 8.04 }\n" |
| 113 |
" USE FloorBoard\n" |
| 114 |
" }\n" |
| 115 |
" Translation { translation 0.53 0 -0.87 }\n" |
| 116 |
" USE FloorStrip\n" |
| 117 |
" Translation { translation 0.53 0 -2.3 }\n" |
| 118 |
" USE FloorStrip\n" |
| 119 |
" Translation { translation 0.53 0 1.3 }\n" |
| 120 |
" USE FloorStrip\n" |
| 121 |
" Translation { translation 0.53 0 1.1 }\n" |
| 122 |
" USE FloorStrip\n" |
| 123 |
" Translation { translation 0.53 0 -0.87 }\n" |
| 124 |
" USE FloorStrip\n" |
| 125 |
" Translation { translation 0.53 0 1.7 }\n" |
| 126 |
" USE FloorStrip\n" |
| 127 |
" Translation { translation 0.53 0 -0.5 }\n" |
| 128 |
" USE FloorStrip\n" |
| 129 |
" }\n" |
| 130 |
" Translation { translation 4.24 0 0 }\n" |
| 131 |
" USE FloorPanel\n" |
| 132 |
" Translation { translation 4.24 0 0 }\n" |
| 133 |
" USE FloorPanel\n" |
| 134 |
"}"; |
| 135 |
|
| 136 |
//////////////////////////////////////////////////////////// |
| 137 |
// CODE FOR The Inventor Mentor STARTS HERE |
| 138 |
|
| 139 |
// The control points for this curve |
| 140 |
float pts[7][3] = { |
| 141 |
{ 4.0, -6.0, 6.0}, |
| 142 |
{-4.0, 1.0, 0.0}, |
| 143 |
{-1.5, 5.0, -6.0}, |
| 144 |
{ 0.0, 2.0, -2.0}, |
| 145 |
{ 1.5, 5.0, -6.0}, |
| 146 |
{ 4.0, 1.0, 0.0}, |
| 147 |
{-4.0, -6.0, 6.0}}; |
| 148 |
|
| 149 |
// The knot vector |
| 150 |
float knots[10] = {1, 2, 3, 4, 5, 5, 6, 7, 8, 9}; |
| 151 |
|
| 152 |
// Create the nodes needed for the B-Spline curve. |
| 153 |
SoSeparator * |
| 154 |
makeCurve() |
| 155 |
{ |
| 156 |
SoSeparator *curveSep = new SoSeparator(); |
| 157 |
curveSep->ref(); |
| 158 |
|
| 159 |
// Set the draw style of the curve. |
| 160 |
SoDrawStyle *drawStyle = new SoDrawStyle; |
| 161 |
drawStyle->lineWidth = 4; |
| 162 |
curveSep->addChild(drawStyle); |
| 163 |
|
| 164 |
// Define the NURBS curve including the control points |
| 165 |
// and a complexity. |
| 166 |
SoComplexity *complexity = new SoComplexity; |
| 167 |
SoCoordinate3 *controlPts = new SoCoordinate3; |
| 168 |
SoNurbsCurve *curve = new SoNurbsCurve; |
| 169 |
complexity->value = 0.8; |
| 170 |
controlPts->point.setValues(0, 7, pts); |
| 171 |
curve->numControlPoints = 7; |
| 172 |
curve->knotVector.setValues(0, 10, knots); |
| 173 |
curveSep->addChild(complexity); |
| 174 |
curveSep->addChild(controlPts); |
| 175 |
curveSep->addChild(curve); |
| 176 |
|
| 177 |
curveSep->unrefNoDelete(); |
| 178 |
return curveSep; |
| 179 |
} |
| 180 |
|
| 181 |
// CODE FOR The Inventor Mentor ENDS HERE |
| 182 |
//////////////////////////////////////////////////////////// |
| 183 |
|
| 184 |
int |
| 185 |
main(int, char **argv) |
| 186 |
{ |
| 187 |
// Initialize Inventor and Xt |
| 188 |
Widget appWindow = SoXt::init(argv[0]); |
| 189 |
if (appWindow == NULL) |
| 190 |
exit(1); |
| 191 |
|
| 192 |
SoSeparator *root = new SoSeparator; |
| 193 |
root->ref(); |
| 194 |
|
| 195 |
// Create the scene graph for the heart |
| 196 |
SoSeparator *heart = new SoSeparator; |
| 197 |
SoSeparator *curveSep = makeCurve(); |
| 198 |
SoLightModel *lmodel = new SoLightModel; |
| 199 |
SoBaseColor *clr = new SoBaseColor; |
| 200 |
|
| 201 |
lmodel->model = SoLightModel::BASE_COLOR; |
| 202 |
clr->rgb.setValue(SbColor(1.0, 0.0, 0.1)); |
| 203 |
heart->addChild(lmodel); |
| 204 |
heart->addChild(clr); |
| 205 |
heart->addChild(curveSep); |
| 206 |
root->addChild(heart); |
| 207 |
|
| 208 |
// Create the scene graph for the floor |
| 209 |
SoSeparator *floor = new SoSeparator; |
| 210 |
SoTranslation *xlate = new SoTranslation; |
| 211 |
SoRotation *rot = new SoRotation; |
| 212 |
SoScale *scale = new SoScale; |
| 213 |
SoInput in; |
| 214 |
SoNode *result; |
| 215 |
|
| 216 |
in.setBuffer(floorData, strlen(floorData)); |
| 217 |
SoDB::read(&in, result); |
| 218 |
xlate->translation.setValue(SbVec3f(-12.0, -5.0, -5.0)); |
| 219 |
scale->scaleFactor.setValue(SbVec3f(2.0, 1.0, 2.0)); |
| 220 |
rot->rotation.setValue(SbRotation(SbVec3f(0.0, 1.0, 0.0), M_PI/2.0)); |
| 221 |
floor->addChild(rot); |
| 222 |
floor->addChild(xlate); |
| 223 |
floor->addChild(scale); |
| 224 |
floor->addChild(result); |
| 225 |
root->addChild(floor); |
| 226 |
|
| 227 |
// Create the scene graph for the heart's shadow |
| 228 |
SoSeparator *shadow = new SoSeparator; |
| 229 |
SoLightModel *shmdl = new SoLightModel; |
| 230 |
SoMaterial *shmtl = new SoMaterial; |
| 231 |
SoBaseColor *shclr = new SoBaseColor; |
| 232 |
SoTranslation *shxl = new SoTranslation; |
| 233 |
SoScale *shscl = new SoScale; |
| 234 |
|
| 235 |
shmdl->model = SoLightModel::BASE_COLOR; |
| 236 |
shclr->rgb.setValue(SbColor(0.21, 0.15, 0.09)); |
| 237 |
shmtl->transparency = 0.5; |
| 238 |
shxl->translation.setValue(SbVec3f(0.0, -4.9, 0.0)); |
| 239 |
shscl->scaleFactor.setValue(SbVec3f(1.0, 0.0, 1.0)); |
| 240 |
shadow->addChild(shmtl); |
| 241 |
shadow->addChild(shmdl); |
| 242 |
shadow->addChild(shclr); |
| 243 |
shadow->addChild(shxl); |
| 244 |
shadow->addChild(shscl); |
| 245 |
shadow->addChild(curveSep); |
| 246 |
root->addChild(shadow); |
| 247 |
|
| 248 |
// Initialize an Examiner Viewer |
| 249 |
SoXtExaminerViewer *viewer = |
| 250 |
new SoXtExaminerViewer(appWindow); |
| 251 |
viewer->setSceneGraph(root); |
| 252 |
viewer->setTitle("B-Spline Curve"); |
| 253 |
SoCamera *cam = viewer->getCamera(); |
| 254 |
cam->position.setValue(SbVec3f(-6.0, 8.0, 20.0)); |
| 255 |
cam->pointAt(SbVec3f(0.0, -2.0, -4.0)); |
| 256 |
viewer->show(); |
| 257 |
|
| 258 |
SoXt::show(appWindow); |
| 259 |
SoXt::mainLoop(); |
| 260 |
} |
| 261 |
|