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