Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Shader question in ES 2 iPad app

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2014
    Posts
    6

    Shader question in ES 2 iPad app

    I'm working on an iPad drawing app and I have a question regarding how to implement a specific fragment shader for a brush in the app. I'm not very familiar with GLSL because the app was previously using ES 1.1 which did not have shaders. I'm looking for guidance, I need this out pretty quickly.

    From a high level:

    How do I get a fragment shader to only build up to a certain alpha value at one location? The brush drawing on top of itself eventually changes to an alpha of 1.0 at the location where it is continuously drawn, but let's say I want the cap to be 0.75 for example. Is there a way to set a cap value for the gl_FragColor in a fragment shader? How should I go about doing this?

    This is what my fragment shader looks like so far:

    Code :
    uniform sampler2D brushTexture;
     
    varying lowp vec4 v_color;
    varying lowp vec2 v_texCoords;
     
    void main()
    {
        gl_FragColor = v_color * texture2D(brushTexture, v_texCoords);
    }

    And my vertex shader if that is helpful:

    Code :
    attribute vec4 position;
    attribute vec2 texCoord;
     
    uniform mat4 MVP;
     
    attribute vec4 a_color;
     
    varying vec4 v_color;
    varying vec2 v_texCoords;
     
    void main()
    {
        v_color = a_color;
        v_texCoords = texCoord;
    	gl_Position = MVP * position;
    }

    Thanks for any help.

  2. #2
    Advanced Member Frequent Contributor arekkusu's Avatar
    Join Date
    Nov 2003
    Posts
    783
    At a high level, you can't do this with simple rendering & blending in OpenGL. You could build a more complex system with "opacity groups" by rendering a collection of brush strokes into an FBO, allowing the alpha to saturate to 1.0, and then compositing the result with the alpha scaled back down to your desired max.


    However, if you are only concerned with iOS, and you can figure out how to express your "saturate to 0.75" per-pixel algorithm in C, then you can use EXT_shader_framebuffer_fetch to implement the same custom blending in the fragment shader. Be warned that it won't easily port to other platforms.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •