射线追踪:来自多个灯的阴影
Ray Tracing: Shadows from Multiple Lights
[edit]和编程中的典型情况,我在发布后不久就弄清楚了!如果您有兴趣,请参阅我的答案:)
我正在使用C 的射线示踪剂,并希望提供一些帮助。我的场景中有2个灯,一个点灯和一个方向的光,还有一堆球(以及一架飞机作为"地板")。
如果我用一个存在的任何一个灯(但没有其他灯)运行射线示踪剂,则它会按预期产生阴影(请参见下图)。
定向光阴影图像
点灯阴影图像
麻烦是,当我用两个灯都有两个灯的射线示踪剂时,只有点灯阴影出现,我才能说明灯是" on"的,因为场景更明亮:
请参阅下面的代码以获取阴影:
bool Scene::shadowtrace( Ray &ray, double t )
{
Object *obj = obj_list;
Light *lt = light_list;
Vector v1, v2; // hit -> light vector
Hit hit;
Vertex intersect = (ray.position( t ));
intersect.plus( ray.D, -0.001 ); // offset intersection ever so slightly away from object, to avoid self-shadowing
v1.set( 0.0, 0.0, 0.0 );
v2.set( 0.0, 0.0, 0.0 ); // initialise
while (lt != (Light *)0)
{
Ray shadowRay;
shadowRay.P = (intersect);
Vertex lightPos = Vertex( 0.0, 0.0, 0.0, 0.0 );
lt->getPosition( lightPos ); // sets value of lightPos
if (lightPos.x > T_LIMIT) // If set absurdly high, we're dealing with a directional light
{
lt->getDirection( v1 ); // sets v1 to light direction (reversed)
v1.normalise( );
shadowRay.D = v1; // set hit-to-light vector as shadowray direction
while (obj != (Object *)0)
{
if (obj->intersect( shadowRay, &hit ) == true)
{
if (!((hit.t * hit.t) < 0.001)) // Self-shadow if very very small t number
{
return true; // ray hits an onject, and the object occurs before the light
}
}
obj = obj->next( );
}
}
else // otherwise, it's a point light :)
{
v1 = (lightPos.minus( intersect )); // find vector from intersection to light
v2 = v1; // keep un-normalised version for preventing mis-shadowing from objects behind the light source
v1.normalise( );
shadowRay.D = v1; // set ray direction to hit-to-light vector
while (obj != (Object *)0)
{
if (obj->intersect( shadowRay, &hit ) == true)
{
if (!((hit.t * hit.t) > (v2.lengthSq( )))) // Check hit.t against magnitude of (un-normalised) intersection-to-light vector
if (!((hit.t * hit.t) < 0.001)) // Self-shadow if very very small t number
{ // Used hit.t^2 to avoid having to SQRT the length. Is acceptable for comparisons
return true; // ray hits an onject, and the object occurs before the light
}
}
obj = obj->next( );
}
}
lt = lt->next( );
}
return false;
}
如果检测到阴影,则仅将环境光归因于该点,否则将归因于环境 散布(我还没有添加镜头)。
任何建议都很棒!
错误警报伙计们!我发现了!
在每个对象列表循环之前,我添加了:
obj = obj_list;
要重置第一个对象,这解决了问题:)
相关文章:
- 用callgrind追踪不必要的副本
- 光线追踪:椭球命中功能
- C++声明模板参数阴影模板参数错误
- DirectX 11 - 点光源阴影
- 平面和球体光线追踪之间的交点
- 深度缓冲区未填充阴影贴图渲染通道中的数据
- 模板中的Typedef导致阴影模板parm错误
- C++具有内置缓存 - 阴影模板参数的对象工厂
- 使用 GCC 5 进行模板参数阴影
- OpenGL - 阴影贴图/深度贴图给出奇怪的结果
- 不完整的多线程光线追踪器花费的时间是预期的两倍
- 固定点硬件射线追踪产品补偿
- 警告派生的类成员阴影级班级成员
- 多个快速读取器 单个慢速写入器:使用具有原子索引的阴影数据安全吗?
- 用VS配音C++.追踪可变变化
- 如何避免本地兰伯达阴影超负荷分辨率
- MFC 用于计算控件的高光、阴影等的算法或函数是什么?
- 光线追踪对象文件漫反射着色问题
- 射线追踪:来自多个灯的阴影
- 在光线追踪器中实现软阴影