summaryrefslogtreecommitdiff
path: root/aa.h
blob: d226fda91bafa07c7e802b3e5eb698dc8db86b45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// Created by Keuin on 2022/4/13.
//

#ifndef RT_AA_H
#define RT_AA_H

#include "vec.h"
#include "viewport.h"
#include <vector>
#include <thread>


// Antialiasing viewport
template<typename T>
class aa_viewport : public viewport<T> {
    unsigned samples;
    std::vector<basic_viewport<T>> subviews;

public:
    aa_viewport(double width, double height, vec3d viewport_center, unsigned samples) : samples(samples) {
        assert(samples >= 1);
        subviews = std::vector<basic_viewport<T>>{samples, {width, height, viewport_center}};
    }

    virtual bitmap<T> render(const hitlist<T> &world, vec3d viewpoint, uint16_t image_width, uint16_t image_height) {
//        // TODO threaded rendering
//        std::vector<std::thread> workers{samples};
//        for (size_t i = 0; i < samples; ++i) {
//            const auto &th = workers[i];
//            const auto &vw = subviews[i];
//        }
        const auto seed = 123456789012345678ULL;
        std::mt19937_64 seedgen{seed}; // generates seeds for workers
        std::vector<bitmap<T>> images{};
        for (const auto &view: subviews) {
            bias_ctx bc{seedgen()};
            images.push_back(view.render(world, viewpoint, image_width, image_height, bc));
        }
        return bitmap<T>::average(images);
    }

};

using aa_viewport8b = aa_viewport<uint8_t>;


#endif //RT_AA_H