Ejemplo n.º 1
0
		void apply( osg::Geode& geode )
		{
			for( unsigned d=0; d<geode.getNumDrawables(); ++d )
			{
				osg::Geometry* geom = geode.getDrawable(d)->asGeometry();
				if ( geom )
				{
					if ( _optimize )
					{
						// activate VBOs
						geom->setUseDisplayList( false );
						geom->setUseVertexBufferObjects( true );
					}

					//geom->setComputeBoundingBoxCallback(  new StaticBoundingBox(_bb));
					//geom->setComputeBoundingBoxCallback(NULL);
					geom->setInitialBound(_bb);
					//geom->dirtyBound();

					// convert to use DrawInstanced
					for( unsigned p=0; p<geom->getNumPrimitiveSets(); ++p )
					{
						osg::PrimitiveSet* ps = geom->getPrimitiveSet(p);
						ps->setNumInstances( _numInstances );
						_primitiveSets.push_back( ps );
					}
					_geometries.push_back(geom);
				}
			}

			traverse(geode);
		}
Ejemplo n.º 2
0
	virtual void apply(osg::Geode& geode)
	{
		for (unsigned i=0; i<geode.getNumDrawables(); ++i)
		{
			osg::Geometry *geo = dynamic_cast<osg::Geometry *>(geode.getDrawable(i));
			if (!geo) continue;

			osg::StateSet *stateset = geo->getStateSet();
			if (!stateset) continue;

			osg::StateAttribute *state = stateset->getAttribute(osg::StateAttribute::MATERIAL);
			if (!state) continue;

			osg::Material *mat = dynamic_cast<osg::Material *>(state);
			if (!mat) continue;

			const osg::Vec4 v4 = mat->getDiffuse(FAB);
			if (v4.r() == 1.0f && v4.g() == 0.0f && v4.b() == 1.0f)
			{
				//VTLOG("oldmat rc %d, ", mat->referenceCount());

				osg::Material *newmat = (osg::Material *)mat->clone(osg::CopyOp::DEEP_COPY_ALL);
				newmat->setDiffuse(FAB, osg::Vec4(c.r*2/3,c.g*2/3,c.b*2/3,1));
				newmat->setAmbient(FAB, osg::Vec4(c.r*1/3,c.g*1/3,c.b*1/3,1));

				//VTLOG("newmat rc %d\n", newmat->referenceCount());

				stateset->setAttribute(newmat);
				//VTLOG(" -> %d %d\n", mat->referenceCount(), newmat->referenceCount());
			}
		}
		osg::NodeVisitor::apply(geode);
	}
Ejemplo n.º 3
0
void OptTexture::apply(osg::Geode& geode)
{
// 	if(geode.getStateSet())
// 	{
// 		apply(geode.getStateSet());
// 	}
	unsigned int cnt = geode.getNumDrawables();
	for(unsigned int i = 0; i < cnt; i++)
	{
		//20130915 MYF模型深度测试不正确
		osg::StateSet *stateset = geode.getDrawable(i)->getOrCreateStateSet();
		stateset->setMode(GL_BLEND,osg::StateAttribute::ON);  //取消深度测试   
		stateset->setMode( GL_DEPTH_TEST, osg::StateAttribute::ON  );  
	//	stateset->setMode( GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED );  
		//stateset->setRenderBinDetails(0, "RenderBin");
		//设置渲染优先级------级别理论上来讲 比你背景的node靠后就行,没设置过的是-1. 

		geode.getDrawable(i)->setStateSet(stateset);


		//geode.getDrawable(i)->getStateSet()->setRenderBinMode(osg::StateSet::RenderBinMode::INHERIT_RENDERBIN_DETAILS);
		//	apply(geode.getDrawable(i)->getStateSet());
	}
	traverse(geode);
}
Ejemplo n.º 4
0
void 
ConvertToDrawInstanced::apply( osg::Geode& geode )
{
    for( unsigned d=0; d<geode.getNumDrawables(); ++d )
    {
        osg::Geometry* geom = geode.getDrawable(d)->asGeometry();
        if ( geom )
        {
            if ( _optimize )
            {
                //osgUtil::IndexMeshVisitor imv;
                //imv.makeMesh( *geom );

                // activate VBOs
                geom->setUseDisplayList( false );
                geom->setUseVertexBufferObjects( true );
            }

            geom->setComputeBoundingBoxCallback( _staticBBoxCallback.get() ); 
            geom->dirtyBound();

            // convert to use DrawInstanced
            for( unsigned p=0; p<geom->getNumPrimitiveSets(); ++p )
            {
                geom->getPrimitiveSet(p)->setNumInstances( _numInstances );
            }
        }
    }

    traverse(geode);
}
Ejemplo n.º 5
0
void 
ShaderGenerator::apply( osg::Geode& geode )
{
    if ( !_active ) return;

    osg::ref_ptr<osg::StateSet> ss = geode.getStateSet();
    if ( ss.valid() )
    {
        _state->pushStateSet( ss.get() );

        osg::ref_ptr<osg::StateSet> replacement;
        if ( processGeometry(ss.get(), replacement) )
        {
            _state->popStateSet();
            
            // optimize state set sharing
            if ( _stateSetCache.valid() )
                _stateSetCache->share(replacement, replacement);

            geode.setStateSet( replacement.get() );

            _state->pushStateSet( replacement.get() );
        }
    }

    for( unsigned d = 0; d < geode.getNumDrawables(); ++d )
    {
        apply( geode.getDrawable(d) );
    }

    if ( ss.valid() )
    {
        _state->popStateSet();
    }
}
Ejemplo n.º 6
0
void WriterNodeVisitor::apply( osg::Geode &node )
{
    pushStateSet(node.getStateSet());
    //_nameStack.push_back(node.getName());
    unsigned int count = node.getNumDrawables();
    ListTriangle listTriangles;
    bool texcoords = false;
    for ( unsigned int i = 0; i < count; i++ )
    {
        osg::Geometry *g = node.getDrawable( i )->asGeometry();
        if ( g != NULL )
        {
            pushStateSet(g->getStateSet());
            createListTriangle(g, listTriangles, texcoords, i);        // May set _succeded to false
            popStateSet(g->getStateSet());
            if (!succeeded()) break;
        }
    }
    if (succeeded() && count > 0)
    {
#if DISABLE_3DS_ANIMATION
        osg::Matrix mat( osg::computeLocalToWorld(getNodePath()) );
        buildFaces(node, mat, listTriangles, texcoords);        // May set _succeded to false
#else
        buildFaces(node, osg::Matrix(), listTriangles, texcoords);        // May set _succeded to false
#endif
    }
    popStateSet(node.getStateSet());
    //_nameStack.pop_back();
    if (succeeded())
        traverse(node);
}
Ejemplo n.º 7
0
void ComputeCylinderVisitor::apply( osg::Geode & geode )
{
    for( unsigned int i = 0; i < geode.getNumDrawables(); ++i )
    {
        applyDrawable( geode.getDrawable( i ) );
    }
}
Ejemplo n.º 8
0
void 
ConvertToDrawInstanced::apply( osg::Geode& geode )
{
    for( unsigned d=0; d<geode.getNumDrawables(); ++d )
    {
        osg::Geometry* geom = geode.getDrawable(d)->asGeometry();
        if ( geom )
        {
            if ( _optimize )
            {
                // activate VBOs
                geom->setUseDisplayList( false );
                geom->setUseVertexBufferObjects( true );
            }

            geom->setInitialBound(_bbox);

            // convert to use DrawInstanced
            for( unsigned p=0; p<geom->getNumPrimitiveSets(); ++p )
            {
                osg::PrimitiveSet* ps = geom->getPrimitiveSet(p);
                ps->setNumInstances( _numInstances );
                _primitiveSets.push_back( ps );
            }

#ifdef USE_INSTANCE_LODS
            geom->setDrawCallback( new LODCallback() );
#endif
        }
    }

    traverse(geode);
}
Ejemplo n.º 9
0
void CURRENT_CLASS::apply(osg::Geode &geode)
{
    // Contained drawables will only be individually considered if we are
    // allowed to continue traversing.
    if(shouldContinueTraversal(geode))
    {
        osg::Drawable *drawable;
        double zNear, zFar;

        // Handle each drawable in this geode
        for(unsigned int i = 0; i < geode.getNumDrawables(); ++i)
        {
            drawable = geode.getDrawable(i);

            const osg::BoundingBox &bb = drawable->getBound();
            if(bb.valid())
            {
                bool containsTest = _localFrusta.back().contains(bb);
                // Make sure drawable will be visible in the scene
                if(!containsTest)
                    continue;

                // Compute near/far distances for current drawable
                zNear = distanceDA(bb.corner(_bbCorners.back().first),
                        _viewMatrices.back());
                zFar = distanceDA(bb.corner(_bbCorners.back().second),
                        _viewMatrices.back());
                if(zNear > zFar)
                    std::swap(zNear,zFar);
                pushDistancePair(zNear,zFar);
            }
        }
    }
}
Ejemplo n.º 10
0
void 
ShaderGenerator::apply( osg::Geode& geode )
{
    osg::ref_ptr<osg::StateSet> ss = geode.getStateSet();
    if ( ss.valid() )
    {
        _state->pushStateSet( ss.get() );

        osg::ref_ptr<osg::StateSet> replacement;
        if ( processGeometry(ss.get(), replacement) )
        {
            _state->popStateSet();
            geode.setStateSet( replacement.get() );
            _state->pushStateSet( replacement.get() );
        }
    }

    for( unsigned d = 0; d < geode.getNumDrawables(); ++d )
    {
        apply( geode.getDrawable(d) );
    }

    if ( ss.valid() )
    {
        _state->popStateSet();
    }
}
Ejemplo n.º 11
0
void LODVisitor::apply( osg::Geode& node )
{
    unsigned int numDrawables = node.getNumDrawables();
    for ( unsigned int cnt = 0; cnt < numDrawables; ++cnt )
    {
        osg::Drawable* p_drawable = node.getDrawable( cnt );
        osg::Geometry* p_geom     = p_drawable->asGeometry();
        // evaluate the geom and generate an appropriate collision geometry
        if ( p_geom )
        {
            p_drawable->setUseDisplayList( false );
            p_drawable->setSupportsDisplayList( false );

            osg::ref_ptr< GLODMesh > p_lodmesh = new GLODMesh( p_geom );
            p_lodmesh->createObject( *( _p_lodSettings.get() ) );
            // the actual lod mesh object is stored in geometry object, the following update and draw callbacks hold references to it
            p_geom->setUserData( p_lodmesh.get() );

            LODDrawCallback* p_cbdraw = new LODDrawCallback( p_lodmesh.get() );
            p_geom->setDrawCallback( p_cbdraw );

            //! TODO get the adaptation period from lod settings!
            LODUpdateCallback* p_cbupdate = new LODUpdateCallback( p_lodmesh.get(), 1.0 );
            p_geom->setUpdateCallback( p_cbupdate );
        }
    }
}
Ejemplo n.º 12
0
 virtual void apply(osg::Geode &n) {
     unsigned int nDraws = n.getNumDrawables();
     for (unsigned int idraw = 0 ; idraw < nDraws; ++idraw) {
         osg::Geometry *geom = n.getDrawable( idraw )->asGeometry();
         osg::Vec3Array *v = static_cast<osg::Vec3Array*>(geom->getVertexArray());
         osg::Vec4Array *vc = static_cast<osg::Vec4Array*>(geom->getColorArray());
         osg::Vec3Array *vn = static_cast<osg::Vec3Array*>(geom->getNormalArray());
         osg::Vec2Array *vt = static_cast<osg::Vec2Array*>(geom->getTexCoordArray(0));
         
         if (geom) {
             unsigned int nPrims = geom->getNumPrimitiveSets();
             for (unsigned int iprim = 0; iprim < nPrims; ++iprim) {
                 osg::PrimitiveSet *p = geom->getPrimitiveSet(iprim);
                 
                 for (GLsizei i = 0; i < p->getNumIndices(); ++i) {
                     _s.vertexPositions.col(_idx) = toE(v->at(p->index(i)));
                     if (_opts & ConvertVertexColors)
                         _s.vertexColors.col(_idx) = toE(vc->at(p->index(i)));
                     if (_opts & ConvertVertexNormals)
                         _s.vertexNormals.col(_idx) = toE(vn->at(p->index(i))).normalized();
                     if (_opts & ConvertVertexUVs) {
                         _s.vertexUVs.col(_idx) = toE(vt->at(p->index(i)));
                     }
                     ++_idx;
                 }
             }
         }
     }
     traverse(n);
 }
