|
|
最近想学习下光线追踪的一般算法.于是开始写个小程序测试.但是碰到了一些问题.
我在不考虑光线的反射与折射递归(也就是简单的光线投掷方法)的时候,渲染出来的结果还算正确.

但是如果我加上反射光线的运算后,图片就出现了以下结果:

光线追踪的trace函数大体是这样的:
Color Context::_trace(Ray ray, int recCnt)
{
if(recCnt > 5)//递归5次自动结束
{
return Color(0.f, 0.f, 0.f, 0.f);
}
Vector pos, nor, texcoord;
Surface* surface;
//射线与场景求交,intersect函数根据ray和场景求交,后几个参数分别返回交点的位置,
//法线,纹理坐标和交点所在表面的指针
if(m_sence->intersect(ray, &pos, &nor, &texcoord, &surface))
{
//如果反射率大于0,则递归构造反射光线
Color result(0.f, 0.f, 0.f, 0.f);
/*if(surface->getMaterial()->getReverberationRate() > 0.0f)
{
Ray reverberationRay(pos, nor*2*nor.dot(ray.getDirection()) - ray.getDirection());
result += surface->getMaterial()->getReverberationRate() * this->_trace(reverberationRay, recCnt + 1);
}*/
//计算场景中各个光源照在此点产生的颜色,并通过阴影光线生成阴影
result += m_sence->lightColor(ray, pos, nor, texcoord, surface);
return result;
}
else//如果和场景没有交点,则返回背景颜色
{
return Color(0.f, 0.f, 0.f, 0.f);
}
}
如果我把其中注释掉的代码打开,就会变成第2张图那样面目全非的结果.
请教下各位达人,这是什么问题? |
|