fixed m4x3 mul, rendering works
authorhgn <hgodden00@gmail.com>
Sat, 10 Jul 2021 14:39:19 +0000 (15:39 +0100)
committerhgn <hgodden00@gmail.com>
Sat, 10 Jul 2021 14:39:19 +0000 (15:39 +0100)
csRadar.c
csrDraw.h
csrMath.h
csrMem.h
vdf.h
vmf.h

index de19eba919c9411a9f523d53fd010ececf517b82..9c8c0b4d0f08e8922896ef1911554a2ba8c6e78a 100644 (file)
--- a/csRadar.c
+++ b/csRadar.c
@@ -39,6 +39,36 @@ int main( int argc, char *argv[] )
                
                vmf_map *map = vmf_init( argv[1], 1 );
                
                
                vmf_map *map = vmf_init( argv[1], 1 );
                
+               // TODO: Make init/free codes
+               csr_target target = 
+               {
+                       .x = 1024, .y = 1024,
+                       .fragments = (csr_frag *)csr_malloc( 1024*1024*sizeof(csr_frag) ),
+                       .bounds = { -1000.f, -1000.f, 1000.f, 1000.f }
+               };
+               csr_rt_clear( &target );
+               
+               draw_vmf_group( &target, map, map->root, 0, NULL, NULL );
+               
+               float *rgba_test = (float *)csr_malloc( 1024*1024*sizeof(float)*3 );
+               
+               for( int l = 0; l < 1024; l ++ )
+               {
+                       for( int x = 0; x < 1024; x ++ )
+                       {
+                               float *dst = &rgba_test[ (l*1024+x)*3 ];
+                               csr_frag *src = &target.fragments[ ((1023-l)*1024+x) ];
+                               
+                               dst[0] = src->co[0];
+                               dst[1] = src->co[1];
+                               dst[2] = src->co[2];
+                       }
+               }
+               
+               csr_32f_write( "hello.pfm", 1024, 1024, rgba_test );
+               
+               free( target.fragments );
+               free( rgba_test );
                vmf_free( map );
                fs_exit();
                
                vmf_free( map );
                fs_exit();
                
index e0d125e7482b0334a156b6c0f33516a99f98f892..30443343d4456e4d71497568672286cc1f88ed3a 100644 (file)
--- a/csrDraw.h
+++ b/csrDraw.h
@@ -1,27 +1,37 @@
 typedef struct csr_frag csr_frag;
 typedef struct csr_frag csr_frag;
+typedef struct csr_target csr_target;
 
 struct csr_frag
 {
        u32 id; // Triangle index
 
 struct csr_frag
 {
        u32 id; // Triangle index
-       float qa, qb;   // Quantities
-       
        float depth;    // 'depth testing'
        float depth;    // 'depth testing'
+       
+       v3f co;
+       v3f nrm;
+};
+
+struct csr_target
+{
+       csr_frag *fragments;
+       u32 x, y; 
+       v4f bounds;
 };
 
 };
 
-void clear_depth( csr_frag fragments[], u32 x, u32 y )
+void csr_rt_clear( csr_target *rt )
 {
 {
-       for( u32 i = 0; i < x*y; i ++ )
+       for( u32 i = 0; i < rt->x*rt->y; i ++ )
        {
        {
-               fragments[ i ].depth = INFINITY;
+               rt->fragments[ i ].depth = 0.f;
        }
 }
 
        }
 }
 
