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(unsigned char *data, unsigned int size, float rScale = 1.0f, double latitude=0.0, double longitude=0.0);
108 SKYRESULT LoadClouds(SkyArchive& cloudArchive, float rScale = 1.0f, double latitude=0.0, double longitude=0.0);
110 protected: // datatypes
111 // Typedef the vectors into cleaner names
112 typedef std::vector<SkyRenderable*> ObjectArray;
114 typedef std::map<int, SkyMaterial*> MaterialSet;
115 typedef std::map<int, SkyLight*> LightSet;
117 typedef std::vector<SkyRenderableInstanceCloud*> CloudInstanceArray;
118 typedef std::vector<SkyCloud*> CloudArray;
119 typedef std::map<int, SkyContainerCloud*> ContainerCloudSet;
121 typedef SkyAABBTree<SkyRenderableInstanceCloud*> CloudBVTree;
123 typedef ObjectArray::iterator ObjectIterator;
124 typedef CloudArray::iterator CloudIterator;
125 typedef CloudInstanceArray::iterator CloudInstanceIterator;
126 typedef MaterialSet::iterator MaterialIterator;
127 typedef LightSet::iterator LightIterator;
128 typedef ContainerCloudSet::iterator ContainerSetIterator;
130 class InstanceComparator
133 bool operator()(SkyRenderableInstance* pA, SkyRenderableInstance *pB)
135 return ((*pA) < (*pB));
139 class ContainerComparator
142 bool operator()(SkyContainerCloud* pA, SkyContainerCloud *pB)
144 return ((*pA) < (*pB));
149 protected: // methods
153 void _SortClouds(CloudInstanceArray& clouds, const Vec3f& vecSortPoint);
155 void _ViewFrustumCullClouds(const Camera& cam, const CloudBVTree::Node *pNode);
156 void _VisualizeCloudBVTree(const Camera& cam, const CloudBVTree::Node *pNode);
158 SKYRESULT _ResolveVisibility(const Camera &cam);
159 bool _TestInsertInstanceIntoClouds(const Camera &cam,
160 const CloudBVTree::Node *pNode,
161 SkyRenderableInstance *pInstanceToInsert,
165 ObjectArray _objects;
167 InstanceArray _instances;
168 InstanceArray _transparentInstances;
169 InstanceArray _visibleInstances; //! @TODO: change this to "_freeInstances"
170 CloudInstanceArray _cloudInstances;
171 CloudInstanceArray _visibleCloudInstances;
172 ContainerCloudSet _containerClouds;
174 MaterialSet _materials;
177 SkyMaterial _wireframeMaterial; // used for rendering the wireframes for debugging
178 SkyMaterial _clearMaterial; // used to maintain state consistency when clearing.
179 CloudBVTree _cloudBVTree;
181 //SkyHeavens *_pSkyBox;
182 //SkyHeightField *_pTerrain;
186 bool _bReshadeClouds;
189 #endif //__SKYSCENEMANAGER_HPP__