[Raytracing]代码框架

该伪代码总结了光线追踪算法。


Function Raytrace(Scene World)

{

    for(each pixel of the image)

    {

         Calculate the ray corresponding to the pixel

            (projection);

         Pixel color=trace(ray,0);

    }

}



color trace(Ray myRay,interger recurs_level)

{

     if(myRay intersects an object Obj)

     {

         Calculate normal N at point of intersection Pi;

         Calculate surface color SC at point of intersection Pi;

         Final_Color=Shade(Obj,myRay,N,SC,Pi,recurs_level);

     }

     else

     {

         Calculate background color BkgC;

         Final_Color=BkgC;

     }

     return Final_Color;

}



color Shade(Object obj,Ray myRay,Normal N,Surface_Color SC,

Point Pi,integer recurslevel)

{

     recurslevel++;

     if(recurslevel>MAX_RECURSION_LEVEL)

         return 0;

     

     for(each light source)

     {

         Calculate light ray(Pi points to light source);

         if(light ray doesn't intersect an object)

         {

             add light contribution to color based

              on the angle between light ray and myRay;

         }

     }

     calculate reflect ray;

     refl_color=trace(refl_ray,recurslevel);

     calculate refract ray;

     refr_color=trace(refr_ray,recurslevel);

     

     return average(color,refl_color,refr_color);

}

你可能感兴趣的:(Trac)