Exemple #1
2
OSG::NodeTransitPtr createLabeledTorus(OSG::Vec3f trans, int idx)
{
    OSG::NodeTransitPtr  node  = OSG::Node::create();
    OSG::TransformRefPtr xform = OSG::Transform::create();
    OSG::Matrix          mat;

    // --- setup transform ------------------
    mat.setIdentity();
    mat.setTranslate(trans);
    xform->setMatrix(mat);
    node->setCore(xform);


    // --- setup label ----------------------
    OSG::NodeRefPtr  labelNode = OSG::Node::create();
    OSG::LabelRefPtr label     = 
        (idx) ? createTextLabel(idx) : createIconLabel();

    updateLabelParams(label, idx);
    labelNode->setCore(label);

    // --- add torus ------------------------
    labelNode->addChild(OSG::makeTorus(.5, 2, 16, 16));

    node->addChild(labelNode);
    return node;
}
OSG_BASE_DLLMAPPING bool MatrixLookAt(OSG::Matrix &result,
                                      OSG::Pnt3f   from, 
                                      OSG::Pnt3f   at, 
                                      OSG::Vec3f   up    )
{
    Vec3f view;
    Vec3f right;
    Vec3f newup;
    Vec3f tmp;

    view = from - at;
    view.normalize();

    right = up.cross(view);

    if(right.dot(right) < TypeTraits<Real>::getDefaultEps())
    {
        return true;
    }

    right.normalize();

    newup = view.cross(right);

    result.setIdentity ();
    result.setTranslate(from[0], from[1], from[2]);

    Matrix tmpm;

    tmpm.setValue(right, newup, view);

    result.mult(tmpm);

    return false;
}
OSG_BASE_DLLMAPPING bool MatrixPerspective(OSG::Matrix &result,
                                           OSG::Real32  rFovy,
                                           OSG::Real32  rAspect,
                                           OSG::Real32  rNear,
                                           OSG::Real32  rFar)
{
    Real32 ct    = osgTan(rFovy);
    bool   error = false;

    if(rNear > rFar)
    {
        SWARNING << "MatrixPerspective: near " << rNear << " > far " << rFar
                 << "!\n" << std::endl;

        error = true;
    }

    if(rFovy <= TypeTraits<Real32>::getDefaultEps())
    {
        SWARNING << "MatrixPerspective: fovy " << rFovy << " very small!\n"
                 << std::endl;

        error = true;
    }

    if(osgAbs(rNear - rFar) < TypeTraits<Real32>::getDefaultEps())
    {
        SWARNING << "MatrixPerspective: near " << rNear << " ~= far " << rFar
                 << "!\n" << std::endl;

        error = true;
    }

    if(rAspect < TypeTraits<Real32>::getDefaultEps())
    {
        SWARNING << "MatrixPerspective: aspect ratio " << rAspect
                 << " very small!\n" << std::endl;

        error = true;
    }

    if(error)
    {
        result.setIdentity();
        return true;
    }

    MatrixFrustum( result, 
                  -rNear * ct * rAspect, 
                   rNear * ct * rAspect,
                  -rNear * ct, 
                   rNear * ct, 
                   rNear, 
                   rFar                );

    return false;
}
OSG_BASE_DLLMAPPING bool MatrixLookAt(OSG::Matrix &result,
                                      OSG::Real32  fromx,
                                      OSG::Real32  fromy,
                                      OSG::Real32  fromz,
                                      OSG::Real32  atx,
                                      OSG::Real32  aty,
                                      OSG::Real32  atz,
                                      OSG::Real32  upx,
                                      OSG::Real32  upy,
                                      OSG::Real32  upz)
{
    Vec3f view;
    Vec3f right;
    Vec3f newup;
    Vec3f up;

    view.setValues(fromx - atx , fromy - aty, fromz - atz);
    view.normalize();

    up.setValues(upx, upy, upz);

    right = up.cross(view);

    if(right.dot(right) < TypeTraits<Real>::getDefaultEps())
    {
        return true;
    }

    right.normalize();

    newup = view.cross(right);

    result.setIdentity ();
    result.setTranslate(fromx, fromy, fromz);

    Matrix tmpm;

    tmpm.setValue(right, newup, view);

    result.mult(tmpm);

    return false;
}
//
// Build the global clip plane state
//
void createClipPlaneDetails(void)
{
    for(int i = 0; i < iNumClipPlanes; ++i)
    {
        ClipPlaneDetails details;

        //
        // Create clip plane chunk
        //
        details._planeBeaconNode = OSG::Node::create();
        details._planeBeaconNode->setCore(OSG::Transform::create());

        container->addContainer(details._planeBeaconNode);

        details._clipPlaneChunk = OSG::ClipPlaneChunk::create();
        details._clipPlaneChunk->setEquation(OSG::Vec4f(1,0,0,0));
        details._clipPlaneChunk->setEnable(false);
        details._clipPlaneChunk->setBeacon(details._planeBeaconNode);


        //
        // Create plane geometry
        //
        details._planeGeometryCore = OSG::makePlaneGeo(100.f, 100.f, 128, 128);

        //
        // Create plane transformation core
        //
        OSG::Matrix mat;
        mat.setIdentity();

        details._planeTrafoCore = OSG::Transform::create();
        details._planeTrafoCore->setMatrix(mat);

        //
        // Define plane color
        //
        details._planeColor = planeCol[i];

        vecClipPlaneDetails.push_back(details);
    }
}
OSG::NodeTransitPtr createScenegraph(void)
{
    // At first we load all needed models from file
    OSG::NodeRecPtr w_high   = 
        OSG::SceneFileHandler::the()->read("Data/woman_high.wrl");

    OSG::NodeRecPtr w_medium = 
        OSG::SceneFileHandler::the()->read("Data/woman_medium.wrl");

    OSG::NodeRecPtr w_low    = 
        OSG::SceneFileHandler::the()->read("Data/woman_low.wrl");
    
    // we check the result
    if((w_high == NULL) || (w_medium == NULL)|| (w_low == NULL))
    {
        std::cout << "It was not possible to load all needed models from file"
                << std::endl;
        return OSG::NodeTransitPtr();
    }
    
    // now the LOD core
    OSG::DistanceLODRecPtr lod = OSG::DistanceLOD::create();
    lod->editSFCenter()->setValue(OSG::Pnt3f(0,0,0));
    lod->editMFRange()->push_back(200);
    lod->editMFRange()->push_back(500);
    
    // the node containing the LOD core. The three models will be
    // added as its children
    OSG::NodeRecPtr lodNode = OSG::Node::create();
    lodNode->setCore(lod);
    lodNode->addChild(w_high);
    lodNode->addChild(w_medium);
    lodNode->addChild(w_low);
    
    // create the node with switch core ********************
    OSG::SwitchRecPtr sw = OSG::Switch::create();
    //Notice: the first choice is 0
    sw->setChoice(0);
    
    OSG::NodeRecPtr switchNode = OSG::Node::create();
    switchNode->setCore(sw);
    switchNode->addChild(lodNode);
    
    //end switch creation **********************************
    
    OSG::NodeRecPtr root = OSG::Node::create();
    root->setCore(OSG::Group::create());
    root->addChild(switchNode);
    
    // we know want to extract the mesh geometry out of the graph
    // it is sufficent to pass the model only as root for searching
    OSG::NodeRecPtr     womanGeometry = checkName(w_high);
    OSG::GeometryRecPtr geo = 
        dynamic_cast<OSG::Geometry *>(womanGeometry->getCore());
    
    //new node with "old" geometry core referenced
    OSG::NodeRecPtr woman = OSG::Node::create();
    woman->setCore(geo);
    
    //translate it a bit to see both women
    OSG::NodeRecPtr      womanTrans = OSG::Node     ::create();
    OSG::TransformRecPtr t          = OSG::Transform::create();
    OSG::Matrix m;
    m.setIdentity();
    m.setTranslate(OSG::Vec3f(0,0,200));
    t->setMatrix(m);
        
    womanTrans->setCore(t);
    womanTrans->addChild(woman);
    
    //add it to the root
    root->addChild(womanTrans);
    
    return OSG::NodeTransitPtr(root);
}
void Animate()
{
    //Show FPS
    //showFpsCounter();

    if(bAnim == true)
    {
        static OSG::Real64   t0 = OSG::getSystemTime();
        
        OSG::Real64          t = OSG::getSystemTime() - t0;
        
        OSG::Real32          rot0 = t * 0.25;
        if(rot0 > 360.0)
            rot0 -= 360.0;
        
        OSG::Real32          rota = t * 0.5;
        if(rota > 360.0)
            rota -= 360.0;
        
        OSG::Real32          rotb = t * 1.0;
        if(rotb > 360.0)
            rotb -= 360.0;
        
        OSG::Real32          rotc = t * 1.5;
        if(rotc > 360.0)
            rotc -= 360.0;
        
        OSG::Real32          rotd = t * 2.0;
        if(rotd > 360.0)
            rotd -= 360.0;
        
        //    _light2_trans->editMatrix().setTranslate(-100.0*sin(rota),-100.0*cos(rota), 250.0);
        
        //animate Trees
        OSG::Quaternion      q;
        
        q.setValueAsAxisRad(1, 1, 1, 0.5 * sin(rota));
        _tree1_trans->editMatrix().setRotate(q);
        _tree1_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0));
        
        q.setIdentity();
        
        q.setValueAsAxisRad(1, 1, 1, 0.5 * sin(rotc));
        _tree2_trans->editMatrix().setRotate(q);
        _tree2_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0));
        
        q.setIdentity();
        
        q.setValueAsAxisRad(1, 1, 1, 0.5 * sin(rotb));
        _tree3_trans->editMatrix().setRotate(q);
        _tree3_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0));
        
        q.setIdentity();
        
        q.setValueAsAxisRad(1, 1, 1, 0.5 * sin(rotb));
        _tree4_trans->editMatrix().setRotate(q);
        _tree4_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0));
        
        q.setIdentity();
        
        q.setValueAsAxisRad(1, 1, 1, 0.5 * sin(rotc));
        _tree5_trans->editMatrix().setRotate(q);
        _tree5_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0));
        
        q.setIdentity();
        
        q.setValueAsAxisRad(1, 1, 1, 0.5 * sin(rotb));
        _tree6_trans->editMatrix().setRotate(q);
        _tree6_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0));
        
        q.setIdentity();
        
        q.setValueAsAxisRad(1, 1, 1, 0.5 * sin(rotd));
        _tree7_trans->editMatrix().setRotate(q);
        _tree7_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0));
        
        q.setIdentity();
        
        q.setValueAsAxisRad(1, 1, 1, 0.5 * sin(rotb));
        _tree8_trans->editMatrix().setRotate(q);
        _tree8_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0));
        
        q.setIdentity();
        
        q.setValueAsAxisRad(1, 1, 1, 0.5 * sin(rotc));
        _tree9_trans->editMatrix().setRotate(q);
        _tree9_trans->editMatrix().setScale(OSG::Vec3f(12.0, 12.0, 10.0));
        
        q.setIdentity();
        
        {
            OSG::Matrix  m;
            m.setIdentity();
            OSG::Vec3f   scale(0.15f,0.15f,0.15f);
            OSG::Vec3f   trans(-40.0 * sin(rotb),-40.0 * cos(rotb),
                          50.0 + 25.0 * sin(rotd));
            q.setValueAsAxisRad(0, 0, 1, -rotb);
            m.setTransform(trans, q, scale);
            _obj1_trans->setMatrix(m);
        }
        
        
        // {
        //     _light2_trans->editMatrix().setTranslate(-40.0 * sin(rotb), -40.0 *
        //                                              cos(rotb),
        //                                              50.0 + 25.0 * sin(rotd));
        // }
        
        //animate Dinos
        
        {
            OSG::Matrix  m;
            m.setIdentity();
            OSG::Vec3f   scale(5.0,5.0,5.0);
            OSG::Real32  ztrans1 = sin(2.0 * rotd);
            if(ztrans1 < 0)
                ztrans1 *= -1.0;
            OSG::Vec3f   trans(-96.0 * cos(rot0),-96.0 * sin(rot0),
                          10.0 + 8.0 * ztrans1);
            m.setScale(OSG::Vec3f(5.0, 5.0, 5.0));
            q.setValueAsAxisRad(0, 0, 1, rot0 - 170);
            m.setTransform(trans, q, scale);
            _dino1_trans->setMatrix(m);
        }
        
        {
            OSG::Matrix  m;
            m.setIdentity();
            OSG::Vec3f   scale(3.0,3.0,3.0);
            OSG::Real32  ztrans1 = sin(2.5 * rotd);
            if(ztrans1 < 0)
                ztrans1 *= -1.0;
            OSG::Vec3f   trans(-96.0 * cos((rot0 - 0.5)),-96.0 * sin((rot0 - 0.5)),
                          6.0 + 8.0 * ztrans1);
            m.setScale(OSG::Vec3f(5.0, 5.0, 5.0));
            q.setValueAsAxisRad(0, 0, 1, rot0 - 169.9);
            m.setTransform(trans, q, scale);
            _dino2_trans->setMatrix(m);
        }
        
        {
            OSG::Matrix  m;
            m.setIdentity();
            OSG::Vec3f   scale(3.0,3.0,3.0);
            OSG::Real32  ztrans1 = sin(3.0 * rotd);
            if(ztrans1 < 0)
                ztrans1 *= -1.0;
            OSG::Vec3f   trans(-96.0 * cos((rot0 - 0.8)),-96.0 * sin((rot0 - 0.8)),
                          6.0 + 8.0 * ztrans1);
            m.setScale(OSG::Vec3f(5.0, 5.0, 5.0));
            q.setValueAsAxisRad(0, 0, 1, rot0 - 170.1);
            m.setTransform(trans, q, scale);
            _dino3_trans->setMatrix(m);
        }
        
        {
            OSG::Matrix  m;
            m.setIdentity();
            OSG::Vec3f   scale(3.0,3.0,3.0);
            OSG::Real32  ztrans1 = sin(2.75 * rotd);
            if(ztrans1 < 0)
                ztrans1 *= -1.0;
            OSG::Vec3f   trans(-96.0 * cos((rot0 - 1.2)),-96.0 * sin((rot0 - 1.2)),
                          6.0 + 8.0 * ztrans1);
            m.setScale(OSG::Vec3f(5.0, 5.0, 5.0));
            q.setValueAsAxisRad(0, 0, 1, rot0 - 170.1);
            m.setTransform(trans, q, scale);
            _dino4_trans->setMatrix(m);
        }
    }

    _navigator.idle(_mousebuttons, _lastx, _lasty);
    mgr->redraw();
}
OSG_BASE_DLLMAPPING bool MatrixStereoPerspective(OSG::Matrix &projection,
                                                 OSG::Matrix &projtrans,
                                                 OSG::Real32  rFovy,
                                                 OSG::Real32  rAspect,
                                                 OSG::Real32  rNear,
                                                 OSG::Real32  rFar,
                                                 OSG::Real32  rZeroparallax,
                                                 OSG::Real32  rEyedistance,
                                                 OSG::Real32  rWhicheye,
                                                 OSG::Real32  rOverlap)
{
    Real32 rLeft;
    Real32 rRight;
    Real32 rTop;
    Real32 rBottom;

    Real32 gltan;
    Real32 rEye = -rEyedistance * (rWhicheye - .5f);
    Real32 d;

    bool  error = false;

    if(rNear > rFar)
    {
        SWARNING << "MatrixPerspective: near " << rNear << " > far " << rFar
                 << "!\n" << std::endl;

        error = true;
    }

    if(rFovy <= TypeTraits<Real32>::getDefaultEps())
    {
        SWARNING << "MatrixPerspective: fovy " << rFovy << " very small!\n"
                 << std::endl;

        error = true;
    }

    if(osgAbs(rNear - rFar) < TypeTraits<Real32>::getDefaultEps())
    {
        SWARNING << "MatrixPerspective: near " << rNear << " ~= far " << rFar
                 << "!\n" << std::endl;

        error = true;
    }

    if(rAspect < TypeTraits<Real32>::getDefaultEps())
    {
        SWARNING << "MatrixPerspective: aspect ratio " << rAspect
                 << " very small!\n"                   << std::endl;

        error = true;
    }

    if(rZeroparallax < TypeTraits<Real32>::getDefaultEps())
    {
        SWARNING << "MatrixPerspective: zero parallax " << rZeroparallax
                 << " very small, setting to 1!\n"      << std::endl;

        rZeroparallax = 1.f;
        error         = true;
    }
    
    if(error)
    {
        projection.setIdentity();
        projtrans .setIdentity();

        return true;
    }

    /* Calculate upper and lower clipping planes */
    rTop    = osgTan(rFovy / 2.0f) * rNear; 
    rBottom = -rTop;

    /* Calculate left and right clipping planes */
    gltan  = osgTan(rFovy / 2.0f) * rAspect;  

    rLeft  = (-gltan + rEye / rZeroparallax) * rNear;
    rRight = ( gltan + rEye / rZeroparallax) * rNear;

    d = rRight - rLeft;

    rLeft  += d * (1.f - rOverlap) * (rWhicheye - .5f);
    rRight += d * (1.f - rOverlap) * (rWhicheye - .5f);

    MatrixFrustum(projection, rLeft, rRight, rBottom, rTop, rNear, rFar);

    projtrans.setIdentity();

    projtrans[3][0] = rEye;

    return false;
}