2016-03-01 98 views
1

我在光線追蹤剛剛實施的思考,這裏是處理反射的代碼,但是我有我的所有代碼上傳到github repository爲更好的閱讀:射線追蹤反射顆粒感

Color finalColor = closestObjectMaterial.GetColor() * AMBIENTLIGHT; // Add ambient light to the calculation 

// Reflections 
if(closestObjectMaterial.GetSpecular() > 0 && closestObjectMaterial.GetSpecular() <= 1) 
{ 
    Vector scalar = closestObjectNormal * (closestObjectNormal.Dot(intersectingRayDir.Negative())); 
    Vector resultantReflection = intersectingRayDir.Negative() + ((scalar + (intersectingRayDir)) * 2); 
    Vector reflectionDirection = resultantReflection.Normalize(); 

    Ray reflectionRay(intersectionRayPos, resultantReflection); 

    // determine what the ray intersects with first 
    std::vector<FPType> reflectionIntersections; 
    for(auto sceneObject : sceneObjects) 
    { 
     reflectionIntersections.push_back(sceneObject->GetIntersection(reflectionRay)); 
    } 

    int closestObjectWithReflection = ClosestObjectIndex(reflectionIntersections); 

    if(closestObjectWithReflection != -1) 
    { 
     // reflection ray missed everthing else 
     if(reflectionIntersections[closestObjectWithReflection] > TOLERANCE) 
     { 
      // determine the position and direction at the point of intersection with the reflection ray 
      // the ray only affects the color if it reflected off something 
      Vector reflectionIntersectionPosition = intersectionRayPos + (resultantReflection * (reflectionIntersections[closestObjectWithReflection])); 
      Vector reflectionIntersectionRayDirection = resultantReflection; 
      Color reflectionIntersectionColor = GetColorAt(reflectionIntersectionPosition, reflectionIntersectionRayDirection, sceneObjects, closestObjectWithReflection, lightSources); 
      finalColor += (reflectionIntersectionColor * closestObjectMaterial.GetReflection()); 
     } 
    } 
} 

我越來越所有這些反射顆粒感文物(這是一個16K的分辨率渲染放大):

enter image description here

但是它更明顯較低分辨率1920×1080像:

enter image description here

enter image description here

回答

1

我認爲問題是,反射光線照射本身。我沒有重新編譯代碼來確認這一點。您可以嘗試在反射光線的起始位置添加一些偏移量。

Vector offset = resultantReflection * 0.001; 
Ray reflectionRay(intersectionRayPos + offset, resultantReflection); 
+0

我結束了使用這個,結果反射需要被歸一化。 'Vector offset = reflectionDirection * 0.001; Ray reflectionRay(intersectionRayPos + offset,resultingReflection);' –

+1

在你的評論中,你已經添加了 //添加了偏移來消除反射中的粒度效應 實際的問題是,你的光線從反射對象A因爲它是距離射線開始位置最近的物體,所以被認爲會自己擊中。所以爲了避免這種情況,我們將光線從物體A的表面稍微開始。 – codetiger