2014-03-05 72 views
1

我現在正在研究射線追蹤,反射部分。我有一切正常工作,包括創建一個帶陰影的球體。現在,我正在實施反思部分。但是,我無法得到它。我的算法是如下:射線追蹤 - 反射

traceRay(Ray ray, int counter){ 
// look through the intersection between ray and list of objects 
// find the final index aka the winning index, (if final index == -1, return background color) 
// then calculate the intersection point 

// perform reflection calculation here 
if(counter > 1 && winning object's reflectivity > 1){ 
    //get the intersection normal, vector N 
    //Calculate the reflection ray, R 
    // let I is the inverse of direction of incoming ray 
    //Calculate R = 2aN - I (a = N dotProduct I) 

    // the reflection ray is origin at the point of intersection between incoming ray and sphere with the R direction 
    Ray reflecRay (intersection_poisition, R); 

    Color reflection = traceRay(reflecRay, counter + 1); 
    // multiply by fraction ks 
    reflection = reflection * ks; 
} 


// the color of the sphere calculated using phong formula in shadeRay function 
Color prefinal = shadeRay(); 


// return the total color of prefinal + reflection 

} 

我試圖得到反映,但無法得到它,任何人都可以請讓我知道如果我的traceRay函數算法是正確的?

+0

您已經列出了您的代碼應該執行的操作,但我們不知道它的實際功能。 –

+1

你怎麼計算你的反射向量?另外,請確保將它從最初碰撞的物體上偏移,以防止自相交(通常沿反射矢量或表面法線偏移)。 – Necrolis

+0

打印出您的原始光線數據和intermediste計算結果可能會有所幫助。 –

回答

3

當反射光線時,需要沿着反射器的法線移動它以避免與反射器本身相交。例如:

const double ERR = 1e-12; 
Ray reflecRay (intersection_poisition + normal*ERR, R);