Sign in to follow this  
Geeman

Render a Sphere

Recommended Posts

Hi, Im trying to do something fairly 'simple' Using raytracing, I want to render a simple sphere. But the thing is, I don't have an polygonal data for it, but instead I want to render it based of some attributes (so it will be a perfect sphere). For example, I know it has a location and radius. So I cast my rays towards the location. I determine how far away the ray is from the spheres location Give that ray point a clamped gradient value (based on the radius). The result is a simple circle gradient: What I want now is to render it with some lighting (and eventually with reflection and specular highlights), but to do this I need the normal of a point on the spheres 'surface' How would I calculate this? If I sent a ray directly to the center of the sphere, I know the normal at that point would be directly reflected towards the camera, but I cant work out how to do it for any part of the 'sphere'

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
the normal of a sphere on a given position is simply the vector from the center of the sphere to this given location. But dont forget to normalize it ;)

Share this post


Link to post
Share on other sites
If your sphere has for center O, and your ray intersects the sphere at point I, then the surface normal at I is colinear with the radius vector OI.

Share this post


Link to post
Share on other sites
First thing's first. You need to find out how to determine (in 3D) if and where a ray intersects with a sphere of a certain radius.

The following publication presents a function called intersectSphere in the QJuliaFragment.cg file. It's in Cg, but should be more than understandable to a C++ programmer:
http://graphics.cs.uiuc.edu/svn/kcrane/web/project_qjulia.html

Using a bounding sphere as such, the intersection test generally runs in constant time, O(n). This speeds up the render quite nicely.

As suggested above, once you find the ray-sphere intersection point on the sphere surface, you determine its surface normal by pointing a unit vector from the sphere centre towards it.

That should lead you right up to the lighting stage.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this