Skip to main content
gamedev.net
🔒 Locked

Render a Sphere

Started by Geeman Apr 30, 2006 at 12:26 PM 2 replies 1.1k views
Original Post
Geeman
Geeman
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'
Fruny
Fruny
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
taby
taby
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.