Hello again,

Sorry, I didn't see the rest of your answer first.
I send you my test program that will fetch the triangles in the
second call of display (first run gets no triangles) [1]. The second
program is to big and at this time closed source, but I append the
significant lines of code. 

####### this is the code that inserts "child" (the surface) into scene
####### graph "scene". After all the barriers the OSGTriangleCB [2] is
####### called. [2] fetches the triangle of node (with or without
####### normals)


    std::cout << "adding child" << std::endl;

    //child gets index 2
    //use subChild to remove 
    beginEditCP(scene, osg::Node::ChildrenFieldMask );
    {
      if (scene->getNChildren() == 3) {
        scene->replaceChild(2, child);
      } else {
        scene->addChild(child);
      }
    }
    endEditCP  (scene, osg::Node::ChildrenFieldMask );

    ::osg::Barrier* mvbarrier
        = osg::ThreadManager::the()->getBarrier("motionvolume");
    
    std::cout << "addChild wait for barrier 1" << std::endl;
    mvbarrier->enter(2);
    std::cout << "addChild gone through barrier 1" << std::endl;
    std::cout << "addChild wait for barrier 2" << std::endl;
    mvbarrier->enter(2);
    std::cout << "addChild gone through barrier 2" << std::endl;

    std::cout << "addChild sync change list" << std::endl;
    osg::Thread* applicationThread 
      = dynamic_cast< osg::Thread*
    >(osg::ThreadManager::getAppThread()); 
    applicationThread->getChangeList()->applyAndClear();

    std::cout << "addChild wait for barrier 3" << std::endl;
    mvbarrier->enter(2);
    std::cout << "addChild gone through barrier 3" << std::endl;

    OSGTriangleCB<Mesh>(child,mesh); 

#### mesh has still 0 triangles

######
###### At the other side the display function waits for this child
######

void display( void ) {

    std::cout << "display wait for barrier 1" << std::endl;
    mvbarrier->enter(2);
    std::cout << "display gone through barrier 1" << std::endl;
    std::cout << "display sync change list" << std::endl;
    convertThread->getChangeList()->applyAndClear();
    std::cout << "display wait for barrier 2" << std::endl;
    mvbarrier->enter(2);
    std::cout << "display gone through barrier 2" << std::endl;
    mgr->redraw();
    std::cout << "display redrawed" << std::endl;
    std::cout << "display wait for barrier 3" << std::endl;
    mvbarrier->enter(2);
    std::cout << "display gone through barrier 3" << std::endl;

}

######

[1] TestOsgTriangleCB.cpp
[2] OSGTriangleCB.cpp 

Hope that this will help. I used so much barriers because of my
confusion about the problem.

Regards,
Denis

On Sat, 12 Feb 2011 08:58:27 -0600
Dirk Reiners <dirk.rein...@gmail.com> wrote:

