finish up plugin architecture
[csRadar.git] / ext_csr_free.c
diff --git a/ext_csr_free.c b/ext_csr_free.c
new file mode 100644 (file)
index 0000000..5873802
--- /dev/null
@@ -0,0 +1,100 @@
+// Copyright (C) 2021 Harry Godden (hgn)
+
+// Basic buffers-only version of csRadar
+//=======================================================================================================================
+
+#include "csRadar.h"
+
+// GBuffer shader
+void frag_gbuffer( void *dest, vmf_vert tri[3], float bca, float bcb, float bcc );
+void frag_gbuffer_clear( void *dest );
+
+csr_shader shader_gbuffer =
+{
+       .stride = sizeof(float)*8, // (origin) x,y, (actual height) z
+       .frag = frag_gbuffer,
+       .clear = frag_gbuffer_clear
+};
+
+// Main drawing function
+void draw_buffers( csr_api *api, int bounds_only );
+
+// Extension implementation
+// =========================================================================================================
+
+// API ENTRY
+void csr_ext_main( csr_api *api )
+{
+       if( !csr_init( api ) )
+               return;
+       
+       csr_create_target( &api->target, api->resolution, api->resolution, api->sampling_mode, &shader_gbuffer );
+       csr_rt_clear( &api->target );
+       
+       // Compute bounds, collect models
+       draw_buffers( api, 1 );
+       csr_auto_fit( &api->target, api->padding );
+       vmf_load_models( api->map );
+       
+       // Draw everything
+       draw_buffers( api, 0 ); 
+}
+
+void csr_ext_exit( csr_api *api )
+{
+       csr_rt_free( &api->target );
+}
+
+void draw_buffers( csr_api *api, int bounds_only )
+{
+       csr_filter filter = { .compute_bounds_only = bounds_only };
+       vmf_map *map = api->map;
+
+       if( api->num_strings == 1 )
+       {
+               // Draw everything
+               csr_vmf_render( &api->target, map, map->root, &filter, NULL, NULL );
+               
+               if( !bounds_only )
+               {
+                       csr_rt_save_buffers( &api->target, api->output_path, "all" );
+               }
+       }
+       else
+       {
+               // Draw groups
+               for( int i = 1; i < api->num_strings; i ++ )
+               {
+                       filter.visgroup = api->strings[ i ];
+                       csr_vmf_render( &api->target, map, map->root, &filter, NULL, NULL );
+
+                       if( !bounds_only )
+                       {
+                               csr_rt_save_buffers( &api->target, api->output_path, api->strings[i] );                 
+                               //csr_rt_save_c32f( ... );
+                               //csr_rt_save_tga( ... );
+                               
+                               // tar_write_dds( ... );
+                               
+                               csr_rt_clear( &api->target );
+                       }
+               }
+       }
+}
+
+void frag_gbuffer( void *dest, vmf_vert tri[3], float bca, float bcb, float bcc )
+{
+       float *dest_colour = (float *)dest;
+
+       v3_muls( tri[0].co, bca, dest_colour );
+       v3_muladds( dest_colour, tri[1].co, bcb, dest_colour );
+       v3_muladds( dest_colour, tri[2].co, bcc, dest_colour );
+       
+       // TODO: Normal map
+}
+
+void frag_gbuffer_clear( void *dest )
+{
+       float *dest_colour = (float *)dest;
+       v3_zero( dest_colour );
+}