| 14 | | |
| 15 | | using namespace osg; |
| | 14 | class Writer3DCNodeVisitor: public osg::NodeVisitor { |
| | 15 | |
| | 16 | public: |
| | 17 | Writer3DCNodeVisitor(std::ostream& fout) : |
| | 18 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), |
| | 19 | _fout(fout) |
| | 20 | { |
| | 21 | // _fout << "# file written by OpenSceneGraph" << std::endl << std::endl; |
| | 22 | } |
| | 23 | |
| | 24 | virtual void apply(osg::Geode &node); |
| | 25 | |
| | 26 | protected: |
| | 27 | |
| | 28 | Writer3DCNodeVisitor& operator = (const Writer3DCNodeVisitor&) { return *this; } |
| | 29 | std::ostream& _fout; |
| | 30 | |
| | 31 | }; |
| | 32 | |
| | 33 | void Writer3DCNodeVisitor::apply( osg::Geode &node ) |
| | 34 | { |
| | 35 | osg::Matrix matrix = osg::computeLocalToWorld(getNodePath()); |
| | 36 | |
| | 37 | unsigned int count = node.getNumDrawables(); |
| | 38 | for ( unsigned int i = 0; i < count; i++ ) |
| | 39 | { |
| | 40 | osg::Geometry *geometry = node.getDrawable( i )->asGeometry(); |
| | 41 | if ( geometry ) |
| | 42 | { |
| | 43 | osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray()); |
| | 44 | osg::Vec3Array* normals = dynamic_cast<osg::Vec3Array*>(geometry->getNormalArray()); |
| | 45 | osg::Vec3Array* colours = dynamic_cast<osg::Vec3Array*>(geometry->getColorArray()); |
| | 46 | |
| | 47 | if ( vertices ) { |
| | 48 | for (unsigned int ii=0;ii<vertices->size();ii++) { |
| | 49 | |
| | 50 | // update nodes with world coords |
| | 51 | osg::Vec3d v = vertices->at(ii) * matrix; |
| | 52 | _fout << v[0] << ' ' << v[1] << ' ' << v[2]; |
| | 53 | |
| | 54 | if ( colours ) |
| | 55 | { |
| | 56 | v=colours->at(ii); |
| | 57 | _fout << ' ' << (int)v[0]*255.0 << ' ' << (int)v[1]*255.0 << ' ' << (int)v[2]*255.0; |
| | 58 | } |
| | 59 | else |
| | 60 | { |
| | 61 | _fout << " 255 255 255"; |
| | 62 | } |
| | 63 | |
| | 64 | if ( normals ) |
| | 65 | { |
| | 66 | v = normals->at(ii); |
| | 67 | _fout << ' ' << v[0] << ' ' << v[1] << ' ' << v[2]; |
| | 68 | } |
| | 69 | else |
| | 70 | { |
| | 71 | _fout << " 0.0 0.0 1.0"; |
| | 72 | } |
| | 73 | |
| | 74 | |
| | 75 | _fout << std::endl; |
| | 76 | } |
| | 77 | } |
| | 78 | |
| | 79 | } |
| | 80 | } |
| | 81 | } |
| 41 | | |
| | 107 | |
| | 108 | unsigned int targetNumVertices = 10000; |
| | 109 | |
| | 110 | osg::Geode* geode = new osg::Geode; |
| | 111 | |
| | 112 | osg::Geometry* geometry = new osg::Geometry; |
| | 113 | |
| | 114 | osg::Vec3Array* vertices = new osg::Vec3Array; |
| | 115 | osg::Vec3Array* normals = new osg::Vec3Array; |
| | 116 | osg::Vec4ubArray* colours = new osg::Vec4ubArray; |
| | 117 | |
| | 118 | osg::Vec3 pos; |
| | 119 | osg::Vec3 normal(0.0,0.0,1.0); |
| | 120 | int r=255,g=255,b=255,a=255; |
| | 121 | char sep; |
| | 122 | |
| 53 | | else |
| 54 | | { |
| 55 | | ++num; |
| 56 | | } |
| 57 | | } |
| 58 | | |
| 59 | | |
| 60 | | osg::notify(osg::INFO) << "num="<<num<<std::endl; |
| 61 | | |
| 62 | | unsigned int targetNumVertices = 10000; |
| 63 | | |
| 64 | | |
| 65 | | osg::Geode* geode = new osg::Geode; |
| 66 | | |
| 67 | | osg::Geometry* geometry = new osg::Geometry; |
| 68 | | |
| 69 | | osg::Vec3Array* vertices = new osg::Vec3Array; |
| 70 | | osg::Vec3Array* normals = new osg::Vec3Array; |
| 71 | | osg::Vec4ubArray* colours = new osg::Vec4ubArray; |
| 72 | | |
| 73 | | vertices->reserve(targetNumVertices); |
| 74 | | normals->reserve(targetNumVertices); |
| 75 | | colours->reserve(targetNumVertices); |
| 76 | | |
| 77 | | fin.close(); |
| 78 | | |
| 79 | | osgDB::ifstream fin2(fileName.c_str()); |
| 80 | | while (fin2) |
| 81 | | { |
| 82 | | fin2.getline(line,LINE_SIZE); |
| 83 | | if (line[0]=='#') |
| 84 | | { |
| 85 | | // comment line |
| 86 | | osg::notify(osg::INFO) <<"Comment: "<<line<<std::endl; |
| 87 | | } |
| 90 | | ++num; |
| 91 | | |
| 92 | | osg::Vec3 pos,normal; |
| 93 | | int r,g,b; |
| 94 | | |
| 95 | | int a = sscanf(line,"%f %f %f %d %d %d %f %f %f", |
| 96 | | &pos.x(),&pos.y(),&pos.z(), |
| 97 | | &r,&g,&b, |
| 98 | | &normal.x(),&normal.y(),&normal.z()); |
| 99 | | |
| 100 | | |
| 101 | | if (a) |
| 102 | | { |
| 103 | | |
| | 134 | int matched = sscanf(line,"%f%c%f%c%f%c%d%c%d%c%d%c%f%c%f%c%f", |
| | 135 | &pos.x(),&sep,&pos.y(),&sep,&pos.z(),&sep, |
| | 136 | &r,&sep,&g,&sep,&b,&sep, |
| | 137 | &normal.x(),&sep,&normal.y(),&sep,&normal.z()); |
| | 138 | |
| | 139 | if (matched) |
| | 140 | { |
| | 141 | |
| 153 | | |
| 154 | | } |
| 155 | | |
| | 189 | |
| | 190 | } |
| | 191 | |
| | 192 | virtual WriteResult writeNode(const osg::Node& node,const std::string& fileName,const Options* options =NULL) const |
| | 193 | { |
| | 194 | if (!acceptsExtension(osgDB::getFileExtension(fileName))) |
| | 195 | return WriteResult(WriteResult::FILE_NOT_HANDLED); |
| | 196 | |
| | 197 | osgDB::ofstream f(fileName.c_str()); |
| | 198 | |
| | 199 | Writer3DCNodeVisitor nv(f); |
| | 200 | |
| | 201 | // we must cast away constness |
| | 202 | (const_cast<osg::Node*>(&node))->accept(nv); |
| | 203 | |
| | 204 | return WriteResult(WriteResult::FILE_SAVED); |
| | 205 | } |