光线追踪 - 反射

Posted

技术标签:

【中文标题】光线追踪 - 反射【英文标题】:Ray Tracing - Reflection 【发布时间】:2014-03-05 07:14:20 【问题描述】:

我现在正在研究光线追踪器,反射部分。我的一切工作正常,包括创建一个带阴影的球体。现在,我正在实现反射部分。但是,我无法得到它。我的算法如下:

traceRay(Ray ray, int counter)
// look through the intersection between ray and list of objects
// find the final index aka the winning index, (if final index == -1, return background color)
// then calculate the intersection point

// perform reflection calculation here
if(counter > 1  && winning object's reflectivity > 1 )
  //get the intersection normal, vector N
  //Calculate the reflection ray, R
  // let I is the inverse of direction of incoming ray
  //Calculate R = 2aN - I (a = N dotProduct I)

  // the reflection ray is origin at the point of intersection between incoming ray and sphere with the R direction
  Ray reflecRay (intersection_poisition, R);

  Color reflection = traceRay(reflecRay, counter + 1);
  // multiply by fraction ks
  reflection = reflection * ks;



// the color of the sphere calculated using phong formula in shadeRay function
Color prefinal = shadeRay();


// return the total color of prefinal + reflection


我试图获得反射但无法获得,谁能告诉我我的 traceRay 函数算法是否正确?

【问题讨论】:

您已经概述了您的代码应该做什么,但我们不知道它实际上做了什么。 你是如何计算你的反射向量的?此外,请确保将其与最初碰撞的对象偏移以防止自相交(通常沿反射矢量或表面法线偏移)。 打印出您的原始射线数据和中间计算结果可能会有些用处。 反射矢量的方向由下式计算: 2aN - I 其中 N 是球体和交点的法线矢量。 a是N点积I,I是入射光线方向的倒数 我没有时间检查这在代数上是否等同于:math.stackexchange.com/questions/13261/… 但我怀疑你无意中反转了光线方向。 【参考方案1】:

反射光线时,需要沿着反射器的法线移动它,以避免与反射器本身相交。例如:

 const double ERR = 1e-12;
 Ray reflecRay (intersection_poisition + normal*ERR, R);

【讨论】:

以上是关于光线追踪 - 反射的主要内容,如果未能解决你的问题,请参考以下文章

2 追踪光线=》2.6 反射光线

没有递归光线追踪就不可能实现反射和折射?

为啥在我的光线追踪器中计算阴影和反射时会丢失细节

光线追踪中的折射?

2 追踪光线=》2.7 透射光线

一个光线追踪demo