assorted crap
[carveJwlIkooP6JGAAIwe30JlM.git] / scene.h
1 #ifndef SCENE_H
2 #define SCENE_H
3
4 #include "common.h"
5 #include "model.h"
6 #include "bvh.h"
7 #include "distq.h"
8
9 typedef struct scene scene;
10
11 struct scene
12 {
13 mdl_vert *arrvertices;
14 u32 *arrindices;
15
16 u32 vertex_count, indice_count,
17 max_vertices, max_indices;
18
19 boxf bbx;
20 mdl_submesh submesh;
21 };
22
23 /* Initialize a scene description with bounded buffers */
24 VG_STATIC scene *scene_init( void *lin_alloc, u32 max_verts, u32 max_indices )
25 {
26 u32 vertex_length = max_verts * sizeof(mdl_vert),
27 index_length = max_indices * sizeof(u32),
28 tot_size = sizeof(scene) + vertex_length + index_length;
29
30 scene *pscene = vg_linear_alloc( lin_alloc, tot_size );
31
32 pscene->arrvertices = (mdl_vert *)(pscene+1);
33 pscene->arrindices = (u32 *)( pscene->arrvertices + max_verts );
34
35 pscene->vertex_count = 0;
36 pscene->indice_count = 0;
37 pscene->max_vertices = max_verts;
38 pscene->max_indices = max_indices;
39
40 memset( &pscene->submesh, 0, sizeof(mdl_submesh) );
41
42 v3_fill( pscene->bbx[0], 999999.9f );
43 v3_fill( pscene->bbx[1], -999999.9f );
44
45 return pscene;
46 }
47
48 /*
49 * Append a model into the scene with a given transform
50 */
51 VG_STATIC void scene_add_submesh( scene *pscene, mdl_context *mdl,
52 mdl_submesh *sm, m4x3f transform )
53 {
54 if( pscene->vertex_count + sm->vertex_count > pscene->max_vertices )
55 {
56 vg_error( "%u(current) + %u > %u\n", pscene->vertex_count,
57 sm->vertex_count,
58 pscene->max_vertices );
59
60 vg_warn( "%p ... %p\n", pscene, sm );
61 vg_fatal_exit_loop( "Scene vertex buffer overflow" );
62 }
63
64 if( pscene->indice_count + sm->indice_count > pscene->max_indices )
65 {
66 vg_error( "%u(current) + %u > %u\n", pscene->indice_count,
67 sm->indice_count,
68 pscene->max_indices );
69 vg_warn( "%p ... %p\n", pscene, sm );
70
71 vg_fatal_exit_loop( "Scene index buffer overflow" );
72 }
73
74 mdl_vert *src_verts = mdl_submesh_vertices( mdl, sm ),
75 *dst_verts = &pscene->arrvertices[ pscene->vertex_count ];
76
77 u32 *src_indices = mdl_submesh_indices( mdl, sm ),
78 *dst_indices = &pscene->arrindices[ pscene->indice_count ];
79
80 /* Transform and place vertices */
81 boxf bbxnew;
82 box_copy( sm->bbx, bbxnew );
83 m4x3_transform_aabb( transform, bbxnew );
84 box_concat( pscene->bbx, bbxnew );
85
86 m3x3f normal_matrix;
87 m3x3_copy( transform, normal_matrix );
88 v3_normalize( normal_matrix[0] );
89 v3_normalize( normal_matrix[1] );
90 v3_normalize( normal_matrix[2] );
91
92 for( u32 i=0; i<sm->vertex_count; i++ )
93 {
94 mdl_vert *pvert = &dst_verts[ i ],
95 *src = &src_verts[ i ];
96
97 m4x3_mulv( transform, src->co, pvert->co );
98 m3x3_mulv( normal_matrix, src->norm, pvert->norm );
99
100 pvert->colour[0] = src->colour[0];
101 pvert->colour[1] = src->colour[1];
102 pvert->colour[2] = src->colour[2];
103 pvert->colour[3] = src->colour[3];
104 pvert->weights[0] = src->weights[0];
105 pvert->weights[1] = src->weights[1];
106 pvert->weights[2] = src->weights[2];
107 pvert->weights[3] = src->weights[3];
108 v2_copy( src->uv, pvert->uv );
109 }
110
111 for( u32 i=0; i<sm->indice_count; i++ )
112 {
113 dst_indices[i] = src_indices[i] + pscene->vertex_count;
114 }
115
116 pscene->vertex_count += sm->vertex_count;
117 pscene->indice_count += sm->indice_count;
118
119 }
120
121 /*
122 * One by one adders for simplified access (mostly procedural stuff)
123 */
124 VG_STATIC void scene_push_tri( scene *pscene, u32 tri[3] )
125 {
126 if( pscene->indice_count + 3 > pscene->max_indices )
127 vg_fatal_exit_loop( "Scene vertex buffer overflow" );
128
129 u32 *dst = &pscene->arrindices[ pscene->indice_count ];
130
131 dst[0] = tri[0];
132 dst[1] = tri[1];
133 dst[2] = tri[2];
134
135 pscene->indice_count += 3;
136 }
137
138 VG_STATIC void scene_push_vert( scene *pscene, mdl_vert *v )
139 {
140 if( pscene->vertex_count + 1 > pscene->max_vertices )
141 vg_fatal_exit_loop( "Scene vertex buffer overflow" );
142
143 mdl_vert *dst = &pscene->arrvertices[ pscene->vertex_count ];
144 *dst = *v;
145
146 pscene->vertex_count ++;
147 }
148
149 VG_STATIC void scene_copy_slice( scene *pscene, mdl_submesh *sm )
150 {
151 sm->indice_start = pscene->submesh.indice_start;
152 sm->indice_count = pscene->indice_count - sm->indice_start;
153
154 sm->vertex_start = pscene->submesh.vertex_start;
155 sm->vertex_count = pscene->vertex_count - sm->vertex_start;
156
157 pscene->submesh.indice_start = pscene->indice_count;
158 pscene->submesh.vertex_start = pscene->vertex_count;
159 }
160
161 /* finalization: tightly pack data */
162 __attribute__((warn_unused_result))
163 VG_STATIC scene *scene_fix( void *lin_alloc, scene *pscene )
164 {
165 return pscene;
166 u32 vertex_count = pscene->vertex_count,
167 indice_count = pscene->indice_count,
168 vertex_length = vertex_count * sizeof(mdl_vert),
169 index_length = indice_count * sizeof(u32),
170 tot_size = sizeof(scene) + vertex_length + index_length;
171
172 /* copy down index data */
173 void *dst_indices = pscene->arrvertices + vertex_length;
174 memmove( dst_indices, pscene->arrindices, index_length );
175
176 /* realloc */
177 pscene = vg_linear_resize( lin_alloc, pscene, tot_size );
178
179 pscene->arrvertices = (mdl_vert *)(pscene+1);
180 pscene->arrindices = (u32 *)(pscene->arrvertices+vertex_count);
181 pscene->max_vertices = vertex_count;
182 pscene->max_indices = indice_count;
183
184 return pscene;
185 }
186
187 #if 0
188 /* finalization: delete any offline buffers and reduce size */
189 __attribute__((warn_unused_result))
190 VG_STATIC scene *scene_free_offline_buffers( void *lin_alloc, scene *pscene )
191 {
192 u32 tot_size = sizeof(scene);
193
194 scene *src_scene = pscene;
195 mdl_vert *src_verts = pscene->arrvertices;
196 u32 *src_indices = pscene->arrindices;
197
198 scene *dst_scene = vg_linear_resize( lin_alloc, pscene, tot_size );
199 memcpy( dst_scene, src_scene, sizeof(scene) );
200
201 dst_scene->arrindices = NULL;
202 dst_scene->arrvertices = NULL;
203
204 return dst_scene;
205 }
206 #endif
207
208 VG_STATIC void scene_upload( scene *pscene, glmesh *mesh )
209 {
210 mesh_upload( mesh,
211 pscene->arrvertices, pscene->vertex_count,
212 pscene->arrindices, pscene->indice_count );
213
214 vg_info( "Scene upload\n" );
215 vg_info( " indices:%u\n", pscene->indice_count );
216 vg_info( " verts:%u\n", pscene->vertex_count );
217 }
218
219 /*
220 * BVH implementation
221 */
222
223 VG_STATIC void scene_bh_expand_bound( void *user, boxf bound, u32 item_index )
224 {
225 scene *s = user;
226 mdl_vert *pa = &s->arrvertices[ s->arrindices[item_index*3+0] ],
227 *pb = &s->arrvertices[ s->arrindices[item_index*3+1] ],
228 *pc = &s->arrvertices[ s->arrindices[item_index*3+2] ];
229
230 box_addpt( bound, pa->co );
231 box_addpt( bound, pb->co );
232 box_addpt( bound, pc->co );
233 }
234
235 VG_STATIC float scene_bh_centroid( void *user, u32 item_index, int axis )
236 {
237 scene *s = user;
238 mdl_vert *pa = &s->arrvertices[ s->arrindices[item_index*3+0] ],
239 *pb = &s->arrvertices[ s->arrindices[item_index*3+1] ],
240 *pc = &s->arrvertices[ s->arrindices[item_index*3+2] ];
241
242 return (pa->co[axis] + pb->co[axis] + pc->co[axis]) * (1.0f/3.0f);
243 }
244
245 VG_STATIC void scene_bh_swap( void *user, u32 ia, u32 ib )
246 {
247 scene *s = user;
248
249 u32 *ti = &s->arrindices[ia*3];
250 u32 *tj = &s->arrindices[ib*3];
251
252 u32 temp[3];
253 temp[0] = ti[0];
254 temp[1] = ti[1];
255 temp[2] = ti[2];
256
257 ti[0] = tj[0];
258 ti[1] = tj[1];
259 ti[2] = tj[2];
260
261 tj[0] = temp[0];
262 tj[1] = temp[1];
263 tj[2] = temp[2];
264 }
265
266 VG_STATIC void scene_bh_debug( void *user, u32 item_index )
267 {
268 scene *s = user;
269 u32 idx = item_index*3;
270 mdl_vert *pa = &s->arrvertices[ s->arrindices[ idx+0 ] ],
271 *pb = &s->arrvertices[ s->arrindices[ idx+1 ] ],
272 *pc = &s->arrvertices[ s->arrindices[ idx+2 ] ];
273
274 vg_line( pa->co, pb->co, 0xff0000ff );
275 vg_line( pb->co, pc->co, 0xff0000ff );
276 vg_line( pc->co, pa->co, 0xff0000ff );
277 }
278
279 VG_STATIC int scene_bh_ray( void *user, u32 index, v3f co,
280 v3f dir, ray_hit *hit )
281 {
282 scene *s = user;
283 v3f positions[3];
284
285 u32 *tri = &s->arrindices[ index*3 ];
286
287 for( int i=0; i<3; i++ )
288 v3_copy( s->arrvertices[tri[i]].co, positions[i] );
289
290 float t;
291 if(ray_tri( positions, co, dir, &t ))
292 {
293 if( t < hit->dist )
294 {
295 hit->dist = t;
296 hit->tri = tri;
297 return 1;
298 }
299 }
300
301 return 0;
302 }
303
304 VG_STATIC void scene_bh_closest( void *user, u32 index, v3f point, v3f closest )
305 {
306 scene *s = user;
307
308 v3f positions[3];
309 u32 *tri = &s->arrindices[ index*3 ];
310 for( int i=0; i<3; i++ )
311 v3_copy( s->arrvertices[tri[i]].co, positions[i] );
312
313 closest_on_triangle_1( point, positions, closest );
314 }
315
316 VG_STATIC bh_system bh_system_scene =
317 {
318 .expand_bound = scene_bh_expand_bound,
319 .item_centroid = scene_bh_centroid,
320 .item_closest = scene_bh_closest,
321 .item_swap = scene_bh_swap,
322 .item_debug = scene_bh_debug,
323 .cast_ray = scene_bh_ray
324 };
325
326 /*
327 * An extra step is added onto the end to calculate the hit normal
328 */
329 VG_STATIC int scene_raycast( scene *s, bh_tree *bh,
330 v3f co, v3f dir, ray_hit *hit )
331 {
332 int count = bh_ray( bh, co, dir, hit );
333
334 if( count )
335 {
336 v3f v0, v1;
337
338 float *pa = s->arrvertices[hit->tri[0]].co,
339 *pb = s->arrvertices[hit->tri[1]].co,
340 *pc = s->arrvertices[hit->tri[2]].co;
341
342 v3_sub( pa, pb, v0 );
343 v3_sub( pc, pb, v1 );
344 v3_cross( v1, v0, hit->normal );
345 v3_normalize( hit->normal );
346 v3_muladds( co, dir, hit->dist, hit->pos );
347 }
348
349 return count;
350 }
351
352 VG_STATIC bh_tree *scene_bh_create( void *lin_alloc, scene *s )
353 {
354 u32 triangle_count = s->indice_count / 3;
355 return bh_create( lin_alloc, &bh_system_scene, s, triangle_count, 2 );
356 }
357
358 #endif