Ejemplo n.º 13
0
	virtual void apply(osg::Geode& node)
	{
		for (int i=0;i<_indent;++i)
		{
			std::cout<<"  ";
		}
		std::cout<<"["<<_indent+1<<"]"<<node.libraryName()
			<<"::"<<node.className()<<std::endl;
		for (unsigned int n=0;n<node.getNumDrawables();++n)
		{
			osg::Drawable *drawable=node.getDrawable(n);
			if(!drawable)continue;
			for (int i=0;i<_indent;++i)
			{
				for (int i=0;i<_indent;++i)
				{
					std::cout<<"  ";
				}
				std::cout<<drawable->libraryName()<<"::"
					<<drawable->className()<<std::endl;
			}
			_indent++;
			traverse(node);
			_indent--;
		}
	
	}
Ejemplo n.º 14
0
void
GeometryModifier::apply( osg::Geode& geode )
{
    // merge drawables if possible for best results
    if (getDrawableMerge())
    {
        osgUtil::Optimizer::MergeGeometryVisitor mgv;
        mgv.setTargetMaximumNumberOfVertices(1000000);
        mgv.mergeGeode(geode);
    }

    for(unsigned int i=0;i<geode.getNumDrawables();++i)
    {
        _drawableCount++;
        osg::ref_ptr< osg::Geometry > geometry = geode.getDrawable(i)->asGeometry();
        if( geometry.valid() )
        {
            _geometryCount++;
            if( geometry->containsSharedArrays() )
                osg::notify( osg::DEBUG_INFO ) << "Warning! Geometry contains shared arrays" << std::endl;

            // Get statistics before
            incStatistics( geometry.get(), _preVertices, _preIndices, _preTriangles );

            osg::ref_ptr< osg::Geometry > newGeom = (*_geomOp)( *geometry );
            geode.replaceDrawable( geometry.get(), newGeom.get() );

            // Get statistics after
            incStatistics( newGeom.get(), _postVertices, _postIndices, _postTriangles );
        }
    }
}
Ejemplo n.º 15
0
void GeometryDataCollector::apply( osg::Geode& node )
{
    //osg::Matrix matrix = osg::computeLocalToWorld( node.getParentalNodePaths()[0] );
    osg::Matrix matrix;
    if ( matrixStack.size()>0 ) matrix = matrixStack.back();
    for ( unsigned int i=0; i<node.getNumDrawables(); ++i )
    {
        osg::Geometry* geom = node.getDrawable(i)->asGeometry();
        if ( geom )
        {
            osg::Vec3Array* va = dynamic_cast<osg::Vec3Array*>( geom->getVertexArray() );
            numTotalVertices += (va ? va->size() : 0);
            
            osg::TriangleFunctor<CollectFaceOperator> functor;
            functor.collector = this;
            functor.matrix = matrix;
            geom->accept( functor );
        }
        else
        {
            osg::TriangleFunctor<CollectFaceOperator> functor;
            functor.collector = this;
            functor.matrix = matrix;
            node.getDrawable(i)->accept( functor );
        }
    }
    traverse( node );
}
Ejemplo n.º 16
0
void osgToy::FacetingVisitor::apply(osg::Geode& geode)
{
    for(unsigned int i = 0; i < geode.getNumDrawables(); i++ )
    {
        osg::Geometry* geom = dynamic_cast<osg::Geometry*>(geode.getDrawable(i));
        if (geom) facet(*geom);
    }
}
Ejemplo n.º 17
0
 void apply(osg::Geode& node)
 {
     apply((osg::Node&)node);
     for(unsigned int i=0;i<node.getNumDrawables();++i)
     {
         osg::Drawable* drawable = node.getDrawable(i);
         if (drawable->getStateSet()) process(drawable->getStateSet());
     }
 }
