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