根据Phong模型,我正在写一个着色器。我正在努力实现这一等式:

其中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;
}不幸的是,镜面术语似乎因某种原因而消失了。我的产出:

正确输出:

第一个球体只有漫射和环境阴影。看起来是对的。其余的都有镜面术语,并产生错误的结果。我的执行有什么问题?
发布于 2014-06-10 15:25:00
这一行看上去不对:
r = 2*nl*(n-l);2*nl是一个标量,所以这是在n - l的方向上,这显然是错误的方向(您也规范了结果,所以乘以2*nl什么都不做)。考虑一下n和l何时指向相同的方向。结果r也应该在相同的方向上,但是这个公式会产生零向量。
我觉得你的括号放错地方了。我认为应该是:
r = (2*nl*n) - l;我们可以很容易地在两个边界上检查这个公式。当n和l指向同一方向时,nl为1,因此结果也是相同的向量,这是正确的。当l与曲面相切时,nl为零,结果为-l,这也是正确的。
https://stackoverflow.com/questions/24132774
复制相似问题