OpenSceneGraph Forum Forum Index OpenSceneGraph Forum
Official forum which mirrors the existent OSG mailing lists. Messages posted here are forwarded to the mailing list and vice versa.
 
   FAQFAQ    SearchSearch    MemberlistMemberlist    RulesRules    UsergroupsUsergroups    RegisterRegister 
 Mail2Forum SettingsMail2Forum Settings  ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
   AlbumAlbum  OpenSceneGraph IRC ChatOpenSceneGraph IRC Chat   SmartFeedSmartFeed 

Load OSG trimesh to ODE: Read Vertices from Mesh after Loading with osgDB::readNodeFile


 
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General
View previous topic :: View next topic  
Author Message
TKoller
Newbie


Joined: 20 Jul 2018
Posts: 3

PostPosted: Fri Jul 20, 2018 3:06 pm    Post subject:
Load OSG trimesh to ODE: Read Vertices from Mesh after Loading with osgDB::readNodeFile
Reply with quote

Hi,


i created a 3D Mesh (3D Cube for test) in Blender and i can read and show it with the osgDB function
readNodeFile

I would like to further use the loaded trimesh in ODE to calculate dynamics and contacts.

I was not able to figure out how to read the vertices,

I discovered that i can convert the retrieved osg::Node to osg::Geometry and get the VertexArray of that one.

But for any reason the dimensions of the array are
12
152
8

instead of something like
8
3
what i would assume for a 3d Cube


If anyone has an idea what i can do to retrieve the vertex data or you need more Details about my Implementation i would be happy to hear from you.


...

Thank you!

Cheers,
Tom
Back to top
View user's profile Send private message
TKoller
Newbie


Joined: 20 Jul 2018
Posts: 3

PostPosted: Mon Jul 23, 2018 9:18 am    Post subject:
Reply with quote

With the help of the Beginners Guide and this entry:

6391

i managed to figure out the basic idea.

Will upload some sample code when its ready to use.
Back to top
View user's profile Send private message
TKoller
Newbie


Joined: 20 Jul 2018
Posts: 3

PostPosted: Mon Aug 06, 2018 10:44 am    Post subject:
Reply with quote

So here is hot it is done:


One can use a custom class extending NodeVisitor to "vist" all geometries in a node and extract the data:

Code:

//Meshvisitor.hpp
#include <osg/NodeVisitor>
#include <osg/Shape>
#include <ode/ode.h>
class MeshVisitor: public osg::NodeVisitor {
public:
   static std::vector<dVector3 *> vertice_allocs;
   static std::vector<dTriIndex *> index_allocs;
   dGeomID geom;   // the resulting geometry
   
   MeshVisitor(dSpaceID space);
   virtual ~MeshVisitor();

   /**
    * Traverses all children of the given node
    *
    * @param node to traverse
    */
   virtual void apply(osg::Node& node);

   /**
    * Extract vertice data from Geometry
    *
    * @param geometry Geometry to extract data from
    */
   virtual void apply(osg::Geometry& geometry);

   /**
    * Clears up all allocated memorys
    *
    * Must call to prevent Memoryleaks at end of application
    */
   static void finishMeshes();
private:
   dSpaceID space;  // and ode space for the resulting geometry
};




the apply functions are called on every node /geometry in the given node.
the class needs a ode space to create the ode object from the osg mesh


the cpp looks as follows



Code:
#include "MeshVisitor.hpp"

#include <osg/Geometry>
#include <iostream>
#include <ode/ode.h>
MeshVisitor::MeshVisitor(dSpaceID space): geom(),space(space) {
   // TODO Auto-generated constructor stub
   setTraversalMode(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN);
}

MeshVisitor::~MeshVisitor() {
   // TODO Auto-generated destructor stub
}


void MeshVisitor::apply(osg::Node& node){
   traverse(node);
}


int dTriCallback (dGeomID TriMesh, dGeomID RefObject, int TriangleIndex){
   //printf(TriangleIndex);
   return 1;
}