> 
>       Hi Denis,
> 
> first question: OpenSG 1 or 2?
> 
> On 02/12/2011 07:21 AM, Denis Röper wrote:
> > Hello,
> >
> > I created some osg::Surface nodes and now I want to fetch all the
> > triangles rendered from these nodes. I used the TriangleIterator to
> > do this, but usually I got no Triangles.
> >
> > I can put the node into the scene graph and hide it with a
> > ColorMaskChunk, wait until it is rendered or call the
> > osg::RenderAction on it (Is this ok for osg::Surface?), but still I
> > get no Triangles. Since I create the node in a thread I synchronise
> > both change lists already.
> 
> OK.
> 
> > Usually I don't want to see this node, so a method without adding
> > the node into the scene graph will be fine, but the ColorMaskChunk
> > is fine, too. But its still not function.
> 
> That shouldn't make a difference.
> 
> > I wrote a simple test program that fetches the Triangles in the
> > "display" callback function after the redraw() call. Here I got the
> > Triangles.
> 
> ...and not before that? That is weird. Can you send us the program?
> 
> The TriangleIterator doesn't really depend on anything, as long as
> the geometry has the required data it should work just fine. And the
> rendering run should not change any data in the Geometry, so the fact
> that it makes a difference is very surprising.
> 
> > Any suggestions? A method without the need to wait for redraw will
> > be great.
> 
> Of course! And it should work like that. If you could send us the
> program that would help a lot.
> 
> Yours
> 
>       Dirk
> 
> ------------------------------------------------------------------------------
> The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio
> XE: Pinpoint memory and threading errors before they happen.
> Find and fix more than 250 security defects in the development cycle.
> Locate bottlenecks in serial and parallel code that limit performance.
> http://p.sf.net/sfu/intel-dev2devfeb
> _______________________________________________
> Opensg-users mailing list
> Opensg-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/opensg-users

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <OpenSG/OSGGLUT.h>
#include <OpenSG/OSGConfig.h>
#include <OpenSG/OSGSimpleGeometry.h>
#include <OpenSG/OSGGLUTWindow.h>
#include <OpenSG/OSGSimpleSceneManager.h>
#include <OpenSG/OSGTransform.h>
#include <OpenSG/OSGNodePtr.h>
#include <OpenSG/OSGGroup.h>
#include <OpenSG/OSGSurface.h>
#include <OpenSG/OSGSimpleMaterial.h>
#include <OpenSG/OSGChunkMaterial.h>
#include <OpenSG/OSGColorMaskChunk.h>
#include <OpenSG/OSGTriangleIterator.h>

// New Headers
#include <OpenSG/OSGAttachment.h>
#include <OpenSG/OSGSimpleAttachments.h>

osg::Action::ResultE enter(osg::NodePtr& node) {
  std::cout << "Enter node : " << node << std::endl;

  if (node->getCore()->getType().isDerivedFrom(::osg::Geometry::getClassType())) {
    ::osg::TriangleIterator it(node);
    size_t i=0;
    const ::osg::Matrix m = node->getToWorld();
    for ( ; !it.isAtEnd(); ++it) {
      const ::osg::Pnt3f p0 = m * it.getPosition(0);
      const ::osg::Pnt3f p1 = m * it.getPosition(1);
      const ::osg::Pnt3f p2 = m * it.getPosition(2);
      std::cout << "p0: " << p0 << std::endl;
      std::cout << "p1: " << p1 << std::endl;
      std::cout << "p2: " << p2 << std::endl;
      i++;
    }
    std::cout << "#triangles: " << i << std::endl;
  }
  return osg::Action::Continue;
}

osg::Action::ResultE leave(osg::NodePtr& node, osg::Action::ResultE result) {
  std::cout << "Leaving node: " << node << "with code: " << result << std::endl;
  return result;
}

class TriangleCB {
public:
  
  TriangleCB() { ; }
  
  osg::NodePtr operator() (osg::NodePtr root) {
    traverse(root, osg::osgTypedMethodFunctor1ObjPtrCPtrRef(
							    this, 
							    &TriangleCB::check));
    return osg::NullFC;
  }
  
  static osg::NodePtr fetch(osg::NodePtr root) {
    TriangleCB f;      
    return f(root);
  }
  
private:
  
  osg::Action::ResultE check(osg::NodePtr& node) {
    if (node->getCore()->getType().isDerivedFrom(::osg::Geometry::getClassType())) {
      ::osg::TriangleIterator it(node);
      size_t i=0;
      for ( ; !it.isAtEnd(); ++it) {
	i++;
      }
      std::cout << "##triangles: " << i << std::endl;
    }
    return osg::Action::Continue;        
  }
  
};

osg::NodePtr node;

// The pointer to the transformation
OSG::TransformPtr trans;

// The SimpleSceneManager to manage simple applications
OSG::SimpleSceneManager *mgr;
OSG::GLUTWindowPtr gwin;

