ffec370b0a02cb76c21e4e0b4e7a1a47ec68e544
[csRadar.git] / ext_csr_free.c
1 // Copyright (C) 2021 Harry Godden (hgn)
2
3 // Basic buffers-only version of csRadar
4 //=======================================================================================================================
5
6 #include "csRadar.h"
7
8 float min_height = 0.f;
9 v4f remapping_range;
10
11 // GBuffer shader
12 void frag_gbuffer( void *dest, vmf_vert tri[3], float bca, float bcb, float bcc );
13 void frag_gbuffer_clear( void *dest );
14
15 csr_shader shader_gbuffer =
16 {
17 .stride = sizeof(float)*8, // (origin) x,y, (actual height) z
18 .frag = frag_gbuffer,
19 .clear = frag_gbuffer_clear
20 };
21
22 // Main drawing function
23 void draw_buffers( csr_api *api, int bounds_only );
24
25 // Extension implementation
26 // =========================================================================================================
27
28 // API ENTRY
29 void csr_ext_main( csr_api *api )
30 {
31 if( !csr_init( api ) )
32 return;
33
34 csr_create_target( &api->target, api->resolution, api->resolution, api->sampling_mode, &shader_gbuffer );
35 csr_rt_clear( &api->target );
36
37 // Compute bounds, collect models
38 draw_buffers( api, 1 );
39 csr_auto_fit( &api->target, api->padding );
40 vmf_load_models( api->map );
41
42 min_height = api->target.bounds[0][2];
43 v2_copy( api->target.bounds[0], remapping_range );
44 v2_sub( api->target.bounds[1], api->target.bounds[0], &remapping_range[2] );
45
46 // Draw everything
47 draw_buffers( api, 0 );
48 }
49
50 void csr_ext_exit( csr_api *api )
51 {
52 csr_rt_free( &api->target );
53 }
54
55 void draw_buffers( csr_api *api, int bounds_only )
56 {
57 csr_filter filter = { .compute_bounds_only = bounds_only };
58 vmf_map *map = api->map;
59
60 if( api->num_strings == 1 )
61 {
62 // Draw everything
63 csr_vmf_render( &api->target, map, map->root, &filter, NULL, NULL );
64
65 if( !bounds_only )
66 {
67 csr_rt_save_buffers( &api->target, api->output_path, "all" );
68 csr_rt_save_tga( &api->target, "test_test_test.tga", 3*sizeof(float), 4 );
69 }
70 }
71 else
72 {
73 // Draw groups
74 for( int i = 1; i < api->num_strings; i ++ )
75 {
76 csr_filter_update_from_strings( &filter, &api->strings[i] );
77
78 csr_vmf_render( &api->target, map, map->root, &filter, NULL, NULL );
79
80 if( !bounds_only )
81 {
82 csr_rt_save_buffers( &api->target, api->output_path, api->strings[i].str );
83 //csr_rt_save_c32f( ... );
84 //csr_rt_save_tga( ... );
85
86 // tar_write_dds( ... );
87 csr_rt_save_tga( &api->target, "test_test_test.tga", 3*sizeof(float), 4 );
88
89 csr_rt_clear( &api->target );
90 }
91 }
92 }
93 }
94
95 void frag_gbuffer( void *dest, vmf_vert tri[3], float bca, float bcb, float bcc )
96 {
97 float *dest_colour = (float *)dest;
98
99 // Position
100 v2_sub( tri[0].origin, remapping_range, dest_colour );
101 v2_div( dest_colour, &remapping_range[2], dest_colour );
102
103 dest_colour[2] = tri[0].co[2]*bca + tri[1].co[2]*bcb + tri[2].co[2]*bcc;
104
105 // Normals
106 v3_muls( tri[0].nrm, bca, dest_colour+3 );
107 v3_muladds( dest_colour+3, tri[1].nrm, bcb, dest_colour+3 );
108 v3_muladds( dest_colour+3, tri[2].nrm, bcc, dest_colour+3 );
109
110 v3_muls( dest_colour+3, 0.5f, dest_colour+3 );
111 v3_add( (v3f){0.5f,0.5f,0.5f}, dest_colour+3, dest_colour+3 );
112
113 // Mask
114 dest_colour[6] = 1.f;
115 }
116
117 void frag_gbuffer_clear( void *dest )
118 {
119 float *dest_colour = (float *)dest;
120 dest_colour[0] = 0.f;
121 dest_colour[1] = 0.f;
122 dest_colour[2] = min_height;
123
124 dest_colour[3] = 0.5f;
125 dest_colour[4] = 0.5f;
126 dest_colour[5] = 1.0f;
127
128 dest_colour[6] = 0.f;
129 }