在光线追踪中生成折射光线

Generating refracted rays in ray tracing

给定入射光线、法向量和两个折射率。我怎样才能计算折射光线。我知道折射的理论方面。就是不知道怎么实现。

函数应该类似于

vec refract(vec incident, vec normal, double index1, double index2);

注意:这不是作业,所以请随意 post 任何代码,最好是 Java

这是我用于爱好光线追踪器的东西: // refraction for incident ray (i) into surface with normal (n), if total internal reflection occurs, then function // return false and output vector (r) is not changed // for example if we entering water from air then ior_ratio = IOR(air)/IOR(water) = 1.0003/1.3333 ... bool refract (const V3& i, const V3& n, R ior_ratio, T& r) { auto cos_i = dot(-i, n); auto cos_t2 = ((R) 1) - ior_ratio * ior_ratio * (((R) 1)- cos_i * cos_i); if (cos_t2 <= 0) return false; r = ior_ratio * i + ((ior_ratio * cos_i - sqrt(abs(cos_t2))) * n); return true; } 请注意,事件的方向让我感到困惑,所以请检查这是否是您所期望的,否则将 -i 更改为 i。此外,您可能需要考虑菲涅尔效应 http://graphics.stanford.edu/courses/cs148-10-summer/docs/2006--degreve--reflection_refraction.pdf