BGE 2d-filter, custom shaders now can have depth texture and luminance texture
authorHamed Zaghaghi <hamed.zaghaghi@gmail.com>
Sat, 28 Jun 2008 03:18:11 +0000 (03:18 +0000)
committerHamed Zaghaghi <hamed.zaghaghi@gmail.com>
Sat, 28 Jun 2008 03:18:11 +0000 (03:18 +0000)
source/blender/makesdna/DNA_actuator_types.h
source/blender/src/buttons_logic.c
source/gameengine/BlenderRoutines/KX_BlenderRenderTools.cpp
source/gameengine/BlenderRoutines/KX_BlenderRenderTools.h
source/gameengine/Converter/KX_ConvertActuators.cpp
source/gameengine/GameLogic/SCA_2DFilterActuator.cpp
source/gameengine/GameLogic/SCA_2DFilterActuator.h
source/gameengine/Rasterizer/RAS_2DFilterManager.cpp
source/gameengine/Rasterizer/RAS_2DFilterManager.h
source/gameengine/Rasterizer/RAS_IRenderTools.h

index 20316f2..51f03a6 100644 (file)
@@ -192,11 +192,13 @@ typedef struct bVisibilityActuator {
 } bVisibilityActuator;
 
 typedef struct bTwoDFilterActuator{
-       char pad[4];
-       /* Tells what type of 2D Filter*/
+       char pad[2];
+       /* bitwise flag for enabling or disabling depth(bit 0) and luminance(bit 1) */
+       short texture_flag;
+       /* Tells what type of 2D Filter */
        short type;
        /* (flag == 0) means 2D filter is activate and
-          (flag != 0) means 2D filter is inactive*/
+          (flag != 0) means 2D filter is inactive */
        short flag;
        int   int_arg;
        /* a float argument */
index 297cde8..bfe2c92 100644 (file)
@@ -2382,7 +2382,11 @@ static short draw_actuatorbuttons(Object *ob, bActuator *act, uiBlock *block, sh
                                break;
                        case ACT_2DFILTER_CUSTOMFILTER:
                                uiDefButI(block, NUM, B_REDR, "Pass Number:", xco+30,yco-44,width-60,19,&tdfa->int_arg,0.0,MAX_RENDER_PASS-1,0.0,0.0,"Set motion blur value");
-                               uiDefIDPoinBut(block, test_scriptpoin_but, ID_SCRIPT, 1, "Script: ", xco+30,yco-64,width-60, 19, &tdfa->text, "");
+                               uiDefIDPoinBut(block, test_scriptpoin_but, ID_SCRIPT, 1, "Script: ", xco+30,yco-64,width/2-32, 19, &tdfa->text, "");
+                               uiDefButS(block, TOG|BIT|0, B_REDR, "Depth", xco+width/2+2 , yco - 64, width/4-16 , 19,
+                                               &tdfa->texture_flag, 0.0, 0.0, 0, 0, "Includes Depth Texture (bgl_DepthTexture)");
+                               uiDefButS(block, TOG|BIT|1, B_REDR, "Luminance", xco+3*width/4-14 , yco - 64, width/4-16 , 19,
+                                               &tdfa->texture_flag, 0.0, 0.0, 0, 0, "Includes Luminance Texture (bgl_LuminanceTexture)");
                                break;
                }
                
index 07a3649..73d2870 100644 (file)
@@ -483,9 +483,9 @@ void KX_BlenderRenderTools::MotionBlur(RAS_IRasterizer* rasterizer)
        }
 }
 
-void KX_BlenderRenderTools::Update2DFilter(RAS_2DFilterManager::RAS_2DFILTER_MODE filtermode, int pass, STR_String& text)
+void KX_BlenderRenderTools::Update2DFilter(RAS_2DFilterManager::RAS_2DFILTER_MODE filtermode, int pass, STR_String& text, short texture_flag)
 {
-       m_filtermanager.EnableFilter(filtermode, pass, text);
+       m_filtermanager.EnableFilter(filtermode, pass, text, texture_flag);
 }
 
 void KX_BlenderRenderTools::Render2DFilters(RAS_ICanvas* canvas)
index 31eaa14..7748e31 100644 (file)
@@ -101,7 +101,7 @@ public:
 
        virtual void MotionBlur(RAS_IRasterizer* rasterizer);
 
