From b95752d31a7b1e224b227093f13a72d8c2b7f34b Mon Sep 17 00:00:00 2001 From: hgn Date: Sun, 11 Jul 2021 04:55:05 +0100 Subject: [PATCH] cropping --- csRadar.c | 14 ++++++- csrDraw.h | 111 +++++++++++++++++++++++++++++++++++++++-------------- csrMath.h | 39 +++++++++++++++++++ csrTypes.h | 1 + vmdl.h | 7 +++- vmf.h | 19 +++------ 6 files changed, 145 insertions(+), 46 deletions(-) diff --git a/csRadar.c b/csRadar.c index 909a645..43b9c0e 100644 --- a/csRadar.c +++ b/csRadar.c @@ -40,7 +40,7 @@ int main( int argc, char *argv[] ) csr_target target; - csr_create_target( &target, 1024, 1024, (v4f){ -1000.f, -1000.f, 1000.f, 1000.f } ); + csr_create_target( &target, 1024, 1024 ); csr_rt_clear( &target ); csr_filter filter_layout = @@ -55,7 +55,17 @@ int main( int argc, char *argv[] ) .visgroup = NULL }; - draw_vmf_group( &target, map, map->root, &filter_buyzone, NULL, NULL ); + csr_filter filter_setup = + { + .classname = NULL, + .visgroup = NULL, + .compute_bounds_only = 1 + }; + + draw_vmf_group( &target, map, map->root, &filter_setup, NULL, NULL ); + csr_auto_fit( &target, 128.f ); + + draw_vmf_group( &target, map, map->root, NULL, NULL, NULL ); float *rgba_test = (float *)csr_malloc( 1024*1024*sizeof(float)*3 ); diff --git a/csrDraw.h b/csrDraw.h index b6e17ca..955ea30 100644 --- a/csrDraw.h +++ b/csrDraw.h @@ -15,21 +15,25 @@ struct csr_target { csr_frag *fragments; u32 x, y; - v4f bounds; + boxf bounds; + float scale; }; struct csr_filter { const char *visgroup; // Limit to this visgroup only const char *classname; // Limit to this exact classname. will not draw world + + int compute_bounds_only; }; -void csr_create_target( csr_target *rt, u32 x, u32 y, v4f bounds ) +void csr_create_target( csr_target *rt, u32 x, u32 y ) { rt->x = x; rt->y = y; rt->fragments = (csr_frag *)csr_malloc( x*y*sizeof(csr_frag) ); - v4_copy( bounds, rt->bounds ); + v3_fill( rt->bounds[0], INFINITY ); + v3_fill( rt->bounds[1], -INFINITY ); } void csr_rt_free( csr_target *rt ) @@ -45,6 +49,27 @@ void csr_rt_clear( csr_target *rt ) } } +void csr_auto_fit( csr_target *rt, float padding ) +{ + // Correct aspect ratio to be square + float dx, dy, d, l, cx, cy; + dx = rt->bounds[1][0] - rt->bounds[0][0]; + dy = rt->bounds[1][1] - rt->bounds[0][1]; + + l = fmaxf( dx, dy ); + d = l * ( l / dx ) * .5f; + + cx = (rt->bounds[1][0] + rt->bounds[0][0]) * .5f; + cy = (rt->bounds[1][1] + rt->bounds[0][1]) * .5f; + + rt->bounds[0][0] = cx - d - padding; + rt->bounds[1][0] = cx + d + padding; + rt->bounds[0][1] = cy - d - padding; + rt->bounds[1][1] = cy + d + padding; + + rt->scale = d + padding; +} + void simple_raster( csr_target *rt, vmf_vert tri[3], int id ) { // Very simplified tracing algorithm @@ -59,26 +84,26 @@ void simple_raster( csr_target *rt, vmf_vert tri[3], int id ) v2_maxv( tri[0].co, tri[1].co, bmax ); v2_maxv( tri[2].co, bmax, bmax ); - float range_x = (rt->bounds[2]-rt->bounds[0])/(float)rt->x; - float range_y = (rt->bounds[3]-rt->bounds[1])/(float)rt->y; + float range_x = (rt->bounds[1][0]-rt->bounds[0][0])/(float)rt->x; + float range_y = (rt->bounds[1][1]-rt->bounds[0][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 ))); + int start_x = csr_min( rt->x-1, csr_max( 0, floorf( (bmin[0]-rt->bounds[0][0])/range_x))); + int end_x = csr_max( 0, csr_min( rt->x-1, floorf( (bmax[0]-rt->bounds[0][0])/range_x ))); + int start_y = csr_min( rt->y-1, csr_max( 0, ceilf( (bmin[1]-rt->bounds[0][1])/range_y ))); + int end_y = csr_max( 0, csr_min( rt->y-1, ceilf( (bmax[1]-rt->bounds[0][1])/range_y ))); 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 ++ ) { - trace_origin[1] = csr_lerpf( rt->bounds[1], rt->bounds[3], (float)py/(float)rt->y ); + trace_origin[1] = csr_lerpf( rt->bounds[0][1], rt->bounds[1][1], (float)py/(float)rt->y ); for( u32 px = start_x; px <= end_x; px ++ ) { csr_frag *frag = &rt->fragments[ py * rt->y + px ]; - trace_origin[0] = csr_lerpf( rt->bounds[0], rt->bounds[2], (float)px/(float)rt->x ); + trace_origin[0] = csr_lerpf( rt->bounds[0][0], rt->bounds[1][0], (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 ); if( tdepth > frag->depth ) @@ -123,9 +148,10 @@ void draw_vmf_group( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *f vmf_solid solid; vmf_vert tri[3]; vdf_node *ent_solid; + boxf trf_bounds; u32 group_id = 0; - int filter_visgroups = 0, filter_classname = 0; + int filter_visgroups = 0, filter_classname = 0, compute_bounds_only = 0; if( filter ) { @@ -139,6 +165,8 @@ void draw_vmf_group( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *f { filter_classname = 1; } + + compute_bounds_only = filter->compute_bounds_only; } // Multiply previous transform with instance transform to create basis @@ -185,17 +213,29 @@ void draw_vmf_group( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *f // Draw model mdl_mesh_t *mdl = &map->models[ ent->user1 ].mdl; - for( int i = 0; i < mdl->num_indices/3; i ++ ) + + if( compute_bounds_only ) + { + box_copy( mdl->bounds, trf_bounds ); + m4x3_transform_aabb( model, trf_bounds ); + + // Join + //box_concat( rt->bounds, trf_bounds ); + } + else { - for( int j = 0; j < 3; j ++ ) + for( int i = 0; i < mdl->num_indices/3; i ++ ) { - 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; + 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 ); } - - csr_draw( rt, tri, 1, model ); } } else if( ent->user & VMF_FLAG_IS_INSTANCE ) @@ -215,18 +255,31 @@ void draw_vmf_group( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *f } } - // Draw brushes - for( int i = 0; i < csr_sb_count( solid.indices )/3; i ++ ) + if( compute_bounds_only ) { - 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_bounds( &solid, trf_bounds ); + m4x3_transform_aabb( transform, trf_bounds ); + box_concat( rt->bounds, trf_bounds ); + } + else + { + // Draw brushes + 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 ); solidgen_ctx_free( &solid ); + + printf( "Bounds resolved to: (%.3f, %.3f, %.3f) -> (%.3f, %.3f, %.3f)\n", + rt->bounds[0][0],rt->bounds[0][1],rt->bounds[0][2], + rt->bounds[1][0],rt->bounds[1][1],rt->bounds[1][2] ); } diff --git a/csrMath.h b/csrMath.h index d9a414f..fae268d 100644 --- a/csrMath.h +++ b/csrMath.h @@ -360,6 +360,45 @@ void m4x3_rotate_z( m4x3f m, float angle ) m4x3_mul( m, t, m ); } +// Warning: These functions are unoptimized.. +void m4x3_expand_aabb_point( m4x3f m, boxf box, v3f point ) +{ + v3f v; + m4x3_mulv( m, point, v ); + + v3_minv( box[0], v, box[0] ); + v3_maxv( box[1], v, box[1] ); +} + +void box_concat( boxf a, boxf b ) +{ + v3_minv( a[0], b[0], a[0] ); + v3_maxv( a[1], b[1], a[1] ); +} + +void box_copy( boxf a, boxf b ) +{ + v3_copy( a[0], b[0] ); + v3_copy( a[1], b[1] ); +} + +void m4x3_transform_aabb( m4x3f m, boxf box ) +{ + v3f a; v3f b; + + v3_copy( box[0], a ); + v3_copy( box[1], b ); + + m4x3_expand_aabb_point( m, box, a ); + m4x3_expand_aabb_point( m, box, (v3f){ a[0], b[1], a[2] } ); + m4x3_expand_aabb_point( m, box, (v3f){ b[0], a[1], a[2] } ); + m4x3_expand_aabb_point( m, box, (v3f){ b[0], b[1], a[2] } ); + m4x3_expand_aabb_point( m, box, b ); + m4x3_expand_aabb_point( m, box, (v3f){ a[0], b[1], b[2] } ); + m4x3_expand_aabb_point( m, box, (v3f){ b[0], a[1], b[2] } ); + m4x3_expand_aabb_point( m, box, (v3f){ b[0], b[1], b[2] } ); +} + // Planes (double precision) // ================================================================================================================== diff --git a/csrTypes.h b/csrTypes.h index 3aafcba..aec727e 100644 --- a/csrTypes.h +++ b/csrTypes.h @@ -14,3 +14,4 @@ typedef float v3f[3]; typedef float v4f[4]; typedef v3f m3x3f[3]; typedef v3f m4x3f[4]; +typedef v3f boxf[2]; diff --git a/vmdl.h b/vmdl.h index c56266a..59d6acb 100644 --- a/vmdl.h +++ b/vmdl.h @@ -461,8 +461,8 @@ typedef struct float *vertices; u32 num_vertices; - v3f mins; - v3f maxs; + // Bounding box + boxf bounds; } mdl_mesh_t; @@ -523,6 +523,9 @@ int mdl_from_find_files( const char *mdlname, mdl_mesh_t *ctx ) return 0; } + v3_copy( pMdl->view_bbmin, ctx->bounds[0] ); + v3_copy( pMdl->view_bbmax, ctx->bounds[1] ); + ctx->num_indices = vtx_count_indices( pVtxHdr ); // Allocate and read indices diff --git a/vmf.h b/vmf.h index ea91d17..d2bbbd2 100644 --- a/vmf.h +++ b/vmf.h @@ -101,27 +101,20 @@ void solidgen_ctx_free( vmf_solid *ctx ) } // Compute bounds of solid gen ctx -void solidgen_bounds( vmf_solid *ctx, u32 start, u32 end, v3f min, v3f max ) +void solidgen_bounds( vmf_solid *ctx, boxf box ) { v3f mine = { INFINITY, INFINITY, INFINITY }; v3f maxe = {-INFINITY,-INFINITY,-INFINITY }; - for( int i = start; i < end; i ++ ) + for( int i = 0; i < csr_sb_count( ctx->verts ); i ++ ) { vmf_vert *vert = ctx->verts + i; - float *co = vert->co; - - mine[0] = fminf( mine[0], co[0] ); - mine[1] = fminf( mine[1], co[1] ); - mine[2] = fminf( mine[2], co[2] ); - - maxe[0] = fmaxf( maxe[0], co[0] ); - maxe[1] = fmaxf( maxe[1], co[1] ); - maxe[2] = fmaxf( maxe[2], co[2] ); + v3_minv( mine, vert->co, mine ); + v3_maxv( maxe, vert->co, maxe ); } - v3_copy( mine, min ); - v3_copy( maxe, max ); + v3_copy( mine, box[0] ); + v3_copy( maxe, box[1] ); } struct -- 2.25.1