static NodePtr makePerturbedUniform (UInt16 numSubdiv, Real32 radius, Real32 rate = 0.1f) { static Real32 factor = 1.1f; NodePtr sphereNode = makeSphere(numSubdiv, radius); GeometryPtr sphere = GeometryPtr::dcast(sphereNode->getCore()); GeoPositionsPtr points = sphere->getPositions(); beginEditCP(points); for (UInt32 i=0; i<points->size(); ++i) { Real32 random = (rand()/Real32(RAND_MAX)); if (random <= rate) { points->setValue(factor*points->getValue(i), i); } } endEditCP(points); NodePtr node = Node::create(); beginEditCP(node); node->setCore(Transform::create()); node->addChild(sphereNode); endEditCP(node); return node; }
// redraw the window void display( void ) { // create the matrix Matrix m; Real32 t = glutGet(GLUT_ELAPSED_TIME ); m.setTransform(Quaternion( Vec3f(0,1,0), t / 1000.f)); // set the transform's matrix beginEditCP(trans, Transform::MatrixFieldMask); { trans->setMatrix(m); } endEditCP (trans, Transform::MatrixFieldMask); /* Manipulate the geometry. The OpenSG geometry structure is pretty flexible. The disadvantage of all this flexibility is that it can be hard to write generic tools, as pretty much all the used types can be one of a number of variants. To simplify that, every kind of GeoProperty has a generic type, e.g. the generic type for positions is Pnt3f, for colors it's Color3f. No matter the internal data representation looks like, all GeoProperties have the generic interface. As does the abstract parent class of every kind of property. Thus it's possible to access the data of an arbitrary geometry using the generic interface. */ // note that this is the abstract parent class, it doesn't have a specific // type GeoPositionsPtr pos = geo->getPositions(); beginEditCP(pos); for(UInt32 i = 0; i < pos->getSize(); i++) { Pnt3f p; pos->getValue(p, i); p[0] += osgsin(t / 300) * p[1] / 100; p[1] += osgsin(t / 300) * p[2] / 100; p[2] += osgsin(t / 300) * p[0] / 100; pos->setValue(p, i); } endEditCP (pos); // right now the geometry doesn't notice changes to the properties, it has // to be notified explicitly beginEditCP(geo, Geometry::PositionsFieldMask); endEditCP (geo, Geometry::PositionsFieldMask); mgr->redraw(); }
static NodePtr makePerturbedAll (UInt16 numSubdiv, Real32 radius, Real32 stdDeviation = 0.1f) { NodePtr sphereNode = makeSphere(numSubdiv, radius); GeometryPtr sphere = GeometryPtr::dcast(sphereNode->getCore()); GeoPositionsPtr points = sphere->getPositions(); beginEditCP(points); for (UInt32 i=0; i<points->size(); ++i) { Real32 factor = 1.0f + stdDeviation * (rand()/Real32(RAND_MAX) - 0.5f); points->setValue(factor*points->getValue(i), i); } endEditCP(points); NodePtr node = Node::create(); beginEditCP(node); node->setCore(Transform::create()); node->addChild(sphereNode); endEditCP(node); return node; }