我在写射线追踪器。到目前为止,我有漫反射,布林照明和反射。我的折射出现问题,我不知道是什么。我希望有人能帮助我。 射线追踪 - 折射错误
我有一个很大的红色diffuse + Blinn球体和一个折射率n = 1.5的小折射体。
小的只是真的搞砸了。
相关代码:
ReflectiveSurface::ReflectiveSurface(const Color& _n, const Color& _k) :
F0(Color(((_n - 1)*(_n - 1) + _k * _k)/((_n + 1)*(_n + 1) + _k * _k))) {}
Color ReflectiveSurface::F(const Point& N, const Point& V) const {
float cosa = fabs(N * V);
return F0 + (F0 * (-1) + 1) * pow(1 - cosa, 5);
}
Color ReflectiveSurface::getColor(const Incidence& incidence, const Scene& scene, int traceDepth) const {
Point reflectedDir = reflect(incidence.normal, incidence.direction);
Ray ray = Ray(incidence.point + reflectedDir * epsilon, reflectedDir);
return F(incidence.normal, incidence.direction) * scene.rayTrace(ray, traceDepth + 1);
}
Point ReflectiveSurface::reflect(const Point& N, const Point& V) const {
return V - N * (2 * (N * V));
}
bool RefractiveSurface::refractionDir(Point& T, Point& N, const Point& V) const {
float cosa = -(N * V), cn = n;
if (cosa < 0) { cosa = -cosa; N = N * (-1); cn = 1/n; }
float disc = 1 - (1 - cosa * cosa)/cn/cn;
if (disc < 0) return false;
T = V/cn + N * (cosa/cn - sqrt(disc));
return true;
}
RefractiveSurface::RefractiveSurface(float _n, const Color& _k) : ReflectiveSurface(Color(1, 1, 1) * _n, _k) {}
Surface* RefractiveSurface::copy() { return new RefractiveSurface(*this); }
Color RefractiveSurface::getColor(const Incidence& incidence, const Scene& scene, int traceDepth) const {
Incidence I = Incidence(incidence);
Color reflectedColor, refractedColor;
Point direction = reflect(I.normal, I.direction);
Ray reflectedRay = Ray(I.point + direction * epsilon, direction);
if (refractionDir(direction, I.normal, I.direction)) {
Ray refractedRay = Ray(I.point + direction * epsilon, direction);
Color colorF = F(I.normal, I.direction);
reflectedColor = colorF * scene.rayTrace(reflectedRay, traceDepth + 1);
refractedColor = (Color(1, 1, 1) - colorF) * scene.rayTrace(refractedRay, traceDepth + 1);
}
else {
reflectedColor = scene.rayTrace(reflectedRay, traceDepth + 1);
}
return reflectedColor + refractedColor;
}
的代码是所有的地方,因为这是一门功课,我不会允许包括额外的头,我必须在发送一个CPP文件,所以我不得不将每一个课程分成前向申报,申报和实施。它让我呕吐,但我尽量保持清洁。有很多代码,所以我只包括我认为最相关的东西。 ReflectiveSurface是RefractiveSurface的父类。 N是表面法线,V是该法线的光线方向矢量,n是折射率。发生结构包含一个点,一个法线和一个方向向量。
公式为Fersnel近似和分别折射向量:
可以在我使用的ε*射线方向上的值,以避免由浮子不精确阴影粉刺代码中看到。不过,类似的事情似乎也发生在小球体上。 另一张截图:
正如你所看到的,球没有出现透明的,但它确实继承漫球体的颜色。它通常也有一些白色像素。
无折射:
涉及什么样的数字?有可能你的数值变得太小,无法用“float”来准确表示,并且将变为零。你可以/你尝试过使用'double'吗? – 2014-11-04 16:56:37
在开始的几次迭代中,当它在getColor函数的末尾以调试模式到达时,似乎处于20-100范围内,然后将与摄像机的距离^ 2相除,饱和。 – PEC 2014-11-04 17:05:52
我们都在想。死星 – 2014-11-05 17:58:02