-
Notifications
You must be signed in to change notification settings - Fork 1
/
vectors.h
195 lines (159 loc) · 4.31 KB
/
vectors.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
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
/*
* vectors.h - Vector, matrix and transformation implementation
*
* Copyright (c) 2023, Dimitrios Alexopoulos All rights reserved.
*/
#ifndef VECTORS_H
#define VECTORS_H
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#define MAT_EPSILON 0.00001
#define PPM_DEPTH 255
// clang-format off
#define point(x, y, z) (Vec4){{x, y, z, 1}}
#define vector(x, y, z) (Vec4){{x, y, z, 0}}
#define color(red, green, blue) (Vec3){{red, green, blue}}
#define IDENTITY (Mat4){{{1, 0, 0, 0},\
{0, 1, 0, 0},\
{0, 0, 1, 0},\
{0, 0, 0, 1}}}
#define translation(x, y, z) (Mat4){{{1, 0, 0, x},\
{0, 1, 0, y},\
{0, 0, 1, z},\
{0, 0, 0, 1}}}
#define scaling(x, y, z) (Mat4){{{x, 0, 0, 0},\
{0, y, 0, 0},\
{0, 0, z, 0},\
{0, 0, 0, 1}}}
#define rotationX(r) (Mat4){{{1, 0, 0, 0},\
{0, cos(r), -sin(r), 0},\
{0, sin(r), cos(r), 0},\
{0, 0, 0, 1}}}
#define rotationY(r) (Mat4){{{ cos(r), 0, sin(r), 0},\
{ 0, 1, 0, 0},\
{-sin(r), 0, cos(r), 0},\
{ 0, 0, 0, 1}}}
#define rotationZ(r) (Mat4){{{cos(r), -sin(r), 0, 0},\
{sin(r), cos(r), 0, 0},\
{ 0, 0, 1, 0},\
{ 0, 0, 0, 1}}}
#define shearing(xy, xz, yx, yz, zx, zy) (Mat4){{{ 1, xy, xz, 0},\
{yx, 1, yz, 0},\
{zx, zy, 1, 0},\
{ 0, 0, 0, 1}}}
// clang-format on
typedef union Vec2
{
struct
{
double x, y;
};
struct
{
double u, v;
};
double elem[2];
} Vec2;
typedef union Vec3
{
struct
{
double x, y, z;
};
struct
{
double u, v, w;
};
struct
{
double r, g, b;
};
struct
{
Vec2 xy;
double _ignored;
};
double elem[3];
} Vec3;
typedef union Vec4
{
struct
{
double x, y, z, w;
};
struct
{
Vec3 xyz;
double _ignored;
};
double elem[4];
} Vec4;
typedef union Mat2
{
double elem[2][2];
Vec2 rows[2];
} Mat2;
typedef union Mat3
{
double elem[3][3];
Vec3 rows[3];
} Mat3;
typedef union Mat4
{
double elem[4][4];
Vec4 rows[4];
} Mat4;
Vec2 vec2Add(Vec2 a, Vec2 b);
Vec2 vec2Sub(Vec2 a, Vec2 b);
Vec2 vec2Mul(Vec2 a, double b);
Vec2 vec2Div(Vec2 a, double b);
Vec2 vec2Neg(Vec2 a);
double vec2Mag(Vec2 a);
Vec2 vec2Norm(Vec2 a);
double vec2Dot(Vec2 a, Vec2 b);
Vec2 vec2Prod(Vec2 a, Vec2 b);
Vec3 vec3Add(Vec3 a, Vec3 b);
Vec3 vec3Sub(Vec3 a, Vec3 b);
Vec3 vec3Mul(Vec3 a, double b);
Vec3 vec3Div(Vec3 a, double b);
Vec3 vec3Neg(Vec3 a);
double vec3Mag(Vec3 a);
Vec3 vec3Norm(Vec3 a);
double vec3Dot(Vec3 a, Vec3 b);
Vec3 vec3Cross(Vec3 a, Vec3 b);
Vec3 vec3Prod(Vec3 a, Vec3 b);
Vec3 vec3PPM(const Vec3 a);
Vec4 vec4Add(Vec4 a, Vec4 b);
Vec4 vec4Sub(Vec4 a, Vec4 b);
Vec4 vec4Mul(Vec4 a, double b);
Vec4 vec4Div(Vec4 a, double b);
Vec4 vec4Neg(Vec4 a);
double vec4Mag(Vec4 a);
Vec4 vec4Norm(Vec4 a);
double vec4Dot(Vec4 a, Vec4 b);
Vec4 vec4Prod(Vec4 a, Vec4 b);
Vec4 vec4Reflect(Vec4 vec, Vec4 normal);
bool mat2Eq(Mat2 a, Mat2 b);
bool mat3Eq(Mat3 a, Mat3 b);
Mat2 mat2Mul(Mat2 a, Mat2 b);
Mat3 mat3Mul(Mat3 a, Mat3 b);
Mat4 mat4Mul(Mat4 a, Mat4 b);
Vec2 mat2VecMul(Mat2 mat, Vec2 vec);
Vec3 mat3VecMul(Mat3 mat, Vec3 vec);
Vec4 mat4VecMul(Mat4 mat, Vec4 vec);
Mat2 mat2Trans(Mat2 a);
Mat3 mat3Trans(Mat3 a);
Mat4 mat4Trans(Mat4 a);
double mat2Det(Mat2 a);
double mat3Det(Mat3 a);
double mat4Det(Mat4 a);
Mat2 mat3SubM(size_t row, size_t col, Mat3 a);
Mat3 mat4SubM(size_t row, size_t col, Mat4 a);
double mat3Min(size_t row, size_t col, Mat3 a);
double mat4Min(size_t row, size_t col, Mat4 a);
double mat3Cof(size_t row, size_t col, Mat3 a);
double mat4Cof(size_t row, size_t col, Mat4 a);
Mat4 mat4Inv(Mat4 a);
Mat4 viewTransform(Vec4 origin, Vec4 destination, Vec4 up);
#endif