views:

173

answers:

2

I'm trying to implement an algorithm from a graphics paper and part of the algorithm is rendering spheres of known radius to a buffer. They say that they render the spheres by computing the location and size in a vertex shader and then doing appropriate shading in a fragment shader.

Any guesses as to how they actually did this? The position and radius are known in world coordinates and the projection is perspective. Does that mean that the sphere will be projected as a circle?

Thanks!

A: 

I found a paper that describes what you need - calculating the bounding quadric. See:

http://web4.cs.ucl.ac.uk/staff/t.weyrich/projects/quadrics/pbg06.pdf

Section 3.2, Bounding Box calculation. The paper also mentions doing it on the vertex shader, so it might be what you're after.

Some personal thought:

You can approximate the bounding box by approximating the size of the sphere by its radius, though. Transform that to screen space and you'll get a slightly larger than correct bounding box, but it won't be that far off. This fails when the camera is too close to the point, or when the sphere it too large, of course. But otherwise should be quite optimal to calculate, as it would be simply a ratio between two similar, right triangles.

If you can figure out the chord length, then the ratio will yield the precise answer, but that's a little beyond me at the moment.

alt text

Of course, that's just a rough approximation, and has a large error sometimes, but it would get things going quickly, easy.

Otherwise, see paper linked above and use the correct way. =]

Xavier Ho
I think I'll be using the approximation you have here, since in the fragment shader I can test EXACTLY whether the fragment should contribute and the vertex shader is really only trying to reduce the number of fragments I have to process.
Ben Jones
A: 

In general, a sphere is seen as an ellipse in perspective. alt text

The above image is at the bottom of this article.

Section 6 of this article describes how the bounding trapezoid of the sphere's projection is obtained. Before computers, artists and draftsmen has to figure this out by hand.

brainjam
Yes, you're correct. Took me some time to think abuot this.
Xavier Ho