aboutsummaryrefslogtreecommitdiff
path: root/src/illuminate/illuminate.cpp
blob: a093fd141922942052ddaf02aaa8463e7d8d1cb5 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "raytracer/raytracer.h"

glm::vec4  RayTracer::illuminationFromPointLight(
        const SceneLightData &light,
        glm::vec4 intersectionWorld,
        glm::vec4 normalWorld,
        glm::vec4  directionToCamera,
        const RenderShapeData &shape,
        const RayTraceScene &scene
        )
{
    auto directionFromIntersectionToLight = light.pos - intersectionWorld;
    directionFromIntersectionToLight = glm::normalize(directionFromIntersectionToLight);

    // check if this light is blocked by an object
    auto distanceToLight = glm::distance(light.pos, intersectionWorld);
    bool isShadow = RayTracer::isShadowed(
            light.pos,
            distanceToLight,
            glm::vec4(directionFromIntersectionToLight),
            glm::vec4(intersectionWorld),
            scene);
    if (isShadow)
    {
        // if this is a shadow, then no light contribution
        return glm::vec4(0.f);
    }

    // calculate attenuation
    float c1 = light.function.x;
    float c2 = light.function.y;
    float c3 = light.function.z;
    float attenuation = std::min(1.f, 1.f / (c1 + distanceToLight * c2 + (distanceToLight * distanceToLight) * c3));

    return phong(
            light.color,
            attenuation,
            directionFromIntersectionToLight,
            directionToCamera,
            intersectionWorld,
            normalWorld,
            shape,
            scene);
}

glm::vec4  RayTracer::illuminationFromSpotLight(
        const SceneLightData &light,
        glm::vec4 intersectionWorld,
        glm::vec4 normalWorld,
        glm::vec4 directionToCamera,
        const RenderShapeData &shape,
        const RayTraceScene &scene
)
{
    auto distance = glm::distance(light.pos, intersectionWorld);

    // calculate the angle from the shape to the spot light
    auto directionFromIntersectionToLight = glm::normalize(light.pos - intersectionWorld);

    // calculate intensity, based on angle. apply falloff if necessary
    auto lightDirection = glm::normalize(light.dir);
    // invert the direction of the intersection to light for dot product to work correctly
    auto cosTheta = glm::dot(-directionFromIntersectionToLight, lightDirection);
    auto theta = glm::acos(cosTheta);

    // determine intensity, based on location on spot cone
    glm::vec4 intensity;
    float inner = light.angle - light.penumbra;
    if (theta <= inner)
    {
        intensity = light.color;
    }
    else if
    (
        theta > inner
        && theta <= light.angle
    )
    {
        // inside the penumbra, need to apply falloff
        float falloff = -2 * std::pow(theta - inner, 3) / std::pow(light.penumbra, 3) +
                        3 * std::pow(theta - inner, 2) / std::pow(light.penumbra, 2);
        intensity = light.color * (1 - falloff);
    }
    else // theta > light.angle
    {
        return glm::vec4(0.f);
    }

    // if the light is within the cone, see if it's a shadow
    auto distanceToLight = glm::distance(light.pos, intersectionWorld);
    bool isShadow = RayTracer::isShadowed(
            light.pos,
            distanceToLight,
            glm::vec4(directionFromIntersectionToLight),
            glm::vec4(intersectionWorld),
            scene);
    if (isShadow)
    {
        // if this is a shadow, then no light contribution
        return glm::vec4(0.f);
    }

    // calculate attenuation
    float c1 = light.function.x;
    float c2 = light.function.y;
    float c3 = light.function.z;
    float attenuation = std::min(1.f, 1.f / (c1 + distance * c2 + (distance * distance) * c3));

    return phong(
            intensity,
            attenuation,
            directionFromIntersectionToLight,
            directionToCamera,
            intersectionWorld,
            normalWorld,
            shape,
            scene);
}

glm::vec4  RayTracer::illuminationFromDirectionalLight(
        const SceneLightData &light,
        glm::vec4 intersectionWorld,
        glm::vec4 normalWorld,
        glm::vec4 directionToCamera,
        const RenderShapeData &shape,
        const RayTraceScene &scene
)
{
    // define direction and distance of directional light
    auto directionFromIntersectionToLight = - light.dir;
    directionFromIntersectionToLight = glm::normalize(directionFromIntersectionToLight);
    float distanceToLight = FINF;   // directional light infinitely far away

    // check if an object blocks ours
    bool isShadow = RayTracer::isShadowed(
            light.pos,
            distanceToLight,
            directionFromIntersectionToLight,
            glm::vec4(intersectionWorld),
            scene);
    if (isShadow)
    {
        // if this is a shadow, then no light contribution
        return glm::vec4(0.f);
    }

    float attenuation = 1.f; // directional lights don't attenuate
    return phong(
            light.color,
            attenuation,
            directionFromIntersectionToLight,
            directionToCamera,
            intersectionWorld,
            normalWorld,
            shape,
            scene);
}



