编辑在编程中是典型的,我在发布后不久就知道了!如果你感兴趣,请看我的答案:)
我正在C++的射线追踪仪上工作,想得到一些帮助。我的场景中有两盏灯,一盏点灯和一盏定向灯,还有一束球体(还有一架作为“地板”的飞机)。
如果我运行光线追踪器中的任何一盏灯存在(但另一种光不存在),它会产生阴影,如预期(见下图)。
问题是,当我运行我的光线追踪器时,只有点光阴影出现时,我才能分辨出光线是“开启”的,因为场景更明亮:
请参阅下面的代码,以检测阴影:
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;
}如果一个阴影被检测到,只有周围的光被归因于点,否则环境+漫射被归因于(我还没有找到增加镜面的方法)。
任何建议都会很好!
发布于 2017-02-24 18:55:14
假警报伙计们!我想出来了!!
在每个对象列表循环之前,我添加了:
obj = obj_list;要重置到第一个对象,这解决了以下问题:)
https://stackoverflow.com/questions/42446050
复制相似问题