Hi,

sure i understand, sorry about that. i just wanted to know if someone already 
experienced the same issue before. what i am actually doing is i created a obj 
loader similar to the obj loader plugin from openscenegraph (the mojoraity of 
the code of copied directly just for test). once i have loaded the file, i 
create a mesh using all the information loaded and set the vertices, normals, 
texture cordinates, and primitive set. no problem there. than i attache my mesh 
to the a viewer like anyother example of the osg and run. like i said before, 
the crash happens when the cullvisitor's traverse function gets called when its 
building the sceneview.

i have attached my mesh generator file to the post.

Thank you!

Cheers,
Renato

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=46096#46096



// Alice - (C) Copyright 1993 Michel Gysen
#pragma hdrfile "owlsys.csm"
#pragma hdrstop

#include <Mesh.h>
//--------------------------------------------------------------------------------------
Mesh::Mesh():geometry(NULL),vertices(NULL),normals(NULL),texture(NULL),
nullValue(0xf0000000),currentElementList(0)
{
  geometry = new osg::Geometry;
}
//--------------------------------------------------------------------------------------
Mesh::~Mesh()
{
  if(!geometry->empty()) geometry->unref();
}
//--------------------------------------------------------------------------------------
PCChar Mesh::SetPath(PCChar pathIn,PCChar fileName,PCChar extName)
 {
  Char dev[4],dir[256],name[64],ext[64];
  *dev = *dir = *name = *ext = 0;
  if(fileName) _splitpath(fileName,dev,dir,name,ext);
  _splitpath(pathIn,(*dev ? 0 : dev),(*dir ? 0 : dir),(*name ? 0 : name),(*ext 
? 0 : ext));
  if(!extName) extName  = ext;
  _makepath(filePath,dev,dir,name,extName);
  return filePath;
 }
//--------------------------------------------------------------------------------------
Int Mesh::addVertices(PCReal arr)
{
  if(arr)
  {
    Real x = arr[0];
    Real y = arr[1];
    Real z = arr[2];

    vertices.push_back(osg::Vec3(x,y,z));

    return 0;
  }
  else
    return -1;
}
//--------------------------------------------------------------------------------------
Int Mesh::addNormals(PCReal arr)
{
  if(arr)
  {
    Real x = arr[0];
    Real y = arr[1];
    Real z = arr[2];

    normals.push_back(osg::Vec3(x,y,z));

    return 0;
  }
  else
    return -1;
}
//--------------------------------------------------------------------------------------
Int Mesh::addTexture(PCReal arr)
{
  if(arr)
  {
    Real x = arr[0];
    Real y = arr[1];    

    texture.push_back(osg::Vec2(x,y));

    return 0;
  }
  else
    return -1;
}
//--------------------------------------------------------------------------------------
/*
 * will add the primitive set beased on the element list data
 */
Int Mesh::addFace(PLong iworkSpace,Int nf)
{
  if(iworkSpace)
  {
    //Elements array that will store our faces indexes
    Element* element = new Element(Element::POLYGON);

    //loop the number of entrires (v/vt/vn) in a line and
    //push the indexes into the element table.
    for(Int i = 0; i < nf; i++)
    {
      element->vertexIndices.push_back(remapVertexIndex(iworkSpace[3*i]));
      element->texCoordIndices.push_back(remapTexCoordIndex(iworkSpace[3*i+1]));
      element->normalIndices.push_back(remapNormalIndex(iworkSpace[3*i+2]));
    }

    if (!element->normalIndices.empty() && element->normalIndices.size() != 
element->vertexIndices.size())
      element->normalIndices.clear();
    if (!element->texCoordIndices.empty() && element->texCoordIndices.size() != 
element->vertexIndices.size())
      element->texCoordIndices.clear();
    if (!element->vertexIndices.empty())
    {
      Element::CoordinateCombination coordateCombination = 
element->getCoordinateCombination();
      if (coordateCombination!=currentElementState.coordinateCombination)
      {
        currentElementState.coordinateCombination = coordateCombination;
        currentElementList = 0; // reset the element list to force a recompute 
of which ElementList to use
      }
      if (!currentElementList)
      {
        currentElementList = & (elementStateMap[currentElementState]);
      }
      //push the elements previoulsy set to the main element list
      currentElementList->push_back(element);
    }
    else
    {
      // empty element, don't both adding, just unref to delete it.
      element->unref();
    }
    //push the elements previoulsy set to the main element list
    return 0;
  }
  else
    return -1;
}
//--------------------------------------------------------------------------------------
/*
 * Convert the element list indices into primitive sets to build the faces
 * All of the code was copied from the openscenegraph obj plugin
 */
