Computer Science and Software Development blog, focusing on the areas of Data Mining, Text Mining, Machine Learning and Web Development.

Thursday, October 16, 2008

Ray Tracing: A Simple Java, Open Source Implementation

From Wikipedia: "Ray tracing is a technique for generating an image by tracing the path of light through pixels in an image plane. The technique is capable of producing a very high degree of photorealism".

Basically, it means that we want to render images by imitating how we perceive the physical world as its image is recreated in our mind using the light rays that hit objects in space and are then returned to our eyes. We do this by modeling the world in a virtual 3d space. We place a virtual "eye" which is looking at a given direction and has a viewing plane, which is the portion of the world which the eye sees. Then, project a "ray of light" from the eye and through each pixel on the viewing plane and follow the ray along its path in space.

We test to see if the ray intersects any objects along that path. If it does, we calculate the color of the object at the point of intersection, taking into account visible light sources, the object's properties such as color, material, etc. This process is actually known as "Ray Casting". To make it "Ray Tracing" we continue tracing the ray after the intersection to see if the reflected ray hits any objects, and so on.

One of the assignments in the Computer Graphics course I took last semester was to write a simple ray tracer in Java. Written by myself and my more than capable partner, Gur Dotan, the tracer was implemented from scratch, and includes the following features:

You can download the application here. Simply run the JAR file to start things up. It is completely self contained and has no 3rd party dependencies. The right window is where you enter the model of the world, and you can download some reference scenes here. Then, simply click Render.

The source code is actually pretty straightforward, can be found here, in the form of an Eclipse project. A short document outlining things is contained in the zip file.

Please note that the code is provided mostly for educational purposes and it is released as-is, without warranty of any kind. That being said, feel free to make any use of it. It is free both as in "free beer" and as in "free speech".

If you're looking for a commercial grade ray tracer, I recommend POV-Ray, an excellent open source ray tracer written in C++.

27 comments:

hi barakI am studying computer science in switzerland and I have a course about rendering algorithms and ray tracing.I tried to run raytracer projcet that is in your weblog but I couldnot.it always says the error "cannot find the main calss".would you pleas help me more as soon as possible?thankszohre

hey, i have a course "parallel computing" and my end semester project is to implement parallel ray tracing and later show the speed up. can i use your code and implement it on clusters? would it be troublesome? i mean since graphics donot work on clusters

Feel free to use my code in any way you'd like. I'd be happy if you sent me a link to the end result.

Ray tracing lends itself well to parallelization since each pixel's color is found independently from other pixels. What you need to do is parallelize the two for loops in RayTracer.renderTo, it shouldn't be too hard, and you should probably get near linear speed-up.

Not sure what yo mean by "graphics don't work on clusters". I guess you'd want to do the rendering (finding pixel colors) in the cluster, then use a client computer to display the result.

i don't understand one thing, might be the simplest though. The sample images you have given, how do i seethe implementation of ray tracing in that? I open an image and click render, it works fine, but isnt it just image rendering? i have to use only one image throughout and check speed up for that particular image.

Ray tracing is a form of image rendering. Each sample file is a description of a 3d world model (where the camera is and where it's looking, where objects are placed, etc.). Rendering is the process of constructing an image from the above model.

Hi Barak, I take your Ray Tracing as base for an exercise and tray to adapt to my input required. They give me fov, near and far planes. I represent the scene in Opengl and then ray trace, but i cant find the relation between screen-dist and screen-width you use and i don't know how to calculate them from my input. Can you help me?

You can implement your own shape object that can tell whether a given ray intersects it (see Box for a reference example). Alternatively, you can break down your complex object (e.g. bunny) into many small rectangles and render those. Keep in mind though that my code is not production grade and will be very slow if you used a very large number of objects in a single scene.

thanks for your reply..i am unable to generate the input files (similar to those you have provided) for images like bunny etc..however i could find some ply files but we are facing difficulty in parsing those files.can you please give some input files for such images as we need in our project.

Hi Barak!I was looking a ray tracing on the web. luckely i found ur project, but itsn´t exactly the type of ray tracing im looking, althoug maybe i could change ur algorithm... the purpose of my project are acoustic measurements... so do you think that would be easy to adapt ur code to acoustic aplication¿?

Hello Barak, my name is Daniel, I'm from Argentina. I'm a Java developer, I love it and I love drawing too.I found raytracing a good way to apply both programmingand drawing together. I like mathematic but I'm notgood about it. I will feel happy when I could create a raytracing program like yours but I realised I need someone who can help me, to guide me about mathematic and get few objectives to achieve this program.

I was following your code, it is simple but I sometimes get lost about mathematic.

For a class, I parallelized your ray tracer using the ParallelJava package. (Tiny change to only one file, but it caused a decent speedup.) My website currently links to this solution, but it does not refer to you. It's a personal site in its infancy; eventually it will give you the props you deserve -- I promise. If you're interested see charego.com/downloads.