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 PortalSceneManager_INCLUDE_ONCE 00033 #define PortalSceneManager_INCLUDE_ONCE 00034 00035 #include <vlGraphics/Actor.hpp> 00036 #include <vlCore/Vector4.hpp> 00037 #include <vlCore/Vector3.hpp> 00038 #include <vlGraphics/SceneManager.hpp> 00039 #include <vlCore/Log.hpp> 00040 #include <vlGraphics/Frustum.hpp> 00041 00042 namespace vl 00043 { 00044 //----------------------------------------------------------------------------- 00045 class Sector; 00046 class SceneManagerPortals; 00047 //----------------------------------------------------------------------------- 00053 class VLGRAPHICS_EXPORT Portal: public Object 00054 { 00055 VL_INSTRUMENT_CLASS(vl::Portal, Object) 00056 00057 friend class SceneManagerPortals; 00058 00059 public: 00061 Portal() 00062 { 00063 VL_DEBUG_SET_OBJECT_NAME() 00064 mIsOpen = true; 00065 mVisitTick = 0; 00066 } 00067 00069 std::vector<fvec3>& geometry() { return mPortalGeometry; } 00071 const std::vector<fvec3>& geometry() const { return mPortalGeometry; } 00072 00074 void setTargetSector(Sector* sector) { mTargetSector = sector; } 00076 Sector* targetSector() { return mTargetSector; } 00078 const Sector* targetSector() const { return mTargetSector; } 00079 00081 bool isOpen() const { return mIsOpen; } 00083 void setIsOpen(bool is_open) { mIsOpen = is_open; } 00084 00085 protected: 00087 void setNormal(const fvec3& n) { mNormal = n; } 00089 const fvec3& normal() const { return mNormal; } 00090 00093 bool computeNormal(); 00094 00095 protected: 00096 std::vector<fvec3> mPortalGeometry; 00097 Sector* mTargetSector; 00098 fvec3 mNormal; 00099 unsigned int mVisitTick; 00100 bool mIsOpen; 00101 }; 00102 //----------------------------------------------------------------------------- 00109 class VLGRAPHICS_EXPORT Sector: public Object 00110 { 00111 VL_INSTRUMENT_CLASS(vl::Sector, Object) 00112 00113 public: 00119 class VisibilityCallback: public Object 00120 { 00121 public: 00130 virtual void operator()(const Camera* cam, SceneManagerPortals* psm, Sector* s, Portal* p) = 0; 00131 }; 00132 public: 00134 Sector() 00135 { 00136 VL_DEBUG_SET_OBJECT_NAME() 00137 mActors = new ActorCollection; 00138 } 00139 00141 ActorCollection* actors() { return mActors.get(); } 00143 const ActorCollection* actors() const { return mActors.get(); } 00144 00146 std::vector< ref<Portal> >& portals() { return mPortals; } 00148 const std::vector< ref<Portal> >& portals() const { return mPortals; } 00149 00152 std::vector< AABB >& volumes() { return mVolumes; } 00155 const std::vector< AABB >& volumes() const { return mVolumes; } 00156 00158 AABB computeBoundingBox(); 00159 00160 std::vector< ref<VisibilityCallback> >& callbacks() { return mCallbacks; } 00161 const std::vector< ref<VisibilityCallback> >& callbacks() const { return mCallbacks; } 00162 void executeCallbacks(const Camera*cam,SceneManagerPortals* psm, Portal*p); 00163 00164 protected: 00165 std::vector< ref<Portal> > mPortals; 00166 std::vector< AABB > mVolumes; 00167 ref< ActorCollection > mActors; 00168 std::vector< ref<VisibilityCallback> > mCallbacks; 00169 }; 00170 //----------------------------------------------------------------------------- 00184 class VLGRAPHICS_EXPORT SceneManagerPortals: public SceneManager 00185 { 00186 VL_INSTRUMENT_CLASS(vl::SceneManagerPortals, SceneManager) 00187 00188 public: 00190 SceneManagerPortals(): mExternalSector(new Sector), mVisitTick(1), mShowPortals(false) 00191 { 00192 VL_DEBUG_SET_OBJECT_NAME() 00193 } 00194 00196 void extractActors(ActorCollection& list); 00198 void extractVisibleActors(ActorCollection& list, const Camera* camera); 00199 00201 std::vector< ref<Sector> >& sectors() { return mSectors; } 00203 const std::vector< ref<Sector> >& sectors() const { return mSectors; } 00204 00212 Sector* externalSector() { return mExternalSector.get(); } 00214 const Sector* externalSector() const { return mExternalSector.get(); } 00215 00217 void computePortalNormals(); 00218 00220 void initialize(); 00221 00223 bool showPortals() const { return mShowPortals; } 00225 void setShowPortals(bool show) { mShowPortals = show; } 00227 void invalidatePortalActors() { mPortalActorMap.clear(); } 00228 00230 const std::vector<Frustum>& frustumStack() const { return mFrustumStack; } 00231 00232 protected: 00233 void renderPortal(Portal* portal); 00234 void visitSector(Sector* prev, Sector* sector, const vec3& eye, const Camera* camera); 00235 Sector* computeStartingSector(const Camera* camera); 00236 00237 protected: 00238 ref<Sector> mExternalSector; 00239 std::vector< ref<Sector> > mSectors; 00240 std::vector< ref<Actor> > mTempActors; 00241 std::map<Portal*, ref<Actor> > mPortalActorMap; 00242 std::vector<Frustum> mFrustumStack; 00243 unsigned mVisitTick; 00244 bool mShowPortals; 00245 }; 00246 //----------------------------------------------------------------------------- 00247 } 00248 00249 #endif