加载中…
正文 字体大小:

ray tracing

(2013-01-07 10:20:22)
标签:

杂谈

ray <wbr>tracing

//Created by pritry

Vec3f RayTracer::mirrorDirection(const Vec3f &normal, const Vec3f &incoming) const
{
 Vec3f R = incoming-2*incoming.Dot3(normal)*normal;
 R.Normalize();
 return R;
}

bool RayTracer::transmittedDirection(const Vec3f &normal, const Vec3f &incoming, float index_i, float index_t, Vec3f &transmitted) const
{
 Vec3f I = incoming;
 I.Negate();

 float index_r = index_i/index_t;
 float NI = normal.Dot3(I);
 float dt = 1-index_r*index_r*(1-NI*NI);
 if(dt<=0)return false;
 float tp = index_r*NI - sqrt(dt);

 transmitted = tp*normal-index_r*I;
 transmitted.Normalize();

 return true;
}

Vec3f RayTracer::traceRay(Ray &ray, float tmin, int bounces, float weight, float indexOfRefraction, Hit &hit) const
{
 int i;
 float d;
 float distanceToLight;
 Vec3f tmp;
 Vec3f normal;
 Vec3f Zero(0, 0, 0);
 Vec3f dirctionToLight;
 Vec3f dirctionMirror;
 Vec3f dirctionTransmitted;
 Vec3f lightcolor;
 Vec3f diffuselightcolor;
 Vec3f color_pixel;
 PhongMaterial back(Zero, Zero, 0, Zero, Zero, 0);
 
 Vec3f::Mult(color_pixel, scene->getAmbientLight(), hit.getMaterial()->getDiffuseColor

                   ());
 normal = hit.getNormal();
 if(ray.getDirection().Dot3(hit.getNormal())>0)
  normal.Negate();

 //==================阴影==============================
 for(i=0; igetNumLights(); i++)
 {
     Vec3f color = Zero;

  scene->getLight(i)->getIllumination(hit.getIntersectionPoint(), dirctionToLight,

                                        lightcolor, distanceToLight);
  Vec3f::Mult(diffuselightcolor, lightcolor, hit.getMaterial()->getDiffuseColor());
  d = max(dirctionToLight.Dot3(normal), 0);

  Ray ray2(hit.getIntersectionPoint()+dirctionToLight*tmin, dirctionToLight);
  Hit hit2(distanceToLight, NULL, Zero);

  scene->getGroup()->intersect(ray2, hit2, tmin);

  float e = hit2.getT() - distanceToLight;
  if(e>=-0.000001f && e<=0.000001f)
  {
   color += diffuselightcolor;
   color += hit.getMaterial()->Shade(ray, hit, dirctionToLight, lightcolor);
  }
  
  color_pixel += d*color;
 }

 //==================反射==============================
 if(hit.getMaterial()->getReflectiveColor() != Zero && bounces>0)
 {
  dirctionMirror = mirrorDirection(normal, ray.getDirection());
  Ray ray3(hit.getIntersectionPoint()+dirctionMirror*tmin, dirctionMirror);
  Hit hit3(FLT_MAX, &back, Zero);

  if(scene->getGroup()->intersect(ray3, hit3, tmin))
   Vec3f::Mult(tmp, traceRay(ray3, tmin, bounces-1, weight, indexOfRefraction, hit3), 

        hit.getMaterial()->getReflectiveColor());
  else
  {
   Vec3f::Mult(tmp, scene->getBackgroundColor() , hit.getMaterial()->getReflectiveColor

                ());
  }
  color_pixel += tmp;
 }

 //==================折射==============================
 if(hit.getMaterial()->getTransparentColor() != Zero && bounces>0)
 {
  float index = (indexOfRefraction==1.0f)?hit.getMaterial()->getIndexOfRefraction

                 ():1.0f;

  if(transmittedDirection(normal, ray.getDirection(), indexOfRefraction, index, 

                          dirctionTransmitted))
  {
   Ray ray4(hit.getIntersectionPoint()+dirctionTransmitted*tmin, dirctionTransmitted);
   Hit hit4(FLT_MAX, &back, Zero);

   if(scene->getGroup()->intersect(ray4, hit4, tmin))
   {
    Vec3f::Mult(tmp, traceRay(ray4, tmin, bounces-1, weight, index, hit4),

                hit.getMaterial()->getTransparentColor());
   }
   else
   {
    Vec3f::Mult(tmp, scene->getBackgroundColor() , hit.getMaterial()-

                    >getTransparentColor());
   }

   color_pixel += tmp;
  }
 }

 return color_pixel;
}

 

0

阅读 评论 收藏 转载 喜欢 打印举报
已投稿到:
前一篇:内存池
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

    < 前一篇内存池
      

    新浪BLOG意见反馈留言板 不良信息反馈 电话:4006900000 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有