Advertisement

calculate pixelsize on screen from boundingsphere

Started by August 23, 2009 07:57 AM
4 comments, last by GameDev.net 15 years, 3 months ago
Hey there! Given -a sphere with centerposition and radius -an (openGL) projection matrix -windowdimensions how do i efficiently compute the radius in pixels the sphere will have on screen? my multiplying the centerposition with projectionmatrix and windowmatrix i get the final pixel position of the center. but i'm only interested in transforming the radius how to do that? (without introducing a second point on the sphere hull, transforming both and then calculating the distance) thanks!
I don't think you can without a second point, since the radius itself it's a distance and a distance needs 2 points, but why would that be inefficent? Besides you can optimize things since the second point only have to be shifted a bit on a single axis. So if you have center(x, y, z), the other point to build the radius would be center + (radius, 0, 0). So you transform x, y, z and x + radius through the matrix, that's only 4 coordinates, skipping the other 2 since they will end up like the transformed center.
Advertisement
A sphere is not necessarily a perfect circle on the screen, as can be seen in the following image (right sphere is stretched):


The most straight forward way might be to calculate that point on the sphere, in the direction furthest from the screen center, to get the circle radius that must include every part of the sphere.

EDIT: I don't know what you use it for, but the easiest way is to calculate the distance from the camera to the sphere center, and divide the radius by that distance, and then multiple it by the screen-size. This is often done for 3D particles etc.
Erik: something like that i'm looking for, but simply radius/distance*screensize can't be correct, since the size on screen is also dependant on camera angle (smaller angle will result in larger size on screen, even with same distance between camera and object). I don't want to use trigonometric functions, that's why i want to do it only using the projectionmatrix.

I use it for LOD level calculation - so it doesn't matter that the projected sphere is not really round - i need only a good estimation of how big the object will be on screen.
Looking at gluPerspective, it should be radius * cot(fov / 2) / Z reasonably close to the center of the screen, which must then be scaled by the screen-size (or screen-size/2 probably..). Then you also have the aspect-ratio, if your screen isn't a square, so you should probably multiply by that too to make sure your calculated radius is big enough.
Here is some code I used, not as simple as I said indeed, but is the fastest I could think of:
v0 = (x, y, z) // center, world coordinatesv1 = v0 + (radius, 0, 0)v0 = v0 * matViewv1 = v1 * matViewright = viewport.width  * 0.5top   = viewport.height * 0.5v0 = v0 * matProjv0.x *= rightv0.y *= topv1 = v1 * matProjv1.x *= rightv1.y *= topscreenRadius = abs(v1.x - v0.x)


I used it to find out 2D points from 3D for interface/mouse input stuff, but I don't think this is the right way to compute LOD distances. For LOD calculations you need to know how far the object is from the camera, not how big it would look like. Based on that distance (which may be squared, so you skip a consuming sqrt) you look up in a table and choose the right LOD.
     0..1000^2 - LOD 0 (biggest)1000^2..3000^2 - LOD 1etc


[Edited by - LeChuckIsBack on August 23, 2009 1:50:38 PM]

This topic is closed to new replies.

Advertisement