Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. void render(bitmap_image& image, const std::vector<Sphere>& scene, bool perspective) {
  2.     // TODO: implement ray tracer
  3.     Camera camera = Camera(Vector(0,5,-25),Vector(0,1,0), Vector(0,0,0), 600);
  4.  
  5.     //setting the bounds for the window
  6.     int l,r,b,t;
  7.     l=-5;
  8.     r=5;
  9.     t=5;
  10.     b=-5;
  11.     float *t1;
  12.     float *t2;
  13.  
  14.  
  15.     for(int y=0;y<image.height();y++) {
  16.         for (int x = 0; x < image.width(); x++) {
  17.             rgb_t color = make_colour(75, 156, 211);
  18.             image.set_pixel(x, y, color);
  19.         }
  20.     }
  21.  
  22.     std::size_t max = image.width() * image.height();
  23.     std::size_t cores = std::thread::hardware_concurrency();
  24.     volatile std::atomic<std::size_t> count(0);
  25.     std::vector<std::future<void>> future_vector;
  26.  
  27.     while (cores--)
  28.         future_vector.emplace_back(
  29.             std::async([=, &image, &count]()
  30.     {
  31.         while (true)
  32.         {
  33.             std::size_t index = count++;
  34.             if (index >= max)
  35.                 break;
  36.             int x = index % image.width();
  37.             int y = index / image.width();
  38.                
  39.  
  40.                         for(int i = scene.size()-1; i>=0; i--){
  41.                             float ui = l + (r-l)*(x+.5) / image.width();
  42.                             float vj = b + (t-b)*(y+.5) / image.height();
  43. //                            Vector camera = Vector(0,0,-10);
  44.                             Vector direction = Vector(ui,vj,-10);
  45.                             Sphere sphere = scene.at(i);
  46.                             Ray newRay = Ray(camera.position, (direction-camera.position),1);
  47.                             Intersection intersect = sphere.intersect(newRay);
  48.                             RayTracer raytracer;
  49.  
  50.                             bool hit = intersect.didIntersect;
  51.                             if (hit) {
  52. //                                std::cout << "hit" << std::endl;
  53.                                 raytracer.performLighting(intersect, sphere.color);
  54.                                 rgb_t color = make_colour(raytracer.performLighting(intersect, intersect.color).r * 255,
  55.                                                           raytracer.performLighting(intersect, intersect.color).g * 255,
  56.                                                           raytracer.performLighting(intersect, intersect.color).b * 255);
  57.                                 image.set_pixel(x, image.height()-y, color);
  58.                             }
  59.                         }
  60.                     }
  61.                 }));
  62.  
  63.  
  64.         image.save_image("../img/new.bmp");
  65.  
  66.  
  67.  
  68.  
  69.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement