Pages

Subscribe:

Sunday, January 22, 2012

Ray Tracing Pseudocode



Hi!

Today I will be explaining the basics of the Ray Tracing algorithm described on my previous Ray Tracing post. Click here to read. The algorithm will be explained using pseudo code and I will be uploading and explaining the C++ code project on the next post. 



Overall Algorithm (Pseudo code)

Here is a very VERY general idea of how the Ray Tracing algorithm works.

...
// For every column of the image
for (horiz_pix = 0; horiz_pix < horiz_res; horiz_pix++)
{
    // For every row of the image
    for (verti_pix = 0; verti_pix < verti_res; verti_pix++)
    {
        // Calculate color of pixel [horiz_pix][verti_pix]
         
        // Get the window 3D point corresponding to the given pixel. Will assume Z is always 0.0
        window_z = 0.0
        
        // Xmin and Xmax are the minimum and maximum values of the "window" on the X axis
        // Ymin and Ymax are the same as the line above but with the Y axis

        // horiz_pix : horiz_res :: (window_x - Xmin) : (Xmax - Xmin) -> Therfore:

        window_x = Xmin + (((horiz_pix + 1/2) * (Xmax - Xmin)) / horiz_res)
        // I use horiz_pix + 1/2 to go through the "center" of the pixel


        // verti_pix : verti_res :: (window_Y - Ymin) : (YmaY - Ymin) -> Therfore:


        window_y = Ymin + (((verti_pix + 1/2) * (YmaY - Ymin)) / verti_res)
        // I use verti_pix + 1/2 to go through the "center" of the pixel


        
        //Make vector "eye - window" a unit vector. |V| = 1
        direction_x = (window_x -  eye_x)
        direction_y = (window_y  -  eye_y)
        direction_z = (window_z - eye_z)
        direction_length = sqrt(( direction_x ^2) + ( direction_y ^2) + ( direction_z ^2))
        direction_x = direction_x  / direction_length
        direction_y = direction_y  / direction_length
        direction_z= direction_z / direction_length


        // Ray => ((eye_x, eye_y, eye_z), [direction_x, direction_y, direction_z])
        // Find color at the end of the ray

        color = get_color( eye_x, eye_y, eye_z , direction_x, direction_y, direction_z )
        paint_pixel(horiz_pix, verti_pix, color)
    }}


So what do we have here? We will be throwing a ray that emerges from the eye and passes right through each of the pixels that conform the image with a resolution of horiz_res X verti_res. To do this we map each of the pixels to a 3D point on the scene space that is contained on the window through which the scene objects are seen. This point will be defined by window_xwindow_y and window_z, from now on called "window point". To map each pixel we also need to define where is the window that leads to the scene. The window is defined by [XminYmin] and [XmaxYmax]; we will suppose that the window will always remain with a value of 0.0 for the Z axis. Then we calculate the vector that leads from the eye to the window point, from now on called "direction vector", and normalize it (so that is has a length of 1, also called a unit vector). Finally we got ourselves a ray with the eye as the anchor and the direction vector as the ray's direction. We calculate the color of the scene for the ray and paint the image pixel with that color. Pretty easy don't you think?


What does get_color function has inside? Well, it is something like this:

//Get the RGB color of a ray that goes through a pixel
get_color( eye_x, eye_y, eye_z , direction_x, direction_y, direction_z ) returns RgbColor[]
{
    RgbColor[] color_list = []
    Intersection[] object_list

    // Get objects with which the ray has an intersection
    object_list = get_intersections( eye_x, eye_y, eye_z , direction_x, direction_y, direction_z );


    if object_list is empty
        // If there wasn't any intersection at all then we return a background color.
        color_list.append(background_color)
    else
        // Else we return the color of the first object with which there was an intersection.
        color_list.append(intersection[0].object.color)
    return color_list
}


I think the code above explains itself pretty well, so let's continue with the first_intersection function:

//Obtains a list of intersecting objects of a ray
get_intersections( eye_x, eye_y, eye_z , direction_x, direction_y, direction_z ) returns Intersection[]
{
    Intersection[] inter_list = []
    ...
    For Each scene_obj In scene_objects
    {
        new_inter = scene_obj.calculate_intersection(eye_x, eye_y, eye_z , direction_x, direction_y, direction_z )
        If new_inter exists

        {
            inter_list.append(new_inter)

        }
    }
   // We sort the intersections according to their distance. Intersections with smaller distances from the eye should be put first on the intersection list.    
    inter_list.sort()
    return inter_list
}



The function above calculates all the intersections of the ray given as a parameter. For each of the objects on the scene, an intersection between the ray and the scene is calculated. If no intersection exists the object is ignored, otherwise it is put on a list. After all the scene objects are checked, the list is sorted in distance ascending order between the objects and the ray's anchor.

