Menu

Custom GLSL Post Shader

Help
2015-12-01
2015-12-02
  • Jerome.D (BeanzMaster)

    Hi to all, i need help i'm trying to make a custom GLSLPostShader

    I played with the GLSLPostShader sample and i take the GLSLPostBlurShader as Template but my shader don't work. Anyone can help me ? I attached the shader code
    And this is the original Vertex/Pixel shader code :


    Language: OpenGL 2  GLSL
    
    Type: Post processing filter.
    
    Inputs
    
    sceneTex (sampler2D): the final scene image.
    Ouputs: color buffer
    
    Shader code:
    
    [Vertex_Shader]
    void main(void)
    {
      gl_Position = ftransform();
      gl_TexCoord[0] = gl_MultiTexCoord0;
    }
    
    [Pixel_Shader]
    uniform sampler2D sceneTex; // 0
    uniform float vx_offset;
    void main() 
    { 
      vec2 uv = gl_TexCoord[0].xy;
    
      vec3 tc = vec3(1.0, 0.0, 0.0);
      if (uv.x < (vx_offset-0.005))
      {
        vec3 pixcol = texture2D(sceneTex, uv).rgb;
        vec3 colors[3];
        colors[0] = vec3(0.,0.,1.);
        colors[1] = vec3(1.,1.,0.);
        colors[2] = vec3(1.,0.,0.);
        float lum = (pixcol.r+pixcol.g+pixcol.b)/3.;
        int ix = (lum < 0.5)? 0:1;
        tc = mix(colors[ix],colors[ix+1],(lum-float(ix)*0.5)/0.5);
      }
      else if (uv.x>=(vx_offset+0.005))
      {
        tc = texture2D(sceneTex, uv).rgb;
      }
        gl_FragColor = vec4(tc, 1.0);
    }
    
     

    Last edit: Jerome.D (BeanzMaster) 2015-12-01
  • Jerome.D (BeanzMaster)

    I be back i've modified and simplify the shader

    unit GLSLPostThermalVisionShader;
    
    interface
    
    {$I GLScene.inc}
    
    uses
      System.Classes,
      // GLS
      GLTexture, GLScene, GLVectorGeometry, GLContext,
      GLSLShader, GLCustomShader, GLRenderContextInfo, GLTextureFormat,
      GLVectorTypes;
    
    
    type
      TGLCustomGLSLPostThermalVisionShader = class(TGLCustomGLSLShader, IGLPostShader)
      private
        // Implementing IGLPostShader.
        procedure DoUseTempTexture(const TempTexture: TGLTextureHandle;TextureTarget: TGLTextureTarget);
        function GetTextureTarget: TGLTextureTarget;
      protected
        procedure DoApply(var rci: TRenderContextInfo; Sender: TObject); override;
        function DoUnApply(var rci: TRenderContextInfo): Boolean; override;
      public
        constructor Create(AOwner: TComponent); override;
      end;
    
      TGLSLPostThermalVisionShader = class(TGLCustomGLSLPostThermalVisionShader)
      published
    
      end;
    
    //----------------------------------------------------------------------
    //----------------------------------------------------------------------
    //----------------------------------------------------------------------
    implementation
    //----------------------------------------------------------------------
    //----------------------------------------------------------------------
    //----------------------------------------------------------------------
    
    { TGLCustomGLSLPostThermalVisionShader }
    
    constructor TGLCustomGLSLPostThermalVisionShader.Create(
      AOwner: TComponent);
    begin
      inherited;
      with VertexProgram.Code do
      begin
        Add('void main(void) ');
        Add('{ ');
        Add('   gl_Position = ftransform();// gl_ModelViewProjectionMatrix * gl_Vertex; ');
        Add('   gl_TexCoord[0] = gl_MultiTexCoord0; ');
        Add('} ');
      end;
    
      with FragmentProgram.Code do
      begin
       Add('uniform sampler2D ScreenTex; ');
       Add(' ');
       Add('void main() ');
       Add('{ ');
       Add('   vec2 uv = gl_TexCoord[0].xy;');
       Add('   vec3 tc = vec3(1.0, 0.0, 0.0);');
       Add('   vec3 pixcol = texture2D(ScreenTex, uv).rgb; ');
       Add('   vec3 colors[3];');
       Add('   colors[0] = vec3(0.,0.,1.); ');
       Add('   colors[1] = vec3(1.,1.,0.); ');
       Add('   colors[2] = vec3(1.,0.,0.); ');
       Add('//     float lum = dot(vec3(0.30, 0.59, 0.11), pixcol.rgb); ');
       Add('   float lum = (pixcol.r+pixcol.g+pixcol.b)/3.;');
       Add('   tc = (lum < 0.5)?  mix(colors[0],colors[1],lum/0.5): mix(colors[1],colors[2],(lum-0.5)/0.5); ');
       Add('   gl_FragColor = vec4(tc, 1.0); ');
       Add('} ');
    
      end;
    
    
    
    end;
    
    procedure TGLCustomGLSLPostThermalVisionShader.DoApply(
      var rci: TRenderContextInfo; Sender: TObject);
    begin
      GetGLSLProg.UseProgramObject;
    end;
    
    function TGLCustomGLSLPostThermalVisionShader.DoUnApply(
      var rci: TRenderContextInfo): Boolean;
    begin
     // rci.GLStates.ActiveTexture := 0;
      GetGLSLProg.EndUseProgramObject;
      Result := False;
    end;
    
    procedure TGLCustomGLSLPostThermalVisionShader.DoUseTempTexture(
      const TempTexture: TGLTextureHandle; TextureTarget: TGLTextureTarget);
    begin
      Param['ScreenTex'].AsCustomTexture[0, TextureTarget] := TempTexture.Handle;
    end;
    
    function TGLCustomGLSLPostThermalVisionShader.GetTextureTarget: TGLTextureTarget;
    begin
      Result := ttTexture2D;
    end;
    

    In the the PostShader Sample i 've modified GLPostShaderHolder correctly but How can set correctly the ScreenTex parameters ?
    Thanks

    PS I tested the GLSL code above with https://www.opengl.org/sdk/tools/ShaderDesigner/
    and it work very well

     

    Last edit: Jerome.D (BeanzMaster) 2015-12-01
  • Jerome.D (BeanzMaster)

    Hi to all, after a good night the problem is solved

    To test it open the GLSLShader PostShader Sample and add this new shader like the blurshader to see the effect

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.