我现在正在研究光线追踪器,反射部分。我让一切正常工作,包括创建一个带有阴影的球体。现在,我正在实现反射部分。但是,我无法理解。我的算法如下:
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 函数算法是否正确吗?
反射光线时,需要沿反射器的法线移动它,以避免与反射器本身相交。例如:
const double ERR = 1e-12;
Ray reflecRay (intersection_poisition + normal*ERR, R);