-       virtual void Update2DFilter(RAS_2DFilterManager::RAS_2DFILTER_MODE filtermode, int pass, STR_String& text);
+       virtual void Update2DFilter(RAS_2DFilterManager::RAS_2DFILTER_MODE filtermode, int pass, STR_String& text, short texture_flag);
 
        virtual void Render2DFilters(RAS_ICanvas* canvas);
 
index a50c072..ea26c55 100644 (file)
@@ -944,7 +944,7 @@ void BL_ConvertActuators(char* maggiename,
                        }
             
                        tmp = new SCA_2DFilterActuator(gameobj, filtermode, _2dfilter->flag,
-                               _2dfilter->float_arg,_2dfilter->int_arg,ketsjiEngine->GetRasterizer(),rendertools);
+                               _2dfilter->float_arg,_2dfilter->int_arg,_2dfilter->texture_flag,ketsjiEngine->GetRasterizer(),rendertools);
 
                        if (_2dfilter->text)
                        {
index f3b5b1f..56249bb 100644 (file)
@@ -18,6 +18,7 @@ SCA_2DFilterActuator::SCA_2DFilterActuator(
                short flag,
                float float_arg,
                int int_arg,
+               short texture_flag,
                RAS_IRasterizer* rasterizer,
                RAS_IRenderTools* rendertools,
         PyTypeObject* T)
@@ -25,6 +26,7 @@ SCA_2DFilterActuator::SCA_2DFilterActuator(
      m_type(type),
         m_flag(flag),
         m_int_arg(int_arg),
+        m_texture_flag(texture_flag),
         m_float_arg(float_arg),
         m_rasterizer(rasterizer),
         m_rendertools(rendertools)
@@ -72,7 +74,7 @@ bool SCA_2DFilterActuator::Update()
        }
        else if(m_type < RAS_2DFilterManager::RAS_2DFILTER_NUMBER_OF_FILTERS)
        {
-               m_rendertools->Update2DFilter(m_type, m_int_arg, m_shaderText);
+               m_rendertools->Update2DFilter(m_type, m_int_arg, m_shaderText, m_texture_flag);
        }
     return true;
 }
index 7b0cfff..451a7b9 100644 (file)
@@ -16,6 +16,7 @@ private:
        short m_flag;
        float m_float_arg;
        int   m_int_arg;
+       short m_texture_flag;
        STR_String      m_shaderText;
        RAS_IRasterizer* m_rasterizer;
        RAS_IRenderTools* m_rendertools;
@@ -28,6 +29,7 @@ public:
                short flag,
                float float_arg,
                int int_arg,
+               short texture_flag,
                RAS_IRasterizer* rasterizer,
                RAS_IRenderTools* rendertools,
         PyTypeObject* T=&Type
index e9ab4cc..e2761ce 100644 (file)
@@ -54,7 +54,7 @@
 
 
 RAS_2DFilterManager::RAS_2DFilterManager():
-texname(-1), texturewidth(-1), textureheight(-1),
+texturewidth(-1), textureheight(-1),
 canvaswidth(-1), canvasheight(-1),
 numberoffilters(0)
 {
@@ -72,8 +72,9 @@ numberoffilters(0)
        {
                m_filters[passindex] = 0;
                m_enabled[passindex] = 0;
+               texflag[passindex] = 0;
        }
-
+       texname[0] = texname[1] = texname[2] = -1;
 }
 
 RAS_2DFilterManager::~RAS_2DFilterManager()
@@ -150,30 +151,54 @@ unsigned int RAS_2DFilterManager::CreateShaderProgram(int filtermode)
                return 0;
 }
 
