我正在根据以下内容编写着色器冯模型 http://en.wikipedia.org/wiki/Phong_reflection_model。我正在尝试实现这个方程:
其中 n 是法线,l 是光线方向,v 是相机方向,r 是光反射。维基百科文章中更详细地描述了这些方程。
截至目前,我仅在定向光源上进行测试,因此没有 r^2 衰减。环境术语添加在下面的函数之外并且效果很好。如果点积为负,则函数 maxDot3 返回 0,就像 Phong 模型中通常所做的那样。
这是我实现上述方程的代码:
#include "PhongMaterial.h"
PhongMaterial::PhongMaterial(const Vec3f &diffuseColor, const Vec3f &specularColor,
float exponent,const Vec3f &transparentColor,
const Vec3f &reflectiveColor,float indexOfRefraction){
_diffuseColor = diffuseColor;
_specularColor = specularColor;
_exponent = exponent;
_reflectiveColor = reflectiveColor;
_transparentColor = transparentColor;
}
Vec3f PhongMaterial::Shade(const Ray &ray, const Hit &hit,
const Vec3f &dirToLight, const Vec3f &lightColor) const{
Vec3f n,l,v,r;
float nl;
l = dirToLight;
n = hit.getNormal();
v = -1.0*(hit.getIntersectionPoint() - ray.getOrigin());
l.Normalize();
n.Normalize();
v.Normalize();
nl = n.maxDot3(l);
r = 2*nl*(n-l);
r.Normalize();
return (_diffuseColor*nl + _specularColor*powf(v.maxDot3(r),_exponent))*lightColor;
}
不幸的是,由于某种原因,镜面反射这个术语似乎消失了。我的输出:
正确输出:
第一个球体仅具有漫反射和环境阴影。看起来不错。其余的都有镜面反射项并产生不正确的结果。我的实现有什么问题吗?