+// Copyright (C) 2021 Harry Godden (hgn)
+
+// Basic buffers-only version of csRadar
+//=======================================================================================================================
+
+#include "csRadar.h"
+
+float min_height = 0.f;
+v4f remapping_range;
+
+// 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 );
+
+ min_height = api->target.bounds[0][2];
+ v2_copy( api->target.bounds[0], remapping_range );
+ v2_sub( api->target.bounds[1], api->target.bounds[0], &remapping_range[2] );
+
+ api->target.bounds[0][2] = api->min_z;
+ api->target.bounds[1][2] = api->max_z;
+
+ // 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" );
+ csr_rt_save_tga( &api->target, "test_test_test.tga", 3*sizeof(float), 4 );
+ }
+ }
+ else
+ {
+ // Draw groups
+ for( int i = 1; i < api->num_strings; i ++ )
+ {
+ csr_filter_update_from_strings( &filter, &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].str );
+ //csr_rt_save_c32f( ... );
+ //csr_rt_save_tga( ... );
+
+ // tar_write_dds( ... );
+ csr_rt_save_tga( &api->target, "test_test_test.tga", 3*sizeof(float), 4 );
+
+ 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;
+
+ // Position
+ v2_sub( tri[0].origin, remapping_range, dest_colour );
+ v2_div( dest_colour, &remapping_range[2], dest_colour );
+
+ dest_colour[2] = tri[0].co[2]*bca + tri[1].co[2]*bcb + tri[2].co[2]*bcc;
+
+ // Normals
+ v3_muls( tri[0].nrm, bca, dest_colour+3 );
+ v3_muladds( dest_colour+3, tri[1].nrm, bcb, dest_colour+3 );
+ v3_muladds( dest_colour+3, tri[2].nrm, bcc, dest_colour+3 );
+
+ v3_muls( dest_colour+3, 0.5f, dest_colour+3 );
+ v3_add( (v3f){0.5f,0.5f,0.5f}, dest_colour+3, dest_colour+3 );
+
+ // Mask
+ dest_colour[6] = 1.f;
+}
+
+void frag_gbuffer_clear( void *dest )
+{
+ float *dest_colour = (float *)dest;
+ dest_colour[0] = 0.f;
+ dest_colour[1] = 0.f;
+ dest_colour[2] = min_height;
+
+ dest_colour[3] = 0.5f;
+ dest_colour[4] = 0.5f;
+ dest_colour[5] = 1.0f;
+
+ dest_colour[6] = 0.f;
+}