bad char
[vg.git] / vg_m.h
diff --git a/vg_m.h b/vg_m.h
index 436ca63bbd5bf553cc9e39118b7e26cbcad7a1de..4af60c8c84adc1022d424071ef4438130ab8ee88 100644 (file)
--- a/vg_m.h
+++ b/vg_m.h
@@ -1,4 +1,4 @@
-/* Copyright (C) 2021-2023 Harry Godden (hgn) - All Rights Reserved 
+/* Copyright (C) 2021-2024 Harry Godden (hgn) - All Rights Reserved 
  *
  *  0. Misc
  *  1. Scalar operations
@@ -24,8 +24,7 @@
  *    6.a Random numbers
  */
 
-#ifndef VG_M_H
-#define VG_M_H
+#pragma once
 
 #include "vg_platform.h"
 #include <math.h>
@@ -1505,6 +1504,36 @@ static inline void m4x4_inv( m4x4f a, m4x4f d )
    v4_muls( d[3], det, d[3] );
 }
 
+/* 
+ * http://www.terathon.com/lengyel/Lengyel-Oblique.pdf 
+ */
+static void m4x4_clip_projection( m4x4f mat, v4f plane ){
+   v4f c = 
+   {
+      (vg_signf(plane[0]) + mat[2][0]) / mat[0][0],
+      (vg_signf(plane[1]) + mat[2][1]) / mat[1][1],
+      -1.0f,
+      (1.0f + mat[2][2]) / mat[3][2]
+   };
+
+   v4_muls( plane, 2.0f / v4_dot(plane,c), c );
+
+   mat[0][2] = c[0];
+   mat[1][2] = c[1];
+   mat[2][2] = c[2] + 1.0f;
+   mat[3][2] = c[3];
+}
+
+/*
+ * Undoes the above operation
+ */
+static void m4x4_reset_clipping( m4x4f mat, float ffar, float fnear ){
+   mat[0][2] = 0.0f; 
+   mat[1][2] = 0.0f; 
+   mat[2][2] = -(ffar + fnear) / (ffar - fnear); 
+   mat[3][2] = -2.0f * ffar * fnear / (ffar - fnear); 
+}
+
 /*
  * -----------------------------------------------------------------------------
  * Section 5.a                       Boxes
@@ -1614,7 +1643,7 @@ static int plane_intersect3( v4f a, v4f b, v4f c, v3f p )
    return 1;
 }
 
-int plane_intersect2( v4f a, v4f b, v3f p, v3f n )
+static int plane_intersect2( v4f a, v4f b, v3f p, v3f n )
 {
    f32 const epsilon = 1e-6f;
 
@@ -2020,7 +2049,7 @@ static void closest_point_elipse( v2f p, v2f e, v2f o )
  * -----------------------------------------------------------------------------
  */
 
-int ray_aabb1( boxf box, v3f co, v3f dir_inv, f32 dist )
+static int ray_aabb1( boxf box, v3f co, v3f dir_inv, f32 dist )
 {
    v3f v0, v1;
    f32 tmin, tmax;
@@ -2580,5 +2609,3 @@ static void vg_rgb_hsv( v3f rgb, v3f hsv ){
 
    hsv[0] = vg_fractf( hsv[0] * (60.0f/360.0f) );
 }
-
-#endif /* VG_M_H */