-
Notifications
You must be signed in to change notification settings - Fork 1
/
lighting.c
54 lines (50 loc) · 1.93 KB
/
lighting.c
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
/*
* lighting.c - Draws a shaded sphere on the canvas and outputs it in PPM format on stdout
*
* Copyright (c) 2023, Dimitrios Alexopoulos All rights reserved.
*/
#include <stdio.h>
#include <stdlib.h>
#include "src/canvas.h"
#include "src/rays.h"
int main(void)
{
Canvas *canvas = canvasCreate(2048, 2048);
Shape sphere = sphere(mat4Mul(translation(2.5, 2.5, 9), scaling(2, 2, 2)), MATERIAL);
sphere.material.color = color(0, 0, 1);
const Light light = light(0, 5, 4, 1, 1, 1);
const Vec4 cameraOrigin = point(2.5, 2.5, 0);
const double canvasX = 0;
const double canvasY = 5;
const double canvasZ = 10;
const double canvasSize = 5;
Vec4 canvasPoint = point(canvasX, canvasY, canvasZ);
Ray cameraRay = {.origin = cameraOrigin};
for (size_t i = 0; i < canvasWidth(canvas); i++)
{
for (size_t j = 0; j < canvasHeight(canvas); j++)
{
canvasPoint.x = canvasX + ((double)i / canvasWidth(canvas)) * canvasSize;
canvasPoint.y = canvasY - ((double)j / canvasHeight(canvas)) * canvasSize;
cameraRay.direction = vec4Sub(canvasPoint, cameraOrigin);
Intersections cameraIntersections = intersect(sphere, cameraRay);
Intersection cameraHit = hit(cameraIntersections);
if (cameraHit.shape.type == SPHERE)
{
Vec4 hitPoint = rayPos(cameraRay, cameraHit.t);
Vec4 hitNormal = normal(sphere, hitPoint);
Vec3 pointColor = lighting(sphere.material, sphere, light, hitPoint,
vec4Neg(vec4Norm(cameraRay.direction)), hitNormal, false);
canvasPixelWrite(canvas, i, j, pointColor);
}
intersectionsDestroy(&cameraIntersections);
}
}
char *ppmOutput = canvasPPM(canvas);
fputs(ppmOutput, stdout);
free(canvas);
canvas = NULL;
free(ppmOutput);
ppmOutput = NULL;
return 0;
}