コード例 #1
0
OSG::NodeTransitPtr OSGSofaShadowGraph::build( 
    Node::SPtr root, 
    bool       ignoreLights )
{
    _scene = NULL;
    _chunkOverrideGroup = NULL;
    _shadowStage = NULL;

    VisualParams* vparams = VisualParams::defaultInstance();

    OSG::NodeUnrecPtr        shadowStageNode;
    
    _chunkOverrideGroup = OSG::ChunkOverrideGroup::create();

    _scene = OSG::makeNodeFor(_chunkOverrideGroup);

    sofa::simulation::OSGVisualUpdateVisitor vm_visitor( vparams);
    vm_visitor.setChunkOverrideGroupNode(_scene);

    
    // get lights
    if (!ignoreLights)
    {
        if (!_shadowStage) 
            _shadowStage = OSG::ShadowStage::create();
        _shadowStage->setMapSize(1024);
        _shadowStage->setShadowSmoothness(0.5f);
        _shadowStage->setShadowMode(OSG::ShadowStage::NO_SHADOW);
        _shadowStage->setAutoSearchForLights(true);
        shadowStageNode = OSG::makeNodeFor(_shadowStage);

        sofa::simulation::OSGLightVisitor light_visitor( vparams);
        //light_visitor.setTurnOffLights(ignoreLights);

        light_visitor.setOSG2Parent(_scene);
        light_visitor.setOSG2ShadowStage(_shadowStage);
        light_visitor.setAttachNode(_scene);
        root->execute(&light_visitor);
        if (light_visitor.getAttachNode())
        {
            vm_visitor.setOSG2Parent(light_visitor.getAttachNode());
            root->execute(&vm_visitor);
        }
    }
    else
    {
        vm_visitor.setOSG2Parent(_scene);
        root->execute(&vm_visitor);
    }

    if (shadowStageNode && !ignoreLights) 
    {
        shadowStageNode->addChild(_scene);
        return OSG::NodeTransitPtr(shadowStageNode);
    }

    return OSG::NodeTransitPtr(_scene);
}
コード例 #2
0
ファイル: houseOfCards.cpp プロジェクト: FabienPean/sofa
int main(int argc, char** argv)
{
    sofa::simulation::tree::init();
    sofa::component::initComponentBase();
    sofa::component::initComponentCommon();
    sofa::component::initComponentGeneral();
    sofa::component::initComponentAdvanced();
    sofa::component::initComponentMisc();
    sofa::gui::initMain();

    unsigned int sizeHouseOfCards=4;
    SReal angle=20.0;
    SReal distanceInBetween=0.1;
    SReal friction=0.8;
    SReal contactDistance=0.03;
    std::string gui = "";


    std::string gui_help = "choose the UI (";
    gui_help += sofa::gui::GUIManager::ListSupportedGUI('|');
    gui_help += ")";

    sofa::helper::parse("This is a SOFA application. Here are the command line arguments")
    .option(&sizeHouseOfCards,'l',"level","number of level of the house of cards")
    .option(&angle,'a',"angle","angle formed by two cards")
    .option(&distanceInBetween,'d',"distance","distance between two cards")
    .option(&friction,'f',"friction","friction coeff")
    .option(&contactDistance,'c',"contactDistance","contact distance")
    .option(&gui,'g',"gui",gui_help.c_str())
    (argc,argv);


    sofa::simulation::setSimulation(new sofa::simulation::tree::TreeSimulation());

    // The graph root node
    Node::SPtr root = sofa::modeling::createRootWithCollisionPipeline("distanceLMConstraint");
    root->setGravity( Coord3(0,-10,0) );
    root->setDt(0.001);

    sofa::component::collision::MinProximityIntersection *intersection;
    root->get(intersection, sofa::core::objectmodel::BaseContext::SearchDown);
    intersection->alarmDistance.setValue(contactDistance);
    intersection->contactDistance.setValue(contactDistance*0.5);

    //************************************
    //Floor
    Node::SPtr  torusFixed = sofa::modeling::createObstacle(root.get(),"mesh/floor.obj", "mesh/floor.obj", "gray");

    //Add the objects
    createHouseOfCards(root.get(),sizeHouseOfCards,distanceInBetween, angle);


    const SReal contactFriction=sqrt(friction);
    sofa::helper::vector< sofa::core::CollisionModel* > listCollisionModels;
    root->getTreeObjects<sofa::core::CollisionModel>(&listCollisionModels);
    for (unsigned int i=0; i<listCollisionModels.size(); ++i) listCollisionModels[i]->setContactFriction(contactFriction);
    root->setAnimate(false);

    //=======================================
    // Export the scene to file
    const std::string fileName="HouseOfCards.xml";
    sofa::simulation::getSimulation()->exportXML(root.get(),fileName.c_str());

    //=======================================
    // Destroy created scene: step needed, as I can't get rid of the locales (the mass can't init correctly as 0.1 is not considered as a floating point).
    sofa::simulation::DeleteVisitor deleteScene(sofa::core::ExecParams::defaultInstance() );
    root->execute(deleteScene);
    root.reset();

    //=======================================
    // Create the GUI
    if (int err=sofa::gui::GUIManager::Init(argv[0],gui.c_str()))
        return err;

    if (int err=sofa::gui::GUIManager::createGUI(NULL))
        return err;

    sofa::gui::GUIManager::SetDimension(800,600);
    //=======================================
    // Load the Scene

    sofa::simulation::Node::SPtr groot = sofa::core::objectmodel::SPtr_dynamic_cast<sofa::simulation::Node>( sofa::simulation::getSimulation()->load(fileName.c_str()));
    if (groot==NULL)
    {
        groot = sofa::simulation::getSimulation()->createNewGraph("");
    }

    sofa::simulation::getSimulation()->init(groot.get());
    sofa::gui::GUIManager::SetScene(groot,fileName.c_str());


    //=======================================
    // Run the main loop
    if (int err=sofa::gui::GUIManager::MainLoop(groot,fileName.c_str()))
        return err;
    Node* ggroot =  sofa::gui::GUIManager::CurrentSimulation();


    if (ggroot!=NULL) sofa::simulation::getSimulation()->unload(groot);

    sofa::simulation::tree::cleanup();
    return 0;
}