Ray Tracer

JOSH LELAND | PHANTOM BADGER

Copyright © All Rights Reserved

You can download this software here: TBA upon completion of marking

 

 

Platform: Visual Studio 2013 using OpenGL

 

Language: C++

 

Created By:

Josh Leland

Minsi Chen (Lecturer at the University of Derby)

Ray Tracer

Graphics 2's first assessment was an introduction to Ray Tracing rendering. The above is my final submission, this module is currently being assessed so there will be no download for this piece of software for now.

Our assessment tasked us with fulfilling the following criteria with our work:

  • Hit Detection with
    • Infinite Planes
  • Triangles
  • Spheres
    • Diffuse Colour using the Lambertian model (Lambert 1770s)
    • Specular Colour using either the Phong(Phong 1973) or Blinn-Phong(Blinn 1977) models
    • Reflections using recursive Ray Tracing
    • Refractions using recursive Ray Tracing
    • Shadow detection through the use of shadow rays

    An optional addition to the project was to modify the framework we had been given to create an Orthographic camera view, something I managed to accomplish towards the end of the assignment.

     

    Ray Tracing is a form of rendering that is not run at real-time, it is done by firing a series of simulated light rays from the camera, and using what they intersect with to determine the colour of the pixel at that position on screen. The inverse of this is through the use of Projection with the Rasterisation Pipeline, which features the use of triangular geometric models called polygons.

     

    On this page Im going to break down how I accomplished the above tasks;

     

    First of all it is important to note that a Ray is represented by the equation P(t) = S + vt, where P(t) is a given point along the ray, S is the origin of the ray, v is the vector, and t is time.

    Hit Detection with planes was relatively simple. If a ray intersects a plane at point P(t) then it satisfies the equation:

    (S+vt) . n + d = 0

    where n is the plane's normal and d is the plane's offset.

     

    It starts getting a bit more tricky after that however, and a lot more Math intensive.

    A Ray's intersection with a Sphere can be analysed using the equation

    ((S+vt) - C) . ((S+vt) - C) = r2

    Where C is the centre of the circle and r is the radius. This may seem simple, but when it comes to calculating the t value from this equation we get:

    t= (-v . (S - C) (+-) SquareRoot((v . (S - C))2 - (v . v)((S - C) . (S - C) - r2 )) / v . v

    although, with the proper formatting, this does look a lot less daunting!

     

    And arguably, triangular intersection is harder than that! Triangular intersection involves checking whether a ray collides with an infinite plane constrained by 3 given points. The result requires the use of 'Barycentric Co-ordinates,' and this website can explain it far better than I can: http://www.blackpawn.com/texts/pointinpoly/

     

    Okay!, thats the basic hit detection done, Im not going to go into depth on the Diffuse or Specular equations, as this website doesnt have nearly enough formatting tools to present it in a way that isnt confusing and silly. For Diffusion I used the Lambert model, and for Diffuse I used the Blinn-Phong model.

     

    Reflection and Refraction posed a challenge, if not just because it was difficult to discern how to translate the equations into code, the equation used to find the vector of a reflected ray is given by:

    Reflection = v - 2(v . n)n

    And Refraction was given by:

    Refraction = r1/r2 i - (n1/n2cosThetai + SquareRoot(1- sin2Thetai)n

    Where r1 and r2 represent the refractive coefficients of each material, and i represents the vector going into the material, with j representing the vector refracted by the material (conveniently, this equation was formed so we wouldnt need j, just its refractive coefficient).

     

    One of the final additions I made was a simple anti-aliasing quality, originally the ray tracer used one ray per pixel, with my latest addition, it now does 9 and averages the colour between them, (one ray in the center, one in each corner and one in the middle of each side).

     

    And thats the fundamentals of how I created my Ray Tracer! I hope you learnt something from my weird explanation-type-thing.