Ejemplo n.º 18
0
 void DisableFreezeOnCullVisitor::apply(osg::Geode &geode)
 {
     for (unsigned int i=0; i<geode.getNumDrawables(); ++i)
     {
         osg::Drawable* drw = geode.getDrawable(i);
         if (osgParticle::ParticleSystem* partsys = dynamic_cast<osgParticle::ParticleSystem*>(drw))
             partsys->setFreezeOnCull(false);
     }
 }
void PhysicsVisitor::apply( osg::Geode& node )
{
    // retrieve the node mask which is used for physics material and later for other properies
    // only the first byte is relevant for pyhsics material description
    _attribute = node.getNodeMask() & 0xFF;

    // this means no need for building static collision geom
    //  this is not the same as MAT_NOCOL, as MAT_NOCOL collisions are detected
    if ( _attribute == Physics::NO_BUILD )
        return;

    // get the accumulated world matrix for this node
    osg::Matrixf  mat = computeLocalToWorld( getNodePath() );
    unsigned int numDrawables = node.getNumDrawables();
    for ( unsigned int cnt = 0; cnt < numDrawables; ++cnt )
    {
        osg::Drawable* p_drawable = node.getDrawable( cnt );
        osg::Geometry* p_geom     = p_drawable->asGeometry();
        // evaluate the geom and generate an appropriate collision geometry
        if ( p_geom )
        {
            osg::Array*      p_verts   = p_geom->getVertexArray();
            osg::IndexArray* p_indices = p_geom->getVertexIndices();
            unsigned int     numPrims  = p_geom->getNumPrimitiveSets();
            {
                for ( unsigned int primcnt = 0; primcnt < numPrims; ++primcnt )
                {
                    osg::PrimitiveSet* p_set = p_geom->getPrimitiveSet( primcnt );
                    switch( p_set->getMode() )
                    {
                        case osg::PrimitiveSet::POINTS:
                        case osg::PrimitiveSet::LINES:
                        case osg::PrimitiveSet::LINE_STRIP:
                        case osg::PrimitiveSet::LINE_LOOP:
                            return;

                        case osg::PrimitiveSet::TRIANGLES:
                            buildTrianlges( p_set, p_verts, mat, p_indices );
                            break;

                        case osg::PrimitiveSet::TRIANGLE_STRIP:
                            buildTrianlgeStrip( p_set, p_verts, mat, p_indices );
                            break;

                        case osg::PrimitiveSet::TRIANGLE_FAN:
                        case osg::PrimitiveSet::QUADS:
                        case osg::PrimitiveSet::QUAD_STRIP:
                        case osg::PrimitiveSet::POLYGON:

                        default:
                            log_error << "Physics Serializer: unsupported primitive set for physics geometry! currently only TRIANGLES and TRIANGLE_STRIP types are supported." << std::endl;
                    }
                }
            }
        }
    }
}
Ejemplo n.º 20
0
 virtual void apply( osg::Geode& geode )
 {
     for( unsigned int i=0; i<geode.getNumDrawables(); i++ )
     {
         osg::StateSet* ss = geode.getDrawable( i )->getStateSet();
         if ( ss ) rewrite( ss );
         osg::NodeVisitor::apply( geode );
     }
 }
