| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | #ifndef LWOSG_SCENELOADER_ |
|---|
| 9 | #define LWOSG_SCENELOADER_ |
|---|
| 10 | |
|---|
| 11 | #include <osg/Group> |
|---|
| 12 | #include <osg/Vec3> |
|---|
| 13 | #include <osg/AnimationPath> |
|---|
| 14 | #include <osg/Vec3> |
|---|
| 15 | #include <osg/Vec4> |
|---|
| 16 | |
|---|
| 17 | #include <osgDB/ReaderWriter> |
|---|
| 18 | |
|---|
| 19 | #include <string> |
|---|
| 20 | #include <vector> |
|---|
| 21 | #include <map> |
|---|
| 22 | |
|---|
| 23 | namespace lwosg |
|---|
| 24 | { |
|---|
| 25 | |
|---|
| 26 | class CoordinateSystemFixer: public osg::Referenced { |
|---|
| 27 | public: |
|---|
| 28 | virtual osg::Vec3 fix_point(const osg::Vec3 &P) const = 0; |
|---|
| 29 | virtual osg::Vec4 fix_point(const osg::Vec4 &P) const = 0; |
|---|
| 30 | virtual osg::Vec3 fix_vector(const osg::Vec3 &V) const = 0; |
|---|
| 31 | virtual osg::Vec4 fix_vector(const osg::Vec4 &V) const = 0; |
|---|
| 32 | virtual inline bool invert_winding() const { return false; } |
|---|
| 33 | |
|---|
| 34 | protected: |
|---|
| 35 | virtual ~CoordinateSystemFixer() {} |
|---|
| 36 | CoordinateSystemFixer &operator=(const CoordinateSystemFixer &) { return *this; } |
|---|
| 37 | }; |
|---|
| 38 | |
|---|
| 39 | class LwoCoordFixer: public CoordinateSystemFixer { |
|---|
| 40 | public: |
|---|
| 41 | inline osg::Vec3 fix_point(const osg::Vec3 &P) const; |
|---|
| 42 | inline osg::Vec4 fix_point(const osg::Vec4 &P) const; |
|---|
| 43 | inline osg::Vec3 fix_vector(const osg::Vec3 &V) const; |
|---|
| 44 | inline osg::Vec4 fix_vector(const osg::Vec4 &V) const; |
|---|
| 45 | inline bool invert_winding() const { return true; } |
|---|
| 46 | |
|---|
| 47 | protected: |
|---|
| 48 | virtual ~LwoCoordFixer() {} |
|---|
| 49 | LwoCoordFixer &operator=(const LwoCoordFixer &) { return *this; } |
|---|
| 50 | }; |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | class SceneLoader { |
|---|
| 54 | public: |
|---|
| 55 | struct Options { |
|---|
| 56 | osg::ref_ptr<CoordinateSystemFixer> csf; |
|---|
| 57 | Options(): csf(new LwoCoordFixer) {} |
|---|
| 58 | }; |
|---|
| 59 | |
|---|
| 60 | typedef std::vector<osg::ref_ptr<osg::AnimationPath> > Animation_list; |
|---|
| 61 | |
|---|
| 62 | SceneLoader(); |
|---|
| 63 | SceneLoader(const Options &options); |
|---|
| 64 | |
|---|
| 65 | osg::Group *load(const std::string &filename, const osgDB::ReaderWriter::Options *options, bool search = false); |
|---|
| 66 | |
|---|
| 67 | inline osg::Group *get_root() { return root_.get(); } |
|---|
| 68 | inline const osg::Group *get_root() const { return root_.get(); } |
|---|
| 69 | |
|---|
| 70 | inline const Options &get_options() const { return options_; } |
|---|
| 71 | inline Options &get_options() { return options_; } |
|---|
| 72 | inline void set_options(const Options &options) { options_ = options; } |
|---|
| 73 | |
|---|
| 74 | inline const Animation_list &get_camera_animations() const { return camera_animations_; } |
|---|
| 75 | inline Animation_list &get_camera_animations() { return camera_animations_; } |
|---|
| 76 | |
|---|
| 77 | struct Motion_envelope { |
|---|
| 78 | struct Key { |
|---|
| 79 | osg::Vec3 position; |
|---|
| 80 | osg::Vec3 ypr; |
|---|
| 81 | osg::Vec3 scale; |
|---|
| 82 | Key(): scale(1, 1, 1) {} |
|---|
| 83 | }; |
|---|
| 84 | typedef std::map<double, Key> Key_map; |
|---|
| 85 | Key_map keys; |
|---|
| 86 | }; |
|---|
| 87 | |
|---|
| 88 | protected: |
|---|
| 89 | bool parse_block(const std::string &name, const std::string &data); |
|---|
| 90 | bool parse_block(const std::string &name, const std::vector<std::string> &data); |
|---|
| 91 | void clear(); |
|---|
| 92 | |
|---|
| 93 | private: |
|---|
| 94 | typedef std::map<std::string, osg::ref_ptr<osg::Group> > Object_map; |
|---|
| 95 | Object_map objects_; |
|---|
| 96 | |
|---|
| 97 | Animation_list camera_animations_; |
|---|
| 98 | |
|---|
| 99 | struct Scene_object { |
|---|
| 100 | osg::ref_ptr<osg::Node> layer_node; |
|---|
| 101 | int parent; |
|---|
| 102 | osg::Vec3 pivot; |
|---|
| 103 | osg::Vec3 pivot_rot; |
|---|
| 104 | Motion_envelope motion; |
|---|
| 105 | std::string name; |
|---|
| 106 | |
|---|
| 107 | Scene_object(): parent(-1) {} |
|---|
| 108 | }; |
|---|
| 109 | |
|---|
| 110 | struct Scene_camera { |
|---|
| 111 | Motion_envelope motion; |
|---|
| 112 | }; |
|---|
| 113 | |
|---|
| 114 | typedef std::vector<Scene_object> Scene_object_list; |
|---|
| 115 | Scene_object_list scene_objects_; |
|---|
| 116 | |
|---|
| 117 | typedef std::vector<Scene_camera> Scene_camera_list; |
|---|
| 118 | Scene_camera_list scene_cameras_; |
|---|
| 119 | |
|---|
| 120 | osg::ref_ptr<osg::Group> root_; |
|---|
| 121 | |
|---|
| 122 | int current_channel_; |
|---|
| 123 | int channel_count_; |
|---|
| 124 | |
|---|
| 125 | bool capture_obj_motion_; |
|---|
| 126 | bool capture_cam_motion_; |
|---|
| 127 | |
|---|
| 128 | Options options_; |
|---|
| 129 | }; |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | inline osg::Vec3 LwoCoordFixer::fix_point(const osg::Vec3 &P) const |
|---|
| 134 | { |
|---|
| 135 | return osg::Vec3(P.x(), P.z(), P.y()); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | inline osg::Vec4 LwoCoordFixer::fix_point(const osg::Vec4 &P) const |
|---|
| 139 | { |
|---|
| 140 | return osg::Vec4(fix_point(osg::Vec3(P.x(), P.y(), P.z())), P.w()); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | inline osg::Vec3 LwoCoordFixer::fix_vector(const osg::Vec3 &V) const |
|---|
| 144 | { |
|---|
| 145 | return fix_point(V); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | inline osg::Vec4 LwoCoordFixer::fix_vector(const osg::Vec4 &V) const |
|---|
| 149 | { |
|---|
| 150 | return fix_point(V); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | #endif |
|---|