Go to the documentation of this file.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
00026
00027
00028
00029
00030
00031
00032 #ifndef Renderable_INCLUDE_ONCE
00033 #define Renderable_INCLUDE_ONCE
00034
00035 #include <vlCore/Object.hpp>
00036 #include <vlCore/Transform.hpp>
00037 #include <vlCore/AABB.hpp>
00038 #include <vlCore/Sphere.hpp>
00039 #include <vlCore/Log.hpp>
00040 #include <vlGraphics/OpenGL.hpp>
00041
00042 namespace vl
00043 {
00044
00045
00046
00047 class Actor;
00048 class Shader;
00049 class Transform;
00050 class Camera;
00051 class OpenGLContext;
00058 class VLGRAPHICS_EXPORT Renderable: public Object
00059 {
00060 Renderable(const Renderable& other): Object(other)
00061 {
00062 VL_DEBUG_SET_OBJECT_NAME()
00063 }
00064
00065 public:
00066 virtual const char* className() { return "vl::Renderable"; }
00067
00069 Renderable(): mBoundsUpdateTick(0), mDisplayList(0), mBoundsDirty(true),
00070 mDisplayListEnabled(false), mDisplayListDirty(true), mVBOEnabled(true), mVBODirty(true){}
00071
00073 virtual ~Renderable() { deleteDisplayList(); }
00074
00076 void render(const Actor* actor, const Shader* shader, const Camera* camera, OpenGLContext* gl_context)
00077 {
00078 VL_CHECK_OGL();
00079
00080
00081 if (isDisplayListEnabled())
00082 {
00083 if ( displayListDirty() )
00084 {
00085 if ( !displayList() )
00086 {
00087 setDisplayList( glGenLists(1) ); VL_CHECK_OGL();
00088 }
00089 VL_CHECK( displayList() );
00090 glNewList( displayList(), GL_COMPILE_AND_EXECUTE ); VL_CHECK_OGL();
00091 render_Implementation( actor, shader, camera, gl_context ); VL_CHECK_OGL();
00092 glEndList(); VL_CHECK_OGL();
00093 setDisplayListDirty( false );
00094 }
00095 else
00096 {
00097 VL_CHECK( displayList() );
00098 glCallList( displayList() );
00099 }
00100 }
00101 else
00102 {
00103
00104 if (vboEnabled() && isVBODirty())
00105 updateVBOs(false,false);
00106
00107
00108 render_Implementation( actor, shader, camera, gl_context ); VL_CHECK_OGL();
00109 }
00110 VL_CHECK_OGL();
00111 }
00112
00114 void computeBounds() { computeBounds_Implementation(); setBoundsDirty(false); }
00115
00117 long long boundsUpdateTick() const { return mBoundsUpdateTick; }
00118
00120 void setBoundsDirty(bool dirty) { mBoundsDirty = dirty; }
00121
00123 bool boundsDirty() const { return mBoundsDirty; }
00124
00126 void setBoundingBox( const AABB& aabb )
00127 {
00128 if (mAABB != aabb)
00129 {
00130 mAABB = aabb;
00131 ++mBoundsUpdateTick;
00132 }
00133 setBoundsDirty(false);
00134 }
00135
00137 void setBoundingSphere( const Sphere& sphere)
00138 {
00139 if (mSphere != sphere)
00140 {
00141 mSphere = sphere;
00142 ++mBoundsUpdateTick;
00143 }
00144 setBoundsDirty(false);
00145 }
00146
00148 const AABB& boundingBox() const
00149 {
00150 if (boundsDirty())
00151 vl::Log::warning("Renderable::boundingBox() returning dirty bounding box, call computeBounds() first or call boundingBox() from a non-const Renderable!\n");
00152 return mAABB;
00153 }
00154
00156 const Sphere& boundingSphere() const
00157 {
00158 if (boundsDirty())
00159 vl::Log::warning("Renderable::boundingSphere() returning dirty bounding sphere, call computeBounds() first or call boundingSphere() from a non-const Renderable!\n");
00160 return mSphere;
00161 }
00162
00164 const AABB& boundingBox()
00165 {
00166 if (boundsDirty())
00167 computeBounds();
00168 return mAABB;
00169 }
00170
00172 const Sphere& boundingSphere()
00173 {
00174 if (boundsDirty())
00175 computeBounds();
00176 return mSphere;
00177 }
00178
00180 unsigned int displayList() const { return mDisplayList; }
00181
00183 void setDisplayList(unsigned int disp_list) { mDisplayList = disp_list; }
00184
00186 bool isDisplayListEnabled() const { return mDisplayListEnabled; }
00187
00189 void setDisplayListEnabled(bool enabled) { mDisplayListEnabled = enabled; }
00190
00192 bool displayListDirty() const { return mDisplayListDirty; }
00193
00195 void setDisplayListDirty(bool dirty) { mDisplayListDirty = dirty; }
00196
00198 bool vboEnabled() const { return mVBOEnabled; }
00199
00201 void setVBOEnabled(bool enabled) { mVBOEnabled = enabled; }
00202
00204 bool isVBODirty() const { return mVBODirty; }
00205
00207 void setVBODirty(bool dirty) { mVBODirty = dirty; }
00208
00211 virtual void updateVBOs(bool discard_local_data, bool force_update) = 0;
00212
00215 virtual void deleteVBOs() {}
00216
00218 void deleteDisplayList()
00219 {
00220 if (displayList())
00221 glDeleteLists(displayList(), 1);
00222 mDisplayList = 0;
00223 }
00224
00225 protected:
00226 virtual void computeBounds_Implementation() = 0;
00227 virtual void render_Implementation(const Actor* actor, const Shader* shader, const Camera* camera, OpenGLContext* gl_context) const = 0;
00228
00229 private:
00230 long long mBoundsUpdateTick;
00231 unsigned int mDisplayList;
00232 bool mBoundsDirty;
00233 bool mDisplayListEnabled;
00234 bool mDisplayListDirty;
00235 bool mVBOEnabled;
00236 bool mVBODirty;
00237 AABB mAABB;
00238 Sphere mSphere;
00239 };
00240 }
00241
00242 #endif