longjump gates
[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 typedef struct scene_vert scene_vert;
10
11 #pragma pack(push,1)
12
13 /* 28 byte vertexs, we don't care about the normals too much,
14 * maybe possible to bring down uv to i16s too */
15 struct scene_vert
16 {
17 v3f co; /* 3*32 */
18 v2f uv; /* 2*32 */
19 i8 norm[4]; /* 4*8 */
20 u8 lights[4]; /* 4*8 */
21 };
22
23 #pragma pack(pop)
24
25 struct scene
26 {
27 scene_vert *arrvertices;
28
29 u32 *arrindices;
30
31 u32 vertex_count, indice_count,
32 max_vertices, max_indices;
33
34 boxf bbx;
35 mdl_submesh submesh;
36 };
37
38 /* Initialize a scene description with bounded buffers */
39 VG_STATIC scene *scene_init( void *lin_alloc, u32 max_verts, u32 max_indices )
40 {
41 u32 vertex_length = max_verts * sizeof(scene_vert),
42 index_length = max_indices * sizeof(u32),
43 tot_size = sizeof(scene) + vertex_length + index_length;
44
45 scene *pscene = vg_linear_alloc( lin_alloc, tot_size );
46
47 pscene->arrvertices = (scene_vert *)(pscene+1);
48 pscene->arrindices = (u32 *)( pscene->arrvertices + max_verts );
49
50 pscene->vertex_count = 0;
51 pscene->indice_count = 0;
52 pscene->max_vertices = max_verts;
53 pscene->max_indices = max_indices;
54
55 memset( &pscene->submesh, 0, sizeof(mdl_submesh) );
56
57 v3_fill( pscene->bbx[0], 999999.9f );
58 v3_fill( pscene->bbx[1], -999999.9f );
59
60 return pscene;
61 }
62
63 VG_STATIC void scene_vert_pack_norm( scene_vert *vert, v3f norm )
64 {
65 v3f n;
66 v3_muls( norm, 127.0f, n );
67 v3_minv( n, (v3f){ 127.0f, 127.0f, 127.0f }, n );
68 v3_maxv( n, (v3f){ -127.0f, -127.0f, -127.0f }, n );
69 vert->norm[0] = n[0];
70 vert->norm[1] = n[1];
71 vert->norm[2] = n[2];
72 vert->norm[3] = 0; /* free byte :D */
73 }
74
75 /*
76 * Append a model into the scene with a given transform
77 */
78 VG_STATIC void scene_add_mdl_submesh( scene *pscene, mdl_context *mdl,
79 mdl_submesh *sm, m4x3f transform )
80 {
81 if( pscene->vertex_count + sm->vertex_count > pscene->max_vertices )
82 {
83 vg_error( "%u(current) + %u > %u\n", pscene->vertex_count,
84 sm->vertex_count,
85 pscene->max_vertices );
86
87 vg_warn( "%p ... %p\n", pscene, sm );
88 vg_fatal_exit_loop( "Scene vertex buffer overflow" );
89 }
90
91 if( pscene->indice_count + sm->indice_count > pscene->max_indices )
92 {
93 vg_error( "%u(current) + %u > %u\n", pscene->indice_count,
94 sm->indice_count,
95 pscene->max_indices );
96 vg_warn( "%p ... %p\n", pscene, sm );
97
98 vg_fatal_exit_loop( "Scene index buffer overflow" );
99 }
100
101 mdl_vert *src_verts = mdl_submesh_vertices( mdl, sm );
102 scene_vert *dst_verts = &pscene->arrvertices[ pscene->vertex_count ];
103
104 u32 *src_indices = mdl_submesh_indices( mdl, sm ),
105 *dst_indices = &pscene->arrindices[ pscene->indice_count ];
106
107 /* Transform and place vertices */
108 boxf bbxnew;
109 box_copy( sm->bbx, bbxnew );
110 m4x3_transform_aabb( transform, bbxnew );
111 box_concat( pscene->bbx, bbxnew );
112
113 m3x3f normal_matrix;
114 m3x3_copy( transform, normal_matrix );
115 v3_normalize( normal_matrix[0] );
116 v3_normalize( normal_matrix[1] );
117 v3_normalize( normal_matrix[2] );
118
119 for( u32 i=0; i<sm->vertex_count; i++ )
120 {
121 mdl_vert *src = &src_verts[ i ];
122 scene_vert *pvert = &dst_verts[ i ];
123
124 m4x3_mulv( transform, src->co, pvert->co );
125
126 v3f normal;
127 m3x3_mulv( normal_matrix, src->norm, normal );
128 scene_vert_pack_norm( pvert, normal );
129
130 v2_copy( src->uv, pvert->uv );
131 }
132
133 for( u32 i=0; i<sm->indice_count; i++ )
134 dst_indices[i] = src_indices[i] + pscene->vertex_count;
135
136 pscene->vertex_count += sm->vertex_count;
137 pscene->indice_count += sm->indice_count;
138 }
139
140 /*
141 * One by one adders for simplified access (mostly procedural stuff)
142 */
143 VG_STATIC void scene_push_tri( scene *pscene, u32 tri[3] )
144 {
145 if( pscene->indice_count + 3 > pscene->max_indices )
146 vg_fatal_exit_loop( "Scene vertex buffer overflow" );
147
148 u32 *dst = &pscene->arrindices[ pscene->indice_count ];
149
150 dst[0] = tri[0];
151 dst[1] = tri[1];
152 dst[2] = tri[2];
153
154 pscene->indice_count += 3;
155 }
156
157 VG_STATIC void scene_push_vert( scene *pscene, scene_vert *v )
158 {
159 if( pscene->vertex_count + 1 > pscene->max_vertices )
160 vg_fatal_exit_loop( "Scene vertex buffer overflow" );
161
162 scene_vert *dst = &pscene->arrvertices[ pscene->vertex_count ];
163 *dst = *v;
164
165 pscene->vertex_count ++;
166 }
167
168 VG_STATIC void scene_copy_slice( scene *pscene, mdl_submesh *sm )
169 {
170 sm->indice_start = pscene->submesh.indice_start;
171 sm->indice_count = pscene->indice_count - sm->indice_start;
172
173 sm->vertex_start = pscene->submesh.vertex_start;
174 sm->vertex_count = pscene->vertex_count - sm->vertex_start;
175
176 pscene->submesh.indice_start = pscene->indice_count;
177 pscene->submesh.vertex_start = pscene->vertex_count;
178 }
179
180 /* finalization: tightly pack data */
181 __attribute__((warn_unused_result))
182 VG_STATIC scene *scene_fix( void *lin_alloc, scene *pscene )
183 {
184 /* FIXME: Why is this disabled? */
185
186 u32 vertex_count = pscene->vertex_count,
187 indice_count = pscene->indice_count,
188 vertex_length = vertex_count * sizeof(scene_vert),
189 index_length = indice_count * sizeof(u32),
190 tot_size = sizeof(scene) + vertex_length + index_length;
191
192 /* copy down index data */
193 void *dst_indices = pscene->arrvertices + vertex_count;
194 memmove( dst_indices, pscene->arrindices, index_length );
195
196 /* realloc */
197 pscene = vg_linear_resize( lin_alloc, pscene, tot_size );
198
199 pscene->arrvertices = (scene_vert *)(pscene+1);
200 pscene->arrindices = (u32 *)(pscene->arrvertices+vertex_count);
201 pscene->max_vertices = vertex_count;
202 pscene->max_indices = indice_count;
203
204 return pscene;
205 }
206
207 #if 0
208 /* finalization: delete any offline buffers and reduce size */
209 __attribute__((warn_unused_result))
210 VG_STATIC scene *scene_free_offline_buffers( void *lin_alloc, scene *pscene )
211 {
212 u32 tot_size = sizeof(scene);
213
214 scene *src_scene = pscene;
215 mdl_vert *src_verts = pscene->arrvertices;
216 u32 *src_indices = pscene->arrindices;
217
218 scene *dst_scene = vg_linear_resize( lin_alloc, pscene, tot_size );
219 memcpy( dst_scene, src_scene, sizeof(scene) );
220
221 dst_scene->arrindices = NULL;
222 dst_scene->arrvertices = NULL;
223
224 return dst_scene;
225 }
226 #endif
227
228 VG_STATIC void scene_upload( scene *pscene, glmesh *mesh )
229 {
230 //assert( mesh->loaded == 0 );
231
232 glGenVertexArrays( 1, &mesh->vao );
233 glGenBuffers( 1, &mesh->vbo );
234 glGenBuffers( 1, &mesh->ebo );
235 glBindVertexArray( mesh->vao );
236
237 size_t stride = sizeof(scene_vert);
238
239 glBindBuffer( GL_ARRAY_BUFFER, mesh->vbo );
240 glBufferData( GL_ARRAY_BUFFER, pscene->vertex_count*stride,
241 pscene->arrvertices, GL_STATIC_DRAW );
242
243 glBindVertexArray( mesh->vao );
244 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mesh->ebo );
245 glBufferData( GL_ELEMENT_ARRAY_BUFFER, pscene->indice_count*sizeof(u32),
246 pscene->arrindices, GL_STATIC_DRAW );
247
248 /* 0: coordinates */
249 glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0 );
250 glEnableVertexAttribArray( 0 );
251
252 /* 1: normal */
253 glVertexAttribPointer( 1, 3, GL_BYTE, GL_TRUE,
254 stride, (void *)offsetof(scene_vert, norm) );
255 glEnableVertexAttribArray( 1 );
256
257 /* 2: uv */
258 glVertexAttribPointer( 2, 2, GL_FLOAT, GL_FALSE,
259 stride, (void *)offsetof(scene_vert, uv) );
260 glEnableVertexAttribArray( 2 );
261
262 /* 3: light cluster */
263 glVertexAttribIPointer( 3, 4, GL_UNSIGNED_BYTE,
264 stride, (void *)offsetof(scene_vert, lights) );
265 glEnableVertexAttribArray( 3 );
266
267 VG_CHECK_GL_ERR();
268
269 mesh->indice_count = pscene->indice_count;
270 mesh->loaded = 1;
271
272 vg_info( "Scene upload ( XYZ_f32 UV_f32 XYZW_i8 )[ u32 ]\n" );
273 vg_info( " indices:%u\n", pscene->indice_count );
274 vg_info( " verts:%u\n", pscene->vertex_count );
275 }
276
277 /*
278 * BVH implementation
279 */
280
281 VG_STATIC void scene_bh_expand_bound( void *user, boxf bound, u32 item_index )
282 {
283 scene *s = user;
284 scene_vert *pa = &s->arrvertices[ s->arrindices[item_index*3+0] ],
285 *pb = &s->arrvertices[ s->arrindices[item_index*3+1] ],
286 *pc = &s->arrvertices[ s->arrindices[item_index*3+2] ];
287
288 box_addpt( bound, pa->co );
289 box_addpt( bound, pb->co );
290 box_addpt( bound, pc->co );
291 }
292
293 VG_STATIC float scene_bh_centroid( void *user, u32 item_index, int axis )
294 {
295 scene *s = user;
296 scene_vert *pa = &s->arrvertices[ s->arrindices[item_index*3+0] ],
297 *pb = &s->arrvertices[ s->arrindices[item_index*3+1] ],
298 *pc = &s->arrvertices[ s->arrindices[item_index*3+2] ];
299
300 return (pa->co[axis] + pb->co[axis] + pc->co[axis]) * (1.0f/3.0f);
301 }
302
303 VG_STATIC void scene_bh_swap( void *user, u32 ia, u32 ib )
304 {
305 scene *s = user;
306
307 u32 *ti = &s->arrindices[ia*3];
308 u32 *tj = &s->arrindices[ib*3];
309
310 u32 temp[3];
311 temp[0] = ti[0];
312 temp[1] = ti[1];
313 temp[2] = ti[2];
314
315 ti[0] = tj[0];
316 ti[1] = tj[1];
317 ti[2] = tj[2];
318
319 tj[0] = temp[0];
320 tj[1] = temp[1];
321 tj[2] = temp[2];
322 }
323
324 VG_STATIC void scene_bh_debug( void *user, u32 item_index )
325 {
326 scene *s = user;
327 u32 idx = item_index*3;
328 scene_vert *pa = &s->arrvertices[ s->arrindices[ idx+0 ] ],
329 *pb = &s->arrvertices[ s->arrindices[ idx+1 ] ],
330 *pc = &s->arrvertices[ s->arrindices[ idx+2 ] ];
331
332 vg_line( pa->co, pb->co, 0xff0000ff );
333 vg_line( pb->co, pc->co, 0xff0000ff );
334 vg_line( pc->co, pa->co, 0xff0000ff );
335 }
336
337 VG_STATIC int scene_bh_ray( void *user, u32 index, v3f co,
338 v3f dir, ray_hit *hit )
339 {
340 scene *s = user;
341 v3f positions[3];
342
343 u32 *tri = &s->arrindices[ index*3 ];
344
345 for( int i=0; i<3; i++ )
346 v3_copy( s->arrvertices[tri[i]].co, positions[i] );
347
348 float t;
349 if(ray_tri( positions, co, dir, &t ))
350 {
351 if( t < hit->dist )
352 {
353 hit->dist = t;
354 hit->tri = tri;
355 return 1;
356 }
357 }
358
359 return 0;
360 }
361
362 VG_STATIC void scene_bh_closest( void *user, u32 index, v3f point, v3f closest )
363 {
364 scene *s = user;
365
366 v3f positions[3];
367 u32 *tri = &s->arrindices[ index*3 ];
368 for( int i=0; i<3; i++ )
369 v3_copy( s->arrvertices[tri[i]].co, positions[i] );
370
371 closest_on_triangle_1( point, positions, closest );
372 }
373
374 VG_STATIC bh_system bh_system_scene =
375 {
376 .expand_bound = scene_bh_expand_bound,
377 .item_centroid = scene_bh_centroid,
378 .item_closest = scene_bh_closest,
379 .item_swap = scene_bh_swap,
380 .item_debug = scene_bh_debug,
381 .cast_ray = scene_bh_ray
382 };
383
384 /*
385 * An extra step is added onto the end to calculate the hit normal
386 */
387 VG_STATIC int scene_raycast( scene *s, bh_tree *bh,
388 v3f co, v3f dir, ray_hit *hit )
389 {
390 int count = bh_ray( bh, co, dir, hit );
391
392 if( count )
393 {
394 v3f v0, v1;
395
396 float *pa = s->arrvertices[hit->tri[0]].co,
397 *pb = s->arrvertices[hit->tri[1]].co,
398 *pc = s->arrvertices[hit->tri[2]].co;
399
400 v3_sub( pa, pb, v0 );
401 v3_sub( pc, pb, v1 );
402 v3_cross( v1, v0, hit->normal );
403 v3_normalize( hit->normal );
404 v3_muladds( co, dir, hit->dist, hit->pos );
405 }
406
407 return count;
408 }
409
410 VG_STATIC bh_tree *scene_bh_create( void *lin_alloc, scene *s )
411 {
412 u32 triangle_count = s->indice_count / 3;
413 return bh_create( lin_alloc, &bh_system_scene, s, triangle_count, 2 );
414 }
415
416 #endif