| 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 13, example 7. |
| 40 |
* |
| 41 |
* A calculator engine computes a closed, planar curve. |
| 42 |
* The output from the engine is connected to the translation |
| 43 |
* applied to a flower object, which consequently moves |
| 44 |
* along the path of the curve. |
| 45 |
*------------------------------------------------------------*/ |
| 46 |
|
| 47 |
#include <cstdlib> |
| 48 |
#include <Inventor/SoDB.h> |
| 49 |
#include <Inventor/Xt/SoXt.h> |
| 50 |
#include <Inventor/Xt/SoXtRenderArea.h> |
| 51 |
#include <Inventor/engines/SoCalculator.h> |
| 52 |
#include <Inventor/engines/SoElapsedTime.h> |
| 53 |
#include <Inventor/engines/SoTimeCounter.h> |
| 54 |
#include <Inventor/nodes/SoDirectionalLight.h> |
| 55 |
#include <Inventor/nodes/SoMaterial.h> |
| 56 |
#include <Inventor/nodes/SoPerspectiveCamera.h> |
| 57 |
#include <Inventor/nodes/SoRotationXYZ.h> |
| 58 |
#include <Inventor/nodes/SoSeparator.h> |
| 59 |
#include <Inventor/nodes/SoTransform.h> |
| 60 |
#include <Inventor/nodes/SoTranslation.h> |
| 61 |
|
| 62 |
int |
| 63 |
main(int , char **argv) |
| 64 |
{ |
| 65 |
// Initialize Inventor and Xt |
| 66 |
Widget myWindow = SoXt::init(argv[0]); |
| 67 |
if (myWindow == NULL) exit(1); |
| 68 |
|
| 69 |
SoSeparator *root = new SoSeparator; |
| 70 |
root->ref(); |
| 71 |
|
| 72 |
// Add a camera and light |
| 73 |
SoPerspectiveCamera *myCamera = new SoPerspectiveCamera; |
| 74 |
myCamera->position.setValue(-0.5, -3.0, 19.0); |
| 75 |
myCamera->nearDistance = 10.0; |
| 76 |
myCamera->farDistance = 26.0; |
| 77 |
root->addChild(myCamera); |
| 78 |
root->addChild(new SoDirectionalLight); |
| 79 |
|
| 80 |
// Rotate scene slightly to get better view |
| 81 |
SoRotationXYZ *globalRotXYZ = new SoRotationXYZ; |
| 82 |
globalRotXYZ->axis = SoRotationXYZ::X; |
| 83 |
globalRotXYZ->angle = M_PI/7; |
| 84 |
root->addChild(globalRotXYZ); |
| 85 |
|
| 86 |
// Read the background path from a file and add to the group |
| 87 |
SoInput myInput; |
| 88 |
if (!myInput.openFile("/usr/share/src/Inventor/examples/data/flowerPath.iv")) |
| 89 |
exit (1); |
| 90 |
SoSeparator *flowerPath = SoDB::readAll(&myInput); |
| 91 |
if (flowerPath == NULL) exit (1); |
| 92 |
root->addChild(flowerPath); |
| 93 |
|
| 94 |
///////////////////////////////////////////////////////////// |
| 95 |
// CODE FOR The Inventor Mentor STARTS HERE |
| 96 |
|
| 97 |
// Flower group |
| 98 |
SoSeparator *flowerGroup = new SoSeparator; |
| 99 |
root->addChild(flowerGroup); |
| 100 |
|
| 101 |
// Read the flower object from a file and add to the group |
| 102 |
if (!myInput.openFile("/usr/share/src/Inventor/examples/data/flower.iv")) |
| 103 |
exit (1); |
| 104 |
SoSeparator *flower= SoDB::readAll(&myInput); |
| 105 |
if (flower == NULL) exit (1); |
| 106 |
|
| 107 |
// Set up the flower transformations |
| 108 |
SoTranslation *danceTranslation = new SoTranslation; |
| 109 |
SoTransform *initialTransform = new SoTransform; |
| 110 |
flowerGroup->addChild(danceTranslation); |
| 111 |
initialTransform->scaleFactor.setValue(10., 10., 10.); |
| 112 |
initialTransform->translation.setValue(0., 0., 5.); |
| 113 |
flowerGroup->addChild(initialTransform); |
| 114 |
flowerGroup->addChild(flower); |
| 115 |
|
| 116 |
// Set up an engine to calculate the motion path: |
| 117 |
// r = 5*cos(5*theta); x = r*cos(theta); z = r*sin(theta) |
| 118 |
// Theta is incremented using a time counter engine, |
| 119 |
// and converted to radians using an expression in |
| 120 |
// the calculator engine. |
| 121 |
SoCalculator *calcXZ = new SoCalculator; |
| 122 |
SoTimeCounter *thetaCounter = new SoTimeCounter; |
| 123 |
|
| 124 |
thetaCounter->max = 360; |
| 125 |
thetaCounter->step = 4; |
| 126 |
thetaCounter->frequency = 0.075; |
| 127 |
|
| 128 |
calcXZ->a.connectFrom(&thetaCounter->output); |
| 129 |
calcXZ->expression.set1Value(0, "ta=a*M_PI/180"); // theta |
| 130 |
calcXZ->expression.set1Value(1, "tb=5*cos(5*ta)"); // r |
| 131 |
calcXZ->expression.set1Value(2, "td=tb*cos(ta)"); // x |
| 132 |
calcXZ->expression.set1Value(3, "te=tb*sin(ta)"); // z |
| 133 |
calcXZ->expression.set1Value(4, "oA=vec3f(td,0,te)"); |
| 134 |
danceTranslation->translation.connectFrom(&calcXZ->oA); |
| 135 |
|
| 136 |
// CODE FOR The Inventor Mentor ENDS HERE |
| 137 |
///////////////////////////////////////////////////////////// |
| 138 |
|
| 139 |
SoXtRenderArea *myRenderArea = new SoXtRenderArea(myWindow); |
| 140 |
myRenderArea->setSceneGraph(root); |
| 141 |
myRenderArea->setTitle("Flower Dance"); |
| 142 |
myRenderArea->show(); |
| 143 |
|
| 144 |
SoXt::show(myWindow); |
| 145 |
SoXt::mainLoop(); |
| 146 |
} |
| 147 |
|