In a particle simulation, I am rendering particles as "spheres" using GL_POINTS instead of a mesh for efficiency. With glEnable(GL_PROGRAM_POINT_SIZE)
I can use gl_PointSize
from the vertex shader to set the size of my resulting 2D point in screen space pixels.
I have the projection matrix, view matrix and the particle position in my vertex shader.
My current approach has been
vec4 pos = proj * view * vec4(position, 1.0);
gl_PointSize = POINT_RADIUS / pos.w;
, which works, but POINT_RADIUS
is not in world space, but rather the pixel size on my near plane (of the perspective projection).
How would I calculate the point size in screen space from a given radius in world space? I can only think of kinda hacky solutions. Is there some mathematically clean or obvious approach to this?