Drawing round points using modern OpenGL
26,429
Solution 1
Solution 2
One way would be to draw point sprites with a circle-texture and a self-made alpha test in the fragment shader:
uniform sampler2D circle;
void main()
{
if(texture(circle, gl_PointCoord).r < 0.5)
discard;
...
}
But in fact you don't even need a texture for this, since a circle is a pretty well-defined mathematical concept. So just check the gl_PointCoord
only, which says in which part of the [0,1] square representing the whole point your current fragment is:
vec2 coord = gl_PointCoord - vec2(0.5); //from [0,1] to [-0.5,0.5]
if(length(coord) > 0.5) //outside of circle radius?
discard;
Related videos on Youtube
Comments
-
Michael IV almost 4 years
I know how to draw round points using fixed pipeline. However I need to do the same using modern OpenGL. Is it possible, or should I use point sprites and textures?
For the interested.Here is how it is done with fixed pipeline:
glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_NOTEQUAL, 0); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable( GL_POINT_SMOOTH ); glPointSize( 8.0 ); glMatrixMode(GL_PROJECTION); glLoadMatrixf(myMatrix); glMatrixMode(GL_MODELVIEW); glLoadMatrixf(myAnotherMatrix); glBegin(GL_POINTS); glColor3f(1,1,1); glVertex3fv(position); glEnd(); glDisable(GL_POINT_SMOOTH); glBlendFunc(GL_NONE, GL_NONE); glDisable(GL_BLEND);
-
Michael IV almost 11 yearsIn fact I have asked if it's possible without doing vodoo in the shaders...But +1 for the links :)
-
Christian Rau almost 11 yearsIn modern OpenGL nothing is possible without doing anything in the shaders, though. I'd rather call the weird behaviour of
GL_POINT_SMOOTH
voodoo. -
Michael IV almost 11 yearsWhy do I need self made alpha test?If the texture comes with alpha then hardware alpha blending should suffice. Shouldn't it?
-
Christian Rau almost 11 years@MichaelIV But alpha testing is deprecated and not part of the core profile anymore. Maybe alpha blending suffices for you, but usually alpha testing would be preferred if possible, since it doesn't pollute the depth buffer with "invisible" fragments (and might also be slightly faster, since
discard
is an early out). And like said, the method without a texture would be better anyway. -
Christian Rau almost 11 years@MichaelIV Imagine you want to draw many many opaque but round particles (yay, confetti time!). When using alpha testing instead of just alpha blending, then there is absolutely no need to depth-sort your particles. If the particles are transparent anyway, though, you would still need to sort them and use alpha blending, so there might really not be a functionality improvement from the
discard
. -
SigTerm almost 11 years@ChristianRau: Alpha-testing alone (without alpha-blending) will result in jagged edges.
-
Christian Rau almost 11 years@SigTerm Right indeed, but there are solutions for this, too. Anything drawn to a discrete pixel grid will result in jagged edges. The solution for this is using multisampling, and together with alpha-to-coverage that will take care of this problem, too (you still need actual alpha, but no need for alpha blending and depth-sorting).
-
Steven Lu almost 11 years"voodoo in the shaders" -- not how you should think about it, and not just with respect to drawing circles, either. Better get with the program, cuz this is how it gets done from here on out. Fixed function is legacy
-
Hassen Dhia over 7 yearswhile this answer can be useful , you should append more explanation in it because the extern links can be expired later and the answer become useless