9 typedef struct scene scene
;
13 mdl_vert
*arrvertices
;
16 u32 vertex_count
, indice_count
,
17 max_vertices
, max_indices
;
23 /* Initialize a scene description with bounded buffers */
24 VG_STATIC scene
*scene_init( void *lin_alloc
, u32 max_verts
, u32 max_indices
)
26 u32 vertex_length
= max_verts
* sizeof(mdl_vert
),
27 index_length
= max_indices
* sizeof(u32
),
28 tot_size
= sizeof(scene
) + vertex_length
+ index_length
;
30 scene
*pscene
= vg_linear_alloc( lin_alloc
, tot_size
);
32 pscene
->arrvertices
= (mdl_vert
*)(pscene
+1);
33 pscene
->arrindices
= (u32
*)( pscene
->arrvertices
+ max_verts
);
35 pscene
->vertex_count
= 0;
36 pscene
->indice_count
= 0;
37 pscene
->max_vertices
= max_verts
;
38 pscene
->max_indices
= max_indices
;
40 memset( &pscene
->submesh
, 0, sizeof(mdl_submesh
) );
42 v3_fill( pscene
->bbx
[0], 999999.9f
);
43 v3_fill( pscene
->bbx
[1], -999999.9f
);
49 * Append a model into the scene with a given transform
51 VG_STATIC
void scene_add_submesh( scene
*pscene
, mdl_context
*mdl
,
52 mdl_submesh
*sm
, m4x3f transform
)
54 if( pscene
->vertex_count
+ sm
->vertex_count
> pscene
->max_vertices
)
56 vg_error( "%u(current) + %u > %u\n", pscene
->vertex_count
,
58 pscene
->max_vertices
);
60 vg_warn( "%p ... %p\n", pscene
, sm
);
61 vg_fatal_exit_loop( "Scene vertex buffer overflow" );
64 if( pscene
->indice_count
+ sm
->indice_count
> pscene
->max_indices
)
66 vg_error( "%u(current) + %u > %u\n", pscene
->indice_count
,
68 pscene
->max_indices
);
69 vg_warn( "%p ... %p\n", pscene
, sm
);
71 vg_fatal_exit_loop( "Scene index buffer overflow" );
74 mdl_vert
*src_verts
= mdl_submesh_vertices( mdl
, sm
),
75 *dst_verts
= &pscene
->arrvertices
[ pscene
->vertex_count
];
77 u32
*src_indices
= mdl_submesh_indices( mdl
, sm
),
78 *dst_indices
= &pscene
->arrindices
[ pscene
->indice_count
];
80 /* Transform and place vertices */
82 box_copy( sm
->bbx
, bbxnew
);
83 m4x3_transform_aabb( transform
, bbxnew
);
84 box_concat( pscene
->bbx
, bbxnew
);
87 m3x3_copy( transform
, normal_matrix
);
88 v3_normalize( normal_matrix
[0] );
89 v3_normalize( normal_matrix
[1] );
90 v3_normalize( normal_matrix
[2] );
92 for( u32 i
=0; i
<sm
->vertex_count
; i
++ )
94 mdl_vert
*pvert
= &dst_verts
[ i
],
95 *src
= &src_verts
[ i
];
97 m4x3_mulv( transform
, src
->co
, pvert
->co
);
98 m3x3_mulv( normal_matrix
, src
->norm
, pvert
->norm
);
100 pvert
->colour
[0] = src
->colour
[0];
101 pvert
->colour
[1] = src
->colour
[1];
102 pvert
->colour
[2] = src
->colour
[2];
103 pvert
->colour
[3] = src
->colour
[3];
104 pvert
->weights
[0] = src
->weights
[0];
105 pvert
->weights
[1] = src
->weights
[1];
106 pvert
->weights
[2] = src
->weights
[2];
107 pvert
->weights
[3] = src
->weights
[3];
108 v2_copy( src
->uv
, pvert
->uv
);
111 for( u32 i
=0; i
<sm
->indice_count
; i
++ )
113 dst_indices
[i
] = src_indices
[i
] + pscene
->vertex_count
;
116 pscene
->vertex_count
+= sm
->vertex_count
;
117 pscene
->indice_count
+= sm
->indice_count
;
122 * One by one adders for simplified access (mostly procedural stuff)
124 VG_STATIC
void scene_push_tri( scene
*pscene
, u32 tri
[3] )
126 if( pscene
->indice_count
+ 3 > pscene
->max_indices
)
127 vg_fatal_exit_loop( "Scene vertex buffer overflow" );
129 u32
*dst
= &pscene
->arrindices
[ pscene
->indice_count
];
135 pscene
->indice_count
+= 3;
138 VG_STATIC
void scene_push_vert( scene
*pscene
, mdl_vert
*v
)
140 if( pscene
->vertex_count
+ 1 > pscene
->max_vertices
)
141 vg_fatal_exit_loop( "Scene vertex buffer overflow" );
143 mdl_vert
*dst
= &pscene
->arrvertices
[ pscene
->vertex_count
];
146 pscene
->vertex_count
++;
149 VG_STATIC
void scene_copy_slice( scene
*pscene
, mdl_submesh
*sm
)
151 sm
->indice_start
= pscene
->submesh
.indice_start
;
152 sm
->indice_count
= pscene
->indice_count
- sm
->indice_start
;
154 sm
->vertex_start
= pscene
->submesh
.vertex_start
;
155 sm
->vertex_count
= pscene
->vertex_count
- sm
->vertex_start
;
157 pscene
->submesh
.indice_start
= pscene
->indice_count
;
158 pscene
->submesh
.vertex_start
= pscene
->vertex_count
;
161 /* finalization: tightly pack data */
162 __attribute__((warn_unused_result
))
163 VG_STATIC scene
*scene_fix( void *lin_alloc
, scene
*pscene
)
166 u32 vertex_count
= pscene
->vertex_count
,
167 indice_count
= pscene
->indice_count
,
168 vertex_length
= vertex_count
* sizeof(mdl_vert
),
169 index_length
= indice_count
* sizeof(u32
),
170 tot_size
= sizeof(scene
) + vertex_length
+ index_length
;
172 /* copy down index data */
173 void *dst_indices
= pscene
->arrvertices
+ vertex_length
;
174 memmove( dst_indices
, pscene
->arrindices
, index_length
);
177 pscene
= vg_linear_resize( lin_alloc
, pscene
, tot_size
);
179 pscene
->arrvertices
= (mdl_vert
*)(pscene
+1);
180 pscene
->arrindices
= (u32
*)(pscene
->arrvertices
+vertex_count
);
181 pscene
->max_vertices
= vertex_count
;
182 pscene
->max_indices
= indice_count
;
188 /* finalization: delete any offline buffers and reduce size */
189 __attribute__((warn_unused_result
))
190 VG_STATIC scene
*scene_free_offline_buffers( void *lin_alloc
, scene
*pscene
)
192 u32 tot_size
= sizeof(scene
);
194 scene
*src_scene
= pscene
;
195 mdl_vert
*src_verts
= pscene
->arrvertices
;
196 u32
*src_indices
= pscene
->arrindices
;
198 scene
*dst_scene
= vg_linear_resize( lin_alloc
, pscene
, tot_size
);
199 memcpy( dst_scene
, src_scene
, sizeof(scene
) );
201 dst_scene
->arrindices
= NULL
;
202 dst_scene
->arrvertices
= NULL
;
208 VG_STATIC
void scene_upload( scene
*pscene
, glmesh
*mesh
)
211 pscene
->arrvertices
, pscene
->vertex_count
,
212 pscene
->arrindices
, pscene
->indice_count
);
214 vg_info( "Scene upload\n" );
215 vg_info( " indices:%u\n", pscene
->indice_count
);
216 vg_info( " verts:%u\n", pscene
->vertex_count
);
223 VG_STATIC
void scene_bh_expand_bound( void *user
, boxf bound
, u32 item_index
)
226 mdl_vert
*pa
= &s
->arrvertices
[ s
->arrindices
[item_index
*3+0] ],
227 *pb
= &s
->arrvertices
[ s
->arrindices
[item_index
*3+1] ],
228 *pc
= &s
->arrvertices
[ s
->arrindices
[item_index
*3+2] ];
230 box_addpt( bound
, pa
->co
);
231 box_addpt( bound
, pb
->co
);
232 box_addpt( bound
, pc
->co
);
235 VG_STATIC
float scene_bh_centroid( void *user
, u32 item_index
, int axis
)
238 mdl_vert
*pa
= &s
->arrvertices
[ s
->arrindices
[item_index
*3+0] ],
239 *pb
= &s
->arrvertices
[ s
->arrindices
[item_index
*3+1] ],
240 *pc
= &s
->arrvertices
[ s
->arrindices
[item_index
*3+2] ];
242 return (pa
->co
[axis
] + pb
->co
[axis
] + pc
->co
[axis
]) * (1.0f
/3.0f
);
245 VG_STATIC
void scene_bh_swap( void *user
, u32 ia
, u32 ib
)
249 u32
*ti
= &s
->arrindices
[ia
*3];
250 u32
*tj
= &s
->arrindices
[ib
*3];
266 VG_STATIC
void scene_bh_debug( void *user
, u32 item_index
)
269 u32 idx
= item_index
*3;
270 mdl_vert
*pa
= &s
->arrvertices
[ s
->arrindices
[ idx
+0 ] ],
271 *pb
= &s
->arrvertices
[ s
->arrindices
[ idx
+1 ] ],
272 *pc
= &s
->arrvertices
[ s
->arrindices
[ idx
+2 ] ];
274 vg_line( pa
->co
, pb
->co
, 0xff0000ff );
275 vg_line( pb
->co
, pc
->co
, 0xff0000ff );
276 vg_line( pc
->co
, pa
->co
, 0xff0000ff );
279 VG_STATIC
int scene_bh_ray( void *user
, u32 index
, v3f co
,
280 v3f dir
, ray_hit
*hit
)
285 u32
*tri
= &s
->arrindices
[ index
*3 ];
287 for( int i
=0; i
<3; i
++ )
288 v3_copy( s
->arrvertices
[tri
[i
]].co
, positions
[i
] );
291 if(ray_tri( positions
, co
, dir
, &t
))
304 VG_STATIC
void scene_bh_closest( void *user
, u32 index
, v3f point
, v3f closest
)
309 u32
*tri
= &s
->arrindices
[ index
*3 ];
310 for( int i
=0; i
<3; i
++ )
311 v3_copy( s
->arrvertices
[tri
[i
]].co
, positions
[i
] );
313 closest_on_triangle_1( point
, positions
, closest
);
316 VG_STATIC bh_system bh_system_scene
=
318 .expand_bound
= scene_bh_expand_bound
,
319 .item_centroid
= scene_bh_centroid
,
320 .item_closest
= scene_bh_closest
,
321 .item_swap
= scene_bh_swap
,
322 .item_debug
= scene_bh_debug
,
323 .cast_ray
= scene_bh_ray
327 * An extra step is added onto the end to calculate the hit normal
329 VG_STATIC
int scene_raycast( scene
*s
, bh_tree
*bh
,
330 v3f co
, v3f dir
, ray_hit
*hit
)
332 int count
= bh_ray( bh
, co
, dir
, hit
);
338 float *pa
= s
->arrvertices
[hit
->tri
[0]].co
,
339 *pb
= s
->arrvertices
[hit
->tri
[1]].co
,
340 *pc
= s
->arrvertices
[hit
->tri
[2]].co
;
342 v3_sub( pa
, pb
, v0
);
343 v3_sub( pc
, pb
, v1
);
344 v3_cross( v1
, v0
, hit
->normal
);
345 v3_normalize( hit
->normal
);
346 v3_muladds( co
, dir
, hit
->dist
, hit
->pos
);
352 VG_STATIC bh_tree
*scene_bh_create( void *lin_alloc
, scene
*s
)
354 u32 triangle_count
= s
->indice_count
/ 3;
355 return bh_create( lin_alloc
, &bh_system_scene
, s
, triangle_count
, 2 );