3 u32
scene_mem_required( scene_context
*ctx
)
5 u32 vertex_length
= vg_align8(ctx
->max_vertices
* sizeof(scene_vert
)),
6 index_length
= vg_align8(ctx
->max_indices
* sizeof(u32
));
8 return vertex_length
+ index_length
;
11 void scene_init( scene_context
*ctx
, u32 max_vertices
, u32 max_indices
)
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
;
20 memset( &ctx
->submesh
, 0, sizeof(mdl_submesh
) );
22 v3_fill( ctx
->bbx
[0], 999999.9f
);
23 v3_fill( ctx
->bbx
[1], -999999.9f
);
26 void scene_supply_buffer( scene_context
*ctx
, void *buffer
)
28 u32 vertex_length
= vg_align8( ctx
->max_vertices
* sizeof(scene_vert
) );
30 ctx
->arrvertices
= buffer
;
31 ctx
->arrindices
= (u32
*)(((u8
*)buffer
) + vertex_length
);
34 void scene_vert_pack_norm( scene_vert
*vert
, v3f norm
, f32 blend
)
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
);
43 vert
->norm
[3] = blend
* 127.0f
;
47 * Append a model into the scene with a given transform
49 void scene_add_mdl_submesh( scene_context
*ctx
, mdl_context
*mdl
,
50 mdl_submesh
*sm
, m4x3f transform
)
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
,
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
,
64 mdl_vert
*src_verts
= mdl_arritm( &mdl
->verts
, sm
->vertex_start
);
65 scene_vert
*dst_verts
= &ctx
->arrvertices
[ ctx
->vertex_count
];
67 u32
*src_indices
= mdl_arritm( &mdl
->indices
, sm
->indice_start
),
68 *dst_indices
= &ctx
->arrindices
[ ctx
->indice_count
];
70 /* Transform and place vertices */
72 box_init_inf( bbxnew
);
73 m4x3_expand_aabb_aabb( transform
, bbxnew
, sm
->bbx
);
74 box_concat( ctx
->bbx
, bbxnew
);
77 m3x3_copy( transform
, normal_matrix
);
78 v3_normalize( normal_matrix
[0] );
79 v3_normalize( normal_matrix
[1] );
80 v3_normalize( normal_matrix
[2] );
82 for( u32 i
=0; i
<sm
->vertex_count
; i
++ ){
83 mdl_vert
*src
= &src_verts
[i
];
84 scene_vert
*pvert
= &dst_verts
[i
];
86 m4x3_mulv( transform
, src
->co
, pvert
->co
);
89 m3x3_mulv( normal_matrix
, src
->norm
, normal
);
90 scene_vert_pack_norm( pvert
, normal
, src
->colour
[0]*(1.0f
/255.0f
) );
92 v2_copy( src
->uv
, pvert
->uv
);
96 for( u32 i
=0; i
<sm
->indice_count
/3; i
++ ){
97 u32
*src
= &src_indices
[i
*3],
98 *dst
= &dst_indices
[real_indices
];
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
);
106 if( v3_length2( tn
) <= 0.00001f
)
110 dst
[0] = src
[0] + ctx
->vertex_count
;
111 dst
[1] = src
[1] + ctx
->vertex_count
;
112 dst
[2] = src
[2] + ctx
->vertex_count
;
117 if( real_indices
!= sm
->indice_count
)
118 vg_warn( "Zero area triangles in model\n" );
120 ctx
->vertex_count
+= sm
->vertex_count
;
121 ctx
->indice_count
+= real_indices
;
125 * One by one adders for simplified access (mostly procedural stuff)
127 void scene_push_tri( scene_context
*ctx
, u32 tri
[3] )
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
);
133 u32
*dst
= &ctx
->arrindices
[ ctx
->indice_count
];
139 ctx
->indice_count
+= 3;
142 void scene_push_vert( scene_context
*ctx
, scene_vert
*v
)
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
);
148 scene_vert
*dst
= &ctx
->arrvertices
[ ctx
->vertex_count
];
151 ctx
->vertex_count
++;
154 void scene_copy_slice( scene_context
*ctx
, mdl_submesh
*sm
)
156 sm
->indice_start
= ctx
->submesh
.indice_start
;
157 sm
->indice_count
= ctx
->indice_count
- sm
->indice_start
;
159 sm
->vertex_start
= ctx
->submesh
.vertex_start
;
160 sm
->vertex_count
= ctx
->vertex_count
- sm
->vertex_start
;
162 ctx
->submesh
.indice_start
= ctx
->indice_count
;
163 ctx
->submesh
.vertex_start
= ctx
->vertex_count
;
166 void scene_set_vertex_flags( scene_context
*ctx
,
167 u32 start
, u32 count
, u16 flags
)
169 for( u32 i
=0; i
<count
; i
++ )
170 ctx
->arrvertices
[ start
+ i
].flags
= flags
;
173 struct scene_upload_info
{
178 void async_scene_upload( void *payload
, u32 size
)
180 struct scene_upload_info
*info
= payload
;
182 //assert( mesh->loaded == 0 );
184 glmesh
*mesh
= info
->mesh
;
185 scene_context
*ctx
= info
->ctx
;
187 glGenVertexArrays( 1, &mesh
->vao
);
188 glGenBuffers( 1, &mesh
->vbo
);
189 glGenBuffers( 1, &mesh
->ebo
);
190 glBindVertexArray( mesh
->vao
);
192 size_t stride
= sizeof(scene_vert
);
194 glBindBuffer( GL_ARRAY_BUFFER
, mesh
->vbo
);
195 glBufferData( GL_ARRAY_BUFFER
, ctx
->vertex_count
*stride
,
196 ctx
->arrvertices
, GL_STATIC_DRAW
);
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
);
204 glVertexAttribPointer( 0, 3, GL_FLOAT
, GL_FALSE
, stride
, (void*)0 );
205 glEnableVertexAttribArray( 0 );
208 glVertexAttribPointer( 1, 4, GL_BYTE
, GL_TRUE
,
209 stride
, (void *)offsetof(scene_vert
, norm
) );
210 glEnableVertexAttribArray( 1 );
213 glVertexAttribPointer( 2, 2, GL_FLOAT
, GL_FALSE
,
214 stride
, (void *)offsetof(scene_vert
, uv
) );
215 glEnableVertexAttribArray( 2 );
219 mesh
->indice_count
= ctx
->indice_count
;
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
);
227 void scene_upload_async( scene_context
*ctx
, glmesh
*mesh
)
229 vg_async_item
*call
= vg_async_alloc( sizeof(struct scene_upload_info
) );
231 struct scene_upload_info
*info
= call
->payload
;
235 vg_async_dispatch( call
, async_scene_upload
);
238 vg_async_item
*scene_alloc_async( scene_context
*scene
, glmesh
*mesh
,
239 u32 max_vertices
, u32 max_indices
)
241 scene_init( scene
, max_vertices
, max_indices
);
242 u32 buf_size
= scene_mem_required( scene
);
244 u32 hdr_size
= vg_align8(sizeof(struct scene_upload_info
));
245 vg_async_item
*call
= vg_async_alloc( hdr_size
+ buf_size
);
247 struct scene_upload_info
*info
= call
->payload
;
252 void *buffer
= ((u8
*)call
->payload
)+hdr_size
;
253 scene_supply_buffer( scene
, buffer
);
263 static void scene_bh_expand_bound( void *user
, boxf bound
, u32 item_index
)
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] ];
270 box_addpt( bound
, pa
->co
);
271 box_addpt( bound
, pb
->co
);
272 box_addpt( bound
, pc
->co
);
275 static float scene_bh_centroid( void *user
, u32 item_index
, int axis
)
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] ];
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
] );
291 return (min
+max
) * 0.5f
;
294 return (pa
->co
[axis
] + pb
->co
[axis
] + pc
->co
[axis
]) * (1.0f
/3.0f
);
298 static void scene_bh_swap( void *user
, u32 ia
, u32 ib
)
300 scene_context
*s
= user
;
302 u32
*ti
= &s
->arrindices
[ia
*3];
303 u32
*tj
= &s
->arrindices
[ib
*3];
319 static void scene_bh_debug( void *user
, u32 item_index
)
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 ] ];
327 vg_line( pa
->co
, pb
->co
, 0xff0000ff );
328 vg_line( pb
->co
, pc
->co
, 0xff0000ff );
329 vg_line( pc
->co
, pa
->co
, 0xff0000ff );
332 static void scene_bh_closest( void *user
, u32 index
, v3f point
, v3f closest
)
334 scene_context
*s
= user
;
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
] );
341 closest_on_triangle_1( point
, positions
, closest
);
344 bh_system bh_system_scene
=
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
,
354 * An extra step is added onto the end to calculate the hit normal
356 int scene_raycast( scene_context
*s
, bh_tree
*bh
,
357 v3f co
, v3f dir
, ray_hit
*hit
, u16 ignore
)
362 bh_iter_init_ray( 0, &it
, co
, dir
, hit
->dist
);
365 while( bh_next( bh
, &it
, &idx
) ){
366 u32
*tri
= &s
->arrindices
[ idx
*3 ];
368 if( s
->arrvertices
[tri
[0]].flags
& ignore
) continue;
371 for( u32 i
=0; i
<3; i
++ )
372 v3_copy( s
->arrvertices
[tri
[i
]].co
, vs
[i
] );
375 if( ray_tri( vs
, co
, dir
, &t
, 0 ) ){
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
;
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
);
400 bh_tree
*scene_bh_create( void *lin_alloc
, scene_context
*s
)
402 u32 triangle_count
= s
->indice_count
/ 3;
403 return bh_create( lin_alloc
, &bh_system_scene
, s
, triangle_count
, 2 );