This patch spawns from this game engine issue:
[blender-staging.git] / source / gameengine / Ketsji / BL_Texture.cpp
index 4a5a426e9e88d4d326c86be6a2fc8cc21f9efe6b..687b1af957dd3b79b88962981e50733b0231989b 100644 (file)
@@ -8,11 +8,16 @@
 #include <OpenGL/glu.h>
 #else
 #include <GL/gl.h>
+/* #if defined(__sun__) && !defined(__sparc__)
+#include <mesa/glu.h>
+#else */
 #include <GL/glu.h>
+/* #endif */
 #endif
 
 #include <iostream>
 #include <map>
+#include <stdlib.h>
 
 #include "BL_Material.h"
 #include "BL_Texture.h"
@@ -100,7 +105,6 @@ void BL_Texture::DeleteTex()
                glDeleteLists((GLuint)mDisableState, 1);
                mDisableState =0;
        }
-
        g_textureManager.clear();
 }
 
@@ -130,16 +134,34 @@ bool BL_Texture::InitFromImage(int unit,  Image *img, bool mipmap)
 
        ActivateUnit(mUnit);
 
-
        if (mTexture != 0) {
                glBindTexture(GL_TEXTURE_2D, mTexture );
                Validate();
                return mOk;
        }
+
+       // look for an existing gl image
+       BL_TextureMap::iterator mapLook = g_textureManager.find(img->id.name);
+       if (mapLook != g_textureManager.end())
+       {
+               if (mapLook->second.gl_texture != 0)
+               {
+                       mTexture = mapLook->second.gl_texture;
+                       glBindTexture(GL_TEXTURE_2D, mTexture);
+                       mOk = IsValid();
+                       return mOk;
+               }
+       }
+
        mNeedsDeleted = 1;
        glGenTextures(1, (GLuint*)&mTexture);
        InitGLTex(ibuf->rect, ibuf->x, ibuf->y, mipmap);
-       
+
+       // track created units
+       BL_TextureObject obj;
+       obj.gl_texture = mTexture;
+       obj.ref_buffer = img;
+       g_textureManager.insert(std::pair<char*, BL_TextureObject>((char*)img->id.name, obj));
 
 
        glDisable(GL_TEXTURE_2D);
@@ -339,10 +361,13 @@ unsigned int BL_Texture::GetTextureType() const
 int BL_Texture::GetMaxUnits()
 {
        GLint unit=0;
-#ifdef GL_ARB_multitexture
-       if(RAS_EXT_support._ARB_multitexture) {
-               glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &unit);
-               return (MAXTEX>=unit?unit:MAXTEX);
+
+#if defined(GL_ARB_multitexture) && defined(WITH_GLEXT)
+       if (!getenv("WITHOUT_GLEXT")) {
+               if(RAS_EXT_support._ARB_multitexture) {
+                       glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &unit);
+                       return (MAXTEX>=unit?unit:MAXTEX);
+               }
        }
 #endif
        return 0;