// forward declaration so we can have the interesting stuff upfront
int setupGLUT( int *argc, char *argv[] );

// redraw the window
void display( void ) {

  std::cout << "display" << std::endl;

  mgr->redraw();

  // osg::RenderAction* ra = osg::RenderAction::create();
  // ra->setWindow(getCPtr(mgr->getWindow()));
  // osg::ViewportPtr viewport = mgr->getWindow()->getPort(0);
  // ra->addNode(node);
  // ra->addNode(osg::makeSphere(3,15));
  // ra->addNode(newMv->getSeparator());
  // viewport->render(ra);
  // ra->apply(newMv->getSeparator());
  // ra->apply(osg::makeSphere(3,15));
  // ra->apply(node);
  // gwin->render(ra);
  // traverse(newMv->getSeparator(),
  // 	   osg::osgTypedFunctionFunctor1CPtrRef<osg::Action::ResultE, osg::NodePtr>(enter), 
  // 	   osg::osgTypedFunctionFunctor2CPtrRef<osg::Action::ResultE, osg::NodePtr, osg::Action::ResultE>(leave));

  traverse(node,
  	   osg::osgTypedFunctionFunctor1CPtrRef<osg::Action::ResultE, osg::NodePtr>(enter), 
  	   osg::osgTypedFunctionFunctor2CPtrRef<osg::Action::ResultE, osg::NodePtr, osg::Action::ResultE>(leave));

  // Mesh3f mesh;
  // OSGTriangleCB tcb(node,mesh);
  // // OSGTriangleCB tcb(newMv->getSeparator(),mesh);
  // std::cout << "fetched " << mesh.vertexCount() << " vertices." << std::endl;
  // std::cout << "fetched " << mesh.faceCount() << " faces." << std::endl;

}

void update(void)
{
    glutPostRedisplay();
}

// Initialize GLUT & OpenSG and set up the scene
int main(int argc, char **argv) {

  // OSG init
  OSG::osgInit(argc,argv);
  
  // GLUT init
  int winid = setupGLUT(&argc, argv);

  // the connection between GLUT and OpenSG
  gwin = OSG::GLUTWindow::create();
  gwin->setId(winid);
  gwin->init();

  // create the scene
  ::osg::SurfacePtr surface;
  node = ::osg::makeCoredNode< ::osg::Surface >(&surface); 

  ::osg::SimpleMaterialPtr mat = ::osg::SimpleMaterial::create();
  surface->setMaterial(mat);
  ::osg::ChunkMaterialPtr cm = ::osg::ChunkMaterialPtr::dcast(mat);
  
  ::osg::ColorMaskChunkPtr occluderMat = ::osg::ColorMaskChunk::create();
  beginEditCP(occluderMat);
  occluderMat->setMaskR( false );
  occluderMat->setMaskG( false );
  occluderMat->setMaskB( false );
  occluderMat->setMaskA( false );
  endEditCP(occluderMat);
  
  beginEditCP(cm);
  cm->addChunk(occluderMat);
  cm->setSortKey(::osg::Int32(10));
  endEditCP(cm);

  ::osg::GeoPositions3fPtr cps = ::osg::GeoPositions3f::create();
  beginEditCP(cps, ::osg::GeoPositions3f::GeoPropDataFieldMask);
  {
    cps->clear();
    //swap cps
    const ::osg::Pnt3f p0(0,0,0);
    const ::osg::Pnt3f p1(0,10,0);
    const ::osg::Pnt3f p2(10,0,0);
    const ::osg::Pnt3f p3(10,10,0);
    cps->getField().push_back(p0);
    cps->getField().push_back(p1);
    cps->getField().push_back(p2);
    cps->getField().push_back(p3);
  }
  endEditCP(cps, ::osg::GeoPositions3f::GeoPropDataFieldMask);
  beginEditCP(surface);
  {
    // let's clear the trimming
    surface->removeCurves();
    // set up dimensions and knot vectors:
    surface->setDimU( 1 );
    surface->setDimV( 1 );
    surface->editMFKnotsU()->clear();
    surface->editMFKnotsU()->push_back(0);
    surface->editMFKnotsU()->push_back(0);
    surface->editMFKnotsU()->push_back(1);
    surface->editMFKnotsU()->push_back(1);
    surface->editMFKnotsV()->clear();
    surface->editMFKnotsV()->push_back(0);
    surface->editMFKnotsV()->push_back(0);
    surface->editMFKnotsV()->push_back(1);
    surface->editMFKnotsV()->push_back(1);
    // set control points
    surface->setControlPoints(cps);
    // set error
    surface->setError(0.05);
    
  }
  endEditCP(surface);

  std::cout << "create the scene" << std::endl;
     
  // add a transformation to make it move     
  OSG::NodePtr scene = OSG::Node::create();  
  trans = OSG::Transform::create();
  beginEditCP(scene, OSG::Node::CoreFieldMask | OSG::Node::ChildrenFieldMask  );
  {
    scene->setCore(trans);
    scene->addChild(node);
  }
  endEditCP  (scene, OSG::Node::CoreFieldMask | OSG::Node::ChildrenFieldMask  );


  // //*****************************************************

  TriangleCB::fetch(node); 
  
  // //*****************************************************


  // create the SimpleSceneManager helper
  mgr = new OSG::SimpleSceneManager;

  // tell the manager what to manage
  mgr->setWindow(gwin );
  mgr->setRoot  (scene);

  std::cout << "show the scene" << std::endl;
  // show the whole scene
  mgr->showAll();

  // GLUT main loop
  glutMainLoop();

  return 0;
}

