| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | #include "Converter.h" |
|---|
| 9 | #include "Tessellator.h" |
|---|
| 10 | |
|---|
| 11 | #include <osg/Notify> |
|---|
| 12 | #include <osg/Geode> |
|---|
| 13 | #include <osg/Geometry> |
|---|
| 14 | #include <osg/CullFace> |
|---|
| 15 | #include <osg/LightModel> |
|---|
| 16 | |
|---|
| 17 | #include <osgDB/FileUtils> |
|---|
| 18 | #include <osgDB/fstream> |
|---|
| 19 | |
|---|
| 20 | #include "lwo2parser.h" |
|---|
| 21 | |
|---|
| 22 | using namespace lwosg; |
|---|
| 23 | |
|---|
| 24 | namespace |
|---|
| 25 | { |
|---|
| 26 | |
|---|
| 27 | struct GeometryBin { |
|---|
| 28 | osg::ref_ptr<osg::DrawElementsUInt> deui_points; |
|---|
| 29 | osg::ref_ptr<osg::DrawElementsUInt> deui_lines; |
|---|
| 30 | osg::ref_ptr<osg::DrawElementsUInt> deui_triangles; |
|---|
| 31 | |
|---|
| 32 | GeometryBin() |
|---|
| 33 | : deui_points(new osg::DrawElementsUInt(GL_POINTS)), |
|---|
| 34 | deui_lines(new osg::DrawElementsUInt(GL_LINES)), |
|---|
| 35 | deui_triangles(new osg::DrawElementsUInt(GL_TRIANGLES)) |
|---|
| 36 | {} |
|---|
| 37 | }; |
|---|
| 38 | |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | Converter::Converter() |
|---|
| 42 | : root_(new osg::Group) |
|---|
| 43 | { |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | Converter::Converter(const Options &options, const osgDB::ReaderWriter::Options* db_options) |
|---|
| 47 | : root_(new osg::Group), |
|---|
| 48 | options_(options), |
|---|
| 49 | db_options_(db_options) |
|---|
| 50 | { |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | osg::Group *Converter::convert(Object &obj) |
|---|
| 54 | { |
|---|
| 55 | if (root_->getNumChildren() > 0) { |
|---|
| 56 | root_->removeChildren(0, root_->getNumChildren()); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | OSG_INFO << "INFO: lwosg::Converter: flattening per-polygon vertex maps\n"; |
|---|
| 60 | for (Object::Layer_map::iterator i=obj.layers().begin(); i!=obj.layers().end(); ++i) { |
|---|
| 61 | for (Layer::Unit_list::iterator j=i->second.units().begin(); j!=i->second.units().end(); ++j) { |
|---|
| 62 | j->flatten_maps(); |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | OSG_INFO << "INFO: lwosg::Converter: creating scene graph\n"; |
|---|
| 67 | build_scene_graph(obj); |
|---|
| 68 | |
|---|
| 69 | return root_.get(); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | void Converter::build_scene_graph(Object &obj) |
|---|
| 73 | { |
|---|
| 74 | |
|---|
| 75 | typedef std::map<int, osg::ref_ptr<osg::Group> > Layer_group_map; |
|---|
| 76 | Layer_group_map lymap; |
|---|
| 77 | |
|---|
| 78 | OSG_DEBUG << "DEBUG INFO: lwosg::Converter: creating layer structure\n"; |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | for (Object::Layer_map::const_iterator i=obj.layers().begin(); i!=obj.layers().end(); ++i) { |
|---|
| 82 | const Layer &layer = i->second; |
|---|
| 83 | osg::ref_ptr<osg::Group> new_group = new osg::Group; |
|---|
| 84 | lymap[layer.number()] = new_group.get(); |
|---|
| 85 | if (layer.get_layer_chunk()) { |
|---|
| 86 | new_group->setName(layer.get_layer_chunk()->name); |
|---|
| 87 | if (layer.get_layer_chunk()->flags & 1) { |
|---|
| 88 | new_group->setNodeMask(0); |
|---|
| 89 | } |
|---|
| 90 | } else { |
|---|
| 91 | new_group->setName("Default_layer"); |
|---|
| 92 | } |
|---|
| 93 | root_->addChild(new_group.get()); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | for (Object::Layer_map::iterator li=obj.layers().begin(); li!=obj.layers().end(); ++li) { |
|---|
| 97 | Layer &layer = li->second; |
|---|
| 98 | |
|---|
| 99 | osg::Group *layer_group = lymap[layer.number()].get(); |
|---|
| 100 | |
|---|
| 101 | OSG_DEBUG << "DEBUG INFO: lwosg::Converter: processing layer '" << layer_group->getName() << "'\n"; |
|---|
| 102 | |
|---|
| 103 | for (Layer::Unit_list::iterator j=layer.units().begin(); j!=layer.units().end(); ++j) { |
|---|
| 104 | |
|---|
| 105 | OSG_DEBUG << "DEBUG INFO: lwosg::Converter: \tcreating primitives\n"; |
|---|
| 106 | |
|---|
| 107 | int tess_success = 0; |
|---|
| 108 | int tess_fail = 0; |
|---|
| 109 | |
|---|
| 110 | typedef std::map<const Surface *, GeometryBin> GeometryBin_map; |
|---|
| 111 | GeometryBin_map bins; |
|---|
| 112 | |
|---|
| 113 | typedef std::map<const Surface *, Unit::Index_list> Remapping_map; |
|---|
| 114 | Remapping_map remappings; |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | j->compute_vertex_remapping(0, remappings[0]); |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | for (Object::Surface_map::const_iterator h=obj.surfaces().begin(); h!=obj.surfaces().end(); ++h) { |
|---|
| 121 | j->compute_vertex_remapping(&h->second, remappings[&h->second]); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | for (unsigned k=0; k<j->polygons().size(); ++k) { |
|---|
| 126 | const Polygon &poly = j->polygons()[k]; |
|---|
| 127 | GeometryBin &bin = bins[poly.get_surface()]; |
|---|
| 128 | const Unit::Index_list &remapping = remappings[poly.get_surface()]; |
|---|
| 129 | |
|---|
| 130 | if (poly.indices().size() == 1) { |
|---|
| 131 | bin.deui_points->push_back(remapping[poly.indices()[0]]); |
|---|
| 132 | } |
|---|
| 133 | if (poly.indices().size() == 2) { |
|---|
| 134 | bin.deui_lines->push_back(remapping[poly.indices()[0]]); |
|---|
| 135 | bin.deui_lines->push_back(remapping[poly.indices()[1]]); |
|---|
| 136 | } |
|---|
| 137 | if (poly.indices().size() == 3) { |
|---|
| 138 | bin.deui_triangles->push_back(remapping[poly.indices()[0]]); |
|---|
| 139 | bin.deui_triangles->push_back(remapping[poly.indices()[1]]); |
|---|
| 140 | bin.deui_triangles->push_back(remapping[poly.indices()[2]]); |
|---|
| 141 | } |
|---|
| 142 | if (poly.indices().size() >= 4) { |
|---|
| 143 | Tessellator tess; |
|---|
| 144 | if (tess.tessellate(poly, j->points(), bin.deui_triangles.get(), &remapping)) { |
|---|
| 145 | ++tess_success; |
|---|
| 146 | } else { |
|---|
| 147 | ++tess_fail; |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | if (tess_success > 0) { |
|---|
| 153 | OSG_DEBUG << "DEBUG INFO: lwosg::Converter: " << tess_success << " polygons have been tessellated correctly\n"; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | if (tess_fail > 0) { |
|---|
| 157 | OSG_WARN << "Warning: lwosg::Converter: could not tessellate " << tess_fail << " polygons correctly. This is probably due to self-intersecting polygons being used, try to Triple them in Lightwave and restart the conversion" << std::endl; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | osg::ref_ptr<osg::Vec3Array> normals = j->normals()->asVec3Array(j->points()->size()); |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | osg::ref_ptr<osg::Geode> geode = new osg::Geode; |
|---|
| 165 | |
|---|
| 166 | for (GeometryBin_map::iterator i=bins.begin(); i!=bins.end(); ++i) { |
|---|
| 167 | const Surface *surface = i->first; |
|---|
| 168 | GeometryBin &bin = i->second; |
|---|
| 169 | |
|---|
| 170 | const Unit::Index_list &remapping = remappings[surface]; |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | OSG_DEBUG << "DEBUG INFO: lwosg::Converter: \tcleaning up redundant vertices and vertex attributes for surface '" << (surface ? surface->get_name() : std::string("anonymous")) << "'\n"; |
|---|
| 174 | osg::ref_ptr<osg::Vec3Array> new_points = new osg::Vec3Array; |
|---|
| 175 | osg::ref_ptr<osg::Vec3Array> new_normals = new osg::Vec3Array; |
|---|
| 176 | for (unsigned pi=0; pi<j->points()->size(); ++pi) { |
|---|
| 177 | if (remapping[pi] != -1) { |
|---|
| 178 | new_points->push_back((*j->points())[pi]); |
|---|
| 179 | new_normals->push_back((*normals)[pi]); |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | OSG_DEBUG << "DEBUG INFO: lwosg::Converter: \tcreating geometry for surface '" << (surface ? surface->get_name() : std::string("anonymous")) << "'\n"; |
|---|
| 184 | |
|---|
| 185 | osg::ref_ptr<osg::Geometry> geo = new osg::Geometry; |
|---|
| 186 | geo->setVertexArray(new_points.get()); |
|---|
| 187 | geo->setNormalArray(new_normals.get()); |
|---|
| 188 | geo->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 189 | |
|---|
| 190 | bool group_used = false; |
|---|
| 191 | |
|---|
| 192 | if (surface) { |
|---|
| 193 | if (!options_.combine_geodes) |
|---|
| 194 | { |
|---|
| 195 | geode->setName(surface->get_name()); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | osg::ref_ptr<VertexMap_map> rm_texture_maps = j->texture_maps()->remap(remapping); |
|---|
| 200 | osg::ref_ptr<VertexMap_map> rm_rgb_maps = j->rgb_maps()->remap(remapping); |
|---|
| 201 | osg::ref_ptr<VertexMap_map> rm_rgba_maps = j->rgba_maps()->remap(remapping); |
|---|
| 202 | osg::Group *sgrp = surface->apply(geo.get(), |
|---|
| 203 | rm_texture_maps.get(), |
|---|
| 204 | rm_rgb_maps.get(), |
|---|
| 205 | rm_rgba_maps.get(), |
|---|
| 206 | options_.max_tex_units, |
|---|
| 207 | options_.use_osgfx, |
|---|
| 208 | options_.force_arb_compression, |
|---|
| 209 | options_.texturemap_bindings, |
|---|
| 210 | db_options_.get()); |
|---|
| 211 | if (sgrp) |
|---|
| 212 | { |
|---|
| 213 | group_used = true; |
|---|
| 214 | osg::ref_ptr<osg::Geode> grp_geode = new osg::Geode; |
|---|
| 215 | grp_geode->setName(surface->get_name()); |
|---|
| 216 | grp_geode->addDrawable(geo.get()); |
|---|
| 217 | sgrp->addChild(grp_geode.get()); |
|---|
| 218 | layer_group->addChild(sgrp); |
|---|
| 219 | } |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | if (!group_used) |
|---|
| 223 | { |
|---|
| 224 | geode->addDrawable(geo.get()); |
|---|
| 225 | if (geode->getNumParents() == 0) |
|---|
| 226 | { |
|---|
| 227 | layer_group->addChild(geode.get()); |
|---|
| 228 | } |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | if (!options_.combine_geodes) |
|---|
| 232 | { |
|---|
| 233 | geode = new osg::Geode; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | if (!bin.deui_points->empty()) geo->addPrimitiveSet(bin.deui_points.get()); |
|---|
| 238 | if (!bin.deui_lines->empty()) geo->addPrimitiveSet(bin.deui_lines.get()); |
|---|
| 239 | if (!bin.deui_triangles->empty()) geo->addPrimitiveSet(bin.deui_triangles.get()); |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | osg::ref_ptr<osg::CullFace> cf = new osg::CullFace; |
|---|
| 244 | cf->setMode(osg::CullFace::BACK); |
|---|
| 245 | root_->getOrCreateStateSet()->setAttributeAndModes(cf.get()); |
|---|
| 246 | |
|---|
| 247 | if (options_.apply_light_model) { |
|---|
| 248 | osg::ref_ptr<osg::LightModel> lm = new osg::LightModel; |
|---|
| 249 | lm->setTwoSided(true); |
|---|
| 250 | lm->setColorControl(osg::LightModel::SEPARATE_SPECULAR_COLOR); |
|---|
| 251 | lm->setAmbientIntensity(osg::Vec4(0, 0, 0, 0)); |
|---|
| 252 | lm->setLocalViewer(true); |
|---|
| 253 | root_->getOrCreateStateSet()->setAttributeAndModes(lm.get()); |
|---|
| 254 | } |
|---|
| 255 | } |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | osg::Group *Converter::convert(const iff::Chunk_list &data) |
|---|
| 259 | { |
|---|
| 260 | Object obj(data); |
|---|
| 261 | obj.set_coordinate_system_fixer(options_.csf.get()); |
|---|
| 262 | return convert(obj); |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | osg::Group *Converter::convert(const std::string &filename) |
|---|
| 266 | { |
|---|
| 267 | std::string file = osgDB::findDataFile(filename, db_options_.get()); |
|---|
| 268 | if (file.empty()) return 0; |
|---|
| 269 | |
|---|
| 270 | osgDB::ifstream ifs(file.c_str(), std::ios_base::in | std::ios_base::binary); |
|---|
| 271 | if (!ifs.is_open()) return 0; |
|---|
| 272 | |
|---|
| 273 | std::vector<char> buffer; |
|---|
| 274 | char c; |
|---|
| 275 | while (ifs.get(c)) buffer.push_back(c); |
|---|
| 276 | |
|---|
| 277 | lwo2::Parser<std::vector<char>::const_iterator > parser(osg::notify(osg::DEBUG_INFO)); |
|---|
| 278 | |
|---|
| 279 | try |
|---|
| 280 | { |
|---|
| 281 | parser.parse(buffer.begin(), buffer.end()); |
|---|
| 282 | } |
|---|
| 283 | catch(lwo2::parser_error &e) |
|---|
| 284 | { |
|---|
| 285 | std::cerr << e.what() << std::endl; |
|---|
| 286 | return 0; |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | for (iff::Chunk_list::const_iterator i=parser.chunks().begin(); i!=parser.chunks().end(); ++i) { |
|---|
| 290 | const lwo2::FORM *form = dynamic_cast<const lwo2::FORM *>(*i); |
|---|
| 291 | if (form) { |
|---|
| 292 | Object obj(form->data); |
|---|
| 293 | obj.set_coordinate_system_fixer(options_.csf.get()); |
|---|
| 294 | if (!convert(obj)) { |
|---|
| 295 | return 0; |
|---|
| 296 | } |
|---|
| 297 | root_->setName(file); |
|---|
| 298 | return root_.get(); |
|---|
| 299 | } |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | return 0; |
|---|
| 304 | } |
|---|