]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/bbcache.hxx
- new FSF addresses
[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 <plib/ssg.h>
29 #include <simgear/screen/extensions.hxx>
30 #include <simgear/screen/RenderTexture.h>
31
32 /**
33  * Billboard helper class.
34  */
35 class SGBbCache {
36 private:
37
38         /**
39         * storage class for impostors state.
40         */
41         class bbInfo {
42         public:
43                 /// the texture used by this impostor
44                 GLuint  texID;
45                 /// the cloud owning this impostor
46                 int             cldID;
47                 float   angleX, angleY;
48                 // creation frame number for debug only
49                 int             frame;
50                 /// last time this entry was used
51                 int             frameUsed;
52                 /// dirty flag for lazy rebuild of impostor
53                 bool    needRedraw;
54         };
55
56         void freeTextureMemory(void);
57     /**
58      * Allocate and initialize the texture pool.
59      * @param count the number of texture to build
60      * @param textureDimension size in pixel of each texture
61      */
62         bool allocTextureMemory(int count, int textureDimension);
63
64         // a list of impostors
65         bbInfo  *bbList;
66         int             bbListCount;
67         int             textureWH;
68         int             cacheSizeKb;
69
70         // for debug only, stats
71         int             builtBBCount;
72         // count of generated BB during the current frame
73         int             builtBBframe;
74
75         long    frameNumber;
76         RenderTexture *rt;
77         bool    rtAvailable;
78
79 public:
80         SGBbCache(void);
81         ~SGBbCache(void);
82
83     /**
84      * Call this first to initialize the cache.
85      * @param cacheCount the number of texture to allocate
86      */
87         void init(int cacheCount);
88
89     /**
90      * Free one cache slot, usualy when the cached object is destroyed.
91      * @param bbId the impostor slot
92      * @param cldId the cloud identifier
93      */
94         void free(int bbId, int cldId);
95
96     /**
97      * Allocate a new impostor.
98      * @param cldId the cloud identifier
99      * @return an impostor slot
100      */
101         int alloc(int cldId);
102
103     /**
104      * Query the texture name associated with this cloud.
105      * @param bbId the impostor slot
106      * @param cldId the cloud identifier
107      * @return a texture name
108      */
109         GLuint QueryTexID(int cldId, int bbId);
110
111     /**
112      * Save the rendered texture from the current context to a new texture.
113      * @param bbId the impostor slot
114      */
115         void setTextureData(int bbId);
116
117     /**
118      * Start the rendering of a billboard in the RTT context.
119      */
120         void beginCapture(void);
121
122     /**
123      * Adjust the projection matrix of the RTT context to the size of the object.
124      * @param radius radius in meters of the object to draw
125      * @param dist_center distance between viewer and object
126      */
127         void setRadius(float radius, float dist_center);
128
129     /**
130      * Forget the RTT and go back to the previous rendering context.
131      */
132         void endCapture(void);
133
134     /**
135      * For debugging only, give the number of frames since the impostor was built.
136      * @param bbId the impostor slot
137      */
138         int queryImpostorAge(int bbId);
139
140     /**
141      * Can we still use this impostor ? Check versus view angles and load.
142      * @param bbId the impostor slot
143      * @param cloudId the cloud identifier
144      * @param angleY rotation needed to face the impostor
145      * @param angleX rotation needed to face the impostor
146      */
147         bool isBbValid( int cloudId, int bbId, float angleY, float angleX);
148
149     /**
150      * Save view angles of this billboard.
151      * @param bbId the impostor slot
152      * @param cloudId the cloud identifier
153      * @param angleY rotation needed to face the impostor
154      * @param angleX rotation needed to face the impostor
155      */
156         void setReference( int cloudId, int bbId, float angleY, float angleX);
157
158     /**
159      * Prepare the cache for the rendering of a new frame.
160      * Do some garbage collect of unused impostors
161      */
162         void startNewFrame(void);
163
164     /**
165      * Alloc the impostors texture memory given the size of the memory pool.
166          * If sizeKb == 0 then the memory pool is freed and impostors are disabled
167      * @param sizeKb size of the texture pool in K
168      */
169         bool setCacheSize(int sizeKb);
170
171     /**
172      * Alloc the impostors texture memory given the count and size of texture.
173          * If count == 0 then the memory pool is freed and impostors are disabled
174      * @param count number of texture to allocate
175      * @param textureDimension size of each texture in pixels
176      */
177         bool setCacheSize(int count, int textureDimension);
178
179         bool isRttAvailable(void) { return rtAvailable; }
180
181     /**
182      * Force all impostors to be rebuilt.
183      */
184         void invalidateCache(void);
185
186     /**
187      * Flag the impostor for a lazy update.
188      * @param bbId the impostor slot
189      * @param cldId the cloud identifier
190      */
191         void invalidate(int cldId, int bbId);
192
193     /**
194      * Return the size of the memory pool used by texture impostors.
195      * @return size of the memory pool in Kb
196      */
197         int queryCacheSize(void);
198
199     /**
200      * Maximum number of impostor to regen each frame.
201      * If we can't update all of them we will do that in the next frame
202      */
203         int maxImpostorRegenFrame;
204 };
205
206 #endif // _BBCACHE_HXX