views:

1740

answers:

7

What are some rasterization algorithms that can just project a 3d sphere into a pixel grid? I want to avoid ray casting. Essentially, given a 3d coordinate and a radius, is there a quick way to just create a 2d circle/ellipse on a pixel grid?

For example: circle at (2,2,2) with radius 4 gets projected to five pixels: p1(2,0)p2(0,1) p3(1,1) p4(2,1)p5(1,2)

I have come across techniques such as pixel splatting for particle systems but I haven't found a clear answer on how to do this.

Thanks

+1  A: 

What projection do you have?

I'm quite sure the projection isn't a circle in the most interesting ones. Hm. I guess the way I would do it would be to find the circumference circle in 3d whose axis* is aligned to the camera point. Pick whatever points needed from the circle and then transform them to screen space. Most simply, that would give you a polygon, but interpolated splines should probably also give you nice results.

*: axis of rotational symmetry

Anders Eurenius
A: 

I don't understand your problem 100%. Is your pixel grid = pixel in screen space or there's a fix ratio?

From my best guess, you can try these:

Map the internal view port coordinate with the screen space pixel and calculate a ratio. Use that ratio to place your object exactly where you wanted. I've written an D3D apps, windows based (not fullscreen) using this method to allow mouse interaction with the 3D objects. This allow moving and resizing the obejct using mouse which makes it feels like ordinary windows apps. I had work out the ratio by trial and error (I wasn't good with matrix calculation).

If you didn't disturb the view projection matrix like I did above, the 3D coordinate is -1.0f to 1.0f left to right and bottom to top. Thus you can easily work out the ratio and place your object appropriately using the same method above

faulty
A: 

Perhaps the simplest way (if you don't mind some linear algebra) is to use a 4x4 projection transformation matrix. In fact, just about any kind of 3D transformation can be performed using a 4x4 matrix.

Here is a Wikipedia article to get you started.

Brian
+2  A: 

The 2D perspective projection of a sphere is neither a circle nor a perfect ellipse, particularly if the sphere is relatively close to the camera.

(it will be a circle if the sphere happens to be sat right in the middle of the projection axis, but that's often not the case).

Alnitak
true but what is it?
Joe Soul-bringer
I don't know exactly - if the sphere is off-center it'll appear very slightly egg-shaped.
Alnitak
This is wrong. The space of the projection of a sphere is a cone; the plane of projection which intersects that cone is an ellipse. The perspective projection of a sphere is always an ellipse.
fuzzyTew
A: 

It sounds like you're willing to put up with a little unfaithfulness to a true projection to get simpler calculations. If so, follow along...

I will start by assuming you can shift, rotate and resize the sphere to match the coordinate system of the projected circle. Let's call r the radius for both of them, and assume the center is at (0,0,0) for the sphere and (0,0) for the circle. The X axis runs left-right for both, the Y axis runs bottom-top for both, and the Z axis runs back to front for the sphere.

Here's the fun part: you're done. The X and Y coordinates are identical for both the sphere and the circle! If the Z coordinate is positive, the point is visible, if it's negative the point is hidden on the back side of the sphere.

Mark Ransom
A: 

cool, so assuming you just take the x,y out of the x,y,z of the sphere to form your circle. How do you go about shading the appropriate pixels on a pixel grid?. Basically, what's the algorithm for rasterizing a circle?

A: 

The projection of a sphere onto a plane is an ellipse, but it can be approximated with a circle. This can be done almost the same as the perspective projection of a point.

Assume the center of the screen is (X=0,Y=0), and the viewer is located at (X=0,Y=0,Z=0). If the sphere is at (sX,sY,sZ) with a radius of sR, and the screen is D units away from the viewer (focal distance), then the circle will be at (cX = sX * D / sZ, cY = sY * D / sZ) with a radius of cR = sR * D / sZ. Everything is simply scaled by D / sZ.

This circle is only a good approximation of the sphere projection if the focal distance (D) is large, or the sX and sY coordinates of the sphere are near to zero.

fuzzyTew