well yeah i guess
[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 pscene->submesh.indice_start = 0;
40 pscene->submesh.indice_count = 0;
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 v2_copy( src->uv, pvert->uv );
105 }
106
107 for( u32 i=0; i<sm->indice_count; i++ )
108 {
109 dst_indices[i] = src_indices[i] + pscene->vertex_count;
110 }
111
112 pscene->vertex_count += sm->vertex_count;
113 pscene->indice_count += sm->indice_count;
114
115 }
116
117 /*
118 * One by one adders for simplified access (mostly procedural stuff)
119 */
120 VG_STATIC void scene_push_tri( scene *pscene, u32 tri[3] )
121 {
122 if( pscene->indice_count + 3 > pscene->max_indices )
123 vg_fatal_exit_loop( "Scene vertex buffer overflow" );
124
125 u32 *dst = &pscene->arrindices[ pscene->indice_count ];
126
127 dst[0] = tri[0];
128 dst[1] = tri[1];
129 dst[2] = tri[2];
130
131 pscene->indice_count += 3;
132 }
133
134 VG_STATIC void scene_push_vert( scene *pscene, mdl_vert *v )
135 {
136 if( pscene->vertex_count + 1 > pscene->max_vertices )
137 vg_fatal_exit_loop( "Scene vertex buffer overflow" );
138
139 mdl_vert *dst = &pscene->arrvertices[ pscene->vertex_count ];
140 *dst = *v;
141
142 pscene->vertex_count ++;
143 }
144
145 VG_STATIC void scene_copy_slice( scene *pscene, mdl_submesh *sm )
146 {
147 sm->indice_start = pscene->submesh.indice_start;
148 sm->indice_count = pscene->indice_count - sm->indice_start;
149
150 sm->vertex_start = pscene->submesh.vertex_start;
151 sm->vertex_count = pscene->vertex_count - sm->vertex_start;
152
153 pscene->submesh.indice_start = pscene->indice_count;
154 pscene->submesh.vertex_start = pscene->vertex_count;
155 }
156
157 /* finalization: tightly pack data */
158 __attribute__((warn_unused_result))
159 VG_STATIC scene *scene_fix( void *lin_alloc, scene *pscene )
160 {
161 u32 vertex_length = pscene->vertex_count * sizeof(mdl_vert),
162 index_length = pscene->indice_count * sizeof(u32),
163 tot_size = sizeof(scene) + vertex_length + index_length;
164
165 scene *src_scene = pscene;
166 mdl_vert *src_verts = pscene->arrvertices;
167 u32 *src_indices = pscene->arrindices;
168
169 scene *dst_scene = vg_linear_resize( lin_alloc, pscene, tot_size );
170 memcpy( dst_scene, src_scene, sizeof(scene) );
171
172 void *dst_verts = dst_scene+1,
173 *dst_indices = dst_verts + vertex_length;
174
175 memcpy( dst_verts, src_verts, vertex_length );
176 memcpy( dst_indices, src_indices, index_length );
177
178 dst_scene->arrvertices = dst_verts;
179 dst_scene->arrindices = dst_indices;
180 dst_scene->max_vertices = pscene->vertex_count;
181 dst_scene->max_indices = pscene->indice_count;
182
183 return dst_scene;
184 }
185
186 #if 0
187 /* finalization: delete any offline buffers and reduce size */
188 __attribute__((warn_unused_result))
189 VG_STATIC scene *scene_free_offline_buffers( void *lin_alloc, scene *pscene )
190 {
191 u32 tot_size = sizeof(scene);
192
193 scene *src_scene = pscene;
194 mdl_vert *src_verts = pscene->arrvertices;
195 u32 *src_indices = pscene->arrindices;
196
197 scene *dst_scene = vg_linear_resize( lin_alloc, pscene, tot_size );
198 memcpy( dst_scene, src_scene, sizeof(scene) );
199
200 dst_scene->arrindices = NULL;
201 dst_scene->arrvertices = NULL;
202
203 return dst_scene;
204 }
205 #endif
206
207 VG_STATIC void scene_upload( scene *pscene, glmesh *mesh )
208 {
209 mesh_upload( mesh,
210 pscene->arrvertices, pscene->vertex_count,
211 pscene->arrindices, pscene->indice_count );
212
213 vg_info( "Scene upload\n" );
214 vg_info( " indices:%u\n", pscene->indice_count );
215 vg_info( " verts:%u\n", pscene->vertex_count );
216 }
217
218 /*
219 * BVH implementation
220 */
221
222 VG_STATIC void scene_bh_expand_bound( void *user, boxf bound, u32 item_index )
223 {
224 scene *s = user;
225 mdl_vert *pa = &s->arrvertices[ s->arrindices[item_index*3+0] ],
226 *pb = &s->arrvertices[ s->arrindices[item_index*3+1] ],
227 *pc = &s->arrvertices[ s->arrindices[item_index*3+2] ];
228
229 box_addpt( bound, pa->co );
230 box_addpt( bound, pb->co );
231 box_addpt( bound, pc->co );
232 }
233
234 VG_STATIC float scene_bh_centroid( void *user, u32 item_index, int axis )
235 {
236 scene *s = user;
237 mdl_vert *pa = &s->arrvertices[ s->arrindices[item_index*3+0] ],
238 *pb = &s->arrvertices[ s->arrindices[item_index*3+1] ],
239 *pc = &s->arrvertices[ s->arrindices[item_index*3+2] ];
240
241 return (pa->co[axis] + pb->co[axis] + pc->co[axis]) * (1.0f/3.0f);
242 }
243
244 VG_STATIC void scene_bh_swap( void *user, u32 ia, u32 ib )
245 {
246 scene *s = user;
247
248 u32 *ti = &s->arrindices[ia*3];
249 u32 *tj = &s->arrindices[ib*3];
250
251 u32 temp[3];
252 temp[0] = ti[0];
253 temp[1] = ti[1];
254 temp[2] = ti[2];
255
256 ti[0] = tj[0];
257 ti[1] = tj[1];
258 ti[2] = tj[2];
259
260 tj[0] = temp[0];
261 tj[1] = temp[1];
262 tj[2] = temp[2];
263 }
264
265 VG_STATIC void scene_bh_debug( void *user, u32 item_index )
266 {
267 scene *s = user;
268 u32 idx = item_index*3;
269 mdl_vert *pa = &s->arrvertices[ s->arrindices[ idx+0 ] ],
270 *pb = &s->arrvertices[ s->arrindices[ idx+1 ] ],
271 *pc = &s->arrvertices[ s->arrindices[ idx+2 ] ];
272
273 vg_line( pa->co, pb->co, 0xff0000ff );
274 vg_line( pb->co, pc->co, 0xff0000ff );
275 vg_line( pc->co, pa->co, 0xff0000ff );
276 }
277
278 VG_STATIC int scene_bh_ray( void *user, u32 index, v3f co,
279 v3f dir, ray_hit *hit )
280 {
281 scene *s = user;
282 v3f positions[3];
283
284 u32 *tri = &s->arrindices[ index*3 ];
285
286 for( int i=0; i<3; i++ )
287 v3_copy( s->arrvertices[tri[i]].co, positions[i] );
288
289 float t;
290 if(ray_tri( positions, co, dir, &t ))
291 {
292 if( t < hit->dist )
293 {
294 hit->dist = t;
295 hit->tri = tri;
296 return 1;
297 }
298 }
299
300 return 0;
301 }
302
303 VG_STATIC bh_system bh_system_scene =
304 {
305 .expand_bound = scene_bh_expand_bound,
306 .item_centroid = scene_bh_centroid,
307 .item_swap = scene_bh_swap,
308 .item_debug = scene_bh_debug,
309 .cast_ray = scene_bh_ray
310 };
311
312 /*
313 * An extra step is added onto the end to calculate the hit normal
314 */
315 VG_STATIC int scene_raycast( scene *s, bh_tree *bh,
316 v3f co, v3f dir, ray_hit *hit )
317 {
318 int count = bh_ray( bh, co, dir, hit );
319
320 if( count )
321 {
322 v3f v0, v1;
323
324 float *pa = s->arrvertices[hit->tri[0]].co,
325 *pb = s->arrvertices[hit->tri[1]].co,
326 *pc = s->arrvertices[hit->tri[2]].co;
327
328 v3_sub( pa, pb, v0 );
329 v3_sub( pc, pb, v1 );
330 v3_cross( v1, v0, hit->normal );
331 v3_normalize( hit->normal );
332 v3_muladds( co, dir, hit->dist, hit->pos );
333 }
334
335 return count;
336 }
337
338 VG_STATIC bh_tree *scene_bh_create( void *lin_alloc, scene *s )
339 {
340 u32 triangle_count = s->indice_count / 3;
341 return bh_create( lin_alloc, &bh_system_scene, s, triangle_count );
342 }
343
344 #endif