Ejemplo n.º 21
0
 virtual void apply(osg::Geode& geode)
 {
     replaceTexure(geode.getStateSet());
     for (unsigned int i = 0; i < geode.getNumDrawables(); ++i)
     {
         replaceTexure(geode.getDrawable(i)->getStateSet());
     }
     traverse(geode);
 }
Ejemplo n.º 22
0
Archivo: osgconv.cpp Proyecto: 3dcl/osg
    virtual void apply(osg::Geode& node)
    {
        if (node.getStateSet())
        {
            node.setStateSet(0);
            ++_numStateSetRemoved;
        }

        traverse(node);
    }
Ejemplo n.º 23
0
void
GeometryValidator::apply(osg::Geode& geode)
{
    for(unsigned i=0; i<geode.getNumDrawables(); ++i)
    {
        osg::Geometry* geom = geode.getDrawable(i)->asGeometry();
        if ( geom )
            apply( *geom );
    }
}
 virtual void apply(osg::Geode& geode)
 {
     apply(geode.getStateSet());
     for(unsigned int i=0; i<geode.getNumDrawables(); ++i)
     {
         apply(geode.getDrawable(i)->getStateSet());
         osg::Geometry* geometry = geode.getDrawable(i)->asGeometry();
         if (geometry) apply(geometry);
     }
 }
