]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/clouds3d/SkyTextureManager.hpp
Clouds3D crashes because there is no Light
[simgear.git] / simgear / scene / sky / clouds3d / SkyTextureManager.hpp
1 //------------------------------------------------------------------------------
2 // File : SkyTextureManager.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 implied warranty.
17 /**
18  * @file SkyTextureManager.hpp
19  * 
20  * Definition of a manager that keeps track of texture locations and sharing of texture files.                                          |
21  */
22 #ifndef SKYTEXTUREMANAGER_HPP
23 #define SKYTEXTUREMANAGER_HPP
24
25 // #pragma warning( disable : 4786)
26
27 #include "SkySingleton.hpp"
28 #include "SkyTexture.hpp"
29 #include <string>
30 #include <list>
31 #include <map>
32
33 using namespace std;
34
35 // forward declaration for singleton
36 class SkyTextureManager;
37
38 //! A singleton of the SkyTextureManager.  Can only create the TextureManager with TextureManager::Instantiate();
39 typedef SkySingleton<SkyTextureManager> TextureManager;
40
41 //------------------------------------------------------------------------------
42 /**
43  * @class SkyTextureManager
44  * @brief A resource manager for textures.
45  * 
46  * This manager allows textures to be shared.  It keeps a mapping of 
47  * filenames to texture objects, and makes it easy to use the same texture
48  * for multiple objects without the objects having to be aware of the 
49  * sharing.  Supports cube map textures, and 2D textures.  Can also be used
50  * to "clone textures", which creates unmanaged texture objects from files 
51  * that are not kept in the mapping, and thus are not shared.
52  */
53 class SkyTextureManager
54 {
55 public: // types
56   typedef list<string> StringList;
57
58 public: // methods
59   //.-------------------------------------------------------------------------.
60   //|  Paths to texture directories
61   //.-------------------------------------------------------------------------.
62   void              AddPath(const string& path);
63   //! Return the list of texture paths that will be searched by Get2DTexture() and Get3DTexture().
64   const StringList& GetPaths() const                    { return _texturePaths;  }
65   //! Clear the list of texture paths that will be searched by Get2DTexture() and Get3DTexture().
66   void              ClearPaths()                        { _texturePaths.clear(); }
67   
68   //.-------------------------------------------------------------------------.
69   //|  Texture loading
70   //.-------------------------------------------------------------------------.
71   SKYRESULT         Get2DTexture(         const string  &filename, 
72                                           SkyTexture&   texture,
73                                           bool          bMipmap             = false); 
74   SKYRESULT         Get3DTexture(         const string  &filename, 
75                                           SkyTexture&   texture,
76                                           unsigned int  iDepth,
77                                           bool          bMipmap             = false,
78                                           bool          bLoadFromSliceFiles = false);
79   SKYRESULT         GetCubeMapTexture(    const string  &filename, 
80                                           SkyTexture&   texture,
81                                           bool          bMipmap             = false);
82
83   //.-------------------------------------------------------------------------.
84   //|  Texture cloning: create a duplicate texture object: not added to set!
85   //.-------------------------------------------------------------------------.
86   SKYRESULT         Clone2DTexture(       const string  &filename, 
87                                           SkyTexture&   texture,
88                                           bool          bMipmap             = false);
89   SKYRESULT         Clone3DTexture(       const string  &filename, 
90                                           SkyTexture&   texture,
91                                           unsigned int  iDepth,
92                                           bool          bMipmap             = false,
93                                           bool          bLoadFromSliceFiles = false );
94   SKYRESULT         CloneCubeMapTexture(  const string  &filename, 
95                                           SkyTexture&   texture,
96                                           bool          bMipmap             = false);
97
98   //.-------------------------------------------------------------------------.
99   //|  Texture Object Creation: not added to the texture set (no filename!)
100   //.-------------------------------------------------------------------------.
101   inline SKYRESULT  Create2DTextureObject(SkyTexture    &texture, 
102                                           unsigned int  iWidth, 
103                                           unsigned int  iHeight,
104                                           unsigned int  iFormat,
105                                           unsigned char *pData);
106   inline SKYRESULT  Create3DTextureObject(SkyTexture    &texture, 
107                                           unsigned int  iWidth, 
108                                           unsigned int  iHeight,
109                                           unsigned int  iDepth,
110                                           unsigned int  iFormat,
111                                           unsigned char *pData);
112
113   //.-------------------------------------------------------------------------.
114   //|  Texture Object Destruction: use this because texture objects are structs
115   //|  that use shallow copies!
116   //.-------------------------------------------------------------------------.
117   static void       DestroyTextureObject( SkyTexture &texture);
118
119   
120 protected:
121   SkyTextureManager(bool bSlice3DTextures = false);
122   ~SkyTextureManager();
123
124   SKYRESULT  _Create2DTextureObject(      SkyTexture    &texture, 
125                                           unsigned int  iWidth, 
126                                           unsigned int  iHeight,
127                                           unsigned int  iFormat,
128                                           unsigned char *pData);
129   SKYRESULT  _Create3DTextureObject(      SkyTexture    &texture, 
130                                           unsigned int  iWidth, 
131                                           unsigned int  iHeight,
132                                           unsigned int  iDepth,
133                                           unsigned int  iFormat,
134                                           unsigned char *pData);
135 private:
136   typedef list<SkyTexture>        TextureList;
137   typedef map<string, SkyTexture> TextureSet;
138   typedef TextureSet::iterator    TextureIterator;
139   
140   //.-------------------------------------------------------------------------.
141   //|  Data
142   //.-------------------------------------------------------------------------.
143   
144   // paths searched for textures specified by filename.
145   StringList  _texturePaths;
146   
147   // cached textures
148   TextureSet  _textures;        // loaded textures
149   // textures created directly, not loaded from file and cached.
150   TextureList _uncachedTextures;
151   
152   // if this is true, then 3D textures will be represented as a set of 2D slices.
153   static bool s_bSlice3DTextures;
154 };
155
156
157 //------------------------------------------------------------------------------
158 // Function               : SkyTextureManager::Create2DTextureObject
159 // Description      : 
160 //------------------------------------------------------------------------------
161 /**
162  * @fn SkyTextureManager::Create2DTextureObject(SkyTexture &texture, unsigned int iWidth, unsigned int iHeight, unsigned int iFormat, unsigned char *pData)
163  * @brief Creates a 2D texture.
164  * 
165  * Creates an OpenGL texture object and returns its ID and dimensions in a SkyTexture structure.
166  * This texture will be deleted by the texture manager at shutdown.
167  *
168  */ 
169 inline SKYRESULT SkyTextureManager::Create2DTextureObject(SkyTexture &texture, 
170                                                           unsigned int iWidth, 
171                                                           unsigned int iHeight, 
172                                                           unsigned int iFormat, 
173                                                           unsigned char *pData)
174 {
175   SKYRESULT retval = _Create2DTextureObject(texture, iWidth, iHeight, iFormat, pData);
176   if SKYSUCCEEDED(retval)
177     _uncachedTextures.push_back(texture);
178   return retval;
179 }
180   
181
182 //------------------------------------------------------------------------------
183 // Function               : SkyTextureManager::Create3DTextureObject
184 // Description      : 
185 //------------------------------------------------------------------------------
186 /**
187  * @fn SkyTextureManager::Create3DTextureObject(SkyTexture &texture, unsigned int iWidth, unsigned int iHeight, unsigned int iDepth, unsigned int iFormat, unsigned char *pData)
188  * @brief Creates a 3D texture.
189  * 
190  * Creates an OpenGL texture object and returns its ID and dimensions in a SkyTexture structure.
191  * This texture will be deleted by the texture manager at shutdown, and should not be destroyed 
192  * by the user.
193  *
194  */ 
195 inline SKYRESULT SkyTextureManager::Create3DTextureObject(SkyTexture &texture, 
196                                                           unsigned int iWidth, 
197                                                           unsigned int iHeight, 
198                                                           unsigned int iDepth, 
199                                                           unsigned int iFormat, 
200                                                           unsigned char *pData)
201 {
202   SKYRESULT retval = _Create3DTextureObject(texture, iWidth, iHeight, iDepth, iFormat, pData);
203   if SKYSUCCEEDED(retval)
204     _uncachedTextures.push_back(texture);
205   return retval;
206 }
207
208 #endif //QGLVUTEXTUREMANAGER_HPP