fixed instance loading forget to append basepath.. other path fixes (windows)
[csRadar.git] / csrDraw.h
index 97fd8a54553966b80b7f94ac210c6e4001ef5857..a3f601e722575d887b965ccd1865f4f64f86ac8d 100644 (file)
--- a/csrDraw.h
+++ b/csrDraw.h
@@ -44,8 +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 );
 
-// Obsolete
-void csr_rt_save_buffers( csr_target *rt, const char *basename, const char *subname );
+void csr_rt_save_tga( csr_target *rt, const char *path, u32 offset, u32 nc );
+void csr_rt_save_c32f( csr_target *rt, const char *path, u32 offset );
+
 
 // Implementation
 //=======================================================================================================================
@@ -228,22 +229,6 @@ void csr_auto_fit( csr_target *rt, float padding )
        csr_update_subsamples( rt );
 }
 
-void csr_write_txt( char const *path, const char *name, csr_target *rt  )
-{
-       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
@@ -284,7 +269,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 ];
@@ -314,11 +299,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 );
                                }
                        }
@@ -333,8 +319,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 +328,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 +428,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 );
@@ -487,38 +477,94 @@ void csr_vmf_render( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *f
        solidgen_ctx_free( &solid );
 }
 
-// Obsolete
-void csr_rt_save_buffers( csr_target *rt, const char *basename, const char *subname )
+void csr_write_filerr( const char *path )
 {
-       char output[ 512 ];
+       log_error( "File write error (No such file or directory): '%s'\n", path );
+}
+
+void csr_write_txt( char const *path, const char *name, csr_target *rt  )
+{
+       FILE *write_ptr;
        
-       float *image = (float *)csr_malloc( 1024*1024*sizeof(float)*3 );
+       write_ptr = fopen( path, "w" );
+       
+       if( write_ptr )
+       {
+               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 );
+       }
+       else
+       {
+               csr_write_filerr( path );
+       }
+}
+
+// ALWAYS RGB32
+void csr_rt_save_c32f( csr_target *rt, const char *path, u32 offset )
+{
+       float *image = (float *)csr_malloc( rt->x*rt->y*3*sizeof(float) );
        
        float contrib = 1.f/(float)rt->num_samples;
        
-       for( int l = 0; l < rt->x; l ++ )
+       for( int i = 0; i < rt->x*rt->y; i ++ )
        {
-               for( int x = 0; x < rt->y; x ++ )
+               void *src = rt->colour + offset + i * rt->num_samples * rt->shader->stride;
+               float *dst = image + i*3;
+               
+               v3_zero( dst );
+               for( int k = 0; k < rt->num_samples; k ++ )
                {
-                       float *dst = &image[ (l*1024+x)*3 ];
-                       void *src = rt->colour + ((1023-l)*1024+x) * rt->num_samples * rt->shader->stride;
-                       
-                       v3_muls( (float *)src, contrib, dst );
-                       
-                       for( int j = 1; j < rt->num_samples; j ++ )
+                       v3_muladds( dst, (float *)(src + k*rt->shader->stride), contrib, dst );
+               }
+       }
+       
+       if( !csr_32f_write( path, rt->x, rt->y, image ) )
+               csr_write_filerr( path );
+               
+       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 i = 0; i < rt->x*rt->y; i ++ )
+       {
+               void *src = rt->colour + offset + i * rt->num_samples * rt->shader->stride;
+               u8 *dst = image + i*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 ++ )
                        {
-                               v3_muladds( dst, (float *)(src + j*rt->shader->stride), contrib, dst );
+                               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];
        }
        
-       // Save position buffer
-       strcpy( output, basename );
-       strcat( output, "." );
-       strcat( output, subname );
-       strcat( output, "_position.pfm" );
-       csr_32f_write( output, rt->x, rt->y, image );   
-       
+       if( !csr_tga_write( path, rt->x, rt->y, nc, image ) )
+               csr_write_filerr( path );
+               
        free( image );
 }