-void simple_raster( csr_frag fragments[], u32 x, u32 y, v4f cam_bounds, vmf_vert tri[3], int id )
+void simple_raster( csr_target *rt, vmf_vert tri[3], int id )
 {
        // Very simplified tracing algorithm
 {
        // Very simplified tracing algorithm
-
+       float tqa = 0.f, tqb = 0.f;
+       
        v2f bmin = { 0.f, 0.f };
        v2f bmin = { 0.f, 0.f };
-       v2f bmax = { x, y };
+       v2f bmax = { rt->x, rt->y };
        
        v2_minv( tri[0].co, tri[1].co, bmin );
        v2_minv( tri[2].co, bmin, bmin );
        
        v2_minv( tri[0].co, tri[1].co, bmin );
        v2_minv( tri[2].co, bmin, bmin );
@@ -29,47 +39,138 @@ void simple_raster( csr_frag fragments[], u32 x, u32 y, v4f cam_bounds, vmf_vert
        v2_maxv( tri[0].co, tri[1].co, bmax );
        v2_maxv( tri[2].co, bmax, bmax );
 
        v2_maxv( tri[0].co, tri[1].co, bmax );
        v2_maxv( tri[2].co, bmax, bmax );
 
-       float range_x = (cam_bounds[2]-cam_bounds[0])/(float)x;
-       float range_y = (cam_bounds[3]-cam_bounds[1])/(float)y;
-
-       int start_x = csr_max( 0, floorf( (bmin[0]-cam_bounds[0])/range_x));
-       int end_x =   csr_min( x, floorf( (bmax[0]-cam_bounds[0])/range_x ));
-       int start_y = csr_max( 0, ceilf( (bmin[1]-cam_bounds[1])/range_y ));
-       int end_y =   csr_min( y, ceilf( (bmax[1]-cam_bounds[1])/range_y ));
+       float range_x = (rt->bounds[2]-rt->bounds[0])/(float)rt->x;
+       float range_y = (rt->bounds[3]-rt->bounds[1])/(float)rt->y;
+       
+       int start_x = csr_min( rt->x-1, csr_max( 0,     floorf( (bmin[0]-rt->bounds[0])/range_x)));
+       int end_x =   csr_max( 0,       csr_min( rt->x-1, floorf( (bmax[0]-rt->bounds[0])/range_x )));
+       int start_y = csr_min( rt->y-1, csr_max( 0,      ceilf( (bmin[1]-rt->bounds[1])/range_y )));
+       int end_y =   csr_max( 0,       csr_min( rt->y-1,  ceilf( (bmax[1]-rt->bounds[1])/range_y )));
        
        
-       v3f trace_dir = { 0.f, 0.f, -1.f };
-       v3f trace_origin = { 0.f, 0.f, 16385.f };
+       v3f trace_dir = { 0.f, 0.f, 1.f };
+       v3f trace_origin = { 0.f, 0.f, -16385.f };
 
 
-       for( u32 py = start_y; py < end_y; py ++ )
+       for( u32 py = start_y; py <= end_y; py ++ )
        {
        {
-               trace_origin[1] = csr_lerpf( cam_bounds[1], cam_bounds[3], (float)py/(float)y );
+               trace_origin[1] = csr_lerpf( rt->bounds[1], rt->bounds[3], (float)py/(float)rt->y );
                
                
-               for( u32 px = start_x; px < end_x; px ++ )
+               for( u32 px = start_x; px <= end_x; px ++ )
                {
                {
-                       trace_origin[0] = csr_lerpf( cam_bounds[0], cam_bounds[2], (float)px/(float)x );
-                       
-                       csr_frag *frag = &fragments[ py*y + px ];
+                       csr_frag *frag = &rt->fragments[ py * rt->y + px ];
 
 
-                       float tqa = 0.f, tqb = 0.f;
+                       trace_origin[0] = csr_lerpf( rt->bounds[0], rt->bounds[2], (float)px/(float)rt->x );
                        float tdepth = csr_ray_tri( trace_origin, trace_dir, tri[0].co, tri[1].co, tri[2].co, &tqa, &tqb );
                        
                        float tdepth = csr_ray_tri( trace_origin, trace_dir, tri[0].co, tri[1].co, tri[2].co, &tqa, &tqb );
                        
-                       if( tdepth < frag->depth )
+                       if( tdepth > frag->depth )
                        {
                                frag->depth = tdepth;
                        {
                                frag->depth = tdepth;
-                               frag->id = id;
-                               frag->qa = tqa;
-                               frag->qb = tqb;
+                               
+                               v3_muls( tri[1].co, tqa, frag->co );
+                               v3_muladds( frag->co, tri[2].co, tqb, frag->co );
+                               v3_muladds( frag->co, tri[0].co, 1.f - tqa - tqb, frag->co );
                        }
                }
        }
 }
 
                        }
                }
        }
 }
 
-// First pass 'fragmentize'
-void draw_buffers( csr_frag fragments[], u32 x, u32 y, v4f cam_bounds, vmf_vert *triangles, u32 triangle_count )
+void csr_draw( csr_target *rt, vmf_vert *triangles, u32 triangle_count, m4x3f transform )
 {
 {
+       m3x3f normal;
+       vmf_vert new_tri[3];
+
+       // Derive normal matrix
+       m4x3_to_3x3( transform, normal );
+       m3x3_inv_transpose( normal, normal );
+
        for( u32 i = 0; i < triangle_count; i ++ )
        {
                vmf_vert *triangle = triangles + i*3;
        for( u32 i = 0; i < triangle_count; i ++ )
        {
                vmf_vert *triangle = triangles + i*3;
-               simple_raster( fragments, x, y, cam_bounds, triangle, i );
+               
+               m4x3_mulv( transform, triangle[0].co, new_tri[0].co );
+               m4x3_mulv( transform, triangle[1].co, new_tri[1].co );
+               m4x3_mulv( transform, triangle[2].co, new_tri[2].co );
+               m3x3_mulv( normal, triangle[0].nrm, new_tri[0].nrm );
+               m3x3_mulv( normal, triangle[1].nrm, new_tri[1].nrm );
+               m3x3_mulv( normal, triangle[2].nrm, new_tri[2].nrm );
+               
+               simple_raster( rt, new_tri, 0 );
        }
 }
        }
 }
+
+void draw_vmf_group( csr_target *rt, vmf_map *map, vdf_node *root, int const group, m4x3f prev, m4x3f inst )
+{
+       m4x3f transform = M4X3_IDENTITY;
+       vmf_solid solid;
+       vmf_vert tri[3];
+
+       // Multiply previous transform with instance transform to create basis
+       if( prev )
+       {
+               m4x3_mul( prev, inst, transform );
+       }
+
+       // Draw brushes
+       solidgen_ctx_init( &solid );
+       vdf_node *world = vdf_next( root, "world", NULL );
+       
+       vdf_foreach( world, "solid", brush )
+       {
+               solidgen_push( &solid, brush );
+       }
+
+       for( int i = 0; i < csr_sb_count( solid.indices )/3; i ++ )
+       {
+               u32 * base = solid.indices + i*3;
+               
+               tri[0] = solid.verts[ base[0] ];
+               tri[1] = solid.verts[ base[1] ];
+               tri[2] = solid.verts[ base[2] ];
+               
+               csr_draw( rt, tri, 1, transform );
+       }
+       
+       solidgen_ctx_reset( &solid );
+               
+       // Actual entity loop
+       m4x3f model;
+       
+       vdf_foreach( root, "entity", ent )
+       {
+               if( ent->user & VMF_FLAG_IS_PROP )
+               {
+                       // Create model transform
+                       m4x3_identity( model );
+                       
+                       vmf_entity_transform( ent, model );
+                       m4x3_mul( transform, model, model );
+                       
+                       // Draw model
+                       mdl_mesh_t *mdl = &map->models[ ent->user1 ].mdl;
+                       for( int i = 0; i < mdl->num_indices/3; i ++ )
+                       {
+                               for( int j = 0; j < 3; j ++ )
+                               {
+                                       v3_copy( &mdl->vertices[ mdl->indices[ i*3+j ] *8 ],   tri[j].co );
+                                       v3_copy( &mdl->vertices[ mdl->indices[ i*3+j ] *8+3 ], tri[j].nrm );
+                                       tri[j].xy[0] = 0.f;
+                                       tri[j].xy[1] = 0.f;
+                               }
+                               
+                               csr_draw( rt, tri, 1, model );
+                       }
+               } 
+               else if( ent->user & VMF_FLAG_IS_INSTANCE )
+               {
+                       m4x3_identity( model );
+                       vmf_entity_transform( ent, model );
+                       
+                       draw_vmf_group( rt, map, map->cache[ ent->user1 ].root, group, transform, model );
+               }
+               else if( ent->user & VMF_FLAG_BRUSH_ENT )
+               {
+                       // ...
+               }
+       }
+       
+       solidgen_ctx_free( &solid );
+}
index 63e5bd0e29115a0ddbb1d11f809d4eed48f27a4b..9149bfb9abba65d219b3455d780b51c40875d7a9 100644 (file)
--- a/csrMath.h
+++ b/csrMath.h
@@ -1,6 +1,8 @@
 // Util
 // ==================================================================================================================
 
 // Util
 // ==================================================================================================================
 
+#define CSR_PIf 3.14159265358979323846264338327950288f
+
 float csr_minf( float a, float b )
 {
        if( a < b )
 float csr_minf( float a, float b )
 {
        if( a < b )
@@ -36,6 +38,11 @@ void v3d_v3f( double a[3], float b[3] )
        b[2] = a[2];
 }
 
        b[2] = a[2];
 }
 
+float csr_rad( float deg )
+{
+       return deg * CSR_PIf / 180.0f;
+}
+
 // Vector 2
 // ==================================================================================================================
 
 // Vector 2
 // ==================================================================================================================
 
@@ -257,16 +264,16 @@ void m4x3_mul( m4x3f a, m4x3f b, m4x3f d )
        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];
        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 + a30;
