aboutsummaryrefslogtreecommitdiff
path: root/src/aliasing/supersample.cpp
blob: aa8e9d3b1de3b88183130969c4872a917a8f9385 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "raytracer/raytracer.h"

/**
 * Extra credit -> Super Sampling
 */

const float SUPERSAMPLE_DISTANCE_FROM_CENTER = .25f; // note: max of .5f, unless overlapping with other pixels
bool SUPER_SAMPLE = false;
bool ADAPTIVE_SUPER_SAMPLING = false;

RGBA RayTracer::superSample(
        glm::vec4 eyeCamera,
        glm::vec4 pixelDirCamera,
        const RayTraceScene &scene) {
    // get the color value at value between center and four corners
    float x_delta = SUPERSAMPLE_DISTANCE_FROM_CENTER / (scene.width());
    float y_delta = SUPERSAMPLE_DISTANCE_FROM_CENTER / (scene.height());
    // TL == TOP LEFT
    // BR = BOTTOM RIGHT, not Battle Royale :)
    glm::vec4 pixelTL = getPixelFromRay(
            eyeCamera,
            glm::vec4(pixelDirCamera.x - x_delta, pixelDirCamera.y - y_delta, pixelDirCamera.z, 0.f),
            scene);
    glm::vec4 pixelTR = getPixelFromRay(
            eyeCamera,
            glm::vec4(pixelDirCamera.x + x_delta, pixelDirCamera.y - y_delta, pixelDirCamera.z, 0.f),
            scene);
    glm::vec4 pixelBL = getPixelFromRay(
            eyeCamera,
            glm::vec4(pixelDirCamera.x - x_delta, pixelDirCamera.y + y_delta, pixelDirCamera.z, 0.f),
            scene);
    glm::vec4 pixelBR = getPixelFromRay(
            eyeCamera,
            glm::vec4(pixelDirCamera.x + x_delta, pixelDirCamera.y + y_delta, pixelDirCamera.z, 0.f),
            scene);

    if (!ADAPTIVE_SUPER_SAMPLING) {
        return toRGBA((pixelTL + pixelTR + pixelBL + pixelBR) / 4.f);
    }

    // ADAPTIVE SUPER SAMPLING
    // make the region from the center of pixel smaller until we hit something
    RGBA nohit = {0, 0, 0, 0}; // just here to say that a is 0 if no hit...
    float TRAVERSE_DISTANCE = .025f;
    float num_pixels = 4.f;
    if (pixelTL.a == 0) {
        num_pixels--;
        float smallerDist = SUPERSAMPLE_DISTANCE_FROM_CENTER - TRAVERSE_DISTANCE;
        while (smallerDist < TRAVERSE_DISTANCE) {
            float x_delta = smallerDist / (scene.width());
            float y_delta = smallerDist / (scene.height());
            pixelTL = getPixelFromRay(
                    eyeCamera,
                    glm::vec4(pixelDirCamera.x - x_delta, pixelDirCamera.y - y_delta, pixelDirCamera.z, 0.f),
                    scene);
            if (pixelTL.a != 0) {
                num_pixels++;
                break;
            }
            smallerDist -= TRAVERSE_DISTANCE;
        }
    }
    if (pixelTR.a == 0) {
        num_pixels--;
        float smallerDist = SUPERSAMPLE_DISTANCE_FROM_CENTER - TRAVERSE_DISTANCE;
        while (smallerDist < TRAVERSE_DISTANCE) {
            float x_delta = smallerDist / (scene.width());
            float y_delta = smallerDist / (scene.height());
            pixelTR = getPixelFromRay(
                    eyeCamera,
                    glm::vec4(pixelDirCamera.x - x_delta, pixelDirCamera.y - y_delta, pixelDirCamera.z, 0.f),
                    scene);
            if (pixelTR.a != 0) {
                num_pixels += 1;
                break;
            }
            smallerDist -= TRAVERSE_DISTANCE;
        }
    }
    if (pixelBL.a == 0) {
        num_pixels--;
        float smallerDist = SUPERSAMPLE_DISTANCE_FROM_CENTER - TRAVERSE_DISTANCE;
        while (smallerDist < TRAVERSE_DISTANCE) {
            float x_delta = smallerDist / (scene.width());
            float y_delta = smallerDist / (scene.height());
            pixelBL = getPixelFromRay(
                    eyeCamera,
                    glm::vec4(pixelDirCamera.x - x_delta, pixelDirCamera.y - y_delta, pixelDirCamera.z, 0.f),
                    scene);
            if (pixelBL.a != 0) {
                num_pixels += 1;
                break;
            }
            smallerDist -= TRAVERSE_DISTANCE;
        }
    }
    if (pixelBR.a == 0) {
        num_pixels--;
        float smallerDist = SUPERSAMPLE_DISTANCE_FROM_CENTER - TRAVERSE_DISTANCE;
        while (smallerDist < TRAVERSE_DISTANCE) {
            float x_delta = smallerDist / (scene.width());
            float y_delta = smallerDist / (scene.height());
            pixelBR = getPixelFromRay(
                    eyeCamera,
                    glm::vec4(pixelDirCamera.x - x_delta, pixelDirCamera.y - y_delta, pixelDirCamera.z, 0.f),
                    scene);
            if (pixelBR.a != 0) {
                num_pixels += 1;
                break;
            }
            smallerDist -= TRAVERSE_DISTANCE;
        }
    }

    if (num_pixels == 0.f) {
        return nohit;
    }
    return toRGBA((pixelTL + pixelTR + pixelBL + pixelBR) / num_pixels);
}