Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void render(bitmap_image& image, const std::vector<Sphere>& scene, bool perspective) {
- // TODO: implement ray tracer
- Camera camera = Camera(Vector(0,5,-25),Vector(0,1,0), Vector(0,0,0), 600);
- //setting the bounds for the window
- int l,r,b,t;
- l=-5;
- r=5;
- t=5;
- b=-5;
- float *t1;
- float *t2;
- for(int y=0;y<image.height();y++) {
- for (int x = 0; x < image.width(); x++) {
- rgb_t color = make_colour(75, 156, 211);
- image.set_pixel(x, y, color);
- }
- }
- std::size_t max = image.width() * image.height();
- std::size_t cores = std::thread::hardware_concurrency();
- volatile std::atomic<std::size_t> count(0);
- std::vector<std::future<void>> future_vector;
- while (cores--)
- future_vector.emplace_back(
- std::async([=, &image, &count]()
- {
- while (true)
- {
- std::size_t index = count++;
- if (index >= max)
- break;
- int x = index % image.width();
- int y = index / image.width();
- for(int i = scene.size()-1; i>=0; i--){
- float ui = l + (r-l)*(x+.5) / image.width();
- float vj = b + (t-b)*(y+.5) / image.height();
- // Vector camera = Vector(0,0,-10);
- Vector direction = Vector(ui,vj,-10);
- Sphere sphere = scene.at(i);
- Ray newRay = Ray(camera.position, (direction-camera.position),1);
- Intersection intersect = sphere.intersect(newRay);
- RayTracer raytracer;
- bool hit = intersect.didIntersect;
- if (hit) {
- // std::cout << "hit" << std::endl;
- raytracer.performLighting(intersect, sphere.color);
- rgb_t color = make_colour(raytracer.performLighting(intersect, intersect.color).r * 255,
- raytracer.performLighting(intersect, intersect.color).g * 255,
- raytracer.performLighting(intersect, intersect.color).b * 255);
- image.set_pixel(x, image.height()-y, color);
- }
- }
- }
- }));
- image.save_image("../img/new.bmp");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement