# Visualization Library

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

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlGraphics/Renderable.hpp

Go to the documentation of this file.

    00001 /**************************************************************************************/
    00002 /*                                                                                    */
    00003 /*  Visualization Library                                                             */
    00004 /*  http://www.visualizationlibrary.com                                               */
    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     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       // display list have priority over VBOs
    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 VBOs
    00104         if (vboEnabled() && isVBODirty())
    00105           updateVBOs(false,false);
    00106
    00107         // render
    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

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:24.
Permission is granted to use this page to write and publish articles regarding Visualization Library.