7 typedef struct scene scene
;
8 typedef struct bvh_node bvh_node
;
44 GLuint tex_dual_noise
;
46 static void scene_init( scene
*pscene
)
49 pscene
->indices
= NULL
;
50 pscene
->vertex_count
= 0;
51 pscene
->indice_count
= 0;
52 pscene
->shadowers
= NULL
;
53 pscene
->shadower_count
= 0;
54 pscene
->shadower_cap
= 0;
55 pscene
->submesh
.indice_start
= 0;
56 pscene
->submesh
.indice_count
= 0;
58 v3_fill( pscene
->bbx
[0], 999999.9f
);
59 v3_fill( pscene
->bbx
[1], -999999.9f
);
61 static int noise_ready
= 0;
66 u8
*buf
= malloc( 256*256*2 );
68 for( int i
=0; i
<256*256; i
++ )
74 for( int y
=0; y
<256; y
++ )
76 for( int x
=0; x
<256; x
++ )
78 u8
*pr
= &buf
[(y
*256+x
)*2],
79 *pg
= &buf
[(((y
+17)&0xff)*256+((x
+37)&0xff))*2+1];
84 /* TODO: This texture should be delted somewhere */
85 glGenTextures( 1, &tex_dual_noise
);
86 glBindTexture( GL_TEXTURE_2D
, tex_dual_noise
);
87 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RG
, 256, 256, 0, GL_RG
,
88 GL_UNSIGNED_BYTE
, buf
);
97 static void *buffer_reserve( void *buffer
, u32 count
, u32
*cap
, u32 amount
,
100 if( count
+amount
> *cap
)
102 *cap
= VG_MAX( (*cap
)*2, (*cap
)+amount
);
104 return realloc( buffer
, (*cap
) * emsize
);
111 * Append a model into the scene with a given transform
113 static void scene_add_model( scene
*pscene
, model
*mdl
, submodel
*submodel
,
114 v3f pos
, float yaw
, float scale
)
116 pscene
->verts
= buffer_reserve( pscene
->verts
, pscene
->vertex_count
,
117 &pscene
->vertex_cap
, submodel
->vertex_count
, sizeof(model_vert
) );
118 pscene
->indices
= buffer_reserve( pscene
->indices
, pscene
->indice_count
,
119 &pscene
->indice_cap
, submodel
->indice_count
, sizeof(u32
) );
121 if( submodel
->sdf_type
)
123 pscene
->shadowers
= buffer_reserve( pscene
->shadowers
,
124 pscene
->shadower_count
, &pscene
->shadower_cap
, 1,
125 sizeof( struct shadower
));
127 struct shadower
*shadower
=
128 &pscene
->shadowers
[ pscene
->shadower_count
++ ];
130 shadower
->sdf
= submodel
->sdf
;
131 shadower
->sdf_type
= submodel
->sdf_type
;
133 v2_muls( shadower
->sdf
.info
, scale
, shadower
->sdf
.info
);
134 v3_muls( shadower
->sdf
.origin
, scale
, shadower
->sdf
.origin
);
135 v3_add( pos
, shadower
->sdf
.origin
, shadower
->sdf
.origin
);
138 /* Transform and place vertices */
139 model_vert
*src_verts
= submodel_vert_data( mdl
, submodel
);
140 u32
*src_indices
= submodel_indice_data( mdl
, submodel
);
143 m4x3_identity( mtx
);
144 m4x3_translate( mtx
, pos
);
145 m4x3_rotate_y( mtx
, yaw
);
146 m4x3_scale( mtx
, scale
);
149 box_copy( submodel
->bbx
, bbxnew
);
150 m4x3_transform_aabb( mtx
, bbxnew
);
151 box_concat( pscene
->bbx
, bbxnew
);
154 m4x3_to_3x3( mtx
, rotation
);
156 float rand_hue
= vg_randf();
158 for( u32 i
=0; i
<submodel
->vertex_count
; i
++ )
160 model_vert
*pvert
= &pscene
->verts
[ pscene
->vertex_count
+i
],
161 *src
= &src_verts
[ i
];
163 m4x3_mulv( mtx
, src
->co
, pvert
->co
);
164 m3x3_mulv( rotation
, src
->norm
, pvert
->norm
);
166 v4_copy( src
->colour
, pvert
->colour
);
167 v2_copy( src
->uv
, pvert
->uv
);
169 float rel_y
= src
->co
[1] / submodel
->bbx
[1][1];
170 pvert
->colour
[0] = rel_y
;
171 pvert
->colour
[2] = rand_hue
;
174 for( u32 i
=0; i
<submodel
->indice_count
; i
++ )
176 u32
*pidx
= &pscene
->indices
[ pscene
->indice_count
+i
];
177 *pidx
= src_indices
[i
] + pscene
->vertex_count
;
180 pscene
->vertex_count
+= submodel
->vertex_count
;
181 pscene
->indice_count
+= submodel
->indice_count
;
184 static void scene_copy_slice( scene
*pscene
, submodel
*sm
)
186 sm
->indice_start
= pscene
->submesh
.indice_start
;
187 sm
->indice_count
= pscene
->indice_count
- sm
->indice_start
;
189 sm
->vertex_start
= pscene
->submesh
.vertex_start
;
190 sm
->vertex_count
= pscene
->vertex_count
- sm
->vertex_start
;
192 pscene
->submesh
.indice_start
= pscene
->indice_count
;
193 pscene
->submesh
.vertex_start
= pscene
->vertex_count
;
196 static void scene_shadow_sphere( scene
*pscene
, v3f sphere
,
197 v4f params
, v3f lightdir
)
199 for( int i
=0; i
<pscene
->vertex_count
; i
++ )
201 model_vert
*vert
= &pscene
->verts
[i
];
204 v3_sub( sphere
, vert
->co
, delta
);
206 float d
= v3_dot( lightdir
, delta
);
209 v3_muls( lightdir
, d
, closest
);
210 float dist
= v3_dist( closest
, delta
),
211 shading
= vg_maxf( dist
- params
[0], 0.0f
);
213 shading
= vg_minf( shading
* params
[1], 1.0f
);
214 vert
->colour
[1] *= shading
;
218 static void scene_shadow_gradient( scene
*pscene
, int comp
,
219 float start
, float length
)
221 float scale
= 1.0f
/ length
;
223 for( int i
=0; i
<pscene
->vertex_count
; i
++ )
225 model_vert
*vert
= &pscene
->verts
[i
];
226 float shading
= start
+ vert
->co
[comp
] * scale
;
228 vert
->colour
[1] = shading
;
234 * Experimental SDF based shadows
236 * https://iquilezles.org/articles/distfunctions/
238 static float sd_cone( v3f co
, sdf_primative
*prim
)
240 float bound
= prim
->info
[1]*1.75f
;
241 if( v3_dist2( prim
->origin
, co
) > bound
*bound
)
245 v3_sub( co
, prim
->origin
, p
);
247 float h
= prim
->info
[1];
248 v2f c
= { prim
->info
[2], prim
->info
[3] };
251 v2_muls( (v2f
){ c
[0]/c
[1], -1.0f
}, h
, q
);
253 w
[0] = v2_length( (v2f
){ p
[0], p
[2] } );
256 v2_muladds( w
, q
, -vg_clampf( v2_dot(w
,q
)/v2_dot(q
,q
), 0.0f
, 1.0f
), a
);
257 v2_muladd( w
, q
, (v2f
){ vg_clampf( w
[0]/q
[0], 0.0f
, 1.0f
), 1.0f
}, b
);
259 float k
= vg_signf( q
[1] ),
260 d
= vg_minf( v2_dot( a
,a
), v2_dot( b
,b
) ),
261 s
= vg_maxf( k
*(w
[0]*q
[1]-w
[1]*q
[0]), k
*(w
[1]-q
[1]) );
263 return sqrtf(d
)*vg_signf(s
);
266 #define CACHE_AMBIENT_SHAPES
268 static float scene_ambient_sample( scene
*pscene
, v3f pos
, v3f dir
)
272 #ifdef CACHE_AMBIENT_SHAPES
273 static struct shadower
*local_shadowers
[32];
274 static int local_shadower_count
= 0;
275 static v3f local_shadower_last
= { -99999.9f
, -999999.9f
, -9999999.9f
};
277 if( v3_dist2( pos
, local_shadower_last
) > 10.0f
*10.0f
)
279 local_shadower_count
= 0;
280 v3_copy( pos
, local_shadower_last
);
282 for( int k
=0; k
<pscene
->shadower_count
; k
++ )
284 struct shadower
*shadower
= &pscene
->shadowers
[k
];
286 if( sd_cone( pos
, &shadower
->sdf
) <= 20.0f
)
288 local_shadowers
[ local_shadower_count
++ ] = shadower
;
289 if( local_shadower_count
== vg_list_size( local_shadowers
) )
296 for( int j
=0; j
<5; j
++ )
299 v3_muladds( pos
, dir
, 1.5f
*(float)j
, tracepos
);
301 float mindist
= 99999.9f
;
303 #ifndef CACHE_AMBIENT_SHAPES
305 for( int k
=0; k
<pscene
->shadower_count
; k
++ ){
306 struct shadower
*shadower
= &pscene
->shadowers
[k
];
309 for( int k
=0; k
<local_shadower_count
; k
++ ){
310 struct shadower
*shadower
= local_shadowers
[k
];
313 float dist
= vg_maxf( 0.0f
, sd_cone( tracepos
, &shadower
->sdf
));
314 mindist
= vg_minf( mindist
, dist
);
318 accum
+= vg_clampf( 1.0f
- mindist
, 0.0f
, 1.0f
)*0.2f
;
325 #define JUST_DO_EVERY_VERT
327 static void scene_compute_occlusion( scene
*pscene
)
329 v3f sundir
= { 0.2f
, 0.9f
, 0.2f
};
330 v3_normalize( sundir
);
332 /* TODO: Make this sample grid be dynamically required.
334 * 1. Only resample the light grid (1x1x1), when a vertex is outside the
337 * 2. Reorder all vertices so that each group of vertices that fit in a
338 * cube are next to eachother in the buffer. This will save cache
341 * for the sorting algorithm, i think we can already assume that *most
342 * vertices will be quite close to eachother. so instead of doing an
343 * exhaustive search we can reorder 1k chunks at a time.
347 v3_sub( pscene
->bbx
[1], pscene
->bbx
[0], sample_area
);
348 v3_ceil( sample_area
, sample_area
);
349 int ax
= sample_area
[0],
354 float *samplegrid
= malloc( ax
*ay
*az
* sizeof(float) );
356 for( int x
=0; x
<ax
; x
++ ){
357 for( int y
=0; y
<ay
; y
++ ){
358 for( int z
=0; z
<az
; z
++ )
360 v3f sample_pos
= { x
,y
,z
};
361 v3_add( pscene
->bbx
[0], sample_pos
, sample_pos
);
362 float accum
= scene_ambient_sample( pscene
, sample_pos
, sundir
);
364 samplegrid
[x
+ y
*ax
+ z
*ax
*ay
] = accum
;
367 v3i cube_pos
= { -999999, -999999, -999999 };
368 int cube_resamples
= 0, hits
= 0, misses
= 0;
370 float s0
=0.0f
,s1
=0.0f
,s2
=0.0f
,s3
=0.0f
,s4
=0.0f
,s5
=0.0f
,s6
=0.0f
,s7
=0.0f
;
373 for( int i
=0; i
<pscene
->vertex_count
; i
++ )
375 model_vert
*vert
= &pscene
->verts
[i
];
379 v3_sub( vert
->co
, pscene
->bbx
[0], q
);
381 v3_copy( vert
->co
, q
);
391 #ifndef JUST_DO_EVERY_VERT
401 s0
= samplegrid
[ x
+ y
*ax
+ z
*ax
*ay
],
402 s1
= samplegrid
[(x
+1) + y
*ax
+ z
*ax
*ay
],
403 s2
= samplegrid
[ x
+ (y
+1)*ax
+ z
*ax
*ay
],
404 s3
= samplegrid
[(x
+1) + (y
+1)*ax
+ z
*ax
*ay
],
405 s4
= samplegrid
[ x
+ y
*ax
+ (z
+1)*ax
*ay
],
406 s5
= samplegrid
[(x
+1) + y
*ax
+ (z
+1)*ax
*ay
],
407 s6
= samplegrid
[ x
+ (y
+1)*ax
+ (z
+1)*ax
*ay
],
408 s7
= samplegrid
[(x
+1) + (y
+1)*ax
+ (z
+1)*ax
*ay
],
410 if( x
!=cube_pos
[0] || y
!=cube_pos
[1] || z
!=cube_pos
[2] )
416 s0
= scene_ambient_sample( pscene
, (v3f
){ x
,y
,z
}, sundir
);
417 s1
= scene_ambient_sample( pscene
, (v3f
){ x
+1,y
,z
}, sundir
);
418 s2
= scene_ambient_sample( pscene
, (v3f
){ x
,y
+1,z
}, sundir
);
419 s3
= scene_ambient_sample( pscene
, (v3f
){ x
+1,y
+1,z
}, sundir
);
420 s4
= scene_ambient_sample( pscene
, (v3f
){ x
,y
,z
+1 }, sundir
);
421 s5
= scene_ambient_sample( pscene
, (v3f
){ x
+1,y
,z
+1 }, sundir
);
422 s6
= scene_ambient_sample( pscene
, (v3f
){ x
,y
+1,z
+1 }, sundir
);
423 s7
= scene_ambient_sample( pscene
, (v3f
){ x
+1,y
+1,z
+1 }, sundir
);
434 s0_s1
= vg_lerpf( s0
, s1
, q
[0] ),
435 s2_s3
= vg_lerpf( s2
, s3
, q
[0] ),
436 s4_s5
= vg_lerpf( s4
, s5
, q
[0] ),
437 s6_s7
= vg_lerpf( s6
, s7
, q
[0] ),
439 s0s1_s2s3
= vg_lerpf( s0_s1
, s2_s3
, q
[1] ),
440 s4s5_s6s7
= vg_lerpf( s4_s5
, s6_s7
, q
[1] ),
441 s0s1s2s3_s4s5s6s7
= vg_lerpf( s0s1_s2s3
, s4s5_s6s7
, q
[2] );
443 vert
->colour
[1] = s0s1s2s3_s4s5s6s7
;
445 vert
->colour
[1] = scene_ambient_sample( pscene
, vert
->co
, sundir
);
450 int cube_resamples
= -1, misses
= 0, hits
= 0;
453 int static_samples
= ax
*ay
*az
,
454 vertex_samples
= pscene
->vertex_count
;
456 if( cube_resamples
< static_samples
)
457 vg_success( "Walking cube beat static grid (%d<%d. %d)!\n",
458 cube_resamples
, static_samples
, vertex_samples
);
460 vg_warn( "Walking cube was worse than static grid (%d<%d. %d).\n",
461 cube_resamples
, static_samples
, vertex_samples
);
463 vg_info( "Hits; %d, misses: %d\n", hits
, misses
);
471 for( int i
=0; i
<pscene
->vertex_count
; i
++ )
473 model_vert
*vert
= &pscene
->verts
[i
];
476 for( int j
=0; j
<5; j
++ )
479 v3_copy( vert
->co
, tracepos
);
480 v3_muladds( tracepos
, sundir
, 1.5f
*(float)j
, tracepos
);
482 float mindist
= 99999.9f
;
484 for( int k
=0; k
<pscene
->shadower_count
; k
++ )
486 struct shadower
*shadower
= &pscene
->shadowers
[k
];
487 float dist
= vg_maxf( 0.0f
, sd_cone( tracepos
, &shadower
->sdf
));
488 mindist
= vg_minf( mindist
, dist
);
491 accum
+= vg_clampf( 1.0f
- mindist
, 0.0f
, 1.0f
)*0.2f
;
494 vert
->colour
[1] = vg_minf( accum
, 1.0f
);
498 static void scene_upload( scene
*pscene
)
500 mesh_upload( &pscene
->mesh
,
501 pscene
->verts
, pscene
->vertex_count
,
502 pscene
->indices
, pscene
->indice_count
);
504 vg_info( "Scene upload\n" );
505 vg_info( " indices:%u\n", pscene
->indice_count
);
506 vg_info( " verts:%u\n", pscene
->vertex_count
);
509 float scene_tree_sway
= 0.1f
;
512 static void scene_foliage_shader_use(void)
514 SHADER_USE( shader_debug_vcol
);
516 glUniformMatrix4fv( SHADER_UNIFORM( shader_debug_vcol
, "uPv" ),
517 1, GL_FALSE
, (float *)vg_pv
);
519 glUniform1i( SHADER_UNIFORM( shader_debug_vcol
, "uMode" ), debugview
);
520 glUniform1i( SHADER_UNIFORM( shader_debug_vcol
, "uTexMain" ), 0 );
522 glUniform1i( SHADER_UNIFORM( shader_debug_vcol
, "uTexGradients" ), 1 );
523 vg_tex2d_bind( &tex_gradients
, 1 );
525 glUniform1i( SHADER_UNIFORM( shader_debug_vcol
, "uTexNoise" ), 2 );
526 glActiveTexture( GL_TEXTURE2
);
527 glBindTexture( GL_TEXTURE_2D
, tex_dual_noise
);
529 glUniform1f( SHADER_UNIFORM( shader_debug_vcol
, "uTime" ), vg_time
);
530 glUniform1f( SHADER_UNIFORM( shader_debug_vcol
, "uSwayAmt" ),
535 static void scene_bind( scene
*pscene
)
537 mesh_bind( &pscene
->mesh
);
540 static void scene_draw( scene
*pscene
)
542 mesh_drawn( 0, pscene
->indice_count
);
545 static void scene_debugsdf( scene
*pscene
)
547 for( int i
=0; i
<pscene
->shadower_count
; i
++ )
549 struct shadower
*shadower
= &pscene
->shadowers
[i
];
552 v3_copy( shadower
->sdf
.origin
, base
);
553 base
[1] -= shadower
->sdf
.info
[1];
554 v3_copy( base
, side
);
555 side
[0] += shadower
->sdf
.info
[0];
557 vg_line2( shadower
->sdf
.origin
, base
, 0xff00ff00, 0xff0000ff );
558 vg_line2( side
, base
, 0xff00ff00, 0xff0000ff );
559 vg_line( side
, shadower
->sdf
.origin
, 0xff00ff00 );
562 v3f p0
= { pscene
->bbx
[0][0], pscene
->bbx
[0][1], pscene
->bbx
[0][2] },
563 p1
= { pscene
->bbx
[0][0], pscene
->bbx
[1][1], pscene
->bbx
[0][2] },
564 p2
= { pscene
->bbx
[1][0], pscene
->bbx
[1][1], pscene
->bbx
[0][2] },
565 p3
= { pscene
->bbx
[1][0], pscene
->bbx
[0][1], pscene
->bbx
[0][2] },
567 p4
= { pscene
->bbx
[0][0], pscene
->bbx
[0][1], pscene
->bbx
[1][2] },
568 p5
= { pscene
->bbx
[0][0], pscene
->bbx
[1][1], pscene
->bbx
[1][2] },
569 p6
= { pscene
->bbx
[1][0], pscene
->bbx
[1][1], pscene
->bbx
[1][2] },
570 p7
= { pscene
->bbx
[1][0], pscene
->bbx
[0][1], pscene
->bbx
[1][2] };
572 u32 col
= 0xffff00c8;
573 vg_line( p0
, p1
, col
);
574 vg_line( p1
, p2
, col
);
575 vg_line( p2
, p3
, col
);
576 vg_line( p3
, p0
, col
);
578 vg_line( p4
, p5
, col
);
579 vg_line( p5
, p6
, col
);
580 vg_line( p6
, p7
, col
);
581 vg_line( p7
, p4
, col
);
583 vg_line( p0
, p4
, col
);
584 vg_line( p1
, p5
, col
);
585 vg_line( p2
, p6
, col
);
586 vg_line( p3
, p7
, col
);
589 static void scene_register(void)
594 /* Physics segment */
596 static int triangle_raycast2d( v3f pA
, v3f pB
, v3f pC
, v3f ray
, float *height
)
598 v2f v0
, v1
, v2
, vp
, vp2
;
599 float d
, bca
= 0.f
, bcb
= 0.f
, bcc
= 0.f
;
601 v0
[0] = pB
[0] - pA
[0];
602 v0
[1] = pB
[2] - pA
[2];
603 v1
[0] = pC
[0] - pA
[0];
604 v1
[1] = pC
[2] - pA
[2];
605 v2
[0] = pB
[0] - pC
[0];
606 v2
[1] = pB
[2] - pC
[2];
608 d
= 1.f
/ (v0
[0]*v1
[1] - v1
[0]*v0
[1]);
611 /* Backface culling */
612 if( v2_cross( v0
, v1
) > 0.f
)
616 vp
[0] = ray
[0] - pA
[0];
617 vp
[1] = ray
[2] - pA
[2];
619 if( v2_cross( v0
, vp
) > 0.f
) return 0;
620 if( v2_cross( vp
, v1
) > 0.f
) return 0;
622 vp2
[0] = ray
[0] - pB
[0];
623 vp2
[1] = ray
[2] - pB
[2];
625 if( v2_cross( vp2
, v2
) > 0.f
) return 0;
627 bcb
= (vp
[0]*v1
[1] - v1
[0]*vp
[1]) * d
;
628 bcc
= (v0
[0]*vp
[1] - vp
[0]*v0
[1]) * d
;
629 bca
= 1.f
- bcb
- bcc
;
631 *height
= pA
[1]*bca
+ pB
[1]*bcb
+ pC
[1]*bcc
;
636 static int sample_scene_height( scene
*pscene
, v3f pos
, v3f norm
)
638 for( int i
=0; i
<pscene
->indice_count
/3; i
++ )
640 u32
*tri
= &pscene
->indices
[i
*3];
642 float *pA
= pscene
->verts
[tri
[0]].co
,
643 *pB
= pscene
->verts
[tri
[1]].co
,
644 *pC
= pscene
->verts
[tri
[2]].co
;
647 if( triangle_raycast2d( pA
, pB
, pC
, pos
, &height
))
654 v3_sub( pA
, pB
, v0
);
655 v3_sub( pC
, pB
, v1
);
656 v3_cross( v1
, v0
, norm
);
657 v3_normalize( norm
);
666 static void sample_scene_normal( scene
*pscene
, v3f pos
, v3f normal
)
668 for( int i
=0; i
<pscene
->indice_count
/3; i
++ )
670 u32
*tri
= &pscene
->indices
[i
*3];
673 if( triangle_raycast2d(
674 pscene
->verts
[ tri
[0] ].co
,
675 pscene
->verts
[ tri
[1] ].co
,
676 pscene
->verts
[ tri
[2] ].co
, pos
, &height
))
680 v3_sub( pscene
->verts
[ tri
[1] ].co
,
681 pscene
->verts
[ tri
[0] ].co
,
684 v3_sub( pscene
->verts
[ tri
[2] ].co
,
685 pscene
->verts
[ tri
[0] ].co
,
688 v3_cross( v0
, v1
, normal
);
689 v3_normalize( normal
);
703 /* if il is 0, this is a leaf */
705 union{ u32 ir
, start
; };
708 static void bvh_update_bounds( scene
*s
, u32 inode
)
710 bvh_node
*node
= &s
->bvh
.nodes
[ inode
];
712 box_init_inf( node
->bbx
);
713 for( u32 i
=0; i
<node
->count
; i
++ )
715 u32 idx
= node
->start
+i
;
716 model_vert
*pa
= &s
->verts
[ s
->indices
[idx
*3+0] ],
717 *pb
= &s
->verts
[ s
->indices
[idx
*3+1] ],
718 *pc
= &s
->verts
[ s
->indices
[idx
*3+2] ];
720 box_addpt( node
->bbx
, pa
->co
);
721 box_addpt( node
->bbx
, pb
->co
);
722 box_addpt( node
->bbx
, pc
->co
);
726 static void bvh_subdiv( scene
*s
, u32 inode
)
728 bvh_node
*node
= &s
->bvh
.nodes
[ inode
];
731 v3_sub( node
->bbx
[1], node
->bbx
[0], extent
);
734 if( extent
[1] > extent
[0] ) axis
= 1;
735 if( extent
[2] > extent
[axis
] ) axis
= 2;
737 float split
= node
->bbx
[0][axis
] + extent
[axis
]*0.5f
;
739 /* To beat: 121,687 / 136,579
744 for( u32 t
=0; t
<node
->count
; t
++ )
746 u32
*ti
= &s
->indices
[(node
->start
+t
)*3];
747 float a
= s
->verts
[ti
[0]].co
[axis
],
748 b
= s
->verts
[ti
[1]].co
[axis
],
749 c
= s
->verts
[ti
[2]].co
[axis
];
752 avg
/= (float)node
->count
;
757 j
= i
+ node
->count
-1;
761 u32
*ti
= &s
->indices
[i
*3];
763 float a
= s
->verts
[ti
[0]].co
[axis
],
764 b
= s
->verts
[ti
[1]].co
[axis
],
765 c
= s
->verts
[ti
[2]].co
[axis
];
767 if( ((a
+b
+c
) / 3.0f
) < split
)
771 /* Swap triangle indices */
772 u32
*tj
= &s
->indices
[j
*3];
790 u32 left_count
= i
- node
->start
;
791 if( left_count
== 0 || left_count
== node
->count
) return;
793 u32 il
= s
->bvh
.node_count
++,
794 ir
= s
->bvh
.node_count
++;
796 struct bvh_node
*lnode
= &s
->bvh
.nodes
[il
],
797 *rnode
= &s
->bvh
.nodes
[ir
];
799 lnode
->start
= node
->start
;
800 lnode
->count
= left_count
;
802 rnode
->count
= node
->count
- left_count
;
808 bvh_update_bounds( s
, il
);
809 bvh_update_bounds( s
, ir
);
814 static void bvh_create( scene
*s
)
816 u32 triangle_count
= s
->indice_count
/ 3;
817 s
->bvh
.nodes
= malloc( sizeof(struct bvh_node
) * (triangle_count
*2-1) );
819 bvh_node
*root
= &s
->bvh
.nodes
[0];
820 s
->bvh
.node_count
= 1;
824 root
->count
= triangle_count
;
827 bvh_update_bounds( s
, 0 );
831 realloc( s
->bvh
.nodes
, sizeof(struct bvh_node
) * s
->bvh
.node_count
);
833 vg_success( "BVH done, size: %u/%u\n", s
->bvh
.node_count
,
834 (triangle_count
*2-1) );
837 static void bvh_debug_node( scene
*s
, u32 inode
, v3f pos
, u32 colour
)
839 struct bvh_node
*node
= &s
->bvh
.nodes
[ inode
];
841 if( (pos
[0] >= node
->bbx
[0][0] && pos
[0] <= node
->bbx
[1][0]) &&
842 (pos
[2] >= node
->bbx
[0][2] && pos
[2] <= node
->bbx
[1][2]) )
846 vg_line_boxf( node
->bbx
, colour
);
848 bvh_debug_node( s
, node
->il
, pos
, colour
);
849 bvh_debug_node( s
, node
->ir
, pos
, colour
);
853 vg_line_boxf( node
->bbx
, 0xff00ff00 );
854 for( u32 i
=0; i
<node
->count
; i
++ )
856 u32 idx
= (node
->start
+i
)*3;
858 model_vert
*pa
= &s
->verts
[ s
->indices
[ idx
+0 ] ],
859 *pb
= &s
->verts
[ s
->indices
[ idx
+1 ] ],
860 *pc
= &s
->verts
[ s
->indices
[ idx
+2 ] ];
862 vg_line( pa
->co
, pb
->co
, 0xff0000ff );
863 vg_line( pb
->co
, pc
->co
, 0xff0000ff );
864 vg_line( pc
->co
, pa
->co
, 0xff0000ff );
870 static void bvh_debug( scene
*s
, v3f pos
)
872 bvh_debug_node( s
, 0, pos
, 0x4000ffa8 );
875 typedef struct ray_hit ray_hit
;
883 int ray_aabb( boxf box
, v3f co
, v3f dir
, float dist
)
888 v3_sub( box
[0], co
, v0
);
889 v3_sub( box
[1], co
, v1
);
890 v3_div( v0
, dir
, v0
);
891 v3_div( v1
, dir
, v1
);
893 tmin
= vg_minf( v0
[0], v1
[0] );
894 tmax
= vg_maxf( v0
[0], v1
[0] );
895 tmin
= vg_maxf( tmin
, vg_minf( v0
[1], v1
[1] ));
896 tmax
= vg_minf( tmax
, vg_maxf( v0
[1], v1
[1] ));
897 tmin
= vg_maxf( tmin
, vg_minf( v0
[2], v1
[2] ));
898 tmax
= vg_minf( tmax
, vg_maxf( v0
[2], v1
[2] ));
900 return tmax
>= tmin
&& tmin
< dist
&& tmax
> 0;
903 static int bvh_ray_tri( scene
*sc
, u32
*tri
, v3f co
, v3f dir
, ray_hit
*hit
)
905 float const kEpsilon
= 0.00001f
;
907 v3f v0
, v1
, h
, s
, q
, n
;
910 float *pa
= sc
->verts
[tri
[0]].co
,
911 *pb
= sc
->verts
[tri
[1]].co
,
912 *pc
= sc
->verts
[tri
[2]].co
;
914 v3_sub( pb
, pa
, v0
);
915 v3_sub( pc
, pa
, v1
);
916 v3_cross( dir
, v1
, h
);
917 v3_cross( v0
, v1
, n
);
919 if( v3_dot( n
, dir
) > 0.0f
) /* Backface culling */
924 if( a
> -kEpsilon
&& a
< kEpsilon
)
930 u
= f
* v3_dot(s
, h
);
931 if( u
< 0.0f
|| u
> 1.0f
)
934 v3_cross( s
, v0
, q
);
935 v
= f
* v3_dot( dir
, q
);
936 if( v
< 0.0f
|| u
+v
> 1.0f
)
939 t
= f
* v3_dot(v1
, q
);
940 if( t
> kEpsilon
&& t
< hit
->dist
)
949 static int bvh_ray( scene
*s
, u32 inode
, v3f co
, v3f dir
, ray_hit
*hit
)
951 bvh_node
*node
= &s
->bvh
.nodes
[ inode
];
953 if( !ray_aabb( node
->bbx
, co
, dir
, hit
->dist
))
960 for( u32 i
=0; i
<node
->count
; i
++ )
962 u32
*indices
= &s
->indices
[ (node
->start
+i
)*3 ];
963 count
+= bvh_ray_tri( s
, indices
, co
, dir
, hit
);
968 count
+= bvh_ray( s
, node
->il
, co
, dir
, hit
);
969 count
+= bvh_ray( s
, node
->ir
, co
, dir
, hit
);
975 static int bvh_raycast( scene
*s
, v3f co
, v3f dir
, ray_hit
*hit
)
978 v3_muladds( co
, dir
, hit
->dist
, pb
);
980 int count
= bvh_ray( s
, 0, co
, dir
, hit
);
984 //vg_line( co, pb, 0xff00ffff );
988 float *pa
= s
->verts
[hit
->tri
[0]].co
,
989 *pb
= s
->verts
[hit
->tri
[1]].co
,
990 *pc
= s
->verts
[hit
->tri
[2]].co
;
992 v3_sub( pa
, pb
, v0
);
993 v3_sub( pc
, pb
, v1
);
994 v3_cross( v1
, v0
, hit
->normal
);
995 v3_normalize( hit
->normal
);
996 v3_muladds( co
, dir
, hit
->dist
, hit
->pos
);
1000 //vg_line( co, pb, 0xff0000ff );
1006 static int bvh_scene_sample_node_h( scene
*s
, u32 inode
, v3f pos
, v3f norm
)
1008 bvh_node
*node
= &s
->bvh
.nodes
[ inode
];
1010 if( (pos
[0] >= node
->bbx
[0][0] && pos
[0] <= node
->bbx
[1][0]) &&
1011 (pos
[2] >= node
->bbx
[0][2] && pos
[2] <= node
->bbx
[1][2]) )
1015 if( bvh_scene_sample_node_h( s
, node
->il
, pos
, norm
)) return 1;
1016 if( bvh_scene_sample_node_h( s
, node
->ir
, pos
, norm
)) return 1;
1020 for( u32 i
=0; i
<node
->count
; i
++ )
1022 u32 idx
= (node
->start
+i
)*3;
1023 model_vert
*pa
= &s
->verts
[ s
->indices
[ idx
+0 ] ],
1024 *pb
= &s
->verts
[ s
->indices
[ idx
+1 ] ],
1025 *pc
= &s
->verts
[ s
->indices
[ idx
+2 ] ];
1028 if( triangle_raycast2d( pa
->co
, pb
->co
, pc
->co
, pos
, &height
))
1035 v3_sub( pa
->co
, pb
->co
, v0
);
1036 v3_sub( pc
->co
, pb
->co
, v1
);
1037 v3_cross( v1
, v0
, norm
);
1038 v3_normalize( norm
);
1050 static int bvh_scene_sample_h( scene
*s
, v3f pos
, v3f norm
)
1052 return bvh_scene_sample_node_h( s
, 0, pos
, norm
);
1055 static int bvh_scene_sample( scene
*s
, v3f pos
, ray_hit
*hit
)
1057 hit
->dist
= INFINITY
;
1060 v3_add( pos
, (v3f
){0.0f
,4.0f
,0.0f
}, ray_pos
);
1062 if( bvh_raycast( s
, ray_pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, hit
))
1064 pos
[1] = hit
->pos
[1];