//
// GLUT callback functions
//

// react to size changes
void reshape(int w, int h)
{
  mgr->resize(w, h);
  glutPostRedisplay();
}

// react to mouse button presses
void mouse(int button, int state, int x, int y)
{
  if (state)
    mgr->mouseButtonRelease(button, x, y);
  else
    mgr->mouseButtonPress(button, x, y);
        
  glutPostRedisplay();
}

// react to mouse motions with pressed buttons
void motion(int x, int y)
{
  mgr->mouseMove(x, y);
  glutPostRedisplay();
}

// react to keys
void keyboard(unsigned char k, int x, int y)
{
  switch(k)
    {
    case 27:    //Esc
      {
	OSG::osgExit();
	exit(0);
      }
      break;
    }
}

// setup the GLUT library which handles the windows for us
int setupGLUT(int *argc, char *argv[])
{
  glutInit(argc, argv);
  glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    
  int winid = glutCreateWindow("OpenSG");
    
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutMouseFunc(mouse);
  glutMotionFunc(motion);
  glutKeyboardFunc(keyboard);

  // call the redraw function whenever there's nothing else to do
  glutIdleFunc(update);

  return winid;
}
#ifndef GEO_OSG_TRAINGLE_CB_HPP
#define GEO_OSG_TRAINGLE_CB_HPP 1

#include <OpenSG/OSGNodePtr.h>
#include <OpenSG/OSGTriangleIterator.h>
// #include <OpenSG/OSGSimpleSceneManager.h>

