Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Renderable.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef Renderable_INCLUDE_ONCE
33 #define Renderable_INCLUDE_ONCE
34 
35 #include <vlCore/Object.hpp>
36 #include <vlCore/Transform.hpp>
37 #include <vlCore/AABB.hpp>
38 #include <vlCore/Sphere.hpp>
39 #include <vlCore/Log.hpp>
40 #include <vlGraphics/OpenGL.hpp>
41 
42 namespace vl
43 {
44  //------------------------------------------------------------------------------
45  // Renderable
46  //------------------------------------------------------------------------------
47  class Actor;
48  class Shader;
49  class Transform;
50  class Camera;
51  class OpenGLContext;
59  {
61 
62  Renderable(const Renderable& other): Object(other)
63  {
64  VL_DEBUG_SET_OBJECT_NAME()
65  }
66 
67 protected:
69  Renderable& operator=(const Renderable&) = default;
70 
71  public:
73  Renderable(): mBoundsUpdateTick(0), mDisplayList(0), mBoundsDirty(true),
74  mDisplayListEnabled(false), mDisplayListDirty(true), mBufferObjectEnabled(true), mBufferObjectDirty(true){}
75 
77  virtual ~Renderable() { deleteDisplayList(); }
78 
80  void render(const Actor* actor, const Shader* shader, const Camera* camera, OpenGLContext* gl_context)
81  {
82  VL_CHECK_OGL();
83 
84  // display list have priority over BufferObjects
85  if (isDisplayListEnabled())
86  {
87  if ( displayListDirty() )
88  {
89  if ( !displayList() )
90  {
91  setDisplayList( glGenLists(1) ); VL_CHECK_OGL();
92  }
93  VL_CHECK( displayList() );
94  glNewList( displayList(), GL_COMPILE_AND_EXECUTE ); VL_CHECK_OGL();
95  render_Implementation( actor, shader, camera, gl_context ); VL_CHECK_OGL();
96  glEndList(); VL_CHECK_OGL();
97  setDisplayListDirty( false );
98  }
99  else
100  {
101  VL_CHECK( displayList() );
102  glCallList( displayList() );
103  }
104  }
105  else
106  {
107  // update BufferObjects
108  if (isBufferObjectEnabled() && isBufferObjectDirty())
109  {
110  updateDirtyBufferObject(BUM_KeepRamBuffer);
111  setBufferObjectDirty(false);
112  }
113 
114  // render
115  render_Implementation( actor, shader, camera, gl_context ); VL_CHECK_OGL();
116  }
117  VL_CHECK_OGL();
118  }
119 
121  void computeBounds() { computeBounds_Implementation(); setBoundsDirty(false); }
122 
124  long long boundsUpdateTick() const { return mBoundsUpdateTick; }
125 
127  void setBoundsDirty(bool dirty) { mBoundsDirty = dirty; }
128 
130  bool boundsDirty() const { return mBoundsDirty; }
131 
133  void setBoundingBox( const AABB& aabb )
134  {
135  if (mAABB != aabb)
136  {
137  mAABB = aabb;
138  ++mBoundsUpdateTick;
139  }
140  setBoundsDirty(false);
141  }
142 
144  void setBoundingSphere( const Sphere& sphere)
145  {
146  if (mSphere != sphere)
147  {
148  mSphere = sphere;
149  ++mBoundsUpdateTick;
150  }
151  setBoundsDirty(false);
152  }
153 
155  const AABB& boundingBox() const
156  {
157  if (boundsDirty())
158  vl::Log::warning("Renderable::boundingBox() returning dirty bounding box, call computeBounds() first or call boundingBox() from a non-const Renderable!\n");
159  return mAABB;
160  }
161 
163  const Sphere& boundingSphere() const
164  {
165  if (boundsDirty())
166  vl::Log::warning("Renderable::boundingSphere() returning dirty bounding sphere, call computeBounds() first or call boundingSphere() from a non-const Renderable!\n");
167  return mSphere;
168  }
169 
171  const AABB& boundingBox()
172  {
173  if (boundsDirty())
174  computeBounds();
175  return mAABB;
176  }
177 
180  {
181  if (boundsDirty())
182  computeBounds();
183  return mSphere;
184  }
185 
187  unsigned int displayList() const { return mDisplayList; }
188 
190  void setDisplayList(unsigned int disp_list) { mDisplayList = disp_list; }
191 
193  bool isDisplayListEnabled() const { return mDisplayListEnabled; }
194 
196  void setDisplayListEnabled(bool enabled) { mDisplayListEnabled = enabled; }
197 
199  bool displayListDirty() const { return mDisplayListDirty; }
200 
202  void setDisplayListDirty(bool dirty) { mDisplayListDirty = dirty; }
203 
205  bool isBufferObjectEnabled() const { return mBufferObjectEnabled; }
206 
208  void setBufferObjectEnabled(bool enabled) { mBufferObjectEnabled = enabled; }
209 
211  bool isBufferObjectDirty() const { return mBufferObjectDirty; }
212 
214  void setBufferObjectDirty(bool dirty = true) { mBufferObjectDirty = dirty; }
215 
218  virtual void updateDirtyBufferObject(EBufferObjectUpdateMode) = 0;
219 
222  virtual void deleteBufferObject() = 0;
223 
226  {
227  if (displayList())
228  glDeleteLists(displayList(), 1);
229  mDisplayList = 0;
230  }
231 
232  protected:
233  virtual void computeBounds_Implementation() = 0;
234  virtual void render_Implementation(const Actor* actor, const Shader* shader, const Camera* camera, OpenGLContext* gl_context) const = 0;
235 
236  private:
237  long long mBoundsUpdateTick;
238  unsigned int mDisplayList;
239  bool mBoundsDirty;
240  bool mDisplayListEnabled;
241  bool mDisplayListDirty;
242  bool mBufferObjectEnabled;
243  bool mBufferObjectDirty;
244  AABB mAABB;
245  Sphere mSphere;
246  };
247 }
248 
249 #endif
Associates a Renderable object to an Effect and Transform.
Definition: Actor.hpp:130
void setBufferObjectDirty(bool dirty=true)
Whether BufferObjects associated to a Renderable should be recomputed on the next rendering...
Definition: Renderable.hpp:214
static void warning(const String &message)
Use this function to provide information about situations that might lead to errors or loss of data...
Definition: Log.cpp:155
virtual ~Renderable()
Destructor.
Definition: Renderable.hpp:77
Represents an OpenGL context, possibly a widget or a pbuffer, which can also respond to keyboard...
bool boundsDirty() const
Returns whether the bounding sphere or bounding box are "dirty", that is, meant to be recomputed...
Definition: Renderable.hpp:130
void setBoundsDirty(bool dirty)
Marks the bounding box and bounding sphere as dirty in order to be recomputed at the next rendering...
Definition: Renderable.hpp:127
bool isBufferObjectEnabled() const
Returns true if BufferObject (vertex buffer object) are enabled for a Renderable (enabled by default)...
Definition: Renderable.hpp:205
void setBoundingSphere(const Sphere &sphere)
Sets the bounding sphere of a Renderable.
Definition: Renderable.hpp:144
void deleteDisplayList()
Deletes the display list currently associated to a Renderable.
Definition: Renderable.hpp:225
void setBoundingBox(const AABB &aabb)
Sets the bounding box of a Renderable.
Definition: Renderable.hpp:133
bool displayListDirty() const
Whether the display list associated to a Renderable should be recompiled at the next rendering...
Definition: Renderable.hpp:199
bool isDisplayListEnabled() const
Returns true if display lists are enabled for a Renderable (disabled by default). ...
Definition: Renderable.hpp:193
const AABB & boundingBox()
Returns the bounding box of a Renderable recomputing the bounds if dirty.
Definition: Renderable.hpp:171
Visualization Library main namespace.
void setDisplayListDirty(bool dirty)
Whether the display list associated to a Renderable should be recompiled at the next rendering...
Definition: Renderable.hpp:202
void setDisplayList(unsigned int disp_list)
Manually assciates a display list to a Renderable (to be used with care).
Definition: Renderable.hpp:190
The AABB class implements an axis-aligned bounding box using vl::real precision.
Definition: AABB.hpp:44
The base class for all the reference counted objects.
Definition: Object.hpp:158
An abstract class that represents all the objects that can be rendered.
Definition: Renderable.hpp:58
Renderable()
Constructor.
Definition: Renderable.hpp:73
EBufferObjectUpdateMode
Manages most of the OpenGL rendering states responsible of the final aspect of the rendered objects...
Definition: Shader.hpp:1830
The Sphere class defines a sphere using a center and a radius using vl::real precision.
Definition: Sphere.hpp:43
void render(const Actor *actor, const Shader *shader, const Camera *camera, OpenGLContext *gl_context)
Renders the Renderable and if necessary compiles the display list and updates the BufferObjects...
Definition: Renderable.hpp:80
Keeps the local buffer on RAM and updates the BufferObject only if it is marked as dirty...
void setDisplayListEnabled(bool enabled)
Enable/disable display lists (disabled by default).
Definition: Renderable.hpp:196
#define VL_CHECK_OGL()
Definition: OpenGL.hpp:156
#define VL_INSTRUMENT_ABSTRACT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:145
void computeBounds()
Recomputes the bounding box and bounding sphere of a Renderable.
Definition: Renderable.hpp:121
bool isBufferObjectDirty() const
Whether BufferObjects associated to a Renderable should be recomputed on the next rendering...
Definition: Renderable.hpp:211
Represents a virtual camera defining, among other things, the point of view from which scenes can be ...
Definition: Camera.hpp:49
const AABB & boundingBox() const
Returns the bounding box of a Renderable without recomputing the bounds if dirty. ...
Definition: Renderable.hpp:155
const Sphere & boundingSphere()
Returns the bounding sphere of a Renderable recomputing the bounds if dirty.
Definition: Renderable.hpp:179
const Sphere & boundingSphere() const
Returns the bounding sphere of a Renderable without recomputing the bounds if dirty.
Definition: Renderable.hpp:163
unsigned int displayList() const
Returns the display list associated to a Renderable or 0 (zero) if no display list is associated...
Definition: Renderable.hpp:187
void setBufferObjectEnabled(bool enabled)
Enable/disable BufferObject (vertex buffer object) (enabled by default).
Definition: Renderable.hpp:208
#define VL_CHECK(expr)
Definition: checks.hpp:73
long long boundsUpdateTick() const
Returns the bounds-update-tick which is a counter incremented every time the bounding box or bounding...
Definition: Renderable.hpp:124