Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

gfx/VisualTriangles

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)1996 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: VisualTriangles 1030 2004-02-11 20:46:17Z jungd $
00019   $Revision: 1.2 $
00020   $Date: 2004-02-11 15:46:17 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
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   // OSGVisuals
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 } // gfx
00138 
00139 #endif

Generated on Thu Jul 29 15:56:20 2004 for OpenSim by doxygen 1.3.6