我的ray tracer

最近,为了练手,也为了graphics的作业,亲手写了一个ray-tracer。

其实,ray tracing的理论还是很简单的,但是,图形学就是这样,看着简单,但是写不出来,门槛很高,非图像所及耶~

理论图如下:

 

理论还是很简单的,算法如下:

main ( ) //主函数

{

         for(需要计算颜色的每一像素pixel) {

                   确定通过视点V和像素pixel的光线R;

                   depth = 0; // 递归深度

                   ratio = 1.0; //当前光线的衰减系数,1.0表示无衰减

                   // color是经计算后返回的颜色值

                   RayTrace(R, ratio, depth, color); 

                   置当前像素pixel的颜色为color;

         }

} // 主函数main( )结束

 

RayTrace(R, ratio, depth, color) //说明:光线跟踪子函数

{

         if(ratio < THRESHOLD) {              //终止条件2

                   color为黑色;

                   return;

         }

         if(depth > MAXDEPTH) {             //终止条件3

                   color为黑色;

                   return;

         }

         // to be continued

 

光线R与场景中的所有物体求交。若存在交点,找出离R起始点最近的交点P

        

if(交点不存在) {                       //终止条件1

                   color为黑色;

                   return;

         }

         用局部光照明模型计算交点P处的颜色值,并将其存入local_color

 

         // to be continued

if(交点P所在的表面为光滑镜面) {

                   计算反射光线Rr;

                   //递归调用!

                   RayTrace(Rr, ks*ratio, depth+1, reflected_color);

         }

         if(交点P所在的表面为透明表面) {

                   计算透射光线Rt;

         //递归调用!

                   RayTrace(Rt, kt*ratio, depth+1,transmitted_color);

         }

         // to be continued

依照Whitted模型合成最终的颜色值,即:

         color = local_color + ks*reflected_color

                kt*transmitted_color

 

         return;

} // 光线跟踪子函数RayTrace( )结束

 

 

在写的时候,有几个类,

Classes

1. Class Vector3

2. Class Ray

3. Class Color

4. Class Geometry

5. Class Sphere

6. Class Screen

  camera and light source

7. Class Light

 

场景中只有R、G、B三个球,此外,只写了reflection,没有refraction和transmission,从效果可以看出,

 

 

效果还是对的,不过,以后还需继续努力,做了反射,以后的折射和透射,以及radiosity和photon mapping都不是问题,下面是努力的方向,

ØRefraction and transmission
ØKd-tree and BVH and grid structure.
ØGPU and CUDA
ØMillions of triangles
ØRender engine

 

posted @ 2010-05-27 23:56  Tandy  阅读(687)  评论(0编辑  收藏  举报