// Calculates the RGBA of a pixel from intersection infomation and globally-defined coefficients
glm::vec4 RayTracer::illuminatePixel(
        glm::vec4  intersectionWorld,
        glm::vec4  normalWorld,
        glm::vec4  directionToCamera,
        const RenderShapeData& shape,
        const RayTraceScene &scene,
        int depth)
{
    // Normalizing directions
    normalWorld           = glm::normalize(normalWorld);
    directionToCamera = glm::normalize(directionToCamera);

    // to be summed then returned
    glm::vec4 illumination(0, 0, 0, 1.f);

    // add the ambient term
    float ka = scene.getGlobalData().ka;
    illumination += ka*shape.primitive.material.cAmbient;

    for (const SceneLightData &light : scene.getLights()) {
        switch (light.type) {
            case LightType::LIGHT_POINT:
                illumination +=
                        illuminationFromPointLight(light, intersectionWorld, normalWorld, directionToCamera, shape, scene);
                continue;
            case LightType::LIGHT_DIRECTIONAL:
                illumination +=
                        illuminationFromDirectionalLight(light, intersectionWorld, normalWorld, directionToCamera, shape, scene);
                continue;
            case LightType::LIGHT_SPOT:
                illumination +=
                        illuminationFromSpotLight(light, intersectionWorld, normalWorld, directionToCamera, shape, scene);
                continue;
            case LightType::LIGHT_AREA:
                illumination +=
                        illuminationFromAreaLight(light, intersectionWorld, normalWorld, directionToCamera, shape, scene);
                continue;
            default:
                continue;
        }
    }

    auto incidentDir = -directionToCamera;
    // recursive raytracing for the reflection and refraction (see reflect.cpp)
    illumination += refract(intersectionWorld, normalWorld, incidentDir, shape, scene, depth + 1);
    illumination += reflect(intersectionWorld, normalWorld, incidentDir, shape, scene, depth + 1);

    return illumination;
}

// helper function to handle the diffuse and specular terms
// also handles the texture within that diffuse term
glm::vec4 RayTracer::phong(
        glm::vec4 lightColor,
        float attenuation,
        glm::vec4 directionFromIntersectionToLight,
        glm::vec4 directionToCamera,
        glm::vec4 intersectionWorld,
        glm::vec4 normalWorld,
        const RenderShapeData &shape,
        const RayTraceScene &scene)
{
    float kd = scene.getGlobalData().kd;
    float ks = scene.getGlobalData().ks;
    auto material = shape.primitive.material;

    glm::vec4 illumination(0.f);

    // calculate diffuse term
    auto dotDiffuse = glm::dot(normalWorld, directionFromIntersectionToLight);
    if (dotDiffuse > 0) // ensure not facing away
    {
        auto diffuse = (kd * material.cDiffuse);
        // commenting out texture stuff bc 4d textures??????
//        if (material.textureMap.isUsed)
//        {
//            glm::vec4 pObject = shape.inverseCTM * glm::vec4(intersectionWorld, 1.f);
//            diffuse = interpolateTexture(pObject, shape, diffuse);
//        }
        illumination += (attenuation * lightColor) * dotDiffuse * diffuse;
    }

    // add specular term
    auto reflectedDirOverNormal =
            2 * glm::dot(directionFromIntersectionToLight, normalWorld) * normalWorld -
            directionFromIntersectionToLight;
    auto dotSpecular = glm::dot(reflectedDirOverNormal, directionToCamera);
    auto toPow = std::pow(dotSpecular, material.shininess);
    if (dotSpecular > 0) {
        illumination += (attenuation * lightColor) * toPow * (ks * material.cSpecular);
    }

    return illumination;
}

// EXTRA CREDIT -> AREA LIGHT
glm::vec4  RayTracer::illuminationFromAreaLight(
        const SceneLightData &light,
        glm::vec4 intersectionWorld,
        glm::vec4 normalWorld,
        glm::vec4  directionToCamera,
        const RenderShapeData &shape,
        const RayTraceScene &scene
) {
    // select a random point within the light's height and width
    float width = light.width;
    float height = light.height;
    float x = ((float) rand() / (float) RAND_MAX) * width - width / 2.f;
    float y = ((float) rand() / (float) RAND_MAX) * height - height / 2.f;
    glm::vec4 lightPosition = light.pos + glm::vec4(x, y, 0.f, 0.f);

    auto directionFromIntersectionToLight = lightPosition - intersectionWorld;
    directionFromIntersectionToLight = glm::normalize(directionFromIntersectionToLight);

    // check if this light is blocked by an object
    auto distanceToLight = glm::distance(lightPosition, intersectionWorld);
    bool isShadow = RayTracer::isShadowed(
            lightPosition,
            distanceToLight,
            glm::vec4(directionFromIntersectionToLight),
            glm::vec4(intersectionWorld),
            scene);
    if (isShadow)
    {
        // if this is a shadow, then show a ray to a random point in the light
        return glm::vec4(0.f);
    }

    // calculate attenuation
    float c1 = light.function.x;
    float c2 = light.function.y;
    float c3 = light.function.z;
    float attenuation = std::min(1.f, 1.f / (c1 + distanceToLight * c2 + (distanceToLight * distanceToLight) * c3));

    return phong(
            light.color,
            attenuation,
            directionFromIntersectionToLight,
            directionToCamera,
            intersectionWorld,
            normalWorld,
            shape,
            scene);
}