2010-04-02 22 views
1

我希望使用Phong照明模式渲染包含一個箱子和點光源的場景。以下是我的計算相關的代碼片斷:使用Phong模型的點光源照明

R3Rgb Phong(R3Scene *scene, R3Ray *ray, R3Intersection *intersection) 
{ 
    R3Rgb radiance; 
    if(intersection->hit == 0) 
    { 
    radiance = scene->background; 
    return radiance; 
    } 

    ... 
    // obtain ambient term 
    ... // this is zero for my test 

    // obtain emissive term 
    ... // this is also zero for my test 

    // for each light in the scene, obtain calculate the diffuse and specular terms 
    R3Rgb intensity_diffuse(0,0,0,1); 
    R3Rgb intensity_specular(0,0,0,1); 
    for(unsigned int i = 0; i < scene->lights.size(); i++) 
    { 
    R3Light *light = scene->Light(i); 
    R3Rgb light_color = LightIntensity(scene->Light(i), intersection->position); 
    R3Vector light_vector = -LightDirection(scene->Light(i), intersection->position); 

    // check if the light is "behind" the surface normal 
    if(normal.Dot(light_vector)<=0) 
     continue; 

    // calculate diffuse reflection 
    if(!Kd.IsBlack()) 
     intensity_diffuse += Kd*normal.Dot(light_vector)*light_color; 

    if(Ks.IsBlack()) 
     continue; 

    // calculate specular reflection 
    ... // this I believe to be irrelevant for the particular test I'm doing 

    } 

    radiance = intensity_diffuse; 
    return radiance; 
} 

R3Rgb LightIntensity(R3Light *light, R3Point position) 
{ 
    R3Rgb light_intensity; 
    double distance; 
    double denominator; 
    if(light->type != R3_DIRECTIONAL_LIGHT) 
    { 
    distance = (position-light->position).Length(); 
    denominator = light->constant_attenuation + 
         (light->linear_attenuation*distance) + 
         (light->quadratic_attenuation*distance*distance); 
    } 

    switch(light->type) 
    { 
    ... 

    case R3_POINT_LIGHT: 
     light_intensity = light->color/denominator; 
     break; 

    ... 
    } 
    return light_intensity; 
} 

R3Vector LightDirection(R3Light *light, R3Point position) 
{ 
    R3Vector light_direction; 
    switch(light->type) 
    { 
    ... 
    case R3_POINT_LIGHT: 
     light_direction = position - light->position; 
     break; 
    ... 
    } 
    light_direction.Normalize(); 
    return light_direction; 
} 

相信誤差必須在某處或者LightDirection(...)LightIntensity(...)功能,因爲當我運行使用定向光源,我獲得所需的渲染圖像我的代碼(因此這導致我相信Phong照明方程是正確的)。此外,在Phong(...)中,當我計算intensity_diffuse並在調試時,我將light_color除以10,我得到的結果圖像看起來更像我所需要的。我是否正確計算了light_color

謝謝。

+0

什麼是錯誤? – 2010-04-02 00:57:32

+0

生成的渲染圖像比預期的要輕。因此,通過「錯誤」我的意思是,也許我在計算給定點位置的光強度時犯了錯誤 – Myx 2010-04-02 00:59:29

+0

我剛剛通過添加'light_intensity = light_intensity /(1. + distance + distance * distance)'來測試實現;'' light_intensity = light-> color/denominator;'LightIntensity(...)'中,它似乎給了我想要的。這是正常的嗎?編輯:這並不能給我我想要的東西,但似乎需要進行某種劃分.... – Myx 2010-04-02 01:12:06

回答

0

原來我沒有錯。我比較結果的「最終圖像」未正確計算。