8

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?

user1118321
  • 25,567
  • 4
  • 55
  • 86
Ben Jones
  • 919
  • 1
  • 8
  • 22
  • It would be helpful if you would quote the graphics paper you're implementing from, I think. – Xavier Ho Apr 28 '10 at 06:34
  • Whoops, forgot to mentions that they use quads. http://www.irisa.fr/prive/kadi/SiteEquipeAsociee/Site_RTR2A/Papiers/rcplat_derniere.pdf is the paper. The section I'm referring to is 5.1: "the splatting on the GPU is performed by drawing a quadrilateral tightly bounding the splatted sphere on the image plane. The position and size of the quadrilateral are computed using a vertex shader. Then each fragment is processed so that its value represents the record's weighted contribution" – Ben Jones Apr 28 '10 at 06:43

3 Answers3

2

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

Xavier Ho
  • 17,011
  • 9
  • 48
  • 52
  • 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 Apr 28 '10 at 16:40
1

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.

Frederik Aalund
  • 1,324
  • 2
  • 14
  • 17
0

In general, a sphere is seen as an ellipse in perspective:

alt text
(source: jrank.org)

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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
brainjam
  • 18,863
  • 8
  • 57
  • 82
  • @Glorfindel: The script seems to have stripped off the rest of that psychology.jrank.org link. – genpfault Jul 24 '19 at 19:37
  • 1
    Right, thanks for catching that. I've tried to catch those instances before, but it turned out much harder; the script couldn't even cope with Stack Overflow users changing their name. Maybe if a specific URL redirects to a bare domain (or something like /index.html) it should prefer the Wayback Machine link. – Glorfindel Jul 24 '19 at 19:44