-       d[0][1] = a01*b00 + a11*b01 + a21*b02 + a31;
-       d[0][2] = a02*b00 + a12*b01 + a22*b02 + a32;
-       d[1][0] = a00*b10 + a10*b11 + a20*b12 + a30;
-       d[1][1] = a01*b10 + a11*b11 + a21*b12 + a31;
-       d[1][2] = a02*b10 + a12*b11 + a22*b12 + a32;
-       d[2][0] = a00*b20 + a10*b21 + a20*b22 + a30;
-       d[2][1] = a01*b20 + a11*b21 + a21*b22 + a31;
-       d[2][2] = a02*b20 + a12*b21 + a22*b22 + a32;
+       
+       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;
        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;
@@ -455,19 +462,19 @@ float csr_ray_tri( v3f o, v3f d, v3f v0, v3f v1, v3f v2, float *u, float *v )
        
        det = v3_dot( v0v1, p );
        
        
        det = v3_dot( v0v1, p );
        
-       if( det < k_cullEpsilon ) return INFINITY;
+       if( det < k_cullEpsilon ) return -INFINITY;
        
        inv = 1.f / det;
        
        v3_sub( o, v0, tv );
        *u = v3_dot( tv, p ) * inv;
        
        
        inv = 1.f / det;
        
        v3_sub( o, v0, tv );
        *u = v3_dot( tv, p ) * inv;
        