-void RAS_2DFilterManager::StartShaderProgram(unsigned int shaderprogram)
+void RAS_2DFilterManager::StartShaderProgram(int passindex)
 {
        GLint uniformLoc;
-       glUseProgramObjectARB(shaderprogram);
-       uniformLoc = glGetUniformLocationARB(shaderprogram, "bgl_RenderedTexture");
+       glUseProgramObjectARB(m_filters[passindex]);
+       uniformLoc = glGetUniformLocationARB(m_filters[passindex], "bgl_RenderedTexture");
        glActiveTextureARB(GL_TEXTURE0);
-       //glActiveTexture(GL_TEXTURE0);
-       glBindTexture(GL_TEXTURE_2D, texname);
+       glBindTexture(GL_TEXTURE_2D, texname[0]);
 
     if (uniformLoc != -1)
     {
                glUniform1iARB(uniformLoc, 0);
     }
-       uniformLoc = glGetUniformLocationARB(shaderprogram, "bgl_TextureCoordinateOffset");
+
+    /* send depth texture to glsl program if it needs */
+       if(texflag[passindex] & 0x1){
+       uniformLoc = glGetUniformLocationARB(m_filters[passindex], "bgl_DepthTexture");
+       glActiveTextureARB(GL_TEXTURE1);
+       glBindTexture(GL_TEXTURE_2D, texname[1]);
+
+       if (uniformLoc != -1)
+       {
+               glUniform1iARB(uniformLoc, 1);
+       }
+    }
+
+    /* send luminance texture to glsl program if it needs */
+       if(texflag[passindex] & 0x1){
+       uniformLoc = glGetUniformLocationARB(m_filters[passindex], "bgl_LuminanceTexture");
+       glActiveTextureARB(GL_TEXTURE2);
+       glBindTexture(GL_TEXTURE_2D, texname[2]);
+
+       if (uniformLoc != -1)
+       {
+               glUniform1iARB(uniformLoc, 2);
+       }
+       }
+       
+       uniformLoc = glGetUniformLocationARB(m_filters[passindex], "bgl_TextureCoordinateOffset");
     if (uniformLoc != -1)
     {
         glUniform2fvARB(uniformLoc, 9, textureoffsets);
     }
-       uniformLoc = glGetUniformLocationARB(shaderprogram, "bgl_RenderedTextureWidth");
+       uniformLoc = glGetUniformLocationARB(m_filters[passindex], "bgl_RenderedTextureWidth");
     if (uniformLoc != -1)
     {
                glUniform1fARB(uniformLoc,texturewidth);
     }
-       uniformLoc = glGetUniformLocationARB(shaderprogram, "bgl_RenderedTextureHeight");
+       uniformLoc = glGetUniformLocationARB(m_filters[passindex], "bgl_RenderedTextureHeight");
     if (uniformLoc != -1)
     {
                glUniform1fARB(uniformLoc,textureheight);
@@ -187,14 +212,33 @@ void RAS_2DFilterManager::EndShaderProgram()
 
 void RAS_2DFilterManager::SetupTexture()
 {
-       if(texname!=-1)
+       if(texname[0]!=-1 || texname[1]!=-1)
        {
-               glDeleteTextures(1,(const GLuint *)&texname);
+               glDeleteTextures(2, texname);
        }
-       glGenTextures(1, (GLuint *)&texname);
-       glBindTexture(GL_TEXTURE_2D, texname);
+       glGenTextures(3, texname);
+
+       glBindTexture(GL_TEXTURE_2D, texname[0]);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texturewidth, textureheight, 0, GL_RGB,
-               GL_UNSIGNED_BYTE, 0);
+                       GL_UNSIGNED_BYTE, 0);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
+
+       glBindTexture(GL_TEXTURE_2D, texname[1]);
+       glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, texturewidth,textureheight, 0, GL_DEPTH_COMPONENT,
+                       GL_FLOAT,NULL);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE,
+                               GL_NONE);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
+       
+       glBindTexture(GL_TEXTURE_2D, texname[2]);
+       glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE16, texturewidth, textureheight, 0, GL_LUMINANCE,
+                       GL_UNSIGNED_BYTE, 0);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
@@ -246,12 +290,27 @@ void RAS_2DFilterManager::RenderFilters(RAS_ICanvas* canvas)
 
        int passindex;
        bool first = true;
