Circle with decreasing alpha

Started by bene_2808, September 12, 2015, 11:12:14

Previous topic - Next topic

bene_2808

Hello,

I want to save a filled circle to the GPU, so I can draw it several times later. But I want to draw the circle in different colors and the color of the middle should always appear as full alpha, whereas the color of the outer borders should appear as zero alpha. Saving the circle as pixel data and then specifiying the desired color before drawing the texture would be one possibility. But is it possible to use a VBO for that purpose instead? I know, color data can be used, too, via VBO. But what if I want to use different colors for each VBO render call, BUT replace the alpha value depending on the accordingly vertex?  ???

Kai

This is a perfect excercise for a shader.
In Java, simply define a quad using 4 vertices with vertex coordinates in [-1..+1] for both X and Y.
In the shader pass-through the unmodified vertex coordinates as varyings between vertex and fragment shader.
Use a uniform to specify your desired color.

Use the following fragment shader:
varying vec2 positionOnQuad;
uniform vec3 opaqueColor;

void main(void) {
  // compute distance between quad center and current fragment
  float len = length(positionOnQuad);
  // linearly attenuate alpha based on distance
  float alpha = max(0.0, 1.0 - len);
  // use opaqueColor with computed alpha value
  gl_FragColor = vec4(opaqueColor, alpha);
}

bene_2808

Thanks for the fast reply :D ;D

I actually tried not to use shaders because I want to run the application on very slow computers... ::) But I think, using a texture or something like this wouldn't be that less demanding, right?  :-\

You're sure, there's no other possibility?

Kai

Quote from: bene_2808 on September 12, 2015, 11:51:22
I actually tried not to use shaders because I want to run the application on very slow computers... ::) But I think, using a texture or something like this wouldn't be that less demanding, right?  :-\
Even very very veeeery slow GPUs can handle that kind of shader. :)
The only restriction is that the GPU/driver needs to implement OpenGL 2.0; or any previous OpenGL version and ARB_shader_objects.
Even the oldest and slowest of Intel's integrated GPUs can do that. :)
A texture would (probably) have a bigger performance impact due to memory access.
However, the biggest issue with textures I see is that they do not scale well with different sizes/resolutions.
A shader solution however always produces pixel-perfect results no matter how you scale/distort your circle.

bene_2808

OK so I'll use the shader  ;D

Arrrgh... Really looking forward to importing the shader :-X Most exciting thing I can imagine...

Thank you :D