Monday, February 25, 2013

Complex Geometry and Interactive Raytracing




A few screenshots and a video of the interactive CUDA raytracer with added support for OBJ files.






It took me a while to complete the algorithm that extrapolates normals in a triangle. The trick is to compute the area of each triangle defines by the intersection point and the three vertices. Once you get that, just find the weighted mean, and you're done. Here is the code:

float4 v0 = triangle.p0 - intersection;
float4 v1 = triangle.p1 - intersection;
float4 v2 = triangle.p2 - intersection;
float a0 = 0.5f*vectorLength(crossProduct( v1,v2 ));
float a1 = 0.5f*vectorLength(crossProduct( v0,v2 ));
float a2 = 0.5f*vectorLength(crossProduct( v0,v1 ));
normal = (triangle.n0*a0 + triangle.n1*a1 + triangle.n2*a2)/(a0+a1+a2);

As simple as that :-)