@@ -350,28 +375,33 @@ int BL_Texture::GetMaxUnits()
 
 void BL_Texture::ActivateFirst()
 {
-#ifdef GL_ARB_multitexture
-       if(RAS_EXT_support._ARB_multitexture)
-               bgl::blActiveTextureARB(GL_TEXTURE0_ARB);
+#if defined(GL_ARB_multitexture) && defined(WITH_GLEXT)
+       if (!getenv("WITHOUT_GLEXT")) {
+               if(RAS_EXT_support._ARB_multitexture)
+                       bgl::blActiveTextureARB(GL_TEXTURE0_ARB);
+       }
 #endif
 }
 
 void BL_Texture::ActivateUnit(int unit)
 {
-#ifdef GL_ARB_multitexture
-       if(RAS_EXT_support._ARB_multitexture)
-               if(unit <= MAXTEX)
-                       bgl::blActiveTextureARB(GL_TEXTURE0_ARB+unit);
+#if defined(GL_ARB_multitexture) && defined(WITH_GLEXT)
+       if (!getenv("WITHOUT_GLEXT")) {
+               if(RAS_EXT_support._ARB_multitexture)
+                       if(unit <= MAXTEX)
+                               bgl::blActiveTextureARB(GL_TEXTURE0_ARB+unit);
+       }
 #endif
 }
 
 
 void BL_Texture::DisableUnit()
 {
-#ifdef GL_ARB_multitexture
-       if(RAS_EXT_support._ARB_multitexture)
-               bgl::blActiveTextureARB(GL_TEXTURE0_ARB+mUnit);
-
+#if defined(GL_ARB_multitexture) && defined(WITH_GLEXT)
+       if (!getenv("WITHOUT_GLEXT")) {
+               if(RAS_EXT_support._ARB_multitexture)
+                       bgl::blActiveTextureARB(GL_TEXTURE0_ARB+mUnit);
+       }
 #endif
 
 
@@ -399,52 +429,54 @@ void BL_Texture::DisableUnit()
 
 void BL_Texture::DisableAllTextures()
 {
-#ifdef GL_ARB_multitexture
-       glDisable(GL_BLEND);
-       for(int i=0; i<MAXTEX; i++) {
+#if defined(GL_ARB_multitexture) && defined(WITH_GLEXT)
+       if (!getenv("WITHOUT_GLEXT")) {
+               glDisable(GL_BLEND);
+               for(int i=0; i<MAXTEX; i++) {
+                       if(RAS_EXT_support._ARB_multitexture)
+                               bgl::blActiveTextureARB(GL_TEXTURE0_ARB+i);
+
+                       glMatrixMode(GL_TEXTURE);
+                       glLoadIdentity();
+                       glMatrixMode(GL_MODELVIEW);
+                       glDisable(GL_TEXTURE_2D);       
+                       glDisable(GL_TEXTURE_GEN_S);
+                       glDisable(GL_TEXTURE_GEN_T);
+                       glDisable(GL_TEXTURE_GEN_R);
+                       glDisable(GL_TEXTURE_GEN_Q);
+                       glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
+               }
                if(RAS_EXT_support._ARB_multitexture)
-                       bgl::blActiveTextureARB(GL_TEXTURE0_ARB+i);
-
-               glMatrixMode(GL_TEXTURE);
-               glLoadIdentity();
-               glMatrixMode(GL_MODELVIEW);
-               glDisable(GL_TEXTURE_2D);       
-               glDisable(GL_TEXTURE_GEN_S);
-               glDisable(GL_TEXTURE_GEN_T);
-               glDisable(GL_TEXTURE_GEN_R);
-               glDisable(GL_TEXTURE_GEN_Q);
-               glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
+                       bgl::blActiveTextureARB(GL_TEXTURE0_ARB);
        }
-       if(RAS_EXT_support._ARB_multitexture)
-               bgl::blActiveTextureARB(GL_TEXTURE0_ARB);
-
 #endif
 }
 
 
 void BL_Texture::ActivateTexture()
 {
-#ifdef GL_ARB_multitexture
-       if(RAS_EXT_support._ARB_multitexture)
-               bgl::blActiveTextureARB(GL_TEXTURE0_ARB+mUnit);
+#if defined(GL_ARB_multitexture) && defined(WITH_GLEXT)
+       if (!getenv("WITHOUT_GLEXT")) {
+               if(RAS_EXT_support._ARB_multitexture)
+                       bgl::blActiveTextureARB(GL_TEXTURE0_ARB+mUnit);
 
 #ifdef GL_ARB_texture_cube_map
-       if (mType == GL_TEXTURE_CUBE_MAP_ARB && RAS_EXT_support._ARB_texture_cube_map)
-       {
-               glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, mTexture );     
-               glEnable(GL_TEXTURE_CUBE_MAP_ARB);
-       }
-       else
+               if (mType == GL_TEXTURE_CUBE_MAP_ARB && RAS_EXT_support._ARB_texture_cube_map)
+               {
+                       glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, mTexture );     
+                       glEnable(GL_TEXTURE_CUBE_MAP_ARB);
+               } else
 #endif
-       {
+               {
 
-               #ifdef GL_ARB_texture_cube_map
-               if(RAS_EXT_support._ARB_texture_cube_map )
-                       glDisable(GL_TEXTURE_CUBE_MAP_ARB);
-               #endif
+                       #ifdef GL_ARB_texture_cube_map
+                       if(RAS_EXT_support._ARB_texture_cube_map )
+                               glDisable(GL_TEXTURE_CUBE_MAP_ARB);
+                       #endif
 
-               glBindTexture( GL_TEXTURE_2D, mTexture );       
-               glEnable(GL_TEXTURE_2D);
+                       glBindTexture( GL_TEXTURE_2D, mTexture );       
+                       glEnable(GL_TEXTURE_2D);
+               }
        }
 #endif
 }
@@ -667,7 +699,7 @@ void my_envmap_split_ima(EnvMap *env, ImBuf *ibuf)
        }
        else {
                for(part=0; part<6; part++) {
-                       env->cube[part]= ibuf= IMB_allocImBuf(dx, dx, 24, IB_rect, 0);
+                       env->cube[part]= IMB_allocImBuf(dx, dx, 24, IB_rect, 0);
                }
                IMB_rectcpy(env->cube[0], ibuf, 
                        0, 0, 0, 0, dx, dx);