Advertisement

OpenGL 4 thick points -- circles using triangle_strip in geometry shader

Started by November 26, 2022 05:45 PM
10 comments, last by taby 2 years ago

I've implemented a point shader that draws thick points as squares, like back in the old OpenGL 1 days. I'd really like to make these into circles though. How would one do this in the OpenGL geometry shader, where one is forced to use a triangle_strip? I've found this:

https://gamedev.net/forums/topic/637760-d3d9-draw-filled-circle-avoiding-triangle-fan/5025229/​

… but I'm not sure it's quite right?

You would be better off drawing a single quad and doing the circle in the pixel shader.

Here is my circle shader that draws an antialiased circle, provided a quad with vertex positions and UVs, where UVs go from -1 to 1 on the quad vertices. Please forgive my ancient GLSL dialect.

uniform vec4 color;
uniform float pixelSize; // size of pixel in UV space
varying vec3 interpolatedPosition;
varying vec2 interpolatedUV;

float border( in float halfPixelSize, in float invPixelSize, in vec2 uv )
{
	float radius = 1.0;
	float d = length(uv);
	float dBorder = radius + halfPixelSize - d;
	return clamp( dBorder, 0.0, pixelSize )*invPixelSize;
}

void main(void)
{
	float halfPixelSize = pixelSize / 2.0;
	float invPixelSize = 1.0 / pixelSize;
	
	float alpha = border( halfPixelSize, invPixelSize, interpolatedUV );
	
	gl_FragColor = vec4( color.rgb, color.a*alpha );
}
Advertisement

Thank you for your input. I will take it all into consideration!

taby said:
I'd really like to make these into circles though.

If you have MSAA on, and draw a point using GL_POINT, it draws a nice circle instead a square.
I think i saw this happening on both NV and AMD, but NV for sure.
Ofc. that's not specified, but that's what i saw happening.

Otherwise agree about PS better than VS, but you may need a discard instead alpha blending depending on your stuff.

The fastest, and eventually best looking option might be to modify the quads from the lines, making the ends angled so they close the gaps. So not points are needed.
With voxels you only have 90 degrees angles, so maybe that's somehow possible without much efford, but not sure.

Just thought about an algorithm to generate per object contours geometrically:

foreach (edge)
{
	vec3 n0 = worldToViewSpace * edge.poly0->normal;
	vec3 n1 = worldToViewSpace * edge.poly1->normal;
	if (n0.z * n1.z < 0) DrawEdge(); // if one normal is facing towards the eye but the other is facing away, we are on a contour.
}

I think this should work pretty well.
But each edge needs to know adjacent face normals.

Please, tell me more about MSAA with framebuffers?

Advertisement

For the time being, I simply create a giant offscreen fbo, render to that, then downsize the image in Paint.net. Voila: we have antialiased everything LOL. Po' man's AA. ?

taby said:
Please, tell me more about MSAA with framebuffers?

Idk how it affects image processing. But i guess if you use forward rendering not much would change for you. OpenGL should abstract most details away.
I have rarely used it myself. But i remember it was very easy to enable, so you can just try it out.

taby said:
For the time being, I simply create a giant offscreen fbo, render to that, then downsize the image in Paint.net. Voila: we have antialiased everything LOL. Po' man's AA.

Hehe, i'd rather say it's rich mans AA. True Multi sampling surely is an option to give epic gfx settings to RTX 4090 owners ; )

All I know is that it runs at like 15 FPS on my laptop lol :(:(

taby said:
15 FPS

I never liked HW AA since the days 3Dfx started to have 4 chips just to make 2x2 MSAA work. (Or maybe they did full super sampling - idk.)
Really feels a useless feature, adding too much complexity which then is useful only in special cases. Today i add HW RT and AI acceleration to the same category of useless features, for now at least.
No wonder NV was interested to by 3Dfx. They surely were excited about the idea to make GPUs 4 x more expensive.

If you want proper AA, TAA is the only option, sadly. IT gives you much higher quality than MSAA at almost negligible cost.
And it can solve many other things for free, e.g. the patterns i see on your sampling to get the bloom. Just jitter your samples, and TAA will turn all your banding and noise into smooth signals.
It can be a godsend, but it also is a rabbit hole on it's own. The biggest problem is you need velocity vectors for everything.

But after that, this awesome tutorial covers all the rest. Because it has all the information in one spot, i no longer think TAA is too complicated or time consuming to implement.

There were some analytical edge based alternatives, e.g. by rasterizing distance to closest edge and then add AA as an image processing post process.
But with increasing geometry resolution this all breaks down, and it does not address shader aliasing at all anyway.
Personally i'm interested in point splatting (like the Dreams game does), and this can do such analytical AA at negligible cost. Here a quick low res render of a terrain patch with spherical projection:

Ignoring the aliasing against the background and the noise if particles are too far apart, it generates a pretty smooth image also under motion.
So maybe it's an argument for splatting over triangles, that the curse TAA is no longer needed, if we go there. (Now that ‘sub pixel detail’ is a big hype, it makes indeed sense to abandon triangles.)
But i rather think the honest answer is that TAA would still improve the image a lot, and i would still add it.

Many people hate TAA for good reasons. It has ghosting and smearing, does not work for VR, requires tweaking and compromise affecting production, etc.
But i rather conclude for realtime we're doomed to stick at TAA till the end of days.

So if you consider to add it, you better do it early than late, because it can help with so many things once you have it.

This topic is closed to new replies.

Advertisement