// VERTEX
"layout (location=0) in vec2 a_co;"
- "uniform mat4 uPv;"
- "uniform mat4 uMdl;"
+ "uniform mat3 uPv;"
+ "uniform mat3 uMdl;"
""
"void main()"
"{"
- " vec4 vert_pos = uPv * uMdl * vec4( a_co.x, 0.0, a_co.y, 1.0 );"
- " gl_Position = vert_pos;"
+ " gl_Position = vec4( uPv * uMdl * vec3( a_co, 1.0 ), 1.0 );"
"}",
// FRAGMENT
"layout (location=0) in vec2 a_co;" // XY
"layout (location=1) in vec2 a_offset;" // XY offset
"layout (location=2) in vec4 a_data;" // atlas-uv, amt, other
- "uniform mat4 uPv;"
+ "uniform mat3 uPv;"
"uniform vec2 uOrigin;"
""
"out vec4 aCoords;"
"void main()"
"{"
"vec2 world_coord = a_co+a_offset+uOrigin;"
- "gl_Position = uPv * vec4( world_coord.x, 0.0, world_coord.y, 1.0 );"
- "aCoords = vec4((a_co*0.98+0.01 + vec2(a_data.x,0.0)) * 0.0625, a_data.zw);"
+ "gl_Position = vec4( uPv * vec3( world_coord, 1.0 ), 1.0 );"
+ "aCoords = vec4( (a_co*0.98+0.01 + vec2(a_data.x,0.0)) * 0.0625, a_data.zw );"
"}",
// FRAGMENT
"vec4 ripple = texture( uTexRipples, vec2( glyph.x - uTime, glyph.y ));"
"float wstatus = -((aCoords.z/128.0)-1.0);" // -1 := None in, 0.0 := Hold middle, 1.0 := All out
"float dev = step( 0.0, glyph.x+wstatus ) * step( glyph.x+wstatus, 1.0 );" //+ abs(glyph.y-0.5) );"
- "vec3 composite = mix( vec3(0.5,0.5,0.5), vec3(0.3,0.6,1.0) + ripple.xyz * 0.05, step( 0.75, glyph.a )*dev ) * glyph.z;"
+
+ // TODO: Fix texture instead of doing this..
+ "float shadow = mix( 1.0, glyph.z, step(0.25, glyph.w) );"
+ "vec3 composite = mix( vec3(0.5,0.5,0.5), vec3(0.3,0.6,1.0) + ripple.xyz * 0.05, step( 0.75, glyph.a )*dev ) * shadow;"
"FragColor = vec4( composite, 1.0 );"
"}"
,
// VERTEX
"layout (location=0) in vec2 a_co;" // XY UV
- "uniform mat4 uPv;"
+ "uniform mat3 uPv;"
"uniform vec3 uPosition;"
"out vec2 aTexCoords;"
"void main()"
"{"
"vec2 world_coord = a_co + uPosition.xy;"
- "gl_Position = uPv * vec4( world_coord.x, 0.0, world_coord.y, 1.0 );"
+ "gl_Position = vec4( uPv * vec3( world_coord, 1.0 ), 1.0 );"
"aTexCoords = vec2( (a_co.x + uPosition.z) * 0.125, a_co.y );"
"}",
// FRAGMENT
UNIFORMS({ "uPv", "uTexMain", "uColour", "uPosition" })
)
-mat4 m_projection;
-mat4 m_view;
-mat4 m_mdl;
+m3x3f m_projection;
+m3x3f m_view;
+m3x3f m_mdl;
int main( int argc, char *argv[] )
{
fishes[ 20 ];
int num_fishes;
- vec3 origin;
+ v2f origin;
struct cell *selected;
int select_valid;
int playing;
map.io = NULL;
}
-static struct cell *map_tile( int pos[2] )
+static struct cell *ptile( int pos[2] )
{
return map.cells + pos[1]*map.x + pos[0];
}
-static struct cell *map_tile_at( int pos[2] )
+static struct cell *gettile( int pos[2] )
{
if( pos[0] >= 0 && pos[0] < map.x && pos[1] >= 0 && pos[1] < map.y )
return map.cells + pos[1]*map.x + pos[0];
return NULL;
}
-static struct cell *map_tile_at_cond( int pos[2], u32 flags )
+static struct cell *getiftile( int pos[2], u32 flags )
{
- struct cell *cell = map_tile_at( pos );
+ struct cell *cell = gettile( pos );
if( cell && (cell->flags & flags) )
return cell;
if( frame->i < 3 )
{
int *dir = output_dirs[ frame->i ];
- tile = map_tile_at_cond( (int[2]){frame->x+dir[0], frame->y+dir[1]}, CELL_FLAG_WALKABLE );
+ tile = getiftile( (int[2]){frame->x+dir[0], frame->y+dir[1]}, CELL_FLAG_WALKABLE );
if( tile && !(tile->flags & CELL_FLAG_VISITED) )
{
{
struct cell *a, *b, *c, *d;
- a = map_tile_at_cond( (int[2]){ x,y+1 }, CELL_FLAG_WALKABLE );
- b = map_tile_at_cond( (int[2]){ x+1,y }, CELL_FLAG_WALKABLE );
- c = map_tile_at_cond( (int[2]){ x,y-1 }, CELL_FLAG_WALKABLE );
- d = map_tile_at_cond( (int[2]){ x-1,y }, CELL_FLAG_WALKABLE );
+ a = getiftile( (int[2]){ x,y+1 }, CELL_FLAG_WALKABLE );
+ b = getiftile( (int[2]){ x+1,y }, CELL_FLAG_WALKABLE );
+ c = getiftile( (int[2]){ x,y-1 }, CELL_FLAG_WALKABLE );
+ d = getiftile( (int[2]){ x-1,y }, CELL_FLAG_WALKABLE );
u32 config = (a?0x1:0x0) | (b?0x2:0x0) | (c?0x4:0x0) | (d?0x8:0x0);
cellbytes[ 0 ] = config;
{
map_tile_coords_from_index( map.io[i], inputcoord );
map_stack_init( inputcoord );
- struct cell *cell = map_tile_at( inputcoord );
+ struct cell *cell = gettile( inputcoord );
int cr[2];
int outrate = outflow == 128? k_rate_flow: 0;
struct cell *a, *b, *d;
- a = map_tile( (int[2]){ cr[0], cr[1]+1 } );
- b = map_tile( (int[2]){ cr[0]+1, cr[1] } );
- d = map_tile( (int[2]){ cr[0]-1, cr[1] } );
+ a = gettile( (int[2]){ cr[0], cr[1]+1 } );
+ b = gettile( (int[2]){ cr[0]+1, cr[1] } );
+ d = gettile( (int[2]){ cr[0]-1, cr[1] } );
int compute_l = 0, compute_r = 0;
{
map_free();
- char *c = str;
+ char const *c = str;
// Scan for width
for(;; map.x ++)
// Origin top left corner
map.origin[0] = -((float)map.x) * 0.5f;
- map.origin[2] = -((float)map.y) * 0.5f;
+ map.origin[1] = -((float)map.y) * 0.5f;
float *offset_array = (float *)malloc( map.x*map.y*2*sizeof(float) );
return 1;
}
+// Determines if tile at position co is contentious
static int map_tile_availible( int co[2] )
{
- // Extract 5x5 grid surrounding tile
+ // Extract tiles area of influence as a 5x5 grid in bitfield format
u32 blob = 0x1000;
for( int y = vg_max( co[1]-2, 0 ); y < vg_min( map.y, co[1]+3 ); y ++ )
for( int x = vg_max( co[0]-2, 0 ); x < vg_min( map.x, co[0]+3 ); x ++ )
- {
- struct cell *cell = map_tile( (int[2]){ x, y } );
-
- if( cell && (cell->flags & CELL_FLAG_WALKABLE) )
+ if( getiftile( (int[2]){ x, y }, CELL_FLAG_WALKABLE ) )
blob |= 0x1 << ((y-(co[1]-2))*5 + x-(co[0]-2));
- }
// Run filter over center 3x3 grid to check for invalid configurations
+ // The kernel codes both the offsets for reaching each tile inside it, as well as the offsets
+ // to move the kernel as a whole.
int kernel[] = { 0, 1, 2, 5, 6, 7, 10, 11, 12 };
for( int i = 0; i < vg_list_size(kernel); i ++ )
- {
if( blob & (0x1 << (6+kernel[i])) )
{
- // (reference window: 0x1CE7) Illegal moves
- // 0100011100010 ;
- // 0000001100011 ;
- // 0000011000110 ;
- // 0110001100000 ;
- // 1100011000000 ;
- // 0100001100010 ;
- // 0100011000010 ;
-
+ // Illegal moves list in bitfield format
u32 invalid[] = { 0x8E2, 0x63, 0xC6, 0xC60, 0x18C0, 0x862, 0x8C2 };
u32 window = blob >> kernel[i];
if((window & invalid[j]) == invalid[j])
return 0;
}
- }
return 1;
}
// Update camera
float ratio = (float)vg_window_y / (float)vg_window_x;
float const size = 7.5f;
- glm_ortho( -size, size, -size*ratio, size*ratio, 0.01f, 150.f, m_projection );
-
- glm_mat4_identity( m_view );
- glm_translate_z( m_view, -10.f );
- glm_rotate_x( m_view, 1.5708f, m_view );
- glm_mat4_mul( m_projection, m_view, vg_pv );
+ m3x3_projection( m_projection, -size, size, size*ratio, -size*ratio );
+ m3x3_identity( m_view );
+ m3x3_mul( m_projection, m_view, vg_pv );
+ vg_projection_update();
// Compute map update
for( int y = 0; y < map.y; y ++ )
for( int x = 0; x < map.x; x ++ )
{
struct cell *tile, *upper, *lower, *l, *r;
- tile = map_tile_at( (int [2]){ x, y } );
+ tile = gettile( (int [2]){ x, y } );
tile->flags &= ~(CELL_FLAG_SPLIT|CELL_FLAG_MERGE|CELL_FLAG_UPLVL);
if( tile->flags & CELL_FLAG_WALKABLE )
{
- r = map_tile_at_cond( (int[2]){ x+1, y }, CELL_FLAG_WALKABLE );
- l = map_tile_at_cond( (int[2]){ x-1, y }, CELL_FLAG_WALKABLE );
+ r = getiftile( (int[2]){ x+1, y }, CELL_FLAG_WALKABLE );
+ l = getiftile( (int[2]){ x-1, y }, CELL_FLAG_WALKABLE );
if( r && l )
{
- upper = map_tile_at_cond( (int[2]){ x, y-1 }, CELL_FLAG_WALKABLE );
- lower = map_tile_at_cond( (int[2]){ x, y+1 }, CELL_FLAG_WALKABLE );
+ upper = getiftile( (int[2]){ x, y-1 }, CELL_FLAG_WALKABLE );
+ lower = getiftile( (int[2]){ x, y+1 }, CELL_FLAG_WALKABLE );
if( upper )
{
}
}
}
-
- // Compute classification
- /*
- map_stack_refresh();
-
- for( int i = 0; i < arrlen( map.io ); i ++ )
- {
- struct *cell cell = &map.cells[ map.io ];
- int coords[2];
- if( cell->flags & CELL_FLAG_INPUT )
- {
- map_tile_coords_from_index( map.io, coords );
- map_stack_init( coords );
-
- do
- {
- if( cell->flags & CELL_FLAG_CONNECTOR )
- {
-
- }
- }
- while( (cell = map_stack_next()) );
- }
- }*/
-
- // Get mouse ray
- vec3 ray_origin;
- vec3 ray_dir;
-
- mat4 pv_inverse;
- vec4 vp = { 0.f, 0.f, vg_window_x, vg_window_y };
- glm_mat4_inv( vg_pv, pv_inverse );
- glm_unprojecti( (vec3){ vg_mouse_x, vg_window_y-vg_mouse_y, -1.f }, pv_inverse, vp, ray_dir );
- glm_unprojecti( (vec3){ vg_mouse_x, vg_window_y-vg_mouse_y, 0.f }, pv_inverse, vp, ray_origin );
- glm_vec3_sub( ray_dir, ray_origin, ray_dir );
-
- // Get floor tile intersection
- float ray_t = -ray_origin[1] / ray_dir[1];
-
- vec3 tile_pos;
- glm_vec3_copy( ray_origin, tile_pos );
- glm_vec3_muladds( ray_dir, ray_t, tile_pos );
- glm_vec3_sub( tile_pos, map.origin, tile_pos );
+ v2f tile_pos;
+ v2_copy( vg_mouse_ws, tile_pos );
+ v2_sub( tile_pos, map.origin, tile_pos );
int tile_x = floorf( tile_pos[0] );
- int tile_y = floorf( tile_pos[2] );
+ int tile_y = floorf( tile_pos[1] );
- map.selected = map_tile_at( (int [2]){tile_x, tile_y} );
+ map.selected = ptile( (int [2]){tile_x, tile_y} );
if( map.playing )
{
- static int fish_counter = 0;
- fish_counter ++;
-
- if( fish_counter > 20 )
- {
- fish_counter = 0;
-
- // Advance characters
- for( int i = 0; i < map.num_fishes; i ++ )
- {
- struct fish *fish = map.fishes + i;
-
- if( !fish->alive )
- continue;
-
- struct cell *tile, *next;
- tile = map_tile_at( fish->co );
-
- if( tile->flags & CELL_FLAG_OUTPUT )
- {
- vg_info( "Fish got zucced (%d)\n", i );
- fish->alive = 0;
- continue;
- }
-
- int die = 0;
- if( tile->flags & CELL_FLAG_SPLIT )
- {
- die = 1;
- int new_dir[][2] = { {0,-1},{1,0},{-1,0} };
- int *test_dir;
-
- for( int j = 0; j < 3; j ++ )
- {
- test_dir = new_dir[ tile->state ];
- tile->state = (tile->state+1)%3;
-
- next = map_tile_at( (int[2]){ fish->co[0]+test_dir[0], fish->co[1]+test_dir[1] } );
- if( next && (next->flags & (CELL_FLAG_WALKABLE)) )
- {
- fish->dir[0] = test_dir[0];
- fish->dir[1] = test_dir[1];
- die = 0;
- break;
- }
- }
- }
-
- next = map_tile_at( (int[2]){ fish->co[0]+fish->dir[0], fish->co[1]+fish->dir[1] } );
- if( !next || (next && !(next->flags & CELL_FLAG_WALKABLE)) )
- {
- // Try UP
- die = 1;
- }
-
- if( die )
- {
- vg_info( "Fish died! (%d)\n", i );
- fish->alive = 0;
- continue;
- }
-
-
- fish->co[0] += fish->dir[0];
- fish->co[1] += fish->dir[1];
- }
-
- // Try spawn fish
- for( int i = 0; i < arrlen( map.io ); i ++ )
- {
- struct cell *input = &map.cells[ map.io[i] ];
-
- if( input->flags & CELL_FLAG_INPUT )
- {
- if( input->state < arrlen( input->conditions ) )
- {
- struct fish *fish = &map.fishes[ map.num_fishes ];
- map_tile_coords_from_index( map.io[i], fish->co );
-
- int output_dirs[][2] = { {0,-1}, {-1,0}, {1,0} };
- int can_spawn = 0;
-
- for( int i = 0; i < vg_list_size( output_dirs ); i ++ )
- {
- int *dir = output_dirs[i];
- struct cell *next = map_tile_at( (int[2]){ fish->co[0]+dir[0], fish->co[1]+dir[1] } );
- if( next && next->flags & CELL_FLAG_CANAL )
- {
- fish->dir[0] = dir[0];
- fish->dir[1] = dir[1];
- can_spawn = 1;
- }
- }
-
- if( can_spawn )
- {
- fish->alive = 1;
- input->state ++;
- map.num_fishes ++;
- }
- }
- }
- }
-
- vg_info( "There are now %u active fish\n", map.num_fishes );
- }
-
if( vg_get_button_down( "go" ) )
{
map.playing = 0;
- map.num_fishes = 0;
-
vg_info( "Ending!\n" );
}
}
if( vg_get_button_down( "go" ) )
{
map.playing = 1;
-
- // Reset everything
- for( int i = 0; i < map.x*map.y; i ++ )
- map.cells[ i ].state = 0;
-
vg_info( "Starting!\n" );
}
map_update_visual();
SHADER_USE( tilemap_shader );
- glUniformMatrix4fv( SHADER_UNIFORM( tilemap_shader, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
+ glUniformMatrix3fv( SHADER_UNIFORM( tilemap_shader, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
glUniform1i( SHADER_UNIFORM( tilemap_shader, "uTexTiles" ), 0 );
glActiveTexture( GL_TEXTURE0 );
glActiveTexture( GL_TEXTURE1 );
glBindTexture( GL_TEXTURE_2D, map.flow_texture );
- glUniform2f( SHADER_UNIFORM( tilemap_shader, "uOrigin" ), map.origin[0], map.origin[2] );
+ glUniform2fv( SHADER_UNIFORM( tilemap_shader, "uOrigin" ), 1, map.origin );
glUniform1f( SHADER_UNIFORM( tilemap_shader, "uTime" ), vg_time * 0.5f );
glDrawArraysInstanced( GL_TRIANGLES, 0, 6, map.x*map.y );
+ /*
SHADER_USE( fish_shader );
- glUniformMatrix4fv( SHADER_UNIFORM( fish_shader, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
+ glUniformMatrix3fv( SHADER_UNIFORM( fish_shader, "uPv" ), 1, GL_FALSE, (float *)vg_pv );
glUniform1i( SHADER_UNIFORM( fish_shader, "uTexMain" ), 0 );
glActiveTexture( GL_TEXTURE0 );
glBindTexture( GL_TEXTURE_2D, fish_texture );
glDrawArrays( GL_TRIANGLES, 0, 6 );
glDisable( GL_BLEND );
+ */
}
void vg_register(void)
--- /dev/null
+// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
+
+// Util
+// ==================================================================================================================
+
+#define VG_PIf 3.14159265358979323846264338327950288f
+
+// Simple min/max replacements
+static inline float vg_minf( float a, float b )
+{
+ return a < b? a: b;
+}
+
+static inline float vg_maxf( float a, float b )
+{
+ return a > b? a: b;
+}
+
+static inline int vg_min( int a, int b )
+{
+ return a < b? a: b;
+}
+
+static inline int vg_max( int a, int b )
+{
+ return a > b? a: b;
+}
+
+// Convert degrees to radians
+static inline float vg_rad( float deg )
+{
+ return deg * VG_PIf / 180.0f;
+}
+
+// Vector 2
+// ==================================================================================================================
+
+static inline void v2_copy( v2f a, v2f b )
+{
+ b[0] = a[0]; b[1] = a[1];
+}
+
+static inline void v2_minv( v2f a, v2f b, v2f dest )
+{
+ dest[0] = vg_minf(a[0], b[0]);
+ dest[1] = vg_minf(a[1], b[1]);
+}
+
+static inline void v2_maxv( v2f a, v2f b, v2f dest )
+{
+ dest[0] = vg_maxf(a[0], b[0]);
+ dest[1] = vg_maxf(a[1], b[1]);
+}
+
+static inline void v2_sub( v2f a, v2f b, v2f d )
+{
+ d[0] = a[0]-b[0]; d[1] = a[1]-b[1];
+}
+
+static inline float v2_cross( v2f a, v2f b )
+{
+ return a[0] * b[1] - a[1] * b[0];
+}
+
+static inline void v2_add( v2f a, v2f b, v2f d )
+{
+ d[0] = a[0]+b[0]; d[1] = a[1]+b[1];
+}
+
+static inline void v2_muls( v2f a, float s, v2f d )
+{
+ d[0] = a[0]*s; d[1] = a[1]*s;
+}
+
+static inline void v2_mul( v2f a, v2f b, v2f d )
+{
+ d[0] = a[0]*b[0]; d[1] = a[1]*b[1];
+}
+
+static inline void v2_div( v2f a, v2f b, v2f d )
+{
+ d[0] = a[0]/b[0]; d[1] = a[1]/b[1];
+}
+
+// Vector 3
+// ==================================================================================================================
+
+static inline void v3_zero( v3f a )
+{
+ a[0] = 0.f; a[1] = 0.f; a[2] = 0.f;
+}
+
+static inline void v3_copy( v3f a, v3f b )
+{
+ b[0] = a[0]; b[1] = a[1]; b[2] = a[2];
+}
+
+static inline void v3_add( v3f a, v3f b, v3f d )
+{
+ d[0] = a[0]+b[0]; d[1] = a[1]+b[1]; d[2] = a[2]+b[2];
+}
+
+static inline void v3_sub( v3f a, v3f b, v3f d )
+{
+ d[0] = a[0]-b[0]; d[1] = a[1]-b[1]; d[2] = a[2]-b[2];
+}
+
+static inline void v3_mul( v3f a, v3f b, v3f d )
+{
+ d[0] = a[0]*b[0]; d[1] = a[1]*b[1]; d[2] = a[2]*b[2];
+}
+
+static inline void v3_div( v3f a, v3f b, v3f d )
+{
+ d[0] = a[0]/b[0]; d[1] = a[1]/b[1]; d[2] = a[2]/b[2];
+}
+
+static inline void v3_muls( v3f a, float s, v3f d )
+{
+ d[0] = a[0]*s; d[1] = a[1]*s; d[2] = a[2]*s;
+}
+
+static inline void v3_divs( v3f a, float s, v3f d )
+{
+ d[0] = a[0]/s; d[1] = a[1]/s; d[2] = a[2]/s;
+}
+
+static inline void v3_muladds( v3f a, v3f b, float s, v3f d )
+{
+ d[0] = a[0]+b[0]*s; d[1] = a[1]+b[1]*s; d[2] = a[2]+b[2]*s;
+}
+
+static inline float v3_dot( v3f a, v3f b )
+{
+ return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
+}
+
+static inline void v3_cross( v3f a, v3f b, v3f d )
+{
+ d[0] = a[1] * b[2] - a[2] * b[1];
+ d[1] = a[2] * b[0] - a[0] * b[2];
+ d[2] = a[0] * b[1] - a[1] * b[0];
+}
+
+static inline float v3_length2( v3f a )
+{
+ return v3_dot( a, a );
+}
+
+static inline float v3_length( v3f a )
+{
+ return sqrtf( v3_length2( a ) );
+}
+
+static inline float v3_dist2( v3f a, v3f b )
+{
+ v3f delta;
+ v3_sub( a, b, delta );
+ return v3_length2( delta );
+}
+
+static inline float v3_dist( v3f a, v3f b )
+{
+ return sqrtf( v3_dist2( a, b ) );
+}
+
+static inline void v3_normalize( v3f a )
+{
+ v3_muls( a, 1.f / v3_length( a ), a );
+}
+
+static inline float csr_lerpf( float a, float b, float t )
+{
+ return a + t*(b-a);
+}
+
+static inline void v3_lerp( v3f a, v3f b, float t, v3f d )
+{
+ d[0] = a[0] + t*(b[0]-a[0]);
+ d[1] = a[1] + t*(b[1]-a[1]);
+ d[2] = a[2] + t*(b[2]-a[2]);
+}
+
+static inline void v3_minv( v3f a, v3f b, v3f dest )
+{
+ dest[0] = vg_minf(a[0], b[0]);
+ dest[1] = vg_minf(a[1], b[1]);
+ dest[2] = vg_minf(a[2], b[2]);
+}
+
+static inline void v3_maxv( v3f a, v3f b, v3f dest )
+{
+ dest[0] = vg_maxf(a[0], b[0]);
+ dest[1] = vg_maxf(a[1], b[1]);
+ dest[2] = vg_maxf(a[2], b[2]);
+}
+
+static inline float v3_minf( v3f a )
+{
+ return vg_minf( vg_minf( a[0], a[1] ), a[2] );
+}
+
+static inline float v3_maxf( v3f a )
+{
+ return vg_maxf( vg_maxf( a[0], a[1] ), a[2] );
+}
+
+static inline void v3_fill( v3f a, float v )
+{
+ a[0] = v;
+ a[1] = v;
+ a[2] = v;
+}
+
+// Vector 4
+// ==================================================================================================================
+
+static inline void v4_copy( v4f a, v4f b )
+{
+ b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; b[3] = a[3];
+}
+
+static inline void v4_zero( v4f a )
+{
+ a[0] = 0.f; a[1] = 0.f; a[2] = 0.f; a[3] = 0.f;
+}
+
+// Matrix 3x3
+//======================================================================================================
+
+#define M3X3_IDENTITY {{1.0f, 0.0f, 0.0f, },\
+ { 0.0f, 1.0f, 0.0f, },\
+ { 0.0f, 0.0f, 1.0f, }}
+
+#define M3X3_ZERO {{0.0f, 0.0f, 0.0f, },\
+ { 0.0f, 0.0f, 0.0f, },\
+ { 0.0f, 0.0f, 0.0f, }}
+
+
+static inline void m3x3_copy( m3x3f a, m3x3f b )
+{
+ v3_copy( a[0], b[0] );
+ v3_copy( a[1], b[1] );
+ v3_copy( a[2], b[2] );
+}
+
+static inline void m3x3_identity( m3x3f a )
+{
+ m3x3f id = M3X3_IDENTITY;
+ m3x3_copy( id, a );
+}
+
+static inline void m3x3_zero( m3x3f a )
+{
+ m3x3f z = M3X3_ZERO;
+ m3x3_copy( z, a );
+}
+
+static inline void m3x3_inv( m3x3f src, m3x3f dest )
+{
+ float a = src[0][0], b = src[0][1], c = src[0][2],
+ d = src[1][0], e = src[1][1], f = src[1][2],
+ g = src[2][0], h = src[2][1], i = src[2][2];
+
+ float det = 1.f /
+ (+a*(e*i-h*f)
+ -b*(d*i-f*g)
+ +c*(d*h-e*g));
+
+ dest[0][0] = (e*i-h*f)*det;
+ dest[0][1] = -(b*i-c*h)*det;
+ dest[0][2] = (b*f-c*e)*det;
+ dest[1][0] = -(d*i-f*g)*det;
+ dest[1][1] = (a*i-c*g)*det;
+ dest[1][2] = -(a*f-d*c)*det;
+ dest[2][0] = (d*h-g*e)*det;
+ dest[2][1] = -(a*h-g*b)*det;
+ dest[2][2] = (a*e-d*b)*det;
+}
+
+static inline void m3x3_transpose( m3x3f src, m3x3f dest )
+{
+ float a = src[0][0], b = src[0][1], c = src[0][2],
+ d = src[1][0], e = src[1][1], f = src[1][2],
+ g = src[2][0], h = src[2][1], i = src[2][2];
+
+ dest[0][0] = a;
+ dest[0][1] = d;
+ dest[0][2] = g;
+ dest[1][0] = b;
+ dest[1][1] = e;
+ dest[1][2] = h;
+ dest[2][0] = c;
+ dest[2][1] = f;
+ dest[2][2] = i;
+}
+
+static inline void m3x3_mul( m3x3f a, m3x3f b, m3x3f d )
+{
+ float a00 = a[0][0], a01 = a[0][1], a02 = a[0][2],
+ a10 = a[1][0], a11 = a[1][1], a12 = a[1][2],
+ a20 = a[2][0], a21 = a[2][1], a22 = a[2][2],
+
+ b00 = b[0][0], b01 = b[0][1], b02 = b[0][2],
+ b10 = b[1][0], b11 = b[1][1], b12 = b[1][2],
+ b20 = b[2][0], b21 = b[2][1], b22 = b[2][2];
+
+ d[0][0] = a00*b00 + a10*b01 + a20*b02;
+ d[0][1] = a01*b00 + a11*b01 + a21*b02;
+ d[0][2] = a02*b00 + a12*b01 + a22*b02;
+ d[1][0] = a00*b10 + a10*b11 + a20*b12;
+ d[1][1] = a01*b10 + a11*b11 + a21*b12;
+ d[1][2] = a02*b10 + a12*b11 + a22*b12;
+ d[2][0] = a00*b20 + a10*b21 + a20*b22;
+ d[2][1] = a01*b20 + a11*b21 + a21*b22;
+ d[2][2] = a02*b20 + a12*b21 + a22*b22;
+}
+
+static inline void m3x3_mulv( m3x3f m, v3f v, v3f d )
+{
+ v3f res;
+
+ res[0] = m[0][0]*v[0] + m[1][0]*v[1] + m[2][0]*v[2];
+ res[1] = m[0][1]*v[0] + m[1][1]*v[1] + m[2][1]*v[2];
+ res[2] = m[0][2]*v[0] + m[1][2]*v[1] + m[2][2]*v[2];
+
+ v3_copy( res, d );
+}
+
+static inline void m3x3_projection( m3x3f dst, float const left, float const right, float const bottom, float const top )
+{
+ float rl, tb;
+
+ m3x3_zero( dst );
+
+ rl = 1.0f / (right - left);
+ tb = 1.0f / (top - bottom);
+
+ dst[0][0] = 2.0f * rl;
+ dst[1][1] = 2.0f * tb;
+ dst[2][2] = 1.0f;
+}
+
+static inline void m3x3_translate( m3x3f m, v3f v )
+{
+ m[2][0] = m[0][0] * v[0] + m[1][0] * v[1] + m[2][0];
+ m[2][1] = m[0][1] * v[0] + m[1][1] * v[1] + m[2][1];
+ m[2][2] = m[0][2] * v[0] + m[1][2] * v[1] + m[2][2];
+}
+
+static inline void m3x3_scale( m3x3f m, v3f v )
+{
+ m[0][0] = m[0][0] * v[0];
+ m[0][1] = m[0][1] * v[0];
+ m[0][2] = m[0][2] * v[0];
+
+ m[1][0] = m[1][0] * v[1];
+ m[1][1] = m[1][1] * v[1];
+ m[1][2] = m[1][2] * v[1];
+}
+
+static inline void m3x3_rotate( m3x3f m, float angle )
+{
+ float m00 = m[0][0], m10 = m[1][0],
+ m01 = m[0][1], m11 = m[1][1],
+ m02 = m[0][2], m12 = m[1][2];
+ float c, s;
+
+ s = sinf( angle );
+ c = cosf( angle );
+
+ m[0][0] = m00 * c + m10 * s;
+ m[0][1] = m01 * c + m11 * s;
+ m[0][2] = m02 * c + m12 * s;
+
+ m[1][0] = m00 * -s + m10 * c;
+ m[1][1] = m01 * -s + m11 * c;
+ m[1][2] = m02 * -s + m12 * c;
+}
+
+// Matrix 4x3
+// ==================================================================================================================
+
+#define M4X3_IDENTITY {{1.0f, 0.0f, 0.0f, },\
+ { 0.0f, 1.0f, 0.0f, },\
+ { 0.0f, 0.0f, 1.0f, },\
+ { 0.0f, 0.0f, 0.0f }}
+
+static inline void m4x3_to_3x3( m4x3f a, m3x3f b )
+{
+ v3_copy( a[0], b[0] );
+ v3_copy( a[1], b[1] );
+ v3_copy( a[2], b[2] );
+}
+
+static inline void m4x3_copy( m4x3f a, m4x3f b )
+{
+ v3_copy( a[0], b[0] );
+ v3_copy( a[1], b[1] );
+ v3_copy( a[2], b[2] );
+ v3_copy( a[3], b[3] );
+}
+
+static inline void m4x3_identity( m4x3f a )
+{
+ m4x3f id = M4X3_IDENTITY;
+ m4x3_copy( id, a );
+}
+
+static inline void m4x3_mul( m4x3f a, m4x3f b, m4x3f d )
+{
+ float
+ a00 = a[0][0], a01 = a[0][1], a02 = a[0][2],
+ a10 = a[1][0], a11 = a[1][1], a12 = a[1][2],
+ a20 = a[2][0], a21 = a[2][1], a22 = a[2][2],
+ a30 = a[3][0], a31 = a[3][1], a32 = a[3][2],
+ b00 = b[0][0], b01 = b[0][1], b02 = b[0][2],
+ b10 = b[1][0], b11 = b[1][1], b12 = b[1][2],
+ b20 = b[2][0], b21 = b[2][1], b22 = b[2][2],
+ b30 = b[3][0], b31 = b[3][1], b32 = b[3][2];
+
+ d[0][0] = a00*b00 + a10*b01 + a20*b02;
+ d[0][1] = a01*b00 + a11*b01 + a21*b02;
+ d[0][2] = a02*b00 + a12*b01 + a22*b02;
+ d[1][0] = a00*b10 + a10*b11 + a20*b12;
+ d[1][1] = a01*b10 + a11*b11 + a21*b12;
+ d[1][2] = a02*b10 + a12*b11 + a22*b12;
+ d[2][0] = a00*b20 + a10*b21 + a20*b22;
+ d[2][1] = a01*b20 + a11*b21 + a21*b22;
+ d[2][2] = a02*b20 + a12*b21 + a22*b22;
+ d[3][0] = a00*b30 + a10*b31 + a20*b32 + a30;
+ d[3][1] = a01*b30 + a11*b31 + a21*b32 + a31;
+ d[3][2] = a02*b30 + a12*b31 + a22*b32 + a32;
+}
+
+static inline void m4x3_mulv( m4x3f m, v3f v, v3f d )
+{
+ v3f res;
+
+ res[0] = m[0][0]*v[0] + m[1][0]*v[1] + m[2][0]*v[2] + m[3][0];
+ res[1] = m[0][1]*v[0] + m[1][1]*v[1] + m[2][1]*v[2] + m[3][1];
+ res[2] = m[0][2]*v[0] + m[1][2]*v[1] + m[2][2]*v[2] + m[3][2];
+
+ v3_copy( res, d );
+}
+
+// Affine transforms
+// ====================================================================================================================
+
+static inline void m4x3_translate( m4x3f m, v3f v )
+{
+ v3_muladds( m[3], m[0], v[0], m[3] );
+ v3_muladds( m[3], m[1], v[1], m[3] );
+ v3_muladds( m[3], m[2], v[2], m[3] );
+}
+
+static inline void m4x3_scale( m4x3f m, float s )
+{
+ v3_muls( m[0], s, m[0] );
+ v3_muls( m[1], s, m[1] );
+ v3_muls( m[2], s, m[2] );
+}
+
+static inline void m4x3_rotate_x( m4x3f m, float angle )
+{
+ m4x3f t = M4X3_IDENTITY;
+ float c, s;
+
+ c = cosf( angle );
+ s = sinf( angle );
+
+ t[1][1] = c;
+ t[1][2] = s;
+ t[2][1] = -s;
+ t[2][2] = c;
+
+ m4x3_mul( m, t, m );
+}
+
+static inline void m4x3_rotate_y( m4x3f m, float angle )
+{
+ m4x3f t = M4X3_IDENTITY;
+ float c, s;
+
+ c = cosf( angle );
+ s = sinf( angle );
+
+ t[0][0] = c;
+ t[0][2] = -s;
+ t[2][0] = s;
+ t[2][2] = c;
+
+ m4x3_mul( m, t, m );
+}
+
+static inline void m4x3_rotate_z( m4x3f m, float angle )
+{
+ m4x3f t = M4X3_IDENTITY;
+ float c, s;
+
+ c = cosf( angle );
+ s = sinf( angle );
+
+ t[0][0] = c;
+ t[0][1] = s;
+ t[1][0] = -s;
+ t[1][1] = c;
+
+ m4x3_mul( m, t, m );
+}
+
+// Warning: These functions are unoptimized..
+static inline void m4x3_expand_aabb_point( m4x3f m, boxf box, v3f point )
+{
+ v3f v;
+ m4x3_mulv( m, point, v );
+
+ v3_minv( box[0], v, box[0] );
+ v3_maxv( box[1], v, box[1] );
+}
+
+static inline void box_concat( boxf a, boxf b )
+{
+ v3_minv( a[0], b[0], a[0] );
+ v3_maxv( a[1], b[1], a[1] );
+}
+
+static inline void box_copy( boxf a, boxf b )
+{
+ v3_copy( a[0], b[0] );
+ v3_copy( a[1], b[1] );
+}
+
+static inline void m4x3_transform_aabb( m4x3f m, boxf box )
+{
+ v3f a; v3f b;
+
+ v3_copy( box[0], a );
+ v3_copy( box[1], b );
+ v3_fill( box[0], INFINITY );
+ v3_fill( box[1], -INFINITY );
+
+ m4x3_expand_aabb_point( m, box, a );
+ m4x3_expand_aabb_point( m, box, (v3f){ a[0], b[1], a[2] } );
+ m4x3_expand_aabb_point( m, box, (v3f){ b[0], a[1], a[2] } );
+ m4x3_expand_aabb_point( m, box, (v3f){ b[0], b[1], a[2] } );
+ m4x3_expand_aabb_point( m, box, b );
+ m4x3_expand_aabb_point( m, box, (v3f){ a[0], b[1], b[2] } );
+ m4x3_expand_aabb_point( m, box, (v3f){ b[0], a[1], b[2] } );
+ m4x3_expand_aabb_point( m, box, (v3f){ b[0], b[1], b[2] } );
+}
+
+// Planes (double precision)
+// ==================================================================================================================
+
+static inline void tri_to_plane( double a[3], double b[3], double c[3], double p[4] )
+{
+ double edge0[3];
+ double edge1[3];
+ double l;
+
+ edge0[0] = b[0] - a[0];
+ edge0[1] = b[1] - a[1];
+ edge0[2] = b[2] - a[2];
+
+ edge1[0] = c[0] - a[0];
+ edge1[1] = c[1] - a[1];
+ edge1[2] = c[2] - a[2];
+
+ p[0] = edge0[1] * edge1[2] - edge0[2] * edge1[1];
+ p[1] = edge0[2] * edge1[0] - edge0[0] * edge1[2];
+ p[2] = edge0[0] * edge1[1] - edge0[1] * edge1[0];
+
+ l = sqrt(p[0] * p[0] + p[1] * p[1] + p[2] * p[2]);
+ p[3] = (p[0] * a[0] + p[1] * a[1] + p[2] * a[2]) / l;
+
+ p[0] = p[0] / l;
+ p[1] = p[1] / l;
+ p[2] = p[2] / l;
+}
+
+static inline int plane_intersect( double a[4], double b[4], double c[4], double p[4] )
+{
+ double const epsilon = 1e-8f;
+
+ double x[3];
+ double d;
+
+ x[0] = a[1] * b[2] - a[2] * b[1];
+ x[1] = a[2] * b[0] - a[0] * b[2];
+ x[2] = a[0] * b[1] - a[1] * b[0];
+
+ d = x[0] * c[0] + x[1] * c[1] + x[2] * c[2];
+
+ if( d < epsilon && d > -epsilon ) return 0;
+
+ p[0] = (b[1] * c[2] - b[2] * c[1]) * -a[3];
+ p[1] = (b[2] * c[0] - b[0] * c[2]) * -a[3];
+ p[2] = (b[0] * c[1] - b[1] * c[0]) * -a[3];
+
+ p[0] += (c[1] * a[2] - c[2] * a[1]) * -b[3];
+ p[1] += (c[2] * a[0] - c[0] * a[2]) * -b[3];
+ p[2] += (c[0] * a[1] - c[1] * a[0]) * -b[3];
+
+ p[0] += (a[1] * b[2] - a[2] * b[1]) * -c[3];
+ p[1] += (a[2] * b[0] - a[0] * b[2]) * -c[3];
+ p[2] += (a[0] * b[1] - a[1] * b[0]) * -c[3];
+
+ p[0] = -p[0] / d;
+ p[1] = -p[1] / d;
+ p[2] = -p[2] / d;
+
+ return 1;
+}
+
+static inline double plane_polarity( double p[4], double a[3] )
+{
+ return
+ (a[0] * p[0] + a[1] * p[1] + a[2] * p[2])
+ -(p[0]*p[3] * p[0] + p[1]*p[3] * p[1] + p[2]*p[3] * p[2])
+ ;
+}