]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/clouds3d/SkySceneManager.hpp
Clouds3D crashes because there is no Light
[simgear.git] / simgear / scene / sky / clouds3d / SkySceneManager.hpp
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 
12 // restrictions. 
13 //
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 
17 // implied warranty.
18 /**
19  * @file SkySceneManager.hpp
20  * 
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.
25  */
26 #ifndef __SKYSCENEMANAGER_HPP__
27 #define __SKYSCENEMANAGER_HPP__
28
29 // warning for truncation of template name for browse info
30 #pragma warning( disable : 4786)
31
32 #include "vec3f.hpp"
33 #include <vector>
34 #include <map>
35
36 #include "SkyUtil.hpp"
37 #include "SkySingleton.hpp"
38 #include "SkyRenderableInstance.hpp"
39 #include "SkyMaterial.hpp"
40 #include "SkyAABBTree.hpp"
41 #include "SkyRenderableInstanceCloud.hpp"
42
43 // forward to reduce unnecessary dependencies
44 class Camera;
45 class SkyRenderable;
46 class SkyRenderableInstance;
47 class SkyMaterial;
48 class SkyLight;
49 class SkyCloud;
50 //class SkyHeavens;
51 //class SkyHeightField;
52
53 //------------------------------------------------------------------------------
54 /**
55 * @class SkySceneManager
56 * @brief Manages all of the renderable objects and instances.
57 */
58 class SkySceneManager;  // Forward declaration
59
60 //! A singleton of the SkySceneManager.  Can only create the SceneManager with SceneManager::Instantiate();
61 typedef SkySingleton<SkySceneManager> SceneManager;
62
63 class SkySceneManager
64 {
65 public:
66   SKYRESULT     AddObject(   SkyRenderable *pObject);
67   SKYRESULT     AddInstance( SkyRenderableInstance *pInstance, bool bTransparent = false);
68   
69   SKYRESULT     AddCloud(    SkyCloud *pCloud);
70   SKYRESULT     AddCloudInstance(SkyRenderableInstanceCloud *pInstance);
71   
72   SKYRESULT     AddMaterial( SkyMaterial *pMaterial);
73   SkyMaterial*  GetMaterial( int iMaterialID);
74   SKYRESULT     ActivateMaterial(int iMaterialID);
75   
76   SKYRESULT     AddLight(    SkyLight *pLight);
77   SkyLight*     GetLight(    int iLightID);
78
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; }
83
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; }
88
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;    }
93   
94   SKYRESULT     Update( const Camera &cam);
95   SKYRESULT     Display( const Camera &cam);
96
97   SKYRESULT     RebuildCloudBVTree();
98   SKYRESULT     ShadeClouds();
99
100   //! Force the illumination of all clouds to be recomputed in the next update.
101   void          ForceReshadeClouds()                    { _bReshadeClouds = true; }
102
103   // sort instances in @a instances from back to front.
104   static void           SortInstances(InstanceArray& instances, const Vec3f& vecSortPoint);
105
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);
109   
110 protected: // datatypes
111   // Typedef the vectors into cleaner names
112   typedef std::vector<SkyRenderable*>               ObjectArray;
113   
114   typedef std::map<int, SkyMaterial*>               MaterialSet;
115   typedef std::map<int, SkyLight*>                  LightSet;
116
117   typedef std::vector<SkyRenderableInstanceCloud*>  CloudInstanceArray;
118   typedef std::vector<SkyCloud*>                    CloudArray;
119   typedef std::map<int, SkyContainerCloud*>         ContainerCloudSet;
120   
121   typedef SkyAABBTree<SkyRenderableInstanceCloud*>  CloudBVTree;
122   
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;
129   
130   class InstanceComparator
131   {
132   public:
133     bool operator()(SkyRenderableInstance* pA, SkyRenderableInstance *pB)
134     {
135       return ((*pA) < (*pB));
136     }
137   };
138
139   class ContainerComparator
140   {
141   public:
142     bool operator()(SkyContainerCloud* pA, SkyContainerCloud *pB)
143     {
144       return ((*pA) < (*pB));
145     }
146   };
147   
148
149 protected: // methods
150   SkySceneManager();
151   ~SkySceneManager();
152   
153   void                                  _SortClouds(CloudInstanceArray& clouds,  const Vec3f& vecSortPoint);
154
155   void          _ViewFrustumCullClouds(const Camera& cam, const CloudBVTree::Node *pNode);
156   void          _VisualizeCloudBVTree(const Camera& cam, const CloudBVTree::Node *pNode);
157   
158   SKYRESULT     _ResolveVisibility(const Camera &cam);
159   bool          _TestInsertInstanceIntoClouds(const Camera              &cam,
160                                               const CloudBVTree::Node   *pNode,
161                                               SkyRenderableInstance     *pInstanceToInsert,
162                                               bool                      bTransparent);
163   
164 private: // data
165   ObjectArray         _objects;
166   CloudArray          _clouds;
167   InstanceArray       _instances;
168   InstanceArray       _transparentInstances;
169   InstanceArray       _visibleInstances;      //! @TODO: change this to "_freeInstances"
170   CloudInstanceArray  _cloudInstances;
171   CloudInstanceArray  _visibleCloudInstances;
172   ContainerCloudSet   _containerClouds;
173
174   MaterialSet         _materials;
175   LightSet            _lights;
176
177   SkyMaterial         _wireframeMaterial;  // used for rendering the wireframes for debugging
178   SkyMaterial         _clearMaterial;      // used to maintain state consistency when clearing.
179   CloudBVTree         _cloudBVTree;
180
181   //SkyHeavens          *_pSkyBox;
182   //SkyHeightField      *_pTerrain;
183
184   bool                _bDrawLights;
185   bool                _bDrawTree;
186   bool                _bReshadeClouds;
187 };
188
189 #endif //__SKYSCENEMANAGER_HPP__