示例#1
0
bool HUDSettings::getInverseModelViewMatrix(osg::Matrix& matrix, osg::NodeVisitor* nv) const
{
    osg::Matrix modelView;
    getModelViewMatrix(modelView,nv);
    matrix.invert(modelView);
    return true;
}
示例#2
0
btCollisionShape* VRPhysics::getConvexShape() {
    btConvexHullShape* shape = new btConvexHullShape();

    OSG::Matrix m;
    OSG::Matrix M = vr_obj->getWorldMatrix();
    M.invert();

    vector<OSG::VRObject*> geos = vr_obj->getObjectListByType("Geometry");
	for (auto g : geos) {
        OSG::VRGeometry* geo = (OSG::VRGeometry*)g;
        if (geo == 0) continue;
        if (geo->getMesh() == 0) continue;
        OSG::GeoVectorPropertyRecPtr pos = geo->getMesh()->getPositions();
        if (pos == 0) continue;

        if (geo != vr_obj) {
            m = geo->getWorldMatrix();
            m.multLeft(M);
        }

		for (unsigned int i = 0; i<pos->size(); i++) {
            OSG::Pnt3f p;
            pos->getValue(p,i);
            if (geo != vr_obj) m.mult(p,p);
            for (int i=0; i<3; i++) p[i] *= scale[i];
            shape->addPoint(btVector3(p[0], p[1], p[2]));
        }
    }

    shape->setMargin(collisionMargin);

    //cout << "\nConstruct Convex shape for " << vr_obj->getName() << endl;
    return shape;
}
static 
void updateSpecialParameter(
    OSG::SHLChunk::GetUniformLocProc  getUniformLocation,
    OSG::DrawEnv                     *action, 
    GLuint                            program           )
{
#if 0
    if(action->getCamera() == NULL || action->getViewport() == NULL)
    {
        FWARNING(("updateSpecialParameter : Can't update OSGSpecialParameter"
                  "parameter, camera or viewport is NULL!\n"));
        return;
    }

    // uploads the camera orientation.
    OSG::Matrix m;
    action->getCamera()->getViewing(m,
                                action->getViewport()->getPixelWidth(),
                                action->getViewport()->getPixelHeight());
    m.invert();
    m[3].setValues(0, 0, 0, 1);

    //std::cout << "uploading matrix " << m << std::endl;

    // get "glUniformMatrix4fvARB" function pointer
    OSGGLUNIFORMMATRIXFVARBPROC uniformMatrix4fv = 
        reinterpret_cast<OSGGLUNIFORMMATRIXFVARBPROC>(
            action->getWindow()->getFunction(
                OSG::SHLChunk::getFuncUniformMatrix4fv()));

    GLint location = getUniformLocation(program, "OSGSpecialParameter");

    if(location != -1)
        uniformMatrix4fv(location, 1, GL_FALSE, m.getValues());
#endif
}
/*! The mouseMove is called by the viewer when the mouse is moved in the
    viewer and this handle is the active one.

    \param x the x-pos of the mouse (pixel)
    \param y the y-pos of the mouse (pixel)
 */
