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 /* https://www.shadertoy.com/view/4sfGzS */
98 #define SHADER_VALUE_NOISE_3D \
99 "uniform sampler2D uTexNoise;" \
101 "float noise( vec3 x )" \
103 "vec3 i = floor(x);" \
104 "vec3 f = fract(x);" \
105 "f = f*f*(3.0-2.0*f);" \
106 "vec2 uv = (i.xy+vec2(37.0,17.0)*i.z) + f.xy;" \
107 "vec2 rg = texture( uTexNoise, (uv+0.5)/256.0).yx;"\
108 "return mix( rg.x, rg.y, f.z );" \
111 "const mat3 m = mat3( 0.00, 0.80, 0.60," \
112 "-0.80, 0.36, -0.48," \
113 "-0.60, -0.48, 0.64 );" \
115 "float fractalNoise( vec3 x )" \
119 "f = 0.5000*noise( q ); q = m*q*2.01;" \
120 "f += 0.2500*noise( q ); q = m*q*2.02;" \
121 "f += 0.1250*noise( q ); q = m*q*2.03;" \
122 "f += 0.0625*noise( q ); q = m*q*2.01;" \
126 SHADER_DEFINE( shader_debug_vcol
,
128 /*Include*/ VERTEX_STANDARD_ATTRIBUTES
131 "uniform mat4x3 uMdl;"
132 "uniform float uTime;"
133 "uniform float uSwayAmt;"
140 "vec3 compute_sway( vec3 pos )"
142 "vec4 sines = vec4( sin(uTime + pos.x)*1.0,"
143 "sin(uTime*1.2 + pos.z*2.0)*1.1,"
144 "sin(uTime*2.33)*0.5,"
145 "sin(uTime*0.6 + pos.x*0.3)*1.3 );"
147 "vec3 offset = vec3( sines.x+sines.y*sines.w, 0.0, sines.x+sines.z );"
148 "return pos + offset*a_colour.r*uSwayAmt;"
153 "vec3 swaypos = compute_sway( a_co );"
154 "gl_Position = uPv * vec4(uMdl * vec4(swaypos,1.0), 1.0 );"
155 "aColour = a_colour;"
157 "aNorm = normalize(mat3(uMdl) * a_norm);"
161 "out vec4 FragColor;"
164 "uniform sampler2D uTexMain;"
165 "uniform sampler2D uTexGradients;"
167 /*Include*/ SHADER_VALUE_NOISE_3D
176 "vec4 colour = vec4(1.0,0.0,0.5,1.0);"
177 "vec4 diffuse = texture( uTexMain, aUv );"
181 "colour = vec4(aNorm * 0.5 + 0.5, 1.0);"
189 "float light = dot(aNorm, vec3(0.2,0.8,0.1));"
190 "vec3 grid3 = fract(aCo);"
192 "colour = vec4(vec3(light)*(1.0-grid3*0.3),1.0);"
196 "colour = vec4( aUv, 0.0, 1.0 );"
200 "if( diffuse.a < 0.45 ) discard;"
205 "float r1 = fractalNoise(aCo);"
206 "colour = vec4( vec3(r1), 1.0 );"
210 "if( diffuse.a < 0.2 ) discard;"
211 "float lighting = 1.0 - aColour.g*0.8;"
213 "float light1 = max(0.0,dot(-vec3(0.5,-0.8,0.25), aNorm));"
214 "float light2 = max(0.0,dot(-vec3(-0.8,0.5,-0.25), aNorm));"
215 "vec3 lt = vec3(0.2,0.2,0.2 ) + "
216 "vec3(1.0,1.0,0.9)*light1 + "
217 "vec3(0.1,0.3,0.4 )*light2;"
220 "colour = vec4(vec3(pow(lighting,1.6)*(diffuse.r*0.7+0.5)),1.0);"
221 "colour = vec4(colour.rgb*lt,1.0);"
223 "vec2 gradUV = vec2(lighting*1.9,aColour.b*0.8);"
224 "vec4 gradient_sample = texture( uTexGradients, gradUV );"
225 "colour = colour*gradient_sample;"
229 "if( diffuse.a < 0.45 ) discard;"
230 "float light = 1.0 - aColour.g;"
231 "light = pow(light,1.6)*(diffuse.r*0.7+0.5);"
232 "float r1 = fractalNoise(aCo*0.01);"
234 "vec2 gradUV = vec2(light*1.9,r1+aColour.b);"
235 "vec4 gradient_sample = texture( uTexGradients, gradUV );"
236 "colour = gradient_sample*light;"
239 "FragColor = colour;"
242 UNIFORMS({ "uPv", "uMode", "uTexMain", "uTexGradients", "uTexNoise", \
243 "uTime", "uSwayAmt", "uMdl" })
246 SHADER_DEFINE( shader_standard_lit
,
248 /*Include*/ VERTEX_STANDARD_ATTRIBUTES
251 "uniform mat4x3 uMdl;"
260 "gl_Position = uPv * vec4( uMdl * vec4(a_co,1.0), 1.0 );"
261 "aColour = a_colour;"
263 "aNorm = mat3(uMdl) * a_norm;"
267 "out vec4 FragColor;"
269 "uniform sampler2D uTexMain;"
270 "uniform vec4 uColour;"
279 "vec3 diffuse = texture( uTexMain, aUv ).rgb;"
281 "float light1 = max(0.0,dot(-vec3(0.5,-0.8,0.25), aNorm));"
282 "float light2 = max(0.0,dot(-vec3(-0.8,0.5,-0.25), aNorm));"
283 "diffuse += vec3(0.2,0.2,0.2) + "
284 "vec3(1.0,1.0,0.9)*light1 + "
285 "vec3(0.1,0.3,0.4)*light2;"
287 "FragColor = vec4(diffuse*uColour.rgb, aColour.a*uColour.a);"
290 UNIFORMS({ "uColour","uTexMain","uPv","uMdl" })
293 SHADER_DEFINE( shader_unlit
,
295 /*Include*/ VERTEX_STANDARD_ATTRIBUTES
298 "uniform mat4x3 uMdl;"
307 "gl_Position = uPv * vec4(uMdl * vec4(a_co,1.0), 1.0);"
308 "aColour = a_colour;"
310 "aNorm = mat3(uMdl) * a_norm;"
314 "out vec4 FragColor;"
316 "uniform sampler2D uTexMain;"
317 "uniform vec4 uColour;"
326 "vec3 diffuse = texture( uTexMain, aUv ).rgb;"
327 "FragColor = vec4(pow(diffuse,vec3(1.0)),1.0);"
330 UNIFORMS({ "uTexMain", "uPv", "uMdl" })
333 static void *buffer_reserve( void *buffer
, u32 count
, u32
*cap
, u32 amount
,
336 if( count
+amount
> *cap
)
338 *cap
= VG_MAX( (*cap
)*2, (*cap
)+amount
);
340 return realloc( buffer
, (*cap
) * emsize
);
347 * Append a model into the scene with a given transform
349 static void scene_add_model( scene
*pscene
, model
*mdl
, submodel
*submodel
,
350 v3f pos
, float yaw
, float scale
)
352 pscene
->verts
= buffer_reserve( pscene
->verts
, pscene
->vertex_count
,
353 &pscene
->vertex_cap
, submodel
->vertex_count
, sizeof(model_vert
) );
354 pscene
->indices
= buffer_reserve( pscene
->indices
, pscene
->indice_count
,
355 &pscene
->indice_cap
, submodel
->indice_count
, sizeof(u32
) );
357 if( submodel
->sdf_type
)
359 pscene
->shadowers
= buffer_reserve( pscene
->shadowers
,
360 pscene
->shadower_count
, &pscene
->shadower_cap
, 1,
361 sizeof( struct shadower
));
363 struct shadower
*shadower
=
364 &pscene
->shadowers
[ pscene
->shadower_count
++ ];
366 shadower
->sdf
= submodel
->sdf
;
367 shadower
->sdf_type
= submodel
->sdf_type
;
369 v2_muls( shadower
->sdf
.info
, scale
, shadower
->sdf
.info
);
370 v3_muls( shadower
->sdf
.origin
, scale
, shadower
->sdf
.origin
);
371 v3_add( pos
, shadower
->sdf
.origin
, shadower
->sdf
.origin
);
374 /* Transform and place vertices */
375 model_vert
*src_verts
= submodel_vert_data( mdl
, submodel
);
376 u32
*src_indices
= submodel_indice_data( mdl
, submodel
);
379 m4x3_identity( mtx
);
380 m4x3_translate( mtx
, pos
);
381 m4x3_rotate_y( mtx
, yaw
);
382 m4x3_scale( mtx
, scale
);
385 box_copy( submodel
->bbx
, bbxnew
);
386 m4x3_transform_aabb( mtx
, bbxnew
);
387 box_concat( pscene
->bbx
, bbxnew
);
390 m4x3_to_3x3( mtx
, rotation
);
392 float rand_hue
= vg_randf();
394 for( u32 i
=0; i
<submodel
->vertex_count
; i
++ )
396 model_vert
*pvert
= &pscene
->verts
[ pscene
->vertex_count
+i
],
397 *src
= &src_verts
[ i
];
399 m4x3_mulv( mtx
, src
->co
, pvert
->co
);
400 m3x3_mulv( rotation
, src
->norm
, pvert
->norm
);
402 v4_copy( src
->colour
, pvert
->colour
);
403 v2_copy( src
->uv
, pvert
->uv
);
405 float rel_y
= src
->co
[1] / submodel
->bbx
[1][1];
406 pvert
->colour
[0] = rel_y
;
407 pvert
->colour
[2] = rand_hue
;
410 for( u32 i
=0; i
<submodel
->indice_count
; i
++ )
412 u32
*pidx
= &pscene
->indices
[ pscene
->indice_count
+i
];
413 *pidx
= src_indices
[i
] + pscene
->vertex_count
;
416 pscene
->vertex_count
+= submodel
->vertex_count
;
417 pscene
->indice_count
+= submodel
->indice_count
;
420 static void scene_copy_slice( scene
*pscene
, submodel
*sm
)
422 sm
->indice_start
= pscene
->submesh
.indice_start
;
423 sm
->indice_count
= pscene
->indice_count
- sm
->indice_start
;
425 sm
->vertex_start
= pscene
->submesh
.vertex_start
;
426 sm
->vertex_count
= pscene
->vertex_count
- sm
->vertex_start
;
428 pscene
->submesh
.indice_start
= pscene
->indice_count
;
429 pscene
->submesh
.vertex_start
= pscene
->vertex_count
;
432 static void scene_shadow_sphere( scene
*pscene
, v3f sphere
,
433 v4f params
, v3f lightdir
)
435 for( int i
=0; i
<pscene
->vertex_count
; i
++ )
437 model_vert
*vert
= &pscene
->verts
[i
];
440 v3_sub( sphere
, vert
->co
, delta
);
442 float d
= v3_dot( lightdir
, delta
);
445 v3_muls( lightdir
, d
, closest
);
446 float dist
= v3_dist( closest
, delta
),
447 shading
= vg_maxf( dist
- params
[0], 0.0f
);
449 shading
= vg_minf( shading
* params
[1], 1.0f
);
450 vert
->colour
[1] *= shading
;
454 static void scene_shadow_gradient( scene
*pscene
, int comp
,
455 float start
, float length
)
457 float scale
= 1.0f
/ length
;
459 for( int i
=0; i
<pscene
->vertex_count
; i
++ )
461 model_vert
*vert
= &pscene
->verts
[i
];
462 float shading
= start
+ vert
->co
[comp
] * scale
;
464 vert
->colour
[1] = shading
;
470 * Experimental SDF based shadows
472 * https://iquilezles.org/articles/distfunctions/
474 static float sd_cone( v3f co
, sdf_primative
*prim
)
476 float bound
= prim
->info
[1]*1.75f
;
477 if( v3_dist2( prim
->origin
, co
) > bound
*bound
)
481 v3_sub( co
, prim
->origin
, p
);
483 float h
= prim
->info
[1];
484 v2f c
= { prim
->info
[2], prim
->info
[3] };
487 v2_muls( (v2f
){ c
[0]/c
[1], -1.0f
}, h
, q
);
489 w
[0] = v2_length( (v2f
){ p
[0], p
[2] } );
492 v2_muladds( w
, q
, -vg_clampf( v2_dot(w
,q
)/v2_dot(q
,q
), 0.0f
, 1.0f
), a
);
493 v2_muladd( w
, q
, (v2f
){ vg_clampf( w
[0]/q
[0], 0.0f
, 1.0f
), 1.0f
}, b
);
495 float k
= vg_signf( q
[1] ),
496 d
= vg_minf( v2_dot( a
,a
), v2_dot( b
,b
) ),
497 s
= vg_maxf( k
*(w
[0]*q
[1]-w
[1]*q
[0]), k
*(w
[1]-q
[1]) );
499 return sqrtf(d
)*vg_signf(s
);
502 #define CACHE_AMBIENT_SHAPES
504 static float scene_ambient_sample( scene
*pscene
, v3f pos
, v3f dir
)
508 #ifdef CACHE_AMBIENT_SHAPES
509 static struct shadower
*local_shadowers
[32];
510 static int local_shadower_count
= 0;
511 static v3f local_shadower_last
= { -99999.9f
, -999999.9f
, -9999999.9f
};
513 if( v3_dist2( pos
, local_shadower_last
) > 10.0f
*10.0f
)
515 local_shadower_count
= 0;
516 v3_copy( pos
, local_shadower_last
);
518 for( int k
=0; k
<pscene
->shadower_count
; k
++ )
520 struct shadower
*shadower
= &pscene
->shadowers
[k
];
522 if( sd_cone( pos
, &shadower
->sdf
) <= 20.0f
)
524 local_shadowers
[ local_shadower_count
++ ] = shadower
;
525 if( local_shadower_count
== vg_list_size( local_shadowers
) )
532 for( int j
=0; j
<5; j
++ )
535 v3_muladds( pos
, dir
, 1.5f
*(float)j
, tracepos
);
537 float mindist
= 99999.9f
;
539 #ifndef CACHE_AMBIENT_SHAPES
541 for( int k
=0; k
<pscene
->shadower_count
; k
++ ){
542 struct shadower
*shadower
= &pscene
->shadowers
[k
];
545 for( int k
=0; k
<local_shadower_count
; k
++ ){
546 struct shadower
*shadower
= local_shadowers
[k
];
549 float dist
= vg_maxf( 0.0f
, sd_cone( tracepos
, &shadower
->sdf
));
550 mindist
= vg_minf( mindist
, dist
);
554 accum
+= vg_clampf( 1.0f
- mindist
, 0.0f
, 1.0f
)*0.2f
;
561 #define JUST_DO_EVERY_VERT
563 static void scene_compute_occlusion( scene
*pscene
)
565 v3f sundir
= { 0.2f
, 0.9f
, 0.2f
};
566 v3_normalize( sundir
);
568 /* TODO: Make this sample grid be dynamically required.
570 * 1. Only resample the light grid (1x1x1), when a vertex is outside the
573 * 2. Reorder all vertices so that each group of vertices that fit in a
574 * cube are next to eachother in the buffer. This will save cache
577 * for the sorting algorithm, i think we can already assume that *most
578 * vertices will be quite close to eachother. so instead of doing an
579 * exhaustive search we can reorder 1k chunks at a time.
583 v3_sub( pscene
->bbx
[1], pscene
->bbx
[0], sample_area
);
584 v3_ceil( sample_area
, sample_area
);
585 int ax
= sample_area
[0],
590 float *samplegrid
= malloc( ax
*ay
*az
* sizeof(float) );
592 for( int x
=0; x
<ax
; x
++ ){
593 for( int y
=0; y
<ay
; y
++ ){
594 for( int z
=0; z
<az
; z
++ )
596 v3f sample_pos
= { x
,y
,z
};
597 v3_add( pscene
->bbx
[0], sample_pos
, sample_pos
);
598 float accum
= scene_ambient_sample( pscene
, sample_pos
, sundir
);
600 samplegrid
[x
+ y
*ax
+ z
*ax
*ay
] = accum
;
603 v3i cube_pos
= { -999999, -999999, -999999 };
604 int cube_resamples
= 0, hits
= 0, misses
= 0;
606 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
;
609 for( int i
=0; i
<pscene
->vertex_count
; i
++ )
611 model_vert
*vert
= &pscene
->verts
[i
];
615 v3_sub( vert
->co
, pscene
->bbx
[0], q
);
617 v3_copy( vert
->co
, q
);
627 #ifndef JUST_DO_EVERY_VERT
637 s0
= samplegrid
[ x
+ y
*ax
+ z
*ax
*ay
],
638 s1
= samplegrid
[(x
+1) + y
*ax
+ z
*ax
*ay
],
639 s2
= samplegrid
[ x
+ (y
+1)*ax
+ z
*ax
*ay
],
640 s3
= samplegrid
[(x
+1) + (y
+1)*ax
+ z
*ax
*ay
],
641 s4
= samplegrid
[ x
+ y
*ax
+ (z
+1)*ax
*ay
],
642 s5
= samplegrid
[(x
+1) + y
*ax
+ (z
+1)*ax
*ay
],
643 s6
= samplegrid
[ x
+ (y
+1)*ax
+ (z
+1)*ax
*ay
],
644 s7
= samplegrid
[(x
+1) + (y
+1)*ax
+ (z
+1)*ax
*ay
],
646 if( x
!=cube_pos
[0] || y
!=cube_pos
[1] || z
!=cube_pos
[2] )
652 s0
= scene_ambient_sample( pscene
, (v3f
){ x
,y
,z
}, sundir
);
653 s1
= scene_ambient_sample( pscene
, (v3f
){ x
+1,y
,z
}, sundir
);
654 s2
= scene_ambient_sample( pscene
, (v3f
){ x
,y
+1,z
}, sundir
);
655 s3
= scene_ambient_sample( pscene
, (v3f
){ x
+1,y
+1,z
}, sundir
);
656 s4
= scene_ambient_sample( pscene
, (v3f
){ x
,y
,z
+1 }, sundir
);
657 s5
= scene_ambient_sample( pscene
, (v3f
){ x
+1,y
,z
+1 }, sundir
);
658 s6
= scene_ambient_sample( pscene
, (v3f
){ x
,y
+1,z
+1 }, sundir
);
659 s7
= scene_ambient_sample( pscene
, (v3f
){ x
+1,y
+1,z
+1 }, sundir
);
670 s0_s1
= vg_lerpf( s0
, s1
, q
[0] ),
671 s2_s3
= vg_lerpf( s2
, s3
, q
[0] ),
672 s4_s5
= vg_lerpf( s4
, s5
, q
[0] ),
673 s6_s7
= vg_lerpf( s6
, s7
, q
[0] ),
675 s0s1_s2s3
= vg_lerpf( s0_s1
, s2_s3
, q
[1] ),
676 s4s5_s6s7
= vg_lerpf( s4_s5
, s6_s7
, q
[1] ),
677 s0s1s2s3_s4s5s6s7
= vg_lerpf( s0s1_s2s3
, s4s5_s6s7
, q
[2] );
679 vert
->colour
[1] = s0s1s2s3_s4s5s6s7
;
681 vert
->colour
[1] = scene_ambient_sample( pscene
, vert
->co
, sundir
);
686 int cube_resamples
= -1, misses
= 0, hits
= 0;
689 int static_samples
= ax
*ay
*az
,
690 vertex_samples
= pscene
->vertex_count
;
692 if( cube_resamples
< static_samples
)
693 vg_success( "Walking cube beat static grid (%d<%d. %d)!\n",
694 cube_resamples
, static_samples
, vertex_samples
);
696 vg_warn( "Walking cube was worse than static grid (%d<%d. %d).\n",
697 cube_resamples
, static_samples
, vertex_samples
);
699 vg_info( "Hits; %d, misses: %d\n", hits
, misses
);
707 for( int i
=0; i
<pscene
->vertex_count
; i
++ )
709 model_vert
*vert
= &pscene
->verts
[i
];
712 for( int j
=0; j
<5; j
++ )
715 v3_copy( vert
->co
, tracepos
);
716 v3_muladds( tracepos
, sundir
, 1.5f
*(float)j
, tracepos
);
718 float mindist
= 99999.9f
;
720 for( int k
=0; k
<pscene
->shadower_count
; k
++ )
722 struct shadower
*shadower
= &pscene
->shadowers
[k
];
723 float dist
= vg_maxf( 0.0f
, sd_cone( tracepos
, &shadower
->sdf
));
724 mindist
= vg_minf( mindist
, dist
);
727 accum
+= vg_clampf( 1.0f
- mindist
, 0.0f
, 1.0f
)*0.2f
;
730 vert
->colour
[1] = vg_minf( accum
, 1.0f
);
734 static void scene_upload( scene
*pscene
)
736 mesh_upload( &pscene
->mesh
,
737 pscene
->verts
, pscene
->vertex_count
,
738 pscene
->indices
, pscene
->indice_count
);
740 vg_info( "Scene upload\n" );
741 vg_info( " indices:%u\n", pscene
->indice_count
);
742 vg_info( " verts:%u\n", pscene
->vertex_count
);
745 float scene_tree_sway
= 0.1f
;
748 static void scene_foliage_shader_use(void)
750 SHADER_USE( shader_debug_vcol
);
752 glUniformMatrix4fv( SHADER_UNIFORM( shader_debug_vcol
, "uPv" ),
753 1, GL_FALSE
, (float *)vg_pv
);
755 glUniform1i( SHADER_UNIFORM( shader_debug_vcol
, "uMode" ), debugview
);
756 glUniform1i( SHADER_UNIFORM( shader_debug_vcol
, "uTexMain" ), 0 );
758 glUniform1i( SHADER_UNIFORM( shader_debug_vcol
, "uTexGradients" ), 1 );
759 vg_tex2d_bind( &tex_gradients
, 1 );
761 glUniform1i( SHADER_UNIFORM( shader_debug_vcol
, "uTexNoise" ), 2 );
762 glActiveTexture( GL_TEXTURE2
);
763 glBindTexture( GL_TEXTURE_2D
, tex_dual_noise
);
765 glUniform1f( SHADER_UNIFORM( shader_debug_vcol
, "uTime" ), vg_time
);
766 glUniform1f( SHADER_UNIFORM( shader_debug_vcol
, "uSwayAmt" ),
771 static void scene_bind( scene
*pscene
)
773 mesh_bind( &pscene
->mesh
);
776 static void scene_draw( scene
*pscene
)
778 mesh_drawn( 0, pscene
->indice_count
);
781 static void scene_debugsdf( scene
*pscene
)
783 for( int i
=0; i
<pscene
->shadower_count
; i
++ )
785 struct shadower
*shadower
= &pscene
->shadowers
[i
];
788 v3_copy( shadower
->sdf
.origin
, base
);
789 base
[1] -= shadower
->sdf
.info
[1];
790 v3_copy( base
, side
);
791 side
[0] += shadower
->sdf
.info
[0];
793 vg_line2( shadower
->sdf
.origin
, base
, 0xff00ff00, 0xff0000ff );
794 vg_line2( side
, base
, 0xff00ff00, 0xff0000ff );
795 vg_line( side
, shadower
->sdf
.origin
, 0xff00ff00 );
798 v3f p0
= { pscene
->bbx
[0][0], pscene
->bbx
[0][1], pscene
->bbx
[0][2] },
799 p1
= { pscene
->bbx
[0][0], pscene
->bbx
[1][1], pscene
->bbx
[0][2] },
800 p2
= { pscene
->bbx
[1][0], pscene
->bbx
[1][1], pscene
->bbx
[0][2] },
801 p3
= { pscene
->bbx
[1][0], pscene
->bbx
[0][1], pscene
->bbx
[0][2] },
803 p4
= { pscene
->bbx
[0][0], pscene
->bbx
[0][1], pscene
->bbx
[1][2] },
804 p5
= { pscene
->bbx
[0][0], pscene
->bbx
[1][1], pscene
->bbx
[1][2] },
805 p6
= { pscene
->bbx
[1][0], pscene
->bbx
[1][1], pscene
->bbx
[1][2] },
806 p7
= { pscene
->bbx
[1][0], pscene
->bbx
[0][1], pscene
->bbx
[1][2] };
808 u32 col
= 0xffff00c8;
809 vg_line( p0
, p1
, col
);
810 vg_line( p1
, p2
, col
);
811 vg_line( p2
, p3
, col
);
812 vg_line( p3
, p0
, col
);
814 vg_line( p4
, p5
, col
);
815 vg_line( p5
, p6
, col
);
816 vg_line( p6
, p7
, col
);
817 vg_line( p7
, p4
, col
);
819 vg_line( p0
, p4
, col
);
820 vg_line( p1
, p5
, col
);
821 vg_line( p2
, p6
, col
);
822 vg_line( p3
, p7
, col
);
825 static void scene_register(void)
827 SHADER_INIT( shader_debug_vcol
);
828 SHADER_INIT( shader_standard_lit
);
829 SHADER_INIT( shader_unlit
);
833 /* Physics segment */
835 static int triangle_raycast2d( v3f pA
, v3f pB
, v3f pC
, v3f ray
, float *height
)
837 v2f v0
, v1
, v2
, vp
, vp2
;
838 float d
, bca
= 0.f
, bcb
= 0.f
, bcc
= 0.f
;
840 v0
[0] = pB
[0] - pA
[0];
841 v0
[1] = pB
[2] - pA
[2];
842 v1
[0] = pC
[0] - pA
[0];
843 v1
[1] = pC
[2] - pA
[2];
844 v2
[0] = pB
[0] - pC
[0];
845 v2
[1] = pB
[2] - pC
[2];
847 d
= 1.f
/ (v0
[0]*v1
[1] - v1
[0]*v0
[1]);
850 /* Backface culling */
851 if( v2_cross( v0
, v1
) > 0.f
)
855 vp
[0] = ray
[0] - pA
[0];
856 vp
[1] = ray
[2] - pA
[2];
858 if( v2_cross( v0
, vp
) > 0.f
) return 0;
859 if( v2_cross( vp
, v1
) > 0.f
) return 0;
861 vp2
[0] = ray
[0] - pB
[0];
862 vp2
[1] = ray
[2] - pB
[2];
864 if( v2_cross( vp2
, v2
) > 0.f
) return 0;
866 bcb
= (vp
[0]*v1
[1] - v1
[0]*vp
[1]) * d
;
867 bcc
= (v0
[0]*vp
[1] - vp
[0]*v0
[1]) * d
;
868 bca
= 1.f
- bcb
- bcc
;
870 *height
= pA
[1]*bca
+ pB
[1]*bcb
+ pC
[1]*bcc
;
875 static int sample_scene_height( scene
*pscene
, v3f pos
, v3f norm
)
877 for( int i
=0; i
<pscene
->indice_count
/3; i
++ )
879 u32
*tri
= &pscene
->indices
[i
*3];
881 float *pA
= pscene
->verts
[tri
[0]].co
,
882 *pB
= pscene
->verts
[tri
[1]].co
,
883 *pC
= pscene
->verts
[tri
[2]].co
;
886 if( triangle_raycast2d( pA
, pB
, pC
, pos
, &height
))
893 v3_sub( pA
, pB
, v0
);
894 v3_sub( pC
, pB
, v1
);
895 v3_cross( v1
, v0
, norm
);
896 v3_normalize( norm
);
905 static void sample_scene_normal( scene
*pscene
, v3f pos
, v3f normal
)
907 for( int i
=0; i
<pscene
->indice_count
/3; i
++ )
909 u32
*tri
= &pscene
->indices
[i
*3];
912 if( triangle_raycast2d(
913 pscene
->verts
[ tri
[0] ].co
,
914 pscene
->verts
[ tri
[1] ].co
,
915 pscene
->verts
[ tri
[2] ].co
, pos
, &height
))
919 v3_sub( pscene
->verts
[ tri
[1] ].co
,
920 pscene
->verts
[ tri
[0] ].co
,
923 v3_sub( pscene
->verts
[ tri
[2] ].co
,
924 pscene
->verts
[ tri
[0] ].co
,
927 v3_cross( v0
, v1
, normal
);
928 v3_normalize( normal
);
942 /* if il is 0, this is a leaf */
944 union{ u32 ir
, start
; };
947 static void bvh_update_bounds( scene
*s
, u32 inode
)
949 bvh_node
*node
= &s
->bvh
.nodes
[ inode
];
951 box_init_inf( node
->bbx
);
952 for( u32 i
=0; i
<node
->count
; i
++ )
954 u32 idx
= node
->start
+i
;
955 model_vert
*pa
= &s
->verts
[ s
->indices
[idx
*3+0] ],
956 *pb
= &s
->verts
[ s
->indices
[idx
*3+1] ],
957 *pc
= &s
->verts
[ s
->indices
[idx
*3+2] ];
959 box_addpt( node
->bbx
, pa
->co
);
960 box_addpt( node
->bbx
, pb
->co
);
961 box_addpt( node
->bbx
, pc
->co
);
965 static void bvh_subdiv( scene
*s
, u32 inode
)
967 bvh_node
*node
= &s
->bvh
.nodes
[ inode
];
970 v3_sub( node
->bbx
[1], node
->bbx
[0], extent
);
973 if( extent
[1] > extent
[0] ) axis
= 1;
974 if( extent
[2] > extent
[axis
] ) axis
= 2;
976 float split
= node
->bbx
[0][axis
] + extent
[axis
]*0.5f
;
978 /* To beat: 121,687 / 136,579
983 for( u32 t
=0; t
<node
->count
; t
++ )
985 u32
*ti
= &s
->indices
[(node
->start
+t
)*3];
986 float a
= s
->verts
[ti
[0]].co
[axis
],
987 b
= s
->verts
[ti
[1]].co
[axis
],
988 c
= s
->verts
[ti
[2]].co
[axis
];
991 avg
/= (float)node
->count
;
996 j
= i
+ node
->count
-1;
1000 u32
*ti
= &s
->indices
[i
*3];
1002 float a
= s
->verts
[ti
[0]].co
[axis
],
1003 b
= s
->verts
[ti
[1]].co
[axis
],
1004 c
= s
->verts
[ti
[2]].co
[axis
];
1006 if( ((a
+b
+c
) / 3.0f
) < split
)
1010 /* Swap triangle indices */
1011 u32
*tj
= &s
->indices
[j
*3];
1029 u32 left_count
= i
- node
->start
;
1030 if( left_count
== 0 || left_count
== node
->count
) return;
1032 u32 il
= s
->bvh
.node_count
++,
1033 ir
= s
->bvh
.node_count
++;
1035 struct bvh_node
*lnode
= &s
->bvh
.nodes
[il
],
1036 *rnode
= &s
->bvh
.nodes
[ir
];
1038 lnode
->start
= node
->start
;
1039 lnode
->count
= left_count
;
1041 rnode
->count
= node
->count
- left_count
;
1047 bvh_update_bounds( s
, il
);
1048 bvh_update_bounds( s
, ir
);
1049 bvh_subdiv( s
, il
);
1050 bvh_subdiv( s
, ir
);
1053 static void bvh_create( scene
*s
)
1055 u32 triangle_count
= s
->indice_count
/ 3;
1056 s
->bvh
.nodes
= malloc( sizeof(struct bvh_node
) * (triangle_count
*2-1) );
1058 bvh_node
*root
= &s
->bvh
.nodes
[0];
1059 s
->bvh
.node_count
= 1;
1063 root
->count
= triangle_count
;
1066 bvh_update_bounds( s
, 0 );
1070 realloc( s
->bvh
.nodes
, sizeof(struct bvh_node
) * s
->bvh
.node_count
);
1072 vg_success( "BVH done, size: %u/%u\n", s
->bvh
.node_count
,
1073 (triangle_count
*2-1) );
1076 static void bvh_debug_node( scene
*s
, u32 inode
, v3f pos
, u32 colour
)
1078 struct bvh_node
*node
= &s
->bvh
.nodes
[ inode
];
1080 if( (pos
[0] >= node
->bbx
[0][0] && pos
[0] <= node
->bbx
[1][0]) &&
1081 (pos
[2] >= node
->bbx
[0][2] && pos
[2] <= node
->bbx
[1][2]) )
1085 vg_line_boxf( node
->bbx
, colour
);
1087 bvh_debug_node( s
, node
->il
, pos
, colour
);
1088 bvh_debug_node( s
, node
->ir
, pos
, colour
);
1092 vg_line_boxf( node
->bbx
, 0xff00ff00 );
1093 for( u32 i
=0; i
<node
->count
; i
++ )
1095 u32 idx
= (node
->start
+i
)*3;
1097 model_vert
*pa
= &s
->verts
[ s
->indices
[ idx
+0 ] ],
1098 *pb
= &s
->verts
[ s
->indices
[ idx
+1 ] ],
1099 *pc
= &s
->verts
[ s
->indices
[ idx
+2 ] ];
1101 vg_line( pa
->co
, pb
->co
, 0xff0000ff );
1102 vg_line( pb
->co
, pc
->co
, 0xff0000ff );
1103 vg_line( pc
->co
, pa
->co
, 0xff0000ff );
1109 static void bvh_debug( scene
*s
, v3f pos
)
1111 bvh_debug_node( s
, 0, pos
, 0x4000ffa8 );
1114 typedef struct ray_hit ray_hit
;
1122 int ray_aabb( boxf box
, v3f co
, v3f dir
, float dist
)
1127 v3_sub( box
[0], co
, v0
);
1128 v3_sub( box
[1], co
, v1
);
1129 v3_div( v0
, dir
, v0
);
1130 v3_div( v1
, dir
, v1
);
1132 tmin
= vg_minf( v0
[0], v1
[0] );
1133 tmax
= vg_maxf( v0
[0], v1
[0] );
1134 tmin
= vg_maxf( tmin
, vg_minf( v0
[1], v1
[1] ));
1135 tmax
= vg_minf( tmax
, vg_maxf( v0
[1], v1
[1] ));
1136 tmin
= vg_maxf( tmin
, vg_minf( v0
[2], v1
[2] ));
1137 tmax
= vg_minf( tmax
, vg_maxf( v0
[2], v1
[2] ));
1139 return tmax
>= tmin
&& tmin
< dist
&& tmax
> 0;
1142 static int bvh_ray_tri( scene
*sc
, u32
*tri
, v3f co
, v3f dir
, ray_hit
*hit
)
1144 float const kEpsilon
= 0.00001f
;
1146 v3f v0
, v1
, h
, s
, q
, n
;
1149 float *pa
= sc
->verts
[tri
[0]].co
,
1150 *pb
= sc
->verts
[tri
[1]].co
,
1151 *pc
= sc
->verts
[tri
[2]].co
;
1153 v3_sub( pb
, pa
, v0
);
1154 v3_sub( pc
, pa
, v1
);
1155 v3_cross( dir
, v1
, h
);
1156 v3_cross( v0
, v1
, n
);
1158 if( v3_dot( n
, dir
) > 0.0f
) /* Backface culling */
1162 a
= v3_dot( v0
, h
);
1163 if( a
> -kEpsilon
&& a
< kEpsilon
)
1167 v3_sub( co
, pa
, s
);
1169 u
= f
* v3_dot(s
, h
);
1170 if( u
< 0.0f
|| u
> 1.0f
)
1173 v3_cross( s
, v0
, q
);
1174 v
= f
* v3_dot( dir
, q
);
1175 if( v
< 0.0f
|| u
+v
> 1.0f
)
1178 t
= f
* v3_dot(v1
, q
);
1179 if( t
> kEpsilon
&& t
< hit
->dist
)
1188 static int bvh_ray( scene
*s
, u32 inode
, v3f co
, v3f dir
, ray_hit
*hit
)
1190 bvh_node
*node
= &s
->bvh
.nodes
[ inode
];
1192 if( !ray_aabb( node
->bbx
, co
, dir
, hit
->dist
))
1199 for( u32 i
=0; i
<node
->count
; i
++ )
1201 u32
*indices
= &s
->indices
[ (node
->start
+i
)*3 ];
1202 count
+= bvh_ray_tri( s
, indices
, co
, dir
, hit
);
1207 count
+= bvh_ray( s
, node
->il
, co
, dir
, hit
);
1208 count
+= bvh_ray( s
, node
->ir
, co
, dir
, hit
);
1214 static int bvh_raycast( scene
*s
, v3f co
, v3f dir
, ray_hit
*hit
)
1217 v3_muladds( co
, dir
, hit
->dist
, pb
);
1219 int count
= bvh_ray( s
, 0, co
, dir
, hit
);
1223 //vg_line( co, pb, 0xff00ffff );
1227 float *pa
= s
->verts
[hit
->tri
[0]].co
,
1228 *pb
= s
->verts
[hit
->tri
[1]].co
,
1229 *pc
= s
->verts
[hit
->tri
[2]].co
;
1231 v3_sub( pa
, pb
, v0
);
1232 v3_sub( pc
, pb
, v1
);
1233 v3_cross( v1
, v0
, hit
->normal
);
1234 v3_normalize( hit
->normal
);
1235 v3_muladds( co
, dir
, hit
->dist
, hit
->pos
);
1239 //vg_line( co, pb, 0xff0000ff );
1245 static int bvh_scene_sample_node_h( scene
*s
, u32 inode
, v3f pos
, v3f norm
)
1247 bvh_node
*node
= &s
->bvh
.nodes
[ inode
];
1249 if( (pos
[0] >= node
->bbx
[0][0] && pos
[0] <= node
->bbx
[1][0]) &&
1250 (pos
[2] >= node
->bbx
[0][2] && pos
[2] <= node
->bbx
[1][2]) )
1254 if( bvh_scene_sample_node_h( s
, node
->il
, pos
, norm
)) return 1;
1255 if( bvh_scene_sample_node_h( s
, node
->ir
, pos
, norm
)) return 1;
1259 for( u32 i
=0; i
<node
->count
; i
++ )
1261 u32 idx
= (node
->start
+i
)*3;
1262 model_vert
*pa
= &s
->verts
[ s
->indices
[ idx
+0 ] ],
1263 *pb
= &s
->verts
[ s
->indices
[ idx
+1 ] ],
1264 *pc
= &s
->verts
[ s
->indices
[ idx
+2 ] ];
1267 if( triangle_raycast2d( pa
->co
, pb
->co
, pc
->co
, pos
, &height
))
1274 v3_sub( pa
->co
, pb
->co
, v0
);
1275 v3_sub( pc
->co
, pb
->co
, v1
);
1276 v3_cross( v1
, v0
, norm
);
1277 v3_normalize( norm
);
1289 static int bvh_scene_sample_h( scene
*s
, v3f pos
, v3f norm
)
1291 return bvh_scene_sample_node_h( s
, 0, pos
, norm
);
1294 static int bvh_scene_sample( scene
*s
, v3f pos
, ray_hit
*hit
)
1296 hit
->dist
= INFINITY
;
1299 v3_add( pos
, (v3f
){0.0f
,4.0f
,0.0f
}, ray_pos
);
1301 if( bvh_raycast( s
, ray_pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, hit
))
1303 pos
[1] = hit
->pos
[1];