Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute bounding quad of a sphere with vertex shader

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?

like image 211
Ben Jones Avatar asked Apr 28 '10 06:04

Ben Jones


2 Answers

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 http://xavierho.com/temp/Sphere-Screen-Space.png

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. =]

like image 152
Xavier Ho Avatar answered Nov 10 '22 14:11

Xavier Ho


The sphere will be projected as an ellipse unless it's at the cameras center as brainjam says.

The article that Xavier Ho links to describes the generalization of sphere projection (That is, quadratic projection). It is a very good read and I recommend it too. However, if you are only interested in sphere projection and more precisely the quadrilateral that bounds the projection then The Mechanics of Robust Stencil Shadows, page 6: Scissor Optimization details how to do it.

A Note on Xavier Ho's Approximation

I would like to add that the approximation that Xavier Ho suggests is, as he notes too, very approximative. I actually used it for a tile-based forward renderer to approximate light bounds in screen space. The following image shows how it neatly enables good performance with 400 omni (spherically bound) lights in a scene: Tile-based Rendering - Far View. However, just like Xavier Ho predicted the inaccuracy of the light bounds causes artifacts up close as seen here when zoomed in: Tile-based Rendering - Close view. The overlapping quadrilaterals fail to bound the lights completely and instead clip the edges revealing the tile grid.

like image 1
Frederik Aalund Avatar answered Nov 10 '22 12:11

Frederik Aalund