BGE Python API cleanup - no functionality changes
[blender.git] / source / gameengine / Ketsji / KX_BlenderMaterial.cpp
index 5efe1ad26ca06ec523c75e99f644155e60dd5c44..6722cd232fe8c8a3392a8414585001ed25992bbb 100644 (file)
@@ -6,22 +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>
-/* #if defined(__sun__) && !defined(__sparc__)
-#include <mesa/glu.h>
-#else
-*/
-#include <GL/glu.h>
-/* #endif */
-#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"
 
@@ -52,9 +35,11 @@ extern "C" {
 #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(
@@ -62,7 +47,6 @@ KX_BlenderMaterial::KX_BlenderMaterial(
        BL_Material *data,
        bool skin,
        int lightlayer,
-       void *clientobject,
        PyTypeObject *T
        )
 :      PyObjectPlus(T),
@@ -73,14 +57,14 @@ 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),
@@ -90,19 +74,16 @@ KX_BlenderMaterial::KX_BlenderMaterial(
 {
        // --------------------------------
        // 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 |=(mMaterial->ras_mode &ALPHA_TEST)!=0?RAS_FORCEALPHA: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
        int enabled = mMaterial->num_enabled;
        int max = BL_Texture::GetMaxUnits();
        mMaterial->num_enabled = enabled>=max?max:enabled;
 
-       // base class
-       m_enabled = mMaterial->num_enabled;
-
        // test the sum of the various modes for equality
        // so we can ether accept or reject this material 
        // as being equal, this is rather important to 
@@ -113,7 +94,7 @@ 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));
 
 }
 
@@ -144,12 +125,15 @@ void KX_BlenderMaterial::OnConstruction()
        if (mConstructed)
                // when material are reused between objects
                return;
+       
+       if(mMaterial->glslmat)
+               SetBlenderGLSLShader();
 
        // for each unique material...
        int i;
        for(i=0; i<mMaterial->num_enabled; i++) {
                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;
                        }
@@ -166,21 +150,49 @@ void KX_BlenderMaterial::OnConstruction()
                        }
                }
        }
+
        mBlendFunc[0] =0;
        mBlendFunc[1] =0;
        mConstructed = true;
 }
 
+void KX_BlenderMaterial::EndFrame()
+{
+       if(mLastBlenderShader) {
+               mLastBlenderShader->SetProg(false);
+               mLastBlenderShader = NULL;
+       }
+
+       if(mLastShader) {
+               mLastShader->SetProg(false);
+               mLastShader = NULL;
+       }
+}
+
 void KX_BlenderMaterial::OnExit()
 {
        if( mShader ) {
-                //note, the shader here is allocated, per unique material
-                //and this function is called per face
-               mShader->SetProg(0);
+               //note, the shader here is allocated, per unique material
+               //and this function is called per face
+               if(mShader == mLastShader) {
+                       mShader->SetProg(false);
+                       mLastShader = NULL;
+               }
+
                delete mShader;
                mShader = 0;
        }
 
+       if( mBlenderShader ) {
+               if(mBlenderShader == mLastBlenderShader) {
+                       mBlenderShader->SetProg(false);
+                       mLastBlenderShader = NULL;
+               }
+
+               delete mBlenderShader;
+               mBlenderShader = 0;
+       }
+
        BL_Texture::ActivateFirst();
        for(int i=0; i<mMaterial->num_enabled; i++) {
                BL_Texture::ActivateUnit(i);
@@ -189,24 +201,30 @@ void KX_BlenderMaterial::OnExit()
        }
 
        if( mMaterial->tface ) 
-               set_tpage(mMaterial->tface);
+               GPU_set_tpage(mMaterial->tface);
 }
 
 
 void KX_BlenderMaterial::setShaderData( bool enable, RAS_IRasterizer *ras)
 {
-       MT_assert(RAS_EXT_support._ARB_shader_objects && mShader);
+       MT_assert(GLEW_ARB_shader_objects && mShader);
 
        int i;
        if( !enable || !mShader->Ok() ) {
                // frame cleanup.
-               mShader->SetProg(false);
+               if(mShader == mLastShader) {
+                       mShader->SetProg(false);
+                       mLastShader = NULL;
+               }
+
+               ras->SetBlendingMode(TF_SOLID);
                BL_Texture::DisableAllTextures();
                return;
        }
 
        BL_Texture::DisableAllTextures();
        mShader->SetProg(true);
+       mLastShader = mShader;
        
        BL_Texture::ActivateFirst();
 
@@ -220,29 +238,60 @@ void KX_BlenderMaterial::setShaderData( bool enable, RAS_IRasterizer *ras)
        }
 
        if(!mUserDefBlend) {
-               setDefaultBlending();
+               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]);
        }
 }
 
