From: hgn Date: Sat, 17 Jul 2021 07:39:24 +0000 (+0100) Subject: height filtering X-Git-Url: https://harrygodden.com/git/?a=commitdiff_plain;ds=inline;h=0ee9d77f45e007b119de2d44feef81516aaddaaf;p=csRadar.git height filtering --- diff --git a/build.sh b/build.sh index a898a68..299bf3a 100755 --- 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: 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 diff --git a/csRadar.c b/csRadar.c index 9f05c2b..f79fa3b 100644 --- a/csRadar.c +++ b/csRadar.c @@ -16,6 +16,8 @@ int main( int argc, char *argv[] ) { .padding = 128.f, .resolution = 1024, + .min_z = -INFINITY, + .max_z = INFINITY, .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; } + + 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" ) ) { @@ -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" + " --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" @@ -169,7 +178,7 @@ int main( int argc, char *argv[] ) } if( !extension ) - extension = "csRadarFree"; + extension = "csr_substance"; csr_so ext = csr_libopen( extension ); diff --git a/csRadar.h b/csRadar.h index 4192e55..0415518 100644 --- 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; + float min_z; + float max_z; + // Main API interface vmf_map *map; csr_target target; diff --git a/csrDraw.h b/csrDraw.h index b744c04..fff1590 100644 --- 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; - 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 ); } } diff --git a/ext_csr_free.c b/ext_csr_free.c deleted file mode 100644 index ffec370..0000000 --- a/ext_csr_free.c +++ /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 index 0000000..4dd2627 --- /dev/null +++ b/ext_csr_substance.c @@ -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; +}