Hello,

I ran into something peculiar. When loading my own simple binary file format into osg vertex array, normal array and element array (unsigned int) it works fine for models of say 1 million faces (~25MB); however, for the same model files it fails for models of say 7 million faces and up (~160MB). The stats handler does say the vertices and geometry and faces are all there, but it doesn't display anything. It runs at 60Hz and the screen is blank. I disabled all culling, set both counter- and clockwise ordering, but nothing helps. All these models work when I just load them to VBOs manually and call a glDrawElements. Any ideas what may be causing this?

(simple, boring, poor code follows)

template <typename T>
void binout(std::ostream& ofs, const T& x) {
   ofs.write((char*)&x, sizeof(T));
}

template <typename T>
void binin(std::istream& is, T& out) {
   is.read((char*)&out, sizeof(T));
}

typedef unsigned int uint32;
typedef unsigned char uint8;

osg::ref_ptr<osg::Node> loadBinFile(const std::string& fname) {
   osg::ref_ptr<osg::Geode> geode = new osg::Geode;
   osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
   geode->addDrawable(geom);

   osg::ref_ptr<osg::Vec3Array> c = new osg::Vec3Array;
   c->push_back(osg::Vec3(0.5, 1.0, 1.0));
   geom->setColorArray(c);
   geom->setColorBinding(osg::Geometry::BIND_OVERALL);

   osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
   osg::ref_ptr<osg::Vec3Array> n = new osg::Vec3Array;
osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES);

   std::cout << "Loading scene " << fname << " ..." << std::endl;
   std::ifstream ifs;
ifs.exceptions(std::ios_base::failbit | std::ios_base::badbit | std::ios_base::eofbit);
   ifs.open(fname.c_str(), std::ios::in | std::ios::binary);

   uint32 nr_objs;
   binin<uint32>(ifs, nr_objs);
std::cout << "File contains " << nr_objs << " object(s); skipping all but first." << std::endl;

   uint32 num_verts, num_faces;
   uint8 r, g, b;

   binin<uint32>(ifs, num_verts);
   binin<uint32>(ifs, num_faces);
   binin<uint8>(ifs, r);
   binin<uint8>(ifs, g);
   binin<uint8>(ifs, b);

   v->resize(num_verts);
   n->resize(num_verts);
   elements->resize(num_faces*3); // 3 indices per face

   // vertex / normal interleaved storage
   std::cout << "Reading vertex data ... " << std::endl;
   for (size_t i=0; i<num_verts; ++i) {
       binin<float>(ifs, (*v)[i].x());
       binin<float>(ifs, (*v)[i].y());
       binin<float>(ifs, (*v)[i].z());
binin<float>(ifs, (*n)[i].x());
       binin<float>(ifs, (*n)[i].y());
       binin<float>(ifs, (*n)[i].z());
   }

   std::cout << "Reading face data ... " << std::endl;
   for (size_t i=0; i<num_faces; ++i) {
       binin<uint32>(ifs, (*elements)[3*i+0]);
       binin<uint32>(ifs, (*elements)[3*i+1]);
       binin<uint32>(ifs, (*elements)[3*i+2]);
   }
//    ifs.read((char*)&((*elements)[0]), num_faces*3*sizeof(uint32));

   std::cout << "Done." << std::endl;

   geom->setVertexArray(v);
   geom->setNormalArray(n);
   geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
   geom->addPrimitiveSet(elements);

   return geode.get();
}

int main(int argc, char* argv[]) {
   osg::ref_ptr<osg::Node> root = loadBinFile("D:\\models\\dragon1.bin");
root->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);

   osgViewer::Viewer viewer;
   viewer.getCamera()->setCullingMode(osg::CullSettings::NO_CULLING);
   viewer.setSceneData(root);
   viewer.addEventHandler(new osgViewer::StatsHandler);
   viewer.run();

   return 0;
}



--
Regards,

Ferdi Smit
INS3 Visualization and 3D Interfaces
CWI Amsterdam, The Netherlands

_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to