2017-03-04 84 views
0

我目前正在研究一个学校项目的射线跟踪器,并且对于本课程的这一步,我们需要使用灯光系统和Blinn-Phong算法实现对象着色。射线跟踪器阴影中的工件和错误代码

下面是最终的图像如果做得正确...

在这一点上我没有得到的反射和阴影还没有,只是试图让阴影第一工作,不断得到由此产生的图片

由于这是很明显的看到,那应该有镜面反射量最高的区域被转绿,我越来越奇怪BL在茶壶周围的一些地方出现文物。我一直在阅读和测试几个小时,无法弄清楚为什么!我90%确定我正确实施了算法。

这里是我的代码,有关此问题...

void shade(RGB& shading, std::vector<Light> lights, Triangle t, Ray& r, Vector3 loc) { 
    float intensity = 1/std::sqrt(lights.size()); 

    for (std::size_t i = 0; i < lights.size(); ++i) { 
    Vector3 n = t.get_normal(); 
    n.normalize(); 
    Vector3 v = r.dir * -1; 
    v.normalize(); 
    Vector3 l = lights[i].position - loc; 
    l.normalize(); 
    Vector3 h = v + l; 
    h.normalize(); 

    float diffuse = MAX(0, n.dot(l)); 
    float spec = std::pow(MAX(0, n.dot(h)), t.fill.shine); 

    shading.r += (t.fill.kd * diffuse * t.fill.rgb.r + t.fill.ks * spec) * intensity; 
    shading.g += (t.fill.kd * diffuse * t.fill.rgb.g + t.fill.ks * spec) * intensity; 
    shading.b += (t.fill.kd * diffuse * t.fill.rgb.b + t.fill.ks * spec) * intensity; 
    } 
} 

// Main function 
int main(int argc, char* argv[]) { 

    // Set input and output file names 
    std::string in_file = (argc > 1) ? argv[1] : "teapot-3.nff"; 
    std::string out_file = (argc > 2) ? argv[2] : "output.ppm"; 

    // Parse the NFF file and get the Viewpoint and Background data 
    NFFParser parser(in_file); 
    parser.read_file(); 

    Viewpoint view = parser.getViewpoint(); 
    Background background = parser.getBackground(); 

    // Camera creation 
    Camera camera(view); 

    // Allocate array of pixels and create camera instance 
    Pixel* pixels = new Pixel[camera.x_res * camera.y_res]; 

    // Collect object data to iterate over 
    std::vector<Polygon> polygons = parser.getPolygons(); 
    std::vector<Patch> patches = parser.getPatches(); 
    std::vector<Light> lights = parser.getLights(); 
    std::vector<Triangle> triangles; 

    // Convert all polygons and patches to trianlges and 
    // build a vector of triangles to iterator over 
    for (std::size_t i = 0; i < polygons.size(); ++i) { 
    std::vector<Triangle> v = polygons[i].fan_to_triangles(); 
    triangles.insert(triangles.end(), v.begin(), v.end()); 
    } 

    for (std::size_t i = 0; i < patches.size(); ++i) { 
    std::vector<Triangle> v = patches[i].fan_to_triangles(); 
    triangles.insert(triangles.end(), v.begin(), v.end()); 
    } 

    std::cout << "Testing for intersections among objects..." << std::endl 
      << "...this may take a moment..." << std::endl 
      << "==========================================" << std::endl; 


    // Iterator over all pixels in the array 
    for (int y = 0; y < camera.y_res; ++y) { 
    for (int x = 0; x < camera.x_res; ++x) { 

     float t = INF; 
     Triangle closest; 

     // Map pixel to image plane coordinates 
     Vector3 image_location = camera.map_to_image(x, y); 
     Ray r(camera.e, image_location - camera.e); 

     // Iteration over Polygons 
     for (std::vector<Triangle>::iterator it = triangles.begin(); it != triangles.end(); ++it) { 
     if (it->intersects(&r, t) && t < r.t_min) { 
      closest = *it; 
      r.t_min = t; 
     } 
     } 

     if (r.t_min == INF) { 
     set_pixel(pixels, y, x, camera.y_res, background.rgb); 
     continue; 
     } 

     RGB shading; 

     shade(shading, lights, closest, r, image_location); 
     set_pixel(pixels, y, x, camera.y_res, shading); 

    } 
    } 


    // Write the array of pixels to the output PPM file 
    PPMWriter writer(out_file); 
    writer.write_pixels(pixels, camera.x_res, camera.y_res); 

    // Deallocate pixels array to avoid memory leaks 
    delete [] pixels; 

    return 0; 
} 

任何指导或帮助将不胜感激!


EDIT

绿色着色被固定但RGB分量封盖,但黑色伪像仍然是一个问题。

+0

您需要粘贴'intersects'方法的定义,因为黑色像素可能是由错误的相交测试引起的。原因可能是茶壶另一面的面孔透过,他们的法线正对着相机,这意味着它们会被遮蔽成黑色。 –

回答

2

对于绿色位,在您的颜色计算中,您正溢出适合​​于像素红色分量的内容。如果查看靠近绿色(0xFE9E4E)处的铜色颜色的RGB与绿色区域(0x019D4F)中的颜色,可以看到这一点。

您需要在计算中包含一些溢出检查。

+0

如果溢出是积极的,我只是在它的组件1(255)? –

+0

这是通常的方法。如果发生的太多,可以降低强度使其变暗一点,使其溢出较少。 – 1201ProgramAlarm

+0

太棒了!元件加盖就像绿色变色的魅力一样工作。只需要弄清楚黑色的人造物 –