Most Optimized way to draw a circle DirectX

Started by
14 comments, last by apatriarca 11 years, 11 months ago
If you can use shaders you could draw fullscreen quad and specify circle position/radius. With little maths inside shader you could have different colors, position, line width, etc.
Advertisement
Why are you using a fullscreen quad (using a constant buffer for the circle positions and orientations?) instead of drawing a quad for each circle (using a geometry shader or point sprites to create it from a single point for example)? What method would you use to render the circles with different line widths? The only one I have in mind is to ray-trace a torus and I wouldn't consider it simple math*.

* Well, I would try to solve a [s]quadratic[/s] quartic equation, is there any better method?
Why are you using a fullscreen quad (using a constant buffer for the circle positions and orientations?) instead of drawing a quad for each circle (using a geometry shader or point sprites to create it from a single point for example)?[/quote]
I believe the OP is using D3D9, making geometry shaders a no-go. That said, I was merely referring to the possibility of drawing the circle using a texture overlay instead of actual vertices, the cost could then be negated by the circle already being textured anyway later on. Vertices aren't always the be all end all of rendering geometry.

The only one I have in mind is to ray-trace a torus and I wouldn't consider it simple math*. * Well, I would try to solve a quadratic equation, is there any better method?[/quote]
Off-topic, but ray-tracing a torus is in fact solving a quartic equation (which gets really messy very quickly). You most definitely don't want to use the analytic solution, but there are numerical solutions which are satisfying (if convoluted). A quadratic equation is for a sphere.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


Off-topic, but ray-tracing a torus is in fact solving a quartic equation (which gets really messy very quickly). You most definitely don't want to use the analytic solution, but there are numerical solutions which are satisfying (if convoluted). A quadratic equation is for a sphere.

Yes, sure. It was a typo. A quadratic equation is actually very simple math.. smile.png For a quartic equation you surely use numerical methods (Ferrari's method is not really well suited for GPUs). I was just curious whether there is a better solution to render a circle with different line widths in a shader...


I believe the OP is using D3D9, making geometry shaders a no-go. That said, I was merely referring to the possibility of drawing the circle using a texture overlay instead of actual vertices, the cost could then be negated by the circle already being textured anyway later on. Vertices aren't always the be all end all of rendering geometry.

I was actually replying to Ripiz. The only problem I see in your method is that it may give "wrong" results at some angles, i.e. when the circle axis is perpendicular to the eye vector. But I would probably use your solution anyway.

What method would you use to render the circles with different line widths? The only one I have in mind is to ray-trace a torus and I wouldn't consider it simple math


Lets say vertexes in fullscreen quad contain 2D positions as texture coordinate (4 vertices = 4 different values). In the pixel shader they'll get interpolated giving 2D position at any given pixel. Then in pixel shader you can calculate whatever is needed.


float4 CirclePS(coord : TEXCOORD0) : COLOR0 {
float2 circleCenter = g_Center; // comes from constant buffer
float circleRadius = g_Radius; // comes from constant buffer
float lineWidth = g_Width; // comes from constant buffer
float2 currentPos = coord; // comes from interpolator

float distance = length(currentPos - currentPos); // distance from center to current position in space (2D plane)

if(distance >= circleRadius) // we're not too close
if(distance <= circleRadius + lineWidth) // and not too far
return float4(1, 0, 0, 1); // it's a line!

return float4(0, 0, 0, 0);
}
ohmy.png We are working in 2D!! It looks like I completely misunderstood the problem.. I was thinking about the rendering of circles in 3D and it was the reason I considered ray-tracing a torus. If we are in 2D, it is surely possible to draw circles with any line width in a shader and there is no problem with the texture solution.

This topic is closed to new replies.

Advertisement