void MeshVisitor::apply(osg::Geometry& geometry){

      // Read vertices and triangles
      osg::Vec3Array *vertices =dynamic_cast<osg::Vec3Array*> (geometry.getVertexArray()); //< 3d vertice array cast
      auto triangles=(geometry.getPrimitiveSet(0));   //the triangle primitives (can have multiple primitive sets)
      std::cout <<geometry.getNumPrimitiveSets()<< std::endl;
      std::cout <<triangles->getMode()<< std::endl;
      // create ode containers
      dVector3 *ode_vertices= new dVector3[vertices->getNumElements()];
      dTriIndex *indices=new dTriIndex[triangles->getNumIndices()];
      std::cout <<triangles->getNumIndices() << std::endl;
      auto center= geometry.getBound().center(); //<center to center it on COM
      std::cout << center[0] << " "<<center [1]<< " "<<center [2]<< std::endl;

      for (unsigned int i = 0; i < vertices->getNumElements(); i++) {
         // calc vertex relative to center
         (*vertices)[i]-=center;
         osg::Vec3d temp = (*vertices)[i];
         memcpy(ode_vertices[i],&temp,3*sizeof(double)); // copy data to ode array
         std::cout << temp[0] << " " << temp[1] << " " << temp[2] <<std::endl;

         std::cout << ode_vertices[i][0] << " " << ode_vertices[i][1] << " " << ode_vertices[i][2] <<std::endl;
      }
      for(unsigned int i=0; i < triangles->getNumIndices();i++){
         indices[i]=triangles->index(i);
      }
      //Make sure geometry is updated to be centered
      geometry.setVertexArray(vertices);
      dTriMeshDataID mesh=dGeomTriMeshDataCreate();
      dGeomTriMeshDataBuildSimple (mesh,(double *)ode_vertices, vertices->getNumElements(),indices,triangles->getNumIndices());
      geom=dCreateTriMesh(space,mesh, dTriCallback, NULL, NULL);

      //save data of trimeshes
      vertice_allocs.push_back(ode_vertices);
      index_allocs.push_back(indices);
}

void MeshVisitor::finishMeshes(){
   for(auto element : vertice_allocs)
      delete element;
   for(auto element : index_allocs)
         delete element;
   vertice_allocs.clear();
   index_allocs.clear();
}

std::vector<dVector3*> MeshVisitor::vertice_allocs;
std::vector<dTriIndex *> MeshVisitor::index_allocs;



the triangles are stored as 3d vertices in geometry.getVertexArray()
which indices form a triangle are stored in the primitivesets
be carefull to check that your object file only contains 1 mesh with primitive type triangle for this code to work
since there are sometimes offsets in the obj file, the geometry is centered \\

the static function Meshvisitor::finishMeshes makes sure, that all allocated data for the ode meshes gets destroyed in the end\\

the dTriCallback determines whether a triangle with given index can collide in ode with other objects, its set to true for all indizes\\


mesh data can then be loaded to ode with using

Code:
osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(filename);
zavi::osg_ode::MeshVisitor mv(space);
node->accept(mv);
//mesh is now stored in mv.geom


this only works if only one mesh is in the object file and if it is using triangle primitives. as far as i observed meshing an object with blender and exporting it fullfills this.


Hope someone can use this

Cheers
[/code]
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Similar Topics
Topic Author Forum Replies Posted
No new posts How do I continuously read the mouse ... kornerr General 9 Wed Feb 06, 2019 2:02 pm View latest post
No new posts Loading osgt from string / Compile re... mmaurus General 2 Mon Jan 21, 2019 11:02 am View latest post
No new posts Selecting a mesh of 3d model (say 3ds... nebsar General 2 Wed Jan 02, 2019 9:47 pm View latest post
No new posts Problem in loading VPB database from ... peckham0927 General 1 Sun Dec 23, 2018 1:25 pm View latest post
No new posts How make my app to load plugins marco.beninca General 1 Thu Dec 06, 2018 2:34 pm View latest post


Board Security Anti Bot Question MOD - phpBB MOD against Spam Bots
Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP