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