Ejemplo n.º 25
0
    void apply(osg::Geode& geode)
    {
        apply(static_cast<osg::Node&>(geode));

        for(unsigned int i=0;i<geode.getNumDrawables();++i)
        {
            osg::Drawable* drawable = geode.getDrawable(i);
            if (drawable->getStateSet()) process(drawable->getStateSet());
        }
    }
Ejemplo n.º 26
0
void LinkVisitor::apply(osg::Geode& node)
{
    for (unsigned int i = 0; i < node.getNumDrawables(); i++)
    {
        osg::Drawable* drawable = node.getDrawable(i);
        if (drawable && drawable->getStateSet())
            handle_stateset(drawable->getStateSet());
    }
    apply(static_cast<osg::Node&>(node));
}
Ejemplo n.º 27
0
void AddQueries::apply( osg::Geode& node )
{
    traverse( node );

    osgwTools::CountsVisitor cv;
    node.accept( cv );
    const unsigned int numVertices = cv.getVertices();
    const osg::BoundingBox& bb = node.getBoundingBox();
    addDataToNodePath( getNodePath(), numVertices, bb );
}
Ejemplo n.º 28
0
static bool writeDrawables( osgDB::OutputStream& os, const osg::Geode& node )
{
    unsigned int size = node.getNumDrawables();
    os << size << os.BEGIN_BRACKET << std::endl;
    for ( unsigned int i=0; i<size; ++i )
    {
        os << node.getDrawable(i);
    }
    os << os.END_BRACKET << std::endl;
    return true;
}
Ejemplo n.º 29
0
void TexCoordVisitor::apply(osg::Geode& geode)
{
    for(unsigned int i = 0; i < geode.getNumDrawables(); i++ )
    {
    	osg::Geometry* geom = dynamic_cast<osg::Geometry*>( geode.getDrawable(i) );
      if ( geom )
      {
      	// To do
      }
    }
}
Ejemplo n.º 30
0
 virtual void apply(osg::Geode& geode)
 {
     for(unsigned int i=0; i<geode.getNumDrawables(); ++i)
     {
         osg::Geometry* geometry = dynamic_cast<osg::Geometry*>(geode.getDrawable(i));
         if (geometry)
         {
             geometry->setUseVertexBufferObjects(true);
         }
     }
 }