+void KX_BlenderMaterial::setBlenderShaderData( bool enable, RAS_IRasterizer *ras)
+{
+       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();
+
+               mBlenderShader->SetProg(true, ras->GetTime());
+               mLastBlenderShader= mBlenderShader;
+       }
+}
 
 void KX_BlenderMaterial::setTexData( bool enable, RAS_IRasterizer *ras)
 {
-       if(RAS_EXT_support._ARB_shader_objects && mShader) 
-               mShader->SetProg(false);
-
        BL_Texture::DisableAllTextures();
-       if( !enable )
+
+       if( !enable ) {
+               ras->SetBlendingMode(TF_SOLID);
                return;
+       }
 
        BL_Texture::ActivateFirst();
 
        if( mMaterial->IdMode == DEFAULT_BLENDER ) {
-               setDefaultBlending();
+               ras->SetBlendingMode(mMaterial->transp);
                return;
        }
 
@@ -252,7 +301,7 @@ void KX_BlenderMaterial::setTexData( bool enable, RAS_IRasterizer *ras)
                        mTextures[0].ActivateTexture();
                        mTextures[0].setTexEnv(0, true);
                        mTextures[0].SetMapping(mMaterial->mapping[0].mapping);
-                       setDefaultBlending(); 
+                       ras->SetBlendingMode(mMaterial->transp);
                }
                return;
        }
@@ -275,9 +324,12 @@ void KX_BlenderMaterial::setTexData( bool enable, RAS_IRasterizer *ras)
        }
 
        if(!mUserDefBlend) {
-               setDefaultBlending();
+               ras->SetBlendingMode(mMaterial->transp);
        }
        else {
+               ras->SetBlendingMode(TF_SOLID);
+               ras->SetBlendingMode(-1); // indicates custom mode
+
                glEnable(GL_BLEND);
                glBlendFunc(mBlendFunc[0], mBlendFunc[1]);
        }
