Vec2f VRIntersect_computeTexel(VRIntersection& ins, NodeRecPtr node) { if (!ins.hit) return Vec2f(0,0); if (node == 0) return Vec2f(0,0); GeometryRefPtr geo = dynamic_cast<Geometry*>( node->getCore() ); if (geo == 0) return Vec2f(0,0); auto texcoords = geo->getTexCoords(); if (texcoords == 0) return Vec2f(0,0); TriangleIterator iter = geo->beginTriangles(); iter.seek( ins.triangle ); Matrix m = node->getToWorld(); m.invert(); Pnt3f local_pnt; m.mult(ins.point, local_pnt); Pnt3f p0 = iter.getPosition(0); Pnt3f p1 = iter.getPosition(1); Pnt3f p2 = iter.getPosition(2); Vec3f cr = (p1 - p0).cross(p2 - p0); Vec3f n = cr; n.normalize(); float areaABC = n.dot(cr); float areaPBC = n.dot((p1 - local_pnt).cross(p2 - local_pnt)); float areaPCA = n.dot((p2 - local_pnt).cross(p0 - local_pnt)); float a = areaPBC / areaABC; float b = areaPCA / areaABC; float c = 1.0f - a - b; return iter.getTexCoords(0) * a + iter.getTexCoords(1) * b + iter.getTexCoords(2) * c; }
NodeTransitPtr OctreeVisualization::createNodeGeo(OctreePtr, const Octree::OTNodePtr node, Material* GeoMaterial, const Node* BaseGeo) { //Make the Geoemtry NodeRecPtr box = deepCloneTree(BaseGeo); dynamic_cast<Geometry*>(box->getCore())->setMaterial(GeoMaterial); Matrix m; TransformRecPtr box_trans; NodeRecPtr trans_node = makeCoredNode<Transform>(&box_trans); Pnt3f Center; node->getVolume().getCenter(Center); m.setTranslate( Center.x(), Center.y(), Center.z()); const Real32 Offset(0.0f); Vec3f Size; node->getVolume().getSize(Size); m.setScale(Size.x()-(Size.x()*Offset), Size.y()-(Size.y()*Offset), Size.z()-(Size.z()*Offset)); box_trans->setMatrix(m); trans_node->addChild(box); return NodeTransitPtr(trans_node); }
////////////////////////////////////////////////////////////////////////// //! build a box ////////////////////////////////////////////////////////////////////////// void buildBox(Node* const spaceGroupNode, PhysicsWorld* const physicsWorld, PhysicsHashSpace* const physicsSpace) { Vec3f Lengths((Real32)(rand()%2)+1.0, (Real32)(rand()%2)+1.0, (Real32)(rand()%2)+1.0); Matrix m; //create OpenSG mesh GeometryRecPtr box; NodeRecPtr boxNode = makeBox(Lengths.x(), Lengths.y(), Lengths.z(), 1, 1, 1); box = dynamic_cast<Geometry*>(boxNode->getCore()); SimpleMaterialRecPtr box_mat = SimpleMaterial::create(); box_mat->setAmbient(Color3f(0.0,0.0,0.0)); box_mat->setDiffuse(Color3f(0.0,1.0 ,0.0)); box->setMaterial(box_mat); TransformRecPtr boxTrans; NodeRecPtr boxTransNode = makeCoredNode<Transform>(&boxTrans); m.setIdentity(); Real32 randX = (Real32)(rand()%10)-5.0; Real32 randY = (Real32)(rand()%10)-5.0; m.setTranslate(randX, randY, 10.0); boxTrans->setMatrix(m); //create ODE data PhysicsBodyRecPtr boxBody = PhysicsBody::create(physicsWorld); boxBody->setPosition(Vec3f(randX, randY, 10.0)); boxBody->setBoxMass(1.0, Lengths.x(), Lengths.y(), Lengths.z()); //std::cout << "mass: " << boxBody->getMass() << std::endl //<< "massCenterOfGravity: " << boxBody->getMassCenterOfGravity().x() << ", " << boxBody->getMassCenterOfGravity().y() << ", " << boxBody->getMassCenterOfGravity().z() << std::endl //<< "massInertiaTensor: " << std::endl //<< boxBody->getMassInertiaTensor()[0][0] << " "<< boxBody->getMassInertiaTensor()[0][1] << " "<< boxBody->getMassInertiaTensor()[0][2] << " " << boxBody->getMassInertiaTensor()[0][3] << std::endl //<< boxBody->getMassInertiaTensor()[1][0] << " "<< boxBody->getMassInertiaTensor()[1][1] << " "<< boxBody->getMassInertiaTensor()[1][2] << " " << boxBody->getMassInertiaTensor()[1][3] << std::endl //<< boxBody->getMassInertiaTensor()[2][0] << " "<< boxBody->getMassInertiaTensor()[2][1] << " "<< boxBody->getMassInertiaTensor()[2][2] << " " << boxBody->getMassInertiaTensor()[2][3] << std::endl //<< boxBody->getMassInertiaTensor()[3][0] << " "<< boxBody->getMassInertiaTensor()[3][1] << " "<< boxBody->getMassInertiaTensor()[3][2] << " " << boxBody->getMassInertiaTensor()[3][3] << std::endl //<< std::endl; PhysicsBoxGeomRecPtr boxGeom = PhysicsBoxGeom::create(); boxGeom->setBody(boxBody); boxGeom->setSpace(physicsSpace); boxGeom->setLengths(Lengths); //add attachments boxNode->addAttachment(boxGeom); boxTransNode->addAttachment(boxBody); boxTransNode->addChild(boxNode); //add to SceneGraph spaceGroupNode->addChild(boxTransNode); commitChanges(); }
////////////////////////////////////////////////////////////////////////// //! trimesh defined by filenode will be loaded ////////////////////////////////////////////////////////////////////////// void buildTriMesh(Node* const TriGeometryBase, Node* const spaceGroupNode, PhysicsWorld* const physicsWorld, PhysicsHashSpace* const physicsSpace) { NodeRecPtr tri = cloneTree(TriGeometryBase); if(tri!=NULL) { GeometryRecPtr triGeo = dynamic_cast<Geometry*>(tri->getCore()); Matrix m; SimpleMaterialRecPtr tri_mat = SimpleMaterial::create(); tri_mat->setAmbient(Color3f(0.1,0.1,0.2)); tri_mat->setDiffuse(Color3f(1.0,0.1,0.7)); triGeo->setMaterial(tri_mat); TransformRecPtr triTrans; NodeRecPtr triTransNode = makeCoredNode<Transform>(&triTrans); m.setIdentity(); Real32 randX = (Real32)(rand()%10)-5.0; Real32 randY = (Real32)(rand()%10)-5.0; m.setTranslate(randX, randY, 18.0); triTrans->setMatrix(m); //create ODE data Vec3f GeometryBounds(calcMinGeometryBounds(triGeo)); PhysicsBodyRecPtr triBody = PhysicsBody::create(physicsWorld); triBody->setPosition(Vec3f(randX, randY, 18.0)); triBody->setLinearDamping(0.0001); triBody->setAngularDamping(0.0001); triBody->setBoxMass(1.0,GeometryBounds.x(), GeometryBounds.y(), GeometryBounds.z()); PhysicsGeomRecPtr triGeom; if(true) { triGeom = PhysicsTriMeshGeom::create(); triGeom->setBody(triBody); //add geom to space for collision triGeom->setSpace(physicsSpace); //set the geometryNode to fill the ode-triMesh NodeRecPtr TorusGeometryNode(makeTorus(0.55, 1.05, 6, 6)); dynamic_pointer_cast<PhysicsTriMeshGeom>(triGeom)->setGeometryNode(TorusGeometryNode); } //add attachments tri->addAttachment(triGeom); triTransNode->addAttachment(triBody); //add to SceneGraph triTransNode->addChild(tri); spaceGroupNode->addChild(triTransNode); } else { SLOG << "Could not read MeshData!" << endLog; } commitChanges(); }
////////////////////////////////////////////////////////////////////////// //! build a sphere ////////////////////////////////////////////////////////////////////////// void buildSphere(Node* const spaceGroupNode, PhysicsWorld* const physicsWorld, PhysicsHashSpace* const physicsSpace) { Real32 Radius((Real32)(rand()%2)*0.5+0.5); Matrix m; //create OpenSG mesh GeometryRecPtr sphere; NodeRecPtr sphereNode = makeSphere(2, Radius); sphere = dynamic_cast<Geometry*>(sphereNode->getCore()); SimpleMaterialRecPtr sphere_mat = SimpleMaterial::create(); sphere_mat->setAmbient(Color3f(0.0,0.0,0.0)); sphere_mat->setDiffuse(Color3f(0.0,0.0,1.0)); sphere->setMaterial(sphere_mat); TransformRecPtr sphereTrans; NodeRecPtr sphereTransNode = makeCoredNode<Transform>(&sphereTrans); m.setIdentity(); Real32 randX = (Real32)(rand()%10)-5.0; Real32 randY = (Real32)(rand()%10)-5.0; m.setTranslate(randX, randY, 10.0); sphereTrans->setMatrix(m); //create ODE data PhysicsBodyRecPtr sphereBody = PhysicsBody::create(physicsWorld); sphereBody->setPosition(Vec3f(randX, randY, 10.0)); sphereBody->setAngularDamping(0.0001); sphereBody->setSphereMass(1.0,Radius); PhysicsSphereGeomRecPtr sphereGeom = PhysicsSphereGeom::create(); sphereGeom->setBody(sphereBody); sphereGeom->setSpace(physicsSpace); sphereGeom->setRadius(Radius); //add attachments sphereNode->addAttachment(sphereGeom); sphereTransNode->addAttachment(sphereBody); sphereTransNode->addChild(sphereNode); //add to SceneGraph spaceGroupNode->addChild(sphereTransNode); commitChanges(); }
NodeTransitPtr createBox(void) { // Make Object Nodes NodeRecPtr ExampleBoxGeo = makeBox(100, 100, 100, 1, 1, 1); MaterialRecPtr GreenMaterial = createMaterial(Color4f(0.0f,1.0f,0.0f,1.0f)); dynamic_cast<Geometry*>(ExampleBoxGeo->getCore())->setMaterial(GreenMaterial); Matrix mat; mat.setTranslate(250.0,250.0,0.0); TransformRecPtr ExampleBoxTranCore = Transform::create(); ExampleBoxTranCore->setMatrix(mat); NodeRecPtr ExampleBox = Node::create(); ExampleBox->setCore(ExampleBoxTranCore); ExampleBox->addChild(ExampleBoxGeo); ExampleBox->setTravMask(0); return NodeTransitPtr(ExampleBox); }
NodeTransitPtr createSphere(void) { // Make Object Nodes NodeRecPtr ExampleSphereGeo = makeSphere(4, 100); MaterialRecPtr RedMaterial = createMaterial(Color4f(1.0f,0.0f,0.0f,1.0f)); dynamic_cast<Geometry*>(ExampleSphereGeo->getCore())->setMaterial(RedMaterial); Matrix mat; mat.setTranslate(250.0,0.0,0.0); TransformRecPtr SphereTranCore = Transform::create(); SphereTranCore->setMatrix(mat); NodeRecPtr ExampleSphere = Node::create(); ExampleSphere->setCore(SphereTranCore); ExampleSphere->addChild(ExampleSphereGeo); ExampleSphere->setTravMask(0); return NodeTransitPtr(ExampleSphere); }
NodeTransitPtr OctreeVisualization::createNodeDistanceLOD(OctreePtr, const Octree::OTNodePtr node, Material* GeoMaterial, const Node* BaseGeo, Real32 Range ) { NodeRecPtr box = deepCloneTree(BaseGeo); dynamic_cast<Geometry*>(box->getCore())->setMaterial(GeoMaterial); //DistanceLOD node DistanceLODRecPtr LOD = DistanceLOD::create(); LOD->editMFRange()->push_back(10.0); NodeRecPtr LODNode = makeNodeFor(LOD); LODNode->addChild(box); LODNode->addChild(makeCoredNode<Group>()); Matrix m; TransformRecPtr box_trans; NodeRecPtr trans_node = makeCoredNode<Transform>(&box_trans); Pnt3f Center; node->getVolume().getCenter(Center); m.setTranslate(Center.x(), Center.y(), Center.z()); const Real32 Offset(0.0f); Vec3f Size; node->getVolume().getSize(Size); m.setScale(Size.x()-(Size.x()*Offset), Size.y()-(Size.y()*Offset), Size.z()-(Size.z()*Offset)); box_trans->setMatrix(m); trans_node->addChild(LODNode); return NodeTransitPtr(trans_node); }
NodeTransitPtr createCone(void) { // Make Object Nodes NodeRecPtr ExampleConeGeo = makeCone(150, 50, 16, true, true); MaterialRecPtr BlueMaterial = createMaterial(Color4f(0.0f,0.0f,1.0f,1.0f)); dynamic_cast<Geometry*>(ExampleConeGeo->getCore())->setMaterial(BlueMaterial); // Preform transformations on them Matrix mat; // On Cone mat.setTranslate(0.0,0.0,-250.0); TransformRecPtr ConeTranCore = Transform::create(); ConeTranCore->setMatrix(mat); NodeRecPtr ExampleCone = Node::create(); ExampleCone->setCore(ConeTranCore); ExampleCone->addChild(ExampleConeGeo); ExampleCone->setTravMask(0); return NodeTransitPtr(ExampleCone); }
void VRGeometry::makeUnique() { if (mesh_node == 0) return; NodeRecPtr clone = deepCloneTree( mesh_node ); setMesh( dynamic_cast<Geometry*>( clone->getCore() ), source ); }
void selectedNodeChanged(void) { _mgr->setHighlight(_SelectedNode); //Update Details Panel if(_SelectedNode == NULL) { _NodeNameValueLabel->setText(""); _NodeCoreTypeValueLabel->setText(""); _NodeMinValueLabel->setText(""); _NodeMaxValueLabel->setText(""); _NodeCenterValueLabel->setText(""); _NodeTriCountValueLabel->setText(""); _NodeTravMaskValueLabel->setText(""); } else { const Char8 *NodeName = getName(_SelectedNode); if(NodeName == NULL) { _NodeNameValueLabel->setText("Unnamed Node"); } else { _NodeNameValueLabel->setText(NodeName); } _NodeCoreTypeValueLabel->setText(_SelectedNode->getCore()->getType().getCName()); BoxVolume DyVol; _SelectedNode->getWorldVolume(DyVol); Pnt3f Min,Max,Center; DyVol.getBounds(Min,Max); DyVol.getCenter(Center); std::string TempText(""); TempText = boost::lexical_cast<std::string>(Min.x()) + ", " +boost::lexical_cast<std::string>(Min.x()) + ", " + boost::lexical_cast<std::string>(Min.x()); _NodeMinValueLabel->setText(TempText); TempText = boost::lexical_cast<std::string>(Max.x()) + ", " +boost::lexical_cast<std::string>(Max.x()) + ", " + boost::lexical_cast<std::string>(Max.x()); _NodeMaxValueLabel->setText(TempText); TempText = boost::lexical_cast<std::string>(Center.x()) + ", " +boost::lexical_cast<std::string>(Center.x()) + ", " + boost::lexical_cast<std::string>(Center.x()); _NodeCenterValueLabel->setText(TempText); _NodeTravMaskValueLabel->setText(boost::lexical_cast<std::string>(_SelectedNode->getTravMask())); //Tri Cound TriCountGraphOpRefPtr TheTriGraphOp = TriCountGraphOp::create(); TheTriGraphOp->traverse(_SelectedNode); _NodeTriCountValueLabel->setText(boost::lexical_cast<std::string>(TheTriGraphOp->getNumTri())); } }
// Initialize GLUT & OpenSG and set up the rootNode int main(int argc, char **argv) { // OSG init osgInit(argc,argv); { // Set up Window WindowEventProducerRecPtr TutorialWindow = createNativeWindow(); TutorialWindow->initWindow(); SimpleSceneManager sceneManager; TutorialWindow->setDisplayCallback(boost::bind(display, &sceneManager)); TutorialWindow->setReshapeCallback(boost::bind(reshape, _1, &sceneManager)); //Attach to events TutorialWindow->connectMousePressed(boost::bind(mousePressed, _1, &sceneManager)); TutorialWindow->connectMouseReleased(boost::bind(mouseReleased, _1, &sceneManager)); TutorialWindow->connectMouseMoved(boost::bind(mouseMoved, _1, &sceneManager)); TutorialWindow->connectMouseDragged(boost::bind(mouseDragged, _1, &sceneManager)); TutorialWindow->connectMouseWheelMoved(boost::bind(mouseWheelMoved, _1, &sceneManager)); // Tell the Manager what to manage sceneManager.setWindow(TutorialWindow); //Make Base Geometry Node NodeRecPtr TriGeometryBase = makeTorus(0.5, 1.0, 24, 24); //Make Main Scene Node NodeRecPtr scene = makeCoredNode<Group>(); setName(scene, "scene"); NodeRecPtr rootNode = Node::create(); setName(rootNode, "rootNode"); ComponentTransformRecPtr Trans; Trans = ComponentTransform::create(); rootNode->setCore(Trans); // add the torus as a child rootNode->addChild(scene); //Make The Physics Characteristics Node PhysicsCharacteristicsDrawableRecPtr PhysDrawable = PhysicsCharacteristicsDrawable::create(); PhysDrawable->setRoot(rootNode); NodeRecPtr PhysDrawableNode = Node::create(); PhysDrawableNode->setCore(PhysDrawable); PhysDrawableNode->setTravMask(TypeTraits<UInt32>::getMin()); rootNode->addChild(PhysDrawableNode); //Setup Physics Scene PhysicsWorldRecPtr physicsWorld = PhysicsWorld::create(); physicsWorld->setWorldContactSurfaceLayer(0.005); physicsWorld->setAutoDisableFlag(1); physicsWorld->setAutoDisableTime(0.75); physicsWorld->setWorldContactMaxCorrectingVel(100.0); physicsWorld->setGravity(Vec3f(0.0, 0.0, -9.81)); PhysicsHashSpaceRecPtr physicsSpace = PhysicsHashSpace::create(); PhysicsHandlerRecPtr physHandler = PhysicsHandler::create(); physHandler->setWorld(physicsWorld); physHandler->pushToSpaces(physicsSpace); physHandler->setUpdateNode(rootNode); physHandler->attachUpdateProducer(TutorialWindow); /************************************************************************/ /* create spaces, geoms and bodys */ /************************************************************************/ //create a group for our space GroupRecPtr spaceGroup; NodeRecPtr spaceGroupNode = makeCoredNode<Group>(&spaceGroup); //create the ground plane GeometryRecPtr plane; NodeRecPtr planeNode = makeBox(30.0, 30.0, 1.0, 1, 1, 1); plane = dynamic_cast<Geometry*>(planeNode->getCore()); //and its Material SimpleMaterialRecPtr plane_mat = SimpleMaterial::create(); plane_mat->setAmbient(Color3f(0.7,0.7,0.7)); plane_mat->setDiffuse(Color3f(0.9,0.6,1.0)); plane->setMaterial(plane_mat); //create Physical Attachments PhysicsBoxGeomRecPtr planeGeom = PhysicsBoxGeom::create(); planeGeom->setLengths(Vec3f(30.0, 30.0, 1.0)); //add geoms to space for collision planeGeom->setSpace(physicsSpace); //add Attachments to nodes... spaceGroupNode->addAttachment(physicsSpace); spaceGroupNode->addAttachment(physHandler); spaceGroupNode->addAttachment(physicsWorld); spaceGroupNode->addChild(planeNode); planeNode->addAttachment(planeGeom); scene->addChild(spaceGroupNode); //Create Statistics Foreground SimpleStatisticsForegroundRecPtr PhysicsStatForeground = SimpleStatisticsForeground::create(); PhysicsStatForeground->setSize(25); PhysicsStatForeground->setColor(Color4f(0,1,0,0.7)); PhysicsStatForeground->addElement(WindowEventProducer::statWindowLoopTime, "Draw FPS: %r.3f"); PhysicsStatForeground->getCollector()->getElem(WindowEventProducer::statWindowLoopTime, true); PhysicsStatForeground->addElement(RenderAction::statNGeometries, "%d Nodes drawn"); PhysicsStatForeground->getCollector()->getElem(RenderAction::statNGeometries, true); PhysicsStatForeground->addElement(PhysicsHandler::statPhysicsTime, "Physics time: %.3f s"); PhysicsStatForeground->getCollector()->getElem(PhysicsHandler::statPhysicsTime, true); PhysicsStatForeground->addElement(PhysicsHandler::statCollisionTime, "Collision time: %.3f s"); PhysicsStatForeground->getCollector()->getElem(PhysicsHandler::statCollisionTime, true); PhysicsStatForeground->addElement(PhysicsHandler::statSimulationTime, "Simulation time: %.3f s"); PhysicsStatForeground->getCollector()->getElem(PhysicsHandler::statSimulationTime, true); PhysicsStatForeground->addElement(PhysicsHandler::statNCollisions, "%d collisions"); PhysicsStatForeground->getCollector()->getElem(PhysicsHandler::statNCollisions, true); PhysicsStatForeground->addElement(PhysicsHandler::statNCollisionTests, "%d collision tests"); PhysicsStatForeground->getCollector()->getElem(PhysicsHandler::statNCollisionTests, true); PhysicsStatForeground->addElement(PhysicsHandler::statNPhysicsSteps, "%d simulation steps per frame"); PhysicsStatForeground->getCollector()->getElem(PhysicsHandler::statNPhysicsSteps, true); TutorialWindow->connectUpdate(boost::bind(handleStatisticsReset, _1), boost::signals2::at_front); StatCollector::setGlobalCollector(PhysicsStatForeground->getCollector()); SimpleStatisticsForegroundRecPtr RenderStatForeground = SimpleStatisticsForeground::create(); RenderStatForeground->setSize(25); RenderStatForeground->setColor(Color4f(0,1,0,0.7)); // tell the manager what to manage sceneManager.setRoot (rootNode); sceneManager.getWindow()->getPort(0)->addForeground(PhysicsStatForeground); sceneManager.getWindow()->getPort(0)->addForeground(RenderStatForeground); TutorialWindow->connectKeyPressed(boost::bind(keyPressed, _1, TriGeometryBase.get(), spaceGroupNode.get(), PhysDrawableNode.get(), physicsWorld.get(), physicsSpace.get())); // show the whole rootNode sceneManager.getNavigator()->set(Pnt3f(20.0,20.0,10.0), Pnt3f(0.0,0.0,0.0), Vec3f(0.0,0.0,1.0)); sceneManager.getNavigator()->setMotionFactor(1.0f); sceneManager.getCamera()->setFar(10000.0f); sceneManager.getCamera()->setNear(0.1f); Vec2f WinSize(TutorialWindow->getDesktopSize() * 0.85f); Pnt2f WinPos((TutorialWindow->getDesktopSize() - WinSize) *0.5); TutorialWindow->openWindow(WinPos, WinSize, "01SimplePhysics"); //Enter main Loop TutorialWindow->mainLoop(); } osgExit(); return 0; }