1 //------------------------------------------------------------------------------
2 // File : SkySceneManager.hpp
3 //------------------------------------------------------------------------------
4 // SkyWorks : Copyright 2002 Mark J. Harris and
5 // The University of North Carolina at Chapel Hill
6 //------------------------------------------------------------------------------
7 // Permission to use, copy, modify, distribute and sell this software and its
8 // documentation for any purpose is hereby granted without fee, provided that
9 // the above copyright notice appear in all copies and that both that copyright
10 // notice and this permission notice appear in supporting documentation.
11 // Binaries may be compiled with this software without any royalties or
14 // The author(s) and The University of North Carolina at Chapel Hill make no
15 // representations about the suitability of this software for any purpose.
16 // It is provided "as is" without express or
19 * @file SkySceneManager.hpp
21 * The SkySceneManager class manages all of the renderable objects and
22 * instances. This class maintains lists of each object and instance.
23 * The scene manager decides what to display, it can make use of various
24 * techniques such as view frustum culling, material grouping, etc.
26 #ifndef __SKYSCENEMANAGER_HPP__
27 #define __SKYSCENEMANAGER_HPP__
29 // warning for truncation of template name for browse info
30 #pragma warning( disable : 4786)
36 #include "SkyUtil.hpp"
37 #include "SkySingleton.hpp"
38 #include "SkyRenderableInstance.hpp"
39 #include "SkyMaterial.hpp"
40 #include "SkyAABBTree.hpp"
41 #include "SkyRenderableInstanceCloud.hpp"
43 // forward to reduce unnecessary dependencies
46 class SkyRenderableInstance;
51 //class SkyHeightField;
53 //------------------------------------------------------------------------------
55 * @class SkySceneManager
56 * @brief Manages all of the renderable objects and instances.
58 class SkySceneManager; // Forward declaration
60 //! A singleton of the SkySceneManager. Can only create the SceneManager with SceneManager::Instantiate();
61 typedef SkySingleton<SkySceneManager> SceneManager;
66 SKYRESULT AddObject( SkyRenderable *pObject);
67 SKYRESULT AddInstance( SkyRenderableInstance *pInstance, bool bTransparent = false);
69 SKYRESULT AddCloud( SkyCloud *pCloud);
70 SKYRESULT AddCloudInstance(SkyRenderableInstanceCloud *pInstance);
72 SKYRESULT AddMaterial( SkyMaterial *pMaterial);
73 SkyMaterial* GetMaterial( int iMaterialID);
74 SKYRESULT ActivateMaterial(int iMaterialID);
76 SKYRESULT AddLight( SkyLight *pLight);
77 SkyLight* GetLight( int iLightID);
79 //! Set the sky box for this scene.
80 // void SetSkyBox( SkyHeavens *pSkyBox) { _pSkyBox = pSkyBox; }
81 //! Set the terrain for this scene.
82 //void SetTerrain( SkyHeightField *pTerrain) { _pTerrain = pTerrain; }
84 //! Enable wireframe display of lights (for debugging).
85 void EnableDrawLights(bool bEnable) { _bDrawLights = bEnable; }
86 //! Enable wireframe display of bounding volume tree of clouds.
87 void EnableDrawBVTree(bool bEnable) { _bDrawTree = bEnable; }
89 //! Returns true if wireframe display of lights is enabled.
90 bool IsDrawLightsEnabled() const { return _bDrawLights; }
91 //! Returns true if wireframe display of the cloud bounding volume tree is enabled.
92 bool IsDrawBVTreeEnabled() const { return _bDrawTree; }
94 SKYRESULT Update( const Camera &cam);
95 SKYRESULT Display( const Camera &cam);
97 SKYRESULT RebuildCloudBVTree();
98 SKYRESULT ShadeClouds();
100 //! Force the illumination of all clouds to be recomputed in the next update.
101 void ForceReshadeClouds() { _bReshadeClouds = true; }
103 // sort instances in @a instances from back to front.
104 static void SortInstances(InstanceArray& instances, const Vec3f& vecSortPoint);
106 // load a set of clouds from an archive file.
107 SKYRESULT LoadClouds(SkyArchive& cloudArchive, float rScale = 1.0f, double latitude=0.0, double longitude=0.0);
109 protected: // datatypes
110 // Typedef the vectors into cleaner names
111 typedef std::vector<SkyRenderable*> ObjectArray;
113 typedef std::map<int, SkyMaterial*> MaterialSet;
114 typedef std::map<int, SkyLight*> LightSet;
116 typedef std::vector<SkyRenderableInstanceCloud*> CloudInstanceArray;
117 typedef std::vector<SkyCloud*> CloudArray;
118 typedef std::map<int, SkyContainerCloud*> ContainerCloudSet;
120 typedef SkyAABBTree<SkyRenderableInstanceCloud*> CloudBVTree;
122 typedef ObjectArray::iterator ObjectIterator;
123 typedef CloudArray::iterator CloudIterator;
124 typedef CloudInstanceArray::iterator CloudInstanceIterator;
125 typedef MaterialSet::iterator MaterialIterator;
126 typedef LightSet::iterator LightIterator;
127 typedef ContainerCloudSet::iterator ContainerSetIterator;
129 class InstanceComparator
132 bool operator()(SkyRenderableInstance* pA, SkyRenderableInstance *pB)
134 return ((*pA) < (*pB));
138 class ContainerComparator
141 bool operator()(SkyContainerCloud* pA, SkyContainerCloud *pB)
143 return ((*pA) < (*pB));
148 protected: // methods
152 void _SortClouds(CloudInstanceArray& clouds, const Vec3f& vecSortPoint);
154 void _ViewFrustumCullClouds(const Camera& cam, const CloudBVTree::Node *pNode);
155 void _VisualizeCloudBVTree(const Camera& cam, const CloudBVTree::Node *pNode);
157 SKYRESULT _ResolveVisibility(const Camera &cam);
158 bool _TestInsertInstanceIntoClouds(const Camera &cam,
159 const CloudBVTree::Node *pNode,
160 SkyRenderableInstance *pInstanceToInsert,
164 ObjectArray _objects;
166 InstanceArray _instances;
167 InstanceArray _transparentInstances;
168 InstanceArray _visibleInstances; //! @TODO: change this to "_freeInstances"
169 CloudInstanceArray _cloudInstances;
170 CloudInstanceArray _visibleCloudInstances;
171 ContainerCloudSet _containerClouds;
173 MaterialSet _materials;
176 SkyMaterial _wireframeMaterial; // used for rendering the wireframes for debugging
177 SkyMaterial _clearMaterial; // used to maintain state consistency when clearing.
178 CloudBVTree _cloudBVTree;
180 //SkyHeavens *_pSkyBox;
181 //SkyHeightField *_pTerrain;
185 bool _bReshadeClouds;
188 #endif //__SKYSCENEMANAGER_HPP__