]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/sky/bbcache.cxx
- remove the SG_GLxxxx_H #defines, since OSG provides its own versions
[simgear.git] / simgear / scene / sky / bbcache.cxx
index ad253cda7eb40d1c66b2a3987f412f0dae769500..66b272750fc990337d0ac9cda689b1c0dec6bffd 100644 (file)
@@ -16,7 +16,7 @@
 //
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 //
 
 #endif
 
 #include <simgear/compiler.h>
+#include <simgear/debug/logstream.hxx>
 
 #include <plib/sg.h>
-#include <plib/ssg.h>
 #include <simgear/screen/extensions.hxx>
 #include <simgear/screen/RenderTexture.h>
-#include SG_GLUT_H
+#include <osg/GLU>
 
 #include "bbcache.hxx"
 
@@ -47,6 +47,7 @@ void SGBbCache::freeTextureMemory(void) {
 
        if( bbListCount ) {
                for(int i = 0 ; i < bbListCount ; i++) {
+                       bbList[i].cldID = 0;
                        if(bbList[i].texID)
                                glDeleteTextures(1, & bbList[i].texID);
                }
@@ -71,51 +72,60 @@ bool SGBbCache::allocTextureMemory(int cacheCount, int textureDimension) {
 
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
-        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        }
     glBindTexture(GL_TEXTURE_2D, 0);
        cacheSizeKb = (textureDimension * textureDimension * 4);
        cacheSizeKb *= cacheCount;
        cacheSizeKb /= 1024;
-       if(rt) {
-               rt->BeginCapture();
-               glViewport(0, 0, textureDimension, textureDimension);
-               rt->EndCapture();
+       if(rtAvailable) {
+               if( rt->BeginCapture() ) {
+                       glViewport(0, 0, textureDimension, textureDimension);
+                       rt->EndCapture();
+               }
        }
        return true;
 }
 
 SGBbCache::SGBbCache(void) :
        bbListCount(0),
-       cacheSizeKb(0),
        textureWH(0),
+       cacheSizeKb(0),
        builtBBCount(0),
+       frameNumber(0),
        rt(0),
        rtAvailable(false),
-       frameNumber(0),
-       maxImpostorRegenFrame(10)
+       maxImpostorRegenFrame(20)
 {
 }
 
 SGBbCache::~SGBbCache(void) {
-       if(rt)
-               delete rt;
+       delete rt;
        freeTextureMemory();
 }
 
 
 void SGBbCache::init(int cacheCount) {
+       GLint colorBits = 0;
+       glGetIntegerv( GL_BLUE_BITS, &colorBits );
 
        rt = new RenderTexture();
        // don't use default rtt on nvidia/win because of poor performance of glCopyTexSubImage2D
        // wihtout default pattrib params - see opengl forum
-       rt->Reset("rgba tex2D ctt");
+       if( colorBits < 8 )
+               rt->Reset("rgba=5,5,5,1 ctt");
+       else
+               rt->Reset("rgba ctt");
+
+//     rt->Reset("rgba tex2D ctt");
 //     rt->Reset("rgba tex2D");
        if( rt->Initialize(256, 256, true) ) {
-               rtAvailable = true;
+               SG_LOG(SG_ALL, SG_INFO, "bbcache:Initialize sucessfull");
                if (rt->BeginCapture())
                {
+                       SG_LOG(SG_ALL, SG_INFO, "bbcache:BeginCapture sucessfull, RTT available");
+                       rtAvailable = true;
                        glViewport(0, 0, 256, 256);
                        glMatrixMode(GL_PROJECTION);
                        glLoadIdentity();
@@ -136,14 +146,16 @@ void SGBbCache::init(int cacheCount) {
                        glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
 
                        rt->EndCapture();
-               }
-       }
+               } else
+                       SG_LOG(SG_ALL, SG_WARN, "bbcache:BeginCapture failed, RTT not available for 3D clouds");
+       } else
+               SG_LOG(SG_ALL, SG_WARN, "bbcache:Initialize failed, RTT not available for 3D clouds");
        if( cacheCount )
-               allocTextureMemory( cacheCount, 256 );
+               allocTextureMemory( cacheCount, 64 );
 
 }
 
-// TODO:not callable atm, texture size not handled correctly
+
 bool SGBbCache::setCacheSize(int count, int textureDimension) {
        if( count < 0 || count > 500)
                return false;
@@ -172,7 +184,7 @@ bool SGBbCache::setCacheSize(int count, int textureDimension) {
        return allocTextureMemory( count, textureDimension);
 }
 
-// TODO:not callable atm, texture size not handled correctly
+
 bool SGBbCache::setCacheSize(int sizeKb) {
        if( sizeKb < 0 || sizeKb > 256*1024)
                return false;
@@ -181,10 +193,10 @@ bool SGBbCache::setCacheSize(int sizeKb) {
                return true;
        int count = 1;
        int textureDimension = 256;
-       if( cacheSizeKb >= 8*1024 ) {
+       if( sizeKb >= 8*1024 ) {
                // more than 32 256x256 textures
                textureDimension = 256;
-       } else  if( cacheSizeKb >= 2*1024 ) {
+       } else  if( sizeKb >= 2*1024 ) {
                // more than 32 128x128 textures
                textureDimension = 128;
        } else  {
@@ -219,6 +231,7 @@ int SGBbCache::alloc(int cldId) {
                        bbList[i].angleX = -999;
                        bbList[i].angleY = -999;
                        bbList[i].frameUsed = 0;
+                       bbList[i].needRedraw = true;
                        return i;
                }
        }
@@ -279,6 +292,7 @@ void SGBbCache::setTextureData(int bbId) {
 //    bbList[bbId].angleY = angleY;
     bbList[bbId].frame = frameNumber;
        bbList[bbId].frameUsed = frameNumber;
+       bbList[bbId].needRedraw = false;
     builtBBCount ++;
        builtBBframe ++;
 }
@@ -305,11 +319,14 @@ bool SGBbCache::isBbValid( int cldId, int bbId, float angleY, float angleX) {
        if( builtBBframe >= maxImpostorRegenFrame )
                return true;
 
-    if( fabs(angleY - bbList[bbId].angleY) >= 5.0 )
-        return false;
+       if( bbList[bbId].needRedraw )
+               return false;
+
+//    if( fabs(angleY - bbList[bbId].angleY) >= 4.0 )
+//        return false;
 
-    if( fabs(angleX - bbList[bbId].angleX) >= 5.0 )
-        return false;
+//    if( fabs(angleX - bbList[bbId].angleX) >= 4.0 )
+//        return false;
 
        bbList[bbId].frameUsed = frameNumber;
        return true;
@@ -328,7 +345,7 @@ void SGBbCache::setReference( int cldId, int bbId, float angleY, float angleX) {
 void SGBbCache::startNewFrame(void) {
        builtBBframe = 0;
        // TOTO:find reasonable value
-       int minFrameNumber = frameNumber - 500;
+       int minFrameNumber = frameNumber - 100;
        frameNumber++;
        // cleanup of unused enties
        for( int bbId = 0 ; bbId < bbListCount ; bbId++)
@@ -337,3 +354,21 @@ void SGBbCache::startNewFrame(void) {
                        bbList[bbId].cldID = 0;
                }
 }
+
+// force all impostors to be rebuilt, this will enventually be done over several frames
+void SGBbCache::invalidateCache(void) {
+
+       for( int bbId = 0 ; bbId < bbListCount ; bbId++)
+//             bbList[bbId].cldID = 0;
+               bbList[bbId].needRedraw = true;
+}
+
+// flag the impostor for a lazy update
+void SGBbCache::invalidate(int cldId, int bbId) {
+       if( bbId < 0 || bbId >= bbListCount )
+               return;
+       if( bbList[bbId].cldID != cldId )
+               return;
+       bbList[bbId].needRedraw = true;
+}
+