Phong闪电模型照亮了角落

时间:2017-10-15 14:49:49

标签: c++ raytracing phong

我正在尝试在this cpp框架中实现phong闪电模型。

扩散效果很好,但是镜面似乎闪现了球体角落......我不确定为什么,因为公式似乎是正确的。有人有想法吗?

实际输出:

enter image description here

我的代码如下:

Material *material = obj->material;            //the hit objects material
Point hit = ray.at(min_t);                     //the hit point
Vector N = obj->normal(hit);                   //the normal at hit point
Vector V = -ray.D;                             //the view vector

Color color = material->color;
double colorFactor = material->ka;

for( size_t i = 0; i < numLights ; ++i)
{
    Vector lightsVector = hit - lights[i]->position;
    lightsVector.normalize();
    Vector vectorReflection = (2*(N.dot(lightsVector))*N) - lightsVector;
    vectorReflection.normalize();
    double diffusion = std::max<double>(0.0,-material->kd*(lightsVector.dot(N)));
    double specular =  material->ks*pow(std::max<double>(0.0,vectorReflection.dot(V.normalized())),material->n);
    std::cout << specular << std::endl;
    colorFactor +=  diffusion + specular;
}
color *=colorFactor;

我非常感谢你能给我的任何暗示......

0 个答案:

没有答案