Sunday, September 30, 2012

Reflection & Refraction

Images of Reflection:

Refraction: Working to correct black dots in the image.

Area Light and soft shadows

Point lights: In order to check the light intersection with object, we need to fire only one ray in the direction of the point light from the currently intersected point.

Area Light: Area light work differently from the point light. As in area light we need to generate more rays towards the geometry representing area light. Hence, a lot more rays are required for the calculation of light.

While implementing area light, I randomly sampled over 9 points on the area light. However, the result is not good. Following image is generated using area light with 9 sample points:

In order to correct it, more samples are required. However, every time I increase the number of samples from 9 to any number greater than that, my CUDA crashes giving error.

Still trying to resolve the issue.

Saturday, September 29, 2012

Specular Highlights

If the direction of reflected light becomes equal to the direction of eye then we see a surface point P highlighted.

reflectedRay = lightRay.direction - 2.0f * glm::dot( lightRay.direction, normal ) * normal;
float cosAngle = glm::max(glm::dot( eye.direction, reflectedRay ),0.0f);
float spec = powf( cosAngle, specularExponent) * dot(specularColor,currentMat.color);
 colors[index] += spec * lightcolor;




To generate shadows, we need to traverse all the light sources and find if there is any object  between  currently intersected object and the light. If, there is an object then the object is in shadow and we do not need to calculate the color of that pixel.


bool inshadow = false;
for(int i = 0 ;i<numberoflights; i++){

     lightDirection  =normalise( light[i] - intersectionPoint);
     lightOrigin = intersectionPoint;
     lightDist = 100000.0f;
     for(int j = 0; j<numberOfObjects; j++){
          if( i != j && object[i]->intersect(lightorigin,lightdirection,lightDist)){
              inshadow = true;



Problems: The problem which I faced generating shadows was that there was an object at the position of light and hence, that was coming in between the light and the other objects. Hence the shadow was not perfectly generated.

Wrong shadow image:

Corrected Image:

Thursday, September 27, 2012

Diffuse lambertian surfaces

Lambert's Cosine Law: It states that the intensity of light on a diffuse surface is directly proportional to the cosine angle θ between the incoming light ray and the unit normal to the surface.
In layman's language, if the angle is low light intensity is low and if angle is high intensity is high.

Hence, applying lambert's law in ray tracer code will give diffuse lambertian surfaces.


float lambert = (lightRay.direction * normal) * coef;
colors += currentMat.color * light.color * lambert ;

Images generated after its application: (although shadows seem wrong)

Tuesday, September 25, 2012

Ray Tracing - CUDA

Ray Tracing Algorithm:

The ray-tracing is a technique that generates image by parsing through each pixel on the screen and calculating color of each pixel on screen. Hence, for each pixel a ray is shot into the scene. The direction of ray is the line tracing from the eye to the center of the pixel. Using the ray Origin and ray direction, we perform various intersection tests to check if the ray intersects any object in the scene. In some scenarios, the ray will intersect more than one object. So, we select the intersection point which is closest to the eye i.e. origin of the ray. Also, we shoot another ray from the intersection point towards the light. If this ray does not intersect with any object on its way to the light, the hit point is illuminated with the color of the light multiplied by material color of the object. If it does intersect with another object, then that pixel of object comes under shadow.


for (int j = 0; j < imageresoultion.y; j++)
       for (int i = 0; i < imageresoultion.x; i++) {
              // compute ray direction
                  Ray r;
                  computeRayDir(i, j, &primRay);
                  vec3 color=vec3(0.0,0.0,0.0);
               // shoot prim ray in the scene and trace it for intersectionpoint
                //apply pixel color to image
                   output(xpix, ypix)->Red =  (int)(color[0]*255);
                   output(xpix, ypix)->Green =(int)( color[1]*255);
                    output(xpix, ypix)->Blue =  (int) (color[2]*255);


First image with CUDA implementation of ray tracing algorithm, with no lighting.