2017-02-24 76 views
0

[編輯]正如編程中的典型情況,我在發佈後不久就想出了它!看到我的答案,如果你有興趣:)光線追蹤:來自多個燈光的陰影

我正在使用C++光線跟蹤器,並希望得到一些幫助。我的場景中有兩盞燈,一盞點燈和一盞定向燈,還有一堆球體(以及一架飛機作爲「地板」)。

如果我運行任一光線存在的光線跟蹤器(但其他光線不存在),它會使陰影如預期般(見下圖)。

Directional light shadows image

Point light shadows image

麻煩的是,當我運行光線追蹤與兩個燈目前,只有點光源陰影出現,我可以告訴光是「上」,因爲場面亮:

參見下面用於檢測陰影我的代碼:

bool Scene::shadowtrace(Ray &ray, double t) 
{ 
    Object *obj = obj_list; 
    Light *lt = light_list; 
    Vector v1, v2; // hit -> light vector 
    Hit hit; 

    Vertex intersect = (ray.position(t)); 
    intersect.plus(ray.D, -0.001); // offset intersection ever so slightly away from object, to avoid self-shadowing 
    v1.set(0.0, 0.0, 0.0); 
    v2.set(0.0, 0.0, 0.0); // initialise 

    while (lt != (Light *)0) 
    { 
     Ray shadowRay; 
     shadowRay.P = (intersect); 

     Vertex lightPos = Vertex(0.0, 0.0, 0.0, 0.0); 
     lt->getPosition(lightPos); // sets value of lightPos 

     if (lightPos.x > T_LIMIT) // If set absurdly high, we're dealing with a directional light 
     { 
      lt->getDirection(v1); // sets v1 to light direction (reversed) 
      v1.normalise(); 
      shadowRay.D = v1; // set hit-to-light vector as shadowray direction 

      while (obj != (Object *)0) 
      { 
       if (obj->intersect(shadowRay, &hit) == true) 
       { 
        if (!((hit.t * hit.t) < 0.001)) // Self-shadow if very very small t number 
        { 
         return true; // ray hits an onject, and the object occurs before the light 
        } 
       } 
       obj = obj->next(); 
      } 
     } 
     else // otherwise, it's a point light :) 
     { 
      v1 = (lightPos.minus(intersect)); // find vector from intersection to light 
      v2 = v1; // keep un-normalised version for preventing mis-shadowing from objects behind the light source 
      v1.normalise(); 
      shadowRay.D = v1; // set ray direction to hit-to-light vector 

      while (obj != (Object *)0) 
      { 
       if (obj->intersect(shadowRay, &hit) == true) 
       { 
        if (!((hit.t * hit.t) > (v2.lengthSq()))) // Check hit.t against magnitude of (un-normalised) intersection-to-light vector 
         if (!((hit.t * hit.t) < 0.001)) // Self-shadow if very very small t number 
         { // Used hit.t^2 to avoid having to SQRT the length. Is acceptable for comparisons 
          return true; // ray hits an onject, and the object occurs before the light 
         } 

       } 

       obj = obj->next(); 
      } 
     } 

     lt = lt->next(); 
    } 

    return false; 
} 

如果檢測到陰影,只有AMB光源歸因於該點,否則環境+漫射歸因於(我還沒有圍繞添加鏡面反射)。

任何建議將是偉大的!

+0

(鏈路用於與兩個燈的輸出[HTTPS:// PUU。 sh/ui6Ka/e81edc5f6e.jpg] - 因爲SO只會讓我在問題中發佈2個鏈接) – NOSHEDMANTIS

回答

0

虛驚一場! 我想通了!

每個對象列表循環之前我加入:

obj = obj_list; 

復位到第一個目的,這解決了這個問題:)