BGE Python API cleanup - no functionality changes
[blender.git] / source / gameengine / Ketsji / KX_BlenderMaterial.cpp
index 429a9ea7a6576b53b2021798dd53a3c49157180f..6722cd232fe8c8a3392a8414585001ed25992bbb 100644 (file)
@@ -6,17 +6,7 @@
 #include <config.h>
 #endif
 
-#ifdef WIN32
-#include <windows.h>
-#endif // WIN32
-#ifdef __APPLE__
-#define GL_GLEXT_LEGACY 1
-#include <OpenGL/gl.h>
-#include <OpenGL/glu.h>
-#else
-#include <GL/gl.h>
-#include <GL/glu.h>
-#endif
+#include "GL/glew.h"
 
 #include "KX_BlenderMaterial.h"
 #include "BL_Material.h"
 #include "MT_Vector4.h"
 #include "MT_Matrix4x4.h"
 
+#include "RAS_BucketManager.h"
 #include "RAS_MeshObject.h"
 #include "RAS_IRasterizer.h"
 #include "RAS_OpenGLRasterizer/RAS_GLExtensionManager.h"
-#include "RAS_OpenGLRasterizer/ARB_multitexture.h"
 
-extern "C" {
-#include "BDR_drawmesh.h"
-}
+#include "GPU_draw.h"
 
 #include "STR_HashedString.h"
 
@@ -44,12 +32,14 @@ extern "C" {
 #include "DNA_object_types.h"
 #include "DNA_material_types.h"
 #include "DNA_image_types.h"
-#include "DNA_mesh_types.h"
+#include "DNA_meshdata_types.h"
 #include "BKE_mesh.h"
 // ------------------------------------
-using namespace bgl;
 #define spit(x) std::cout << x << std::endl;
 
+BL_Shader *KX_BlenderMaterial::mLastShader = NULL;
+BL_BlenderShader *KX_BlenderMaterial::mLastBlenderShader = NULL;
+
 //static PyObject *gTextureDict = 0;
 
 KX_BlenderMaterial::KX_BlenderMaterial(
@@ -57,7 +47,6 @@ KX_BlenderMaterial::KX_BlenderMaterial(
        BL_Material *data,
        bool skin,
        int lightlayer,
-       void *clientobject,
        PyTypeObject *T
        )
 :      PyObjectPlus(T),
@@ -68,37 +57,32 @@ KX_BlenderMaterial::KX_BlenderMaterial(
                data->tilexrep[0],
                data->tileyrep[0],
                data->mode,
-               ((data->ras_mode &TRANSP)!=0),
+               data->transp,
+               ((data->ras_mode &ALPHA)!=0),
                ((data->ras_mode &ZSORT)!=0),
-               lightlayer,
-               ((data->ras_mode &TRIANGLE)!=0),
-               clientobject
+               lightlayer
        ),
        mMaterial(data),
        mShader(0),
+       mBlenderShader(0),
        mScene(scene),
        mUserDefBlend(0),
        mModified(0),
+       mConstructed(false),
        mPass(0)
 
 {
-       ///RAS_EXT_support._ARB_multitexture == true if were here
-
        // --------------------------------
        // RAS_IPolyMaterial variables... 
-       m_flag |=RAS_BLENDERMAT;
-       m_flag |=(mMaterial->IdMode>=ONETEX)?RAS_MULTITEX:0;
-       m_flag |=(mMaterial->ras_mode & USE_LIGHT)!=0?RAS_MULTILIGHT:0;
-       
+       m_flag |= RAS_BLENDERMAT;
+       m_flag |= (mMaterial->IdMode>=ONETEX)? RAS_MULTITEX: 0;
+       m_flag |= ((mMaterial->ras_mode & USE_LIGHT)!=0)? RAS_MULTILIGHT: 0;
+       m_flag |= (mMaterial->glslmat)? RAS_BLENDERGLSL: 0;
+
        // figure max
-       #ifdef GL_ARB_multitexture
        int enabled = mMaterial->num_enabled;
-       mMaterial->num_enabled = enabled>=bgl::max_texture_units?bgl::max_texture_units:enabled;
-       #else
-       mMaterial->num_enabled=0;
-       #endif
-
-       m_enabled = mMaterial->num_enabled;
+       int max = BL_Texture::GetMaxUnits();
+       mMaterial->num_enabled = enabled>=max?max:enabled;
 
        // test the sum of the various modes for equality
        // so we can ether accept or reject this material 
@@ -110,300 +94,245 @@ KX_BlenderMaterial::KX_BlenderMaterial(
                          mMaterial->blend_mode[i]
                         );
        }
-       m_multimode += mMaterial->IdMode+mMaterial->ras_mode;
+       m_multimode += mMaterial->IdMode+ (mMaterial->ras_mode & ~(COLLIDER|USE_LIGHT));
 
 }
 
-
 KX_BlenderMaterial::~KX_BlenderMaterial()
 {
        // cleanup work
-       OnExit();
+       if (mConstructed)
+               // clean only if material was actually used
+               OnExit();
 }
 
 
-TFace* KX_BlenderMaterial::GetTFace(void) const 
+MTFace* KX_BlenderMaterial::GetMTFace(void) const 
 {
        // fonts on polys
        MT_assert(mMaterial->tface);
        return mMaterial->tface;
 }
 
+unsigned int* KX_BlenderMaterial::GetMCol(void) const 
+{
+       // fonts on polys
+       return mMaterial->rgb;
+}
+
 void KX_BlenderMaterial::OnConstruction()
 {
+       if (mConstructed)
+               // when material are reused between objects
+               return;
+       
+       if(mMaterial->glslmat)
+               SetBlenderGLSLShader();
+
        // for each unique material...
-       #ifdef GL_ARB_multitexture
-/*     will be used to switch textures
-       if(!gTextureDict)
-               gTextureDict = PyDict_New();
-*/
        int i;
        for(i=0; i<mMaterial->num_enabled; i++) {
-       bgl::blActiveTextureARB(GL_TEXTURE0_ARB+i);
-               #ifdef GL_ARB_texture_cube_map
                if( mMaterial->mapping[i].mapping & USEENV ) {
-                       if(!RAS_EXT_support._ARB_texture_cube_map) {
+                       if(!GLEW_ARB_texture_cube_map) {
                                spit("CubeMap textures not supported");
                                continue;
                        }
-                       if(!mTextures[i].InitCubeMap( mMaterial->cubemap[i] ) )
+                       if(!mTextures[i].InitCubeMap(i, mMaterial->cubemap[i] ) )
                                spit("unable to initialize image("<<i<<") in "<< 
-                                               mMaterial->matname<< ", image will not be available");
+                                                mMaterial->matname<< ", image will not be available");
                } 
        
                else {
-               #endif//GL_ARB_texture_cube_map
                        if( mMaterial->img[i] ) {
-                               if( ! mTextures[i].InitFromImage(mMaterial->img[i], (mMaterial->flag[i] &MIPMAP)!=0 ))
+                               if( ! mTextures[i].InitFromImage(i, mMaterial->img[i], (mMaterial->flag[i] &MIPMAP)!=0 ))
                                        spit("unable to initialize image("<<i<<") in "<< 
-                                                mMaterial->matname<< ", image will not be available");
+                                               mMaterial->matname<< ", image will not be available");
                        }
-               #ifdef GL_ARB_texture_cube_map
                }
-               #endif//GL_ARB_texture_cube_map
-               /*PyDict_SetItemString(gTextureDict, mTextures[i].GetName().Ptr(), PyInt_FromLong(mTextures[i]));*/
        }
-       #endif//GL_ARB_multitexture
 
        mBlendFunc[0] =0;
        mBlendFunc[1] =0;
+       mConstructed = true;
 }
 