-       if( *u < 0.f || *u > 1.f ) return INFINITY;
+       if( *u < 0.f || *u > 1.f ) return -INFINITY;
        
        v3_cross( tv, v0v1, qv );
        *v = v3_dot( d, qv ) * inv;
        
        
        v3_cross( tv, v0v1, qv );
        *v = v3_dot( d, qv ) * inv;
        
-       if( *v < 0.f || *u + *v > 1.f ) return INFINITY;
+       if( *v < 0.f || *u + *v > 1.f ) return -INFINITY;
        
        return v3_dot( v0v2, qv ) * inv;
 }
        
        return v3_dot( v0v2, qv ) * inv;
 }
index b2be8e8bcb576d9c68dd3a5895da8b3ae172703b..2c12e44a462c644aa86baa84008cb9e0d958a4ce 100644 (file)
--- a/csrMem.h
+++ b/csrMem.h
@@ -83,6 +83,14 @@ void csr_sb_free( void *arr )
        }
 }
 
        }
 }
 
+void csr_sb_clear( void *arr )
+{
+       if( arr )
+       {
+               ((u32 *)csr_sb_raw( arr ))[1] = 0;
+       }
+}
+
 // djb2 - Dan Bernstein
 unsigned long djb2( unsigned char const *str )
 {
 // djb2 - Dan Bernstein
 unsigned long djb2( unsigned char const *str )
 {
diff --git a/vdf.h b/vdf.h
index 2b77a7a4f3bf65fc87aa0b7c54bc7978d8e1a782..892b65d5f304386a0efa532888e14c79398c20ca 100644 (file)
--- a/vdf.h
+++ b/vdf.h
@@ -1,12 +1,12 @@
 #define vdf_foreach( NODE, STR, AS ) \
 #define vdf_foreach( NODE, STR, AS ) \
-int __vdf_it_##__LINE__ = 0; \
+int __vdf_it_##AS = 0; \
 vdf_node * AS;\
 vdf_node * AS;\
-while( (AS = vdf_next( NODE, STR, &__vdf_it_##__LINE__ )) )
+while( (AS = vdf_next( NODE, STR, &__vdf_it_##AS )) )
 
 #define kv_foreach( NODE, STR, AS ) \
 
 #define kv_foreach( NODE, STR, AS ) \
-int __kv_it_##__LINE__ = 0; \
+int __kv_it_##AS = 0; \
 const char * AS;\
 const char * AS;\
-while( (AS = kv_iter( NODE, STR, &__kv_it_##__LINE__ )) )
+while( (AS = kv_iter( NODE, STR, &__kv_it_##AS )) )
 
 #include <stdio.h>
 #include <stdint.h>
 
 #include <stdio.h>
 #include <stdint.h>
@@ -105,6 +105,7 @@ struct vdf_node
        vdf_kv  *pairs;
        
        u32             user;
        vdf_kv  *pairs;
        
        u32             user;
+       u32             user1;
 };
 
 vdf_node *vdf_next( vdf_node *node, const char *name, int *it )
 };
 
 vdf_node *vdf_next( vdf_node *node, const char *name, int *it )
diff --git a/vmf.h b/vmf.h
index cdec7dd94ec2550dfe228fcd1ff02bd2bbd5a81c..4793e0bda61af53b28c06a4bc521193369c9bc0a 100644 (file)
--- a/vmf.h
+++ b/vmf.h
@@ -1,4 +1,7 @@
 #define SOLID_MAX_SIDES 512
 #define SOLID_MAX_SIDES 512
+#define VMF_FLAG_IS_PROP 0x1
+#define VMF_FLAG_IS_INSTANCE 0x2
+#define VMF_FLAG_BRUSH_ENT 0x4
 
 typedef struct vmf_solid vmf_solid;
 typedef struct vmf_vert vmf_vert;
 
 typedef struct vmf_solid vmf_solid;
 typedef struct vmf_vert vmf_vert;
@@ -67,6 +70,8 @@ struct vmf_map
                u32     hash;
                
                vdf_node *root;
                u32     hash;
                
                vdf_node *root;
+               
+               m4x3f transform;
        }
        *cache;
        
        }
        *cache;
        
@@ -75,6 +80,12 @@ struct vmf_map
 
 // IMPLEMENTATION
 
 
 // IMPLEMENTATION
 
+void solidgen_ctx_reset( vmf_solid *ctx )
+{
+       csr_sb_clear( ctx->verts );
+       csr_sb_clear( ctx->indices );
+}
+
 void solidgen_ctx_init( vmf_solid *ctx )
 {
        const u32 init_size = 128;
 void solidgen_ctx_init( vmf_solid *ctx )
 {
        const u32 init_size = 128;
@@ -573,12 +584,17 @@ u32 vmf_get_mdl( vmf_map *map, const char *mdl )
        return 0;
 }
 
        return 0;
 }
 
+int vmf_class_is_prop( vdf_node *ent )
+{
+       return !strncmp( kv_get( ent, "classname", "" ), "prop_", 5 );
+}
+
 void vmf_populate_models( vdf_node *vmf, vmf_map *map )
 {
        vdf_foreach( vmf, "entity", ent )
        {
                // Use any class name with prop_
 void vmf_populate_models( vdf_node *vmf, vmf_map *map )
 {
        vdf_foreach( vmf, "entity", ent )
        {
                // Use any class name with prop_
-               if( !strncmp( kv_get( ent, "classname", "" ), "prop_", 5 ))
+               if( vmf_class_is_prop( ent ) )
                {
                        // Check if it exists
                        const char *model_path = kv_get( ent, "model", "" );
                {
                        // Check if it exists
                        const char *model_path = kv_get( ent, "model", "" );
@@ -598,7 +614,8 @@ void vmf_populate_models( vdf_node *vmf, vmf_map *map )
                        }
                        
                        // Assign prop-ID for later use
                        }
                        
                        // Assign prop-ID for later use
-                       ent->user = mdl_id;
+                       ent->user = VMF_FLAG_IS_PROP;
+                       ent->user1 = mdl_id;
                }
        }
 }
                }
        }
 }
@@ -621,7 +638,7 @@ void vmf_load_models( vmf_map *map )
                vmf_populate_models( map->cache[i].root, map );
        }
        
                vmf_populate_models( map->cache[i].root, map );
        }
        
-       printf( "Indexed (%u) models\n", csr_sb_count( map->models ) );
+       printf( "Indexed (%u) models\n", csr_sb_count( map->models )-1 );
        
        u32 num_success = 0;
        
        
        u32 num_success = 0;
        
@@ -629,20 +646,22 @@ void vmf_load_models( vmf_map *map )
        // TODO: Make nice loading bar
        for( int i = 1; i < csr_sb_count( map->models ); i ++ )
        {
        // TODO: Make nice loading bar
        for( int i = 1; i < csr_sb_count( map->models ); i ++ )
        {
-               printf( "Load model (%d)\n", i );
-               
                struct vmf_model *mdl = &map->models[i];
                
                if( mdl_from_find_files( mdl->str, &mdl->mdl ) )
                {
                        num_success ++;
                }
                struct vmf_model *mdl = &map->models[i];
                
                if( mdl_from_find_files( mdl->str, &mdl->mdl ) )
                {
                        num_success ++;
                }
+               else
+               {
+                       fprintf( stderr, "Failed to load model: %s\n", mdl->str );
+               }
        }
        
        }
        
-       printf( "Done (%u of %u loaded)\n", num_success, csr_sb_count( map->models ) );
+       printf( "Done (%u of %u loaded)\n", num_success, csr_sb_count( map->models )-1 );
 }
 
 }
 
-void vmf_init_subvmf( vmf_map *map, const char *subvmf );
+u32 vmf_init_subvmf( vmf_map *map, const char *subvmf );
 
 void vmf_load_all_instances( vmf_map *map, vdf_node *vmf )
 {
 
 void vmf_load_all_instances( vmf_map *map, vdf_node *vmf )
 {
@@ -654,32 +673,39 @@ void vmf_load_all_instances( vmf_map *map, vdf_node *vmf )
                        const char *path = kv_get( ent, "file", "" );
                        if( strcmp( path, "" ) )
                        {
                        const char *path = kv_get( ent, "file", "" );
                        if( strcmp( path, "" ) )
                        {
-                               vmf_init_subvmf( map, path );
+                               if( (ent->user1 = vmf_init_subvmf( map, path )))
+                               {
+                                       ent->user1 --;
+                                       ent->user = VMF_FLAG_IS_INSTANCE;
+                               }
                        }
                }
        }
 }
 
                        }
                }
        }
 }
 