@@ -293,29 +345,31 @@ KX_BlenderMaterial::ActivatShaders(
        // reset... 
        if(tmp->mMaterial->IsShared()) 
                cachingInfo =0;
-       
+
+       if(mLastBlenderShader) {
+               mLastBlenderShader->SetProg(false);
+               mLastBlenderShader= NULL;
+       }
+
        if (GetCachingInfo() != cachingInfo) {
 
                if (!cachingInfo)
-                       tmp->setShaderData( false, rasty);
+                       tmp->setShaderData(false, rasty);
                
                cachingInfo = GetCachingInfo();
        
-               if (rasty->GetDrawingMode() == RAS_IRasterizer::KX_TEXTURED ) {
-                       tmp->setShaderData( true, rasty);
-                       rasty->EnableTextures(true);
-               }
-               else {
-                       tmp->setShaderData( false, rasty);
-                       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);
@@ -329,6 +383,49 @@ KX_BlenderMaterial::ActivatShaders(
        ActivateTexGen(rasty);
 }
 
+void
+KX_BlenderMaterial::ActivateBlenderShaders(
+       RAS_IRasterizer* rasty, 
+       TCachingInfo& cachingInfo)const
+{
+       KX_BlenderMaterial *tmp = const_cast<KX_BlenderMaterial*>(this);
+
+       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);
+
+               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) ||
+                   (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
 KX_BlenderMaterial::ActivateMat( 
        RAS_IRasterizer* rasty,  
@@ -336,27 +433,35 @@ KX_BlenderMaterial::ActivateMat(
        )const
 {
        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,rasty );
                
                cachingInfo = GetCachingInfo();
 
-               if (rasty->GetDrawingMode() == RAS_IRasterizer::KX_TEXTURED) {
+               if (rasty->GetDrawingMode() == RAS_IRasterizer::KX_TEXTURED)
                        tmp->setTexData( true,rasty  );
-                       rasty->EnableTextures(true);
-               }
-               else{
+               else
                        tmp->setTexData( false,rasty);
-                       rasty->EnableTextures(false);
-               }
 
                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);
@@ -365,77 +470,113 @@ KX_BlenderMaterial::ActivateMat(
                else
                        rasty->SetLines(false);
        }
+
        ActivatGLMaterials(rasty);
        ActivateTexGen(rasty);
 }
 
-
 bool 
 KX_BlenderMaterial::Activate( 
        RAS_IRasterizer* rasty,  
        TCachingInfo& cachingInfo
        )const
 {
-       bool dopass = false;
-       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 {
+                       if(mShader == mLastShader) {
+                               mShader->SetProg(false);
+                               mLastShader = NULL;
+                       }
+                       mPass = 0;
+                       return false;
+               }
+       }
+       else if( GLEW_ARB_shader_objects && (mBlenderShader && mBlenderShader->Ok() ) ) {
+               if(mPass++ == 0) {
+                       ActivateBlenderShaders(rasty, cachingInfo);
+                       return true;
                }
                else {
-                       mShader->SetProg(false);
                        mPass = 0;
-                       dopass = false;
-                       return dopass;
+                       return false;
                }
        }
        else {
-               switch (mPass++)
-               {
-                       case 0:
-                               ActivateMat(rasty, cachingInfo);
-                               dopass = true;
-                               break;
-                       default:
-                               mPass = 0;
-                               dopass = false;
-                               break;
+               if(mPass++ == 0) {
+                       ActivateMat(rasty, cachingInfo);
+                       return true;
+               }
+               else {
+                       mPass = 0;
+                       return false;
                }
        }
-       return dopass;
 }
 
-void KX_BlenderMaterial::ActivateMeshSlot(const KX_MeshSlot & ms, RAS_IRasterizer* rasty) const
+bool KX_BlenderMaterial::UsesLighting(RAS_IRasterizer *rasty) const
 {
-       if(mShader && RAS_EXT_support._ARB_shader_objects)
+       if(!RAS_IPolyMaterial::UsesLighting(rasty))
+               return false;
+
+       if(mShader && mShader->Ok())
+               return true;
+       else if(mBlenderShader && mBlenderShader->Ok())
+               return false;
+       else
+               return true;
+}
+
+void KX_BlenderMaterial::ActivateMeshSlot(const RAS_MeshSlot & ms, RAS_IRasterizer* rasty) const
+{
+       if(mShader && GLEW_ARB_shader_objects) {
                mShader->Update(ms, rasty);
+       }
+       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);
+       }
 }
 
 void KX_BlenderMaterial::ActivatGLMaterials( RAS_IRasterizer* rasty )const
 {
-       rasty->SetSpecularity(
-               mMaterial->speccolor[0]*mMaterial->spec_f,
-               mMaterial->speccolor[1]*mMaterial->spec_f,
-               mMaterial->speccolor[2]*mMaterial->spec_f,
-               mMaterial->spec_f
-       );
+       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->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->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 );
 
-       rasty->SetEmissive(     
-               mMaterial->matcolor[0]*mMaterial->emit,
-               mMaterial->matcolor[1]*mMaterial->emit,
-               mMaterial->matcolor[2]*mMaterial->emit,
-               1.0 );
+               rasty->SetAmbient(mMaterial->amb);
+       }
 
-       rasty->SetAmbient(mMaterial->amb);
        if (mMaterial->material)
                rasty->SetPolygonOffset(-mMaterial->material->zoffs, 0.0);
 }
@@ -443,59 +584,43 @@ void KX_BlenderMaterial::ActivatGLMaterials( RAS_IRasterizer* rasty )const
 
 void KX_BlenderMaterial::ActivateTexGen(RAS_IRasterizer *ras) const
 {
-       if(mShader && RAS_EXT_support._ARB_shader_objects)
-               if(mShader->GetAttribute() == BL_Shader::SHD_TANGENT)
-                       ras->SetAttrib(RAS_IRasterizer::RAS_TEXTANGENT);
+       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);
+                       }
+               }
 
-       for(int i=0; i<mMaterial->num_enabled; i++) {
-               int mode = mMaterial->mapping[i].mapping;
+               ras->SetTexCoordNum(mMaterial->num_enabled);
 
-               if (mode &USECUSTOMUV)
-               {
-                       STR_String str = mMaterial->mapping[i].uvCoName;
-                       if (!str.IsEmpty())
-                               ras->SetTexCoords(RAS_IRasterizer::RAS_TEXCO_UV2, i);
-                       continue;
-               }
+               for(int i=0; i<mMaterial->num_enabled; i++) {
+                       int mode = mMaterial->mapping[i].mapping;
 
-               if( mode &(USEREFL|USEOBJ))
-                       ras->SetTexCoords(RAS_IRasterizer::RAS_TEXCO_GEN, i);
-               else if(mode &USEORCO)
-                       ras->SetTexCoords(RAS_IRasterizer::RAS_TEXCO_ORCO, i);
-               else if(mode &USENORM)
-                       ras->SetTexCoords(RAS_IRasterizer::RAS_TEXCO_NORM, i);
-               else if(mode &USEUV)
-                       ras->SetTexCoords(RAS_IRasterizer::RAS_TEXCO_UV1, i);
-               else if(mode &USETANG)
-                       ras->SetTexCoords(RAS_IRasterizer::RAS_TEXTANGENT, i);
-               else 
-                       ras->SetTexCoords(RAS_IRasterizer::RAS_TEXCO_DISABLE, i);
-       }
-}
+                       if (mode &USECUSTOMUV)
+                       {
+                               STR_String str = mMaterial->mapping[i].uvCoName;
+                               if (!str.IsEmpty())
+                                       ras->SetTexCoord(RAS_IRasterizer::RAS_TEXCO_UV2, i);
+                               continue;
+                       }
 
-bool KX_BlenderMaterial::setDefaultBlending()
-{
-       if( mMaterial->transp &TF_ADD) {
-               glEnable(GL_BLEND);
-               glBlendFunc(GL_ONE, GL_ONE);
-               glDisable ( GL_ALPHA_TEST );
-               return true;
-       }
-       
-       if( mMaterial->transp & TF_ALPHA ) {
-               glEnable(GL_BLEND);
-               glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-               glDisable ( GL_ALPHA_TEST );
-               return true;
-       }
-       
-       if( mMaterial->transp & TF_CLIP ) {
-               glDisable(GL_BLEND); 
-               glEnable ( GL_ALPHA_TEST );
-               glAlphaFunc(GL_GREATER, 0.5f);
-               return true;
+                       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);
+               }
        }
-       return false;
 }
 
 void KX_BlenderMaterial::setTexMatrixData(int i)
@@ -503,8 +628,7 @@ void KX_BlenderMaterial::setTexMatrixData(int i)
        glMatrixMode(GL_TEXTURE);
        glLoadIdentity();
 
-#ifdef GL_ARB_texture_cube_map
-       if( RAS_EXT_support._ARB_texture_cube_map && 
+       if( GLEW_ARB_texture_cube_map && 
                mTextures[i].GetTextureType() == GL_TEXTURE_CUBE_MAP_ARB && 
                mMaterial->mapping[i].mapping & USEREFL) {
                glScalef( 
@@ -514,7 +638,6 @@ void KX_BlenderMaterial::setTexMatrixData(int i)
                );
        }
        else
-#endif
        {
                glScalef( 
                        mMaterial->mapping[i].scale[0], 
@@ -572,8 +695,7 @@ void KX_BlenderMaterial::setObjectMatrixData(int i, RAS_IRasterizer *ras)
        glEnable(GL_TEXTURE_GEN_T);
        glEnable(GL_TEXTURE_GEN_R);
 
-       MT_Matrix4x4 mvmat;
-       ras->GetViewMatrix(mvmat);
+       const MT_Matrix4x4& mvmat = ras->GetViewMatrix();
 
        glMatrixMode(GL_TEXTURE);
        glLoadIdentity();
@@ -626,69 +748,71 @@ PyMethodDef KX_BlenderMaterial::Methods[] =
        {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)
 {
-       _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
@@ -700,9 +824,10 @@ KX_PYMETHODDEF_DOC( KX_BlenderMaterial, getShader , "getShader()")
                }
 
                if(mShader && !mShader->GetError()) {
+                       m_flag &= ~RAS_BLENDERGLSL;
                        mMaterial->SetSharedMaterial(true);
-                       Py_INCREF(mShader);
-                       return mShader;
+                       mScene->GetBucketManager()->ReleaseDisplayLists(this);
+                       return mShader->GetProxy();
                }else
                {
                        // decref all references to the object
@@ -710,26 +835,28 @@ 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()")
 {
        return PyInt_FromLong( mMaterial->material_index );
@@ -764,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++)
@@ -784,7 +911,7 @@ KX_PYMETHODDEF_DOC( KX_BlenderMaterial, setBlending , "setBlending( GameLogic.sr
                        return NULL;
                }
                mUserDefBlend = true;
-               Py_Return;
+               Py_RETURN_NONE;
        }
        return NULL;
 }