by Xooxer
3D Object Rendering and Animation [More]
To download this demo for your Linux/Mac installation, enter this on your command line:

DreamDownload byond://Xooxer.mobRay##version=1

Emulator users, in the BYOND pager go to File | Open Location and enter this URL:


Version 1.0
Date added: Oct 14 2012
Last updated: Oct 15 2012
0 fans

This project source is now available for all.

Early screenshot of the raytracing engine at work:
2 white spheres reflecting light, one refracting


Xooxer: (Oct 15 2012, 11:13 pm)
This started off as a port of a ray tracing tutorial in C/C++ I discovered online. (I'm sorry, but I don't know where to find that tutorial now. If you come across it, please let me know.) The main engine of this program is a direct conversion of that tutorial. Everything else was slated to comprise the scene handling and animation features, but were never finished. As such, this source code stands as more a proof of concept than an actual working prototype.

However, as I said, the main ray tracing engine is functional. To demonstrate the rendering in action, simply click the Render button located on the main program window. This will take several minutes to complete. Once finished, you will have the image shown here on this hub page.

The files,,, and are not essential to the rendering engine. It may give a runtime or compile error, but you should be able to run the program without these files. (A quick test shows some errors at compile time, but commenting out the offending code gives the expected result, and renders the final image the same.)

For some reason I wasn't able to determine, my Perlin function caused the render to fail. That's why I've not completely implemented that feature (namely, the GenNoise() proc used to add the texture is commented out in the InitializeRT() proc in Its purpose was to provide a simple procedural texture on the basic primitives. You may wish to try and integrate the noise function, but I would suggest another noise generating algorithm, as I believe this one is faulty.

You can define your own scene by modifying the InitializeRT() procedure in the file. As you can see this procedure initializes datums for each shape instance in the scene, along with a single light source. The default scene, shown above, consists of three spheres, six planes and a light source. The planes are arranged in a cube, so each plane functions as as a wall of the cube. The spheres and light source are located within this cube. The "camera" is also located inside the cube, otherwise, we would not see the scene at all, since there is no light source outside the cube.

I would suggest keeping the cube and creating your own scene within its confines. Simply remove the the spheres and replace with your own scene objects and render. You may have to adjust the cube's dimensions to accommodate your scene.

Essentially, what is happening in this program is that we're tracing the path of a single ray of light from the camera's point of view, through the screen at a known pixel location, and into the scene comprised of our primitive shapes. The procedures defined in the shapes' DM files determines how the ray of light reflects and refracts as it encounters the edges of the shape.

Once the light source is reached by the ray, the color for that pixel is known, and the tracer moves on to the next pixel in the image, tracing another ray of light from the camera, through the scene and finally to the light source. When all the pixels have been determined, the image can be rendered and shown to the user.

The program uses a render buffer to hold the pixel information, and a rather nifty procedure for assembling the blocks of images into the final render. If I remember correctly, BYOND was being stubborn when I tried to render directly to an icon larger than 32x32. That's why WriteImage() seems to obtuse. I'm still a bit fuzzy on what exactly is going on there. If you need specific help writing the image, let me know and I'll disassemble my code for you.

I'm not the best person to try and explain the complex vector math involved in ray tracing, so I'll refer you to these resources online: raytrace/rtrace0.htm Raytracer-in-C++-Introduction-What-is-ray-tracing.html
Red Hall Dev: (Oct 15 2012, 1:47 pm)
Red Hall *Looks for the "Do Something Cool" button*

Um yeah, not quite but a readme or just brief instructions would be great.