That’s not a moon…

texturedraytrace Thats not a moon... raytracer Programming

…it’s a partially reflective UV texture mapped sphere with the image of the moon textured on it. And that one behind it is the place the Battlestar Galactica is going next season. And there’s a big fucking crate, because what wouldn’t a “Hello World” raytraced image be without a big fucking crate. icon smile Thats not a moon... raytracer Programming Before that I had a nice ranchy image of Ino from Guilty Gear XX, but I figured, who would want to see that when you could be looking at a crate?

So what does this image prove?

a. I’m a big nerd and have too much free time — and I do, cause I’m on vacation (or “vacation?”) which means I get to do whatever the hell I want, and yes, I choose to work on my raytracer, because I enjoy it. Put that in your pipe and smoke it;

b. I got rudimentary UV texture maps working for spheres and triangle meshes;

c. I can load arbitrary triangle meshes, so obviously I would load a cube because there’s nothing more interesting than a cube;

d. The moon is actually partially reflective and would in reality reflect a red and blue infinite checkerboard plane that stretches far and wide across the universe;

e. All of the above.

My raytracer’s actually come to the point where it takes a couple of minutes to raytrace a triangle mesh. Such as this image:

donut Thats not a moon... raytracer Programming

So what’s next on the agenda? Well…I fixed some of the horkiness of the raytracer, but it’s still horked in other ways. There are still issues with the reflection, refraction doesn’t work and so on and so forth…a lot of little quibbles with the mathematics that I need to iron out. It’ll get there. If you’re curious how long it took to add texture mapping and mesh loading, on my part not too long… a couple of hours each. For mesh loading, I had an OBJ loader cooked up for our old game engine Bushido. The tricky part was to understand how to find ray-triangle intersections — and to find a good one at that. If you’re looking for a good resource for barycentric ray-triangle intersection testing then google search for “Fast, Minimum Storage Ray/Triangle Intersection” by Tomas Moeller and Ben Trumbore. They also wrote a book called Real-Time Rendering which I need to get my hands on. As one senior engineer where I work told me, it’s the new bible for computer graphics.

For texture mapping. I had also done a TGA loader for Bushido, so I imported that and made it much more portable. There are also great UV coordinate finding algorithms online for spheres. The 3D mesh UV coordinate algorithm is just based off of the barycentric intersection test. Nice and easy and it gives me much more flexibility in what I can load into my raytracer.

For even further down the road I’m looking at implementing a kd-tree to speed up my raytracing especially with all these triangle meshes. I also need to consider anti-aliasing. I should also think about a transformation pipeline — that’s right, I got away without one for as long as I could! Multiple lights might be fun. Soft shadows. I’d really like to get into the procedural texturing.

Above all…having a GUI interface to set up a raytracer scene would really help. One of my goals is to keep my raytracer code portable, so it’s not dependent on OGL or DirectX. That way I can implement a GUI in the future so I can get a wireframe view of everything before I hit the big ole render button.

More to come…but dinner beckons, and then Rapture.

Horked but Working!

Here’s an image from the current state of my raytracer. The big win from all of this is that my raytracer is now recursive so I can cast reflection and refraction rays from my incident ray. It’s still buggy as hell and not showing correct results.

raytrace3 Horked but Working! raytracer Programming

I believe the issue I’m facing here is a z-depth one. I’m noticing that a lot of my calculations are the inverse of what I expect. I don’t believe I’m supposed to see the shadow like that, and definitely I should not be seeing the reflection of those two smaller spheres. Now that I have a fairly decent raytracer working it’s time to go back and correct all the little details.

Raytracing II

After another day of work here’s the result:

raytrace2 Raytracing II raytracer Programming

With the help of Foley, et al. I fixed my illumination code. The specular highlight you see here works more correctly then in the previous image. I’m not so hot about the washed out look of the sphere, but that’s mainly due to the white ambient light I’m using. I also have shadows. I’m not 100% sure if the shadows are correct, but it’s a start.

Three spheres, one checkerboard infinite plane…I have a very simple scene list now and I can add spheres and planes at will. Maybe oneday even arbitrary 3D models. How could would that be?

I’m excited.

There’s so much I can do now. The next major refactoring/addition will be to create a recursive raytracing algorithm which will provide me with refraction and reflection. Chrome spheres here I comes!