void POVWriterNodeVisitor::apply( Geode& node )
{
   pushStateSet( node.getStateSet() );

   // iterate through drawables
   for(unsigned int i=0; i<node.getNumDrawables(); ++i)
   {
      // get drawable
      const Drawable *d = node.getDrawable(i);
      if (!d) continue;

      // push state set
      const StateSet *ss = d->getStateSet();
      if( ss )  pushStateSet( ss );

      // transformation matrix
      Matrix m = _transformationStack.top();

      // process lights
      processLights( _stateSetStack.top().get(), m );

      // process geometry
      const Geometry *g = d->asGeometry();
      if( g )
         processGeometry( g, _stateSetStack.top().get(), m );

      // pop state set
      if( ss )  popStateSet( ss );
   }

   popStateSet( node.getStateSet() );
}
Пример #2
0
  void BaseDotVisitor::apply(Geode& node) {
    int id;
    if ( getOrCreateId( &node, id ) ) {
      handle( node, id );
      handleNodeAndTraverse( node, id );

      unsigned int i;
      for ( i = 0; i < node.getNumDrawables(); i++ ) {
        osg::Drawable* drawable = node.getDrawable( i );
        int id2;
        if ( getOrCreateId( drawable, id2 ) ) {
          handle( *drawable, id2 );
          osg::StateSet* s = drawable->getStateSet();
          if ( s ) {
            int id3;
            if ( getOrCreateId( s, id3 ) ) {
              handle( *s, id3 );
            }
            handle( *drawable, *s, id2, id3 );
          }
        }
        handle( node, *drawable, id, id2 );
      }

    }

  }
void ShadowVolumeGeometryGenerator::apply( Geode& geode )
{
   StateSet *ss = geode.getStateSet();
   if(ss)
      setCurrentFacingAndOrdering(ss);

   if( geode.getStateSet() )
      pushState( geode.getStateSet() );

   for( unsigned int i=0; i<geode.getNumDrawables(); ++i )
   {
      Drawable* drawable = geode.getDrawable( i );

      if( drawable->getStateSet() )
            pushState( drawable->getStateSet() );

      apply( geode.getDrawable( i ) );

      if( drawable->getStateSet() )
         popState(drawable->getStateSet());
   }

   if(ss)
      setCurrentFacingAndOrdering(ss);
   if( geode.getStateSet() )
      popState(geode);
}
Пример #4
0
void TriStripVisitor::apply(Geode& geode)
{
    for(unsigned int i = 0; i < geode.getNumDrawables(); ++i )
    {
        osg::Geometry* geom = dynamic_cast<osg::Geometry*>(geode.getDrawable(i));
        if (geom) _geometryList.insert(geom);
    }
}
Пример #5
0
/** The 'apply' method for 'Geode' type instances
 * @param searchNode : Geode : node that is searched
 */
