Is it possible to change the alpha range on the GPU ?

Hello experts,

I hope you can help me.

I draw thousands of colored quads by using the Quadstrip and Quads functionality.
The vertices and colors are stored in vbo buffers and it works well and fast.

I use blending to just create a let me say a brightness effect

glBlendFunc(GL.GL_SRC_ALPHA,GL.GL_ZERO) this works also well.

Now I want to adapt the alpha range to the lowest and highest value of my received alpha value
to spread the alpha range exactly to my maximum ranges.

normally :
alpha 0
my lowest value 0,234
my highest value 0,822
alpha 1

I need:
alpha 0 mapped to 0,234
alpha 1 mapped to 0,822

Has anybody an idea how I can solve this so that the change affect directly the drawing of my stored quads in the vbo´s .
Is there a OGL functionality available or is a shader a solution (I never wrote one )
Thanks for your answers

A 3 line shader?

Sorry I don´t understand your question .
I don´t know very much about shaders, can a shader be used to solve my problem ?

a shader can manipulate your output color.
And so, you can of course only change the alpha channel if you wish.

you could do just something like this


//vertex shader(just your standart one):
in vec4 in_position;
in vec2 in_texcoords;

out vec2 texcoords

uniform mat4 MVP_Matrix;

void main()
{
   texcoords = in_texcoords;
   glPosition = MVP_Matrix * in_position;
}

//fragment shader:
in vec2 texcoords;
uniform sampler2D colormap;

uniform float low, high;

void main(){
  vec4 color = texture(colormap, texcoords);
  color.a = color.a * (high-low) + low
  gl_FragColor = color;
}

Thank you for the answer …
A good information that a fragment shader can solve the problem.
I have to read a little bit more about shader programming, at the moment I did not unstand your code
because of less knowledge.
I use jogl to acces the OGL interface and I will look for examples

Is it possible to set new values from my program on the cpu during the shaders is working ?

If you stick to earlier OpenGL versions you don’t even need a vertex shader. You only need a very small fragment shader:


uniform sampler2D sampler;
uniform float low, high;

void main(){
    gl_FragColor = texture2D(sampler, gl_TexCoord[0]);
    gl_FragColor.a = gl_FragColor.a * (high-low) + low;
}

You can also change the high and low uniforms into constants if you don’t need to be able to set them at runtime, which would make the Java code to setup the shader a little bit simpler. Well, Danny02 put it pretty well, but that’s a newer GLSL version which is a bit harder for beginners.

thanks
so I will start reading shader information :slight_smile: