routes
[carveJwlIkooP6JGAAIwe30JlM.git] / scene.h
1 #ifndef SCENE_H
2 #define SCENE_H
3
4 #include "common.h"
5 #include "model.h"
6 #include "bvh.h"
7
8 typedef struct scene scene;
9
10 struct scene
11 {
12 glmesh mesh;
13
14 mdl_vert *verts;
15 u32 *indices;
16
17 bh_tree bhtris;
18 u32 vertex_count,
19 indice_count,
20 vertex_cap,
21 indice_cap;
22
23 boxf bbx;
24
25 u32 shadower_count,
26 shadower_cap;
27
28 mdl_submesh submesh;
29 };
30
31 #if 0
32 GLuint tex_dual_noise;
33 #endif
34
35 static void scene_init( scene *pscene )
36 {
37 pscene->verts = NULL;
38 pscene->indices = NULL;
39 pscene->vertex_count = 0;
40 pscene->indice_count = 0;
41 pscene->shadower_count = 0;
42 pscene->shadower_cap = 0;
43 pscene->submesh.indice_start = 0;
44 pscene->submesh.indice_count = 0;
45
46 v3_fill( pscene->bbx[0], 999999.9f );
47 v3_fill( pscene->bbx[1], -999999.9f );
48
49 #if 0
50 static int noise_ready = 0;
51 if( !noise_ready )
52 {
53 noise_ready = 1;
54
55 u8 *buf = malloc( 256*256*2 );
56
57 for( int i=0; i<256*256; i++ )
58 {
59 u8 val = rand()&0xff;
60 buf[i*2] = val;
61 }
62
63 for( int y=0; y<256; y++ )
64 {
65 for( int x=0; x<256; x++ )
66 {
67 u8 *pr = &buf[(y*256+x)*2],
68 *pg = &buf[(((y+17)&0xff)*256+((x+37)&0xff))*2+1];
69 *pg = *pr;
70 }
71 }
72
73 /* TODO: This texture should be delted somewhere */
74 glGenTextures( 1, &tex_dual_noise );
75 glBindTexture( GL_TEXTURE_2D, tex_dual_noise );
76 glTexImage2D( GL_TEXTURE_2D, 0, GL_RG, 256, 256, 0, GL_RG,
77 GL_UNSIGNED_BYTE, buf );
78
79 vg_tex2d_linear();
80 vg_tex2d_repeat();
81
82 free( buf );
83 }
84 #endif
85 }
86
87 static void *buffer_reserve( void *buffer, u32 count, u32 *cap, u32 amount,
88 size_t emsize )
89 {
90 if( count+amount > *cap )
91 {
92 *cap = VG_MAX( (*cap)*2, (*cap)+amount );
93
94 return realloc( buffer, (*cap) * emsize );
95 }
96
97 return buffer;
98 }
99
100 static void *buffer_fix( void *buffer, u32 count, u32 *cap, size_t emsize )
101 {
102 *cap = count;
103 return realloc( buffer, (*cap) * emsize );
104 }
105
106 /*
107 * Append a model into the scene with a given transform
108 */
109
110 static void scene_add_submesh( scene *pscene, mdl_header *mdl,
111 mdl_submesh *sm, m4x3f transform )
112 {
113 pscene->verts = buffer_reserve( pscene->verts, pscene->vertex_count,
114 &pscene->vertex_cap, sm->vertex_count, sizeof(mdl_vert) );
115 pscene->indices = buffer_reserve( pscene->indices, pscene->indice_count,
116 &pscene->indice_cap, sm->indice_count, sizeof(u32) );
117
118 m3x3f normal_matrix;
119 m3x3_copy( transform, normal_matrix );
120 v3_normalize( normal_matrix[0] );
121 v3_normalize( normal_matrix[1] );
122 v3_normalize( normal_matrix[2] );
123
124 /* Transform and place vertices */
125 mdl_vert *src_verts = mdl_submesh_vertices( mdl, sm );
126 u32 *src_indices = mdl_submesh_indices( mdl, sm );
127
128 boxf bbxnew;
129 box_copy( sm->bbx, bbxnew );
130 m4x3_transform_aabb( transform, bbxnew );
131 box_concat( pscene->bbx, bbxnew );
132
133 for( u32 i=0; i<sm->vertex_count; i++ )
134 {
135 mdl_vert *pvert = &pscene->verts[ pscene->vertex_count+i ],
136 *src = &src_verts[ i ];
137
138 m4x3_mulv( transform, src->co, pvert->co );
139 m3x3_mulv( normal_matrix, src->norm, pvert->norm );
140
141 v4_copy( src->colour, pvert->colour );
142 v2_copy( src->uv, pvert->uv );
143 }
144
145 for( u32 i=0; i<sm->indice_count; i++ )
146 {
147 u32 *pidx = &pscene->indices[ pscene->indice_count+i ];
148 *pidx = src_indices[i] + pscene->vertex_count;
149 }
150
151 pscene->vertex_count += sm->vertex_count;
152 pscene->indice_count += sm->indice_count;
153 }
154
155 static void scene_copy_slice( scene *pscene, mdl_submesh *sm )
156 {
157 sm->indice_start = pscene->submesh.indice_start;
158 sm->indice_count = pscene->indice_count - sm->indice_start;
159
160 sm->vertex_start = pscene->submesh.vertex_start;
161 sm->vertex_count = pscene->vertex_count - sm->vertex_start;
162
163 pscene->submesh.indice_start = pscene->indice_count;
164 pscene->submesh.vertex_start = pscene->vertex_count;
165 }
166
167 static void scene_fix( scene *pscene )
168 {
169 buffer_fix( pscene->verts, pscene->vertex_count,
170 &pscene->vertex_cap, sizeof( mdl_vert ));
171
172 buffer_fix( pscene->indices, pscene->indice_count,
173 &pscene->indice_cap, sizeof( mdl_vert ));
174 }
175
176 static void scene_upload( scene *pscene )
177 {
178 mesh_upload( &pscene->mesh,
179 pscene->verts, pscene->vertex_count,
180 pscene->indices, pscene->indice_count );
181
182 vg_info( "Scene upload\n" );
183 vg_info( " indices:%u\n", pscene->indice_count );
184 vg_info( " verts:%u\n", pscene->vertex_count );
185 }
186
187 static void scene_bind( scene *pscene )
188 {
189 mesh_bind( &pscene->mesh );
190 }
191
192 static void scene_draw( scene *pscene )
193 {
194 mesh_drawn( 0, pscene->indice_count );
195 }
196
197 static void scene_free( scene *pscene )
198 {
199 free( pscene->verts );
200 free( pscene->indices );
201
202 /* TODO: bvh */
203 }
204
205 static void scene_register(void)
206 {
207 }
208
209 /*
210 * BVH implementation
211 */
212
213 static void scene_bh_expand_bound( void *user, boxf bound, u32 item_index )
214 {
215 scene *s = user;
216 mdl_vert *pa = &s->verts[ s->indices[item_index*3+0] ],
217 *pb = &s->verts[ s->indices[item_index*3+1] ],
218 *pc = &s->verts[ s->indices[item_index*3+2] ];
219
220 box_addpt( bound, pa->co );
221 box_addpt( bound, pb->co );
222 box_addpt( bound, pc->co );
223 }
224
225 static float scene_bh_centroid( void *user, u32 item_index, int axis )
226 {
227 scene *s = user;
228 mdl_vert *pa = &s->verts[ s->indices[item_index*3+0] ],
229 *pb = &s->verts[ s->indices[item_index*3+1] ],
230 *pc = &s->verts[ s->indices[item_index*3+2] ];
231
232 return (pa->co[axis] + pb->co[axis] + pc->co[axis]) * (1.0f/3.0f);
233 }
234
235 static void scene_bh_swap( void *user, u32 ia, u32 ib )
236 {
237 scene *s = user;
238
239 u32 *ti = &s->indices[ia*3];
240 u32 *tj = &s->indices[ib*3];
241
242 u32 temp[3];
243 temp[0] = ti[0];
244 temp[1] = ti[1];
245 temp[2] = ti[2];
246
247 ti[0] = tj[0];
248 ti[1] = tj[1];
249 ti[2] = tj[2];
250
251 tj[0] = temp[0];
252 tj[1] = temp[1];
253 tj[2] = temp[2];
254 }
255
256 static void scene_bh_debug( void *user, u32 item_index )
257 {
258 scene *s = user;
259 u32 idx = item_index*3;
260 mdl_vert *pa = &s->verts[ s->indices[ idx+0 ] ],
261 *pb = &s->verts[ s->indices[ idx+1 ] ],
262 *pc = &s->verts[ s->indices[ idx+2 ] ];
263
264 vg_line( pa->co, pb->co, 0xff0000ff );
265 vg_line( pb->co, pc->co, 0xff0000ff );
266 vg_line( pc->co, pa->co, 0xff0000ff );
267 }
268
269 static int scene_bh_ray( void *user, u32 index, v3f co, v3f dir, ray_hit *hit )
270 {
271 scene *s = user;
272 v3f positions[3];
273
274 u32 *tri = &s->indices[ index*3 ];
275
276 for( int i=0; i<3; i++ )
277 v3_copy( s->verts[tri[i]].co, positions[i] );
278
279 float t;
280 if(ray_tri( positions, co, dir, &t ))
281 {
282 if( t < hit->dist )
283 {
284 hit->dist = t;
285 hit->tri = tri;
286 return 1;
287 }
288 }
289
290 return 0;
291 }
292
293 static bh_system bh_system_scene =
294 {
295 .expand_bound = scene_bh_expand_bound,
296 .item_centroid = scene_bh_centroid,
297 .item_swap = scene_bh_swap,
298 .item_debug = scene_bh_debug,
299 .cast_ray = scene_bh_ray
300 };
301
302 /*
303 * An extra step is added onto the end to calculate the hit normal
304 */
305 static int scene_raycast( scene *s, v3f co, v3f dir, ray_hit *hit )
306 {
307 int count = bh_ray( &s->bhtris, 0, co, dir, hit );
308
309 if( count )
310 {
311 v3f v0, v1;
312
313 float *pa = s->verts[hit->tri[0]].co,
314 *pb = s->verts[hit->tri[1]].co,
315 *pc = s->verts[hit->tri[2]].co;
316
317 v3_sub( pa, pb, v0 );
318 v3_sub( pc, pb, v1 );
319 v3_cross( v1, v0, hit->normal );
320 v3_normalize( hit->normal );
321 v3_muladds( co, dir, hit->dist, hit->pos );
322 }
323
324 return count;
325 }
326
327 static void scene_bh_create( scene *s )
328 {
329 u32 triangle_count = s->indice_count / 3;
330 bh_create( &s->bhtris, &bh_system_scene, s, triangle_count );
331 }
332
333 #endif