void PlaneMoveManipulator::mouseMove(const Int16 x,
                            const Int16 y)
{
    SLOG << "==============================" << endLog;
    SLOG << "PlaneMoveManipulator::mouseMove() enter x=" << x << " y=" << y << endLog;

    // get the beacon's core (must be ComponentTransform) and it's center
    if( getTarget() == NULL )
    {
        SWARNING << "Handle has no target.\n";
        return;
        
    }
    // get transformation of beacon
    Transform *t = dynamic_cast<Transform *>(getTarget()->getCore());

    if( t == NULL )
    {
        SWARNING << "handled object has no parent transform!\n";
        return;
    }

    Vec3f      translation;       // for matrix decomposition
    Quaternion rotation;
    Vec3f      scaleFactor;
    Quaternion scaleOrientation;

    t->getMatrix().getTransform(translation, rotation, scaleFactor,
                                scaleOrientation);

    OSG::Line viewray;
    getViewport()->getCamera()->calcViewRay(viewray, x, y, *getViewport());

    SLOG << "Manipulator::mouseMove(): viewray: " << viewray << endLog;

    // Get manipulator axes into world space
    OSG::Matrix tm = getTarget()->getToWorld();
    
    Vec3f rot_axis;
    tm.multFull(Vec3f(0,1,0), rot_axis);

    Plane pl(rot_axis, getClickPoint());

    Pnt3f plpoint;
    
    if (pl.intersect(viewray, plpoint) == true) // Ignore moving out of the plane...
    {
        SLOG << "Manipulator::mouseMove(): plpoint: " << plpoint << endLog;
         
        Vec3f      trans = getBaseTranslation();
        Quaternion rot   = getBaseRotation();

        // Get manipulator axes into world space
        Vec3f xp,zp;

        tm.multFull(Vec3f(1,0,0), xp);
        tm.multFull(Vec3f(0,0,1), zp);
        
        if (getActiveSubHandle() == getHandleXNode())
        {
            Line xaxis(getClickPoint(), xp);
            Line zaxis(getClickPoint(), zp);

            Real32 fx = xaxis.getClosestPointT(plpoint);
            Real32 fz = zaxis.getClosestPointT(plpoint);

            SLOG << "Manipulator::mouseMove(): xaxis: " << xaxis << " zaxis: " << zaxis <<endLog;
            SLOG << "Manipulator::mouseMove(): fx: " << fx << " fz: " << fz <<endLog;
        
            // Alternative: transform hitpoint into manip space
            OSG::Matrix m = getTarget()->getToWorld();
            m.invert();
            
            Pnt3f mpoint;
            m.mult(plpoint, mpoint);
            
            SLOG << "Manipulator::mouseMove(): mpoint:" << mpoint << endLog;

            trans = trans + xp * fx + zp * fz;
        }
        else if (getActiveSubHandle() == getHandleZNode())
        {
            Pnt3f wcenter;
            
            tm.multFull(Pnt3f(0,getLength()[1],0), wcenter);
            
            Vec3f vclick, vcurrent;
            
            vclick = getClickPoint() - wcenter;
            vcurrent = plpoint - wcenter;
            
            vclick.normalize();
            vcurrent.normalize();
            
            Real32 a = vclick.enclosedAngle(vcurrent);
            
            SLOG << "Manipulator::mouseMove(): wcenter:" << wcenter << "" <<endLog;
            SLOG << "Manipulator::mouseMove(): vclick:" << vclick << " vcurrent:" << vcurrent <<endLog;
            SLOG << "Manipulator::mouseMove(): angle:" << a << " deg: " << osgRad2Degree(a) << endLog;
        }
        
        Matrix m;

        m.setTransform(trans, rot, scaleFactor, scaleOrientation);

        t->setMatrix(m);
        
    }

    setLastMousePos(Pnt2f(Real32(x), Real32(y)));
    updateHandleTransform();

    //SLOG << "Manipulator::mouseMove() leave\n" << std::flush;
}
void CgFXPassChunk::updateStateUniforms(DrawEnv  *pEnv)
{
    CgFXMaterial *pMat = _sfMaterial.getValue();

    OSG_ASSERT(pMat != NULL);

          CGeffect     pEffect        = pMat->getEffect        ();
          UInt32       uiStateVars    = pMat->getStateVariables();
    const std::string *vStateVarNames = pMat->getStateVarNames ();

          UInt32       uiMask         = 0x0001;


    OSG_ASSERT(pEffect != NULL);

    Matrix mObj2World = pEnv->getObjectToWorld();

    std::string szTmp;

    for(UInt32 i = 0; i < CgFXMaterial::NumStateVars; ++i)
    {
        if(uiStateVars == 0x0000)
            break;
        
        switch(uiStateVars & uiMask)
        {
            case CgFXMaterial::CgProjectionMask:
            {
                CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[
                            CgFXMaterial::CgProjection].c_str());

                CgFXMaterial::checkForCgError("cgGetNamedEffectParameter", 
                                              NULL);

                OSG_ASSERT(pMatrixParam != NULL);

                cgGLSetMatrixParameterfc(
                    pMatrixParam,
                    pEnv->_openGLState.getProjection().getValues());

                CgFXMaterial::checkForCgError("cgGLSetMatrixParameterfc", NULL);
            }
            break;

            case CgFXMaterial::CgModelViewProjectionMask:
            {
                CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[
                            CgFXMaterial::CgModelViewProjection].c_str());

                CgFXMaterial::checkForCgError("cgGetNamedEffectParameter", 
                                              NULL);

                OSG_ASSERT(pMatrixParam != NULL);

                Matrix mWorld2Scrn = pEnv->getWorldToScreen();

                mWorld2Scrn.mult(mObj2World);                

                cgGLSetMatrixParameterfc(pMatrixParam,
                                         mWorld2Scrn.getValues());

                CgFXMaterial::checkForCgError("cgGLSetMatrixParameterfc", 
                                              NULL);
            }
            break;


            // -------------
            // Model | World
            // -------------
            case CgFXMaterial::CgModelMask:
            {
                CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[
                            CgFXMaterial::CgModel].c_str());

                CgFXMaterial::checkForCgError("cgGetNamedEffectParameter", 
                                              NULL);

                OSG_ASSERT(pMatrixParam != NULL);

                cgGLSetMatrixParameterfc(pMatrixParam,
                                         mObj2World.getValues());

                CgFXMaterial::checkForCgError("cgGLSetMatrixParameterfc", 
                                              NULL);
            }
            break;

            case CgFXMaterial::CgModelIMask:
            {
                 CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[
                            CgFXMaterial::CgModelI].c_str());

                CgFXMaterial::checkForCgError("cgGetNamedEffectParameter", 
                                              NULL);

                OSG_ASSERT(pMatrixParam != NULL);

                Matrix mModelI = mObj2World;

                mModelI.invert();

                cgGLSetMatrixParameterfc(pMatrixParam,
                                         mModelI.getValues());

                CgFXMaterial::checkForCgError("cgGLSetMatrixParameterfc", 
                                              NULL);
            }
            break;

            case CgFXMaterial::CgModelITMask:
            {
                 CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[
                            CgFXMaterial::CgModelIT].c_str());

                OSG_ASSERT(pMatrixParam != NULL);

                Matrix mModelIT = mObj2World;

                mModelIT.invert   ();
                mModelIT.transpose();

                cgGLSetMatrixParameterfc(pMatrixParam,
                                         mModelIT.getValues());

                CgFXMaterial::checkForCgError("cgGLSetMatrixParameterfc", 
                                              NULL);
            }
            break;


            // ---------------------
            // ModelView | WorldView
            // ---------------------
            case CgFXMaterial::CgModelViewMask:
            {
                 CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[CgFXMaterial::CgModelView].c_str());

                CgFXMaterial::checkForCgError("cgGetNamedEffectParameter", 
                                              NULL);

                OSG_ASSERT(pMatrixParam != NULL);

                Matrix mCameraViewing = pEnv->getCameraViewing();

                mCameraViewing.mult(mObj2World);
                
                cgGLSetMatrixParameterfc(pMatrixParam,
                                         mCameraViewing.getValues());

                CgFXMaterial::checkForCgError("cgGLSetMatrixParameterfc", 
                                              NULL);
            }
            break;

            case CgFXMaterial::CgModelViewIMask:
            {
                CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[CgFXMaterial::CgModelViewI].c_str());

                CgFXMaterial::checkForCgError("cgGetNamedEffectParameter", 
                                              NULL);

                OSG_ASSERT(pMatrixParam != NULL);

                Matrix mCameraViewing = pEnv->getCameraViewing();

                mCameraViewing.mult(mObj2World);
                mCameraViewing.invert();

                cgGLSetMatrixParameterfc(pMatrixParam,
                                         mCameraViewing.getValues());

                CgFXMaterial::checkForCgError("cgGLSetMatrixParameterfc", 
                                              NULL);
            }
            break;

            case CgFXMaterial::CgModelViewITMask:
            {
                CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[CgFXMaterial::CgModelViewIT].c_str());

                CgFXMaterial::checkForCgError("cgGetNamedEffectParameter", 
                                              NULL);

                OSG_ASSERT(pMatrixParam != NULL);

                Matrix mCameraViewing = pEnv->getCameraViewing();

                mCameraViewing.mult     (mObj2World);
                mCameraViewing.invert   (          );
                mCameraViewing.transpose(          );

                cgGLSetMatrixParameterfc(pMatrixParam,
                                         mCameraViewing.getValues());

                CgFXMaterial::checkForCgError("cgGLSetMatrixParameterfc", 
                                              NULL);
            }
            break;


            // -------------
            // View
            // -------------
            case CgFXMaterial::CgViewMask:
            {
                 CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[
                            CgFXMaterial::CgView].c_str());

                CgFXMaterial::checkForCgError("cgGetNamedEffectParameter", 
                                              NULL);

                OSG_ASSERT(pMatrixParam != NULL);

                cgGLSetMatrixParameterfc(pMatrixParam,
                                         pEnv->getCameraViewing().getValues());

                CgFXMaterial::checkForCgError("cgGLSetMatrixParameterfc", 
                                              NULL);
            }
            break;

            case CgFXMaterial::CgViewIMask:
            {
                 CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[
                            CgFXMaterial::CgViewI].c_str());

                CgFXMaterial::checkForCgError("cgGetNamedEffectParameter", 
                                              NULL);

                OSG_ASSERT(pMatrixParam != NULL);

                OSG::Matrix mCameraViewing = pEnv->getCameraViewing();

                mCameraViewing.invert();

                cgGLSetMatrixParameterfc(pMatrixParam,
                                         mCameraViewing.getValues());

                CgFXMaterial::checkForCgError("cgGLSetMatrixParameterfc", 
                                              NULL);
            }
            break;

            case CgFXMaterial::CgViewITMask:
            {
                 CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[
                            CgFXMaterial::CgViewIT].c_str());

                CgFXMaterial::checkForCgError("cgGetNamedEffectParameter", 
                                              NULL);

                OSG_ASSERT(pMatrixParam != NULL);

                OSG::Matrix mCameraViewing = pEnv->getCameraViewing();

                mCameraViewing.invert   ();
                mCameraViewing.transpose();

                cgGLSetMatrixParameterfc(pMatrixParam,
                                         mCameraViewing.getValues());

                CgFXMaterial::checkForCgError("cgGLSetMatrixParameterfc", 
                                              NULL);
            }
            break;


            case CgFXMaterial::CgViewProjectionMask:
            {
                 CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[
                            CgFXMaterial::CgViewProjection].c_str());

                CgFXMaterial::checkForCgError("cgGetNamedEffectParameter", 
                                              NULL);

                OSG_ASSERT(pMatrixParam != NULL);

                cgGLSetMatrixParameterfc(pMatrixParam,
                                         pEnv->getWorldToScreen().getValues());

                CgFXMaterial::checkForCgError("cgGLSetMatrixParameterfc", 
                                              NULL);
            }
            break;
    
            case CgFXMaterial::CgTimeMask:
            {
                CGparameter pTime = 
                    cgGetNamedEffectParameter(
                        pEffect,
                        vStateVarNames[
                            CgFXMaterial::CgTime].c_str());


                CgFXMaterial::checkForCgError("cgGetNamedEffectParameter", 
                                              NULL);

                OSG_ASSERT(pTime != NULL);

                static const UInt16 MaxLeftDecDigits(4);

                // getSystemTime() returns a time value as a 64-bit floating
                // point number.  But the time value taken by Cg is a 32-bit
                // float.  This can cause a problem with precision when
                // getSystemTime() returns large values, that truncate when cast
                // to a 32-bit float.
                //
                // To deal with this, we are removing the most significant
                // decimal digits left of the decimal points after the
                // MaxLeftDecDigits one

                Time tSysTime(OSG::getSystemTime());

                Time tBase10Shift(osgPow<Time>(10, MaxLeftDecDigits));
                Time tTruncValue(
                    tSysTime - 
                    (floor(tSysTime / tBase10Shift) * tBase10Shift));
                
                cgSetParameter1f(pTime, static_cast<Real32>(tTruncValue));
            }
            break;

            default:
                break;
        };

        uiStateVars &= ~uiMask;
        uiMask      =   uiMask << 1;
    }
}
void CgFXPassChunk::updateStateUniforms(DrawEnv  *pEnv)
{
    CgFXMaterial *pMat = _sfMaterial.getValue();

    OSG_ASSERT(pMat != NULL);

          CGeffect     pEffect        = pMat->getEffect        ();
          UInt32       uiStateVars    = pMat->getStateVariables();
    const std::string *vStateVarNames = pMat->getStateVarNames ();

          UInt32       uiMask         = 0x0001;


    OSG_ASSERT(pEffect != NULL);

    Matrix mObj2World = pEnv->getObjectToWorld();

    std::string szTmp;

    for(UInt32 i = 0; i < CgFXMaterial::NumStateVars; ++i)
    {
        if(uiStateVars == 0x0000)
            break;
        
        switch(uiStateVars & uiMask)
        {
            case CgFXMaterial::CgProjectionMask:
            {
                CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[
                            CgFXMaterial::CgProjection].c_str());

                OSG_ASSERT(pMatrixParam != NULL);

                cgGLSetMatrixParameterfc(
                    pMatrixParam,
                    pEnv->_openGLState.getProjection().getValues());
            }
            break;

            case CgFXMaterial::CgModelViewProjectionMask:
            {
                CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[
                            CgFXMaterial::CgModelViewProjection].c_str());

                OSG_ASSERT(pMatrixParam != NULL);

                Matrix mWorld2Scrn = pEnv->getWorldToScreen();

                mWorld2Scrn.mult(mObj2World);                

                cgGLSetMatrixParameterfc(pMatrixParam,
                                         mWorld2Scrn.getValues());
            }
            break;


            // -------------
            // Model | World
            // -------------
            case CgFXMaterial::CgModelMask:
            {
                CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[
                            CgFXMaterial::CgModel].c_str());

                OSG_ASSERT(pMatrixParam != NULL);

                cgGLSetMatrixParameterfc(pMatrixParam,
                                         mObj2World.getValues());
            }
            break;
            case CgFXMaterial::CgModelIMask:
            {
                 CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[
                            CgFXMaterial::CgModelI].c_str());

                OSG_ASSERT(pMatrixParam != NULL);

                Matrix mModelI = mObj2World;

                mModelI.invert();

                cgGLSetMatrixParameterfc(pMatrixParam,
                                         mModelI.getValues());
           }
            break;
            case CgFXMaterial::CgModelITMask:
            {
                 CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[
                            CgFXMaterial::CgModelIT].c_str());

                OSG_ASSERT(pMatrixParam != NULL);

                Matrix mModelIT = mObj2World;

                mModelIT.invert   ();
                mModelIT.transpose();

                cgGLSetMatrixParameterfc(pMatrixParam,
                                         mModelIT.getValues());
            }
            break;


            // ---------------------
            // ModelView | WorldView
            // ---------------------
            case CgFXMaterial::CgModelViewMask:
            {
                 CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[CgFXMaterial::CgModelView].c_str());

                OSG_ASSERT(pMatrixParam != NULL);

                Matrix mCameraViewing = pEnv->getCameraViewing();

                mCameraViewing.mult(mObj2World);
                
                cgGLSetMatrixParameterfc(pMatrixParam,
                                         mCameraViewing.getValues());
            }
            break;
            case CgFXMaterial::CgModelViewIMask:
            {
                CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[CgFXMaterial::CgModelViewI].c_str());

                OSG_ASSERT(pMatrixParam != NULL);

                Matrix mCameraViewing = pEnv->getCameraViewing();

                mCameraViewing.mult(mObj2World);
                mCameraViewing.invert();

                cgGLSetMatrixParameterfc(pMatrixParam,
                                         mCameraViewing.getValues());
            }
            break;
            case CgFXMaterial::CgModelViewITMask:
            {
                CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[CgFXMaterial::CgModelViewIT].c_str());

                OSG_ASSERT(pMatrixParam != NULL);

                Matrix mCameraViewing = pEnv->getCameraViewing();

                mCameraViewing.mult     (mObj2World);
                mCameraViewing.invert   (          );
                mCameraViewing.transpose(          );

                cgGLSetMatrixParameterfc(pMatrixParam,
                                         mCameraViewing.getValues());
            }
            break;


            // -------------
            // View
            // -------------
            case CgFXMaterial::CgViewMask:
            {
                 CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[
                            CgFXMaterial::CgView].c_str());

                OSG_ASSERT(pMatrixParam != NULL);

                cgGLSetMatrixParameterfc(pMatrixParam,
                                         pEnv->getCameraViewing().getValues());
            }
            break;
            case CgFXMaterial::CgViewIMask:
            {
                 CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[
                            CgFXMaterial::CgViewI].c_str());

                OSG_ASSERT(pMatrixParam != NULL);

                OSG::Matrix mCameraViewing = pEnv->getCameraViewing();

                mCameraViewing.invert();

                cgGLSetMatrixParameterfc(pMatrixParam,
                                         mCameraViewing.getValues());
            }
            break;
            case CgFXMaterial::CgViewITMask:
            {
                 CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[
                            CgFXMaterial::CgViewIT].c_str());

                OSG_ASSERT(pMatrixParam != NULL);

                OSG::Matrix mCameraViewing = pEnv->getCameraViewing();

                mCameraViewing.invert   ();
                mCameraViewing.transpose();

                cgGLSetMatrixParameterfc(pMatrixParam,
                                         mCameraViewing.getValues());
            }
            break;


            case CgFXMaterial::CgViewProjectionMask:
            {
                 CGparameter pMatrixParam = 
                    cgGetNamedEffectParameter(
                        pEffect, 
                        vStateVarNames[
                            CgFXMaterial::CgViewProjection].c_str());

                OSG_ASSERT(pMatrixParam != NULL);

                cgGLSetMatrixParameterfc(pMatrixParam,
                                         pEnv->getWorldToScreen().getValues());
            }
            break;

            default:
                break;
        };

        uiStateVars &= ~uiMask;
        uiMask      =   uiMask << 1;
    }
}