Some details are missing on the three pseudo code functions described above, but the general idea of the Ray Tracing algorithm remains. I will be inserting new pieces of code in there as the project advances. On the next post I will be translating this pseudo code to C++ language and describing how to calculate intersections between a ray and a sphere. Hope you enjoy!

Best regards,

Carlos Fernandez

Thursday, January 19, 2012

Stanford Game Theory Course

Hi!

Stanford University Logo
Yesterday my awesome girlfriend sent me a link about a FREE course that Stanford University will be giving on game theory :)! I posted the link below for those of you interested on subscribing. As said on the course article, "... game theory is the mathematical modeling of strategic interaction among rational (and irrational) agents...", but is also normally used for common games such as poker, chess and is the base foundation of many of the most popular video games nowadays. Totally recommended.

Click here to subscribe to the course. 

Monday, January 16, 2012

Ray Tracing

Hi!

Kind of weird that I am late for my first official post but it's been a busy weekend and I think I still have to get the habit of making weekly posts (first time ever having a blog  = P).

Image generated using Ray Tracing
It has been difficult to decide which subject should I choose to make a good blog starter. After heavily consideration on RPG Games, Mobile Games and 3D Graphics, I decided that 3D Graphics would work best of them all, as I already took a course on the subject and I can seize the opportunity to polish my programming skills on low level languages to make a 3D Image Generation Tool.

As my first project on Ferlocar, I will be working on Ray Tracing :).

For those of you who are not familiarized with the term, this post will give you a brief description of what Ray Tracing means and the project that is ahead for me and any of you who's interested on making your own 3D Image Generation Tool from scratch.

Ray Tracing


What is it?

Ray Tracing is an algorithm that is used to generate three-dimensional images by tracing a ray through pixels in a plane.It is a very elegant technique that has many characteristics that should be taken into account before being used:
  • Generates very realistic images.
  • Provides a very consistent math of reality (you will see soon enough).
  • It can be implemented relatively easy (the basic algorithm).
  • Very helpful to generate shadows, mirrors, transparency, textures, etc.
  • It is VERY slow, which is why it is not normally used on real time environments, such as video games. Still, many of the Pixar and Dreamworks movies are made with this powerful tool.
So... How does it works?

Most of the Ray Tracing realistic nature is based on perspective. To generate a Ray Tracing image there are three main components: the scene, the window and the eye. I will explain each of them as it will help us to understand better the overall explanation of the algorithm.
  • Scene: Is conformed by all the objects that exist on the 3D world that will be represented on the image that is generated. This objects include spheres, cylinders, polygons, cones, etc. Basically any object that can be represented with an equation or mathematical model can be included as part of a ray tracing scene.
  • Eye: It is a point on the 3D space from which the scene is seen. It gives an "anchor" from which rays emerge to pass through the window observe the scene. It gives perspective and the image will be different depending on the position of the eye. 
  • Window: It is the grid through which the rays emerging from the eye pass in order to see what's in the scene. It is with the window that each pixel's color on the image is determined.
I know all of this is confusing, but sometimes an image is better than a thousand words:

Ray Tracing algorithm
In the case above, the "Camera" is the eye, from which all the "View Rays" emerge to observe the "Scene Objects". For each pixel on the generated image, a "View Ray" is shot from the "Camera" on the direction of the window (marked as "Image" above). The intersection of each "View Ray"  with any object on the scene is the one that determines the color of the respective pixel. The color of the object will also vary according to the light sources (which will be discussed on a future post).

What you will be needing


In order to keep up with this project you will be needing:

  • Good programming skills (I will not be explaining how to program in here, although if you have any doubts about what does an specific portion of the code does, I will be more than happy to answer any question)
  • Basic physics and math knowledge (mostly about linear algebra). Still, I will try to explain the basic math and physics topics covered on each section of the Ray Tracing tool.
  • C++ knowledge.  I myself don't know how to program on C++ but I've done some C projects (including a basic Ray Tracer) and will try to learn C++ along the way.

Other details:
  • I will be working on Windows operating system.
  • Have not decided on the IDE but probably Netbeans or Visual Studio 2010

On next Saturday, I will be posting the overall description of the Ray Tracing algorithm on pseudo code and the C++ equivalent. Hope you enjoy it!

Regards!

Carlos Fernandez 

Tuesday, January 10, 2012

Welcome to Ferlocar!


Welcome!

If you are a newbie programmer who's interested in computer graphics and game programming you have reached the correct place. I will be making weekly posts on Saturdays on how to make games and create computer graphics. Hope you enjoy it : )