2017-07-26 75 views
2

我认为这些应该是循环的。我认为我的法线有问题,但我没有发现他们有什么问题。再次,为法线找到一个好的测试是困难的。为什么我的镜面高光椭圆?

以下是图像:enter image description here

下面是每个光我的阴影代码,而忽略了对反射递归部分:

lighting = (hit.obj.ambient + hit.obj.emission); 
    const glm::vec3 view_direction = glm::normalize(eye - hit.pos); 
    const glm::vec3 reflection = glm::normalize((static_cast<float>(2) * (glm::dot(view_direction, hit.normal) * hit.normal)) - view_direction); 
    for(int i = 0; i < numused; ++i) 
    { 

     glm::vec3 hit_to_light = (lights[i].pos - hit.pos); 
     float dist = glm::length(hit_to_light); 
     glm::vec3 light_direction = glm::normalize(hit_to_light); 
     Ray lightray(hit.pos, light_direction); 
     Intersection blocked = Intersect(lightray, scene, verbose ? verbose : false); 
     if(blocked.dist >= dist) 
     { 
      glm::vec3 halfangle = glm::normalize(view_direction + light_direction); 
      float specular_multiplier = pow(std::max(glm::dot(halfangle,hit.normal), 0.f), shininess); 

      glm::vec3 attenuation_term = lights[i].rgb * (1.0f/(attenuation + dist * linear + dist*dist * quad)); 
      glm::vec3 diffuse_term = hit.obj.diffuse * (std::max(glm::dot(light_direction,hit.normal) , 0.f)); 
      glm::vec3 specular_term = hit.obj.specular * specular_multiplier; 
     } 
    } 

,这里是哪里变换对象空间正常行对于世界空间:

*norm = glm::normalize(transinv * glm::vec4(glm::normalize(p - sphere_center), 0)); 

使用完整的Phong模型,而不是布林 - 海防,我得到泪滴亮点:

enter image description here

如果根据(的绝对值)在交叉点得到以下图像(R = X,G = Y,B = Z)正常的I彩色像素: enter image description here

+0

如何你改变你的法线?既然你可能从球体开始并且非均匀地缩放它们,你需要使用转置逆而不是正则转换矩阵(参见[这个问题])(https://math.stackexchange.com/questions/924659/transformation-of -a-surface-normal)更多详细信息) –

+0

我确实使用了transpose inverse来转换法线,我使用'glm :: transpose(glm :: inverse(object_transform))'所以我不认为矩阵计算错误。为了防止出现问题,我会将正常转换线添加到此问题中。 – user393454

+0

虽然它可能还不是完整的解决方案,但是您是否尝试从Blinn-Phong模型(仅为近似值)转换到完整的Phong模型,即用'dot(reflection,light_direction)替换'dot(halfangle,normal)' )'? –

回答

0

我已经解决了这个问题。事实证明,法线都略微偏离,但不足以使法线着色的图像可以描绘它。

我通过计算统一比例和平移球体上的法线发现了这一点。发生在该行

的问题,我改变了法线世界空间:

*norm = glm::normalize(transinv * glm::vec4(glm::normalize(p - sphere_center), 0)); 

我假定齐次坐标变换,因为它是零事先(旋转和尺度不影响后,也可以0它,因为它是0,所以也不能翻译)。然而,它不是0,因为矩阵是转置的,所以最下面一行填充了反向平移,导致齐次坐标不为零。

然后将4-矢量归一化并将结果分配给3-矢量。 3矢量的构造函数只是删除最后一个条目,所以正常情况不会被标准化。

下面是最终画面: enter image description here