void OcclusionQueryVisitor::addOQN( osg::Node& node ) { VertexCounter vc( _occluderThreshold ); node.accept( vc ); if (vc.exceeded()) { // Insert OQN(s) above this node. unsigned int np = node.getNumParents(); while (np--) { osg::Group* parent = dynamic_cast<osg::Group*>( node.getParent( np ) ); if (parent != NULL) { osg::ref_ptr<osg::OcclusionQueryNode> oqn = new osg::OcclusionQueryNode(); oqn->addChild( &node ); parent->replaceChild( &node, oqn.get() ); oqn->setName( getNextOQNName() ); // Set all OQNs to use the same query StateSets (instead of multiple copies // of the same StateSet) for efficiency. oqn->setQueryStateSet( _state.get() ); oqn->setDebugStateSet( _debugState.get() ); } } } }
void ShaderGenerator::duplicateSharedNode(osg::Node& node) { if ( node.getNumParents() > 1 ) { for(int i=1; i<(int)node.getNumParents(); ++i) { osg::Group* parent = node.getParent(i); osg::Node* replicant = osg::clone( &node, osg::CopyOp::DEEP_COPY_NODES | osg::CopyOp::DEEP_COPY_DRAWABLES | osg::CopyOp::DEEP_COPY_ARRAYS); parent->replaceChild(&node, replicant); } } }
void FindGroupByRecIndex::apply(osg::Node &searchNode) { if (searchNode.getUserDataContainer() && searchNode.getUserDataContainer()->getNumUserObjects()) { NodeUserData* holder = dynamic_cast<NodeUserData*>(searchNode.getUserDataContainer()->getUserObject(0)); if (holder && holder->mIndex == mRecIndex) { osg::Group* group = searchNode.asGroup(); if (!group) group = searchNode.getParent(0); mFound = group; mFoundPath = getNodePath(); return; } } traverse(searchNode); }
void PlaneMoveManipulator::mouseButtonPress(const UInt16 button, const Int16 x, const Int16 y ) { Transform *t = dynamic_cast<Transform *>(getTarget()->getCore()); if (t == NULL) { SWARNING << "PlaneMoveManipulator::mouseButtonPress() target is not a Transform!" << endLog; return; } SLOG << "PlaneMoveManipulator::mouseButtonPress() button=" << button << " x=" << x << " y=" << y << std::endl << endLog; OSG::Line viewray; getViewport()->getCamera()->calcViewRay(viewray, x, y, *getViewport()); OSG::Node *scene = getTarget(); while (scene->getParent() != 0) { scene = scene->getParent(); } OSG::IntersectActionRefPtr act = OSG::IntersectAction::create(); act->setLine( viewray ); act->apply( scene ); SLOG << "PlaneMoveManipulator::mouseButtonPress() viewray=" << viewray << " scene=" << scene << endLog; if ( act->didHit() ) { SLOG << "PlaneMoveManipulator::mouseButtonPress() hit! at " << act->getHitPoint() << endLog; // Get manipulator plane into world space OSG::Matrix m = getTarget()->getToWorld(); Vec3f translation; // for matrix decomposition Quaternion rotation; Vec3f scaleFactor; Quaternion scaleOrientation; t->getMatrix().getTransform(translation, rotation, scaleFactor, scaleOrientation); Vec3f rot_axis; rotation.multVec(Vec3f(0,1,0), rot_axis); Plane pl(rot_axis, act->getHitPoint()); SLOG << "PlaneMoveManipulator::mouseButtonPress() world plane: " << pl << endLog; setClickPoint(act->getHitPoint()); setBaseTranslation(translation); setBaseRotation(rotation); setActive(true); } act = NULL; }
void jrOSGHighlightVisitor::apply(osg::Node &node) { if (node.getName().find("LowerArm_Rotator") != std::string::npos) { std::cout << "highlighting: " << node.getName() << std::endl; osgFX::Scribe* scribe = new osgFX::Scribe(); osg::Group* parent = node.getParent(0); scribe->setName("highlighted"); scribe->setWireframeColor(osg::Vec4(0,1,0,0.5)); scribe->addChild(&node); parent->replaceChild(&node, scribe); parent = node.getParent(0); osg::MatrixTransform* transform = new osg::MatrixTransform; const double angle = 0.1; const osg::Vec3d axis(-1, 0, 0); transform->setMatrix(osg::Matrix::rotate(angle, axis)); transform->setName("trans"); transform->addChild(&node); parent->replaceChild(&node, transform); } else if (node.getName().find("highlighted") != std::string::npos) { std::cout << "removing highlight on: " << node.getName() << std::endl; osg::Group* parent = node.getParent(0); osgFX::Scribe* scribe = dynamic_cast<osgFX::Scribe*>(&node); parent->replaceChild(&node, scribe->getChild(0)); return; } else if (node.getName().find("trans") != std::string::npos) { std::cout << "altering: " << node.getName() << std::endl; osg::Group* parent = node.getParent(0); osg::MatrixTransform* transform = dynamic_cast<osg::MatrixTransform*>(&node); osg::Quat currentRotate = transform->getMatrix().getRotate(); osg::Matrix currentMatrix = transform->getMatrix(); osg::Vec4 currentVec = currentRotate.asVec4(); std::cout << "current: " << currentVec[3] << " " << std::endl; const double angle = currentVec[3] + 0.1; const osg::Vec3d axis(-1, 0, 0); transform->setMatrix(osg::Matrix::rotate(angle, axis)); std::cout << "current: " << angle << " " << std::endl; //currentRotate.set(currentVec[0] + 0.1,0,0,0); //currentMatrix.setRotate(currentRotate); //transform->setMatrix(currentMatrix); //transform.setMatrix()); parent->replaceChild(&node, transform); return; } traverse(node); }
// Initialize GLUT & OpenSG and set up the scene int main(int argc, char **argv) { OSG::preloadSharedObject("OSGFileIO"); OSG::preloadSharedObject("OSGTBFileIO"); OSG::preloadSharedObject("OSGImageFileIO"); // OSG init OSG::osgInit(argc,argv); // GLUT init int winid = setupGLUT(&argc, argv); // open a new scope, because the pointers below should go out of scope // before entering glutMainLoop. // Otherwise OpenSG will complain about objects being alive after shutdown. { // the connection between GLUT and OpenSG OSG::GLUTWindowRefPtr gwin = OSG::GLUTWindow::create(); gwin->setGlutId(winid); gwin->init(); // create the scene // create a pretty simple graph: a Group with two Transforms as children, // each of which carries a single Geometry. // The scene group OSG::NodeRefPtr scene = OSG::Node::create(); OSG::GroupRefPtr g = OSG::Group::create(); scene->setCore(g); // The cylinder and its transformation OSG::NodeRefPtr cyl = OSG::Node::create(); OSG::GeometryRefPtr cylgeo = OSG::makeCylinderGeo( 1.4f, .3f, 64, true, true, true ); cyl->setCore(cylgeo); cyltrans = OSG::Transform::create(); OSG::NodeRefPtr cyltransnode = OSG::Node::create(); cyltransnode->setCore (cyltrans); cyltransnode->addChild(cyl ); // add it to the scene scene->addChild(cyltransnode); // The torus and its transformation OSG::NodeRefPtr torus = OSG::Node::create(); OSG::GeometryRefPtr torusgeo = OSG::makeTorusGeo( .2f, 1, 32, 64 ); torus->setCore(torusgeo); tortrans = OSG::Transform::create(); OSG::NodeRefPtr tortransnode = OSG::Node::create(); tortransnode->setCore (tortrans); tortransnode->addChild(torus ); // add it to the scene scene->addChild(tortransnode); // create the materials: Here, just using cgfx materials. OSG::CgFXMaterialRefPtr mat1 = OSG::CgFXMaterial::create(); if(argc > 1) { mat1->setEffectFile(argv[1]); } // assign the material to the geometry cylgeo->setMaterial(mat1); // assign the material to the geometry torusgeo->setMaterial(mat1); OSG::commitChanges(); // create the SimpleSceneManager helper mgr = new OSG::SimpleSceneManager; // tell the manager what to manage mgr->setWindow(gwin ); // file io OSG::FCFileType::FCPtrStore Containers; Containers.insert(scene); //Use an empty Ignore types vector OSG::FCFileType::FCTypeVector IgnoreTypes; //IgnoreTypes.push_back(Node::getClassType().getId()); //Write the Field Containers to a xml file OSG::FCFileHandler::the()->write(Containers,OSG::BoostPath("C:/Users/danielg/Desktop/test.xml"),IgnoreTypes); //Read FieldContainers from an XML file OSG::FCFileType::FCPtrStore NewContainers; NewContainers = OSG::FCFileHandler::the()->read(OSG::BoostPath("C://Users//danielg//Documents//VirtualCellData//trunk//Artwork//Models//Vehicles_and_Tools//Protein_Ship//Ship_Export_Test.dae")); //Write the read FieldContainers to an XML file // OSG::FCFileHandler::the()->write(NewContainers,OSG::BoostPath("C:/Users/danielg/Desktop/test2.xml"),IgnoreTypes); //NewContainers.clear(); // NewContainers = OSG::FCFileHandler::the()->read(OSG::BoostPath("C:/Users/danielg/Desktop/test2.xml")); OSG::FCFileType::FCPtrStore::iterator itor = NewContainers.begin(); OSG::FCFileType::FCPtrStore::iterator endIt = NewContainers.end(); OSG::NodeRefPtr root; for(; itor != endIt; itor++) { OSG::Node *cur = OSG::dynamic_pointer_cast<OSG::Node>((*itor)); if(cur != NULL) { if(cur->getParent() == NULL) { root = cur; //mgr->setRoot(cur); break; } } } std::string filepath("C://Users//danielg//Desktop//test.osb"); OSG::SceneFileHandler::the()->write(root,filepath.c_str()); root = OSG::SceneFileHandler::the()->read(filepath.c_str()); if(root != NULL) { mgr->setRoot(root); } else { std::cout << std::endl << "ERROR READING THE OSB FILE BACK IN~~~~!" << std::endl; } // show the whole scene mgr->showAll(); } // GLUT main loop glutMainLoop(); return 0; }
void OsgIconSizeUpdaterVisitor::apply( osg::Node& node ) { std::string node_name = node.getName(); if (node_name == "-icon-") { //get the position of the icon by asking it´s parents osg::Group *father_node = node.getParent(0); if (std::string("MatrixTransform") == father_node->className()) { osg::MatrixTransform *mt = (osg::MatrixTransform *) father_node; osg::Vec3d icon_position = mt->getMatrix().getTrans(); osg::Matrixd md = mt->getMatrix(); IconUserData *iud = (IconUserData *)node.getUserData(); /*float distance = (float) sqrt(pow(icon_position.x() - camera_pos_x, 2) + pow(icon_position.y() - camera_pos_y, 2) + pow(icon_position.z() - camera_pos_z, 2));*/ icon_position.set(osg::Vec3(iud->GetPosition().x, iud->GetPosition().y, iud->GetPosition().z)); float distance = (float) sqrt(pow(icon_position.x() - camera_pos_x, 2) + pow(icon_position.y() - camera_pos_y, 2) + pow(icon_position.z() - camera_pos_z, 2)); float final_size = 0.0f; if (iud->GetHeight() != 0.0f) min_height = iud->GetHeight(); cpw::Point3d<float> op = iud->GetPosition(); if (distance <= min_distance) { final_size = max_size; md.setTrans(osg::Vec3d(op.x, op.y, op.z + min_height)); } else if (distance >= max_distance) { final_size = min_size; md.setTrans(osg::Vec3d(op.x, op.y, op.z + max_height)); } else if ((min_distance < distance) && (distance < max_distance)) { float increment_distance = max_distance - min_distance; float size_difference = max_size - min_size; float height_difference = max_height - min_height; float final_height; final_size = max_size - (size_difference * ((distance-min_distance)/increment_distance)); final_height = min_height + (height_difference * ((distance-min_distance)/increment_distance)); md.setTrans(op.x, op.y, op.z + final_height); //update the matrix } osg::ref_ptr<osg::StateSet> set = node.getStateSet(); /// Give some size to the points to be able to see the sprite osg::ref_ptr<osg::Point> point = new osg::Point(); mt->setMatrix(md); point->setSize(final_size); set->setAttribute(point.get()); } } //keep searching traverse(node); }