| 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 Programming Guide, |
| 39 |
* chapter 8, example 4. |
| 40 |
* |
| 41 |
* This example creates and displays a Bezier Surface |
| 42 |
* with trim curves. The surface is identical to the |
| 43 |
* surface in example 08.3.BezSurf. The SoNurbsProfile |
| 44 |
* class is used to define the trims. |
| 45 |
* |
| 46 |
* One trim curve makes a counter-clockwise square around |
| 47 |
* the whole surface. Two other trim curves are combined |
| 48 |
* end to end to cut a hole from the surface. The outside |
| 49 |
* trim curve and the first inside trim curve are both |
| 50 |
* order 2 curves and are therefore sets of straight lines |
| 51 |
* in parameter space. The second inside curve is a |
| 52 |
* Bezier curve. |
| 53 |
*----------------------------------------------------------------*/ |
| 54 |
|
| 55 |
#include <cstdlib> |
| 56 |
#include <Inventor/SoDB.h> |
| 57 |
#include <Inventor/SoInput.h> |
| 58 |
#include <Inventor/Xt/SoXt.h> |
| 59 |
#include <Inventor/Xt/viewers/SoXtExaminerViewer.h> |
| 60 |
#include <Inventor/nodes/SoBaseColor.h> |
| 61 |
#include <Inventor/nodes/SoCamera.h> |
| 62 |
#include <Inventor/nodes/SoComplexity.h> |
| 63 |
#include <Inventor/nodes/SoCoordinate3.h> |
| 64 |
#include <Inventor/nodes/SoDrawStyle.h> |
| 65 |
#include <Inventor/nodes/SoLightModel.h> |
| 66 |
#include <Inventor/nodes/SoMaterial.h> |
| 67 |
#include <Inventor/nodes/SoNurbsProfile.h> |
| 68 |
#include <Inventor/nodes/SoNurbsSurface.h> |
| 69 |
#include <Inventor/nodes/SoProfileCoordinate2.h> |
| 70 |
#include <Inventor/nodes/SoRotation.h> |
| 71 |
#include <Inventor/nodes/SoScale.h> |
| 72 |
#include <Inventor/nodes/SoSeparator.h> |
| 73 |
#include <Inventor/nodes/SoTexture2.h> |
| 74 |
#include <Inventor/nodes/SoTranslation.h> |
| 75 |
|
| 76 |
static char *floorData = |
| 77 |
"#Inventor V2.0 ascii\n" |
| 78 |
"Separator {\n" |
| 79 |
" SpotLight {\n" |
| 80 |
" cutOffAngle 0.9\n" |
| 81 |
" dropOffRate 0.2\n" |
| 82 |
" location 6 12 2\n" |
| 83 |
" direction 0 -1 0\n" |
| 84 |
" }\n" |
| 85 |
" ShapeHints {\n" |
| 86 |
" faceType UNKNOWN_FACE_TYPE\n" |
| 87 |
" }\n" |
| 88 |
" Texture2Transform {\n" |
| 89 |
" #rotation 1.57\n" |
| 90 |
" scaleFactor 8 8\n" |
| 91 |
" }\n" |
| 92 |
" Texture2 {\n" |
| 93 |
" filename /usr/share/src/Inventor/examples/data/oak.rgb\n" |
| 94 |
" }\n" |
| 95 |
" NormalBinding {\n" |
| 96 |
" value PER_PART\n" |
| 97 |
" }\n" |
| 98 |
" Material { diffuseColor 1 1 1 specularColor 1 1 1 shininess 0.4 }\n" |
| 99 |
" DEF FloorPanel Separator {\n" |
| 100 |
" DEF FloorStrip Separator {\n" |
| 101 |
" DEF FloorBoard Separator {\n" |
| 102 |
" Normal { vector 0 1 0 }\n" |
| 103 |
" TextureCoordinate2 {\n" |
| 104 |
" point [ 0 0, 0.5 0, 0.5 2, 0.5 4, 0.5 6,\n" |
| 105 |
" 0.5 8, 0 8, 0 6, 0 4, 0 2 ] }\n" |
| 106 |
" Coordinate3 {\n" |
| 107 |
" point [ 0 0 0, .5 0 0, .5 0 -2, .5 0 -4, .5 0 -6,\n" |
| 108 |
" .5 0 -8, 0 0 -8, 0 0 -6, 0 0 -4, 0 0 -2, ]\n" |
| 109 |
" }\n" |
| 110 |
" FaceSet { numVertices 10 }\n" |
| 111 |
" BaseColor { rgb 0.3 0.1 0.0 }\n" |
| 112 |
" Translation { translation 0.125 0 -0.333 }\n" |
| 113 |
" Cylinder { parts TOP radius 0.04167 height 0.002 }\n" |
| 114 |
" Translation { translation 0.25 0 0 }\n" |
| 115 |
" Cylinder { parts TOP radius 0.04167 height 0.002 }\n" |
| 116 |
" Translation { translation 0 0 -7.333 }\n" |
| 117 |
" Cylinder { parts TOP radius 0.04167 height 0.002 }\n" |
| 118 |
" Translation { translation -0.25 0 0 }\n" |
| 119 |
" Cylinder { parts TOP radius 0.04167 height 0.002 }\n" |
| 120 |
" }\n" |
| 121 |
" Translation { translation 0 0 8.03 }\n" |
| 122 |
" USE FloorBoard\n" |
| 123 |
" Translation { translation 0 0 8.04 }\n" |
| 124 |
" USE FloorBoard\n" |
| 125 |
" }\n" |
| 126 |
" Translation { translation 0.53 0 -0.87 }\n" |
| 127 |
" USE FloorStrip\n" |
| 128 |
" Translation { translation 0.53 0 -2.3 }\n" |
| 129 |
" USE FloorStrip\n" |
| 130 |
" Translation { translation 0.53 0 1.3 }\n" |
| 131 |
" USE FloorStrip\n" |
| 132 |
" Translation { translation 0.53 0 1.1 }\n" |
| 133 |
" USE FloorStrip\n" |
| 134 |
" Translation { translation 0.53 0 -0.87 }\n" |
| 135 |
" USE FloorStrip\n" |
| 136 |
" Translation { translation 0.53 0 1.7 }\n" |
| 137 |
" USE FloorStrip\n" |
| 138 |
" Translation { translation 0.53 0 -0.5 }\n" |
| 139 |
" USE FloorStrip\n" |
| 140 |
" }\n" |
| 141 |
" Translation { translation 4.24 0 0 }\n" |
| 142 |
" USE FloorPanel\n" |
| 143 |
" Translation { translation 4.24 0 0 }\n" |
| 144 |
" USE FloorPanel\n" |
| 145 |
"}"; |
| 146 |
|
| 147 |
//////////////////////////////////////////////////////////// |
| 148 |
// CODE FOR The Inventor Mentor STARTS HERE |
| 149 |
|
| 150 |
// The array of trim coordinates |
| 151 |
float tpts[12][2] = { |
| 152 |
{0.0, 0.0}, |
| 153 |
{1.0, 0.0}, |
| 154 |
{1.0, 1.0}, |
| 155 |
{0.0, 1.0}, |
| 156 |
{0.2, 0.2}, |
| 157 |
{0.2, 0.7}, |
| 158 |
{0.9, 0.7}, |
| 159 |
{0.9, 0.2}, |
| 160 |
{0.7, 0.0}, |
| 161 |
{0.4, 0.8}}; |
| 162 |
|
| 163 |
// The 16 coordinates defining the Bezier surface. |
| 164 |
float pts[16][3] = { |
| 165 |
{-4.5, -2.0, 8.0}, |
| 166 |
{-2.0, 1.0, 8.0}, |
| 167 |
{ 2.0, -3.0, 6.0}, |
| 168 |
{ 5.0, -1.0, 8.0}, |
| 169 |
{-3.0, 3.0, 4.0}, |
| 170 |
{ 0.0, -1.0, 4.0}, |
| 171 |
{ 1.0, -1.0, 4.0}, |
| 172 |
{ 3.0, 2.0, 4.0}, |
| 173 |
{-5.0, -2.0, -2.0}, |
| 174 |
{-2.0, -4.0, -2.0}, |
| 175 |
{ 2.0, -1.0, -2.0}, |
| 176 |
{ 5.0, 0.0, -2.0}, |
| 177 |
{-4.5, 2.0, -6.0}, |
| 178 |
{-2.0, -4.0, -5.0}, |
| 179 |
{ 2.0, 3.0, -5.0}, |
| 180 |
{ 4.5, -2.0, -6.0}}; |
| 181 |
|
| 182 |
// The 3 knot vectors for the 3 trim curves. |
| 183 |
float tknots1[7] = {0, 0, 1, 2, 3, 4, 4}; |
| 184 |
float tknots2[6] = {0, 0, 1, 2, 3, 3}; |
| 185 |
float tknots3[8] = {0, 0, 0, 0, 1, 1, 1, 1}; |
| 186 |
|
| 187 |
// The Bezier knot vector for the surface. |
| 188 |
// This knot vector is used in both the U and |
| 189 |
// V directions. |
| 190 |
float knots[8] = {0, 0, 0, 0, 1, 1, 1, 1}; |
| 191 |
|
| 192 |
// Create the nodes needed for the Bezier patch |
| 193 |
// and its trim curves. |
| 194 |
SoSeparator * |
| 195 |
makeSurface() |
| 196 |
{ |
| 197 |
SoSeparator *surfSep = new SoSeparator(); |
| 198 |
surfSep->ref(); |
| 199 |
|
| 200 |
// Define the Bezier surface including the control |
| 201 |
// points, trim curve, and a complexity. |
| 202 |
SoComplexity *complexity = new SoComplexity; |
| 203 |
SoCoordinate3 *controlPts = new SoCoordinate3; |
| 204 |
SoNurbsSurface *surface = new SoNurbsSurface; |
| 205 |
complexity->value = 0.7; |
| 206 |
controlPts->point.setValues(0, 16, pts); |
| 207 |
surface->numUControlPoints.setValue(4); |
| 208 |
surface->numVControlPoints.setValue(4); |
| 209 |
surface->uKnotVector.setValues(0, 8, knots); |
| 210 |
surface->vKnotVector.setValues(0, 8, knots); |
| 211 |
surfSep->addChild(complexity); |
| 212 |
surfSep->addChild(controlPts); |
| 213 |
|
| 214 |
SoProfileCoordinate2 *trimPts = new SoProfileCoordinate2; |
| 215 |
SoNurbsProfile *nTrim1 = new SoNurbsProfile; |
| 216 |
SoNurbsProfile *nTrim2 = new SoNurbsProfile; |
| 217 |
SoNurbsProfile *nTrim3 = new SoNurbsProfile; |
| 218 |
int32_t trimInds[5]; |
| 219 |
|
| 220 |
trimPts->point.setValues(0, 12, tpts); |
| 221 |
trimInds[0] = 0; |
| 222 |
trimInds[1] = 1; |
| 223 |
trimInds[2] = 2; |
| 224 |
trimInds[3] = 3; |
| 225 |
trimInds[4] = 0; |
| 226 |
nTrim1->index.setValues(0, 5, trimInds); |
| 227 |
nTrim1->knotVector.setValues(0, 7, tknots1); |
| 228 |
trimInds[0] = 4; |
| 229 |
trimInds[1] = 5; |
| 230 |
trimInds[2] = 6; |
| 231 |
trimInds[3] = 7; |
| 232 |
nTrim2->linkage.setValue(SoProfile::START_NEW); |
| 233 |
nTrim2->index.setValues(0, 4, trimInds); |
| 234 |
nTrim2->knotVector.setValues(0, 6, tknots2); |
| 235 |
trimInds[0] = 7; |
| 236 |
trimInds[1] = 8; |
| 237 |
trimInds[2] = 9; |
| 238 |
trimInds[3] = 4; |
| 239 |
nTrim3->linkage.setValue(SoProfile::ADD_TO_CURRENT); |
| 240 |
nTrim3->index.setValues(0, 4, trimInds); |
| 241 |
nTrim3->knotVector.setValues(0, 8, tknots3); |
| 242 |
|
| 243 |
surfSep->addChild(trimPts); |
| 244 |
surfSep->addChild(nTrim1); |
| 245 |
surfSep->addChild(nTrim2); |
| 246 |
surfSep->addChild(nTrim3); |
| 247 |
surfSep->addChild(surface); |
| 248 |
|
| 249 |
surfSep->unrefNoDelete(); |
| 250 |
return surfSep; |
| 251 |
} |
| 252 |
|
| 253 |
// CODE FOR The Inventor Mentor ENDS HERE |
| 254 |
//////////////////////////////////////////////////////////// |
| 255 |
|
| 256 |
int |
| 257 |
main(int, char **argv) |
| 258 |
{ |
| 259 |
// Initialize Inventor and Xt |
| 260 |
Widget appWindow = SoXt::init(argv[0]); |
| 261 |
if (appWindow == NULL) |
| 262 |
exit(1); |
| 263 |
|
| 264 |
SoSeparator *root = new SoSeparator; |
| 265 |
root->ref(); |
| 266 |
|
| 267 |
SoRotation *rot = new SoRotation; |
| 268 |
rot->rotation.setValue(SbRotation(SbVec3f(0.0, 1.0, 0.0), |
| 269 |
M_PI/2.0)); |
| 270 |
root->addChild(rot); |
| 271 |
|
| 272 |
// Create the scene graph for the carpet; |
| 273 |
SoSeparator *carpet = new SoSeparator; |
| 274 |
SoSeparator *surf = makeSurface(); |
| 275 |
SoTexture2 *tex = new SoTexture2; |
| 276 |
|
| 277 |
tex->ref(); |
| 278 |
tex->filename.setValue("/usr/share/src/Inventor/examples/data/diamondRug.rgb"); |
| 279 |
carpet->addChild(tex); |
| 280 |
carpet->addChild(surf); |
| 281 |
root->addChild(carpet); |
| 282 |
|
| 283 |
// Create the scene graph for the floor |
| 284 |
SoSeparator *floor = new SoSeparator; |
| 285 |
SoTranslation *xlate = new SoTranslation; |
| 286 |
SoScale *scale = new SoScale; |
| 287 |
SoInput in; |
| 288 |
SoNode *result; |
| 289 |
|
| 290 |
in.setBuffer(floorData, strlen(floorData)); |
| 291 |
SoDB::read(&in, result); |
| 292 |
xlate->translation.setValue(SbVec3f(-12.0, -5.0, -5.0)); |
| 293 |
scale->scaleFactor.setValue(SbVec3f(2.0, 1.0, 2.0)); |
| 294 |
floor->addChild(xlate); |
| 295 |
floor->addChild(scale); |
| 296 |
floor->addChild(result); |
| 297 |
root->addChild(floor); |
| 298 |
|
| 299 |
// Create the scene graph for the carpet's shadow |
| 300 |
SoSeparator *shadow = new SoSeparator; |
| 301 |
SoLightModel *shmdl = new SoLightModel; |
| 302 |
SoMaterial *shmtl = new SoMaterial; |
| 303 |
SoBaseColor *shclr = new SoBaseColor; |
| 304 |
SoTranslation *shxl = new SoTranslation; |
| 305 |
SoScale *shscl = new SoScale; |
| 306 |
|
| 307 |
shmdl->model = SoLightModel::BASE_COLOR; |
| 308 |
shclr->rgb.setValue(SbColor(0.21, 0.15, 0.09)); |
| 309 |
shmtl->transparency = 0.3; |
| 310 |
shxl->translation.setValue(SbVec3f(0.0, -4.9, 0.0)); |
| 311 |
shscl->scaleFactor.setValue(SbVec3f(1.0, 0.0, 1.0)); |
| 312 |
shadow->addChild(shmtl); |
| 313 |
shadow->addChild(shmdl); |
| 314 |
shadow->addChild(shclr); |
| 315 |
shadow->addChild(shxl); |
| 316 |
shadow->addChild(shscl); |
| 317 |
shadow->addChild(surf); |
| 318 |
root->addChild(shadow); |
| 319 |
|
| 320 |
// Initialize an Examiner Viewer |
| 321 |
SoXtExaminerViewer *viewer = |
| 322 |
new SoXtExaminerViewer(appWindow); |
| 323 |
viewer->setSceneGraph(root); |
| 324 |
viewer->setTitle("Trimmed Nurbs Surface"); |
| 325 |
SoCamera *cam = viewer->getCamera(); |
| 326 |
cam->position.setValue(SbVec3f(-8.5, 13.0, 23.0)); |
| 327 |
cam->pointAt(SbVec3f(-2.0, -2.0, -4.0)); |
| 328 |
viewer->show(); |
| 329 |
|
| 330 |
SoXt::show(appWindow); |
| 331 |
SoXt::mainLoop(); |
| 332 |
} |
| 333 |
|