-void KX_BlenderMaterial::OnExit()
+void KX_BlenderMaterial::EndFrame()
 {
-       #ifdef GL_ARB_multitexture
-       
-       #ifdef GL_ARB_shader_objects
-       if( RAS_EXT_support._ARB_shader_objects && mShader ) {
-                //note, the shader here is allocated, per unique material
-                //and this function is called per face
-               bgl::blUseProgramObjectARB(0);
-               delete mShader;
-               mShader = 0;
+       if(mLastBlenderShader) {
+               mLastBlenderShader->SetProg(false);
+               mLastBlenderShader = NULL;
        }
-       #endif //GL_ARB_shader_objects
 
-       for(int i=0; i<mMaterial->num_enabled; i++) {
-               bgl::blActiveTextureARB(GL_TEXTURE0_ARB+i);
-
-               mTextures[i].DeleteTex();
-
-               glMatrixMode(GL_TEXTURE);
-               glLoadIdentity();
-               glMatrixMode(GL_MODELVIEW);
+       if(mLastShader) {
+               mLastShader->SetProg(false);
+               mLastShader = NULL;
+       }
+}
 
-               #ifdef GL_ARB_texture_cube_map
-               if(RAS_EXT_support._ARB_texture_cube_map)
-                       glDisable(GL_TEXTURE_CUBE_MAP_ARB);
-               #endif//GL_ARB_texture_cube_map
+void KX_BlenderMaterial::OnExit()
+{
+       if( mShader ) {
+               //note, the shader here is allocated, per unique material
+               //and this function is called per face
+               if(mShader == mLastShader) {
+                       mShader->SetProg(false);
+                       mLastShader = NULL;
+               }
 
-               glDisable(GL_TEXTURE_2D);
-               glDisable(GL_TEXTURE_GEN_S);
-               glDisable(GL_TEXTURE_GEN_T);
-               glDisable(GL_TEXTURE_GEN_R);
-               glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
+               delete mShader;
+               mShader = 0;
        }
-       
-       /*if (gTextureDict) {
-               PyDict_Clear(gTextureDict);
-               Py_DECREF(gTextureDict);
-               gTextureDict = 0;
-       }*/
 
-       bgl::blActiveTextureARB(GL_TEXTURE0_ARB);
-
-       #ifdef GL_ARB_texture_cube_map
-       if(RAS_EXT_support._ARB_texture_cube_map)
-               glDisable(GL_TEXTURE_CUBE_MAP_ARB);
-       #endif//GL_ARB_texture_cube_map
+       if( mBlenderShader ) {
+               if(mBlenderShader == mLastBlenderShader) {
+                       mBlenderShader->SetProg(false);
+                       mLastBlenderShader = NULL;
+               }
 
-       glDisable(GL_TEXTURE_2D);
+               delete mBlenderShader;
+               mBlenderShader = 0;
+       }
 
-       #endif//GL_ARB_multitexture
+       BL_Texture::ActivateFirst();
+       for(int i=0; i<mMaterial->num_enabled; i++) {
+               BL_Texture::ActivateUnit(i);
+               mTextures[i].DeleteTex();
+               mTextures[i].DisableUnit();
+       }
 
-       // make sure multi texture units 
-       // revert back to blender...
-       // --
        if( mMaterial->tface ) 
-               set_tpage(mMaterial->tface);
-}
-
-
-void KX_BlenderMaterial::DisableTexData()
-{
-       glDisable(GL_BLEND);
-       #ifdef GL_ARB_multitexture
-       int i=(MAXTEX>=bgl::max_texture_units?bgl::max_texture_units:MAXTEX)-1;
-       for(; i>=0; i--) {
-               bgl::blActiveTextureARB(GL_TEXTURE0_ARB+i);
-               glMatrixMode(GL_TEXTURE);
-               glLoadIdentity();
-               glMatrixMode(GL_MODELVIEW);
-
-               #ifdef GL_ARB_texture_cube_map
-               if(RAS_EXT_support._ARB_texture_cube_map)
-                       glDisable(GL_TEXTURE_CUBE_MAP_ARB);
-               #endif//GL_ARB_texture_cube_map
-
-               glDisable(GL_TEXTURE_2D);       
-               glDisable(GL_TEXTURE_GEN_S);
-               glDisable(GL_TEXTURE_GEN_T);
-               glDisable(GL_TEXTURE_GEN_R);
-               glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
-       }
-       #endif//GL_ARB_multitexture
+               GPU_set_tpage(mMaterial->tface);
 }
 
 
-void KX_BlenderMaterial::setShaderData( bool enable )
+void KX_BlenderMaterial::setShaderData( bool enable, RAS_IRasterizer *ras)
 {
-       #ifdef GL_ARB_multitexture 
-       #ifdef GL_ARB_shader_objects 
-
-       MT_assert(RAS_EXT_support._ARB_shader_objects && mShader);
+       MT_assert(GLEW_ARB_shader_objects && mShader);
 
        int i;
        if( !enable || !mShader->Ok() ) {
                // frame cleanup.
-               bgl::blUseProgramObjectARB( 0 );
-               DisableTexData();
+               if(mShader == mLastShader) {
+                       mShader->SetProg(false);
+                       mLastShader = NULL;
+               }
+
+               ras->SetBlendingMode(TF_SOLID);
+               BL_Texture::DisableAllTextures();
                return;
        }
 
-       DisableTexData();
-       bgl::blUseProgramObjectARB( mShader->GetProg() );
+       BL_Texture::DisableAllTextures();
+       mShader->SetProg(true);
+       mLastShader = mShader;
        
-       // for each enabled unit
-       for(i=0; i<mMaterial->num_enabled; i++) {
-
-               const uSampler *samp = mShader->getSampler(i);
-               if( samp->loc == -1 || samp->glTexture == 0 ) continue;
+       BL_Texture::ActivateFirst();
 
-               bgl::blActiveTextureARB(GL_TEXTURE0_ARB+i);
+       mShader->ApplyShader();
 
-               #ifdef GL_ARB_texture_cube_map
-               if( mMaterial->mapping[i].mapping &USEENV ) {
-                       glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, samp->glTexture /* mTextures[i]*/ );    
-                       glEnable( GL_TEXTURE_CUBE_MAP_ARB );
-               } 
-               else {
-               #endif//GL_ARB_texture_cube_map
-                       glBindTexture( GL_TEXTURE_2D, samp->glTexture   /*mTextures[i]*/ );     
-                       glEnable( GL_TEXTURE_2D );
-               #ifdef GL_ARB_texture_cube_map
-               }
-               #endif//GL_ARB_texture_cube_map
-               // use a sampler
-               bgl::blUniform1iARB(samp->loc, i );
+       // for each enabled unit
+       for(i=0; i<mMaterial->num_enabled; i++) {
+               if(!mTextures[i].Ok()) continue;
+               mTextures[i].ActivateTexture();
+               mTextures[0].SetMapping(mMaterial->mapping[i].mapping);
        }
 
        if(!mUserDefBlend) {
-               setDefaultBlending();
-       }else
-       {
-               glEnable(GL_BLEND);
+               ras->SetBlendingMode(mMaterial->transp);
+       }
+       else {
+               ras->SetBlendingMode(TF_SOLID);
+               ras->SetBlendingMode(-1); // indicates custom mode
+
                // tested to be valid enums
+               glEnable(GL_BLEND);
                glBlendFunc(mBlendFunc[0], mBlendFunc[1]);
        }
-
-       #endif//GL_ARB_shader_objects
-       #endif//GL_ARB_multitexture
 }
 
-
-void KX_BlenderMaterial::setTexData( bool enable )
+void KX_BlenderMaterial::setBlenderShaderData( bool enable, RAS_IRasterizer *ras)
 {
-       #ifdef GL_ARB_multitexture
-       int i;
+       if( !enable || !mBlenderShader->Ok() ) {
+               ras->SetBlendingMode(TF_SOLID);
+
+               // frame cleanup.
+               if(mLastBlenderShader) {
+                       mLastBlenderShader->SetProg(false);
+                       mLastBlenderShader= NULL;
+               }
+               else
+                       BL_Texture::DisableAllTextures();
+
+               return;
+       }
+
+       if(!mBlenderShader->Equals(mLastBlenderShader)) {
+               ras->SetBlendingMode(mMaterial->transp);
+
+               if(mLastBlenderShader)
+                       mLastBlenderShader->SetProg(false);
+               else
+                       BL_Texture::DisableAllTextures();
 
-       #ifdef GL_ARB_shader_objects
-       if(RAS_EXT_support._ARB_shader_objects) {
-               // switch back to fixed func
-               bgl::blUseProgramObjectARB( 0 );
+               mBlenderShader->SetProg(true, ras->GetTime());
+               mLastBlenderShader= mBlenderShader;
        }
-       #endif//GL_ARB_shader_objects
+}
+
+void KX_BlenderMaterial::setTexData( bool enable, RAS_IRasterizer *ras)
+{
+       BL_Texture::DisableAllTextures();
 
        if( !enable ) {
-                // frame cleanup.
-               DisableTexData();
+               ras->SetBlendingMode(TF_SOLID);
                return;
        }
-       
-       DisableTexData();
+
+       BL_Texture::ActivateFirst();
 
        if( mMaterial->IdMode == DEFAULT_BLENDER ) {
-               setDefaultBlending();
+               ras->SetBlendingMode(mMaterial->transp);
                return;
        }
 
        if( mMaterial->IdMode == TEXFACE ) {
-
                // no material connected to the object
-               if( mTextures[0] ) {
-                       if( !mTextures[0].Ok() ) return;
-                       bgl::blActiveTextureARB(GL_TEXTURE0_ARB);
-                       glBindTexture( GL_TEXTURE_2D, mTextures[0] );   
-                       glEnable(GL_TEXTURE_2D);
-                       setTextureEnvironment( -1 ); // modulate
-                       setEnvMap( (mMaterial->mapping[0].mapping &USEREFL)!=0 );
-                       setDefaultBlending(); 
+               if( mTextures[0].Ok() ) {
+                       mTextures[0].ActivateTexture();
+                       mTextures[0].setTexEnv(0, true);
+                       mTextures[0].SetMapping(mMaterial->mapping[0].mapping);
+                       ras->SetBlendingMode(mMaterial->transp);
                }
                return;
        }
 
-       int lastblend = 0;
-
-       // for each enabled unit
-       for(i=0; (i<mMaterial->num_enabled); i++) {
+       int mode = 0,i=0;
+       for(i=0; (i<mMaterial->num_enabled && i<MAXTEX); i++) {
                if( !mTextures[i].Ok() ) continue;
 
-               bgl::blActiveTextureARB(GL_TEXTURE0_ARB+i);
-
-               #ifdef GL_ARB_texture_cube_map
-               // use environment maps
-               if( mMaterial->mapping[i].mapping &USEENV && RAS_EXT_support._ARB_texture_cube_map ) {
-                       glBindTexture( GL_TEXTURE_CUBE_MAP_ARB, mTextures[i] ); 
-                       glEnable(GL_TEXTURE_CUBE_MAP_ARB);
-                       setTextureEnvironment( i );
-
-                       if( mMaterial->mapping[i].mapping &USEREFL )
-                               setEnvMap( true, true );
-                       else if(mMaterial->mapping[i].mapping &USEOBJ)
-                               setObjectMatrixData(i);
-                       else
-                               setTexMatrixData( i );
-               } 
-               // 2d textures
-               else { 
-               #endif//GL_ARB_texture_cube_map
-                       glBindTexture( GL_TEXTURE_2D, mTextures[i] );   
-                       glEnable( GL_TEXTURE_2D );
-                       setTextureEnvironment( i );
-                       
-                       if( mMaterial->mapping[i].mapping &USEREFL ){
-                               setEnvMap( true );
-                       }
-                       else if(mMaterial->mapping[i].mapping &USEOBJ){
-                               setObjectMatrixData(i);
-                       }
-                       else {
-                               setTexMatrixData( i );
-                       }
+               mTextures[i].ActivateTexture();
+               mTextures[i].setTexEnv(mMaterial);
+               mode = mMaterial->mapping[i].mapping;
 
-               #ifdef GL_ARB_texture_cube_map
-               }
-               #endif//GL_ARB_texture_cube_map
+               if(mode &USEOBJ)
+                       setObjectMatrixData(i, ras);
+               else
+                       mTextures[i].SetMapping(mode);
+               
+               if(!(mode &USEOBJ))
+                       setTexMatrixData( i );
        }
+
        if(!mUserDefBlend) {
-               setDefaultBlending();
-       }else
-       {
+               ras->SetBlendingMode(mMaterial->transp);
+       }
+       else {
+               ras->SetBlendingMode(TF_SOLID);
+               ras->SetBlendingMode(-1); // indicates custom mode
+
                glEnable(GL_BLEND);
-               // tested to be valid enums
                glBlendFunc(mBlendFunc[0], mBlendFunc[1]);
        }
-
-       #endif//GL_ARB_multitexture
 }
 
 void
@@ -411,66 +340,90 @@ KX_BlenderMaterial::ActivatShaders(
        RAS_IRasterizer* rasty, 
        TCachingInfo& cachingInfo)const
 {
+       KX_BlenderMaterial *tmp = const_cast<KX_BlenderMaterial*>(this);
+
+       // reset... 
+       if(tmp->mMaterial->IsShared()) 
+               cachingInfo =0;
+
+       if(mLastBlenderShader) {
+               mLastBlenderShader->SetProg(false);
+               mLastBlenderShader= NULL;
+       }
+
        if (GetCachingInfo() != cachingInfo) {
-               KX_BlenderMaterial *tmp = const_cast<KX_BlenderMaterial*>(this);
 
                if (!cachingInfo)
-                       tmp->setShaderData( false );
+                       tmp->setShaderData(false, rasty);
                
                cachingInfo = GetCachingInfo();
        
-               if (rasty->GetDrawingMode() == RAS_IRasterizer::KX_TEXTURED ) {
-                       tmp->setShaderData( true );
-                       rasty->EnableTextures(true);
-               }
-               else {
-                       tmp->setShaderData( false );
-                       rasty->EnableTextures(false);
-               }
+               if(rasty->GetDrawingMode() == RAS_IRasterizer::KX_TEXTURED)
+                       tmp->setShaderData(true, rasty);
+               else
+                       tmp->setShaderData(false, rasty);
 
                if(mMaterial->mode & RAS_IRasterizer::KX_TWOSIDE)
                        rasty->SetCullFace(false);
                else
                        rasty->SetCullFace(true);
 
-               if (((mMaterial->ras_mode &WIRE)!=0) || mMaterial->mode & RAS_IRasterizer::KX_LINES)
+               if (((mMaterial->ras_mode &WIRE)!=0) || (mMaterial->mode & RAS_IRasterizer::KX_LINES) ||
+                   (rasty->GetDrawingMode() <= RAS_IRasterizer::KX_WIREFRAME))
+               {               
+                       if((mMaterial->ras_mode &WIRE)!=0) 
+                               rasty->SetCullFace(false);
                        rasty->SetLines(true);
+               }
                else
                        rasty->SetLines(false);
        }
-       
-       // shaders have access to the variables set here
-       // via builtin GLSL variables
-       // eg: gl_FrontMaterial.diffuse
-       // --
-       rasty->SetSpecularity(
-               mMaterial->speccolor[0]*mMaterial->spec_f,
-               mMaterial->speccolor[1]*mMaterial->spec_f,
-               mMaterial->speccolor[2]*mMaterial->spec_f,
-               mMaterial->spec_f
-       );
 
-       rasty->SetShinyness( mMaterial->hard );
+       ActivatGLMaterials(rasty);
+       ActivateTexGen(rasty);
+}
 
-       rasty->SetDiffuse(
-               mMaterial->matcolor[0]*mMaterial->ref+mMaterial->emit, 
-               mMaterial->matcolor[1]*mMaterial->ref+mMaterial->emit,
-               mMaterial->matcolor[2]*mMaterial->ref+mMaterial->emit,
-               1.0f);
+void
+KX_BlenderMaterial::ActivateBlenderShaders(
+       RAS_IRasterizer* rasty, 
+       TCachingInfo& cachingInfo)const
+{
+       KX_BlenderMaterial *tmp = const_cast<KX_BlenderMaterial*>(this);
 
-       rasty->SetEmissive(     
-               mMaterial->matcolor[0]*mMaterial->emit,
-               mMaterial->matcolor[1]*mMaterial->emit,
-               mMaterial->matcolor[2]*mMaterial->emit,
-               1.0
-               );
+       if(mLastShader) {
+               mLastShader->SetProg(false);
+               mLastShader= NULL;
+       }
+
+       if (GetCachingInfo() != cachingInfo) {
+               if (!cachingInfo)
+                       tmp->setBlenderShaderData(false, rasty);
+               
+               cachingInfo = GetCachingInfo();
+       
+               if(rasty->GetDrawingMode() == RAS_IRasterizer::KX_TEXTURED)
+                       tmp->setBlenderShaderData(true, rasty);
+               else
+                       tmp->setBlenderShaderData(false, rasty);
 
-       // Lagan's patch...
-       // added material factor
-       rasty->SetAmbient(mMaterial->amb);
+               if(mMaterial->mode & RAS_IRasterizer::KX_TWOSIDE)
+                       rasty->SetCullFace(false);
+               else
+                       rasty->SetCullFace(true);
 
-       if (mMaterial->material)
-               rasty->SetPolygonOffset(-mMaterial->material->zoffs, 0.0);
+               if (((mMaterial->ras_mode & WIRE)!=0) || (mMaterial->mode & RAS_IRasterizer::KX_LINES) ||
+                   (rasty->GetDrawingMode() <= RAS_IRasterizer::KX_WIREFRAME))
+               {               
+                       if((mMaterial->ras_mode &WIRE)!=0) 
+                               rasty->SetCullFace(false);
+                       rasty->SetLines(true);
+               }
+               else
+                       rasty->SetLines(false);
+
+               ActivatGLMaterials(rasty);
+               mBlenderShader->SetAttribs(rasty, mMaterial);
+       }
 }
 
 void
@@ -479,62 +432,47 @@ KX_BlenderMaterial::ActivateMat(
        TCachingInfo& cachingInfo
        )const
 {
-       if (GetCachingInfo() != cachingInfo) {
-               KX_BlenderMaterial *tmp = const_cast<KX_BlenderMaterial*>(this);
+       KX_BlenderMaterial *tmp = const_cast<KX_BlenderMaterial*>(this);
 
+       if(mLastShader) {
+               mLastShader->SetProg(false);
+               mLastShader= NULL;
+       }
+
+       if(mLastBlenderShader) {
+               mLastBlenderShader->SetProg(false);
+               mLastBlenderShader= NULL;
+       }
+
+       if (GetCachingInfo() != cachingInfo) {
                if (!cachingInfo) 
-                       tmp->setTexData( false );
+                       tmp->setTexData( false,rasty );
                
                cachingInfo = GetCachingInfo();
 
-               if (rasty->GetDrawingMode() == RAS_IRasterizer::KX_TEXTURED) {
-                       tmp->setTexData( true );
-                       rasty->EnableTextures(true);
-               }
-               else{
-                       tmp->setTexData( false );
-                       rasty->EnableTextures(false);
-               }
+               if (rasty->GetDrawingMode() == RAS_IRasterizer::KX_TEXTURED)
+                       tmp->setTexData( true,rasty  );
+               else
+                       tmp->setTexData( false,rasty);
 
                if(mMaterial->mode & RAS_IRasterizer::KX_TWOSIDE)
                        rasty->SetCullFace(false);
                else
                        rasty->SetCullFace(true);
 
-               if (((mMaterial->ras_mode &WIRE)!=0) || mMaterial->mode & RAS_IRasterizer::KX_LINES)
+               if (((mMaterial->ras_mode &WIRE)!=0) || (mMaterial->mode & RAS_IRasterizer::KX_LINES) ||
+                   (rasty->GetDrawingMode() <= RAS_IRasterizer::KX_WIREFRAME))
+               {               
+                       if((mMaterial->ras_mode &WIRE)!=0) 
+                               rasty->SetCullFace(false);
                        rasty->SetLines(true);
+               }
                else
                        rasty->SetLines(false);
        }
-               
-       rasty->SetSpecularity(
-               mMaterial->speccolor[0]*mMaterial->spec_f,
-               mMaterial->speccolor[1]*mMaterial->spec_f,
-               mMaterial->speccolor[2]*mMaterial->spec_f,
-               mMaterial->spec_f
-       );
-
-       rasty->SetShinyness( mMaterial->hard );
-
-       rasty->SetDiffuse(
-               mMaterial->matcolor[0]*mMaterial->ref+mMaterial->emit, 
-               mMaterial->matcolor[1]*mMaterial->ref+mMaterial->emit,
-               mMaterial->matcolor[2]*mMaterial->ref+mMaterial->emit,
-               1.0f);
-
-       rasty->SetEmissive(     
-               mMaterial->matcolor[0]*mMaterial->emit,
-               mMaterial->matcolor[1]*mMaterial->emit,
-               mMaterial->matcolor[2]*mMaterial->emit,
-               1.0
-               );
-       
-       // Lagan's patch...
-       // added material factor
-       rasty->SetAmbient(mMaterial->amb);
 
-       if (mMaterial->material)
-               rasty->SetPolygonOffset(-mMaterial->material->zoffs, 0.0);
+       ActivatGLMaterials(rasty);
+       ActivateTexGen(rasty);
 }
 
 bool 
@@ -543,235 +481,170 @@ KX_BlenderMaterial::Activate(
        TCachingInfo& cachingInfo
        )const
 {
-       bool dopass = false;
-       #ifdef GL_ARB_shader_objects
-       if( RAS_EXT_support._ARB_shader_objects &&
-               ( mShader && mShader->Ok() ) ) {
-
-               if( (mPass++) < mShader->getNumPass() ) {
+       if(GLEW_ARB_shader_objects && (mShader && mShader->Ok())) {
+               if((mPass++) < mShader->getNumPass() ) {
                        ActivatShaders(rasty, cachingInfo);
-                       dopass = true;
-                       return dopass;
+                       return true;
                }
                else {
-                       bgl::blUseProgramObjectARB( 0 );
+                       if(mShader == mLastShader) {
+                               mShader->SetProg(false);
+                               mLastShader = NULL;
+                       }
                        mPass = 0;
-                       dopass = false;
-                       return dopass;
+                       return false;
                }
        }
-       else {
-       #endif//GL_ARB_shader_objects
-               switch (mPass++)
-               {
-                       case 0:
-                               ActivateMat(rasty, cachingInfo);
-                               dopass = true;
-                               break;
-                       default:
-                               mPass = 0;
-                               dopass = false;
-                               break;
+       else if( GLEW_ARB_shader_objects && (mBlenderShader && mBlenderShader->Ok() ) ) {
+               if(mPass++ == 0) {
+                       ActivateBlenderShaders(rasty, cachingInfo);
+                       return true;
                }
-       #ifdef GL_ARB_shader_objects
-       }
-       #endif//GL_ARB_shader_objects
-       return dopass;
-}
-
-void KX_BlenderMaterial::setTextureEnvironment( int textureIndex )
-{
-#ifndef GL_ARB_texture_env_combine
-       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
-       return;
-#else
-       if(textureIndex == -1 || !RAS_EXT_support._ARB_texture_env_combine){
-               glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
-               return;
-       }
-
-       glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB );
-
-       GLfloat blend_operand           = GL_SRC_COLOR;
-       GLfloat blend_operand_prev  = GL_SRC_COLOR;
-
-       GLenum combiner = GL_COMBINE_RGB_ARB;
-       GLenum source0 = GL_SOURCE0_RGB_ARB;
-       GLenum source1 = GL_SOURCE1_RGB_ARB;
-       GLenum source2 = GL_SOURCE2_RGB_ARB;
-       GLenum op0 = GL_OPERAND0_RGB_ARB;
-       GLenum op1 = GL_OPERAND1_RGB_ARB;
-       GLenum op2 = GL_OPERAND2_RGB_ARB;
-       GLfloat alphaOp = GL_SRC_ALPHA;
-
-       // switch to alpha combiners
-       if( (mMaterial->flag[textureIndex] &TEXALPHA) ) {
-               combiner = GL_COMBINE_ALPHA_ARB;
-               source0  = GL_SOURCE0_ALPHA_ARB;
-               source1 = GL_SOURCE1_ALPHA_ARB;
-               source2 = GL_SOURCE2_ALPHA_ARB;
-               op0 = GL_OPERAND0_ALPHA_ARB;
-               op1 = GL_OPERAND1_ALPHA_ARB;
-               op2 = GL_OPERAND2_ALPHA_ARB;
-               blend_operand = GL_SRC_ALPHA;
-               
-               // invert
-               if(mMaterial->flag[textureIndex] &TEXNEG) {
-                       blend_operand_prev = GL_ONE_MINUS_SRC_ALPHA;
-                       blend_operand = GL_ONE_MINUS_SRC_ALPHA;
+               else {
+                       mPass = 0;
+                       return false;
                }
        }
        else {
-               if(mMaterial->flag[textureIndex] &TEXNEG) {
-                       blend_operand_prev=GL_ONE_MINUS_SRC_COLOR;
-                       blend_operand = GL_ONE_MINUS_SRC_COLOR;
+               if(mPass++ == 0) {
+                       ActivateMat(rasty, cachingInfo);
+                       return true;
+               }
+               else {
+                       mPass = 0;
+                       return false;
                }
        }
-       bool using_alpha = false;
+}
 
-       if(mMaterial->flag[textureIndex] &USEALPHA){
-               alphaOp = GL_ONE_MINUS_SRC_ALPHA;
-               using_alpha=true;
-       }
-       else if(mMaterial->flag[textureIndex] &USENEGALPHA){
-               alphaOp = GL_SRC_ALPHA;
-               using_alpha = true;
-       }
+bool KX_BlenderMaterial::UsesLighting(RAS_IRasterizer *rasty) const
+{
+       if(!RAS_IPolyMaterial::UsesLighting(rasty))
+               return false;
 
-       switch( mMaterial->blend_mode[textureIndex] ) {
-               case BLEND_MIX:
-                       {
-                               // ------------------------------
-                               if(!using_alpha) {
-                                       GLfloat base_col[4];
-                                       base_col[0]      = base_col[1]  = base_col[2]  = 0.f;
-                                       base_col[3]      = 1.f-mMaterial->color_blend[textureIndex];
-                                       glTexEnvfv( GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR,base_col );
-                               }
-                               glTexEnvf(      GL_TEXTURE_ENV, combiner,       GL_INTERPOLATE_ARB);
-                               glTexEnvf(      GL_TEXTURE_ENV, source0,        GL_PREVIOUS_ARB);
-                               glTexEnvf(      GL_TEXTURE_ENV, op0,            blend_operand_prev );
-                               glTexEnvf(      GL_TEXTURE_ENV, source1,        GL_TEXTURE );
-                               glTexEnvf(      GL_TEXTURE_ENV, op1,            blend_operand);
-                               if(!using_alpha)
-                                       glTexEnvf(      GL_TEXTURE_ENV, source2,        GL_CONSTANT_ARB );
-                               else
-                                       glTexEnvf(      GL_TEXTURE_ENV, source2,        GL_TEXTURE );
-
-                               glTexEnvf(      GL_TEXTURE_ENV, op2,            alphaOp);
-                       }break;
-               case BLEND_MUL: 
-                       {
-                               // ------------------------------
-                               glTexEnvf(      GL_TEXTURE_ENV, combiner,       GL_MODULATE);
-                               glTexEnvf(      GL_TEXTURE_ENV, source0,        GL_PREVIOUS_ARB);
-                               glTexEnvf(      GL_TEXTURE_ENV, op0,            blend_operand_prev);
-                               glTexEnvf(      GL_TEXTURE_ENV, source1,        GL_TEXTURE );
-                               if(using_alpha)
-                                       glTexEnvf(      GL_TEXTURE_ENV, op1,            alphaOp);
-                               else
-                                       glTexEnvf(      GL_TEXTURE_ENV, op1,            blend_operand);
-                       }break;
-               case BLEND_ADD: 
-                       {
-                               // ------------------------------
-                               glTexEnvf(      GL_TEXTURE_ENV, combiner,       GL_ADD_SIGNED_ARB);
-                               glTexEnvf(      GL_TEXTURE_ENV, source0,        GL_PREVIOUS_ARB );
-                               glTexEnvf(      GL_TEXTURE_ENV, op0,            blend_operand_prev );
-                               glTexEnvf(      GL_TEXTURE_ENV, source1,        GL_TEXTURE );
-                               if(using_alpha)
-                                       glTexEnvf(      GL_TEXTURE_ENV, op1,            alphaOp);
-                               else
-                                       glTexEnvf(      GL_TEXTURE_ENV, op1,            blend_operand);
-                       }break;
-               case BLEND_SUB: 
-                       {
-                               // ------------------------------
-                               glTexEnvf(      GL_TEXTURE_ENV, combiner,       GL_SUBTRACT_ARB);
-                               glTexEnvf(      GL_TEXTURE_ENV, source0,        GL_PREVIOUS_ARB );
-                               glTexEnvf(      GL_TEXTURE_ENV, op0,            blend_operand_prev );
-                               glTexEnvf(      GL_TEXTURE_ENV, source1,        GL_TEXTURE );
-                               glTexEnvf(      GL_TEXTURE_ENV, op1,            blend_operand);
-                       }break;
-               case BLEND_SCR: 
-                       {
-                               // ------------------------------
-                               glTexEnvf(      GL_TEXTURE_ENV, combiner,       GL_ADD);
-                               glTexEnvf(      GL_TEXTURE_ENV, source0,        GL_PREVIOUS_ARB );
-                               glTexEnvf(      GL_TEXTURE_ENV, op0,            blend_operand_prev );
-                               glTexEnvf(      GL_TEXTURE_ENV, source1,        GL_TEXTURE );
-                               if(using_alpha)
-                                       glTexEnvf(      GL_TEXTURE_ENV, op1,            alphaOp);
-                               else
-                                       glTexEnvf(      GL_TEXTURE_ENV, op1,            blend_operand);
-                       } break;
-       }
-       glTexEnvf(      GL_TEXTURE_ENV, GL_RGB_SCALE_ARB,       1.0);
-#endif //!GL_ARB_texture_env_combine
+       if(mShader && mShader->Ok())
+               return true;
+       else if(mBlenderShader && mBlenderShader->Ok())
+               return false;
+       else
+               return true;
 }
 
-bool KX_BlenderMaterial::setDefaultBlending()
+void KX_BlenderMaterial::ActivateMeshSlot(const RAS_MeshSlot & ms, RAS_IRasterizer* rasty) const
 {
-       if( mMaterial->transp &TF_ADD) {
-               glEnable(GL_BLEND);
-               glBlendFunc(GL_ONE, GL_ONE);
-               return true;
+       if(mShader && GLEW_ARB_shader_objects) {
+               mShader->Update(ms, rasty);
        }
-       
-       if( mMaterial->transp & TF_ALPHA ) {
-               glEnable(GL_BLEND);
-               glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-               return true;
+       else if(mBlenderShader && GLEW_ARB_shader_objects) {
+               int blendmode;
+
+               mBlenderShader->Update(ms, rasty);
+
+               /* we do blend modes here, because they can change per object
+                * with the same material due to obcolor/obalpha */
+               blendmode = mBlenderShader->GetBlendMode();
+               if((blendmode == TF_SOLID || blendmode == TF_ALPHA) && mMaterial->transp != TF_SOLID)
+                       blendmode = mMaterial->transp;
+
+               rasty->SetBlendingMode(blendmode);
        }
-       
-       glDisable(GL_BLEND);
-       return false;
 }
 
-void KX_BlenderMaterial::setEnvMap(bool val, bool cube)
+void KX_BlenderMaterial::ActivatGLMaterials( RAS_IRasterizer* rasty )const
 {
-       #ifdef GL_ARB_texture_cube_map
-       if( cube && RAS_EXT_support._ARB_texture_cube_map ) 
-       {
-               glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB );
-               glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
-               glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB);
+       if(mShader || !mBlenderShader) {
+               rasty->SetSpecularity(
+                       mMaterial->speccolor[0]*mMaterial->spec_f,
+                       mMaterial->speccolor[1]*mMaterial->spec_f,
+                       mMaterial->speccolor[2]*mMaterial->spec_f,
+                       mMaterial->spec_f
+               );
+
+               rasty->SetShinyness( mMaterial->hard );
+
+               rasty->SetDiffuse(
+                       mMaterial->matcolor[0]*mMaterial->ref+mMaterial->emit, 
+                       mMaterial->matcolor[1]*mMaterial->ref+mMaterial->emit,
+                       mMaterial->matcolor[2]*mMaterial->ref+mMaterial->emit,
+                       1.0f);
+
+               rasty->SetEmissive(     
+                       mMaterial->matcolor[0]*mMaterial->emit,
+                       mMaterial->matcolor[1]*mMaterial->emit,
+                       mMaterial->matcolor[2]*mMaterial->emit,
+                       1.0 );
 
-               glEnable(GL_TEXTURE_GEN_S);
-               glEnable(GL_TEXTURE_GEN_T);
-               glEnable(GL_TEXTURE_GEN_R);
+               rasty->SetAmbient(mMaterial->amb);
        }
-       else {
-       #endif//GL_ARB_texture_cube_map
-               if( val ) {
-                       glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP );
-                       glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
-                       
-                       glEnable(GL_TEXTURE_GEN_S);
-                       glEnable(GL_TEXTURE_GEN_T);
-                       glEnable(GL_TEXTURE_GEN_R);
+
+       if (mMaterial->material)
+               rasty->SetPolygonOffset(-mMaterial->material->zoffs, 0.0);
+}
+
+
+void KX_BlenderMaterial::ActivateTexGen(RAS_IRasterizer *ras) const
+{
+       if(ras->GetDrawingMode() == RAS_IRasterizer::KX_TEXTURED) {
+               ras->SetAttribNum(0);
+               if(mShader && GLEW_ARB_shader_objects) {
+                       if(mShader->GetAttribute() == BL_Shader::SHD_TANGENT) {
+                               ras->SetAttrib(RAS_IRasterizer::RAS_TEXCO_DISABLE, 0);
+                               ras->SetAttrib(RAS_IRasterizer::RAS_TEXTANGENT, 1);
+                               ras->SetAttribNum(2);
+                       }
                }
-               else {
-                       glDisable(GL_TEXTURE_GEN_S);
-                       glDisable(GL_TEXTURE_GEN_T);
-                       glDisable(GL_TEXTURE_GEN_R);
+
+               ras->SetTexCoordNum(mMaterial->num_enabled);
+
+               for(int i=0; i<mMaterial->num_enabled; i++) {
+                       int mode = mMaterial->mapping[i].mapping;
+
+                       if (mode &USECUSTOMUV)
+                       {
+                               STR_String str = mMaterial->mapping[i].uvCoName;
+                               if (!str.IsEmpty())
+                                       ras->SetTexCoord(RAS_IRasterizer::RAS_TEXCO_UV2, i);
+                               continue;
+                       }
+
+                       if( mode &(USEREFL|USEOBJ))
+                               ras->SetTexCoord(RAS_IRasterizer::RAS_TEXCO_GEN, i);
+                       else if(mode &USEORCO)
+                               ras->SetTexCoord(RAS_IRasterizer::RAS_TEXCO_ORCO, i);
+                       else if(mode &USENORM)
+                               ras->SetTexCoord(RAS_IRasterizer::RAS_TEXCO_NORM, i);
+                       else if(mode &USEUV)
+                               ras->SetTexCoord(RAS_IRasterizer::RAS_TEXCO_UV1, i);
+                       else if(mode &USETANG)
+                               ras->SetTexCoord(RAS_IRasterizer::RAS_TEXTANGENT, i);
+                       else 
+                               ras->SetTexCoord(RAS_IRasterizer::RAS_TEXCO_DISABLE, i);
                }
-       #ifdef GL_ARB_texture_cube_map
        }
-       #endif//GL_ARB_texture_cube_map
 }
 
-
 void KX_BlenderMaterial::setTexMatrixData(int i)
 {
        glMatrixMode(GL_TEXTURE);
        glLoadIdentity();
 
-       glScalef( 
-               mMaterial->mapping[i].scale[0], 
-               mMaterial->mapping[i].scale[1], 
-               mMaterial->mapping[i].scale[2]
-       );
+       if( GLEW_ARB_texture_cube_map && 
+               mTextures[i].GetTextureType() == GL_TEXTURE_CUBE_MAP_ARB && 
+               mMaterial->mapping[i].mapping & USEREFL) {
+               glScalef( 
+                       mMaterial->mapping[i].scale[0], 
+                       -mMaterial->mapping[i].scale[1], 
+                       -mMaterial->mapping[i].scale[2]
+               );
+       }
+       else
+       {
+               glScalef( 
+                       mMaterial->mapping[i].scale[0], 
+                       mMaterial->mapping[i].scale[1], 
+                       mMaterial->mapping[i].scale[2]
+               );
+       }
        glTranslatef(
                mMaterial->mapping[i].offsets[0],
                mMaterial->mapping[i].offsets[1], 
@@ -793,17 +666,13 @@ static void GetProjPlane(BL_Material *mat, int index,int num, float*param)
                param[2] = 1.f;
 }
 
-
-void KX_BlenderMaterial::setObjectMatrixData(int i)
+void KX_BlenderMaterial::setObjectMatrixData(int i, RAS_IRasterizer *ras)
 {
-       // will work without cubemaps
-       // but a cubemap will look the best
        KX_GameObject *obj = 
                (KX_GameObject*)
                mScene->GetObjectList()->FindValue(mMaterial->mapping[i].objconame);
 
-       if(!obj)
-               return;
+       if(!obj) return;
 
        glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR );
        glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR );
@@ -826,9 +695,7 @@ void KX_BlenderMaterial::setObjectMatrixData(int i)
        glEnable(GL_TEXTURE_GEN_T);
        glEnable(GL_TEXTURE_GEN_R);
 
-       float matr[16];
-       glGetFloatv(GL_MODELVIEW_MATRIX, matr);
-       MT_Matrix4x4 mvmat(matr);
+       const MT_Matrix4x4& mvmat = ras->GetViewMatrix();
 
        glMatrixMode(GL_TEXTURE);
        glLoadIdentity();
@@ -848,7 +715,6 @@ void KX_BlenderMaterial::setObjectMatrixData(int i)
 
 }
 
-
 // ------------------------------------
 void KX_BlenderMaterial::UpdateIPO(
        MT_Vector4 rgba,
@@ -879,76 +745,74 @@ PyMethodDef KX_BlenderMaterial::Methods[] =
        KX_PYMETHODTABLE( KX_BlenderMaterial, getShader ),
        KX_PYMETHODTABLE( KX_BlenderMaterial, getMaterialIndex ),
        KX_PYMETHODTABLE( KX_BlenderMaterial, setBlending ),
-//     KX_PYMETHODTABLE( KX_BlenderMaterial, getTexture ),
-//     KX_PYMETHODTABLE( KX_BlenderMaterial, setTexture ),
-
        {NULL,NULL} //Sentinel
 };
 
+PyAttributeDef KX_BlenderMaterial::Attributes[] = {
+       { NULL }        //Sentinel
+};
 
 PyTypeObject KX_BlenderMaterial::Type = {
-       PyObject_HEAD_INIT(&PyType_Type)
+       PyObject_HEAD_INIT(NULL)
                0,
                "KX_BlenderMaterial",
-               sizeof(KX_BlenderMaterial),
+               sizeof(PyObjectPlus_Proxy),
+               0,
+               py_base_dealloc,
                0,
-               PyDestructor,
                0,
-               __getattr,
-               __setattr,
                0,
-               __repr,
-               0
+               0,
+               py_base_repr,
+               0,0,0,0,0,0,
+               py_base_getattro,
+               py_base_setattro,
+               0,0,0,0,0,0,0,0,0,
+               Methods
 };
 
 
 PyParentObject KX_BlenderMaterial::Parents[] = {
-       &PyObjectPlus::Type,
        &KX_BlenderMaterial::Type,
+       &PyObjectPlus::Type,
        NULL
 };
 
 
-PyObject* KX_BlenderMaterial::_getattr(const STR_String& attr)
+PyObject* KX_BlenderMaterial::py_getattro(PyObject *attr)
 {
-       // nodda ?
-       _getattr_up(PyObjectPlus);
+       py_getattro_up(PyObjectPlus);
 }
 
-int KX_BlenderMaterial::_setattr(const STR_String& attr, PyObject *pyvalue)
+int KX_BlenderMaterial::py_setattro(PyObject *attr, PyObject *pyvalue)
 {
-       return PyObjectPlus::_setattr(attr, pyvalue);
+       return PyObjectPlus::py_setattro(attr, pyvalue);
 }
 
 
 KX_PYMETHODDEF_DOC( KX_BlenderMaterial, getShader , "getShader()")
 {
-#ifdef GL_ARB_fragment_shader
-       if( !RAS_EXT_support._ARB_fragment_shader) {
+       if( !GLEW_ARB_fragment_shader) {
                if(!mModified)
                        spit("Fragment shaders not supported");
        
                mModified = true;
-               Py_Return;
+               Py_RETURN_NONE;
        }
-#endif
 
-#ifdef GL_ARB_vertex_shader
-       if( !RAS_EXT_support._ARB_vertex_shader) {
+       if( !GLEW_ARB_vertex_shader) {
                if(!mModified)
                        spit("Vertex shaders not supported");
 
                mModified = true;
-               Py_Return;
+               Py_RETURN_NONE;
        }
-#endif
 
-#ifdef GL_ARB_shader_objects
-       if(!RAS_EXT_support._ARB_shader_objects)  {
+       if(!GLEW_ARB_shader_objects)  {
                if(!mModified)
                        spit("GLSL not supported");
                mModified = true;
-               Py_Return;
+               Py_RETURN_NONE;
        }
        else {
                // returns Py_None on error
@@ -956,18 +820,14 @@ KX_PYMETHODDEF_DOC( KX_BlenderMaterial, getShader , "getShader()")
 
                if(!mShader && !mModified) {
                        mShader = new BL_Shader();
-                       for(int i= 0; i<mMaterial->num_enabled; i++) {
-                               if(mMaterial->mapping[i].mapping & USEENV )
-                                       mShader->InitializeSampler(SAMP_CUBE, i, 0, mTextures[i]);
-                               else
-                                       mShader->InitializeSampler(SAMP_2D, i, 0, mTextures[i]);
-                       }
                        mModified = true;
                }
 
                if(mShader && !mShader->GetError()) {
-                       Py_INCREF(mShader);
-                       return mShader;
+                       m_flag &= ~RAS_BLENDERGLSL;
+                       mMaterial->SetSharedMaterial(true);
+                       mScene->GetBucketManager()->ReleaseDisplayLists(this);
+                       return mShader->GetProxy();
                }else
                {
                        // decref all references to the object
@@ -975,23 +835,26 @@ KX_PYMETHODDEF_DOC( KX_BlenderMaterial, getShader , "getShader()")
                        // We will then go back to fixed functionality
                        // for this material
                        if(mShader) {
-                               if(mShader->ob_refcnt > 1) {
-                                       Py_DECREF(mShader);
-                               }
-                               else {
-                                       delete mShader;
-                                       mShader=0;
-                               }
+                               delete mShader; /* will handle python de-referencing */
+                               mShader=0;
                        }
                }
-               Py_Return;
+               Py_RETURN_NONE;
        }
        PyErr_Format(PyExc_ValueError, "GLSL Error");
        return NULL;
+}
+
 
-#else
-       Py_Return;
-#endif//GL_ARB_shader_objects
+void KX_BlenderMaterial::SetBlenderGLSLShader(void)
+{
+       if(!mBlenderShader)
+               mBlenderShader = new BL_BlenderShader(mScene, mMaterial->material, m_lightlayer);
+
+       if(!mBlenderShader->Ok()) {
+               delete mBlenderShader;
+               mBlenderShader = 0;
+       }
 }
 
 KX_PYMETHODDEF_DOC( KX_BlenderMaterial, getMaterialIndex, "getMaterialIndex()")
@@ -1028,7 +891,7 @@ static unsigned int GL_array[11] = {
 KX_PYMETHODDEF_DOC( KX_BlenderMaterial, setBlending , "setBlending( GameLogic.src, GameLogic.dest)")
 {
        unsigned int b[2];
-       if(PyArg_ParseTuple(args, "ii", &b[0], &b[1]))
+       if(PyArg_ParseTuple(args, "ii:setBlending", &b[0], &b[1]))
        {
                bool value_found[2] = {false, false};
                for(int i=0; i<11; i++)
@@ -1048,7 +911,7 @@ KX_PYMETHODDEF_DOC( KX_BlenderMaterial, setBlending , "setBlending( GameLogic.sr
                        return NULL;
                }
                mUserDefBlend = true;
-               Py_Return;
+               Py_RETURN_NONE;
        }
        return NULL;
 }