From fe3b68dbb1d9536a807aed97d607ebbc22993f1d Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 18 Jan 2006 12:13:48 +0000 Subject: [PATCH] From Marco Jez: here is a patch that enables a new option named "BIND_TEXTURE_MAP" in the LWO plugin. Its purpose is to allow explicit binding between texture UV maps defined in the LWO file and OpenGL texture units, overriding the default mechanism that allocates texture units automatically. This is useful when you have an UV map built in Lightwave (for example an atlas map) but no textures actually using it, so you can keep the UV map (that would be discarded otherwise) and add a texture later int your program. Syntax is: BIND_TEXTURE_MAP --- src/osgPlugins/lwo/Converter.cpp | 1 + src/osgPlugins/lwo/Converter.h | 73 ++++++++-------- src/osgPlugins/lwo/ReaderWriterLWO.cpp | 9 ++ src/osgPlugins/lwo/Surface.cpp | 21 ++++- src/osgPlugins/lwo/Surface.h | 110 ++++++++++++------------- src/osgPlugins/lwo/VertexMap.h | 69 ++++++++-------- 6 files changed, 157 insertions(+), 126 deletions(-) diff --git a/src/osgPlugins/lwo/Converter.cpp b/src/osgPlugins/lwo/Converter.cpp index 272e515c5..f950291ef 100644 --- a/src/osgPlugins/lwo/Converter.cpp +++ b/src/osgPlugins/lwo/Converter.cpp @@ -207,6 +207,7 @@ void Converter::build_scene_graph(Object &obj) options_.max_tex_units, options_.use_osgfx, options_.force_arb_compression, + options_.texturemap_bindings, db_options_.get()); if (sgrp) { diff --git a/src/osgPlugins/lwo/Converter.h b/src/osgPlugins/lwo/Converter.h index 4e498ea26..d21fe0a4a 100644 --- a/src/osgPlugins/lwo/Converter.h +++ b/src/osgPlugins/lwo/Converter.h @@ -21,50 +21,51 @@ namespace lwosg { - class Converter { - public: + class Converter { + public: - struct Options { - osg::ref_ptr csf; - int max_tex_units; - bool apply_light_model; - bool use_osgfx; - bool force_arb_compression; - bool combine_geodes; + struct Options { + osg::ref_ptr csf; + int max_tex_units; + bool apply_light_model; + bool use_osgfx; + bool force_arb_compression; + bool combine_geodes; + VertexMap_binding_map texturemap_bindings; - Options() - : csf(new LwoCoordFixer), - max_tex_units(0), - apply_light_model(true), - use_osgfx(false), - force_arb_compression(false), - combine_geodes(false) - { - } - }; + Options() + : csf(new LwoCoordFixer), + max_tex_units(0), + apply_light_model(true), + use_osgfx(false), + force_arb_compression(false), + combine_geodes(false) + { + } + }; - Converter(); - Converter(const Options &options, const osgDB::ReaderWriter::Options* db_options); + Converter(); + Converter(const Options &options, const osgDB::ReaderWriter::Options* db_options); - osg::Group *convert(Object &obj); - osg::Group *convert(const iff::Chunk_list &data); - osg::Group *convert(const std::string &filename); + osg::Group *convert(Object &obj); + osg::Group *convert(const iff::Chunk_list &data); + osg::Group *convert(const std::string &filename); - inline osg::Group *get_root() { return root_.get(); } - inline const osg::Group *get_root() const { return root_.get(); } + inline osg::Group *get_root() { return root_.get(); } + inline const osg::Group *get_root() const { return root_.get(); } - inline const Options &get_options() const { return options_; } - inline Options &get_options() { return options_; } - inline void set_options(const Options &options) { options_ = options; } + inline const Options &get_options() const { return options_; } + inline Options &get_options() { return options_; } + inline void set_options(const Options &options) { options_ = options; } - protected: - void build_scene_graph(Object &obj); + protected: + void build_scene_graph(Object &obj); - private: - osg::ref_ptr root_; - Options options_; - osg::ref_ptr db_options_; - }; + private: + osg::ref_ptr root_; + Options options_; + osg::ref_ptr db_options_; + }; } diff --git a/src/osgPlugins/lwo/ReaderWriterLWO.cpp b/src/osgPlugins/lwo/ReaderWriterLWO.cpp index 7bcb5aa32..2b5c0e3ef 100644 --- a/src/osgPlugins/lwo/ReaderWriterLWO.cpp +++ b/src/osgPlugins/lwo/ReaderWriterLWO.cpp @@ -98,6 +98,15 @@ lwosg::Converter::Options ReaderWriterLWO::parse_options(const Options *options) if (opt == "FORCE_ARB_COMPRESSION") conv_options.force_arb_compression = true; if (opt == "USE_OSGFX") conv_options.use_osgfx = true; if (opt == "NO_LIGHTMODEL_ATTRIBUTE") conv_options.apply_light_model = false; + if (opt == "BIND_TEXTURE_MAP") + { + std::string mapname; + int unit; + if (iss >> mapname >> unit) + { + conv_options.texturemap_bindings.insert(std::make_pair(mapname, unit)); + } + } if (opt == "MAX_TEXTURE_UNITS") { int n; if (iss >> n) { diff --git a/src/osgPlugins/lwo/Surface.cpp b/src/osgPlugins/lwo/Surface.cpp index d511313dd..2a159dcd7 100644 --- a/src/osgPlugins/lwo/Surface.cpp +++ b/src/osgPlugins/lwo/Surface.cpp @@ -255,7 +255,7 @@ void Surface::generate_stateset(int max_tex_units, bool force_arb_compression, c } } -osg::Group *Surface::apply(osg::Geometry *geo, const VertexMap_map *texture_maps, const VertexMap_map *rgb_maps, const VertexMap_map *rgba_maps, int max_tex_units, bool use_osgfx, bool force_arb_compression, const osgDB::ReaderWriter::Options* db_options) const +osg::Group *Surface::apply(osg::Geometry *geo, const VertexMap_map *texture_maps, const VertexMap_map *rgb_maps, const VertexMap_map *rgba_maps, int max_tex_units, bool use_osgfx, bool force_arb_compression, const VertexMap_binding_map &texmap_bindings, const osgDB::ReaderWriter::Options* db_options) const { int num_points = 0; @@ -284,6 +284,25 @@ osg::Group *Surface::apply(osg::Geometry *geo, const VertexMap_map *texture_maps } } } + + for (VertexMap_binding_map::const_iterator i=texmap_bindings.begin(); i!=texmap_bindings.end(); ++i) + { + for (VertexMap_map::const_iterator j=texture_maps->begin(); j!=texture_maps->end(); ++j) + { + if (j->first == i->first) + { + if (geo->getTexCoordArray(i->second) != 0) + { + osg::notify(osg::WARN) << "Warning: lwosg::Surface: explicing binding of texture map '" << i->first << "' to texunit " << i->second << " will replace existing texture map" << std::endl; + } + geo->setTexCoordArray(i->second, j->second->asVec2Array(num_points)); + } + else + { + osg::notify(osg::WARN) << "Warning: lwosg::Surface: explicit binding of texture map '" << i->first << "' to texunit " << i->second << " was requested but there is no such map in this LWO file" << std::endl; + } + } + } osg::Vec4 color = osg::Vec4(base_color_, 1-transparency_); diff --git a/src/osgPlugins/lwo/Surface.h b/src/osgPlugins/lwo/Surface.h index f265b0faa..abb83df21 100644 --- a/src/osgPlugins/lwo/Surface.h +++ b/src/osgPlugins/lwo/Surface.h @@ -27,80 +27,80 @@ namespace lwosg { - class Surface { - public: + class Surface { + public: - enum Sidedness { - NONE = 0, - FRONT_ONLY = 1, - BACK_ONLY = 2, - FRONT_AND_BACK = 3 - }; + enum Sidedness { + NONE = 0, + FRONT_ONLY = 1, + BACK_ONLY = 2, + FRONT_AND_BACK = 3 + }; - typedef std::multimap Block_map; + typedef std::multimap Block_map; - Surface(); - Surface(const lwo2::FORM::SURF *surf, const Clip_map &clips); + Surface(); + Surface(const lwo2::FORM::SURF *surf, const Clip_map &clips); - void compile(const lwo2::FORM::SURF *surf, const Clip_map &clips); + void compile(const lwo2::FORM::SURF *surf, const Clip_map &clips); - osg::Group *apply(osg::Geometry *geo, const VertexMap_map *texture_maps, const VertexMap_map *rgb_maps, const VertexMap_map *rgba_maps, int max_tex_units, bool use_osgfx, bool force_arb_compression, const osgDB::ReaderWriter::Options *db_options) const; + osg::Group *apply(osg::Geometry *geo, const VertexMap_map *texture_maps, const VertexMap_map *rgb_maps, const VertexMap_map *rgba_maps, int max_tex_units, bool use_osgfx, bool force_arb_compression, const VertexMap_binding_map &texmap_bindings, const osgDB::ReaderWriter::Options *db_options) const; - void generate_stateset(int max_tex_units, bool force_arb_compression, const osgDB::ReaderWriter::Options* options) const; + void generate_stateset(int max_tex_units, bool force_arb_compression, const osgDB::ReaderWriter::Options* options) const; - inline const std::string &get_name() const { return name_; } - inline void set_name(const std::string &n) { name_ = n; } + inline const std::string &get_name() const { return name_; } + inline void set_name(const std::string &n) { name_ = n; } - inline const osg::Vec3 &get_base_color() const { return base_color_; } + inline const osg::Vec3 &get_base_color() const { return base_color_; } - inline float get_diffuse() const { return diffuse_; } - inline float get_luminosity() const { return luminosity_; } - inline float get_specularity() const { return specularity_; } - inline float get_reflection() const { return reflection_; } - inline float get_transparency() const { return transparency_; } - inline float get_translucency() const { return translucency_; } - inline float get_glossiness() const { return glossiness_; } + inline float get_diffuse() const { return diffuse_; } + inline float get_luminosity() const { return luminosity_; } + inline float get_specularity() const { return specularity_; } + inline float get_reflection() const { return reflection_; } + inline float get_transparency() const { return transparency_; } + inline float get_translucency() const { return translucency_; } + inline float get_glossiness() const { return glossiness_; } - inline Sidedness get_sidedness() const { return sidedness_; } + inline Sidedness get_sidedness() const { return sidedness_; } - inline float get_max_smoothing_angle() const { return max_smoothing_angle_; } + inline float get_max_smoothing_angle() const { return max_smoothing_angle_; } - inline const std::string &get_color_map_type() const { return color_map_type_; } - inline const std::string &get_color_map_name() const { return color_map_name_; } - inline float get_color_map_intensity() const { return color_map_intensity_; } + inline const std::string &get_color_map_type() const { return color_map_type_; } + inline const std::string &get_color_map_name() const { return color_map_name_; } + inline float get_color_map_intensity() const { return color_map_intensity_; } - inline Block_map &blocks() { return blocks_; } - inline const Block_map &blocks() const { return blocks_; } + inline Block_map &blocks() { return blocks_; } + inline const Block_map &blocks() const { return blocks_; } /* - inline const std::string &get_uv_map_name() const { return uv_map_name_; } + inline const std::string &get_uv_map_name() const { return uv_map_name_; } - inline bool has_clip() const { return clip_ != 0; } - inline const Clip *get_clip() const { return clip_; } - inline void set_clip(const Clip *c) { clip_ = c; } + inline bool has_clip() const { return clip_ != 0; } + inline const Clip *get_clip() const { return clip_; } + inline void set_clip(const Clip *c) { clip_ = c; } */ - private: - std::string name_; - osg::Vec3 base_color_; - float diffuse_; - float luminosity_; - float specularity_; - float reflection_; - float transparency_; - float translucency_; - float glossiness_; - Sidedness sidedness_; - float max_smoothing_angle_; - std::string color_map_type_; - std::string color_map_name_; - float color_map_intensity_; + private: + std::string name_; + osg::Vec3 base_color_; + float diffuse_; + float luminosity_; + float specularity_; + float reflection_; + float transparency_; + float translucency_; + float glossiness_; + Sidedness sidedness_; + float max_smoothing_angle_; + std::string color_map_type_; + std::string color_map_name_; + float color_map_intensity_; - Block_map blocks_; + Block_map blocks_; /* - std::string uv_map_name_; - const Clip *clip_; + std::string uv_map_name_; + const Clip *clip_; */ - mutable osg::ref_ptr stateset_; - }; + mutable osg::ref_ptr stateset_; + }; } diff --git a/src/osgPlugins/lwo/VertexMap.h b/src/osgPlugins/lwo/VertexMap.h index 4d9699cc2..c106a9595 100644 --- a/src/osgPlugins/lwo/VertexMap.h +++ b/src/osgPlugins/lwo/VertexMap.h @@ -21,53 +21,54 @@ namespace lwosg { - ///////////////////////////////////////////////////////////////////////// - // VERTEX MAP + ///////////////////////////////////////////////////////////////////////// + // VERTEX MAP - typedef std::map VertexMap_impl; + typedef std::map VertexMap_impl; - class VertexMap: public VertexMap_impl, public osg::Referenced { - public: - VertexMap(): VertexMap_impl(), osg::Referenced() {} + class VertexMap: public VertexMap_impl, public osg::Referenced { + public: + VertexMap(): VertexMap_impl(), osg::Referenced() {} - osg::Vec2Array *asVec2Array(int num_vertices, const osg::Vec2 &default_value = osg::Vec2(0, 0), const osg::Vec2 &modulator = osg::Vec2(1, 1)) const; - osg::Vec3Array *asVec3Array(int num_vertices, const osg::Vec3 &default_value = osg::Vec3(0, 0, 0), const osg::Vec3 &modulator = osg::Vec3(1, 1, 1)) const; - osg::Vec4Array *asVec4Array(int num_vertices, const osg::Vec4 &default_value = osg::Vec4(0, 0, 0, 0), const osg::Vec4 &modulator = osg::Vec4(1, 1, 1, 1)) const; + osg::Vec2Array *asVec2Array(int num_vertices, const osg::Vec2 &default_value = osg::Vec2(0, 0), const osg::Vec2 &modulator = osg::Vec2(1, 1)) const; + osg::Vec3Array *asVec3Array(int num_vertices, const osg::Vec3 &default_value = osg::Vec3(0, 0, 0), const osg::Vec3 &modulator = osg::Vec3(1, 1, 1)) const; + osg::Vec4Array *asVec4Array(int num_vertices, const osg::Vec4 &default_value = osg::Vec4(0, 0, 0, 0), const osg::Vec4 &modulator = osg::Vec4(1, 1, 1, 1)) const; - VertexMap *remap(const std::vector &remapping) const; + VertexMap *remap(const std::vector &remapping) const; - protected: - virtual ~VertexMap() {} - VertexMap &operator=(const VertexMap &) { return *this; } - }; + protected: + virtual ~VertexMap() {} + VertexMap &operator=(const VertexMap &) { return *this; } + }; - ///////////////////////////////////////////////////////////////////////// - // VERTEX MAP MAP + ///////////////////////////////////////////////////////////////////////// + // VERTEX MAP MAP - typedef std::map > VertexMap_map_impl; + typedef std::map > VertexMap_map_impl; + typedef std::multimap VertexMap_binding_map; - class VertexMap_map: public VertexMap_map_impl, public osg::Referenced { - public: - VertexMap_map(): VertexMap_map_impl(), osg::Referenced() {} + class VertexMap_map: public VertexMap_map_impl, public osg::Referenced { + public: + VertexMap_map(): VertexMap_map_impl(), osg::Referenced() {} - VertexMap *getOrCreate(const std::string &name) - { - osg::ref_ptr &vmap = operator[](name); - if (!vmap.valid()) { - vmap = new VertexMap; - } - return vmap.get(); - } + VertexMap *getOrCreate(const std::string &name) + { + osg::ref_ptr &vmap = operator[](name); + if (!vmap.valid()) { + vmap = new VertexMap; + } + return vmap.get(); + } - VertexMap_map *remap(const std::vector &remapping) const; + VertexMap_map *remap(const std::vector &remapping) const; - protected: - virtual ~VertexMap_map() {} - VertexMap_map &operator=(const VertexMap_map &) { return *this; } + protected: + virtual ~VertexMap_map() {} + VertexMap_map &operator=(const VertexMap_map &) { return *this; } - private: - }; + private: + }; }