00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _GFX_VISUALTRIANGLES_
00026 #define _GFX_VISUALTRIANGLES_
00027
00028 #include <base/Matrix4>
00029 #include <gfx/gfx>
00030 #include <gfx/Visual>
00031 #include <gfx/TriangleContainer>
00032 #include <gfx/TriangleIteratorState>
00033
00034 #include <osg/NodeVisitor>
00035
00036 #include <stack>
00037
00038
00039 namespace gfx {
00040
00041 class TriangleIterator;
00042 class TriangleIteratorState;
00043
00044
00045 class VisualTriangles : public TriangleContainer,
00046 public base::Object
00047 {
00048 public:
00049 VisualTriangles(const Visual& visual);
00050 VisualTriangles(const Visual& visual, Int LODChild);
00051
00052 virtual ~VisualTriangles() {}
00053
00054 virtual String className() const { return String("VisualTriangles"); }
00055
00056 typedef array<Triangle3> TriangleArray;
00057
00058 virtual const TriangleArray& getTriangleArray() const
00059 { return triangles; }
00060
00061 protected:
00062 TriangleArray triangles;
00063 Int LODChild;
00064
00065 class TriangleArrayIteratorState : public TriangleIteratorState
00066 {
00067 public:
00068 TriangleArrayIteratorState(const TriangleArrayIteratorState& i)
00069 : triangles(i.triangles), index(i.index) {}
00070 TriangleArrayIteratorState(const TriangleArray& triangles, Int index=0)
00071 : triangles(triangles), index(index) {}
00072 virtual ~TriangleArrayIteratorState() {}
00073
00074 virtual String className() const { return String("TriangleArrayIteratorState"); }
00075
00076 virtual Object& clone() const { return *new TriangleArrayIteratorState(*this); }
00077
00078 virtual const Triangle3& operator*() { return triangles[index]; }
00079
00080 friend class VisualTriangles;
00081 protected:
00082 const TriangleArray& triangles;
00083 Int index;
00084 };
00085
00086
00087 virtual TriangleIteratorState* newTriangleIteratorState(bool begin=true) const
00088 {
00089 if (begin)
00090 return new TriangleArrayIteratorState(triangles,0);
00091 else
00092 return new TriangleArrayIteratorState(triangles,triangles.size());
00093 }
00094 virtual void nextTriangle(TriangleIteratorState* iteratorState) const
00095 {
00096 TriangleArrayIteratorState* state = static_cast<TriangleArrayIteratorState*>(iteratorState);
00097 if (state->index < triangles.size()) state->index++;
00098 }
00099 virtual void deleteTriangleIteratorState(TriangleIteratorState* state) const
00100 { delete state; }
00101 virtual bool equalStates(TriangleIteratorState* state1, TriangleIteratorState* state2) const
00102 {
00103 TriangleArrayIteratorState* tastate1 = dynamic_cast<TriangleArrayIteratorState*>(state1);
00104 TriangleArrayIteratorState* tastate2 = dynamic_cast<TriangleArrayIteratorState*>(state2);
00105 return (tastate1->index == tastate2->index);
00106 }
00107
00108
00109
00110 virtual void extractTriangles(osg::Node* node);
00111
00112 class TriangleExtractor : public osg::NodeVisitor
00113 {
00114 TriangleExtractor(TriangleArray& triangles, Int LODChild)
00115 : NodeVisitor(TRAVERSE_ALL_CHILDREN), LODChild(LODChild), triangles(triangles)
00116 { transform.push(Matrix4()); }
00117
00118 virtual void apply(osg::Transform& node);
00119 virtual void apply(osg::Switch& node);
00120 virtual void apply(osg::Group& node);
00121 virtual void apply(osg::LOD& node);
00122 virtual void apply(osg::Geode &node);
00123
00124 protected:
00125 Int LODChild;
00126 std::stack<Matrix4> transform;
00127 TriangleArray& triangles;
00128
00129 friend class VisualTriangles;
00130 };
00131
00132
00133 friend class TriangleIterator;
00134 };
00135
00136
00137 }
00138
00139 #endif