void lgNodeOverseer::apply(Geode &searchNode)
{
    cout << spaces() << "Géode :"<< searchNode.getName() << ", " << searchNode.getNumDrawables() << " drawables" << endl;
    if(searchNode.getNumDrawables()>0 && showDrawable){
        for (unsigned i=0;i<searchNode.getNumDrawables();i++)
        {
            _level++;
            cout << spaces() << "Drawable " << i << " : " << searchNode.getDrawable(i)->getName() << endl;
            ref_ptr<Geometry> myGeom = dynamic_cast<Geometry*>(searchNode.getDrawable(i));
            if(myGeom)
            {
                ref_ptr<Vec3Array> arrayVertex = (Vec3Array*) myGeom->getVertexArray();
                _level++;
                int size = arrayVertex->size();
                cout << spaces() << "Il y a " << size << " sommets" << endl;
                for (int j=0; j<size;j++)
                {
                    cout << spaces() << "Sommet " << j+1 << " : x=" << arrayVertex->at(j).x() <<
                            ", y=" << arrayVertex->at(j).y() << ", z=" << arrayVertex->at(j).z() << endl;
                }
                _level--;
            }
            else
            {
                cout << "Pas de géométrie" << endl;
            }
            _level--;
        }
        cout << spaces() << endl;
    }
    // If no node is found, return searchNode
    if (searchForName == "")
    {
        foundNodeList.push_back(&searchNode);
    }
    else
    {
        if (searchNode.getName() == searchForName)
        {
            foundNodeList.push_back(&searchNode);
        }
        _level++;
        traverse(searchNode);
        _level--;
    }
}
Пример #6
0
void IntersectVisitor::apply(Geode& geode)
{
    if (!enterNode(geode)) return;

    for(unsigned int i = 0; i < geode.getNumDrawables(); i++ )
    {
        intersect(*geode.getDrawable(i));
    }

    leaveNode();
}
Пример #7
0
void StateSetVisitor::apply( Geode& geode )
{
   StateSet *ss = geode.getStateSet();
   if( ss )
      apply( *ss );

   for( unsigned int i=0; i<geode.getNumDrawables(); ++i )
      apply( *geode.getDrawable( i ) );

   traverse( geode );
}
void DisplayRequirementsVisitor::apply(Geode& geode)
{
    osg::StateSet* geode_stateset = geode.getStateSet();
    if (geode_stateset) applyStateSet(*geode_stateset);
    
    for(unsigned int i = 0; i < geode.getNumDrawables(); i++ )
    {
        osg::StateSet* stateset = geode.getDrawable(i)->getStateSet();
        if (stateset) applyStateSet(*stateset);
    }
}
unsigned int Object3D::getNumDrawables() const
{
    GeodeFindVisitor visitor;
    _originalNode->accept(visitor);
    Geode *geode = visitor.getFirst();
    if(geode)
    {
        return geode->getNumDrawables();
    }
    return 0;
}
void Object3D::dirty()
{
    GeodeFindVisitor visitor;
    _originalNode->accept(visitor);
    std::vector<Geode*> geodeList = visitor.getGeodeList();
    std::vector<Geode*>::iterator it;
    for(it=geodeList.begin(); it!=geodeList.end(); ++it)
    {
        Geode *geode = *it;
        if(geode)
        {
            for(unsigned int i=0; i<geode->getNumDrawables(); i++)
            {
                Drawable *drawable = geode->getDrawable(i);
                drawable->dirtyBound();
                Geometry* geom(drawable->asGeometry());
                osgUtil::SmoothingVisitor sv;
                sv.smooth(*geom);
                geom->getNormalArray()->dirty();
            }
        }
    }
}
Пример #11
0
void ScreenMVCullVisitor::apply(Geode& node)
{
    bool status = _cullingStatus;
    bool firstStatus = _firstCullStatus;
    if(isCulled(node))
    {
        _firstCullStatus = firstStatus;
        _cullingStatus = status;
        return;
    }

    // push the node's state.
    StateSet* node_state = node.getStateSet();
    if(node_state)
        pushStateSet(node_state);

    // traverse any call callbacks and traverse any children.
    handle_cull_callbacks_and_traverse(node);

    RefMatrix& matrix = *getModelViewMatrix();
    for(unsigned int i = 0; i < node.getNumDrawables(); ++i)
    {
        Drawable* drawable = node.getDrawable(i);
        const BoundingBox &bb = drawable->getBound();

        if(drawable->getCullCallback())
        {
            if(drawable->getCullCallback()->cull(this,drawable,&_renderInfo)
                    == true)
                continue;
        }

        //else
        {
            if(node.isCullingActive() && isCulled(bb))
                continue;
        }

        if(_computeNearFar && bb.valid())
        {
            if(!updateCalculatedNearFar(matrix,*drawable,false))
                continue;
        }

        // need to track how push/pops there are, so we can unravel the stack correctly.
        unsigned int numPopStateSetRequired = 0;

        // push the geoset's state on the geostate stack.    
        StateSet* stateset = drawable->getStateSet();
        if(stateset)
        {
            ++numPopStateSetRequired;
            pushStateSet(stateset);
        }

        CullingSet& cs = getCurrentCullingSet();
        if(!cs.getStateFrustumList().empty())
        {
            osg::CullingSet::StateFrustumList& sfl = cs.getStateFrustumList();
            for(osg::CullingSet::StateFrustumList::iterator itr = sfl.begin();
                    itr != sfl.end(); ++itr)
            {
                if(itr->second.contains(bb))
                {
                    ++numPopStateSetRequired;
                    pushStateSet(itr->first.get());
                }
            }
        }

        float depth = bb.valid() ? distance(bb.center(),matrix) : 0.0f;

        if(osg::isNaN(depth))
        {
            /*OSG_NOTIFY(osg::NOTICE)<<"CullVisitor::apply(Geode&) detected NaN,"<<std::endl
             <<"    depth="<<depth<<", center=("<<bb.center()<<"),"<<std::endl
             <<"    matrix="<<matrix<<std::endl;
             OSG_NOTIFY(osg::DEBUG_INFO) << "    NodePath:" << std::endl;
             for (NodePath::const_iterator i = getNodePath().begin(); i != getNodePath().end(); ++i)
             {
             OSG_NOTIFY(osg::DEBUG_INFO) << "        \"" << (*i)->getName() << "\"" << std::endl;
             }*/
        }
        else
        {
            addDrawableAndDepth(drawable,&matrix,depth);
        }

        for(unsigned int i = 0; i < numPopStateSetRequired; ++i)
        {
            popStateSet();
        }

    }

    // pop the node's state off the geostate stack.    
    if(node_state)
        popStateSet();

    _firstCullStatus = firstStatus;
    _cullingStatus = status;
}
Пример #12
0
void Normals::MakeNormalsVisitor::apply( Geode &geode )
{
    for( unsigned int i = 0; i < geode.getNumDrawables(); i++ )
    {
        Geometry *geom = dynamic_cast<Geometry *>(geode.getDrawable(i));
        if( geom )
        {
            if (geom->containsDeprecatedData()) geom->fixDeprecatedData();
            
            Vec3Array *coords   = dynamic_cast<Vec3Array*>(geom->getVertexArray());
            if( coords == 0L )
                continue;

            Vec3Array *normals  = dynamic_cast<Vec3Array*>(geom->getNormalArray());
            if( normals == 0L )
                continue;

            Geometry::AttributeBinding binding = geom->getNormalBinding();
            if( binding == Geometry::BIND_OFF )
                continue;

            if( binding == Geometry::BIND_OVERALL )
            {
                Vec3 v(0,0,0);
                Vec3 n = normals->front();

                Vec3Array::iterator coord_index = coords->begin();
                while( coord_index != coords->end() )
                  v += *(coord_index++) * _mat;
                v /= (float)(coords->size());

                n *= _normal_scale;
                _local_coords->push_back( v );
                _local_coords->push_back( (v + n));
            }
            else // BIND_PER_PRIMITIVE_SET, BIND_PER_VERTEX
            {
                Geometry::PrimitiveSetList& primitiveSets = geom->getPrimitiveSetList();
                Geometry::PrimitiveSetList::iterator itr;

                Vec3Array::iterator coord_index   = coords->begin();
                Vec3Array::iterator normals_index = normals->begin();

                for(itr=primitiveSets.begin(); itr!=primitiveSets.end(); ++itr)
                {
#ifdef DEBUG
                    _printPrimitiveType( (*itr).get() );
#endif
                    if( binding == Geometry::BIND_PER_PRIMITIVE_SET )
                    {
                        Vec3 v(0,0,0);
                        Vec3 n = *(normals_index++);
                        int ni = (*itr)->getNumIndices();
                        for( int i = 0; i < ni; i++ )
                            v += *(coord_index++) * _mat;
                        v /= (float)(ni);

                        n *= _normal_scale;
                        _local_coords->push_back( v );
                        _local_coords->push_back( (v + n));
                    }
                    else
                    {
                        switch((*itr)->getMode())
                        {
                            case(PrimitiveSet::TRIANGLES):
                            {
                                for( unsigned int j = 0; j < (*itr)->getNumPrimitives(); j++ )
                                {
                                    _processPrimitive( 3, coord_index, normals_index, binding );
                                    coord_index += 3;
                                    normals_index+=3;
                                }
                                break;
                            }
                            case(PrimitiveSet::TRIANGLE_STRIP):
                            {
                                for( unsigned int j = 0; j < (*itr)->getNumIndices()-2; j++ )
                                {
                                    _processPrimitive( 3, coord_index, normals_index, binding );
                                    coord_index++;
                                    normals_index++;
                                }
                                coord_index += 2;
                                if( binding == Geometry::BIND_PER_VERTEX )
                                    normals_index += 2;
                                break;
                            }
                            case(PrimitiveSet::TRIANGLE_FAN):
                                break;

                            case(PrimitiveSet::QUADS):
                            {
                                for( unsigned int j = 0; j < (*itr)->getNumPrimitives(); j++ )
                                {
                                    _processPrimitive( 4, coord_index, normals_index, binding );
                                    coord_index += 4;
                                    normals_index +=4;
                                }
                                break;
                            }
                            case(PrimitiveSet::QUAD_STRIP):
                                break;

                            case(PrimitiveSet::POLYGON):
                            {
                                DrawArrayLengths* dal = dynamic_cast<DrawArrayLengths*>((*itr).get());
                                if (dal) {
                                    for (unsigned int j = 0; j < dal->size(); ++j) {
                                        unsigned int num_prim = (*dal)[j];
                                        //OSG_WARN << "j=" << j << " num_prim=" << num_prim << std::endl;
                                        _processPrimitive(num_prim, coord_index, normals_index, binding);
                                        coord_index += num_prim;
                                        normals_index += num_prim;
                                    }
                                }
                                break;
                            }

                            default:
                                break;
                        }
                    }
                }
            }
        }
    }
    traverse( geode );
}
Пример #13
0
void
Lwo2Layer::GenerateGeode( Geode& geode, short tags_count, DrawableToTagMapping& tag_mapping)
{
  OSG_DEBUG;

  // variable used to track using textures
  bool have_texture_coords;

  // create diffirent geomerty for each tag
  for (short current_tag = 0; current_tag < tags_count; current_tag++)
    {
      have_texture_coords = false;

      // new geometry
      ref_ptr<Geometry> geometry = new Geometry;

      // create coords array
      ref_ptr<Vec3Array> coords = new Vec3Array;

      // create texture array
      ref_ptr<Vec2Array> texcoords = new Vec2Array;

      // selecting polygons for current layer only
      int polygon_index = 0;
      PolygonsList polygons;
      IteratorPolygonsList polygon_iterator;
      for (polygon_iterator = _polygons.begin(); polygon_iterator != _polygons.end(); polygon_iterator++, polygon_index++)
        {
          // *polygon_iterator it's a PolygonsList

          // polygons of current tag only
          if (_polygons_tag[polygon_index] == current_tag)
            {

              // reset point_index member for later comparing poins data
              PointsList points_list = *polygon_iterator;
              for (unsigned int i = 0; i < points_list.size(); i++)
                {
                  points_list[i].point_index = 0;
                }

              polygons.push_back(*polygon_iterator);
            }
        }

      // find and compose triangle fans
      PolygonsList triangle_fans;
      _find_triangle_fans(polygons, triangle_fans);

      // find and compose triangle strips
      PolygonsList triangle_strips;
      _find_triangle_strips(polygons, triangle_strips);

      // polygons of current layer
      polygon_index = 0;
      for (polygon_iterator = polygons.begin(); polygon_iterator != polygons.end(); polygon_iterator++, polygon_index++)
        {
          if ((*polygon_iterator)[0].point_index != -1)
            {
              // all points of polygon
              for (IteratorPoint itr = (*polygon_iterator).begin(); itr != (*polygon_iterator).end(); itr++)
                {
                  // *itr - it's a PointData

                  // polygons data
                  (*coords).push_back((*itr).coord);
                  (*texcoords).push_back((*itr).texcoord);

                  if ((*itr).texcoord.x() != -1.0f || (*itr).texcoord.y() != -1.0f)
                    {
                      have_texture_coords = true;
                    }
                }

              unsigned int points_start = (*coords).size() - (*polygon_iterator).size();
              unsigned int points_count = (*polygon_iterator).size();
              if (points_count == 3)
                {
                  geometry->addPrimitiveSet(new DrawArrays(PrimitiveSet::TRIANGLES, points_start, points_count));
                }
              else if (points_count == 4)
                {
                  geometry->addPrimitiveSet(new DrawArrays(PrimitiveSet::QUADS, points_start, points_count));
                }
              else
                {
                  geometry->addPrimitiveSet(new DrawArrays(PrimitiveSet::POLYGON, points_start, points_count));
                }
            }
        }

      // triangle fans of current layer
      polygon_index = 0;
      for (polygon_iterator = triangle_fans.begin(); polygon_iterator != triangle_fans.end(); polygon_iterator++, polygon_index++)
        {

          // all points of polygon
          for (IteratorPoint itr = (*polygon_iterator).begin(); itr != (*polygon_iterator).end(); itr++)
            {
              // *itr - it's a PointData

              // polygons data
              (*coords).push_back((*itr).coord);
              (*texcoords).push_back((*itr).texcoord);

              if ((*itr).texcoord.x() != -1.0f || (*itr).texcoord.y() != -1.0f)
                {
                  have_texture_coords = true;
                }
            }

          unsigned int points_start = (*coords).size() - (*polygon_iterator).size();
          unsigned int points_count = (*polygon_iterator).size();
          geometry->addPrimitiveSet(new DrawArrays(PrimitiveSet::TRIANGLE_FAN, points_start, points_count));
        }

      // triangle strips of current layer
      polygon_index = 0;
      for (polygon_iterator = triangle_strips.begin(); polygon_iterator != triangle_strips.end(); polygon_iterator++, polygon_index++)
        {

          // all points of polygon
          for (IteratorPoint itr = (*polygon_iterator).begin(); itr != (*polygon_iterator).end(); itr++)
            {
              // *itr - it's a PointData

              // polygons data
              (*coords).push_back((*itr).coord);
              (*texcoords).push_back((*itr).texcoord);

              if ((*itr).texcoord.x() != -1.0f || (*itr).texcoord.y() != -1.0f)
                {
                  have_texture_coords = true;
                }
            }

          unsigned int points_start = (*coords).size() - (*polygon_iterator).size();
          unsigned int points_count = (*polygon_iterator).size();
          geometry->addPrimitiveSet(new DrawArrays(PrimitiveSet::TRIANGLE_STRIP, points_start, points_count));
        }

      // add geometry if it contains any points
      if (coords->size() != 0)
        {
          geometry->setVertexArray(coords.get());

          // assign texture array
          if (have_texture_coords)
            {
              geometry->setTexCoordArray(0, texcoords.get());
            }

          // generate normals
          osgUtil::SmoothingVisitor smoother;
          smoother.smooth(*(geometry.get()));

          geode.addDrawable(geometry.get());

          OSG_DEBUG << "  inserting tag " << geode.getNumDrawables() - 1 << ":" << current_tag << std::endl;
          tag_mapping.insert(PairDrawableToTag(geode.getNumDrawables() - 1, current_tag));
        }
    }
}
Пример #14
0
void osgToy::Normals::MakeNormalsVisitor::apply( Geode &geode )
{
    for( unsigned int i = 0; i < geode.getNumDrawables(); i++ )
    {
        osg::Geometry *geom = dynamic_cast<osg::Geometry *>(geode.getDrawable(i));
        if( geom )
        {
            osg::Vec3Array *coords   = dynamic_cast<osg::Vec3Array*>(geom->getVertexArray());
            if( coords == 0L )
                continue;

            osg::Vec3Array *normals  = dynamic_cast<osg::Vec3Array*>(geom->getNormalArray());
            if( normals == 0L )
                continue;

            osg::Geometry::AttributeBinding binding = geom->getNormalBinding();
            if( binding == osg::Geometry::BIND_OFF )
                continue;

            if( binding == osg::Geometry::BIND_OVERALL )
            {
                osg::Vec3 v(0,0,0);
                osg::Vec3 n = normals->front();

                osg::Vec3Array::iterator coord_index = coords->begin();
                while( coord_index != coords->end() )
                  v += *(coord_index++);
                v /= (float)(coords->size()); 

                n *= _normal_scale;
                _local_coords->push_back( v );
                _local_coords->push_back( (v + n));
            }
            else // BIND_PER_PRIMTIVE_SET, BIND_PER_PRIMTITIV, BIND_PER_VERTEX
            {
                osg::Geometry::PrimitiveSetList& primitiveSets = geom->getPrimitiveSetList();
                osg::Geometry::PrimitiveSetList::iterator itr;

                osg::Vec3Array::iterator coord_index   = coords->begin();
                osg::Vec3Array::iterator normals_index = normals->begin();

                for(itr=primitiveSets.begin(); itr!=primitiveSets.end(); ++itr)
                {
                    if( binding == osg::Geometry::BIND_PER_PRIMITIVE_SET )
                    {
                        osg::Vec3 v(0,0,0);
                        osg::Vec3 n = *(normals_index++); 
                        int ni = (*itr)->getNumIndices();
                        for( int i = 0; i < ni; i++ )
                            v += *(coord_index++);
                        v /= (float)(ni);

                        n *= _normal_scale;
                        _local_coords->push_back( v );
                        _local_coords->push_back( (v + n));

                    }
                    else 
                    {
                        switch((*itr)->getMode())
                        {
                            case(osg::PrimitiveSet::TRIANGLES):
                                for( unsigned int j = 0; j < (*itr)->getNumPrimitives(); j++ )
                                {
                                    _processPrimitive( 3, coord_index, normals_index, binding );
                                    coord_index += 3;
                                    if( binding == osg::Geometry::BIND_PER_PRIMITIVE )
                                        normals_index++;
                                    else 
                                        normals_index+=3;
                                    
                                }
                                break;

                            case(osg::PrimitiveSet::TRIANGLE_STRIP):
                                for( unsigned int j = 0; j < (*itr)->getNumIndices()-2; j++ )
                                {
                                    _processPrimitive( 3, coord_index, normals_index, binding );
                                    coord_index++;
                                    normals_index++;
                                }
                                coord_index += 2;
                                if( binding == osg::Geometry::BIND_PER_VERTEX )
                                    normals_index += 2;
                                break;

                            case(osg::PrimitiveSet::TRIANGLE_FAN):
                                break;

                            case(osg::PrimitiveSet::QUADS):
                                for( unsigned int j = 0; j < (*itr)->getNumPrimitives(); j++ )
                                {
                                    _processPrimitive( 4, coord_index, normals_index, binding );
                                    coord_index += 4;
                                    if( binding == osg::Geometry::BIND_PER_PRIMITIVE )
                                        normals_index++;
                                    else 
                                        normals_index+=4;
                                }
                                break;

                            case(osg::PrimitiveSet::QUAD_STRIP):
                            case(osg::PrimitiveSet::POLYGON):
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        }
    }
    traverse( geode );
}