| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | #ifndef LWOSG_POLYGON_ |
|---|
| 9 | #define LWOSG_POLYGON_ |
|---|
| 10 | |
|---|
| 11 | #include "lwo2chunks.h" |
|---|
| 12 | #include "Surface.h" |
|---|
| 13 | #include "VertexMap.h" |
|---|
| 14 | |
|---|
| 15 | #include <osg/Vec3> |
|---|
| 16 | #include <osg/Array> |
|---|
| 17 | |
|---|
| 18 | #include <string> |
|---|
| 19 | #include <map> |
|---|
| 20 | |
|---|
| 21 | namespace lwosg |
|---|
| 22 | { |
|---|
| 23 | |
|---|
| 24 | class Polygon { |
|---|
| 25 | public: |
|---|
| 26 | typedef std::vector<int> Index_list; |
|---|
| 27 | typedef std::map<int, int> Duplication_map; |
|---|
| 28 | |
|---|
| 29 | Polygon(); |
|---|
| 30 | |
|---|
| 31 | inline void set_invert_normal(bool v = true) { invert_normal_ = v; } |
|---|
| 32 | |
|---|
| 33 | inline const Index_list &indices() const { return indices_; } |
|---|
| 34 | inline Index_list &indices() { dirty_normal(); return indices_; } |
|---|
| 35 | |
|---|
| 36 | inline const Duplication_map &dup_vertices() const { return dup_vertices_; } |
|---|
| 37 | inline Duplication_map &dup_vertices() { return dup_vertices_; } |
|---|
| 38 | |
|---|
| 39 | inline const VertexMap *local_normals() const { return local_normals_.get(); } |
|---|
| 40 | inline VertexMap *local_normals() { return local_normals_.get(); } |
|---|
| 41 | |
|---|
| 42 | inline const VertexMap_map *weight_maps() const { return weight_maps_.get(); } |
|---|
| 43 | inline VertexMap_map *weight_maps() { return weight_maps_.get(); } |
|---|
| 44 | |
|---|
| 45 | inline const VertexMap_map *texture_maps() const { return texture_maps_.get(); } |
|---|
| 46 | inline VertexMap_map *texture_maps() { return texture_maps_.get(); } |
|---|
| 47 | |
|---|
| 48 | inline const VertexMap_map *rgb_maps() const { return rgb_maps_.get(); } |
|---|
| 49 | inline VertexMap_map *rgb_maps() { return rgb_maps_.get(); } |
|---|
| 50 | |
|---|
| 51 | inline const VertexMap_map *rgba_maps() const { return rgba_maps_.get(); } |
|---|
| 52 | inline VertexMap_map *rgba_maps() { return rgba_maps_.get(); } |
|---|
| 53 | |
|---|
| 54 | inline bool has_surface() const { return surf_ != 0; } |
|---|
| 55 | inline const Surface *get_surface() const { return surf_; } |
|---|
| 56 | inline void set_surface(const Surface *s) { surf_ = s; } |
|---|
| 57 | |
|---|
| 58 | inline bool has_part() const { return !part_.empty(); } |
|---|
| 59 | inline const std::string &get_part_name() const { return part_; } |
|---|
| 60 | inline void set_part_name(const std::string &n) { part_ = n; } |
|---|
| 61 | |
|---|
| 62 | inline bool has_smoothing_group() const { return !smoothing_group_.empty(); } |
|---|
| 63 | inline const std::string &get_smoothing_group() const { return smoothing_group_; } |
|---|
| 64 | inline void set_smoothing_group(const std::string &n) { smoothing_group_ = n; } |
|---|
| 65 | |
|---|
| 66 | inline const osg::Vec3 &face_normal(const osg::Vec3Array *points) const; |
|---|
| 67 | |
|---|
| 68 | protected: |
|---|
| 69 | inline void dirty_normal() { last_used_points_ = 0; } |
|---|
| 70 | |
|---|
| 71 | private: |
|---|
| 72 | Index_list indices_; |
|---|
| 73 | Duplication_map dup_vertices_; |
|---|
| 74 | |
|---|
| 75 | const Surface *surf_; |
|---|
| 76 | std::string part_; |
|---|
| 77 | std::string smoothing_group_; |
|---|
| 78 | |
|---|
| 79 | osg::ref_ptr<VertexMap> local_normals_; |
|---|
| 80 | osg::ref_ptr<VertexMap_map> weight_maps_; |
|---|
| 81 | osg::ref_ptr<VertexMap_map> texture_maps_; |
|---|
| 82 | osg::ref_ptr<VertexMap_map> rgb_maps_; |
|---|
| 83 | osg::ref_ptr<VertexMap_map> rgba_maps_; |
|---|
| 84 | |
|---|
| 85 | bool invert_normal_; |
|---|
| 86 | |
|---|
| 87 | mutable const osg::Vec3Array *last_used_points_; |
|---|
| 88 | mutable osg::Vec3 normal_; |
|---|
| 89 | }; |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | inline const osg::Vec3 &Polygon::face_normal(const osg::Vec3Array *points) const |
|---|
| 94 | { |
|---|
| 95 | if (last_used_points_ != points) { |
|---|
| 96 | normal_.set(0, 0, 0); |
|---|
| 97 | if (indices_.size() >= 3) { |
|---|
| 98 | const osg::Vec3 &A = points->at(indices_.front()); |
|---|
| 99 | const osg::Vec3 &B = points->at(indices_[1]); |
|---|
| 100 | const osg::Vec3 &C = points->at(indices_.back()); |
|---|
| 101 | if (invert_normal_) { |
|---|
| 102 | normal_ = (C - A) ^ (B - A); |
|---|
| 103 | } else { |
|---|
| 104 | normal_ = (B - A) ^ (C - A); |
|---|
| 105 | } |
|---|
| 106 | float len = normal_.length(); |
|---|
| 107 | if (len != 0) { |
|---|
| 108 | normal_ /= len; |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | last_used_points_ = points; |
|---|
| 112 | } |
|---|
| 113 | return normal_; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | #endif |
|---|