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 pscene
->submesh
.indice_start
= pscene
->indice_count
;
428 static void scene_shadow_sphere( scene
*pscene
, v3f sphere
,
429 v4f params
, v3f lightdir
)
431 for( int i
=0; i
<pscene
->vertex_count
; i
++ )
433 model_vert
*vert
= &pscene
->verts
[i
];
436 v3_sub( sphere
, vert
->co
, delta
);
438 float d
= v3_dot( lightdir
, delta
);
441 v3_muls( lightdir
, d
, closest
);
442 float dist
= v3_dist( closest
, delta
),
443 shading
= vg_maxf( dist
- params
[0], 0.0f
);
445 shading
= vg_minf( shading
* params
[1], 1.0f
);
446 vert
->colour
[1] *= shading
;
450 static void scene_shadow_gradient( scene
*pscene
, int comp
,
451 float start
, float length
)
453 float scale
= 1.0f
/ length
;
455 for( int i
=0; i
<pscene
->vertex_count
; i
++ )
457 model_vert
*vert
= &pscene
->verts
[i
];
458 float shading
= start
+ vert
->co
[comp
] * scale
;
460 vert
->colour
[1] = shading
;
466 * Experimental SDF based shadows
468 * https://iquilezles.org/articles/distfunctions/
470 static float sd_cone( v3f co
, sdf_primative
*prim
)
472 float bound
= prim
->info
[1]*1.75f
;
473 if( v3_dist2( prim
->origin
, co
) > bound
*bound
)
477 v3_sub( co
, prim
->origin
, p
);
479 float h
= prim
->info
[1];
480 v2f c
= { prim
->info
[2], prim
->info
[3] };
483 v2_muls( (v2f
){ c
[0]/c
[1], -1.0f
}, h
, q
);
485 w
[0] = v2_length( (v2f
){ p
[0], p
[2] } );
488 v2_muladds( w
, q
, -vg_clampf( v2_dot(w
,q
)/v2_dot(q
,q
), 0.0f
, 1.0f
), a
);
489 v2_muladd( w
, q
, (v2f
){ vg_clampf( w
[0]/q
[0], 0.0f
, 1.0f
), 1.0f
}, b
);
491 float k
= vg_signf( q
[1] ),
492 d
= vg_minf( v2_dot( a
,a
), v2_dot( b
,b
) ),
493 s
= vg_maxf( k
*(w
[0]*q
[1]-w
[1]*q
[0]), k
*(w
[1]-q
[1]) );
495 return sqrtf(d
)*vg_signf(s
);
498 #define CACHE_AMBIENT_SHAPES
500 static float scene_ambient_sample( scene
*pscene
, v3f pos
, v3f dir
)
504 #ifdef CACHE_AMBIENT_SHAPES
505 static struct shadower
*local_shadowers
[32];
506 static int local_shadower_count
= 0;
507 static v3f local_shadower_last
= { -99999.9f
, -999999.9f
, -9999999.9f
};
509 if( v3_dist2( pos
, local_shadower_last
) > 10.0f
*10.0f
)
511 local_shadower_count
= 0;
512 v3_copy( pos
, local_shadower_last
);
514 for( int k
=0; k
<pscene
->shadower_count
; k
++ )
516 struct shadower
*shadower
= &pscene
->shadowers
[k
];
518 if( sd_cone( pos
, &shadower
->sdf
) <= 20.0f
)
520 local_shadowers
[ local_shadower_count
++ ] = shadower
;
521 if( local_shadower_count
== vg_list_size( local_shadowers
) )
528 for( int j
=0; j
<5; j
++ )
531 v3_muladds( pos
, dir
, 1.5f
*(float)j
, tracepos
);
533 float mindist
= 99999.9f
;
535 #ifndef CACHE_AMBIENT_SHAPES
537 for( int k
=0; k
<pscene
->shadower_count
; k
++ ){
538 struct shadower
*shadower
= &pscene
->shadowers
[k
];
541 for( int k
=0; k
<local_shadower_count
; k
++ ){
542 struct shadower
*shadower
= local_shadowers
[k
];
545 float dist
= vg_maxf( 0.0f
, sd_cone( tracepos
, &shadower
->sdf
));
546 mindist
= vg_minf( mindist
, dist
);
550 accum
+= vg_clampf( 1.0f
- mindist
, 0.0f
, 1.0f
)*0.2f
;
557 #define JUST_DO_EVERY_VERT
559 static void scene_compute_occlusion( scene
*pscene
)
561 v3f sundir
= { 0.2f
, 0.9f
, 0.2f
};
562 v3_normalize( sundir
);
564 /* TODO: Make this sample grid be dynamically required.
566 * 1. Only resample the light grid (1x1x1), when a vertex is outside the
569 * 2. Reorder all vertices so that each group of vertices that fit in a
570 * cube are next to eachother in the buffer. This will save cache
573 * for the sorting algorithm, i think we can already assume that *most
574 * vertices will be quite close to eachother. so instead of doing an
575 * exhaustive search we can reorder 1k chunks at a time.
579 v3_sub( pscene
->bbx
[1], pscene
->bbx
[0], sample_area
);
580 v3_ceil( sample_area
, sample_area
);
581 int ax
= sample_area
[0],
586 float *samplegrid
= malloc( ax
*ay
*az
* sizeof(float) );
588 for( int x
=0; x
<ax
; x
++ ){
589 for( int y
=0; y
<ay
; y
++ ){
590 for( int z
=0; z
<az
; z
++ )
592 v3f sample_pos
= { x
,y
,z
};
593 v3_add( pscene
->bbx
[0], sample_pos
, sample_pos
);
594 float accum
= scene_ambient_sample( pscene
, sample_pos
, sundir
);
596 samplegrid
[x
+ y
*ax
+ z
*ax
*ay
] = accum
;
599 v3i cube_pos
= { -999999, -999999, -999999 };
600 int cube_resamples
= 0, hits
= 0, misses
= 0;
602 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
;
605 for( int i
=0; i
<pscene
->vertex_count
; i
++ )
607 model_vert
*vert
= &pscene
->verts
[i
];
611 v3_sub( vert
->co
, pscene
->bbx
[0], q
);
613 v3_copy( vert
->co
, q
);
623 #ifndef JUST_DO_EVERY_VERT
633 s0
= samplegrid
[ x
+ y
*ax
+ z
*ax
*ay
],
634 s1
= samplegrid
[(x
+1) + y
*ax
+ z
*ax
*ay
],
635 s2
= samplegrid
[ x
+ (y
+1)*ax
+ z
*ax
*ay
],
636 s3
= samplegrid
[(x
+1) + (y
+1)*ax
+ z
*ax
*ay
],
637 s4
= samplegrid
[ x
+ y
*ax
+ (z
+1)*ax
*ay
],
638 s5
= samplegrid
[(x
+1) + y
*ax
+ (z
+1)*ax
*ay
],
639 s6
= samplegrid
[ x
+ (y
+1)*ax
+ (z
+1)*ax
*ay
],
640 s7
= samplegrid
[(x
+1) + (y
+1)*ax
+ (z
+1)*ax
*ay
],
642 if( x
!=cube_pos
[0] || y
!=cube_pos
[1] || z
!=cube_pos
[2] )
648 s0
= scene_ambient_sample( pscene
, (v3f
){ x
,y
,z
}, sundir
);
649 s1
= scene_ambient_sample( pscene
, (v3f
){ x
+1,y
,z
}, sundir
);
650 s2
= scene_ambient_sample( pscene
, (v3f
){ x
,y
+1,z
}, sundir
);
651 s3
= scene_ambient_sample( pscene
, (v3f
){ x
+1,y
+1,z
}, sundir
);
652 s4
= scene_ambient_sample( pscene
, (v3f
){ x
,y
,z
+1 }, sundir
);
653 s5
= scene_ambient_sample( pscene
, (v3f
){ x
+1,y
,z
+1 }, sundir
);
654 s6
= scene_ambient_sample( pscene
, (v3f
){ x
,y
+1,z
+1 }, sundir
);
655 s7
= scene_ambient_sample( pscene
, (v3f
){ x
+1,y
+1,z
+1 }, sundir
);
666 s0_s1
= vg_lerpf( s0
, s1
, q
[0] ),
667 s2_s3
= vg_lerpf( s2
, s3
, q
[0] ),
668 s4_s5
= vg_lerpf( s4
, s5
, q
[0] ),
669 s6_s7
= vg_lerpf( s6
, s7
, q
[0] ),
671 s0s1_s2s3
= vg_lerpf( s0_s1
, s2_s3
, q
[1] ),
672 s4s5_s6s7
= vg_lerpf( s4_s5
, s6_s7
, q
[1] ),
673 s0s1s2s3_s4s5s6s7
= vg_lerpf( s0s1_s2s3
, s4s5_s6s7
, q
[2] );
675 vert
->colour
[1] = s0s1s2s3_s4s5s6s7
;
677 vert
->colour
[1] = scene_ambient_sample( pscene
, vert
->co
, sundir
);
682 int cube_resamples
= -1, misses
= 0, hits
= 0;
685 int static_samples
= ax
*ay
*az
,
686 vertex_samples
= pscene
->vertex_count
;
688 if( cube_resamples
< static_samples
)
689 vg_success( "Walking cube beat static grid (%d<%d. %d)!\n",
690 cube_resamples
, static_samples
, vertex_samples
);
692 vg_warn( "Walking cube was worse than static grid (%d<%d. %d).\n",
693 cube_resamples
, static_samples
, vertex_samples
);
695 vg_info( "Hits; %d, misses: %d\n", hits
, misses
);
703 for( int i
=0; i
<pscene
->vertex_count
; i
++ )
705 model_vert
*vert
= &pscene
->verts
[i
];
708 for( int j
=0; j
<5; j
++ )
711 v3_copy( vert
->co
, tracepos
);
712 v3_muladds( tracepos
, sundir
, 1.5f
*(float)j
, tracepos
);
714 float mindist
= 99999.9f
;
716 for( int k
=0; k
<pscene
->shadower_count
; k
++ )
718 struct shadower
*shadower
= &pscene
->shadowers
[k
];
719 float dist
= vg_maxf( 0.0f
, sd_cone( tracepos
, &shadower
->sdf
));
720 mindist
= vg_minf( mindist
, dist
);
723 accum
+= vg_clampf( 1.0f
- mindist
, 0.0f
, 1.0f
)*0.2f
;
726 vert
->colour
[1] = vg_minf( accum
, 1.0f
);
730 static void scene_upload( scene
*pscene
)
732 mesh_upload( &pscene
->mesh
,
733 pscene
->verts
, pscene
->vertex_count
,
734 pscene
->indices
, pscene
->indice_count
);
736 vg_info( "Scene upload\n" );
737 vg_info( " indices:%u\n", pscene
->indice_count
);
738 vg_info( " verts:%u\n", pscene
->vertex_count
);
741 float scene_tree_sway
= 0.1f
;
744 static void scene_foliage_shader_use(void)
746 SHADER_USE( shader_debug_vcol
);
748 glUniformMatrix4fv( SHADER_UNIFORM( shader_debug_vcol
, "uPv" ),
749 1, GL_FALSE
, (float *)vg_pv
);
751 glUniform1i( SHADER_UNIFORM( shader_debug_vcol
, "uMode" ), debugview
);
752 glUniform1i( SHADER_UNIFORM( shader_debug_vcol
, "uTexMain" ), 0 );
754 glUniform1i( SHADER_UNIFORM( shader_debug_vcol
, "uTexGradients" ), 1 );
755 vg_tex2d_bind( &tex_gradients
, 1 );
757 glUniform1i( SHADER_UNIFORM( shader_debug_vcol
, "uTexNoise" ), 2 );
758 glActiveTexture( GL_TEXTURE2
);
759 glBindTexture( GL_TEXTURE_2D
, tex_dual_noise
);
761 glUniform1f( SHADER_UNIFORM( shader_debug_vcol
, "uTime" ), vg_time
);
762 glUniform1f( SHADER_UNIFORM( shader_debug_vcol
, "uSwayAmt" ),
767 static void scene_bind( scene
*pscene
)
769 mesh_bind( &pscene
->mesh
);
772 static void scene_draw( scene
*pscene
)
774 mesh_drawn( 0, pscene
->indice_count
);
777 static void scene_debugsdf( scene
*pscene
)
779 for( int i
=0; i
<pscene
->shadower_count
; i
++ )
781 struct shadower
*shadower
= &pscene
->shadowers
[i
];
784 v3_copy( shadower
->sdf
.origin
, base
);
785 base
[1] -= shadower
->sdf
.info
[1];
786 v3_copy( base
, side
);
787 side
[0] += shadower
->sdf
.info
[0];
789 vg_line2( shadower
->sdf
.origin
, base
, 0xff00ff00, 0xff0000ff );
790 vg_line2( side
, base
, 0xff00ff00, 0xff0000ff );
791 vg_line( side
, shadower
->sdf
.origin
, 0xff00ff00 );
794 v3f p0
= { pscene
->bbx
[0][0], pscene
->bbx
[0][1], pscene
->bbx
[0][2] },
795 p1
= { pscene
->bbx
[0][0], pscene
->bbx
[1][1], pscene
->bbx
[0][2] },
796 p2
= { pscene
->bbx
[1][0], pscene
->bbx
[1][1], pscene
->bbx
[0][2] },
797 p3
= { pscene
->bbx
[1][0], pscene
->bbx
[0][1], pscene
->bbx
[0][2] },
799 p4
= { pscene
->bbx
[0][0], pscene
->bbx
[0][1], pscene
->bbx
[1][2] },
800 p5
= { pscene
->bbx
[0][0], pscene
->bbx
[1][1], pscene
->bbx
[1][2] },
801 p6
= { pscene
->bbx
[1][0], pscene
->bbx
[1][1], pscene
->bbx
[1][2] },
802 p7
= { pscene
->bbx
[1][0], pscene
->bbx
[0][1], pscene
->bbx
[1][2] };
804 u32 col
= 0xffff00c8;
805 vg_line( p0
, p1
, col
);
806 vg_line( p1
, p2
, col
);
807 vg_line( p2
, p3
, col
);
808 vg_line( p3
, p0
, col
);
810 vg_line( p4
, p5
, col
);
811 vg_line( p5
, p6
, col
);
812 vg_line( p6
, p7
, col
);
813 vg_line( p7
, p4
, col
);
815 vg_line( p0
, p4
, col
);
816 vg_line( p1
, p5
, col
);
817 vg_line( p2
, p6
, col
);
818 vg_line( p3
, p7
, col
);
821 static void scene_register(void)
823 SHADER_INIT( shader_debug_vcol
);
824 SHADER_INIT( shader_standard_lit
);
825 SHADER_INIT( shader_unlit
);
829 /* Physics segment */
831 static int triangle_raycast2d( v3f pA
, v3f pB
, v3f pC
, v3f ray
, float *height
)
833 v2f v0
, v1
, v2
, vp
, vp2
;
834 float d
, bca
= 0.f
, bcb
= 0.f
, bcc
= 0.f
;
836 v0
[0] = pB
[0] - pA
[0];
837 v0
[1] = pB
[2] - pA
[2];
838 v1
[0] = pC
[0] - pA
[0];
839 v1
[1] = pC
[2] - pA
[2];
840 v2
[0] = pB
[0] - pC
[0];
841 v2
[1] = pB
[2] - pC
[2];
843 d
= 1.f
/ (v0
[0]*v1
[1] - v1
[0]*v0
[1]);
846 /* Backface culling */
847 if( v2_cross( v0
, v1
) > 0.f
)
851 vp
[0] = ray
[0] - pA
[0];
852 vp
[1] = ray
[2] - pA
[2];
854 if( v2_cross( v0
, vp
) > 0.f
) return 0;
855 if( v2_cross( vp
, v1
) > 0.f
) return 0;
857 vp2
[0] = ray
[0] - pB
[0];
858 vp2
[1] = ray
[2] - pB
[2];
860 if( v2_cross( vp2
, v2
) > 0.f
) return 0;
862 bcb
= (vp
[0]*v1
[1] - v1
[0]*vp
[1]) * d
;
863 bcc
= (v0
[0]*vp
[1] - vp
[0]*v0
[1]) * d
;
864 bca
= 1.f
- bcb
- bcc
;
866 *height
= pA
[1]*bca
+ pB
[1]*bcb
+ pC
[1]*bcc
;
871 static int sample_scene_height( scene
*pscene
, v3f pos
, v3f norm
)
873 for( int i
=0; i
<pscene
->indice_count
/3; i
++ )
875 u32
*tri
= &pscene
->indices
[i
*3];
877 float *pA
= pscene
->verts
[tri
[0]].co
,
878 *pB
= pscene
->verts
[tri
[1]].co
,
879 *pC
= pscene
->verts
[tri
[2]].co
;
882 if( triangle_raycast2d( pA
, pB
, pC
, pos
, &height
))
889 v3_sub( pA
, pB
, v0
);
890 v3_sub( pC
, pB
, v1
);
891 v3_cross( v1
, v0
, norm
);
892 v3_normalize( norm
);
901 static void sample_scene_normal( scene
*pscene
, v3f pos
, v3f normal
)
903 for( int i
=0; i
<pscene
->indice_count
/3; i
++ )
905 u32
*tri
= &pscene
->indices
[i
*3];
908 if( triangle_raycast2d(
909 pscene
->verts
[ tri
[0] ].co
,
910 pscene
->verts
[ tri
[1] ].co
,
911 pscene
->verts
[ tri
[2] ].co
, pos
, &height
))
915 v3_sub( pscene
->verts
[ tri
[1] ].co
,
916 pscene
->verts
[ tri
[0] ].co
,
919 v3_sub( pscene
->verts
[ tri
[2] ].co
,
920 pscene
->verts
[ tri
[0] ].co
,
923 v3_cross( v0
, v1
, normal
);
924 v3_normalize( normal
);
938 /* if il is 0, this is a leaf */
940 union{ u32 ir
, start
; };
943 static void bvh_update_bounds( scene
*s
, u32 inode
)
945 bvh_node
*node
= &s
->bvh
.nodes
[ inode
];
947 box_init_inf( node
->bbx
);
948 for( u32 i
=0; i
<node
->count
; i
++ )
950 u32 idx
= node
->start
+i
;
951 model_vert
*pa
= &s
->verts
[ s
->indices
[idx
*3+0] ],
952 *pb
= &s
->verts
[ s
->indices
[idx
*3+1] ],
953 *pc
= &s
->verts
[ s
->indices
[idx
*3+2] ];
955 box_addpt( node
->bbx
, pa
->co
);
956 box_addpt( node
->bbx
, pb
->co
);
957 box_addpt( node
->bbx
, pc
->co
);
961 static void bvh_subdiv( scene
*s
, u32 inode
)
963 bvh_node
*node
= &s
->bvh
.nodes
[ inode
];
966 v3_sub( node
->bbx
[1], node
->bbx
[0], extent
);
969 if( extent
[1] > extent
[0] ) axis
= 1;
970 if( extent
[2] > extent
[axis
] ) axis
= 2;
972 float split
= node
->bbx
[0][axis
] + extent
[axis
]*0.5f
;
974 /* To beat: 121,687 / 136,579
979 for( u32 t
=0; t
<node
->count
; t
++ )
981 u32
*ti
= &s
->indices
[(node
->start
+t
)*3];
982 float a
= s
->verts
[ti
[0]].co
[axis
],
983 b
= s
->verts
[ti
[1]].co
[axis
],
984 c
= s
->verts
[ti
[2]].co
[axis
];
987 avg
/= (float)node
->count
;
992 j
= i
+ node
->count
-1;
996 u32
*ti
= &s
->indices
[i
*3];
998 float a
= s
->verts
[ti
[0]].co
[axis
],
999 b
= s
->verts
[ti
[1]].co
[axis
],
1000 c
= s
->verts
[ti
[2]].co
[axis
];
1002 if( ((a
+b
+c
) / 3.0f
) < split
)
1006 /* Swap triangle indices */
1007 u32
*tj
= &s
->indices
[j
*3];
1025 u32 left_count
= i
- node
->start
;
1026 if( left_count
== 0 || left_count
== node
->count
) return;
1028 u32 il
= s
->bvh
.node_count
++,
1029 ir
= s
->bvh
.node_count
++;
1031 struct bvh_node
*lnode
= &s
->bvh
.nodes
[il
],
1032 *rnode
= &s
->bvh
.nodes
[ir
];
1034 lnode
->start
= node
->start
;
1035 lnode
->count
= left_count
;
1037 rnode
->count
= node
->count
- left_count
;
1043 bvh_update_bounds( s
, il
);
1044 bvh_update_bounds( s
, ir
);
1045 bvh_subdiv( s
, il
);
1046 bvh_subdiv( s
, ir
);
1049 static void bvh_create( scene
*s
)
1051 u32 triangle_count
= s
->indice_count
/ 3;
1052 s
->bvh
.nodes
= malloc( sizeof(struct bvh_node
) * (triangle_count
*2-1) );
1054 bvh_node
*root
= &s
->bvh
.nodes
[0];
1055 s
->bvh
.node_count
= 1;
1059 root
->count
= triangle_count
;
1062 bvh_update_bounds( s
, 0 );
1066 realloc( s
->bvh
.nodes
, sizeof(struct bvh_node
) * s
->bvh
.node_count
);
1068 vg_success( "BVH done, size: %u/%u\n", s
->bvh
.node_count
,
1069 (triangle_count
*2-1) );
1072 static void bvh_debug_node( scene
*s
, u32 inode
, v3f pos
, u32 colour
)
1074 struct bvh_node
*node
= &s
->bvh
.nodes
[ inode
];
1076 if( (pos
[0] >= node
->bbx
[0][0] && pos
[0] <= node
->bbx
[1][0]) &&
1077 (pos
[2] >= node
->bbx
[0][2] && pos
[2] <= node
->bbx
[1][2]) )
1081 vg_line_boxf( node
->bbx
, colour
);
1083 bvh_debug_node( s
, node
->il
, pos
, colour
);
1084 bvh_debug_node( s
, node
->ir
, pos
, colour
);
1088 vg_line_boxf( node
->bbx
, 0xff00ff00 );
1089 for( u32 i
=0; i
<node
->count
; i
++ )
1091 u32 idx
= (node
->start
+i
)*3;
1093 model_vert
*pa
= &s
->verts
[ s
->indices
[ idx
+0 ] ],
1094 *pb
= &s
->verts
[ s
->indices
[ idx
+1 ] ],
1095 *pc
= &s
->verts
[ s
->indices
[ idx
+2 ] ];
1097 vg_line( pa
->co
, pb
->co
, 0xff0000ff );
1098 vg_line( pb
->co
, pc
->co
, 0xff0000ff );
1099 vg_line( pc
->co
, pa
->co
, 0xff0000ff );
1105 static void bvh_debug( scene
*s
, v3f pos
)
1107 bvh_debug_node( s
, 0, pos
, 0x4000ffa8 );
1110 typedef struct ray_hit ray_hit
;
1118 int ray_aabb( boxf box
, v3f co
, v3f dir
, float dist
)
1123 v3_sub( box
[0], co
, v0
);
1124 v3_sub( box
[1], co
, v1
);
1125 v3_div( v0
, dir
, v0
);
1126 v3_div( v1
, dir
, v1
);
1128 tmin
= vg_minf( v0
[0], v1
[0] );
1129 tmax
= vg_maxf( v0
[0], v1
[0] );
1130 tmin
= vg_maxf( tmin
, vg_minf( v0
[1], v1
[1] ));
1131 tmax
= vg_minf( tmax
, vg_maxf( v0
[1], v1
[1] ));
1132 tmin
= vg_maxf( tmin
, vg_minf( v0
[2], v1
[2] ));
1133 tmax
= vg_minf( tmax
, vg_maxf( v0
[2], v1
[2] ));
1135 return tmax
>= tmin
&& tmin
< dist
&& tmax
> 0;
1138 static int bvh_ray_tri( scene
*sc
, u32
*tri
, v3f co
, v3f dir
, ray_hit
*hit
)
1140 float const kEpsilon
= 0.00001f
;
1142 v3f v0
, v1
, h
, s
, q
, n
;
1145 float *pa
= sc
->verts
[tri
[0]].co
,
1146 *pb
= sc
->verts
[tri
[1]].co
,
1147 *pc
= sc
->verts
[tri
[2]].co
;
1149 v3_sub( pb
, pa
, v0
);
1150 v3_sub( pc
, pa
, v1
);
1151 v3_cross( dir
, v1
, h
);
1152 v3_cross( v0
, v1
, n
);
1154 if( v3_dot( n
, dir
) > 0.0f
) /* Backface culling */
1158 a
= v3_dot( v0
, h
);
1159 if( a
> -kEpsilon
&& a
< kEpsilon
)
1163 v3_sub( co
, pa
, s
);
1165 u
= f
* v3_dot(s
, h
);
1166 if( u
< 0.0f
|| u
> 1.0f
)
1169 v3_cross( s
, v0
, q
);
1170 v
= f
* v3_dot( dir
, q
);
1171 if( v
< 0.0f
|| u
+v
> 1.0f
)
1174 t
= f
* v3_dot(v1
, q
);
1175 if( t
> kEpsilon
&& t
< hit
->dist
)
1184 static int bvh_ray( scene
*s
, u32 inode
, v3f co
, v3f dir
, ray_hit
*hit
)
1186 bvh_node
*node
= &s
->bvh
.nodes
[ inode
];
1188 if( !ray_aabb( node
->bbx
, co
, dir
, hit
->dist
))
1195 for( u32 i
=0; i
<node
->count
; i
++ )
1197 u32
*indices
= &s
->indices
[ (node
->start
+i
)*3 ];
1198 count
+= bvh_ray_tri( s
, indices
, co
, dir
, hit
);
1203 count
+= bvh_ray( s
, node
->il
, co
, dir
, hit
);
1204 count
+= bvh_ray( s
, node
->ir
, co
, dir
, hit
);
1210 static int bvh_raycast( scene
*s
, v3f co
, v3f dir
, ray_hit
*hit
)
1213 v3_muladds( co
, dir
, hit
->dist
, pb
);
1215 int count
= bvh_ray( s
, 0, co
, dir
, hit
);
1219 //vg_line( co, pb, 0xff00ffff );
1223 float *pa
= s
->verts
[hit
->tri
[0]].co
,
1224 *pb
= s
->verts
[hit
->tri
[1]].co
,
1225 *pc
= s
->verts
[hit
->tri
[2]].co
;
1227 v3_sub( pa
, pb
, v0
);
1228 v3_sub( pc
, pb
, v1
);
1229 v3_cross( v1
, v0
, hit
->normal
);
1230 v3_normalize( hit
->normal
);
1231 v3_muladds( co
, dir
, hit
->dist
, hit
->pos
);
1235 //vg_line( co, pb, 0xff0000ff );
1241 static int bvh_scene_sample_node_h( scene
*s
, u32 inode
, v3f pos
, v3f norm
)
1243 bvh_node
*node
= &s
->bvh
.nodes
[ inode
];
1245 if( (pos
[0] >= node
->bbx
[0][0] && pos
[0] <= node
->bbx
[1][0]) &&
1246 (pos
[2] >= node
->bbx
[0][2] && pos
[2] <= node
->bbx
[1][2]) )
1250 if( bvh_scene_sample_node_h( s
, node
->il
, pos
, norm
)) return 1;
1251 if( bvh_scene_sample_node_h( s
, node
->ir
, pos
, norm
)) return 1;
1255 for( u32 i
=0; i
<node
->count
; i
++ )
1257 u32 idx
= (node
->start
+i
)*3;
1258 model_vert
*pa
= &s
->verts
[ s
->indices
[ idx
+0 ] ],
1259 *pb
= &s
->verts
[ s
->indices
[ idx
+1 ] ],
1260 *pc
= &s
->verts
[ s
->indices
[ idx
+2 ] ];
1263 if( triangle_raycast2d( pa
->co
, pb
->co
, pc
->co
, pos
, &height
))
1270 v3_sub( pa
->co
, pb
->co
, v0
);
1271 v3_sub( pc
->co
, pb
->co
, v1
);
1272 v3_cross( v1
, v0
, norm
);
1273 v3_normalize( norm
);
1285 static int bvh_scene_sample_h( scene
*s
, v3f pos
, v3f norm
)
1287 return bvh_scene_sample_node_h( s
, 0, pos
, norm
);
1290 static int bvh_scene_sample( scene
*s
, v3f pos
, v3f norm
)
1293 hit
.dist
= INFINITY
;
1296 v3_add( pos
, (v3f
){0.0f
,16000.0f
,0.0f
}, ray_pos
);
1298 if( bvh_raycast( s
, ray_pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
1300 pos
[1] = hit
.pos
[1];
1303 v3_copy( hit
.normal
, norm
);