X-Git-Url: https://harrygodden.com/git/?p=csRadar.git;a=blobdiff_plain;f=csrDraw.h;h=955ea3080b6cf1e3024838e21b63289ac4d7664b;hp=e0635bbdafabb5d93b2297eb965394d1345bde33;hb=b95752d31a7b1e224b227093f13a72d8c2b7f34b;hpb=a97099abba0a239e20929f04ece9d6839c96ac14 diff --git a/csrDraw.h b/csrDraw.h index e0635bb..955ea30 100644 --- a/csrDraw.h +++ b/csrDraw.h @@ -1,29 +1,82 @@ -#include - typedef struct csr_frag csr_frag; +typedef struct csr_target csr_target; +typedef struct csr_filter csr_filter; struct csr_frag { u32 id; // Triangle index - float qa, qb; // Quantities - float depth; // 'depth testing' + + v3f co; + v3f nrm; +}; + +struct csr_target +{ + csr_frag *fragments; + u32 x, y; + boxf bounds; + float scale; }; -void clear_depth( csr_frag fragments[], u32 x, u32 y ) +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 ) +{ + rt->x = x; + rt->y = y; + rt->fragments = (csr_frag *)csr_malloc( x*y*sizeof(csr_frag) ); + v3_fill( rt->bounds[0], INFINITY ); + v3_fill( rt->bounds[1], -INFINITY ); +} + +void csr_rt_free( csr_target *rt ) +{ + free( rt->fragments ); +} + +void csr_rt_clear( csr_target *rt ) { - for( u32 i = 0; i < x*y; i ++ ) + for( u32 i = 0; i < rt->x*rt->y; i ++ ) { - fragments[ i ].depth = INFINITY; + rt->fragments[ i ].depth = 0.f; } } -void simple_raster( csr_frag fragments[], u32 x, u32 y, v4f cam_bounds, vmf_vert tri[3], int id ) +void csr_auto_fit( csr_target *rt, float padding ) { - // Very simplified tracing algorithm + // 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 + float tqa = 0.f, tqb = 0.f; + v2f bmin = { 0.f, 0.f }; - v2f bmax = { x, y }; + v2f bmax = { rt->x, rt->y }; v2_minv( tri[0].co, tri[1].co, bmin ); v2_minv( tri[2].co, bmin, bmin ); @@ -31,58 +84,202 @@ void simple_raster( csr_frag fragments[], u32 x, u32 y, v4f cam_bounds, vmf_vert v2_maxv( tri[0].co, tri[1].co, bmax ); v2_maxv( tri[2].co, bmax, bmax ); - float range_x = (cam_bounds[2]-cam_bounds[0])/(float)x; - float range_y = (cam_bounds[3]-cam_bounds[1])/(float)y; - - int start_x = csr_max( 0, floorf( (bmin[0]-cam_bounds[0])/range_x)); - int end_x = csr_min( x, floorf( (bmax[0]-cam_bounds[0])/range_x )); - int start_y = csr_max( 0, ceilf( (bmin[1]-cam_bounds[1])/range_y )); - int end_y = csr_min( y, ceilf( (bmax[1]-cam_bounds[1])/range_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][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 }; + 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 ++ ) + for( u32 py = start_y; py <= end_y; py ++ ) { - trace_origin[1] = csr_lerpf( cam_bounds[1], cam_bounds[3], (float)py/(float)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 ++ ) + for( u32 px = start_x; px <= end_x; px ++ ) { - trace_origin[0] = csr_lerpf( cam_bounds[0], cam_bounds[2], (float)px/(float)x ); - - csr_frag *frag = &fragments[ py*y + px ]; + csr_frag *frag = &rt->fragments[ py * rt->y + px ]; - float tqa = 0.f, tqb = 0.f; + 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 ) + if( tdepth > frag->depth ) { frag->depth = tdepth; - frag->id = id; - frag->qa = tqa; - frag->qb = tqb; + + v3_muls( tri[1].co, tqa, frag->co ); + v3_muladds( frag->co, tri[2].co, tqb, frag->co ); + v3_muladds( frag->co, tri[0].co, 1.f - tqa - tqb, frag->co ); } } } } -// First pass 'fragmentize' -void draw_buffers( csr_frag fragments[], u32 x, u32 y, v4f cam_bounds, vmf_vert *triangles, u32 triangle_count ) +void csr_draw( csr_target *rt, vmf_vert *triangles, u32 triangle_count, m4x3f transform ) { - clock_t t; - t = clock(); - printf("Timer starts\n"); - - clear_depth( fragments, x, y ); - + m3x3f normal; + vmf_vert new_tri[3]; + + // Derive normal matrix + m4x3_to_3x3( transform, normal ); + m3x3_inv_transpose( normal, normal ); + for( u32 i = 0; i < triangle_count; i ++ ) { vmf_vert *triangle = triangles + i*3; - simple_raster( fragments, x, y, cam_bounds, triangle, i ); + + m4x3_mulv( transform, triangle[0].co, new_tri[0].co ); + m4x3_mulv( transform, triangle[1].co, new_tri[1].co ); + m4x3_mulv( transform, triangle[2].co, new_tri[2].co ); + m3x3_mulv( normal, triangle[0].nrm, new_tri[0].nrm ); + m3x3_mulv( normal, triangle[1].nrm, new_tri[1].nrm ); + m3x3_mulv( normal, triangle[2].nrm, new_tri[2].nrm ); + + simple_raster( rt, new_tri, 0 ); } +} + +void draw_vmf_group( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *filter, m4x3f prev, m4x3f inst ) +{ + m4x3f transform = M4X3_IDENTITY; + 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, compute_bounds_only = 0; + + if( filter ) + { + if( filter->visgroup ) + { + filter_visgroups = 1; + group_id = vmf_visgroup_id( root, filter->visgroup ); + } + + if( filter->classname ) + { + filter_classname = 1; + } + + compute_bounds_only = filter->compute_bounds_only; + } + + // Multiply previous transform with instance transform to create basis + if( prev ) + { + m4x3_mul( prev, inst, transform ); + } + + // Gather world brushes + solidgen_ctx_init( &solid ); + + if( !filter_classname ) + { + vdf_node *world = vdf_next( root, "world", NULL ); + + vdf_foreach( world, "solid", brush ) + { + if( filter_visgroups && !vmf_visgroup_match( brush, group_id ) ) + continue; + + solidgen_push( &solid, brush ); + } + } + + // Actual entity loop + m4x3f model; + + vdf_foreach( root, "entity", ent ) + { + if( filter_visgroups && !vmf_visgroup_match( ent, group_id ) ) + continue; + + if( filter_classname ) + if( strcmp( kv_get( ent, "classname", "" ), filter->classname ) ) + continue; + + if( ent->user & VMF_FLAG_IS_PROP ) + { + // Create model transform + m4x3_identity( model ); + + vmf_entity_transform( ent, model ); + m4x3_mul( transform, model, model ); + + // Draw model + mdl_mesh_t *mdl = &map->models[ ent->user1 ].mdl; + + 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 i = 0; i < mdl->num_indices/3; i ++ ) + { + 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 ); + } + } + } + else if( ent->user & VMF_FLAG_IS_INSTANCE ) + { + m4x3_identity( model ); + vmf_entity_transform( ent, model ); + + draw_vmf_group( rt, map, map->cache[ ent->user1 ].root, filter, transform, model ); + } + else + { + // Brush entity + if( (ent_solid = vdf_next( ent, "solid", NULL )) ) + { + solidgen_push( &solid, ent_solid ); + } + } + } + + if( compute_bounds_only ) + { + 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("Timer ends \n"); - t = clock() - t; - double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time - printf("Tracing took %f seconds to execute\n", time_taken); + 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] ); }