summaryrefslogtreecommitdiff
path: root/source/calcify.h
diff options
context:
space:
mode:
authortalha <talha@talhaamir.xyz>2025-12-29 13:45:53 +0500
committertalha <talha@talhaamir.xyz>2025-12-29 13:45:53 +0500
commit9044db4794e95ef9dbc2d5bfde4215ddc82d6ae8 (patch)
treea63784ac600579006a650c49f30d6d2f94b6905f /source/calcify.h
parent0b3d69976819219e71350b6a988d1704fd5f0746 (diff)
added 2d "meshes"HEADmain
Diffstat (limited to 'source/calcify.h')
-rw-r--r--source/calcify.h42
1 files changed, 42 insertions, 0 deletions
diff --git a/source/calcify.h b/source/calcify.h
index d0c3499..0814df7 100644
--- a/source/calcify.h
+++ b/source/calcify.h
@@ -22,6 +22,15 @@ typedef int8_t b8;
#define ABS(x) ((x) > 0 ? (x) : -(x))
#define MIN(x, y) ((x) < (y) ? (y) : (x))
#define MAX(x, y) ((x) > (y) ? (y) : (x))
+#define CLAMP(x, lo, hi) ((x) <= (lo) ? (lo) : ((x) >= (hi) ? (hi) : (x)))
+
+union Vec2 {
+ struct {
+ r32 x;
+ r32 y;
+ };
+ r32 buffer[2];
+};
union Vec3 {
struct {
@@ -53,6 +62,7 @@ union Mat4 {
r32 buffer[16];
};
+Vec3 calcify_normalize3v(Vec3 v);
Mat4 calcify_init4m(void);
Mat4 calcify_ident4m(void);
Mat4 calcify_multiply4mv(Mat4 a, Mat4 b);
@@ -60,6 +70,14 @@ Mat4 calcify_multiply4m(Mat4 a, Mat4 b);
Mat4 calcify_translation_matrix4m(r32 x, r32 y, r32 z);
Mat4 calcify_ortho4m(r32 left, r32 right, r32 top, r32 bot, r32 near, r32 far);
+Vec3 calcify_normalize3v(Vec3 v) {
+ Vec3 res = {};
+ r32 len = sqrt((v.x*v.x) + (v.y*v.y) + (v.z*v.z));
+ res = (Vec3){v.x/len, v.y/len, v.z/len};
+
+ return res;
+}
+
Mat4 calcify_init4m(void) {
Mat4 res;
memset(&res, 0, sizeof(Mat4));
@@ -132,6 +150,30 @@ Mat4 calcify_scaling_matrix4m(r32 x, r32 y) {
return scale;
}
+Mat4 calcify_rotation_matrix4m(r32 angle_radians, Vec3 axis)
+{
+ Mat4 res = calcify_ident4m();
+ axis = calcify_normalize3v(axis);
+
+ r32 cos_theta = cosf(angle_radians);
+ r32 sin_theta = sinf(angle_radians);
+ r32 cos_value = 1.0f - cos_theta;
+
+ res.array[0][0] = (axis.x * axis.x * cos_value) + cos_theta;
+ res.array[0][1] = (axis.x * axis.y * cos_value) + (axis.z * sin_theta);
+ res.array[0][2] = (axis.x * axis.z * cos_value) - (axis.y * sin_theta);
+
+ res.array[1][0] = (axis.x * axis.y * cos_value) - (axis.z * sin_theta);
+ res.array[1][1] = (axis.y * axis.y * cos_value) + cos_theta;
+ res.array[1][2] = (axis.y * axis.z * cos_value) + (axis.x * sin_theta);
+
+ res.array[2][0] = (axis.x * axis.z * cos_value) + (axis.y * sin_theta);
+ res.array[2][1] = (axis.y * axis.z * cos_value) - (axis.x * sin_theta);
+ res.array[2][2] = (axis.z * axis.z * cos_value) + cos_value;
+
+ return res;
+}
+
Mat4 calcify_ortho4m(r32 left, r32 right, r32 bottom, r32 top, r32 near, r32 far) {
Mat4 res = calcify_init4m();
res.array[0][0] = 2.0f/(right - left);