Tuesday, January 24, 2012

CUDA ray tracing part 1


RTObject.h includes three basic classesRTObject, HitStructure, BBox. I combined Triangle and Sphere into one class--RTObject, based on the following considerations:
a. Sphere class has two basic properties (sphere center and radius), whereas, Triangle class has three points(points are float3 type, radius is float type)
b. In CUDA, it's not like C++ has complete class hierarchy, which means you can’t have a super class which does not contain anything and its two subclasses containing their own data. CUDA cannot figure out what the super class points to in running time. This is my supposition
c. In my RTObject there are four basic properties (three float3 data (points) and a float data(radius)). If a RTObject is a triangle, I ignore the radius, and if it's a sphere I ignore two points and let the left point be the sphere center. It may waste a lot of memory space for a scene full of many spheres, However I believe that a normal scene contains only a few spheres which may waste a little memory space three basic classes , whereas, triangles are the basic elements of meshes or complicated scenes, for the meshes, only radius data are useless(4 bytes), which is a kind of trade off between complexity design and a little overusing memory.

In Ray.h, I normalize every type of ray whenever created except the shadow ray. Each ray maintains its own tmin and tmax for the intersection test. tmax will be changed only if the intersection condition is satisfied, which is the criterion of intersection test.

In Mesh.h, technically it is supposed to have a pointer to a triangle list and a pointer to its BVH list. However on normal Nvidia devices, CUDA does not support deep copy (when you copy the data of Mesh class, it does not copy the data its data members(pointers) point to). Instead I use index tokens to mark the triangle list and BVH list. When you copy mesh lists, as well as the triangle lists and the BVH lists their tokens point to .

In BVH.h, building BVH is only based on the information of AABB (axis aligned bounding box) of all objects in the scene. I put the whole built BVH into BVH array .
a. Find the mid point of the current part of the BBoxes Array
b. Use spatial median cut
c. Store the start, median, and end position of the BBoxes Array,
let the start position be the start position of subarray1 (which still is inside of BBoxes Array), just as if it’s independent)
let the median position be the end position of subarray1
let the median position be thestart position of subarray2
let the end position be the end position of subarray2
d. goto a;