1 // Billboard helper class
3 // Written by Harald JOHNSEN, started April 2005.
5 // Copyright (C) 2005 Harald JOHNSEN - hjohnsen@evc.net
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.
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.
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
24 # include <simgear_config.h>
27 #include <simgear/compiler.h>
28 #include <simgear/debug/logstream.hxx>
32 #include <simgear/screen/extensions.hxx>
33 #include <simgear/screen/RenderTexture.h>
36 #include "bbcache.hxx"
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
47 void SGBbCache::freeTextureMemory(void) {
50 for(int i = 0 ; i < bbListCount ; i++) {
53 glDeleteTextures(1, & bbList[i].texID);
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++) {
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);
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);
79 glBindTexture(GL_TEXTURE_2D, 0);
80 cacheSizeKb = (textureDimension * textureDimension * 4);
81 cacheSizeKb *= cacheCount;
84 if( rt->BeginCapture() ) {
85 glViewport(0, 0, textureDimension, textureDimension);
92 SGBbCache::SGBbCache(void) :
100 maxImpostorRegenFrame(20)
104 SGBbCache::~SGBbCache(void) {
111 void SGBbCache::init(int cacheCount) {
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())
122 SG_LOG(SG_ALL, SG_INFO, "bbcache:BeginCapture sucessfull, RTT available");
124 glViewport(0, 0, 256, 256);
125 glMatrixMode(GL_PROJECTION);
127 gluPerspective(60.0, 1, 1, 5.0);
128 glMatrixMode(GL_MODELVIEW);
130 glDisable(GL_LIGHTING);
131 glEnable(GL_COLOR_MATERIAL);
132 glDisable(GL_CULL_FACE);
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);
141 glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
145 SG_LOG(SG_ALL, SG_WARN, "bbcache:BeginCapture failed, RTT not available for 3D clouds");
147 SG_LOG(SG_ALL, SG_WARN, "bbcache:Initialize failed, RTT not available for 3D clouds");
149 allocTextureMemory( cacheCount, 64 );
154 bool SGBbCache::setCacheSize(int count, int textureDimension) {
155 if( count < 0 || count > 500)
161 // only allow some reasonable dimensions
162 switch(textureDimension) {
165 textureDimension = 256;
172 // rt is 256 so higher texture size has no meaning
173 textureDimension = 256;
176 textureDimension = 128;
179 return allocTextureMemory( count, textureDimension);
183 bool SGBbCache::setCacheSize(int sizeKb) {
184 if( sizeKb < 0 || sizeKb > 256*1024)
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;
198 // don't go under 64x64 textures
199 textureDimension = 64;
201 count = (sizeKb * 1024) / (textureDimension * textureDimension * 4);
204 return allocTextureMemory( count, textureDimension);
207 int SGBbCache::queryCacheSize(void) {
211 void SGBbCache::free(int bbId, int cldId) {
212 if( bbId < 0 || bbId >= bbListCount )
214 if( bbList[bbId].cldID != cldId )
216 bbList[bbId].cldID = 0;
219 int SGBbCache::alloc(int cldId) {
220 // pretend we have no more texture if render to texture is not available
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;
236 GLuint SGBbCache::QueryTexID(int cldId, int bbId) {
237 if( bbId < 0 || bbId >= bbListCount )
239 if( bbList[bbId].cldID != cldId )
241 return bbList[bbId].texID;
244 int SGBbCache::queryImpostorAge(int bbId) {
245 if( bbId < 0 || bbId >= bbListCount )
247 return frameNumber - bbList[bbId].frame;
250 void SGBbCache::beginCapture(void) {
254 glClear(GL_COLOR_BUFFER_BIT);
260 void SGBbCache::setRadius(float radius, float dist_center) {
262 //set viewport to texture resolution
263 //glViewport(0, 0, 256, 256);
264 glMatrixMode(GL_PROJECTION);
267 float near_ = dist_center - radius;
268 float far_ = dist_center + radius;
271 glFrustum(-1, 1, -1, 1, 1, 1 + radius * 2);
273 border = (near_ * radius) / sqrt(dist_center * dist_center - radius * radius);
274 glFrustum(-border, border, -border, border, near_, far_);
276 glMatrixMode(GL_MODELVIEW);
279 void SGBbCache::setTextureData(int bbId) {
280 if( bbId < 0 || bbId >= bbListCount )
283 glBindTexture(GL_TEXTURE_2D, bbList[bbId].texID);
284 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureWH, textureWH);
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;
295 void SGBbCache::endCapture(void) {
298 // glBindTexture(GL_TEXTURE_2D, rt->GetTextureID() );
303 bool SGBbCache::isBbValid( int cldId, int bbId, float angleY, float angleX) {
304 if( bbId < 0 || bbId >= bbListCount )
306 if( bbList[bbId].cldID != cldId )
309 // it was just allocated
310 if( bbList[bbId].frameUsed == 0)
313 // we reuse old impostor to speed up things
314 if( builtBBframe >= maxImpostorRegenFrame )
317 if( bbList[bbId].needRedraw )
320 // if( fabs(angleY - bbList[bbId].angleY) >= 4.0 )
323 // if( fabs(angleX - bbList[bbId].angleX) >= 4.0 )
326 bbList[bbId].frameUsed = frameNumber;
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 )
334 if( bbList[bbId].cldID != cldId )
336 bbList[bbId].angleX = angleX;
337 bbList[bbId].angleY = angleY;
340 void SGBbCache::startNewFrame(void) {
342 // TOTO:find reasonable value
343 int minFrameNumber = frameNumber - 100;
345 // cleanup of unused enties
346 for( int bbId = 0 ; bbId < bbListCount ; bbId++)
347 if( (bbList[bbId].cldID != 0) && (bbList[bbId].frameUsed < minFrameNumber) ) {
349 bbList[bbId].cldID = 0;
353 // force all impostors to be rebuilt, this will enventually be done over several frames
354 void SGBbCache::invalidateCache(void) {
356 for( int bbId = 0 ; bbId < bbListCount ; bbId++)
357 // bbList[bbId].cldID = 0;
358 bbList[bbId].needRedraw = true;
361 // flag the impostor for a lazy update
362 void SGBbCache::invalidate(int cldId, int bbId) {
363 if( bbId < 0 || bbId >= bbListCount )
365 if( bbList[bbId].cldID != cldId )
367 bbList[bbId].needRedraw = true;