SoSeparator *root = new SoSeparator;
root->ref();
// Add a camera and light
SoPerspectiveCamera *myCamera = new SoPerspectiveCamera;
myCamera->position.setValue(-0.5f, -3.0f, 19.0f);
myCamera->nearDistance = 10.0f;
myCamera->farDistance = 26.0f;
root->addChild(myCamera);
root->addChild(new SoDirectionalLight);
// Rotate scene slightly to get better view
SoRotationXYZ *globalRotXYZ = new SoRotationXYZ;
globalRotXYZ->axis = SoRotationXYZ::X;
globalRotXYZ->angle = (float)(M_PI/7.0f);
root->addChild(globalRotXYZ);
// Read the background path from a file and add to the group
SoInput myInput;
if (!myInput.openFile("../data/flowerPath.iv"))
exit (1);
SoSeparator *flowerPath = SoDB::readAll(&myInput);
if (flowerPath == NULL)
exit(1);
root->addChild(flowerPath);
/////////////////////////////////////////////////////////////
// CODE FOR The Inventor Mentor STARTS HERE
// Flower group
SoSeparator *flowerGroup = new SoSeparator;
root->addChild(flowerGroup);
// Read the flower object from a file and add to the group
if (!myInput.openFile("../data/flower.iv"))
exit (1);
SoSeparator *flower= SoDB::readAll(&myInput);
if (flower == NULL) exit (1);
// Set up the flower transformations
SoTranslation *danceTranslation = new SoTranslation;
SoTransform *initialTransform = new SoTransform;
flowerGroup->addChild(danceTranslation);
initialTransform->scaleFactor.setValue(10.0f, 10.0f, 10.0f);
initialTransform->translation.setValue(0.0f, 0.0f, 5.0f);
flowerGroup->addChild(initialTransform);
flowerGroup->addChild(flower);
// Set up an engine to calculate the motion path:
// r = 5*cos(5*theta); x = r*cos(theta); z = r*sin(theta)
// Theta is incremented using a time counter engine,
// and converted to radians using an expression in
// the calculator engine.
SoCalculator *calcXZ = new SoCalculator;
SoTimeCounter *thetaCounter = new SoTimeCounter;
thetaCounter->max = 360;
thetaCounter->step = 4;
thetaCounter->frequency = 0.075f;
calcXZ->a.connectFrom(&thetaCounter->output);
calcXZ->expression.set1Value(0, "ta=a*M_PI/180"); // theta
calcXZ->expression.set1Value(1, "tb=5*cos(5*ta)"); // r
calcXZ->expression.set1Value(2, "td=tb*cos(ta)"); // x
calcXZ->expression.set1Value(3, "te=tb*sin(ta)"); // z
calcXZ->expression.set1Value(4, "oA=vec3f(td,0,te)");
danceTranslation->translation.connectFrom(&calcXZ->oA);
// CODE FOR The Inventor Mentor ENDS HERE
/////////////////////////////////////////////////////////////
SoWinRenderArea *myRenderArea = new SoWinRenderArea(myWindow);
myRenderArea->setSceneGraph(root);
myRenderArea->setTitle("Flower Dance");
myRenderArea->show();
SoWin::show(myWindow);
SoWin::mainLoop();
return 0;
}