8 typedef struct scene_context scene_context
;
9 typedef struct scene_vert scene_vert
;
13 /* 32 byte vertexs, we don't care about the normals too much,
14 * maybe possible to bring down uv to i16s too */
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.
33 scene_vert
*arrvertices
;
36 u32 vertex_count
, indice_count
,
37 max_vertices
, max_indices
;
43 VG_STATIC u32
scene_mem_required( scene_context
*ctx
)
45 u32 vertex_length
= vg_align8(ctx
->max_vertices
* sizeof(scene_vert
)),
46 index_length
= vg_align8(ctx
->max_indices
* sizeof(u32
));
48 return vertex_length
+ index_length
;
52 void scene_init( scene_context
*ctx
, u32 max_vertices
, u32 max_indices
)
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
;
61 memset( &ctx
->submesh
, 0, sizeof(mdl_submesh
) );
63 v3_fill( ctx
->bbx
[0], 999999.9f
);
64 v3_fill( ctx
->bbx
[1], -999999.9f
);
67 void scene_supply_buffer( scene_context
*ctx
, void *buffer
)
69 u32 vertex_length
= vg_align8( ctx
->max_vertices
* sizeof(scene_vert
) );
71 ctx
->arrvertices
= buffer
;
72 ctx
->arrindices
= (u32
*)(((u8
*)buffer
) + vertex_length
);
75 VG_STATIC
void scene_vert_pack_norm( scene_vert
*vert
, v3f norm
)
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
);
84 vert
->norm
[3] = 0; /* free byte :D */
88 * Append a model into the scene with a given transform
90 VG_STATIC
void scene_add_mdl_submesh( scene_context
*ctx
, mdl_context
*mdl
,
91 mdl_submesh
*sm
, m4x3f transform
)
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
,
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
,
105 mdl_vert
*src_verts
= mdl_arritm( &mdl
->verts
, sm
->vertex_start
);
106 scene_vert
*dst_verts
= &ctx
->arrvertices
[ ctx
->vertex_count
];
108 u32
*src_indices
= mdl_arritm( &mdl
->indices
, sm
->indice_start
),
109 *dst_indices
= &ctx
->arrindices
[ ctx
->indice_count
];
111 /* Transform and place vertices */
113 box_init_inf( bbxnew
);
114 m4x3_expand_aabb_aabb( transform
, bbxnew
, sm
->bbx
);
115 box_concat( ctx
->bbx
, bbxnew
);
118 m3x3_copy( transform
, normal_matrix
);
119 v3_normalize( normal_matrix
[0] );
120 v3_normalize( normal_matrix
[1] );
121 v3_normalize( normal_matrix
[2] );
123 for( u32 i
=0; i
<sm
->vertex_count
; i
++ ){
124 mdl_vert
*src
= &src_verts
[i
];
125 scene_vert
*pvert
= &dst_verts
[i
];
127 m4x3_mulv( transform
, src
->co
, pvert
->co
);
130 m3x3_mulv( normal_matrix
, src
->norm
, normal
);
131 scene_vert_pack_norm( pvert
, normal
);
133 v2_copy( src
->uv
, pvert
->uv
);
136 u32 real_indices
= 0;
137 for( u32 i
=0; i
<sm
->indice_count
/3; i
++ ){
138 u32
*src
= &src_indices
[i
*3],
139 *dst
= &dst_indices
[real_indices
];
142 v3_sub( src_verts
[src
[2]].co
, src_verts
[src
[0]].co
, ab
);
143 v3_sub( src_verts
[src
[1]].co
, src_verts
[src
[0]].co
, ac
);
144 v3_cross( ac
, ab
, tn
);
147 if( v3_length2( tn
) <= 0.00001f
)
151 dst
[0] = src
[0] + ctx
->vertex_count
;
152 dst
[1] = src
[1] + ctx
->vertex_count
;
153 dst
[2] = src
[2] + ctx
->vertex_count
;
158 if( real_indices
!= sm
->indice_count
)
159 vg_warn( "Zero area triangles in model\n" );
161 ctx
->vertex_count
+= sm
->vertex_count
;
162 ctx
->indice_count
+= real_indices
;
166 * One by one adders for simplified access (mostly procedural stuff)
168 VG_STATIC
void scene_push_tri( scene_context
*ctx
, u32 tri
[3] )
170 if( ctx
->indice_count
+ 3 > ctx
->max_indices
)
171 vg_fatal_error( "Scene indice buffer overflow (%u exceeds %u)\n",
172 ctx
->indice_count
+3, ctx
->max_indices
);
174 u32
*dst
= &ctx
->arrindices
[ ctx
->indice_count
];
180 ctx
->indice_count
+= 3;
183 VG_STATIC
void scene_push_vert( scene_context
*ctx
, scene_vert
*v
)
185 if( ctx
->vertex_count
+ 1 > ctx
->max_vertices
)
186 vg_fatal_error( "Scene vertex buffer overflow (%u exceeds %u)\n",
187 ctx
->vertex_count
+1, ctx
->max_vertices
);
189 scene_vert
*dst
= &ctx
->arrvertices
[ ctx
->vertex_count
];
192 ctx
->vertex_count
++;
195 VG_STATIC
void scene_copy_slice( scene_context
*ctx
, mdl_submesh
*sm
)
197 sm
->indice_start
= ctx
->submesh
.indice_start
;
198 sm
->indice_count
= ctx
->indice_count
- sm
->indice_start
;
200 sm
->vertex_start
= ctx
->submesh
.vertex_start
;
201 sm
->vertex_count
= ctx
->vertex_count
- sm
->vertex_start
;
203 ctx
->submesh
.indice_start
= ctx
->indice_count
;
204 ctx
->submesh
.vertex_start
= ctx
->vertex_count
;
207 struct scene_upload_info
{
212 VG_STATIC
void async_scene_upload( void *payload
, u32 size
)
214 struct scene_upload_info
*info
= payload
;
216 //assert( mesh->loaded == 0 );
218 glmesh
*mesh
= info
->mesh
;
219 scene_context
*ctx
= info
->ctx
;
221 glGenVertexArrays( 1, &mesh
->vao
);
222 glGenBuffers( 1, &mesh
->vbo
);
223 glGenBuffers( 1, &mesh
->ebo
);
224 glBindVertexArray( mesh
->vao
);
226 size_t stride
= sizeof(scene_vert
);
228 glBindBuffer( GL_ARRAY_BUFFER
, mesh
->vbo
);
229 glBufferData( GL_ARRAY_BUFFER
, ctx
->vertex_count
*stride
,
230 ctx
->arrvertices
, GL_STATIC_DRAW
);
232 glBindVertexArray( mesh
->vao
);
233 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, mesh
->ebo
);
234 glBufferData( GL_ELEMENT_ARRAY_BUFFER
, ctx
->indice_count
*sizeof(u32
),
235 ctx
->arrindices
, GL_STATIC_DRAW
);
238 glVertexAttribPointer( 0, 3, GL_FLOAT
, GL_FALSE
, stride
, (void*)0 );
239 glEnableVertexAttribArray( 0 );
242 glVertexAttribPointer( 1, 4, GL_BYTE
, GL_TRUE
,
243 stride
, (void *)offsetof(scene_vert
, norm
) );
244 glEnableVertexAttribArray( 1 );
247 glVertexAttribPointer( 2, 2, GL_FLOAT
, GL_FALSE
,
248 stride
, (void *)offsetof(scene_vert
, uv
) );
249 glEnableVertexAttribArray( 2 );
253 mesh
->indice_count
= ctx
->indice_count
;
256 vg_info( "Scene upload ( XYZ_f32 UV_f32 XYZW_i8 )[ u32 ]\n" );
257 vg_info( " indices:%u\n", ctx
->indice_count
);
258 vg_info( " verts:%u\n", ctx
->vertex_count
);
261 VG_STATIC
void scene_upload_async( scene_context
*ctx
, glmesh
*mesh
)
263 vg_async_item
*call
= vg_async_alloc( sizeof(struct scene_upload_info
) );
265 struct scene_upload_info
*info
= call
->payload
;
269 vg_async_dispatch( call
, async_scene_upload
);
273 vg_async_item
*scene_alloc_async( scene_context
*scene
, glmesh
*mesh
,
274 u32 max_vertices
, u32 max_indices
)
276 scene_init( scene
, max_vertices
, max_indices
);
277 u32 buf_size
= scene_mem_required( scene
);
279 u32 hdr_size
= vg_align8(sizeof(struct scene_upload_info
));
280 vg_async_item
*call
= vg_async_alloc( hdr_size
+ buf_size
);
282 struct scene_upload_info
*info
= call
->payload
;
287 void *buffer
= ((u8
*)call
->payload
)+hdr_size
;
288 scene_supply_buffer( scene
, buffer
);
298 VG_STATIC
void scene_bh_expand_bound( void *user
, boxf bound
, u32 item_index
)
300 scene_context
*s
= user
;
301 scene_vert
*pa
= &s
->arrvertices
[ s
->arrindices
[item_index
*3+0] ],
302 *pb
= &s
->arrvertices
[ s
->arrindices
[item_index
*3+1] ],
303 *pc
= &s
->arrvertices
[ s
->arrindices
[item_index
*3+2] ];
305 box_addpt( bound
, pa
->co
);
306 box_addpt( bound
, pb
->co
);
307 box_addpt( bound
, pc
->co
);
310 VG_STATIC
float scene_bh_centroid( void *user
, u32 item_index
, int axis
)
312 scene_context
*s
= user
;
313 scene_vert
*pa
= &s
->arrvertices
[ s
->arrindices
[item_index
*3+0] ],
314 *pb
= &s
->arrvertices
[ s
->arrindices
[item_index
*3+1] ],
315 *pc
= &s
->arrvertices
[ s
->arrindices
[item_index
*3+2] ];
321 min
= vg_minf( pa
->co
[axis
], pb
->co
[axis
] );
322 max
= vg_maxf( pa
->co
[axis
], pb
->co
[axis
] );
323 min
= vg_minf( min
, pc
->co
[axis
] );
324 max
= vg_maxf( max
, pc
->co
[axis
] );
326 return (min
+max
) * 0.5f
;
329 return (pa
->co
[axis
] + pb
->co
[axis
] + pc
->co
[axis
]) * (1.0f
/3.0f
);
333 VG_STATIC
void scene_bh_swap( void *user
, u32 ia
, u32 ib
)
335 scene_context
*s
= user
;
337 u32
*ti
= &s
->arrindices
[ia
*3];
338 u32
*tj
= &s
->arrindices
[ib
*3];
354 VG_STATIC
void scene_bh_debug( void *user
, u32 item_index
)
356 scene_context
*s
= user
;
357 u32 idx
= item_index
*3;
358 scene_vert
*pa
= &s
->arrvertices
[ s
->arrindices
[ idx
+0 ] ],
359 *pb
= &s
->arrvertices
[ s
->arrindices
[ idx
+1 ] ],
360 *pc
= &s
->arrvertices
[ s
->arrindices
[ idx
+2 ] ];
362 vg_line( pa
->co
, pb
->co
, 0xff0000ff );
363 vg_line( pb
->co
, pc
->co
, 0xff0000ff );
364 vg_line( pc
->co
, pa
->co
, 0xff0000ff );
367 VG_STATIC
int scene_bh_ray( void *user
, u32 index
, v3f co
,
368 v3f dir
, ray_hit
*hit
)
370 scene_context
*s
= user
;
373 u32
*tri
= &s
->arrindices
[ index
*3 ];
375 for( int i
=0; i
<3; i
++ )
376 v3_copy( s
->arrvertices
[tri
[i
]].co
, positions
[i
] );
379 if(ray_tri( positions
, co
, dir
, &t
)){
390 VG_STATIC
void scene_bh_closest( void *user
, u32 index
, v3f point
, v3f closest
)
392 scene_context
*s
= user
;
395 u32
*tri
= &s
->arrindices
[ index
*3 ];
396 for( int i
=0; i
<3; i
++ )
397 v3_copy( s
->arrvertices
[tri
[i
]].co
, positions
[i
] );
399 closest_on_triangle_1( point
, positions
, closest
);
402 VG_STATIC bh_system bh_system_scene
=
404 .expand_bound
= scene_bh_expand_bound
,
405 .item_centroid
= scene_bh_centroid
,
406 .item_closest
= scene_bh_closest
,
407 .item_swap
= scene_bh_swap
,
408 .item_debug
= scene_bh_debug
,
409 .cast_ray
= scene_bh_ray
413 * An extra step is added onto the end to calculate the hit normal
415 VG_STATIC
int scene_raycast( scene_context
*s
, bh_tree
*bh
,
416 v3f co
, v3f dir
, ray_hit
*hit
)
418 int count
= bh_ray( bh
, co
, dir
, hit
);
423 float *pa
= s
->arrvertices
[hit
->tri
[0]].co
,
424 *pb
= s
->arrvertices
[hit
->tri
[1]].co
,
425 *pc
= s
->arrvertices
[hit
->tri
[2]].co
;
427 v3_sub( pa
, pb
, v0
);
428 v3_sub( pc
, pb
, v1
);
429 v3_cross( v1
, v0
, hit
->normal
);
430 v3_normalize( hit
->normal
);
431 v3_muladds( co
, dir
, hit
->dist
, hit
->pos
);
437 VG_STATIC bh_tree
*scene_bh_create( void *lin_alloc
, scene_context
*s
)
439 u32 triangle_count
= s
->indice_count
/ 3;
440 return bh_create( lin_alloc
, &bh_system_scene
, s
, triangle_count
, 2 );