Sunday, May 6, 2007

First post!

One night, being an ub3r g33k, I decided that I, too, would write a ray-tracer. Being much more l33t than everyone else, I began by rubbing two sticks together, in hopes of creating enough heat to start a fire, with which I would smelt enough silicone to form the computer with which I would create the ult1mat3 r@ytr@c3r. I quickly abandoned this approach for two reasons-
  1. It was taking to long.
  2. Computer chips are made from silicon, not silicone.
So, instead, I grabbed my copy of "Advanced Graphics Programming in Turbo Pascal" (Roger T. Stevens and Christopher D. Watkins). This epic work on graphics programming includes many great gems that were invaluable during the creation of the ray tracer, such as

Because of the large amount of information in the ray traced (image) file... it is probably not possible to fit a high resolution (image) file onto a standard 360K floppy disk.

My first two choices were 1) what language to implement the ray tracer in and 2) what file format to output to. Given the performance requirements needed for ray-tracing, and need for an output format that would eventually store large images, I choose Ruby as the language and ASCII-mode ppm as the output format.

Next, I typed in all the vector and matrix math from the book, carefully converting to Ruby syntax. By the end, I expected this to be the largest effort of the entire project, although this did not end up being the case.

After all of the math was entered, I then added the sphere intersection logic. After verifying that this functioned correctly, I added the main rendering loop, which is almost entirely original code. I adjusted this loop several times, but could not yet get the perspective to come out right.

x /= @viewport_size
y /= @viewport_size
#film_point = vec(x, y,0)
film_point = vec(x, y,-@viewport_z)
#film_point = vec(x, y,@viewport_z)
#film_point = vec(0, 0, 0)
#film_point = vec(0, 0, -@viewport_z)

# My original way, causes distortion
ray_dir = vec(-x, -y, @viewport_z)
# Books way, causes way worse distortion

#eye = vec_lin_comb(x, @u_vec, y,@v_vec)

#ray_dir = vec_add(@view_vec, eye)



ray_dir_normal = vec_normalize( ray_dir )

hit = cast_ray(film_point, ray_dir_normal)

image_buffer[ix][iy] = hit[0].get_color if(hit[0] != nil)


This is used to render this image.



Notice several things about the picture above:
  1. The spheres are all different colors, instead of all being purple-ish.
  2. I've already posted code, yeah I'm talking to you, brailsmt.
  3. There are no rendered light sources in the image.
Note that there aren't any non-rendered light sources either. This is because I spent most of my time trying to fix the perspective problem mentioned above. Oh well. I'll stop by brailsmt's (http://brailsmt.blogspot.com) cube in the morning and pester him until he admits how he fixed it in his code.

1 comment:

brailsmt said...

LOL! C++ R4y TR4c3r5 are teh r0x0rz!!!1