rigidbody view
[vg.git] / vg_m.h
diff --git a/vg_m.h b/vg_m.h
index 6cbac62295773393f84749c5b62b9b871efd2c4c..3d4f53ffd53eca654a003663be5a014ea43238d3 100644 (file)
--- a/vg_m.h
+++ b/vg_m.h
@@ -1505,6 +1505,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
@@ -2531,4 +2561,54 @@ static void vg_rand_cone( vg_rand *rand, v3f out_dir, f32 angle ){
    out_dir[2] = cosf(r);
 }
 
+static void vg_hsv_rgb( v3f hsv, v3f rgb ){
+   i32 i = floorf( hsv[0]*6.0f );
+   f32 v = hsv[2],
+       f = hsv[0] * 6.0f - (f32)i,
+       p = v * (1.0f-hsv[1]),
+       q = v * (1.0f-f*hsv[1]),
+       t = v * (1.0f-(1.0f-f)*hsv[1]);
+
+   switch( i % 6 ){
+      case 0: rgb[0] = v; rgb[1] = t; rgb[2] = p; break;
+      case 1: rgb[0] = q; rgb[1] = v; rgb[2] = p; break;
+      case 2: rgb[0] = p; rgb[1] = v; rgb[2] = t; break;
+      case 3: rgb[0] = p; rgb[1] = q; rgb[2] = v; break;
+      case 4: rgb[0] = t; rgb[1] = p; rgb[2] = v; break;
+      case 5: rgb[0] = v; rgb[1] = p; rgb[2] = q; break;
+   }
+}
+
+static void vg_rgb_hsv( v3f rgb, v3f hsv ){
+   f32 min = v3_minf( rgb ),
+       max = v3_maxf( rgb ),
+       range = max-min,
+       k_epsilon = 0.00001f;
+
+   hsv[2] = max;
+   if( range < k_epsilon ){
+      hsv[0] = 0.0f;
+      hsv[1] = 0.0f;
+      return;
+   }
+
+   if( max > k_epsilon ){
+      hsv[1] = range/max;
+   }
+   else {
+      hsv[0] = 0.0f;
+      hsv[1] = 0.0f;
+      return;
+   }
+
+   if( rgb[0] >= max )
+      hsv[0] = (rgb[1]-rgb[2])/range;
+   else if( max == rgb[1] )
+      hsv[0] = 2.0f+(rgb[2]-rgb[0])/range;
+   else
+      hsv[0] = 4.0f+(rgb[0]-rgb[1])/range;
+
+   hsv[0] = vg_fractf( hsv[0] * (60.0f/360.0f) );
+}
+
 #endif /* VG_M_H */