955ea3080b6cf1e3024838e21b63289ac4d7664b
[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 struct csr_frag
6 {
7 u32 id; // Triangle index
8 float depth; // 'depth testing'
9
10 v3f co;
11 v3f nrm;
12 };
13
14 struct csr_target
15 {
16 csr_frag *fragments;
17 u32 x, y;
18 boxf bounds;
19 float scale;
20 };
21
22 struct csr_filter
23 {
24 const char *visgroup; // Limit to this visgroup only
25 const char *classname; // Limit to this exact classname. will not draw world
26
27 int compute_bounds_only;
28 };
29
30 void csr_create_target( csr_target *rt, u32 x, u32 y )
31 {
32 rt->x = x;
33 rt->y = y;
34 rt->fragments = (csr_frag *)csr_malloc( x*y*sizeof(csr_frag) );
35 v3_fill( rt->bounds[0], INFINITY );
36 v3_fill( rt->bounds[1], -INFINITY );
37 }
38
39 void csr_rt_free( csr_target *rt )
40 {
41 free( rt->fragments );
42 }
43
44 void csr_rt_clear( csr_target *rt )
45 {
46 for( u32 i = 0; i < rt->x*rt->y; i ++ )
47 {
48 rt->fragments[ i ].depth = 0.f;
49 }
50 }
51
52 void csr_auto_fit( csr_target *rt, float padding )
53 {
54 // Correct aspect ratio to be square
55 float dx, dy, d, l, cx, cy;
56 dx = rt->bounds[1][0] - rt->bounds[0][0];
57 dy = rt->bounds[1][1] - rt->bounds[0][1];
58
59 l = fmaxf( dx, dy );
60 d = l * ( l / dx ) * .5f;
61
62 cx = (rt->bounds[1][0] + rt->bounds[0][0]) * .5f;
63 cy = (rt->bounds[1][1] + rt->bounds[0][1]) * .5f;
64
65 rt->bounds[0][0] = cx - d - padding;
66 rt->bounds[1][0] = cx + d + padding;
67 rt->bounds[0][1] = cy - d - padding;
68 rt->bounds[1][1] = cy + d + padding;
69
70 rt->scale = d + padding;
71 }
72
73 void simple_raster( csr_target *rt, vmf_vert tri[3], int id )
74 {
75 // Very simplified tracing algorithm
76 float tqa = 0.f, tqb = 0.f;
77
78 v2f bmin = { 0.f, 0.f };
79 v2f bmax = { rt->x, rt->y };
80
81 v2_minv( tri[0].co, tri[1].co, bmin );
82 v2_minv( tri[2].co, bmin, bmin );
83
84 v2_maxv( tri[0].co, tri[1].co, bmax );
85 v2_maxv( tri[2].co, bmax, bmax );
86
87 float range_x = (rt->bounds[1][0]-rt->bounds[0][0])/(float)rt->x;
88 float range_y = (rt->bounds[1][1]-rt->bounds[0][1])/(float)rt->y;
89
90 int start_x = csr_min( rt->x-1, csr_max( 0, floorf( (bmin[0]-rt->bounds[0][0])/range_x)));
91 int end_x = csr_max( 0, csr_min( rt->x-1, floorf( (bmax[0]-rt->bounds[0][0])/range_x )));
92 int start_y = csr_min( rt->y-1, csr_max( 0, ceilf( (bmin[1]-rt->bounds[0][1])/range_y )));
93 int end_y = csr_max( 0, csr_min( rt->y-1, ceilf( (bmax[1]-rt->bounds[0][1])/range_y )));
94
95 v3f trace_dir = { 0.f, 0.f, 1.f };
96 v3f trace_origin = { 0.f, 0.f, -16385.f };
97
98 for( u32 py = start_y; py <= end_y; py ++ )
99 {
100 trace_origin[1] = csr_lerpf( rt->bounds[0][1], rt->bounds[1][1], (float)py/(float)rt->y );
101
102 for( u32 px = start_x; px <= end_x; px ++ )
103 {
104 csr_frag *frag = &rt->fragments[ py * rt->y + px ];
105
106 trace_origin[0] = csr_lerpf( rt->bounds[0][0], rt->bounds[1][0], (float)px/(float)rt->x );
107 float tdepth = csr_ray_tri( trace_origin, trace_dir, tri[0].co, tri[1].co, tri[2].co, &tqa, &tqb );
108
109 if( tdepth > frag->depth )
110 {
111 frag->depth = tdepth;
112
113 v3_muls( tri[1].co, tqa, frag->co );
114 v3_muladds( frag->co, tri[2].co, tqb, frag->co );
115 v3_muladds( frag->co, tri[0].co, 1.f - tqa - tqb, frag->co );
116 }
117 }
118 }
119 }
120
121 void csr_draw( csr_target *rt, vmf_vert *triangles, u32 triangle_count, m4x3f transform )
122 {
123 m3x3f normal;
124 vmf_vert new_tri[3];
125
126 // Derive normal matrix
127 m4x3_to_3x3( transform, normal );
128 m3x3_inv_transpose( normal, normal );
129
130 for( u32 i = 0; i < triangle_count; i ++ )
131 {
132 vmf_vert *triangle = triangles + i*3;
133
134 m4x3_mulv( transform, triangle[0].co, new_tri[0].co );
135 m4x3_mulv( transform, triangle[1].co, new_tri[1].co );
136 m4x3_mulv( transform, triangle[2].co, new_tri[2].co );
137 m3x3_mulv( normal, triangle[0].nrm, new_tri[0].nrm );
138 m3x3_mulv( normal, triangle[1].nrm, new_tri[1].nrm );
139 m3x3_mulv( normal, triangle[2].nrm, new_tri[2].nrm );
140
141 simple_raster( rt, new_tri, 0 );
142 }
143 }
144
145 void draw_vmf_group( csr_target *rt, vmf_map *map, vdf_node *root, csr_filter *filter, m4x3f prev, m4x3f inst )
146 {
147 m4x3f transform = M4X3_IDENTITY;
148 vmf_solid solid;
149 vmf_vert tri[3];
150 vdf_node *ent_solid;
151 boxf trf_bounds;
152
153 u32 group_id = 0;
154 int filter_visgroups = 0, filter_classname = 0, compute_bounds_only = 0;
155
156 if( filter )
157 {
158 if( filter->visgroup )
159 {
160 filter_visgroups = 1;
161 group_id = vmf_visgroup_id( root, filter->visgroup );
162 }
163
164 if( filter->classname )
165 {
166 filter_classname = 1;
167 }
168
169 compute_bounds_only = filter->compute_bounds_only;
170 }
171
172 // Multiply previous transform with instance transform to create basis
173 if( prev )
174 {
175 m4x3_mul( prev, inst, transform );
176 }
177
178 // Gather world brushes
179 solidgen_ctx_init( &solid );
180
181 if( !filter_classname )
182 {
183 vdf_node *world = vdf_next( root, "world", NULL );
184
185 vdf_foreach( world, "solid", brush )
186 {
187 if( filter_visgroups && !vmf_visgroup_match( brush, group_id ) )
188 continue;
189
190 solidgen_push( &solid, brush );
191 }
192 }
193
194 // Actual entity loop
195 m4x3f model;
196
197 vdf_foreach( root, "entity", ent )
198 {
199 if( filter_visgroups && !vmf_visgroup_match( ent, group_id ) )
200 continue;
201
202 if( filter_classname )
203 if( strcmp( kv_get( ent, "classname", "" ), filter->classname ) )
204 continue;
205
206 if( ent->user & VMF_FLAG_IS_PROP )
207 {
208 // Create model transform
209 m4x3_identity( model );
210
211 vmf_entity_transform( ent, model );
212 m4x3_mul( transform, model, model );
213
214 // Draw model
215 mdl_mesh_t *mdl = &map->models[ ent->user1 ].mdl;
216
217 if( compute_bounds_only )
218 {
219 box_copy( mdl->bounds, trf_bounds );
220 m4x3_transform_aabb( model, trf_bounds );
221
222 // Join
223 //box_concat( rt->bounds, trf_bounds );
224 }
225 else
226 {
227 for( int i = 0; i < mdl->num_indices/3; i ++ )
228 {
229 for( int j = 0; j < 3; j ++ )
230 {
231 v3_copy( &mdl->vertices[ mdl->indices[ i*3+j ] *8 ], tri[j].co );
232 v3_copy( &mdl->vertices[ mdl->indices[ i*3+j ] *8+3 ], tri[j].nrm );
233 tri[j].xy[0] = 0.f;
234 tri[j].xy[1] = 0.f;
235 }
236
237 csr_draw( rt, tri, 1, model );
238 }
239 }
240 }
241 else if( ent->user & VMF_FLAG_IS_INSTANCE )
242 {
243 m4x3_identity( model );
244 vmf_entity_transform( ent, model );
245
246 draw_vmf_group( rt, map, map->cache[ ent->user1 ].root, filter, transform, model );
247 }
248 else
249 {
250 // Brush entity
251 if( (ent_solid = vdf_next( ent, "solid", NULL )) )
252 {
253 solidgen_push( &solid, ent_solid );
254 }
255 }
256 }
257
258 if( compute_bounds_only )
259 {
260 solidgen_bounds( &solid, trf_bounds );
261 m4x3_transform_aabb( transform, trf_bounds );
262 box_concat( rt->bounds, trf_bounds );
263 }
264 else
265 {
266 // Draw brushes
267 for( int i = 0; i < csr_sb_count( solid.indices )/3; i ++ )
268 {
269 u32 * base = solid.indices + i*3;
270
271 tri[0] = solid.verts[ base[0] ];
272 tri[1] = solid.verts[ base[1] ];
273 tri[2] = solid.verts[ base[2] ];
274
275 csr_draw( rt, tri, 1, transform );
276 }
277 }
278
279 solidgen_ctx_reset( &solid );
280 solidgen_ctx_free( &solid );
281
282 printf( "Bounds resolved to: (%.3f, %.3f, %.3f) -> (%.3f, %.3f, %.3f)\n",
283 rt->bounds[0][0],rt->bounds[0][1],rt->bounds[0][2],
284 rt->bounds[1][0],rt->bounds[1][1],rt->bounds[1][2] );
285 }