compiler warnings
[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_context scene_context;
9 typedef struct scene_vert scene_vert;
10
11 #pragma pack(push,1)
12
13 /* 32 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 u16 flags; /* only for the cpu. its junk on the gpu */
21 u16 unused[3];
22 };
23
24 #pragma pack(pop)
25
26 /*
27 * 1. this should probably be a CONTEXT based approach unlike this mess.
28 * take a bit of the mdl_context ideas and redo this header. its messed up
29 * pretty bad right now.
30 */
31
32 struct scene_context
33 {
34 scene_vert *arrvertices;
35 u32 *arrindices;
36
37 u32 vertex_count, indice_count,
38 max_vertices, max_indices;
39
40 boxf bbx;
41 mdl_submesh submesh;
42 };
43
44 static u32 scene_mem_required( scene_context *ctx )
45 {
46 u32 vertex_length = vg_align8(ctx->max_vertices * sizeof(scene_vert)),
47 index_length = vg_align8(ctx->max_indices * sizeof(u32));
48
49 return vertex_length + index_length;
50 }
51
52 static
53 void scene_init( scene_context *ctx, u32 max_vertices, u32 max_indices )
54 {
55 ctx->vertex_count = 0;
56 ctx->indice_count = 0;
57 ctx->max_vertices = max_vertices;
58 ctx->max_indices = max_indices;
59 ctx->arrindices = NULL; /* must be filled out by user */
60 ctx->arrvertices = NULL;
61
62 memset( &ctx->submesh, 0, sizeof(mdl_submesh) );
63
64 v3_fill( ctx->bbx[0], 999999.9f );
65 v3_fill( ctx->bbx[1], -999999.9f );
66 }
67
68 void scene_supply_buffer( scene_context *ctx, void *buffer )
69 {
70 u32 vertex_length = vg_align8( ctx->max_vertices * sizeof(scene_vert) );
71
72 ctx->arrvertices = buffer;
73 ctx->arrindices = (u32*)(((u8*)buffer) + vertex_length);
74 }
75
76 static void scene_vert_pack_norm( scene_vert *vert, v3f norm )
77 {
78 v3f n;
79 v3_muls( norm, 127.0f, n );
80 v3_minv( n, (v3f){ 127.0f, 127.0f, 127.0f }, n );
81 v3_maxv( n, (v3f){ -127.0f, -127.0f, -127.0f }, n );
82 vert->norm[0] = n[0];
83 vert->norm[1] = n[1];
84 vert->norm[2] = n[2];
85 vert->norm[3] = 0; /* free byte :D */
86 }
87
88 /*
89 * Append a model into the scene with a given transform
90 */
91 static void scene_add_mdl_submesh( scene_context *ctx, mdl_context *mdl,
92 mdl_submesh *sm, m4x3f transform )
93 {
94 if( ctx->vertex_count + sm->vertex_count > ctx->max_vertices ){
95 vg_fatal_error( "Scene vertex buffer overflow (%u exceeds %u)\n",
96 ctx->vertex_count + sm->vertex_count,
97 ctx->max_vertices );
98 }
99
100 if( ctx->indice_count + sm->indice_count > ctx->max_indices ){
101 vg_fatal_error( "Scene index buffer overflow (%u exceeds %u)\n",
102 ctx->indice_count + sm->indice_count,
103 ctx->max_indices );
104 }
105
106 mdl_vert *src_verts = mdl_arritm( &mdl->verts, sm->vertex_start );
107 scene_vert *dst_verts = &ctx->arrvertices[ ctx->vertex_count ];
108
109 u32 *src_indices = mdl_arritm( &mdl->indices, sm->indice_start ),
110 *dst_indices = &ctx->arrindices[ ctx->indice_count ];
111
112 /* Transform and place vertices */
113 boxf bbxnew;
114 box_init_inf( bbxnew );
115 m4x3_expand_aabb_aabb( transform, bbxnew, sm->bbx );
116 box_concat( ctx->bbx, bbxnew );
117
118 m3x3f normal_matrix;
119 m3x3_copy( transform, normal_matrix );
120 v3_normalize( normal_matrix[0] );
121 v3_normalize( normal_matrix[1] );
122 v3_normalize( normal_matrix[2] );
123
124 for( u32 i=0; i<sm->vertex_count; i++ ){
125 mdl_vert *src = &src_verts[i];
126 scene_vert *pvert = &dst_verts[i];
127
128 m4x3_mulv( transform, src->co, pvert->co );
129
130 v3f normal;
131 m3x3_mulv( normal_matrix, src->norm, normal );
132 scene_vert_pack_norm( pvert, normal );
133
134 v2_copy( src->uv, pvert->uv );
135 }
136
137 u32 real_indices = 0;
138 for( u32 i=0; i<sm->indice_count/3; i++ ){
139 u32 *src = &src_indices[i*3],
140 *dst = &dst_indices[real_indices];
141
142 v3f ab, ac, tn;
143 v3_sub( src_verts[src[2]].co, src_verts[src[0]].co, ab );
144 v3_sub( src_verts[src[1]].co, src_verts[src[0]].co, ac );
145 v3_cross( ac, ab, tn );
146
147 #if 0
148 if( v3_length2( tn ) <= 0.00001f )
149 continue;
150 #endif
151
152 dst[0] = src[0] + ctx->vertex_count;
153 dst[1] = src[1] + ctx->vertex_count;
154 dst[2] = src[2] + ctx->vertex_count;
155
156 real_indices += 3;
157 }
158
159 if( real_indices != sm->indice_count )
160 vg_warn( "Zero area triangles in model\n" );
161
162 ctx->vertex_count += sm->vertex_count;
163 ctx->indice_count += real_indices;
164 }
165
166 /*
167 * One by one adders for simplified access (mostly procedural stuff)
168 */
169 static void scene_push_tri( scene_context *ctx, u32 tri[3] )
170 {
171 if( ctx->indice_count + 3 > ctx->max_indices )
172 vg_fatal_error( "Scene indice buffer overflow (%u exceeds %u)\n",
173 ctx->indice_count+3, ctx->max_indices );
174
175 u32 *dst = &ctx->arrindices[ ctx->indice_count ];
176
177 dst[0] = tri[0];
178 dst[1] = tri[1];
179 dst[2] = tri[2];
180
181 ctx->indice_count += 3;
182 }
183
184 static void scene_push_vert( scene_context *ctx, scene_vert *v )
185 {
186 if( ctx->vertex_count + 1 > ctx->max_vertices )
187 vg_fatal_error( "Scene vertex buffer overflow (%u exceeds %u)\n",
188 ctx->vertex_count+1, ctx->max_vertices );
189
190 scene_vert *dst = &ctx->arrvertices[ ctx->vertex_count ];
191 *dst = *v;
192
193 ctx->vertex_count ++;
194 }
195
196 static void scene_copy_slice( scene_context *ctx, mdl_submesh *sm )
197 {
198 sm->indice_start = ctx->submesh.indice_start;
199 sm->indice_count = ctx->indice_count - sm->indice_start;
200
201 sm->vertex_start = ctx->submesh.vertex_start;
202 sm->vertex_count = ctx->vertex_count - sm->vertex_start;
203
204 ctx->submesh.indice_start = ctx->indice_count;
205 ctx->submesh.vertex_start = ctx->vertex_count;
206 }
207
208 static void scene_set_vertex_flags( scene_context *ctx,
209 u32 start, u32 count, u16 flags ){
210 for( u32 i=0; i<count; i++ )
211 ctx->arrvertices[ start + i ].flags = flags;
212 }
213
214 struct scene_upload_info{
215 scene_context *ctx;
216 glmesh *mesh;
217 };
218
219 static void async_scene_upload( void *payload, u32 size )
220 {
221 struct scene_upload_info *info = payload;
222
223 //assert( mesh->loaded == 0 );
224
225 glmesh *mesh = info->mesh;
226 scene_context *ctx = info->ctx;
227
228 glGenVertexArrays( 1, &mesh->vao );
229 glGenBuffers( 1, &mesh->vbo );
230 glGenBuffers( 1, &mesh->ebo );
231 glBindVertexArray( mesh->vao );
232
233 size_t stride = sizeof(scene_vert);
234
235 glBindBuffer( GL_ARRAY_BUFFER, mesh->vbo );
236 glBufferData( GL_ARRAY_BUFFER, ctx->vertex_count*stride,
237 ctx->arrvertices, GL_STATIC_DRAW );
238
239 glBindVertexArray( mesh->vao );
240 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mesh->ebo );
241 glBufferData( GL_ELEMENT_ARRAY_BUFFER, ctx->indice_count*sizeof(u32),
242 ctx->arrindices, GL_STATIC_DRAW );
243
244 /* 0: coordinates */
245 glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0 );
246 glEnableVertexAttribArray( 0 );
247
248 /* 1: normal */
249 glVertexAttribPointer( 1, 4, GL_BYTE, GL_TRUE,
250 stride, (void *)offsetof(scene_vert, norm) );
251 glEnableVertexAttribArray( 1 );
252
253 /* 2: uv */
254 glVertexAttribPointer( 2, 2, GL_FLOAT, GL_FALSE,
255 stride, (void *)offsetof(scene_vert, uv) );
256 glEnableVertexAttribArray( 2 );
257
258 VG_CHECK_GL_ERR();
259
260 mesh->indice_count = ctx->indice_count;
261 mesh->loaded = 1;
262
263 vg_info( "Scene upload ( XYZ_f32 UV_f32 XYZW_i8 )[ u32 ]\n" );
264 vg_info( " indices:%u\n", ctx->indice_count );
265 vg_info( " verts:%u\n", ctx->vertex_count );
266 }
267
268 static void scene_upload_async( scene_context *ctx, glmesh *mesh )
269 {
270 vg_async_item *call = vg_async_alloc( sizeof(struct scene_upload_info) );
271
272 struct scene_upload_info *info = call->payload;
273 info->mesh = mesh;
274 info->ctx = ctx;
275
276 vg_async_dispatch( call, async_scene_upload );
277 }
278
279 static
280 vg_async_item *scene_alloc_async( scene_context *scene, glmesh *mesh,
281 u32 max_vertices, u32 max_indices )
282 {
283 scene_init( scene, max_vertices, max_indices );
284 u32 buf_size = scene_mem_required( scene );
285
286 u32 hdr_size = vg_align8(sizeof(struct scene_upload_info));
287 vg_async_item *call = vg_async_alloc( hdr_size + buf_size );
288
289 struct scene_upload_info *info = call->payload;
290
291 info->mesh = mesh;
292 info->ctx = scene;
293
294 void *buffer = ((u8*)call->payload)+hdr_size;
295 scene_supply_buffer( scene, buffer );
296
297 return call;
298 }
299
300
301 /*
302 * BVH implementation
303 */
304
305 static void scene_bh_expand_bound( void *user, boxf bound, u32 item_index )
306 {
307 scene_context *s = user;
308 scene_vert *pa = &s->arrvertices[ s->arrindices[item_index*3+0] ],
309 *pb = &s->arrvertices[ s->arrindices[item_index*3+1] ],
310 *pc = &s->arrvertices[ s->arrindices[item_index*3+2] ];
311
312 box_addpt( bound, pa->co );
313 box_addpt( bound, pb->co );
314 box_addpt( bound, pc->co );
315 }
316
317 static float scene_bh_centroid( void *user, u32 item_index, int axis )
318 {
319 scene_context *s = user;
320 scene_vert *pa = &s->arrvertices[ s->arrindices[item_index*3+0] ],
321 *pb = &s->arrvertices[ s->arrindices[item_index*3+1] ],
322 *pc = &s->arrvertices[ s->arrindices[item_index*3+2] ];
323
324 #if 0
325
326 float min, max;
327
328 min = vg_minf( pa->co[axis], pb->co[axis] );
329 max = vg_maxf( pa->co[axis], pb->co[axis] );
330 min = vg_minf( min, pc->co[axis] );
331 max = vg_maxf( max, pc->co[axis] );
332
333 return (min+max) * 0.5f;
334
335 #else
336 return (pa->co[axis] + pb->co[axis] + pc->co[axis]) * (1.0f/3.0f);
337 #endif
338 }
339
340 static void scene_bh_swap( void *user, u32 ia, u32 ib )
341 {
342 scene_context *s = user;
343
344 u32 *ti = &s->arrindices[ia*3];
345 u32 *tj = &s->arrindices[ib*3];
346
347 u32 temp[3];
348 temp[0] = ti[0];
349 temp[1] = ti[1];
350 temp[2] = ti[2];
351
352 ti[0] = tj[0];
353 ti[1] = tj[1];
354 ti[2] = tj[2];
355
356 tj[0] = temp[0];
357 tj[1] = temp[1];
358 tj[2] = temp[2];
359 }
360
361 static void scene_bh_debug( void *user, u32 item_index )
362 {
363 scene_context *s = user;
364 u32 idx = item_index*3;
365 scene_vert *pa = &s->arrvertices[ s->arrindices[ idx+0 ] ],
366 *pb = &s->arrvertices[ s->arrindices[ idx+1 ] ],
367 *pc = &s->arrvertices[ s->arrindices[ idx+2 ] ];
368
369 vg_line( pa->co, pb->co, 0xff0000ff );
370 vg_line( pb->co, pc->co, 0xff0000ff );
371 vg_line( pc->co, pa->co, 0xff0000ff );
372 }
373
374 static void scene_bh_closest( void *user, u32 index, v3f point, v3f closest )
375 {
376 scene_context *s = user;
377
378 v3f positions[3];
379 u32 *tri = &s->arrindices[ index*3 ];
380 for( int i=0; i<3; i++ )
381 v3_copy( s->arrvertices[tri[i]].co, positions[i] );
382
383 closest_on_triangle_1( point, positions, closest );
384 }
385
386 static bh_system bh_system_scene =
387 {
388 .expand_bound = scene_bh_expand_bound,
389 .item_centroid = scene_bh_centroid,
390 .item_closest = scene_bh_closest,
391 .item_swap = scene_bh_swap,
392 .item_debug = scene_bh_debug,
393 };
394
395 /*
396 * An extra step is added onto the end to calculate the hit normal
397 */
398 static int scene_raycast( scene_context *s, bh_tree *bh,
399 v3f co, v3f dir, ray_hit *hit, u16 ignore )
400 {
401 hit->tri = NULL;
402
403 bh_iter it;
404 bh_iter_init_ray( 0, &it, co, dir, hit->dist );
405 i32 idx;
406
407 while( bh_next( bh, &it, &idx ) ){
408 u32 *tri = &s->arrindices[ idx*3 ];
409
410 if( s->arrvertices[tri[0]].flags & ignore ) continue;
411
412 v3f vs[3];
413 for( u32 i=0; i<3; i++ )
414 v3_copy( s->arrvertices[tri[i]].co, vs[i] );
415
416 f32 t;
417 if( ray_tri( vs, co, dir, &t ) ){
418 if( t < hit->dist ){
419 hit->dist = t;
420 hit->tri = tri;
421 }
422 }
423 }
424
425 if( hit->tri ){
426 v3f v0, v1;
427
428 float *pa = s->arrvertices[hit->tri[0]].co,
429 *pb = s->arrvertices[hit->tri[1]].co,
430 *pc = s->arrvertices[hit->tri[2]].co;
431
432 v3_sub( pa, pb, v0 );
433 v3_sub( pc, pb, v1 );
434 v3_cross( v1, v0, hit->normal );
435 v3_normalize( hit->normal );
436 v3_muladds( co, dir, hit->dist, hit->pos );
437 }
438
439 return hit->tri?1:0;
440 }
441
442 static bh_tree *scene_bh_create( void *lin_alloc, scene_context *s )
443 {
444 u32 triangle_count = s->indice_count / 3;
445 return bh_create( lin_alloc, &bh_system_scene, s, triangle_count, 2 );
446 }
447
448 #endif