-void vmf_init_subvmf( vmf_map *map, const char *subvmf )
+// TODO: Merge this into above function.. doesnt need to be seperated
+u32 vmf_init_subvmf( vmf_map *map, const char *subvmf )
 {
 {
-       printf( "Loading subvmf: %s\n", subvmf );
-       
+       u32 id;
        u32 hash = djb2( (const unsigned char *)subvmf );
        
        // Check if present
        u32 hash = djb2( (const unsigned char *)subvmf );
        
        // Check if present
-       for( int i = 0; i < csr_sb_count( map->cache ); i ++ )
+       for( u32 i = 0; i < csr_sb_count( map->cache ); i ++ )
        {
                if( hash == map->cache[i].hash )
                {
                        if( !strcmp( map->cache[i].name, subvmf ) )
                        {
        {
                if( hash == map->cache[i].hash )
                {
                        if( !strcmp( map->cache[i].name, subvmf ) )
                        {
-                               return;
+                               return i+1;
                        }
                }
        }
        
                        }
                }
        }
        
+       printf( "Loading subvmf: %s\n", subvmf );
+       
+       id = csr_sb_count( map->cache );
        map->cache = csr_sb_reserve( map->cache, 1, sizeof( struct vmf_instance ));
        map->cache = csr_sb_reserve( map->cache, 1, sizeof( struct vmf_instance ));
-       struct vmf_instance *inst = &map->cache[ csr_sb_count( map->cache ) ];
+       struct vmf_instance *inst = &map->cache[ id ];
        
        if( (inst->root = vdf_open_file( subvmf )) )
        {
        
        if( (inst->root = vdf_open_file( subvmf )) )
        {
@@ -691,12 +717,13 @@ void vmf_init_subvmf( vmf_map *map, const char *subvmf )
                
                // Recursive load other instances
                vmf_load_all_instances( map, inst->root );
                
                // Recursive load other instances
                vmf_load_all_instances( map, inst->root );
+               
+               return id+1;
        }
        else
        {
        }
        else
        {
-               // TODO: Don't die here?
                fprintf( stderr, "Failed to load instance file\n" );
                fprintf( stderr, "Failed to load instance file\n" );
-               exit(0);
+               return 0;
        }
 }
 
        }
 }
 
@@ -781,3 +808,26 @@ void solidgen_to_obj( vmf_solid *ctx, const char *path )
                fprintf( stderr, "Could not open %s for writing\n", path );
        }
 }
                fprintf( stderr, "Could not open %s for writing\n", path );
        }
 }
+
+void vmf_entity_transform( vdf_node *ent, m4x3f mat )
+{
+       v3f angles = {0.f,0.f,0.f};
+       v3f offset = {0.f,0.f,0.f};
+       float scale;
+       
+       // Parse
+       scale = kv_get_float( ent, "uniformscale", 1.f );
+       kv_float_array( ent, "angles", 3, angles );
+       kv_float_array( ent, "origin", 3, offset );
+       
+       // Translation
+       m4x3_translate( mat, offset );
+       
+       // Make rotation ( Pitch yaw roll // YZX. Source->OpenGL ordering a lil messed up )
+       m4x3_rotate_z( mat, csr_rad( angles[1] ) );
+       m4x3_rotate_y( mat, csr_rad( angles[0] ) );
+       m4x3_rotate_x( mat, csr_rad( angles[2] ) );
+       
+       // Scale
+       m4x3_scale( mat, scale );
+}