]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/bbcache.hxx
Merge branch 'ehofman/sound'
[simgear.git] / simgear / scene / sky / bbcache.hxx
1 // Billboard helper class
2 //
3 // Written by Harald JOHNSEN, started April 2005.
4 //
5 // Copyright (C) 2005  Harald JOHNSEN - hjohnsen@evc.net
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 //
22
23 #ifndef _BBCACHE_HXX
24 #define _BBCACHE_HXX
25
26
27 #include <plib/sg.h>
28 #include <simgear/screen/extensions.hxx>
29 #include <simgear/screen/RenderTexture.h>
30
31 /**
32  * Billboard helper class.
33  */
34 class SGBbCache {
35 private:
36
37         /**
38         * storage class for impostors state.
39         */
40         class bbInfo {
41         public:
42                 /// the texture used by this impostor
43                 GLuint  texID;
44                 /// the cloud owning this impostor
45                 int             cldID;
46                 float   angleX, angleY;
47                 // creation frame number for debug only
48                 int             frame;
49                 /// last time this entry was used
50                 int             frameUsed;
51                 /// dirty flag for lazy rebuild of impostor
52                 bool    needRedraw;
53         };
54
55         void freeTextureMemory(void);
56     /**
57      * Allocate and initialize the texture pool.
58      * @param count the number of texture to build
59      * @param textureDimension size in pixel of each texture
60      */
61         bool allocTextureMemory(int count, int textureDimension);
62
63         // a list of impostors
64         bbInfo  *bbList;
65         int             bbListCount;
66         int             textureWH;
67         int             cacheSizeKb;
68
69         // for debug only, stats
70         int             builtBBCount;
71         // count of generated BB during the current frame
72         int             builtBBframe;
73
74         long    frameNumber;
75         RenderTexture *rt;
76         bool    rtAvailable;
77
78 public:
79         SGBbCache(void);
80         ~SGBbCache(void);
81
82     /**
83      * Call this first to initialize the cache.
84      * @param cacheCount the number of texture to allocate
85      */
86         void init(int cacheCount);
87
88     /**
89      * Free one cache slot, usualy when the cached object is destroyed.
90      * @param bbId the impostor slot
91      * @param cldId the cloud identifier
92      */
93         void free(int bbId, int cldId);
94
95     /**
96      * Allocate a new impostor.
97      * @param cldId the cloud identifier
98      * @return an impostor slot
99      */
100         int alloc(int cldId);
101
102     /**
103      * Query the texture name associated with this cloud.
104      * @param bbId the impostor slot
105      * @param cldId the cloud identifier
106      * @return a texture name
107      */
108         GLuint QueryTexID(int cldId, int bbId);
109
110     /**
111      * Save the rendered texture from the current context to a new texture.
112      * @param bbId the impostor slot
113      */
114         void setTextureData(int bbId);
115
116     /**
117      * Start the rendering of a billboard in the RTT context.
118      */
119         void beginCapture(void);
120
121     /**
122      * Adjust the projection matrix of the RTT context to the size of the object.
123      * @param radius radius in meters of the object to draw
124      * @param dist_center distance between viewer and object
125      */
126         void setRadius(float radius, float dist_center);
127
128     /**
129      * Forget the RTT and go back to the previous rendering context.
130      */
131         void endCapture(void);
132
133     /**
134      * For debugging only, give the number of frames since the impostor was built.
135      * @param bbId the impostor slot
136      */
137         int queryImpostorAge(int bbId);
138
139     /**
140      * Can we still use this impostor ? Check versus view angles and load.
141      * @param bbId the impostor slot
142      * @param cloudId the cloud identifier
143      * @param angleY rotation needed to face the impostor
144      * @param angleX rotation needed to face the impostor
145      */
146         bool isBbValid( int cloudId, int bbId, float angleY, float angleX);
147
148     /**
149      * Save view angles of this billboard.
150      * @param bbId the impostor slot
151      * @param cloudId the cloud identifier
152      * @param angleY rotation needed to face the impostor
153      * @param angleX rotation needed to face the impostor
154      */
155         void setReference( int cloudId, int bbId, float angleY, float angleX);
156
157     /**
158      * Prepare the cache for the rendering of a new frame.
159      * Do some garbage collect of unused impostors
160      */
161         void startNewFrame(void);
162
163     /**
164      * Alloc the impostors texture memory given the size of the memory pool.
165          * If sizeKb == 0 then the memory pool is freed and impostors are disabled
166      * @param sizeKb size of the texture pool in K
167      */
168         bool setCacheSize(int sizeKb);
169
170     /**
171      * Alloc the impostors texture memory given the count and size of texture.
172          * If count == 0 then the memory pool is freed and impostors are disabled
173      * @param count number of texture to allocate
174      * @param textureDimension size of each texture in pixels
175      */
176         bool setCacheSize(int count, int textureDimension);
177
178         bool isRttAvailable(void) { return rtAvailable; }
179
180     /**
181      * Force all impostors to be rebuilt.
182      */
183         void invalidateCache(void);
184
185     /**
186      * Flag the impostor for a lazy update.
187      * @param bbId the impostor slot
188      * @param cldId the cloud identifier
189      */
190         void invalidate(int cldId, int bbId);
191
192     /**
193      * Return the size of the memory pool used by texture impostors.
194      * @return size of the memory pool in Kb
195      */
196         int queryCacheSize(void);
197
198     /**
199      * Maximum number of impostor to regen each frame.
200      * If we can't update all of them we will do that in the next frame
201      */
202         int maxImpostorRegenFrame;
203 };
204
205 #endif // _BBCACHE_HXX