]> git.mxchange.org Git - simgear.git/blob - simgear/scene/sky/bbcache.cxx
da84fbce25857432e0638e1f1408b58c7f0a2136
[simgear.git] / simgear / scene / sky / bbcache.cxx
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, 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
20 //
21 //
22
23 #ifdef HAVE_CONFIG_H
24 #  include <simgear_config.h>
25 #endif
26
27 #include <simgear/compiler.h>
28 #include <simgear/debug/logstream.hxx>
29
30 #include <plib/sg.h>
31 #include <plib/ssg.h>
32 #include <simgear/screen/extensions.hxx>
33 #include <simgear/screen/RenderTexture.h>
34 #include SG_GLU_H
35
36 #include "bbcache.hxx"
37
38
39 /*
40 memory usage :
41 size          1 tex       8 tex       32 tex      64 tex
42 64x64x4        16k        128k        512k         1Mo
43 128x128x4      64k        512k        2Mo          4Mo
44 256x256x4     256k        2Mo         8Mo         16Mo
45 */
46
47 void SGBbCache::freeTextureMemory(void) {
48
49         if( bbListCount ) {
50                 for(int i = 0 ; i < bbListCount ; i++) {
51                         bbList[i].cldID = 0;
52                         if(bbList[i].texID)
53                                 glDeleteTextures(1, & bbList[i].texID);
54                 }
55                 delete [] bbList;
56         }
57         bbListCount = 0;
58         cacheSizeKb = 0;
59         textureWH   = 0;
60 }
61
62 bool SGBbCache::allocTextureMemory(int cacheCount, int textureDimension) {
63         textureWH = textureDimension;
64         bbListCount = cacheCount;
65         bbList = new bbInfo[bbListCount];
66         for(int i = 0 ; i < bbListCount ; i++) {
67                 bbList[i].cldID = 0;
68                 bbList[i].texID = 0;
69         glGenTextures(1, &bbList[i].texID);
70         glBindTexture(GL_TEXTURE_2D, bbList[i].texID);
71         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 
72                          textureDimension, textureDimension, 0, GL_RGB, GL_FLOAT, NULL);
73
74         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
75         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
76         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
77         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
78         }
79     glBindTexture(GL_TEXTURE_2D, 0);
80         cacheSizeKb = (textureDimension * textureDimension * 4);
81         cacheSizeKb *= cacheCount;
82         cacheSizeKb /= 1024;
83         if(rtAvailable) {
84                 if( rt->BeginCapture() ) {
85                         glViewport(0, 0, textureDimension, textureDimension);
86                         rt->EndCapture();
87                 }
88         }
89         return true;
90 }
91
92 SGBbCache::SGBbCache(void) :
93         bbListCount(0),
94         cacheSizeKb(0),
95         textureWH(0),
96         builtBBCount(0),
97         rt(0),
98         rtAvailable(false),
99         frameNumber(0),
100         maxImpostorRegenFrame(20)
101 {
102 }
103
104 SGBbCache::~SGBbCache(void) {
105         if(rt)
106                 delete rt;
107         freeTextureMemory();
108 }
109
110
111 void SGBbCache::init(int cacheCount) {
112
113         rt = new RenderTexture();
114         // don't use default rtt on nvidia/win because of poor performance of glCopyTexSubImage2D
115         // wihtout default pattrib params - see opengl forum
116         rt->Reset("rgba tex2D ctt");
117 //      rt->Reset("rgba tex2D");
118         if( rt->Initialize(256, 256, true) ) {
119                 SG_LOG(SG_ALL, SG_INFO, "bbcache:Initialize sucessfull");
120                 if (rt->BeginCapture())
121                 {
122                         SG_LOG(SG_ALL, SG_INFO, "bbcache:BeginCapture sucessfull, RTT available");
123                         rtAvailable = true;
124                         glViewport(0, 0, 256, 256);
125                         glMatrixMode(GL_PROJECTION);
126                         glLoadIdentity();
127                         gluPerspective(60.0,  1, 1, 5.0);
128                         glMatrixMode(GL_MODELVIEW);
129                         glLoadIdentity();
130                         glDisable(GL_LIGHTING);
131                         glEnable(GL_COLOR_MATERIAL);
132                         glDisable(GL_CULL_FACE);
133                         glDisable(GL_FOG);
134                         glDisable(GL_DEPTH_TEST);
135                         glClearColor(0.0, 0.0, 0.0, 0.0);
136                         glEnable(GL_TEXTURE_2D);
137                         glEnable(GL_ALPHA_TEST);
138                         glAlphaFunc(GL_GREATER, 0.0f);
139                         glEnable(GL_SMOOTH);
140                         glEnable(GL_BLEND);
141                         glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
142
143                         rt->EndCapture();
144                 } else
145                         SG_LOG(SG_ALL, SG_WARN, "bbcache:BeginCapture failed, RTT not available for 3D clouds");
146         } else
147                 SG_LOG(SG_ALL, SG_WARN, "bbcache:Initialize failed, RTT not available for 3D clouds");
148         if( cacheCount )
149                 allocTextureMemory( cacheCount, 64 );
150
151 }
152
153
154 bool SGBbCache::setCacheSize(int count, int textureDimension) {
155         if( count < 0 || count > 500)
156                 return false;
157         freeTextureMemory();
158         if( count == 0)
159                 return true;
160
161         // only allow some reasonable dimensions
162         switch(textureDimension) {
163                 case 0:
164                         // default size
165                         textureDimension = 256;
166                         break;
167                 case 64:
168                 case 128:
169                 case 256:
170                         break;
171                 case 512:
172                         // rt is 256 so higher texture size has no meaning
173                         textureDimension = 256;
174                         break;
175                 default:
176                         textureDimension = 128;
177                         break;
178         }
179         return allocTextureMemory( count, textureDimension);
180 }
181
182
183 bool SGBbCache::setCacheSize(int sizeKb) {
184         if( sizeKb < 0 || sizeKb > 256*1024)
185                 return false;
186         freeTextureMemory();
187         if( sizeKb == 0)
188                 return true;
189         int count = 1;
190         int textureDimension = 256;
191         if( sizeKb >= 8*1024 ) {
192                 // more than 32 256x256 textures
193                 textureDimension = 256;
194         } else  if( sizeKb >= 2*1024 ) {
195                 // more than 32 128x128 textures
196                 textureDimension = 128;
197         } else  {
198                 // don't go under 64x64 textures
199                 textureDimension = 64;
200         }
201         count = (sizeKb * 1024) / (textureDimension * textureDimension * 4);
202         if(count == 0)
203                 count = 1;
204         return allocTextureMemory( count, textureDimension);
205 }
206
207 int SGBbCache::queryCacheSize(void) {
208         return cacheSizeKb;
209 }
210
211 void SGBbCache::free(int bbId, int cldId) {
212         if( bbId < 0 || bbId >= bbListCount )
213                 return;
214         if( bbList[bbId].cldID != cldId )
215                 return;
216         bbList[bbId].cldID = 0;
217 }
218
219 int SGBbCache::alloc(int cldId) {
220         // pretend we have no more texture if render to texture is not available
221         if( ! rtAvailable )
222                 return -1;
223         for(int i = 0 ; i < bbListCount ; i++) {
224                 if( (bbList[i].cldID == 0) && (bbList[i].texID != 0) ) {
225             bbList[i].cldID = cldId;
226                         bbList[i].angleX = -999;
227                         bbList[i].angleY = -999;
228                         bbList[i].frameUsed = 0;
229                         bbList[i].needRedraw = true;
230                         return i;
231                 }
232         }
233         return -1;
234 }
235
236 GLuint SGBbCache::QueryTexID(int cldId, int bbId) {
237         if( bbId < 0 || bbId >= bbListCount )
238                 return 0;
239         if( bbList[bbId].cldID != cldId )
240                 return 0;
241         return bbList[bbId].texID;
242 }
243
244 int SGBbCache::queryImpostorAge(int bbId) {
245         if( bbId < 0 || bbId >= bbListCount )
246                 return 0;
247         return frameNumber - bbList[bbId].frame;
248 }
249
250 void SGBbCache::beginCapture(void) {
251
252         rt->BeginCapture();
253
254         glClear(GL_COLOR_BUFFER_BIT);
255
256 }
257
258
259
260 void SGBbCache::setRadius(float radius, float dist_center) {
261         float border;
262         //set viewport to texture resolution
263         //glViewport(0, 0, 256, 256);
264         glMatrixMode(GL_PROJECTION);
265     glLoadIdentity();
266
267     float near_ = dist_center - radius;
268     float far_ = dist_center + radius;
269         if( near_ <= 0 ) {
270         // we are in trouble
271         glFrustum(-1, 1, -1, 1, 1, 1 + radius * 2);
272         } else {
273         border = (near_ * radius) / sqrt(dist_center * dist_center - radius * radius);
274         glFrustum(-border, border, -border, border, near_, far_);
275         }
276     glMatrixMode(GL_MODELVIEW);
277     glLoadIdentity();
278 }
279 void SGBbCache::setTextureData(int bbId) {
280         if( bbId < 0 || bbId >= bbListCount )
281                 return;
282
283     glBindTexture(GL_TEXTURE_2D, bbList[bbId].texID);
284         glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureWH, textureWH);
285  
286 //    bbList[bbId].angleX = angleX;
287 //    bbList[bbId].angleY = angleY;
288     bbList[bbId].frame = frameNumber;
289         bbList[bbId].frameUsed = frameNumber;
290         bbList[bbId].needRedraw = false;
291     builtBBCount ++;
292         builtBBframe ++;
293 }
294
295 void SGBbCache::endCapture(void) {
296
297         rt->EndCapture();
298 //    glBindTexture(GL_TEXTURE_2D, rt->GetTextureID() );
299
300 }
301
302
303 bool SGBbCache::isBbValid( int cldId, int bbId, float angleY, float angleX) {
304         if( bbId < 0 || bbId >= bbListCount )
305                 return false;
306         if( bbList[bbId].cldID != cldId )
307                 return false;
308
309         // it was just allocated
310         if( bbList[bbId].frameUsed == 0)
311                 return false;
312
313         // we reuse old impostor to speed up things
314         if( builtBBframe >= maxImpostorRegenFrame )
315                 return true;
316
317         if( bbList[bbId].needRedraw )
318                 return false;
319
320 //    if( fabs(angleY - bbList[bbId].angleY) >= 4.0 )
321 //        return false;
322
323 //    if( fabs(angleX - bbList[bbId].angleX) >= 4.0 )
324 //        return false;
325
326         bbList[bbId].frameUsed = frameNumber;
327         return true;
328 }
329
330 // TODO:this is not the right way to handle that
331 void SGBbCache::setReference( int cldId, int bbId, float angleY, float angleX) {
332         if( bbId < 0 || bbId >= bbListCount )
333                 return;
334         if( bbList[bbId].cldID != cldId )
335                 return;
336         bbList[bbId].angleX = angleX;
337         bbList[bbId].angleY = angleY;
338 }
339
340 void SGBbCache::startNewFrame(void) {
341         builtBBframe = 0;
342         // TOTO:find reasonable value
343         int minFrameNumber = frameNumber - 100;
344         frameNumber++;
345         // cleanup of unused enties
346         for( int bbId = 0 ; bbId < bbListCount ; bbId++)
347                 if( (bbList[bbId].cldID != 0) && (bbList[bbId].frameUsed < minFrameNumber) ) {
348                         // entry is now free
349                         bbList[bbId].cldID = 0;
350                 }
351 }
352
353 // force all impostors to be rebuilt, this will enventually be done over several frames
354 void SGBbCache::invalidateCache(void) {
355
356         for( int bbId = 0 ; bbId < bbListCount ; bbId++)
357 //              bbList[bbId].cldID = 0;
358                 bbList[bbId].needRedraw = true;
359 }
360
361 // flag the impostor for a lazy update
362 void SGBbCache::invalidate(int cldId, int bbId) {
363         if( bbId < 0 || bbId >= bbListCount )
364                 return;
365         if( bbList[bbId].cldID != cldId )
366                 return;
367         bbList[bbId].needRedraw = true;
368 }
369