height filtering
authorhgn <hgodden00@gmail.com>
Sat, 17 Jul 2021 07:39:24 +0000 (08:39 +0100)
committerhgn <hgodden00@gmail.com>
Sat, 17 Jul 2021 07:39:24 +0000 (08:39 +0100)
build.sh
csRadar.c
csRadar.h
csrDraw.h
ext_csr_free.c [deleted file]
ext_csr_substance.c [new file with mode: 0644]

index a898a6872cb8a2f462cbd2ea26d08e7c45eef2f4..299bf3ab2f95d29180ce28dd8e51451b24b1906c 100755 (executable)
--- a/build.sh
+++ b/build.sh
@@ -18,5 +18,5 @@ mkdir -p ext
 echo build: csRadar
 gcc $flags -rdynamic csRadar.c -o csRadar -lm -ldl
 
 echo build: csRadar
 gcc $flags -rdynamic csRadar.c -o csRadar -lm -ldl
 
-echo build: ext/csRadarFree.so
-gcc $flags -fpic -shared -o ext/csRadarFree.so ext_csr_free.c -lm
+echo build: ext/csr_substance.so
+gcc $flags -fpic -shared -o ext/csr_substance.so ext_csr_substance.c -lm
index 9f05c2b02aaafd6fc105a6182dd82e5b1e13230f..f79fa3b8ef3902113ae975e40bf032204542112f 100644 (file)
--- a/csRadar.c
+++ b/csRadar.c
@@ -16,6 +16,8 @@ int main( int argc, char *argv[] )
        {
                .padding = 128.f,
                .resolution = 1024,
        {
                .padding = 128.f,
                .resolution = 1024,
+               .min_z = -INFINITY,
+               .max_z =  INFINITY,
                .write_txt = 1,
                .api_version = csr_api_version,
                .sampling_mode = k_EMSAA_RGSS
                .write_txt = 1,
                .api_version = csr_api_version,
                .sampling_mode = k_EMSAA_RGSS
@@ -91,6 +93,11 @@ int main( int argc, char *argv[] )
                {
                        extension = arg;
                }
                {
                        extension = arg;
                }
+               
+               if( (arg = csr_long_opt_arg( "min" )) )
+                       api.min_z = atof( arg );
+               if( (arg = csr_long_opt_arg( "max" )) )
+                       api.max_z = atof( arg );
 
                if( csr_opt( 'v' ) || csr_long_opt( "version" ) )
                {
 
                if( csr_opt( 'v' ) || csr_long_opt( "version" ) )
                {
@@ -121,6 +128,8 @@ int main( int argc, char *argv[] )
                                "   --no-txt                       Don't create matching radar txt\n"
                                "   --multi-sample=RGSS            [ none, 2x, rgss, 8r ]\n"
                                "   --extension=TAR                Use an extension binary instead\n"
                                "   --no-txt                       Don't create matching radar txt\n"
                                "   --multi-sample=RGSS            [ none, 2x, rgss, 8r ]\n"
                                "   --extension=TAR                Use an extension binary instead\n"
+                               "   --min=z                        Miniumum height to render\n"
+                               "   --max=z                        Maxiumum height to render\n"
                                "\n"
                                "   -v --version                   Display program version\n"
                                "   -h --help                      Display this help text\n"
                                "\n"
                                "   -v --version                   Display program version\n"
                                "   -h --help                      Display this help text\n"
@@ -169,7 +178,7 @@ int main( int argc, char *argv[] )
                        }
                
                        if( !extension )
                        }
                
                        if( !extension )
-                               extension = "csRadarFree";
+                               extension = "csr_substance";
                
                        csr_so ext = csr_libopen( extension );
                        
                
                        csr_so ext = csr_libopen( extension );
                        
index 4192e555ff13a5f9343d11a210099e9d59c42459..0415518def2d26fd5fdc036b17363d0d26bf936a 100644 (file)
--- a/csRadar.h
+++ b/csRadar.h
@@ -64,6 +64,9 @@ struct csr_api
        char vmf_name[ 128 ];                   // Just the base name eg. my_map
        EMSAA sampling_mode;
        
        char vmf_name[ 128 ];                   // Just the base name eg. my_map
        EMSAA sampling_mode;
        
+       float min_z;
+       float max_z;
+       
        // Main API interface
        vmf_map *map;
        csr_target target;
        // Main API interface
        vmf_map *map;
        csr_target target;
index b744c044e9058ed379c23004bac8d05286bf41c4..fff159094fd7b12bec3dac8e97ca4964eac95583 100644 (file)
--- a/csrDraw.h
+++ b/csrDraw.h
@@ -317,11 +317,12 @@ void simple_raster( csr_target *rt, vmf_vert tri[3] )
                                bcc = (v0[0]*vp[1] - vp[0]*v0[1]) * d;
                                bca = 1.f - bcb - bcc;
                                
                                bcc = (v0[0]*vp[1] - vp[0]*v0[1]) * d;
                                bca = 1.f - bcb - bcc;
                                
-                               float hit = (tri[0].co[2] * bca + tri[1].co[2] * bcb + tri[2].co[2] * bcc) +16385.f;
+                               float hit = tri[0].co[2] * bca + tri[1].co[2] * bcb + tri[2].co[2] * bcc;
+                               float hit_depth = hit + 16385.f;
                                
                                
-                               if( hit > depth[i] )
+                               if( hit_depth > depth[i] && hit >= rt->bounds[0][2] && hit <= rt->bounds[1][2] )
                                {
                                {
-                                       depth[i] = hit;
+                                       depth[i] = hit_depth;
                                        rt->shader->frag( frag+i*rt->shader->stride, tri, bca, bcb, bcc );
                                }
                        }
                                        rt->shader->frag( frag+i*rt->shader->stride, tri, bca, bcb, bcc );
                                }
                        }
diff --git a/ext_csr_free.c b/ext_csr_free.c
deleted file mode 100644 (file)
index ffec370..0000000
+++ /dev/null
@@ -1,129 +0,0 @@
-// 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] );
-       
-       // 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;
-}
diff --git a/ext_csr_substance.c b/ext_csr_substance.c
new file mode 100644 (file)
index 0000000..4dd2627
--- /dev/null
@@ -0,0 +1,132 @@
+// 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;
+}