Menu

Pass Texture to GLSL Vertex Shader

Help
Ronald
2020-10-11
2020-10-16
  • Ronald

    Ronald - 2020-10-11

    Hello,

    In Texture3D sample, a color lookup table is created that can be applied to Texture voxels.
    I want to implement this in a GLSL vertex shader using a Texture1D color lookup texture.

    In TGlDirectOpenGL.OnRender, I added

    GLSLShader.Apply(rci, nil);
    

    Problem is: how to pass textures to the shader? I tried to use Param[].AsUniformBuffer:

    procedure TForm1.GLSLShader1Apply(Shader: TGLCustomGLSLShader);
    begin
      with Shader do
      begin
        Param['aimage'].AsUniformBuffer := FTexture3d.Handle;
        Param['acolor'].AsUniformBuffer := FTexture1d.Handle;
      end;
    end;
    

    but this gives me error messages "Unknown uniform".

    Shader code (test only):

    #version 330  //OpenGl 3.3 or higher
    
    uniform sampler3D aimage;
    uniform sampler1D acolor;
    
    void main()
    {
    }
    

    Suggestions/ideas are highly appreciated!

    Regards, Ronald

     
  • Daniel

    Daniel - 2020-10-11

    Hey!

    Try this:
    Param['aimage'].AsTexture[0] := FTexture3d.Handle;
    or
    Param['aimage'].AsTexture3D[0] := FTexture3d.Handle;

     
  • Ronald

    Ronald - 2020-10-11

    Hi Daniel,
    Thanks! Tried your suggestion, unfortunately, same error message.
    Somehow, I have to make the texture known as "uniform", but I have no clue how.
    Could not find any sample using a 3D texture with a shader.
    Regards, Ronald

     
  • Daniel

    Daniel - 2020-10-11

    Have you managed to make a shader work with a simple texture (sampler2D)?

     
  • Ronald

    Ronald - 2020-10-11

    Unfortunately, no.

    I did manage to do 3D volume rendering of a Texture3D (using TGLDirectOpenGL.OnRender), but I did not manage to get any shader to work with it.

    In fact, when trying to use a GLSL fragment shader setting fixed color (using gl_FragColor), my entire 3D texture was replaced by a small colored dot.

     
  • Ronald

    Ronald - 2020-10-13

    Is this possible at all using GLScene?

     
  • Daniel

    Daniel - 2020-10-13

    What object do you attach the shader to?
    Usually it's added to a material/texture.

     
  • Ronald

    Ronald - 2020-10-14

    The shader is not attached to a GLScene object. I based my project on GLScene's Texture3D demo, which uses GLDirectOpenGL.OnRender event for rendering, in which I added GLSLShader.Apply just before gl.Begin and GLSLShader.UnApply just after gl.End.

    3D objects are rendered OK without the Shader/Apply/UnApply, but I want to be able to remap grayscale values in Texture to rgb colors dynamically (In Dicom terminology change Window Center/Width).

    I try to implement a Transfer Function as described here using GLScene.

      GLSLShader1.Apply(rci, nil);
      gl.begin_ (GL_QUADS);
    
      for i := 0 to n - 1 do
      begin
        gl.Vertex3f( vx.X + vy.X + vz.X * z,  vx.Y + vy.Y + vz.Y * z,  vx.Z + vy.Z + vz.Z * z);
        gl.Vertex3f(-vx.X + vy.X + vz.X * z, -vx.Y + vy.Y + vz.Y * z, -vx.Z + vy.Z + vz.Z * z);
        gl.Vertex3f(-vx.X - vy.X + vz.X * z, -vx.Y - vy.Y + vz.Y * z, -vx.Z - vy.Z + vz.Z * z);
        gl.Vertex3f( vx.X - vy.X + vz.X * z,  vx.Y - vy.Y + vz.Y * z,  vx.Z - vy.Z + vz.Z * z);
        z := z + d;
      end;
    
      gl.End_;
      GLSLShader1.UnApply(rci);
    
     

    Last edit: Ronald 2020-10-14
  • Jerome.D (BeanzMaster)

    Hi Ronald can you put the full code of OpenGLDirectRender function and how you generate your 1D Texture, and also the full code of your GLSL script.

    The best way for using a specific shader and apply it to any model is to make a child of TGLCustomShader you can take a look at the sample code of GLFurShader, GLIvoryShader, GLErodeShader ect... unit to see how to do. Is not so complicated

    Cheers

    Jérôme

     
  • Ronald

    Ronald - 2020-10-16

    Hi Jerome,

    Thanks for reaching out. Original issue ("Unknown uniform") turned out to caused by the fact that during testing, I did not use the uniforms in shader code (empty main()) and thus, shader compiler removed the uniform.

    However, the issue of "how to change texture colors via lookup table in shader" is still unsolved, as is the usage of TGLSLShader (or TGLCustomShader derived class) in combination with TDirectOpenGL.OnRender.

    For that, I will start a new Topic. As this issue can be investigated using existing Texture3D demo, I will create sample code based on Texture3D demo and go from there.

    Regards, Ronald

    PS Daniel, thanks for pointing me in the right direction regarding texture reference passing to shader!

     
  • Jerome.D (BeanzMaster)

    Thanks for reaching out. Original issue ("Unknown uniform") turned out to caused by the fact that during testing, I did not use the uniforms in shader code (empty main()) and thus, shader compiler removed the uniform.

    This behaviour is normal. Because passing an empty value have non sense. You must also take care of the compatibility of the shader's script, GLScene use OpenGL Legacy (ver < 3.0)

    However, the issue of "how to change texture colors via lookup table in shader" is still unsolved,
    Ok, so you try to pass an array of byte or TGLColor. So the best way i think is to pass thrue a 1D Texture see here Sampler (GLSL) for more information

    In the shader for passing 1D Texture you must do as is :

    param['MyTexture1DParamNameInScript'].AsTexture1D[0{Idx}] := MyTGLTexture;
    

    is the usage of TGLSLShader (or TGLCustomShader derived class) in combination with TDirectOpenGL.OnRender.

    When you make a descendant of TGLSLShader (or TGLCustomShader TDirectOpenGL is not needed. Just attach the shader to the material.

    For that, I will start a new Topic. As this issue can be investigated using existing Texture3D demo, I will create sample code based on Texture3D demo and go from there.

    This is a good idea, post your sample as zip here when it's ready,
    It will more easy for help you
    I'm using Lazarus but it's not a problem, I already have the texture3D sample works.
    The only thing i've need is your GLSL Script and how you initialize the array of color.

    Regards. Jérôme

     

    Last edit: Jerome.D (BeanzMaster) 2020-10-16

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.