vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
grass_drawable.h
Go to the documentation of this file.
1 #ifndef _GRASS_GEOMETRY_H
2 #define _GRASS_GEOMETRY_H
3 
4 #include <osg/Geometry>
5 #include <osg/Drawable>
6 #include <osg/Geode>
7 #include <osg/Vec3>
8 #include <vector>
9 #include <osg/matrix>
10 
11 #include <osgutil/CullVisitor>
12 
13 #include "grass_parameters.h"
14 #include "grass_mesh.h"
15 
16 
17 using namespace osg;
18 
19 namespace grass
20 {
21 
22 
23 
24 
25  // Extends class Drawable. Adds some grass-ability-specific stuff like private
26  // display list or a pointer to a more detailed version of this Drawable.
27  class GrassDrawable : public Drawable
28  {
29  public:
30  // This extended cosntructor collects all triangles of given Geode and creates
31  // a grass-like mesh from them, so the original mesh of given Geode is covered with grass
32  GrassDrawable(Geode & g, GrassAbilityParameters * parameters);
33 
34  // Default constructor creates emplty Drawable
35  GrassDrawable(Mesh * newMesh, GrassAbilityParameters * parameters);
36 
37  // we will need to manage something prior to destroying this object
38  virtual ~GrassDrawable();
39 
40  // some necessary stuff
41  GrassDrawable(const GrassDrawable& gd, const CopyOp& copyop = CopyOp::SHALLOW_COPY);
42  virtual Object* cloneType() const { return new GrassDrawable(*this); }
43  virtual Object* clone(const CopyOp& copyop) const { return new GrassDrawable(*this, copyop); }
44  virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const GrassDrawable*>(obj) != NULL; }
45  virtual const char* libraryName() const { return "grass"; }
46  virtual const char* className() const { return "GrassDrawable"; }
47 
48  // custom method which manages drawing of this object
49  virtual void drawImplementation(RenderInfo& renderInfo) const;
50 
51  // this method is called by our friend - our private CullCallback and determines
52  // whether to draw this drawable or whether to draw its more detailed version etc.
53  void ManageCulling(osgUtil::CullVisitor * cv, RenderInfo* renderInfo);
54 
55  // role of the object of this class
56  enum ROLE
57  {
58  TOP_LEVEL, // constins the original geometry - is drawn always (if visible, of course)
59  INTERMEDIATE, // is only an intermediate node in something like a oct-tree - constains nothing to draw
60  DETAILED // contains individual grass leaves - is drawn only if is close enough to eye
61  };
62 
63  protected:
64  // holds level of detail (depends on distance from the center of the mesh to the eye point)
65  float LOD;
66 
67  // distance form eye (to the center of the mesh)
69 
70  // true if displaylist ready for drawing
71  mutable bool displayListValid;
72 
73  // prepares displaylist according to the role of the object
74  void PrepareDisplayList(osg::RenderInfo & renderInfo) const;
75 
76  // set of parameters of parent ability
77  ref_ptr<GrassAbilityParameters> params;
78 
79  // specifies the meaning of this object
81 
82  // A kind of a private subtree - it is used during processing cullcallback of this Geometry.
83  // If is this geometry too close to eye, this tree is traversed to collect visible geometry
84  // in it and this parent low-detail geometry is not used.
85  // Otherwise (if eye is far away) only this parent geometry is used or drawing.
86  std::vector<ref_ptr<GrassDrawable>> moreDetailedVersion;
87 
88  // display list containing the geometry of this Drawable. SetUseDisplayList is set to false for this object,
89  // so it manages its drawing itself every frame. And it uses this display list for it.
90  mutable GLuint grassDisplayList;
91 
92  // mesh of this Drawable
93  ref_ptr<Mesh> mesh;
94 
95  // ensures that more detailed version of this piece of grass is ready for drawing
96  void EnsureDetailedVersionIsReady();
97  };
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 }; // end of namespace grass
108 
109 
110 
111 #endif _GRASS_GEOMETRY_H