8 typedef struct scene scene
;
12 mdl_vert
*arrvertices
;
15 u32 vertex_count
, indice_count
,
16 max_vertices
, max_indices
;
22 /* Initialize a scene description with bounded buffers */
23 VG_STATIC scene
*scene_init( void *lin_alloc
, u32 max_verts
, u32 max_indices
)
25 u32 vertex_length
= max_verts
* sizeof(mdl_vert
),
26 index_length
= max_indices
* sizeof(u32
),
27 tot_size
= sizeof(scene
) + vertex_length
+ index_length
;
29 scene
*pscene
= vg_linear_alloc( lin_alloc
, tot_size
);
31 pscene
->arrvertices
= (mdl_vert
*)(pscene
+1);
32 pscene
->arrindices
= (u32
*)( pscene
->arrvertices
+ max_verts
);
34 pscene
->vertex_count
= 0;
35 pscene
->indice_count
= 0;
36 pscene
->max_vertices
= max_verts
;
37 pscene
->max_indices
= max_indices
;
39 memset( &pscene
->submesh
, 0, sizeof(mdl_submesh
) );
41 v3_fill( pscene
->bbx
[0], 999999.9f
);
42 v3_fill( pscene
->bbx
[1], -999999.9f
);
48 * Append a model into the scene with a given transform
50 VG_STATIC
void scene_add_submesh( scene
*pscene
, mdl_context
*mdl
,
51 mdl_submesh
*sm
, m4x3f transform
)
53 if( pscene
->vertex_count
+ sm
->vertex_count
> pscene
->max_vertices
)
55 vg_error( "%u(current) + %u > %u\n", pscene
->vertex_count
,
57 pscene
->max_vertices
);
59 vg_warn( "%p ... %p\n", pscene
, sm
);
60 vg_fatal_exit_loop( "Scene vertex buffer overflow" );
63 if( pscene
->indice_count
+ sm
->indice_count
> pscene
->max_indices
)
65 vg_error( "%u(current) + %u > %u\n", pscene
->indice_count
,
67 pscene
->max_indices
);
68 vg_warn( "%p ... %p\n", pscene
, sm
);
70 vg_fatal_exit_loop( "Scene index buffer overflow" );
73 mdl_vert
*src_verts
= mdl_submesh_vertices( mdl
, sm
),
74 *dst_verts
= &pscene
->arrvertices
[ pscene
->vertex_count
];
76 u32
*src_indices
= mdl_submesh_indices( mdl
, sm
),
77 *dst_indices
= &pscene
->arrindices
[ pscene
->indice_count
];
79 /* Transform and place vertices */
81 box_copy( sm
->bbx
, bbxnew
);
82 m4x3_transform_aabb( transform
, bbxnew
);
83 box_concat( pscene
->bbx
, bbxnew
);
86 m3x3_copy( transform
, normal_matrix
);
87 v3_normalize( normal_matrix
[0] );
88 v3_normalize( normal_matrix
[1] );
89 v3_normalize( normal_matrix
[2] );
91 for( u32 i
=0; i
<sm
->vertex_count
; i
++ )
93 mdl_vert
*pvert
= &dst_verts
[ i
],
94 *src
= &src_verts
[ i
];
96 m4x3_mulv( transform
, src
->co
, pvert
->co
);
97 m3x3_mulv( normal_matrix
, src
->norm
, pvert
->norm
);
99 pvert
->colour
[0] = src
->colour
[0];
100 pvert
->colour
[1] = src
->colour
[1];
101 pvert
->colour
[2] = src
->colour
[2];
102 pvert
->colour
[3] = src
->colour
[3];
103 pvert
->weights
[0] = src
->weights
[0];
104 pvert
->weights
[1] = src
->weights
[1];
105 pvert
->weights
[2] = src
->weights
[2];
106 pvert
->weights
[3] = src
->weights
[3];
107 v2_copy( src
->uv
, pvert
->uv
);
110 for( u32 i
=0; i
<sm
->indice_count
; i
++ )
112 dst_indices
[i
] = src_indices
[i
] + pscene
->vertex_count
;
115 pscene
->vertex_count
+= sm
->vertex_count
;
116 pscene
->indice_count
+= sm
->indice_count
;
121 * One by one adders for simplified access (mostly procedural stuff)
123 VG_STATIC
void scene_push_tri( scene
*pscene
, u32 tri
[3] )
125 if( pscene
->indice_count
+ 3 > pscene
->max_indices
)
126 vg_fatal_exit_loop( "Scene vertex buffer overflow" );
128 u32
*dst
= &pscene
->arrindices
[ pscene
->indice_count
];
134 pscene
->indice_count
+= 3;
137 VG_STATIC
void scene_push_vert( scene
*pscene
, mdl_vert
*v
)
139 if( pscene
->vertex_count
+ 1 > pscene
->max_vertices
)
140 vg_fatal_exit_loop( "Scene vertex buffer overflow" );
142 mdl_vert
*dst
= &pscene
->arrvertices
[ pscene
->vertex_count
];
145 pscene
->vertex_count
++;
148 VG_STATIC
void scene_copy_slice( scene
*pscene
, mdl_submesh
*sm
)
150 sm
->indice_start
= pscene
->submesh
.indice_start
;
151 sm
->indice_count
= pscene
->indice_count
- sm
->indice_start
;
153 sm
->vertex_start
= pscene
->submesh
.vertex_start
;
154 sm
->vertex_count
= pscene
->vertex_count
- sm
->vertex_start
;
156 pscene
->submesh
.indice_start
= pscene
->indice_count
;
157 pscene
->submesh
.vertex_start
= pscene
->vertex_count
;
160 /* finalization: tightly pack data */
161 __attribute__((warn_unused_result
))
162 VG_STATIC scene
*scene_fix( void *lin_alloc
, scene
*pscene
)
165 u32 vertex_count
= pscene
->vertex_count
,
166 indice_count
= pscene
->indice_count
,
167 vertex_length
= vertex_count
* sizeof(mdl_vert
),
168 index_length
= indice_count
* sizeof(u32
),
169 tot_size
= sizeof(scene
) + vertex_length
+ index_length
;
171 /* copy down index data */
172 void *dst_indices
= pscene
->arrvertices
+ vertex_length
;
173 memmove( dst_indices
, pscene
->arrindices
, index_length
);
176 pscene
= vg_linear_resize( lin_alloc
, pscene
, tot_size
);
178 pscene
->arrvertices
= (mdl_vert
*)(pscene
+1);
179 pscene
->arrindices
= (u32
*)(pscene
->arrvertices
+vertex_count
);
180 pscene
->max_vertices
= vertex_count
;
181 pscene
->max_indices
= indice_count
;
187 /* finalization: delete any offline buffers and reduce size */
188 __attribute__((warn_unused_result
))
189 VG_STATIC scene
*scene_free_offline_buffers( void *lin_alloc
, scene
*pscene
)
191 u32 tot_size
= sizeof(scene
);
193 scene
*src_scene
= pscene
;
194 mdl_vert
*src_verts
= pscene
->arrvertices
;
195 u32
*src_indices
= pscene
->arrindices
;
197 scene
*dst_scene
= vg_linear_resize( lin_alloc
, pscene
, tot_size
);
198 memcpy( dst_scene
, src_scene
, sizeof(scene
) );
200 dst_scene
->arrindices
= NULL
;
201 dst_scene
->arrvertices
= NULL
;
207 VG_STATIC
void scene_upload( scene
*pscene
, glmesh
*mesh
)
210 pscene
->arrvertices
, pscene
->vertex_count
,
211 pscene
->arrindices
, pscene
->indice_count
);
213 vg_info( "Scene upload\n" );
214 vg_info( " indices:%u\n", pscene
->indice_count
);
215 vg_info( " verts:%u\n", pscene
->vertex_count
);
222 VG_STATIC
void scene_bh_expand_bound( void *user
, boxf bound
, u32 item_index
)
225 mdl_vert
*pa
= &s
->arrvertices
[ s
->arrindices
[item_index
*3+0] ],
226 *pb
= &s
->arrvertices
[ s
->arrindices
[item_index
*3+1] ],
227 *pc
= &s
->arrvertices
[ s
->arrindices
[item_index
*3+2] ];
229 box_addpt( bound
, pa
->co
);
230 box_addpt( bound
, pb
->co
);
231 box_addpt( bound
, pc
->co
);
234 VG_STATIC
float scene_bh_centroid( void *user
, u32 item_index
, int axis
)
237 mdl_vert
*pa
= &s
->arrvertices
[ s
->arrindices
[item_index
*3+0] ],
238 *pb
= &s
->arrvertices
[ s
->arrindices
[item_index
*3+1] ],
239 *pc
= &s
->arrvertices
[ s
->arrindices
[item_index
*3+2] ];
241 return (pa
->co
[axis
] + pb
->co
[axis
] + pc
->co
[axis
]) * (1.0f
/3.0f
);
244 VG_STATIC
void scene_bh_swap( void *user
, u32 ia
, u32 ib
)
248 u32
*ti
= &s
->arrindices
[ia
*3];
249 u32
*tj
= &s
->arrindices
[ib
*3];
265 VG_STATIC
void scene_bh_debug( void *user
, u32 item_index
)
268 u32 idx
= item_index
*3;
269 mdl_vert
*pa
= &s
->arrvertices
[ s
->arrindices
[ idx
+0 ] ],
270 *pb
= &s
->arrvertices
[ s
->arrindices
[ idx
+1 ] ],
271 *pc
= &s
->arrvertices
[ s
->arrindices
[ idx
+2 ] ];
273 vg_line( pa
->co
, pb
->co
, 0xff0000ff );
274 vg_line( pb
->co
, pc
->co
, 0xff0000ff );
275 vg_line( pc
->co
, pa
->co
, 0xff0000ff );
278 VG_STATIC
int scene_bh_ray( void *user
, u32 index
, v3f co
,
279 v3f dir
, ray_hit
*hit
)
284 u32
*tri
= &s
->arrindices
[ index
*3 ];
286 for( int i
=0; i
<3; i
++ )
287 v3_copy( s
->arrvertices
[tri
[i
]].co
, positions
[i
] );
290 if(ray_tri( positions
, co
, dir
, &t
))
303 VG_STATIC
void scene_bh_closest( void *user
, u32 index
, v3f point
, v3f closest
)
308 u32
*tri
= &s
->arrindices
[ index
*3 ];
309 for( int i
=0; i
<3; i
++ )
310 v3_copy( s
->arrvertices
[tri
[i
]].co
, positions
[i
] );
312 closest_on_triangle_1( point
, positions
, closest
);
315 VG_STATIC bh_system bh_system_scene
=
317 .expand_bound
= scene_bh_expand_bound
,
318 .item_centroid
= scene_bh_centroid
,
319 .item_closest
= scene_bh_closest
,
320 .item_swap
= scene_bh_swap
,
321 .item_debug
= scene_bh_debug
,
322 .cast_ray
= scene_bh_ray
326 * An extra step is added onto the end to calculate the hit normal
328 VG_STATIC
int scene_raycast( scene
*s
, bh_tree
*bh
,
329 v3f co
, v3f dir
, ray_hit
*hit
)
331 int count
= bh_ray( bh
, co
, dir
, hit
);
337 float *pa
= s
->arrvertices
[hit
->tri
[0]].co
,
338 *pb
= s
->arrvertices
[hit
->tri
[1]].co
,
339 *pc
= s
->arrvertices
[hit
->tri
[2]].co
;
341 v3_sub( pa
, pb
, v0
);
342 v3_sub( pc
, pb
, v1
);
343 v3_cross( v1
, v0
, hit
->normal
);
344 v3_normalize( hit
->normal
);
345 v3_muladds( co
, dir
, hit
->dist
, hit
->pos
);
351 VG_STATIC bh_tree
*scene_bh_create( void *lin_alloc
, scene
*s
)
353 u32 triangle_count
= s
->indice_count
/ 3;
354 return bh_create( lin_alloc
, &bh_system_scene
, s
, triangle_count
, 2 );