namespace geo {

namespace opensg {

namespace detail {

template <class Mesh,
          bool b>
void getTriangles(::osg::NodePtr node, 
                  Mesh& mesh,
                  const boost::integral_constant<bool, b>&) {
  typedef typename Mesh::vertex_type MeshPnt3f;
  typedef typename Mesh::vertex_type::normal_type Norm3f;

  ::osg::TriangleIterator it(node);
  for ( ; !it.isAtEnd(); ++it) {
    const ::osg::Pnt3f p0 = it.getPosition(0);
    const ::osg::Pnt3f p1 = it.getPosition(1);
    const ::osg::Pnt3f p2 = it.getPosition(2);
    
    const MeshPnt3f p00(p0[0],p0[1],p0[2]);
    const MeshPnt3f p01(p1[0],p1[1],p1[2]);
    const MeshPnt3f p02(p2[0],p2[1],p2[2]);
    const typename Mesh::vertex_handle_type vh0 = mesh.addVertex(p00);
    const typename Mesh::vertex_handle_type vh1 = mesh.addVertex(p01);
    const typename Mesh::vertex_handle_type vh2 = mesh.addVertex(p02);
    mesh.addIndices(vh0,vh1,vh2);
  }
}

template <class Mesh>
void getTriangles(::osg::NodePtr node, 
                  Mesh& mesh,
                  const boost::true_type&) {

  typedef typename Mesh::vertex_type MeshPnt3f;
  typedef typename Mesh::vertex_type::vec_type Vec3f;
  typedef typename Mesh::vertex_type::normal_type Norm3f;

  const ::osg::Matrix m = node->getToWorld();

  ::osg::TriangleIterator it(node);
  for ( ; !it.isAtEnd(); ++it) {
    const ::osg::Pnt3f p0 = m * it.getPosition(0);
    const ::osg::Pnt3f p1 = m * it.getPosition(1);
    const ::osg::Pnt3f p2 = m * it.getPosition(2);
    
    const ::osg::Vec3f n0 = it.getNormal(0);
    const ::osg::Vec3f n1 = it.getNormal(1);
    const ::osg::Vec3f n2 = it.getNormal(2);
    
    const MeshPnt3f p00(mkVec<Vec3f>(p0[0],p0[1],p0[2]),
                        mkVec<Norm3f>(n0[0],n0[1],n0[2]));
    const MeshPnt3f p01(mkVec<Vec3f>(p1[0],p1[1],p1[2]),
                        mkVec<Norm3f>(n1[0],n1[1],n1[2]));
    const MeshPnt3f p02(mkVec<Vec3f>(p2[0],p2[1],p2[2]),
                        mkVec<Norm3f>(n2[0],n2[1],n2[2]));
    const typename Mesh::vertex_handle_type vh0 = mesh.addVertex(p00);
    const typename Mesh::vertex_handle_type vh1 = mesh.addVertex(p01);
    const typename Mesh::vertex_handle_type vh2 = mesh.addVertex(p02);
    mesh.addIndices(vh0,vh1,vh2);
  }
}

}

template <class Mesh>
class OSGTriangleCB {

public:

  typedef Mesh mesh_type;

  /**
   * node OpenSG-Geo-Knoten welcher in Dreicke
   *      umgewandelt werden soll
   */
  OSGTriangleCB(::osg::NodePtr node, 
                Mesh& mesh) 
    : m_mesh(mesh) {
    
    if (node->getCore()->getType().isDerivedFrom(::osg::Geometry::getClassType())) {
      // std::cout << "found a geometry node. fetch triangles." << std::endl;
      detail::getTriangles(node,mesh,
                           geo::has_normal<typename Mesh::vertex_type>());
    } else if (node->getCore()->getType().isDerivedFrom(::osg::Group::getClassType())) {
      // std::cout << "found a group node. process " 
      //           << node->getNChildren() << " children. " << std::endl;
      ::osg::UInt32 n = node->getNChildren();
      for (size_t i=0; i<n; i++) {
        OSGTriangleCB(node->getChild(i),mesh);
      }
    } else {
      // std::cout << "found unknown node. return. " << std::endl;
    }
    // std::cout << "fetched " << mesh.vertexCount() << " vertices." << std::endl;
    // std::cout << "fetched " << mesh.faceCount() << " triangles." << std::endl;
  }
  
  ~OSGTriangleCB(){
  }
  
private:
  
  Mesh& m_mesh;
   
};

}

}

#endif
/*
  Local Variables:
  mode:c++
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  c-basic-offset:2
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
Opensg-users mailing list
Opensg-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/opensg-users

Reply via email to