f2b007fdd92bd13b0f1b65defa3602916e92fe54
[csRadar.git] / csrDraw.h
1 typedef struct csr_frag csr_frag;
2 typedef struct csr_target csr_target;
3 typedef struct csr_filter csr_filter;
4
5 // MSAA patterns
6 v2f csr_msaa_1[] =
7 {
8 {0.f, 0.f}
9 };
10
11 v2f csr_msaa_2x2[] =
12 {
13 { 0.25f, 0.25f },
14 { 0.25f, -0.25f },
15 { -0.25f, -0.25f },
16 { -0.25f, 0.25f }
17 };
18
19 v2f csr_msaa_2x2rgss[] =
20 {
21
22 };
23
24 struct csr_frag
25 {
26 v3f co;
27 v3f nrm;
28
29 float depth;
30 };
31
32 struct csr_target
33 {
34 csr_frag *fragments;
35
36 u32 x, y;
37 boxf bounds;
38 float scale;
39
40 v2f subsamples[ 16 ];
41 int num_samples;
42 };
43
44 struct csr_filter
45 {
46 const char *visgroup; // Limit to this visgroup only
47 const char *classname; // Limit to this exact classname. will not draw world
48
49 int compute_bounds_only;
50 };
51
52 void csr_create_target( csr_target *rt, u32 x, u32 y )
53 {
54 rt->x = x;
55 rt->y = y;
56 rt->num_samples = 4;
57
58 rt->fragments = (csr_frag *)csr_malloc( x*y*sizeof(csr_frag)*rt->num_samples );
59
60 v3_fill( rt->bounds[0], INFINITY );
61 v3_fill( rt->bounds[1], -INFINITY );
62 }
63
64 void csr_update_subsamples( csr_target *rt )
65 {
66 float range_x = (rt->bounds[1][0]-rt->bounds[0][0]);
67 float range_y = (rt->bounds[1][1]-rt->bounds[0][1]);
68
69 v2f pixel_size = { range_x/(float)rt->x, range_y/(float)rt->y };
70
71 rt->subsamples[0][0] = pixel_size[0] * -0.25f;
72 rt->subsamples[0][1] = 0.f;
73 rt->subsamples[1][0] = pixel_size[0] * 0.75f;
74 rt->subsamples[1][1] = pixel_size[1] * 0.25f;
75 rt->subsamples[2][0] = 0.f;
76 rt->subsamples[2][1] = pixel_size[1] * 0.5f;
77 rt->subsamples[3][0] = pixel_size[0] * 0.5f;
78 rt->subsamples[3][1] = pixel_size[1] * 0.75f;
79 }
80
81 void csr_rt_free( csr_target *rt )
82 {
83 free( rt->fragments );
84 }
85
86 void csr_rt_clear( csr_target *rt )
87 {
88 for( u32 i = 0; i < rt->x*rt->y*rt->num_samples; i ++ )
89 {
90 v3_zero( rt->fragments[ i ].co );
91 v3_zero( rt->fragments[ i ].nrm );
92 rt->fragments[i].depth = 0.f;
93 }
94 }
95
96 void csr_auto_fit( csr_target *rt, float padding )
97 {
98 // Correct aspect ratio to be square
99 float dx, dy, d, l, cx, cy;
100 dx = rt->bounds[1][0] - rt->bounds[0][0];
101 dy = rt->bounds[1][1] - rt->bounds[0][1];
102
103 l = fmaxf( dx, dy ) * .5f;
104
105 cx = (rt->bounds[1][0] + rt->bounds[0][0]) * .5f;
106 cy = (rt->bounds[1][1] + rt->bounds[0][1]) * .5f;
107
108 rt->bounds[0][0] = cx - l - padding;
109 rt->bounds[1][0] = cx + l + padding;
110 rt->bounds[0][1] = cy - l - padding;
111 rt->bounds[1][1] = cy + l + padding;
112
113 rt->scale = l + padding;
114
115 csr_update_subsamples( rt );
116 }
117
118 void csr_write_txt( char const *path, const char *name, csr_target *rt )
119 {
120 FILE *write_ptr;
121
122 write_ptr = fopen( path, "w" );
123
124 fprintf( write_ptr, "\"%s\"\n\{\n", name );
125 fprintf( write_ptr, "\t\"material\" \"overviews/%s\"\n", name );
126 fprintf( write_ptr, "\t\"pos_x\" \"%.8f\"\n", rt->bounds[0][0] );
127 fprintf( write_ptr, "\t\"pos_y\" \"%.8f\"\n", rt->bounds[0][1] );
128 fprintf( write_ptr, "\t\"scale\" \"%.8f\"\n", rt->scale / (float)rt->x );
129 fprintf( write_ptr, "}\n" );
130
131 fclose( write_ptr );
132 }
133
134 void simple_raster( csr_target *rt, vmf_vert tri[3] )
135 {
136 // Very very simplified rasterizing algorithm
137 v2f bmin = { 0.f, 0.f };
138 v2f bmax = { rt->x, rt->y };
139
140 v2_minv( tri[0].co, tri[1].co, bmin );
141 v2_minv( tri[2].co, bmin, bmin );
142
143 v2_maxv( tri[0].co, tri[1].co, bmax );
144 v2_maxv( tri[2].co, bmax, bmax );
145
146 float range_x = (rt->bounds[1][0]-rt->bounds[0][0])/(float)rt->x;
147 float range_y = (rt->bounds[1][1]-rt->bounds[0][1])/(float)rt->y;
148
149 int start_x = csr_min( rt->x-1, csr_max( 0, floorf( (bmin[0]-rt->bounds[0][0])/range_x)));
150 int end_x = csr_max( 0, csr_min( rt->x-1, ceilf( (bmax[0]-rt->bounds[0][0])/range_x)));
151 int start_y = csr_min( rt->y-1, csr_max( 0, floorf( (bmin[1]-rt->bounds[0][1])/range_y)));
152 int end_y = csr_max( 0, csr_min( rt->y-1, ceilf( (bmax[1]-rt->bounds[0][1])/range_y)));
153
154 v2f v0, v1, v2, vp;
155 float d, bca = 0.f, bcb = 0.f, bcc = 0.f;
156
157 v2_sub( tri[1].co, tri[0].co, v0 );
158 v2_sub( tri[2].co, tri[0].co, v1 );
159 v2_sub( tri[1].co, tri[2].co, v2 );
160 d = 1.f / (v0[0]*v1[1] - v1[0]*v0[1]);
161
162 // Backface culling
163 if( v2_cross( v0, v1 ) > 0.f )
164 return;
165
166 v2f trace_origin;
167
168 for( u32 py = start_y; py <= end_y; py ++ )
169 {
170 trace_origin[1] = csr_lerpf( rt->bounds[0][1], rt->bounds[1][1], (float)py/(float)rt->y );
171
172 for( u32 px = start_x; px <= end_x; px ++ )
173 {
174 csr_frag *frag = &rt->fragments[ (py * rt->y + px) * rt->num_samples ];
175
176 trace_origin[0] = csr_lerpf( rt->bounds[0][0], rt->bounds[1][0], (float)px/(float)rt->x );
177
178 // Determine coverage
179 for( int i = 0; i < rt->num_samples; i ++ )
180 {
181 v3f sample_origin;
182
183 v2_add( rt->subsamples[ i ], trace_origin, sample_origin );
184 v2_sub( sample_origin, tri[0].co, vp );
185
186 if( v2_cross( v0, vp ) > 0.f )
187 continue;
188 if( v2_cross( vp, v1 ) > 0.f )
189 continue;
190
191 v2f vp2;
192 v2_sub( sample_origin, tri[2].co, vp2 );
193
194 if( v2_cross( vp2, v2 ) > 0.f )
195 continue;
196
197 bcb = (vp[0]*v1[1] - v1[0]*vp[1]) * d;
198 bcc = (v0[0]*vp[1] - vp[0]*v0[1]) * d;
199 bca = 1.f - bcb - bcc;
200
201 float hit = (tri[0].co[2] * bca + tri[1].co[2] * bcb + tri[2].co[2] * bcc) +16385.f;
202
203 if( hit > frag[i].depth )
204 {
205 frag[i].depth = hit;
206 v3_muls( tri[0].co, bca, frag[i].co );
207 v3_muladds( frag[i].co, tri[1].co, bcb, frag[i].co );
208 v3_muladds( frag[i].co, tri[2].co, bcc, frag[i].co );
209
210 // TODO: Same for normal map
211 }
212 }
213 }
214 }
215 }
216
217 void csr_draw( csr_target *rt, vmf_vert *triangles, u32 triangle_count, m4x3f transform )
218 {
219 m3x3f normal;
220 vmf_vert new_tri[3];
221
222 // Derive normal matrix
223 m4x3_to_3x3( transform, normal );
224
225 // NOTE: This isn't strictly necessary since CS:GO only uses uniform scaling.
226 m3x3_inv_transpose( normal, normal );
227
228 for( u32 i = 0; i < triangle_count; i ++ )
229 {
230 vmf_vert *triangle = triangles + i*3;
231
232 m4x3_mulv( transform, triangle[0].co, new_tri[0].co );
233 m4x3_mulv( transform, triangle[1].co, new_tri[1].co );
234 m4x3_mulv( transform, triangle[2].co, new_tri[2].co );
235 m3x3_mulv( normal, triangle[0].nrm, new_tri[0].nrm );
236 m3x3_mulv( normal, triangle[1].nrm, new_tri[1].nrm );
237 m3x3_mulv( normal, triangle[2].nrm, new_tri[2].nrm );
238
239 simple_raster( rt, new_tri );
240 }
241 }
242
243 void draw_vmf_group( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *filter, m4x3f prev, m4x3f inst )
244 {
245 m4x3f transform = M4X3_IDENTITY;
246 vmf_solid solid;
247 vmf_vert tri[3];
248 boxf trf_bounds;
249
250 u32 group_id = 0;
251 int filter_visgroups = 0, filter_classname = 0, compute_bounds_only = 0;
252
253 if( filter )
254 {
255 if( filter->visgroup )
256 {
257 filter_visgroups = 1;
258 group_id = vmf_visgroup_id( root, filter->visgroup );
259 }
260
261 if( filter->classname )
262 {
263 filter_classname = 1;
264 }
265
266 compute_bounds_only = filter->compute_bounds_only;
267 }
268
269 // Multiply previous transform with instance transform to create basis
270 if( prev )
271 {
272 m4x3_mul( prev, inst, transform );
273 }
274
275 // Gather world brushes
276 solidgen_ctx_init( &solid );
277
278 if( !filter_classname )
279 {
280 vdf_node *world = vdf_next( root, "world", NULL );
281
282 vdf_foreach( world, "solid", brush )
283 {
284 if( filter_visgroups && !vmf_visgroup_match( brush, group_id ) )
285 continue;
286
287 // TODO: heap-use-after-free
288 solidgen_push( &solid, brush );
289 }
290 }
291
292 // Actual entity loop
293 m4x3f model;
294
295 vdf_foreach( root, "entity", ent )
296 {
297 if( filter_visgroups && !vmf_visgroup_match( ent, group_id ) )
298 continue;
299
300 if( filter_classname )
301 if( strcmp( kv_get( ent, "classname", "" ), filter->classname ) )
302 continue;
303
304 if( ent->user & VMF_FLAG_IS_PROP )
305 {
306 // Create model transform
307 m4x3_identity( model );
308
309 vmf_entity_transform( ent, model );
310 m4x3_mul( transform, model, model );
311
312 // Draw model
313 mdl_mesh_t *mdl = &map->models[ ent->user1 ].mdl;
314
315 if( compute_bounds_only )
316 {
317 box_copy( mdl->bounds, trf_bounds );
318 m4x3_transform_aabb( model, trf_bounds );
319
320 // Join
321 box_concat( rt->bounds, trf_bounds );
322 }
323 else
324 {
325 for( int i = 0; i < mdl->num_indices/3; i ++ )
326 {
327 for( int j = 0; j < 3; j ++ )
328 {
329 v3_copy( &mdl->vertices[ mdl->indices[ i*3+j ] *8 ], tri[j].co );
330 v3_copy( &mdl->vertices[ mdl->indices[ i*3+j ] *8+3 ], tri[j].nrm );
331 tri[j].xy[0] = 0.f;
332 tri[j].xy[1] = 0.f;
333 }
334
335 csr_draw( rt, tri, 1, model );
336 }
337 }
338 }
339 else if( ent->user & VMF_FLAG_IS_INSTANCE )
340 {
341 m4x3_identity( model );
342 vmf_entity_transform( ent, model );
343
344 draw_vmf_group( rt, map, map->cache[ ent->user1 ].root, filter, transform, model );
345 }
346 else
347 {
348 // Brush entity
349 vdf_foreach( ent, "solid", ent_solid )
350 {
351 solidgen_push( &solid, ent_solid );
352 }
353 }
354 }
355
356 if( compute_bounds_only )
357 {
358 solidgen_bounds( &solid, trf_bounds );
359 m4x3_transform_aabb( transform, trf_bounds );
360 box_concat( rt->bounds, trf_bounds );
361 }
362 else
363 {
364 // Draw brushes
365 for( int i = 0; i < csr_sb_count( solid.indices )/3; i ++ )
366 {
367 u32 * base = solid.indices + i*3;
368
369 tri[0] = solid.verts[ base[0] ];
370 tri[1] = solid.verts[ base[1] ];
371 tri[2] = solid.verts[ base[2] ];
372
373 csr_draw( rt, tri, 1, transform );
374 }
375 }
376
377 solidgen_ctx_reset( &solid );
378 solidgen_ctx_free( &solid );
379 }
380
381 void csr_rt_save_buffers( csr_target *rt, const char *basename, const char *subname )
382 {
383 char output[ 512 ];
384
385 float *image = (float *)csr_malloc( 1024*1024*sizeof(float)*3 );
386
387 for( int l = 0; l < rt->x; l ++ )
388 {
389 for( int x = 0; x < rt->y; x ++ )
390 {
391 float *dst = &image[ (l*1024+x)*3 ];
392 csr_frag *src = &rt->fragments[ ((1023-l)*1024+x)*rt->num_samples ];
393
394 v3_zero( dst );
395 v3_muls( src[0].co, 1.f/(float)rt->num_samples, dst );
396 v3_muladds( dst, src[1].co, 1.f/(float)rt->num_samples, dst );
397 v3_muladds( dst, src[2].co, 1.f/(float)rt->num_samples, dst );
398 v3_muladds( dst, src[3].co, 1.f/(float)rt->num_samples, dst );
399 }
400 }
401
402 // Save position buffer
403 strcpy( output, basename );
404 strcat( output, "." );
405 strcat( output, subname );
406 strcat( output, "_position.pfm" );
407 csr_32f_write( output, rt->x, rt->y, image );
408
409 free( image );
410 }