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