Friday, April 27, 2007

MRTS (More Ray Tracer Stuff)

I took some time out of working on the ray tracing algorithm to fix a few nagging bugs and make it easier to specify the scene that I am rendering. I fixed the bug that caused spheres to render distorted if they were off center. I also fixed the lighting code and fixed a bug that caused any sphere not at the origin to be incorrectly shaded. Probably the biggest change was to move the definition of the scene outside of the code and into a data file. Now I can change the scene very quickly without a recompile. That is very handy for testing, and for just having fun with it to see how the lighting model behaves if I move a sphere or there, or change the light sources, etc...

Here is a sample XML scene definition, and the image it created. For my co-worker (*ahem* Brandon) that takes issue with rendering the light sources... DEAL WITH IT! :D

 
<scene>
<light_sources>
<color name='blue_light' red='0x00' green='0x00' blue='0xff' />
<color name='red_light' red='0xff' green='0x00' blue='0x00' />
<color name='white_light' red='0xff' green='0xff' blue='0xff' />

<sphere x=' 5.0' y='-5.0' z='-2' radius='0.1' color='white_light' />
<sphere x='-5.0' y='-2.0' z=' 4' radius='0.1' color='white_light' />
</light_sources>

<objects>
<color name='clr' red='0xff' green='0xff' blue='0xff' />

<sphere x=' 0' y=' 0' z='0' radius='1.5' color='clr' />
<sphere x=' 0' y='-10' z='0' radius='1.5' color='clr' />
<sphere x=' 0' y=' 10' z='0' radius='1.5' color='clr' />
<sphere x=' 10' y=' 0' z='0' radius='1.5' color='clr' />
<sphere x='-10' y=' 0' z='0' radius='1.5' color='clr' />
<sphere x=' 10' y=' 10' z='0' radius='1.5' color='clr' />
<sphere x=' 10' y='-10' z='0' radius='1.5' color='clr' />
<sphere x='-10' y=' 10' z='0' radius='1.5' color='clr' />
<sphere x='-10' y='-10' z='0' radius='1.5' color='clr' />
</objects>
</scene>

The purpose of this scene is to show how the light is reflected differently off each sphere depending on where the light sources are. With no further delay, here is the image:


And since Brandon wants to see code so badly, here you go:

int main(int argc, char ** argv) {
// do stuff
}

Till next time, when I plan to add either anti-aliasing, secondary rays, or specular lighting, or perhaps some combination of those items. Stay tuned!

Update:
So I outdid myself. It is apparently really easy to add antialiasing to a ray tracer. It just kills performance. The main gist is this, instead of sending out just one ray per pixel in the image, I send out 4, each offset slightly, I then take the average of each ray and set that to the be value for the new pixel. Here is the same scene above with antialiasing. Notice how the sphere's boundaries are not so pixelated, they are smoother. Now, I just need to generalize the code to handle any number of rays per pixel, not just four. I'm getting ahead of myself. Enjoy the image:

No comments: