Mark Sachs (ksleet) wrote,
Mark Sachs

  • Mood:
  • Music:

This image makes me nostalgic for some reason.

This unassuming little checkered sphere: just what it says, a raytraced planet. Well, "planet" may be rather grandiose, but it is what it says.

The basic scheme is pretty simple. We have a little window, 200 by 200 pixels across, that is supposed to look out into a 3D world with the eye point at a particular location and facing a particular direction. Given that information, we loop through every pixel in the window and for each one create a ray going from the eye point out into space. We use some math to determine if the ray intersects the sphere. This lets us know that we should draw a pixel there.

If we do want to draw a pixel, we convert the point in 3D space where the intersection happens into spherical coordinates relative to the sphere's center, which gives us the latitude and longitude on the sphere. With that information, we can decide just what is on that location of the sphere -- in the case of our little test case here it's just a simple red or white color, but we could look up the information with anything: a texture map, a procedurally generated surface, you name it. We could also light the pixel as we wish, perhaps even casting rays out from the light source to the point in question to see if anything obstructs it (which would make this more true ray-tracing.)

The plus side of this method is that it, theoretically, lets us draw perfectly round spheres, at least to the limit of the math precision available, no matter how close the sphere is to the eye point. Most games and graphics programs which want to draw a textured sphere simply draw a many-sided polygon instead, and these break down on close inspection as the polygon edges become visible. The downside of course is that since it doesn't use our fancy graphics accelerator cards it is fearsomely slow, even on a modern computer -- and never mind my wheezy old PC. The 200x200 window runs tolerably, but 1000x1000 chugs along at a frame or so per second.

Still, I can't help but be rather chuffed, as our friends across the pond say, that I got this working, and pretty easily, too. It's something I've wanted to try out for a while and I'm glad I finally broke down and did it.
Tags: raytracer
  • Post a new comment


    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.