Visualization Library v1.0.3A lightweight C++ OpenGL middleware for 2D/3D graphics |
[Download] [Tutorials] [All Classes] [Grouped Classes] |
00001 /**************************************************************************************/ 00002 /* */ 00003 /* Visualization Library */ 00004 /* http://visualizationlibrary.org */ 00005 /* */ 00006 /* Copyright (c) 2005-2010, Michele Bosi */ 00007 /* All rights reserved. */ 00008 /* */ 00009 /* Redistribution and use in source and binary forms, with or without modification, */ 00010 /* are permitted provided that the following conditions are met: */ 00011 /* */ 00012 /* - Redistributions of source code must retain the above copyright notice, this */ 00013 /* list of conditions and the following disclaimer. */ 00014 /* */ 00015 /* - Redistributions in binary form must reproduce the above copyright notice, this */ 00016 /* list of conditions and the following disclaimer in the documentation and/or */ 00017 /* other materials provided with the distribution. */ 00018 /* */ 00019 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */ 00020 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */ 00021 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */ 00022 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */ 00023 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */ 00024 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */ 00025 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */ 00026 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */ 00027 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */ 00028 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 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 // Renderable 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 VL_INSTRUMENT_ABSTRACT_CLASS(vl::Renderable, Object) 00061 00062 Renderable(const Renderable& other): Object(other) 00063 { 00064 VL_DEBUG_SET_OBJECT_NAME() 00065 } 00066 00067 public: 00069 Renderable(): mBoundsUpdateTick(0), mDisplayList(0), mBoundsDirty(true), 00070 mDisplayListEnabled(false), mDisplayListDirty(true), mBufferObjectEnabled(true), mBufferObjectDirty(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 // display list have priority over BufferObjects 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 // update BufferObjects 00104 if (isBufferObjectEnabled() && isBufferObjectDirty()) 00105 { 00106 updateDirtyBufferObject(BUM_KeepRamBuffer); 00107 setBufferObjectDirty(false); 00108 } 00109 00110 // render 00111 render_Implementation( actor, shader, camera, gl_context ); VL_CHECK_OGL(); 00112 } 00113 VL_CHECK_OGL(); 00114 } 00115 00117 void computeBounds() { computeBounds_Implementation(); setBoundsDirty(false); } 00118 00120 long long boundsUpdateTick() const { return mBoundsUpdateTick; } 00121 00123 void setBoundsDirty(bool dirty) { mBoundsDirty = dirty; } 00124 00126 bool boundsDirty() const { return mBoundsDirty; } 00127 00129 void setBoundingBox( const AABB& aabb ) 00130 { 00131 if (mAABB != aabb) 00132 { 00133 mAABB = aabb; 00134 ++mBoundsUpdateTick; 00135 } 00136 setBoundsDirty(false); 00137 } 00138 00140 void setBoundingSphere( const Sphere& sphere) 00141 { 00142 if (mSphere != sphere) 00143 { 00144 mSphere = sphere; 00145 ++mBoundsUpdateTick; 00146 } 00147 setBoundsDirty(false); 00148 } 00149 00151 const AABB& boundingBox() const 00152 { 00153 if (boundsDirty()) 00154 vl::Log::warning("Renderable::boundingBox() returning dirty bounding box, call computeBounds() first or call boundingBox() from a non-const Renderable!\n"); 00155 return mAABB; 00156 } 00157 00159 const Sphere& boundingSphere() const 00160 { 00161 if (boundsDirty()) 00162 vl::Log::warning("Renderable::boundingSphere() returning dirty bounding sphere, call computeBounds() first or call boundingSphere() from a non-const Renderable!\n"); 00163 return mSphere; 00164 } 00165 00167 const AABB& boundingBox() 00168 { 00169 if (boundsDirty()) 00170 computeBounds(); 00171 return mAABB; 00172 } 00173 00175 const Sphere& boundingSphere() 00176 { 00177 if (boundsDirty()) 00178 computeBounds(); 00179 return mSphere; 00180 } 00181 00183 unsigned int displayList() const { return mDisplayList; } 00184 00186 void setDisplayList(unsigned int disp_list) { mDisplayList = disp_list; } 00187 00189 bool isDisplayListEnabled() const { return mDisplayListEnabled; } 00190 00192 void setDisplayListEnabled(bool enabled) { mDisplayListEnabled = enabled; } 00193 00195 bool displayListDirty() const { return mDisplayListDirty; } 00196 00198 void setDisplayListDirty(bool dirty) { mDisplayListDirty = dirty; } 00199 00201 bool isBufferObjectEnabled() const { return mBufferObjectEnabled; } 00202 00204 void setBufferObjectEnabled(bool enabled) { mBufferObjectEnabled = enabled; } 00205 00207 bool isBufferObjectDirty() const { return mBufferObjectDirty; } 00208 00210 void setBufferObjectDirty(bool dirty) { mBufferObjectDirty = dirty; } 00211 00214 virtual void updateDirtyBufferObject(EBufferObjectUpdateMode) = 0; 00215 00218 virtual void deleteBufferObject() = 0; 00219 00221 void deleteDisplayList() 00222 { 00223 if (displayList()) 00224 glDeleteLists(displayList(), 1); 00225 mDisplayList = 0; 00226 } 00227 00228 protected: 00229 virtual void computeBounds_Implementation() = 0; 00230 virtual void render_Implementation(const Actor* actor, const Shader* shader, const Camera* camera, OpenGLContext* gl_context) const = 0; 00231 00232 private: 00233 long long mBoundsUpdateTick; 00234 unsigned int mDisplayList; 00235 bool mBoundsDirty; 00236 bool mDisplayListEnabled; 00237 bool mDisplayListDirty; 00238 bool mBufferObjectEnabled; 00239 bool mBufferObjectDirty; 00240 AABB mAABB; 00241 Sphere mSphere; 00242 }; 00243 } 00244 00245 #endif