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