2017-07-26 78 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