Int Mesh::convertElementListToGeometry(ElementList& elem)
{
  UInt numVertexIndices = 0;
  UInt numNormalIndices = 0;
  UInt numTexCoordIndices = 0;

  UInt numPointElements = 0;
  UInt numPolylineElements = 0;
  UInt numPolygonElements = 0;

  ElementList::iterator itr;

  for(itr = elem.begin(); itr != elem.end(); ++itr)
  {
    Element& element = *(*itr);

    numVertexIndices += element.vertexIndices.size();
    numNormalIndices += element.normalIndices.size();
    numTexCoordIndices += element.texCoordIndices.size();

    numPointElements += (element.dataType==Element::POINTS) ? 1 : 0;
    numPolylineElements += (element.dataType==Element::POLYLINE) ? 1 : 0;
    numPolygonElements += (element.dataType==Element::POLYGON) ? 1 : 0;
  }
  if(numVertexIndices==0) return 0;
  
  if(numTexCoordIndices!=0 && numTexCoordIndices!=numVertexIndices)
    numTexCoordIndices = 0; //Incorrect number of normals, ignore them

  if(numNormalIndices!=0 && numNormalIndices!=numVertexIndices)
    numNormalIndices = 0; //Incorrect number of normals, ignore them"
  
  osg::Vec3Array* v  = numVertexIndices   ? new osg::Vec3Array : 0;
  osg::Vec2Array* vt = numTexCoordIndices ? new osg::Vec2Array : 0;
  osg::Vec3Array* vn = numNormalIndices   ? new osg::Vec3Array : 0;
  
  if(v)  
    v->reserve(numVertexIndices);
  if(vt) 
    vt->reserve(numTexCoordIndices);
  if(vn) 
    vn->reserve(numNormalIndices);

  if(v)  
    geometry->setVertexArray(v);
  if(vt) 
    geometry->setTexCoordArray(0,vt);
  if(vn)
  {
    geometry->setNormalArray(vn);
    geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
  }

  if (numPolygonElements>0)
  {
    UInt startPos = v->size();
    for(itr = elem.begin(); itr != elem.end(); ++itr)
    {
      Element& element = *(*itr);
      if(element.dataType == Element::POLYGON)
      {
        if(element.vertexIndices.size()>4)
        {
          osg::DrawArrays* drawArrays = new 
osg::DrawArrays(GL_POLYGON,startPos,element.vertexIndices.size());
          startPos += element.vertexIndices.size();
          geometry->addPrimitiveSet(drawArrays);
        }
        else
        {
          osg::DrawArrays* drawArrays = new 
osg::DrawArrays(GL_TRIANGLE_FAN,startPos,element.vertexIndices.size());
          startPos += element.vertexIndices.size();
          geometry->addPrimitiveSet(drawArrays);
        }
        for(Element::IndexList::iterator index_itr = 
element.vertexIndices.begin();
            index_itr != element.vertexIndices.end();
            ++index_itr)
        {
          v->push_back(transformVertex(vertices[*index_itr],FALSE));
        }

        if (numTexCoordIndices)
        {
          for(Element::IndexList::iterator index_itr = 
element.texCoordIndices.begin();
              index_itr != element.texCoordIndices.end();
              ++index_itr)
          {
            vt->push_back(texture[*index_itr]);
          }
        }

        if (numNormalIndices)
        {
          for(Element::IndexList::iterator index_itr = 
element.normalIndices.begin();
              index_itr != element.normalIndices.end();
              ++index_itr)
          {
            vn->push_back(transformNormal(normals[*index_itr],FALSE));
          }
        }
      }
    }
  }  
  return 0;
}
//--------------------------------------------------------------------------------------
/*
 *
 */
osg::Geode* Mesh::Create()
{
  for(ElementStateMap::iterator itr = elementStateMap.begin();
      itr != elementStateMap.end();
      ++itr)
  {
    ElementList& elem = itr->second;

    convertElementListToGeometry(elem);

    if(geometry)
    {
      addDrawable(geometry);
    }
  }
  return this;
}
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to