+
        for(passindex =0; passindex<MAX_RENDER_PASS; passindex++)
        {
                if(m_filters[passindex] && m_enabled[passindex])
                {
                        if(first)
                        {
+                               /* this pass needs depth texture*/
+                               if(texflag[passindex] & 0x1){
+                                       glActiveTextureARB(GL_TEXTURE1);
+                                       glBindTexture(GL_TEXTURE_2D, texname[1]);
+                                       glCopyTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT, 0,0, texturewidth,textureheight, 0);
+                               }
+                               
+                               /* this pass needs luminance texture*/
+                               if(texflag[passindex] & 0x2){
+                                       glActiveTextureARB(GL_TEXTURE2);
+                                       glBindTexture(GL_TEXTURE_2D, texname[2]);
+                                       glCopyTexImage2D(GL_TEXTURE_2D,0,GL_LUMINANCE16, 0,0, texturewidth,textureheight, 0);
+                               }
+
                                glGetIntegerv(GL_VIEWPORT,(GLint *)viewport);
                                glViewport(0, 0, texturewidth, textureheight);
 
@@ -263,11 +322,13 @@ void RAS_2DFilterManager::RenderFilters(RAS_ICanvas* canvas)
                                first = false;
                        }
                        
-                       StartShaderProgram(m_filters[passindex]);
+                       StartShaderProgram(passindex);
 
-                       glBindTexture(GL_TEXTURE_2D, texname);
-                       glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 0, 0, texturewidth, textureheight, 0);
 
+                       glActiveTextureARB(GL_TEXTURE0);
+                       glBindTexture(GL_TEXTURE_2D, texname[0]);
+                       glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 0, 0, texturewidth, textureheight, 0);
+                          
                        glClear(GL_COLOR_BUFFER_BIT);
 
                        glBegin(GL_QUADS);
@@ -288,7 +349,7 @@ void RAS_2DFilterManager::RenderFilters(RAS_ICanvas* canvas)
        }
 }
 
-void RAS_2DFilterManager::EnableFilter(RAS_2DFILTER_MODE mode, int pass, STR_String& text)
+void RAS_2DFilterManager::EnableFilter(RAS_2DFILTER_MODE mode, int pass, STR_String& text, short tflag)
 {
        if(!isshadersupported)
                return;
@@ -313,11 +374,13 @@ void RAS_2DFilterManager::EnableFilter(RAS_2DFILTER_MODE mode, int pass, STR_Str
                        glDeleteObjectARB(m_filters[pass]);
                m_enabled[pass] = 0;
                m_filters[pass] = 0;
+               texflag[pass] = 0;
                return;
        }
        
        if(mode == RAS_2DFILTER_CUSTOMFILTER)
        {
+               texflag[pass] = tflag;
                if(m_filters[pass])
                        glDeleteObjectARB(m_filters[pass]);
                m_filters[pass] = CreateShaderProgram(text.Ptr());
index cff8685..faf7c65 100644 (file)
@@ -37,17 +37,21 @@ class RAS_2DFilterManager
 private:
        unsigned int    CreateShaderProgram(char* shadersource);
        unsigned int    CreateShaderProgram(int filtermode);
-       void                    StartShaderProgram(unsigned int shaderprogram);
+       void                    StartShaderProgram(int passindex);
        void                    EndShaderProgram();
 
        float                   textureoffsets[18];
        float                   view[4];
-       unsigned int    texname;
+       /* texname[0] contains render to texture, texname[1] contains depth texture,  texname[2] contains luminance texture*/
+       unsigned int    texname[3]; 
        int                             texturewidth;
        int                             textureheight;
        int                             canvaswidth;
        int                             canvasheight;
        int                             numberoffilters;
+       /* bit 0: enable/disable depth texture
+        * bit 1: enable/disable luminance texture*/
+       short                   texflag[MAX_RENDER_PASS];
 
        bool                    isshadersupported;
 public:
@@ -83,6 +87,6 @@ public:
 
        void RenderFilters(RAS_ICanvas* canvas);
 
-       void EnableFilter(RAS_2DFILTER_MODE mode, int pass, STR_String& text);
+       void EnableFilter(RAS_2DFILTER_MODE mode, int pass, STR_String& text, short tflag);
 };
 #endif
index bcbf907..781f90d 100644 (file)
@@ -185,7 +185,7 @@ public:
 
        virtual
                void
-               Update2DFilter(RAS_2DFilterManager::RAS_2DFILTER_MODE filtermode, int pass, STR_String& text)=0;
+               Update2DFilter(RAS_2DFilterManager::RAS_2DFILTER_MODE filtermode, int pass, STR_String& text, short textureflag)=0;
 
        virtual
                void