-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrays.h
161 lines (133 loc) · 3.55 KB
/
rays.h
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
/*
* rays.h - Ray and sphere intersections
*
* Copyright (c) 2023, Dimitrios Alexopoulos All rights reserved.
*/
#include <stdbool.h>
#include <stddef.h>
#include "canvas.h"
#include "vectors.h"
// clang-format off
#define ray(x, y, z, xdir, ydir, zdir) (Ray){point(x, y, z), vector(xdir, ydir, zdir)}
#define MATERIAL (Material){{{1, 1, 1}}, 0.1, 0.9, 0.9, 200, false}
#define material(r, g, b, ambient, diffuse, specular, shininess) (Material){{{r, g, b}}, ambient, diffuse, specular, shininess}
#define light(x, y, z, r, g, b) (Light){point(x, y, z), {{r, g, b}}}
#define sphere(transform, material) (Shape){SPHERE, transform, mat4Inv(transform), material}
#define plane(transform, material) (Shape){PLANE, transform, mat4Inv(transform), material}
// clang-format on
typedef struct
{
Vec4 origin;
Vec4 direction;
} Ray;
typedef enum
{
SPHERE,
PLANE,
NO_HIT
} ShapeType;
typedef struct
{
Vec3 a;
Vec3 b;
Mat4 transform;
Mat4 transformInv;
} StripePattern;
// TODO: Check best way to pack struct
typedef struct
{
Vec3 color;
double ambient;
double diffuse;
double specular;
double shininess;
bool hasPattern;
StripePattern pattern;
} Material;
typedef struct
{
ShapeType type;
Mat4 transform;
Mat4 transformInv;
Material material;
} Shape;
typedef struct
{
Vec4 position;
Vec3 intensity;
} Light;
typedef struct
{
size_t lightCount;
size_t shapeCount;
Light *lights;
Shape *shapes;
} World;
typedef struct
{
Shape shape;
double t;
} Intersection;
typedef struct
{
size_t size;
size_t capacity;
Intersection *elem;
} Intersections;
typedef struct
{
Shape shape;
double t;
Vec4 point;
Vec4 overPoint;
Vec4 camera;
Vec4 normal;
bool inside;
} Computations;
typedef struct
{
size_t hsize;
size_t vsize;
double fov;
double pixelSize;
double halfWidth;
double halfHeight;
Mat4 transform;
Mat4 transformInv;
} Camera;
// typedef struct
// {
// Vec3 (*function)(Vec4, const void*);
// void *parameters;
// } Pattern;
// typedef enum
// {
// STRIPPED
// } PatternType;
void intersectionsCreate(Intersections *dest, size_t size);
void intersectionsCopy(Intersections *dest, const Intersections *src);
void intersectionsDestroy(Intersections *dest);
void intersectionsSort(Intersections *dest);
void intersectionsResize(Intersections *dest, size_t size);
void intersectionsPush(Intersections *dest, Intersection intersection);
Intersection intersectionPop(Intersections *dest);
Vec4 rayPos(Ray ray, double t);
Ray rayTransform(Ray ray, Mat4 mat);
Ray rayPixel(Camera camera, size_t x, size_t y);
Intersections intersect(Shape shape, Ray ray);
Intersection hit(Intersections intersections);
Vec4 normal(Shape shape, Vec4 point);
Vec3 lighting(Material material, Shape object, Light light, Vec4 point, Vec4 eye, Vec4 normal, bool inShadow);
void worldDestroy(World *world);
World defaultWorld(void);
Intersections intersectWorld(World world, Ray ray);
bool isShadowed(World world, size_t lightIndex, Vec4 point);
Computations prepareComputations(Intersection intersection, Ray ray);
Vec3 shadeHit(World world, Computations computations);
Vec3 colorAt(World world, Ray ray);
Camera cameraInit(size_t hsize, size_t vsize, double fov, Mat4 transform);
Canvas *render(Camera camera, World world);
// Vec3 defaultPattern(Vec4 point, const void *parameters);
StripePattern stripePattern(Vec3 colorA, Vec3 colorB, Mat4 transform);
Vec3 stripeAt(StripePattern pattern, Vec4 point);
Vec3 stripeAtObject(Shape shape, Vec4 point);