X-Git-Url: https://harrygodden.com/git/?p=csRadar.git;a=blobdiff_plain;f=csrDraw.h;fp=csrDraw.h;h=b744c044e9058ed379c23004bac8d05286bf41c4;hp=97fd8a54553966b80b7f94ac210c6e4001ef5857;hb=dee56773cecd3a165331732008b7c0acf6f13393;hpb=abfc6360542cb480122313a36f6ed02c08074ed5 diff --git a/csrDraw.h b/csrDraw.h index 97fd8a5..b744c04 100644 --- a/csrDraw.h +++ b/csrDraw.h @@ -44,6 +44,9 @@ void csr_draw( csr_target *rt, vmf_vert *triangles, u32 triangle_count, m4x3f tr // Filter is optional, it can be st to NULL to just render everything. void csr_vmf_render( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *filter, m4x3f prev, m4x3f inst ); + +void csr_rt_save_tga( csr_target *rt, const char *path, u32 offset, u32 nc ); + // Obsolete void csr_rt_save_buffers( csr_target *rt, const char *basename, const char *subname ); @@ -284,7 +287,7 @@ void simple_raster( csr_target *rt, vmf_vert tri[3] ) for( u32 px = start_x; px <= end_x; px ++ ) { - u32 sample_index = (py * rt->y + px) * rt->num_samples; + u32 sample_index = ((rt->y-py-1)*rt->x+px) * rt->num_samples; void *frag = rt->colour + sample_index*rt->shader->stride; float *depth = &rt->depth[ sample_index ]; @@ -333,8 +336,6 @@ void csr_draw( csr_target *rt, vmf_vert *triangles, u32 triangle_count, m4x3f tr // Derive normal matrix m4x3_to_3x3( transform, normal ); - - // NOTE: This isn't strictly necessary since CS:GO only uses uniform scaling. m3x3_inv_transpose( normal, normal ); for( u32 i = 0; i < triangle_count; i ++ ) @@ -344,10 +345,17 @@ void csr_draw( csr_target *rt, vmf_vert *triangles, u32 triangle_count, m4x3f tr 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 ); + v3_normalize( new_tri[0].nrm ); + v3_normalize( new_tri[1].nrm ); + v3_normalize( new_tri[2].nrm ); + + m4x3_mulv( transform, triangles[0].origin, new_tri[0].origin ); + simple_raster( rt, new_tri ); } } @@ -437,8 +445,7 @@ void csr_vmf_render( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *f { 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; + v3_zero( tri[j].origin ); } csr_draw( rt, tri, 1, model ); @@ -522,4 +529,45 @@ void csr_rt_save_buffers( csr_target *rt, const char *basename, const char *subn free( image ); } +// Save floating point buffer to tga. Must be in range (0-1) +// Offset and stride are in bytes +void csr_rt_save_tga( csr_target *rt, const char *path, u32 offset, u32 nc ) +{ + u8 *image = (u8 *)csr_malloc( rt->x*rt->y * 4 ); + + float contrib = 255.f/(float)rt->num_samples; + + for( int y = 0; y < rt->y; y ++ ) + { + for( int x = 0; x < rt->x; x ++ ) + { + u32 pixel_index = (y*rt->x + x); + + void *src = rt->colour + offset + pixel_index * rt->num_samples * rt->shader->stride; + u8 *dst = image + pixel_index*4; + + v4f accum = { 0.f, 0.f, 0.f, 0.f }; + + for( int k = 0; k < rt->num_samples; k ++ ) + { + float *src_sample = (float *)(src + k*rt->shader->stride); + + for( int j = 0; j < nc; j ++ ) + { + accum[ j ] += src_sample[ j ] * contrib; + } + } + + // TODO: Clamp this value + dst[0] = accum[0]; + dst[1] = accum[1]; + dst[2] = accum[2]; + dst[3] = accum[3]; + } + } + + csr_tga_write( path, rt->x, rt->y, nc, image ); + free( image ); +} + #endif