| | 16 | |
| | 17 | osg::ref_ptr<osg::Geometry> ReaderWriterVRML2::convertVRML97IndexedLineSet(openvrml::node *vrml_ifs) const |
| | 18 | { |
| | 19 | osg::ref_ptr<osg::Geometry> osg_geom = new osg::Geometry(); |
| | 20 | |
| | 21 | osg_geom->addPrimitiveSet(new osg::DrawArrayLengths(osg::PrimitiveSet::LINE_STRIP)); |
| | 22 | |
| | 23 | // get array of vertex coordinate_nodes |
| | 24 | if(vrml_ifs->type().id() == "IndexedLineSet") |
| | 25 | { |
| | 26 | std::auto_ptr<openvrml::field_value> fv = vrml_ifs->field("coord"); |
| | 27 | const openvrml::sfnode *sfn = dynamic_cast<const openvrml::sfnode *>(fv.get()); |
| | 28 | |
| | 29 | openvrml::coordinate_node *vrml_coord_node = dynamic_cast<openvrml::coordinate_node *>((sfn->value()).get()); |
| | 30 | const std::vector<openvrml::vec3f> &vrml_coord = vrml_coord_node->point(); |
| | 31 | |
| | 32 | osg::ref_ptr<osg::Vec3Array> osg_vertices = new osg::Vec3Array(); |
| | 33 | |
| | 34 | unsigned i; |
| | 35 | for (i = 0; i < vrml_coord.size(); i++) |
| | 36 | { |
| | 37 | openvrml::vec3f vec = vrml_coord[i]; |
| | 38 | osg_vertices->push_back(osg::Vec3(vec[0], vec[1], vec[2])); |
| | 39 | } |
| | 40 | |
| | 41 | osg_geom->setVertexArray(osg_vertices.get()); |
| | 42 | |
| | 43 | // get array of vertex indices |
| | 44 | std::auto_ptr<openvrml::field_value> fv2 = vrml_ifs->field("coordIndex"); |
| | 45 | const openvrml::mfint32 *vrml_coord_index = dynamic_cast<const openvrml::mfint32 *>(fv2.get()); |
| | 46 | |
| | 47 | osg::ref_ptr<osg::IntArray> osg_vert_index = new osg::IntArray(); |
| | 48 | |
| | 49 | int num_vert = 0; |
| | 50 | for (i = 0; i < vrml_coord_index->value().size(); i++) |
| | 51 | { |
| | 52 | int index = vrml_coord_index->value()[i]; |
| | 53 | if (index == -1) |
| | 54 | { |
| | 55 | static_cast<osg::DrawArrayLengths*>(osg_geom->getPrimitiveSet(0))->push_back(num_vert); |
| | 56 | num_vert = 0; |
| | 57 | } |
| | 58 | else |
| | 59 | { |
| | 60 | osg_vert_index->push_back(index); |
| | 61 | ++num_vert; |
| | 62 | } |
| | 63 | } |
| | 64 | |
| | 65 | if (num_vert) |
| | 66 | { |
| | 67 | //GvdB: Last coordIndex wasn't -1 |
| | 68 | static_cast<osg::DrawArrayLengths*>(osg_geom->getPrimitiveSet(0))->push_back(num_vert); |
| | 69 | } |
| | 70 | |
| | 71 | osg_geom->setVertexIndices(osg_vert_index.get()); |
| | 72 | } |
| | 73 | |
| | 74 | // get array of colours per vertex (if specified) |
| | 75 | { |
| | 76 | std::auto_ptr<openvrml::field_value> fv = vrml_ifs->field("color"); |
| | 77 | const openvrml::sfnode *sfn = dynamic_cast<const openvrml::sfnode *>(fv.get()); |
| | 78 | openvrml::color_node *vrml_color_node = dynamic_cast<openvrml::color_node *>(sfn->value().get()); |
| | 79 | |
| | 80 | if (vrml_color_node != 0) // if no colors, node is NULL pointer |
| | 81 | { |
| | 82 | const std::vector<openvrml::color> &vrml_colors = vrml_color_node->color(); |
| | 83 | |
| | 84 | osg::ref_ptr<osg::Vec3Array> osg_colors = new osg::Vec3Array(); |
| | 85 | |
| | 86 | unsigned i; |
| | 87 | for (i = 0; i < vrml_colors.size(); i++) |
| | 88 | { |
| | 89 | const openvrml::color color = vrml_colors[i]; |
| | 90 | osg_colors->push_back(osg::Vec3(color.r(), color.g(), color.b())); |
| | 91 | } |
| | 92 | osg_geom->setColorArray(osg_colors.get()); |
| | 93 | |
| | 94 | // get array of color indices |
| | 95 | std::auto_ptr<openvrml::field_value> fv2 = vrml_ifs->field("colorIndex"); |
| | 96 | const openvrml::mfint32 *vrml_color_index = dynamic_cast<const openvrml::mfint32 *>(fv2.get()); |
| | 97 | |
| | 98 | osg::ref_ptr<osg::IntArray> osg_color_index = new osg::IntArray(); |
| | 99 | |
| | 100 | if(vrml_color_index->value().size() > 0) |
| | 101 | { |
| | 102 | for (i = 0; i < vrml_color_index->value().size(); i++) |
| | 103 | { |
| | 104 | int index = vrml_color_index->value()[i]; |
| | 105 | if (index != -1) { |
| | 106 | osg_color_index->push_back(index); |
| | 107 | } |
| | 108 | } |
| | 109 | osg_geom->setColorIndices(osg_color_index.get()); |
| | 110 | } else |
| | 111 | // unspecified, use coordIndices field |
| | 112 | osg_geom->setColorIndices(osg_geom->getVertexIndices()); |
| | 113 | |
| | 114 | // get color binding |
| | 115 | std::auto_ptr<openvrml::field_value> fv3 = vrml_ifs->field("colorPerVertex"); |
| | 116 | const openvrml::sfbool *vrml_color_per_vertex = dynamic_cast<const openvrml::sfbool *>(fv3.get()); |
| | 117 | |
| | 118 | if (vrml_color_per_vertex->value()) |
| | 119 | { |
| | 120 | osg_geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
| | 121 | } else |
| | 122 | { |
| | 123 | osg_geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE); |
| | 124 | } |
| | 125 | } |
| | 126 | } |
| | 127 | |
| | 128 | osg_geom->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF); |
| | 129 | |
| | 130 | return osg_geom; |
| | 131 | } |