latest
[carveJwlIkooP6JGAAIwe30JlM.git] / scene.h
1 #include "vg/vg.h"
2 #include "model.h"
3
4 GLuint tex_dual_noise;
5
6 struct scene
7 {
8 glmesh mesh;
9
10 model_vert *verts;
11 u32 *indices;
12
13 u32 vertex_count,
14 indice_count,
15 vertex_cap,
16 indice_cap;
17
18 boxf bbx;
19
20 struct shadower
21 {
22 sdf_primative sdf;
23 esdf_type sdf_type;
24 }
25 *shadowers;
26
27 u32 shadower_count,
28 shadower_cap;
29
30 submodel submesh;
31 };
32
33 static void scene_init( scene *pscene )
34 {
35 pscene->verts = NULL;
36 pscene->indices = NULL;
37 pscene->vertex_count = 0;
38 pscene->indice_count = 0;
39 pscene->shadowers = NULL;
40 pscene->shadower_count = 0;
41 pscene->shadower_cap = 0;
42 pscene->submesh.indice_start = 0;
43 pscene->submesh.indice_count = 0;
44
45 v3_fill( pscene->bbx[0], 999999.9f );
46 v3_fill( pscene->bbx[1], -999999.9f );
47
48 static int noise_ready = 0;
49 if( !noise_ready )
50 {
51 noise_ready = 1;
52
53 u8 *buf = malloc( 256*256*2 );
54
55 for( int i=0; i<256*256; i++ )
56 {
57 u8 val = rand()&0xff;
58 buf[i*2] = val;
59 }
60
61 for( int y=0; y<256; y++ )
62 {
63 for( int x=0; x<256; x++ )
64 {
65 u8 *pr = &buf[(y*256+x)*2],
66 *pg = &buf[(((y+17)&0xff)*256+((x+37)&0xff))*2+1];
67 *pg = *pr;
68 }
69 }
70
71 /* TODO: This texture should be delted somewhere */
72 glGenTextures( 1, &tex_dual_noise );
73 glBindTexture( GL_TEXTURE_2D, tex_dual_noise );
74 glTexImage2D( GL_TEXTURE_2D, 0, GL_RG, 256, 256, 0, GL_RG,
75 GL_UNSIGNED_BYTE, buf );
76
77 vg_tex2d_linear();
78 vg_tex2d_repeat();
79
80 free( buf );
81 }
82 }
83
84 /* https://www.shadertoy.com/view/4sfGzS */
85 #define SHADER_VALUE_NOISE_3D \
86 "uniform sampler2D uTexNoise;" \
87 "" \
88 "float noise( vec3 x )" \
89 "{" \
90 "vec3 i = floor(x);" \
91 "vec3 f = fract(x);" \
92 "f = f*f*(3.0-2.0*f);" \
93 "vec2 uv = (i.xy+vec2(37.0,17.0)*i.z) + f.xy;" \
94 "vec2 rg = texture( uTexNoise, (uv+0.5)/256.0).yx;"\
95 "return mix( rg.x, rg.y, f.z );" \
96 "}" \
97 "" \
98 "const mat3 m = mat3( 0.00, 0.80, 0.60," \
99 "-0.80, 0.36, -0.48," \
100 "-0.60, -0.48, 0.64 );" \
101 "" \
102 "float fractalNoise( vec3 x )" \
103 "{" \
104 "vec3 q = 8.0*x;" \
105 "float f;" \
106 "f = 0.5000*noise( q ); q = m*q*2.01;" \
107 "f += 0.2500*noise( q ); q = m*q*2.02;" \
108 "f += 0.1250*noise( q ); q = m*q*2.03;" \
109 "f += 0.0625*noise( q ); q = m*q*2.01;" \
110 "return f;" \
111 "}"
112
113 #define VERTEX_STANDARD_ATTRIBUTES \
114 "layout (location=0) in vec3 a_co;" \
115 "layout (location=1) in vec3 a_norm;" \
116 "layout (location=2) in vec4 a_colour;" \
117 "layout (location=3) in vec2 a_uv;"
118
119 SHADER_DEFINE( shader_debug_vcol,
120
121 /*Include*/ VERTEX_STANDARD_ATTRIBUTES
122
123 "uniform mat4 uPv;"
124 "uniform mat4 uMdl;"
125 "uniform float uTime;"
126 "uniform float uSwayAmt;"
127 ""
128 "out vec4 aColour;"
129 "out vec2 aUv;"
130 "out vec3 aNorm;"
131 "out vec3 aCo;"
132 ""
133 "vec3 compute_sway( vec3 pos )"
134 "{"
135 "vec4 sines = vec4( sin(uTime + pos.x)*1.0,"
136 "sin(uTime*1.2 + pos.z*2.0)*1.1,"
137 "sin(uTime*2.33)*0.5,"
138 "sin(uTime*0.6 + pos.x*0.3)*1.3 );"
139
140 "vec3 offset = vec3( sines.x+sines.y*sines.w, 0.0, sines.x+sines.z );"
141 "return pos + offset*a_colour.r*uSwayAmt;"
142 "}"
143 ""
144 "void main()"
145 "{"
146 "vec3 swaypos = compute_sway( a_co );"
147 "gl_Position = uPv * uMdl * vec4( swaypos, 1.0 );"
148 "aColour = a_colour;"
149 "aUv = a_uv;"
150 "aNorm = normalize(mat3(uMdl) * a_norm);"
151 "aCo = a_co;"
152 "}",
153 /* Fragment */
154 "out vec4 FragColor;"
155 ""
156 "uniform int uMode;"
157 "uniform sampler2D uTexMain;"
158 "uniform sampler2D uTexGradients;"
159 ""
160 /*Include*/ SHADER_VALUE_NOISE_3D
161 ""
162 "in vec4 aColour;"
163 "in vec2 aUv;"
164 "in vec3 aNorm;"
165 "in vec3 aCo;"
166 ""
167 "void main()"
168 "{"
169 "vec4 colour = vec4(1.0,0.0,0.5,1.0);"
170 "vec4 diffuse = texture( uTexMain, aUv );"
171
172 "if( uMode == 1 )"
173 "{"
174 "colour = vec4(aNorm * 0.5 + 0.5, 1.0);"
175 "}"
176 "if( uMode == 2 )"
177 "{"
178 "colour = aColour;"
179 "}"
180 "if( uMode == 3 )"
181 "{"
182 "float light = dot(aNorm, vec3(0.2,0.8,0.1));"
183 "vec3 grid3 = fract(aCo);"
184
185 "colour = vec4(vec3(light)*(1.0-grid3*0.3),1.0);"
186 "}"
187 "if( uMode == 4 )"
188 "{"
189 "colour = vec4( aUv, 0.0, 1.0 );"
190 "}"
191 "if( uMode == 5 )"
192 "{"
193 "if( diffuse.a < 0.45 ) discard;"
194 "colour = diffuse;"
195 "}"
196 "if( uMode == 6 )"
197 "{"
198 "float r1 = fractalNoise(aCo);"
199 "colour = vec4( vec3(r1), 1.0 );"
200 "}"
201 "if( uMode == 7 )"
202 "{"
203 "if( diffuse.a < 0.2 ) discard;"
204 "float lighting = 1.0 - aColour.g*0.8;"
205
206 "float light1 = max(0.0,dot(-vec3(0.5,-0.8,0.25), aNorm));"
207 "float light2 = max(0.0,dot(-vec3(-0.8,0.5,-0.25), aNorm));"
208 "vec3 lt = vec3(0.2,0.2,0.2 ) + "
209 "vec3(1.0,1.0,0.9)*light1 + "
210 "vec3(0.1,0.3,0.4 )*light2;"
211
212
213 "colour = vec4(vec3(pow(lighting,1.6)*(diffuse.r*0.7+0.5)),1.0);"
214 "colour = vec4(colour.rgb*lt,1.0);"
215
216 "vec2 gradUV = vec2(lighting*1.9,aColour.b*0.8);"
217 "vec4 gradient_sample = texture( uTexGradients, gradUV );"
218 "colour = colour*gradient_sample;"
219 "}"
220 "if( uMode == 8 )"
221 "{"
222 "if( diffuse.a < 0.45 ) discard;"
223 "float light = 1.0 - aColour.g;"
224 "light = pow(light,1.6)*(diffuse.r*0.7+0.5);"
225 "float r1 = fractalNoise(aCo*0.01);"
226
227 "vec2 gradUV = vec2(light*1.9,r1+aColour.b);"
228 "vec4 gradient_sample = texture( uTexGradients, gradUV );"
229 "colour = gradient_sample*light;"
230 "}"
231
232 "FragColor = colour;"
233 "}"
234 ,
235 UNIFORMS({ "uPv", "uMode", "uTexMain", "uTexGradients", "uTexNoise", \
236 "uTime", "uSwayAmt", "uMdl" })
237 )
238
239 SHADER_DEFINE( shader_standard_lit,
240
241 /*Include*/ VERTEX_STANDARD_ATTRIBUTES
242
243 "uniform mat4 uPv;"
244 "uniform mat4 uMdl;"
245 ""
246 "out vec4 aColour;"
247 "out vec2 aUv;"
248 "out vec3 aNorm;"
249 "out vec3 aCo;"
250 ""
251 "void main()"
252 "{"
253 "gl_Position = uPv * uMdl * vec4( a_co, 1.0 );"
254 "aColour = a_colour;"
255 "aUv = a_uv;"
256 "aNorm = mat3(uMdl) * a_norm;"
257 "aCo = a_co;"
258 "}",
259 /* Fragment */
260 "out vec4 FragColor;"
261 ""
262 "uniform sampler2D uTexMain;"
263 "uniform vec4 uColour;"
264 ""
265 "in vec4 aColour;"
266 "in vec2 aUv;"
267 "in vec3 aNorm;"
268 "in vec3 aCo;"
269 ""
270 "void main()"
271 "{"
272 "vec3 diffuse = texture( uTexMain, aUv ).rgb;"
273
274 "float light1 = max(0.0,dot(-vec3(0.5,-0.8,0.25), aNorm));"
275 "float light2 = max(0.0,dot(-vec3(-0.8,0.5,-0.25), aNorm));"
276 "diffuse += vec3(0.2,0.2,0.2 ) + "
277 "vec3(1.0,1.0,0.9)*light1 + "
278 "vec3(0.1,0.3,0.4 )*light2;"
279
280 "FragColor = vec4((diffuse*uColour.rgb),"
281 "aColour.a*uColour.a);"
282 "}"
283 ,
284 UNIFORMS({ "uColour","uTexMain","uPv","uMdl" })
285 )
286
287 SHADER_DEFINE( shader_unlit,
288
289 /*Include*/ VERTEX_STANDARD_ATTRIBUTES
290
291 "uniform mat4 uPv;"
292 "uniform mat4 uMdl;"
293 ""
294 "out vec4 aColour;"
295 "out vec2 aUv;"
296 "out vec3 aNorm;"
297 "out vec3 aCo;"
298 ""
299 "void main()"
300 "{"
301 "gl_Position = uPv * uMdl * vec4( a_co, 1.0 );"
302 "aColour = a_colour;"
303 "aUv = a_uv;"
304 "aNorm = mat3(uMdl) * a_norm;"
305 "aCo = a_co;"
306 "}",
307 /* Fragment */
308 "out vec4 FragColor;"
309 ""
310 "uniform sampler2D uTexMain;"
311 "uniform vec4 uColour;"
312 ""
313 "in vec4 aColour;"
314 "in vec2 aUv;"
315 "in vec3 aNorm;"
316 "in vec3 aCo;"
317 ""
318 "void main()"
319 "{"
320 "vec3 diffuse = texture( uTexMain, aUv ).rgb;"
321 "FragColor = vec4(pow(diffuse,vec3(1.0/2.2)),1.0);"
322 "}"
323 ,
324 UNIFORMS({ "uTexMain", "uPv", "uMdl" })
325 )
326
327 static void *buffer_reserve( void *buffer, u32 count, u32 *cap, u32 amount,
328 size_t emsize )
329 {
330 if( count+amount > *cap )
331 {
332 *cap = VG_MAX( (*cap)*2, (*cap)+amount );
333
334 return realloc( buffer, (*cap) * emsize );
335 }
336
337 return buffer;
338 }
339
340 /*
341 * Append a model into the scene with a given transform
342 */
343 static void scene_add_model( scene *pscene, model *mdl, submodel *submodel,
344 v3f pos, float yaw, float scale )
345 {
346 pscene->verts = buffer_reserve( pscene->verts, pscene->vertex_count,
347 &pscene->vertex_cap, submodel->vertex_count, sizeof(model_vert) );
348 pscene->indices = buffer_reserve( pscene->indices, pscene->indice_count,
349 &pscene->indice_cap, submodel->indice_count, sizeof(u32) );
350
351 if( submodel->sdf_type )
352 {
353 pscene->shadowers = buffer_reserve( pscene->shadowers,
354 pscene->shadower_count, &pscene->shadower_cap, 1,
355 sizeof( struct shadower ));
356
357 struct shadower *shadower =
358 &pscene->shadowers[ pscene->shadower_count ++ ];
359
360 shadower->sdf = submodel->sdf;
361 shadower->sdf_type = submodel->sdf_type;
362
363 v2_muls( shadower->sdf.info, scale, shadower->sdf.info );
364 v3_muls( shadower->sdf.origin, scale, shadower->sdf.origin );
365 v3_add( pos, shadower->sdf.origin, shadower->sdf.origin );
366 }
367
368 /* Transform and place vertices */
369 model_vert *src_verts = submodel_vert_data( mdl, submodel );
370 u32 *src_indices = submodel_indice_data( mdl, submodel );
371
372 m4x3f mtx;
373 m4x3_identity( mtx );
374 m4x3_translate( mtx, pos );
375 m4x3_rotate_y( mtx, yaw );
376 m4x3_scale( mtx, scale );
377
378 boxf bbxnew;
379 box_copy( submodel->bbx, bbxnew );
380 m4x3_transform_aabb( mtx, bbxnew );
381 box_concat( pscene->bbx, bbxnew );
382
383 m3x3f rotation;
384 m4x3_to_3x3( mtx, rotation );
385
386 float rand_hue = vg_randf();
387
388 for( u32 i=0; i<submodel->vertex_count; i++ )
389 {
390 model_vert *pvert = &pscene->verts[ pscene->vertex_count+i ],
391 *src = &src_verts[ i ];
392
393 m4x3_mulv( mtx, src->co, pvert->co );
394 m3x3_mulv( rotation, src->norm, pvert->norm );
395
396 v4_copy( src->colour, pvert->colour );
397 v2_copy( src->uv, pvert->uv );
398
399 float rel_y = src->co[1] / submodel->bbx[1][1];
400 pvert->colour[0] = rel_y;
401 pvert->colour[2] = rand_hue;
402 }
403
404 for( u32 i=0; i<submodel->indice_count; i++ )
405 {
406 u32 *pidx = &pscene->indices[ pscene->indice_count+i ];
407 *pidx = src_indices[i] + pscene->vertex_count;
408 }
409
410 pscene->vertex_count += submodel->vertex_count;
411 pscene->indice_count += submodel->indice_count;
412 }
413
414 static void scene_copy_slice( scene *pscene, submodel *sm )
415 {
416 sm->indice_start = pscene->submesh.indice_start;
417 sm->indice_count = pscene->indice_count - sm->indice_start;
418
419 pscene->submesh.indice_start = pscene->indice_count;
420 }
421
422 static void scene_shadow_sphere( scene *pscene, v3f sphere,
423 v4f params, v3f lightdir )
424 {
425 for( int i=0; i<pscene->vertex_count; i++ )
426 {
427 model_vert *vert = &pscene->verts[i];
428
429 v3f delta;
430 v3_sub( sphere, vert->co, delta );
431
432 float d = v3_dot( lightdir, delta );
433 v3f closest;
434
435 v3_muls( lightdir, d, closest );
436 float dist = v3_dist( closest, delta ),
437 shading = vg_maxf( dist - params[0], 0.0f );
438
439 shading = vg_minf( shading * params[1], 1.0f );
440 vert->colour[1] *= shading;
441 }
442 }
443
444 static void scene_shadow_gradient( scene *pscene, int comp,
445 float start, float length )
446 {
447 float scale = 1.0f / length;
448
449 for( int i=0; i<pscene->vertex_count; i++ )
450 {
451 model_vert *vert = &pscene->verts[i];
452 float shading = start + vert->co[comp] * scale;
453
454 vert->colour[1] = shading;
455 }
456 }
457
458 /* Temporary */
459 static int sample_scene_height( scene *pscene, v3f pos, v3f norm )
460 {
461 for( int i=0; i<pscene->indice_count/3; i++ )
462 {
463 u32 *tri = &pscene->indices[i*3];
464
465 float *pA = pscene->verts[tri[0]].co,
466 *pB = pscene->verts[tri[1]].co,
467 *pC = pscene->verts[tri[2]].co;
468
469 float height;
470 if( triangle_raycast( pA, pB, pC, pos, &height ))
471 {
472 pos[1] = height;
473
474 if( norm )
475 {
476 v3f v0, v1;
477 v3_sub( pA, pB, v0 );
478 v3_sub( pC, pB, v1 );
479 v3_cross( v1, v0, norm );
480 v3_normalize( norm );
481 }
482
483 return 1;
484 }
485 }
486 return 0;
487 }
488
489 static void sample_scene_normal( scene *pscene, v3f pos, v3f normal )
490 {
491 for( int i=0; i<pscene->indice_count/3; i++ )
492 {
493 u32 *tri = &pscene->indices[i*3];
494
495 float height;
496 if( triangle_raycast(
497 pscene->verts[ tri[0] ].co,
498 pscene->verts[ tri[1] ].co,
499 pscene->verts[ tri[2] ].co, pos, &height ))
500 {
501 v3f v0, v1;
502
503 v3_sub( pscene->verts[ tri[1] ].co,
504 pscene->verts[ tri[0] ].co,
505 v0 );
506
507 v3_sub( pscene->verts[ tri[2] ].co,
508 pscene->verts[ tri[0] ].co,
509 v1 );
510
511 v3_cross( v0, v1, normal );
512 v3_normalize( normal );
513 return;
514 }
515 }
516
517 normal[0] = 0.0f;
518 normal[1] = 1.0f;
519 normal[2] = 0.0f;
520 }
521
522 /*
523 * Experimental SDF based shadows
524 *
525 * https://iquilezles.org/articles/distfunctions/
526 */
527 static float sd_cone( v3f co, sdf_primative *prim )
528 {
529 float bound = prim->info[1]*1.75f;
530 if( v3_dist2( prim->origin, co ) > bound*bound )
531 return 999999.9f;
532
533 v3f p;
534 v3_sub( co, prim->origin, p );
535
536 float h = prim->info[1];
537 v2f c = { prim->info[2], prim->info[3] };
538
539 v2f q, w, a, b;
540 v2_muls( (v2f){ c[0]/c[1], -1.0f }, h, q );
541
542 w[0] = v2_length( (v2f){ p[0], p[2] } );
543 w[1] = p[1];
544
545 v2_muladds( w, q, -vg_clampf( v2_dot(w,q)/v2_dot(q,q), 0.0f, 1.0f ), a );
546 v2_muladd( w, q, (v2f){ vg_clampf( w[0]/q[0], 0.0f, 1.0f ), 1.0f }, b );
547
548 float k = vg_signf( q[1] ),
549 d = vg_minf( v2_dot( a,a ), v2_dot( b,b ) ),
550 s = vg_maxf( k*(w[0]*q[1]-w[1]*q[0]), k*(w[1]-q[1]) );
551
552 return sqrtf(d)*vg_signf(s);
553 }
554
555 #define CACHE_AMBIENT_SHAPES
556
557 static float scene_ambient_sample( scene *pscene, v3f pos, v3f dir )
558 {
559 float accum = 0.0f;
560
561 #ifdef CACHE_AMBIENT_SHAPES
562 static struct shadower *local_shadowers[32];
563 static int local_shadower_count = 0;
564 static v3f local_shadower_last = { -99999.9f, -999999.9f, -9999999.9f };
565
566 if( v3_dist2( pos, local_shadower_last ) > 10.0f*10.0f )
567 {
568 local_shadower_count = 0;
569 v3_copy( pos, local_shadower_last );
570
571 for( int k=0; k<pscene->shadower_count; k++ )
572 {
573 struct shadower *shadower = &pscene->shadowers[k];
574
575 if( sd_cone( pos, &shadower->sdf ) <= 20.0f )
576 {
577 local_shadowers[ local_shadower_count ++ ] = shadower;
578 if( local_shadower_count == vg_list_size( local_shadowers ) )
579 break;
580 }
581 }
582 }
583 #endif
584
585 for( int j=0; j<5; j++ )
586 {
587 v3f tracepos;
588 v3_muladds( pos, dir, 1.5f*(float)j, tracepos );
589
590 float mindist = 99999.9f;
591
592 #ifndef CACHE_AMBIENT_SHAPES
593
594 for( int k=0; k<pscene->shadower_count; k++ ){
595 struct shadower *shadower = &pscene->shadowers[k];
596 #else
597
598 for( int k=0; k<local_shadower_count; k++ ){
599 struct shadower *shadower = local_shadowers[k];
600 #endif
601
602 float dist = vg_maxf( 0.0f, sd_cone( tracepos, &shadower->sdf ));
603 mindist = vg_minf( mindist, dist );
604 }
605
606
607 accum += vg_clampf( 1.0f - mindist, 0.0f, 1.0f )*0.2f;
608 }
609
610 return accum;
611 }
612
613 #define DYNAMIC_GRID
614 #define JUST_DO_EVERY_VERT
615
616 static void scene_compute_occlusion( scene *pscene )
617 {
618 v3f sundir = { 0.2f, 0.9f, 0.2f };
619 v3_normalize( sundir );
620
621 /* TODO: Make this sample grid be dynamically required.
622 *
623 * 1. Only resample the light grid (1x1x1), when a vertex is outside the
624 * current cube
625 *
626 * 2. Reorder all vertices so that each group of vertices that fit in a
627 * cube are next to eachother in the buffer. This will save cache
628 * misses.
629 *
630 * for the sorting algorithm, i think we can already assume that *most
631 * vertices will be quite close to eachother. so instead of doing an
632 * exhaustive search we can reorder 1k chunks at a time.
633 */
634
635 v3f sample_area;
636 v3_sub( pscene->bbx[1], pscene->bbx[0], sample_area );
637 v3_ceil( sample_area, sample_area );
638 int ax = sample_area[0],
639 ay = sample_area[1],
640 az = sample_area[2];
641
642 #ifndef DYNAMIC_GRID
643 float *samplegrid = malloc( ax*ay*az* sizeof(float) );
644
645 for( int x=0; x<ax; x++ ){
646 for( int y=0; y<ay; y++ ){
647 for( int z=0; z<az; z++ )
648 {
649 v3f sample_pos = { x,y,z };
650 v3_add( pscene->bbx[0], sample_pos, sample_pos );
651 float accum = scene_ambient_sample( pscene, sample_pos, sundir );
652
653 samplegrid[x + y*ax + z*ax*ay] = accum;
654 }}}
655 #else
656 v3i cube_pos = { -999999, -999999, -999999 };
657 int cube_resamples = 0, hits = 0, misses = 0;
658
659 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;
660 #endif
661
662 for( int i=0; i<pscene->vertex_count; i++ )
663 {
664 model_vert *vert = &pscene->verts[i];
665 v3f rel, q;
666
667 #ifndef DYNAMIC_GRID
668 v3_sub( vert->co, pscene->bbx[0], q );
669 #else
670 v3_copy( vert->co, q );
671 #endif
672
673 v3_floor( q, rel );
674 v3_sub( q, rel, q );
675
676 int x=rel[0],
677 y=rel[1],
678 z=rel[2];
679
680 #ifndef JUST_DO_EVERY_VERT
681 #ifndef DYNAMIC_GRID
682 x = VG_MIN(x,ax-2);
683 y = VG_MIN(y,ay-2);
684 z = VG_MIN(z,az-2);
685 x = VG_MAX(x,0);
686 y = VG_MAX(y,0);
687 z = VG_MAX(z,0);
688
689 float
690 s0 = samplegrid[ x + y*ax + z*ax*ay],
691 s1 = samplegrid[(x+1) + y*ax + z*ax*ay],
692 s2 = samplegrid[ x + (y+1)*ax + z*ax*ay],
693 s3 = samplegrid[(x+1) + (y+1)*ax + z*ax*ay],
694 s4 = samplegrid[ x + y*ax + (z+1)*ax*ay],
695 s5 = samplegrid[(x+1) + y*ax + (z+1)*ax*ay],
696 s6 = samplegrid[ x + (y+1)*ax + (z+1)*ax*ay],
697 s7 = samplegrid[(x+1) + (y+1)*ax + (z+1)*ax*ay],
698 #else
699 if( x!=cube_pos[0] || y!=cube_pos[1] || z!=cube_pos[2] )
700 {
701 cube_pos[0] = x;
702 cube_pos[1] = y;
703 cube_pos[2] = z;
704
705 s0 = scene_ambient_sample( pscene, (v3f){ x,y,z }, sundir );
706 s1 = scene_ambient_sample( pscene, (v3f){ x+1,y,z }, sundir );
707 s2 = scene_ambient_sample( pscene, (v3f){ x,y+1,z }, sundir );
708 s3 = scene_ambient_sample( pscene, (v3f){ x+1,y+1,z }, sundir );
709 s4 = scene_ambient_sample( pscene, (v3f){ x,y,z+1 }, sundir );
710 s5 = scene_ambient_sample( pscene, (v3f){ x+1,y,z+1 }, sundir );
711 s6 = scene_ambient_sample( pscene, (v3f){ x,y+1,z+1 }, sundir );
712 s7 = scene_ambient_sample( pscene, (v3f){ x+1,y+1,z+1 }, sundir );
713
714 cube_resamples += 8;
715 misses ++;
716 }
717 else
718 hits ++;
719
720 float
721 #endif
722
723 s0_s1 = vg_lerpf( s0, s1, q[0] ),
724 s2_s3 = vg_lerpf( s2, s3, q[0] ),
725 s4_s5 = vg_lerpf( s4, s5, q[0] ),
726 s6_s7 = vg_lerpf( s6, s7, q[0] ),
727
728 s0s1_s2s3 = vg_lerpf( s0_s1, s2_s3, q[1] ),
729 s4s5_s6s7 = vg_lerpf( s4_s5, s6_s7, q[1] ),
730 s0s1s2s3_s4s5s6s7 = vg_lerpf( s0s1_s2s3, s4s5_s6s7, q[2] );
731
732 vert->colour[1] = s0s1s2s3_s4s5s6s7;
733 #else
734 vert->colour[1] = scene_ambient_sample( pscene, vert->co, sundir );
735 #endif
736 }
737
738 #ifndef DYNAMIC_GRID
739 int cube_resamples = -1, misses = 0, hits = 0;
740 #endif
741
742 int static_samples = ax*ay*az,
743 vertex_samples = pscene->vertex_count;
744
745 if( cube_resamples < static_samples )
746 vg_success( "Walking cube beat static grid (%d<%d. %d)!\n",
747 cube_resamples, static_samples, vertex_samples );
748 else
749 vg_warn( "Walking cube was worse than static grid (%d<%d. %d).\n",
750 cube_resamples, static_samples, vertex_samples );
751
752 vg_info( "Hits; %d, misses: %d\n", hits, misses );
753
754 #ifndef DYNAMIC_GRID
755 free( samplegrid );
756 #endif
757
758 return;
759
760 for( int i=0; i<pscene->vertex_count; i++ )
761 {
762 model_vert *vert = &pscene->verts[i];
763 float accum = 0.0f;
764
765 for( int j=0; j<5; j++ )
766 {
767 v3f tracepos;
768 v3_copy( vert->co, tracepos );
769 v3_muladds( tracepos, sundir, 1.5f*(float)j, tracepos );
770
771 float mindist = 99999.9f;
772
773 for( int k=0; k<pscene->shadower_count; k++ )
774 {
775 struct shadower *shadower = &pscene->shadowers[k];
776 float dist = vg_maxf( 0.0f, sd_cone( tracepos, &shadower->sdf ));
777 mindist = vg_minf( mindist, dist );
778 }
779
780 accum += vg_clampf( 1.0f - mindist, 0.0f, 1.0f )*0.2f;
781 }
782
783 vert->colour[1] = vg_minf( accum, 1.0f );
784 }
785 }
786
787 static void scene_upload( scene *pscene )
788 {
789 mesh_upload( &pscene->mesh,
790 pscene->verts, pscene->vertex_count,
791 pscene->indices, pscene->indice_count );
792
793 vg_info( "Scene upload\n" );
794 vg_info( " indices:%u\n", pscene->indice_count );
795 vg_info( " verts:%u\n", pscene->vertex_count );
796 }
797
798 float scene_tree_sway = 0.1f;
799
800 static void scene_foliage_shader_use(void)
801 {
802 SHADER_USE( shader_debug_vcol );
803
804 glUniformMatrix4fv( SHADER_UNIFORM( shader_debug_vcol, "uPv" ),
805 1, GL_FALSE, (float *)vg_pv );
806
807 glUniform1i( SHADER_UNIFORM( shader_debug_vcol, "uMode" ), debugview );
808 glUniform1i( SHADER_UNIFORM( shader_debug_vcol, "uTexMain" ), 0 );
809
810 glUniform1i( SHADER_UNIFORM( shader_debug_vcol, "uTexGradients" ), 1 );
811 vg_tex2d_bind( &tex_gradients, 1 );
812
813 glUniform1i( SHADER_UNIFORM( shader_debug_vcol, "uTexNoise" ), 2 );
814 glActiveTexture( GL_TEXTURE2 );
815 glBindTexture( GL_TEXTURE_2D, tex_dual_noise );
816
817 glUniform1f( SHADER_UNIFORM( shader_debug_vcol, "uTime" ), vg_time );
818 glUniform1f( SHADER_UNIFORM( shader_debug_vcol, "uSwayAmt" ),
819 scene_tree_sway );
820 }
821
822 static void scene_bind( scene *pscene )
823 {
824 mesh_bind( &pscene->mesh );
825 }
826
827 static void scene_draw( scene *pscene )
828 {
829 mesh_drawn( 0, pscene->indice_count );
830 }
831
832 static void scene_debugsdf( scene *pscene )
833 {
834 for( int i=0; i<pscene->shadower_count; i++ )
835 {
836 struct shadower *shadower = &pscene->shadowers[i];
837
838 v3f base, side;
839 v3_copy( shadower->sdf.origin, base );
840 base[1] -= shadower->sdf.info[1];
841 v3_copy( base, side );
842 side[0] += shadower->sdf.info[0];
843
844 vg_line2( shadower->sdf.origin, base, 0xff00ff00, 0xff0000ff );
845 vg_line2( side, base, 0xff00ff00, 0xff0000ff );
846 vg_line( side, shadower->sdf.origin, 0xff00ff00 );
847 }
848
849 v3f p0 = { pscene->bbx[0][0], pscene->bbx[0][1], pscene->bbx[0][2] },
850 p1 = { pscene->bbx[0][0], pscene->bbx[1][1], pscene->bbx[0][2] },
851 p2 = { pscene->bbx[1][0], pscene->bbx[1][1], pscene->bbx[0][2] },
852 p3 = { pscene->bbx[1][0], pscene->bbx[0][1], pscene->bbx[0][2] },
853
854 p4 = { pscene->bbx[0][0], pscene->bbx[0][1], pscene->bbx[1][2] },
855 p5 = { pscene->bbx[0][0], pscene->bbx[1][1], pscene->bbx[1][2] },
856 p6 = { pscene->bbx[1][0], pscene->bbx[1][1], pscene->bbx[1][2] },
857 p7 = { pscene->bbx[1][0], pscene->bbx[0][1], pscene->bbx[1][2] };
858
859 u32 col = 0xffff00c8;
860 vg_line( p0, p1, col );
861 vg_line( p1, p2, col );
862 vg_line( p2, p3, col );
863 vg_line( p3, p0, col );
864
865 vg_line( p4, p5, col );
866 vg_line( p5, p6, col );
867 vg_line( p6, p7, col );
868 vg_line( p7, p4, col );
869
870 vg_line( p0, p4, col );
871 vg_line( p1, p5, col );
872 vg_line( p2, p6, col );
873 vg_line( p3, p7, col );
874 }
875
876 static void scene_register(void)
877 {
878 SHADER_INIT( shader_debug_vcol );
879 SHADER_INIT( shader_standard_lit );
880 SHADER_INIT( shader_unlit );
881 }