Visualization Library 2.0.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  public:
69  Renderable(): mBoundsUpdateTick(0), mDisplayList(0), mBoundsDirty(true),
70  mDisplayListEnabled(false), mDisplayListDirty(true), mBufferObjectEnabled(true), mBufferObjectDirty(true){}
71 
73  virtual ~Renderable() { deleteDisplayList(); }
74 
76  void render(const Actor* actor, const Shader* shader, const Camera* camera, OpenGLContext* gl_context)
77  {
78  VL_CHECK_OGL();
79 
80  // display list have priority over BufferObjects
81  if (isDisplayListEnabled())
82  {
83  if ( displayListDirty() )
84  {
85  if ( !displayList() )
86  {
87  setDisplayList( glGenLists(1) ); VL_CHECK_OGL();
88  }
89  VL_CHECK( displayList() );
90  glNewList( displayList(), GL_COMPILE_AND_EXECUTE ); VL_CHECK_OGL();
91  render_Implementation( actor, shader, camera, gl_context ); VL_CHECK_OGL();
92  glEndList(); VL_CHECK_OGL();
93  setDisplayListDirty( false );
94  }
95  else
96  {
97  VL_CHECK( displayList() );
98  glCallList( displayList() );
99  }
100  }
101  else
102  {
103  // update BufferObjects
104  if (isBufferObjectEnabled() && isBufferObjectDirty())
105  {
106  updateDirtyBufferObject(BUM_KeepRamBuffer);
107  setBufferObjectDirty(false);
108  }
109 
110  // render
111  render_Implementation( actor, shader, camera, gl_context ); VL_CHECK_OGL();
112  }
113  VL_CHECK_OGL();
114  }
115 
117  void computeBounds() { computeBounds_Implementation(); setBoundsDirty(false); }
118 
120  long long boundsUpdateTick() const { return mBoundsUpdateTick; }
121 
123  void setBoundsDirty(bool dirty) { mBoundsDirty = dirty; }
124 
126  bool boundsDirty() const { return mBoundsDirty; }
127 
129  void setBoundingBox( const AABB& aabb )
130  {
131  if (mAABB != aabb)
132  {
133  mAABB = aabb;
134  ++mBoundsUpdateTick;
135  }
136  setBoundsDirty(false);
137  }
138 
140  void setBoundingSphere( const Sphere& sphere)
141  {
142  if (mSphere != sphere)
143  {
144  mSphere = sphere;
145  ++mBoundsUpdateTick;
146  }
147  setBoundsDirty(false);
148  }
149 
151  const AABB& boundingBox() const
152  {
153  if (boundsDirty())
154  vl::Log::warning("Renderable::boundingBox() returning dirty bounding box, call computeBounds() first or call boundingBox() from a non-const Renderable!\n");
155  return mAABB;
156  }
157 
159  const Sphere& boundingSphere() const
160  {
161  if (boundsDirty())
162  vl::Log::warning("Renderable::boundingSphere() returning dirty bounding sphere, call computeBounds() first or call boundingSphere() from a non-const Renderable!\n");
163  return mSphere;
164  }
165 
167  const AABB& boundingBox()
168  {
169  if (boundsDirty())
170  computeBounds();
171  return mAABB;
172  }
173 
176  {
177  if (boundsDirty())
178  computeBounds();
179  return mSphere;
180  }
181 
183  unsigned int displayList() const { return mDisplayList; }
184 
186  void setDisplayList(unsigned int disp_list) { mDisplayList = disp_list; }
187 
189  bool isDisplayListEnabled() const { return mDisplayListEnabled; }
190 
192  void setDisplayListEnabled(bool enabled) { mDisplayListEnabled = enabled; }
193 
195  bool displayListDirty() const { return mDisplayListDirty; }
196 
198  void setDisplayListDirty(bool dirty) { mDisplayListDirty = dirty; }
199 
201  bool isBufferObjectEnabled() const { return mBufferObjectEnabled; }
202 
204  void setBufferObjectEnabled(bool enabled) { mBufferObjectEnabled = enabled; }
205 
207  bool isBufferObjectDirty() const { return mBufferObjectDirty; }
208 
210  void setBufferObjectDirty(bool dirty = true) { mBufferObjectDirty = dirty; }
211 
214  virtual void updateDirtyBufferObject(EBufferObjectUpdateMode) = 0;
215 
218  virtual void deleteBufferObject() = 0;
219 
222  {
223  if (displayList())
224  glDeleteLists(displayList(), 1);
225  mDisplayList = 0;
226  }
227 
228  protected:
229  virtual void computeBounds_Implementation() = 0;
230  virtual void render_Implementation(const Actor* actor, const Shader* shader, const Camera* camera, OpenGLContext* gl_context) const = 0;
231 
232  private:
233  long long mBoundsUpdateTick;
234  unsigned int mDisplayList;
235  bool mBoundsDirty;
236  bool mDisplayListEnabled;
237  bool mDisplayListDirty;
238  bool mBufferObjectEnabled;
239  bool mBufferObjectDirty;
240  AABB mAABB;
241  Sphere mSphere;
242  };
243 }
244 
245 #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:210
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:73
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:126
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:123
bool isBufferObjectEnabled() const
Returns true if BufferObject (vertex buffer object) are enabled for a Renderable (enabled by default)...
Definition: Renderable.hpp:201
void setBoundingSphere(const Sphere &sphere)
Sets the bounding sphere of a Renderable.
Definition: Renderable.hpp:140
void deleteDisplayList()
Deletes the display list currently associated to a Renderable.
Definition: Renderable.hpp:221
void setBoundingBox(const AABB &aabb)
Sets the bounding box of a Renderable.
Definition: Renderable.hpp:129
bool displayListDirty() const
Whether the display list associated to a Renderable should be recompiled at the next rendering...
Definition: Renderable.hpp:195
bool isDisplayListEnabled() const
Returns true if display lists are enabled for a Renderable (disabled by default). ...
Definition: Renderable.hpp:189
const AABB & boundingBox()
Returns the bounding box of a Renderable recomputing the bounds if dirty.
Definition: Renderable.hpp:167
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:198
void setDisplayList(unsigned int disp_list)
Manually assciates a display list to a Renderable (to be used with care).
Definition: Renderable.hpp:186
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:69
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:76
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:192
#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:117
bool isBufferObjectDirty() const
Whether BufferObjects associated to a Renderable should be recomputed on the next rendering...
Definition: Renderable.hpp:207
Represents a virtual camera defining, among other things, the point of view from which scenes can be ...
Definition: Camera.hpp:50
const AABB & boundingBox() const
Returns the bounding box of a Renderable without recomputing the bounds if dirty. ...
Definition: Renderable.hpp:151
const Sphere & boundingSphere()
Returns the bounding sphere of a Renderable recomputing the bounds if dirty.
Definition: Renderable.hpp:175
const Sphere & boundingSphere() const
Returns the bounding sphere of a Renderable without recomputing the bounds if dirty.
Definition: Renderable.hpp:159
unsigned int displayList() const
Returns the display list associated to a Renderable or 0 (zero) if no display list is associated...
Definition: Renderable.hpp:183
void setBufferObjectEnabled(bool enabled)
Enable/disable BufferObject (vertex buffer object) (enabled by default).
Definition: Renderable.hpp:204
#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:120