X-Git-Url: https://harrygodden.com/git/?p=csRadar.git;a=blobdiff_plain;f=csrDraw.h;h=29189a1e9cfd16cc21e10dc6d5367cdcc9627845;hp=30443343d4456e4d71497568672286cc1f88ed3a;hb=1d2752b69b6285d5eb6436728c6cf84228a9ccde;hpb=1bdd02062cda9b107c4d66586d3c036ea15f6ae2 diff --git a/csrDraw.h b/csrDraw.h index 3044334..29189a1 100644 --- a/csrDraw.h +++ b/csrDraw.h @@ -1,35 +1,121 @@ 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 depth; // 'depth testing' - +{ v3f co; v3f nrm; + + float depth; }; struct csr_target { csr_frag *fragments; + u32 x, y; - v4f bounds; + boxf bounds; + float scale; + + v2f subsamples[ 16 ]; + int num_samples; +}; + +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->num_samples = 4; + + rt->fragments = (csr_frag *)csr_malloc( x*y*sizeof(csr_frag)*rt->num_samples ); + + v3_fill( rt->bounds[0], INFINITY ); + v3_fill( rt->bounds[1], -INFINITY ); +} + +void csr_update_subsamples( csr_target *rt ) +{ + float range_x = (rt->bounds[1][0]-rt->bounds[0][0]); + float range_y = (rt->bounds[1][1]-rt->bounds[0][1]); + + v2f pixel_size = { range_x/(float)rt->x, range_y/(float)rt->y }; + + rt->subsamples[0][0] = pixel_size[0] * -0.25f; + rt->subsamples[0][1] = 0.f; + rt->subsamples[1][0] = pixel_size[0] * 0.75f; + rt->subsamples[1][1] = pixel_size[1] * 0.25f; + rt->subsamples[2][0] = 0.f; + rt->subsamples[2][1] = pixel_size[1] * 0.5f; + rt->subsamples[3][0] = pixel_size[0] * 0.5f; + rt->subsamples[3][1] = pixel_size[1] * 0.75f; +} + +void csr_rt_free( csr_target *rt ) +{ + free( rt->fragments ); +} + void csr_rt_clear( csr_target *rt ) { - for( u32 i = 0; i < rt->x*rt->y; i ++ ) + for( u32 i = 0; i < rt->x*rt->y*rt->num_samples; i ++ ) { - rt->fragments[ i ].depth = 0.f; + v3_zero( rt->fragments[ i ].co ); + v3_zero( rt->fragments[ i ].nrm ); + rt->fragments[i].depth = 0.f; } } -void simple_raster( csr_target *rt, vmf_vert tri[3], int id ) +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; + + csr_update_subsamples( rt ); +} + +void csr_write_txt( char const *path, const char *name, csr_target *rt ) { - // Very simplified tracing algorithm - float tqa = 0.f, tqb = 0.f; + FILE *write_ptr; + write_ptr = fopen( path, "w" ); + + fprintf( write_ptr, "\"%s\"\n\{\n", name ); + fprintf( write_ptr, "\t\"material\" \"overviews/%s\"\n", name ); + fprintf( write_ptr, "\t\"pos_x\" \"%.8f\"\n", rt->bounds[0][0] ); + fprintf( write_ptr, "\t\"pos_y\" \"%.8f\"\n", rt->bounds[0][1] ); + fprintf( write_ptr, "\t\"scale\" \"%.8f\"\n", rt->scale / (float)rt->x ); + fprintf( write_ptr, "}\n" ); + + fclose( write_ptr ); +} + +void simple_raster( csr_target *rt, vmf_vert tri[3] ) +{ + // Very very simplified rasterizing algorithm v2f bmin = { 0.f, 0.f }; v2f bmax = { rt->x, rt->y }; @@ -39,35 +125,72 @@ 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][0])/range_x))); + int end_x = csr_max( 0, csr_min( rt->x-1, ceilf( (bmax[0]-rt->bounds[0][0])/range_x))); + int start_y = csr_min( rt->y-1, csr_max( 0, floorf( (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))); - 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 ))); + v2f v0, v1, v2, vp; + float d, bca = 0.f, bcb = 0.f, bcc = 0.f; - v3f trace_dir = { 0.f, 0.f, 1.f }; - v3f trace_origin = { 0.f, 0.f, -16385.f }; + v2_sub( tri[1].co, tri[0].co, v0 ); + v2_sub( tri[2].co, tri[0].co, v1 ); + v2_sub( tri[1].co, tri[2].co, v2 ); + d = 1.f / (v0[0]*v1[1] - v1[0]*v0[1]); + + // Backface culling + if( v2_cross( v0, v1 ) > 0.f ) + return; + + v2f trace_origin; 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 ]; + csr_frag *frag = &rt->fragments[ (py * rt->y + px) * rt->num_samples ]; - trace_origin[0] = csr_lerpf( rt->bounds[0], rt->bounds[2], (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 ); + trace_origin[0] = csr_lerpf( rt->bounds[0][0], rt->bounds[1][0], (float)px/(float)rt->x ); - if( tdepth > frag->depth ) + // Determine coverage + for( int i = 0; i < rt->num_samples; i ++ ) { - frag->depth = tdepth; + v3f sample_origin; + + v2_add( rt->subsamples[ i ], trace_origin, sample_origin ); + v2_sub( sample_origin, tri[0].co, vp ); + + if( v2_cross( v0, vp ) > 0.f ) + continue; + if( v2_cross( vp, v1 ) > 0.f ) + continue; + + v2f vp2; + v2_sub( sample_origin, tri[2].co, vp2 ); + + if( v2_cross( vp2, v2 ) > 0.f ) + continue; + + bcb = (vp[0]*v1[1] - v1[0]*vp[1]) * d; + 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; - 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 ); + if( hit > frag[i].depth ) + { + frag[i].depth = hit; + v3_muls( tri[0].co, bca, frag[i].co ); + v3_muladds( frag[i].co, tri[1].co, bcb, frag[i].co ); + v3_muladds( frag[i].co, tri[2].co, bcc, frag[i].co ); + + // TODO: Same for normal map + } } } } @@ -93,49 +216,71 @@ void csr_draw( csr_target *rt, vmf_vert *triangles, u32 triangle_count, m4x3f tr 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 ); + simple_raster( rt, new_tri ); } } -void draw_vmf_group( csr_target *rt, vmf_map *map, vdf_node *root, int const group, m4x3f prev, m4x3f inst ) +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 ); } - // Draw brushes + // Gather world brushes solidgen_ctx_init( &solid ); - vdf_node *world = vdf_next( root, "world", NULL ); - vdf_foreach( world, "solid", brush ) - { - solidgen_push( &solid, brush ); - } - - for( int i = 0; i < csr_sb_count( solid.indices )/3; i ++ ) + if( !filter_classname ) { - u32 * base = solid.indices + i*3; - - tri[0] = solid.verts[ base[0] ]; - tri[1] = solid.verts[ base[1] ]; - tri[2] = solid.verts[ base[2] ]; + vdf_node *world = vdf_next( root, "world", NULL ); - csr_draw( rt, tri, 1, transform ); + vdf_foreach( world, "solid", brush ) + { + if( filter_visgroups && !vmf_visgroup_match( brush, group_id ) ) + continue; + + solidgen_push( &solid, brush ); + } } - - solidgen_ctx_reset( &solid ); // 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 @@ -146,17 +291,29 @@ void draw_vmf_group( csr_target *rt, vmf_map *map, vdf_node *root, int const gro // 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 ) @@ -164,13 +321,70 @@ void draw_vmf_group( csr_target *rt, vmf_map *map, vdf_node *root, int const gro m4x3_identity( model ); vmf_entity_transform( ent, model ); - draw_vmf_group( rt, map, map->cache[ ent->user1 ].root, group, transform, model ); + draw_vmf_group( rt, map, map->cache[ ent->user1 ].root, filter, transform, model ); } - else if( ent->user & VMF_FLAG_BRUSH_ENT ) + 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 ); } + +void csr_rt_save_buffers( csr_target *rt, const char *basename, const char *subname ) +{ + char output[ 512 ]; + + float *image = (float *)csr_malloc( 1024*1024*sizeof(float)*3 ); + + for( int l = 0; l < rt->x; l ++ ) + { + for( int x = 0; x < rt->y; x ++ ) + { + float *dst = &image[ (l*1024+x)*3 ]; + csr_frag *src = &rt->fragments[ ((1023-l)*1024+x)*rt->num_samples ]; + + v3_zero( dst ); + v3_muls( src[0].co, 1.f/(float)rt->num_samples, dst ); + v3_muladds( dst, src[1].co, 1.f/(float)rt->num_samples, dst ); + v3_muladds( dst, src[2].co, 1.f/(float)rt->num_samples, dst ); + v3_muladds( dst, src[3].co, 1.f/(float)rt->num_samples, dst ); + } + } + + // Save position buffer + strcpy( output, basename ); + strcat( output, "." ); + strcat( output, subname ); + strcat( output, "_position.pfm" ); + csr_32f_write( output, rt->x, rt->y, image ); + + free( image ); +}