-typedef struct scene scene;
-typedef struct bvh_node bvh_node;
-
-struct scene
-{
- glmesh mesh;
-
- model_vert *verts;
- u32 *indices;
-
- struct
- {
- bvh_node *nodes;
- u32 node_count;
- }
- bvh;
-
- u32 vertex_count,
- indice_count,
- vertex_cap,
- indice_cap;
-
- boxf bbx;
-
- struct shadower
- {
- sdf_primative sdf;
- esdf_type sdf_type;
- }
- *shadowers;
-
- u32 shadower_count,
- shadower_cap;
-
- submodel submesh;
-};
-
-GLuint tex_dual_noise;
-
-static void scene_init( scene *pscene )
-{
- pscene->verts = NULL;
- pscene->indices = NULL;
- pscene->vertex_count = 0;
- pscene->indice_count = 0;
- pscene->shadowers = NULL;
- pscene->shadower_count = 0;
- pscene->shadower_cap = 0;
- pscene->submesh.indice_start = 0;
- pscene->submesh.indice_count = 0;
-
- v3_fill( pscene->bbx[0], 999999.9f );
- v3_fill( pscene->bbx[1], -999999.9f );
-
- static int noise_ready = 0;
- if( !noise_ready )
- {
- noise_ready = 1;
-
- u8 *buf = malloc( 256*256*2 );
-
- for( int i=0; i<256*256; i++ )
- {
- u8 val = rand()&0xff;
- buf[i*2] = val;
- }
-
- for( int y=0; y<256; y++ )
- {
- for( int x=0; x<256; x++ )
- {
- u8 *pr = &buf[(y*256+x)*2],
- *pg = &buf[(((y+17)&0xff)*256+((x+37)&0xff))*2+1];
- *pg = *pr;
- }
- }
-
- /* TODO: This texture should be delted somewhere */
- glGenTextures( 1, &tex_dual_noise );
- glBindTexture( GL_TEXTURE_2D, tex_dual_noise );
- glTexImage2D( GL_TEXTURE_2D, 0, GL_RG, 256, 256, 0, GL_RG,
- GL_UNSIGNED_BYTE, buf );
-
- vg_tex2d_linear();
- vg_tex2d_repeat();
-
- free( buf );
- }
-}
-
-/* https://www.shadertoy.com/view/4sfGzS */
-#define SHADER_VALUE_NOISE_3D \
-"uniform sampler2D uTexNoise;" \
-"" \
-"float noise( vec3 x )" \
-"{" \
- "vec3 i = floor(x);" \
- "vec3 f = fract(x);" \
- "f = f*f*(3.0-2.0*f);" \
- "vec2 uv = (i.xy+vec2(37.0,17.0)*i.z) + f.xy;" \
- "vec2 rg = texture( uTexNoise, (uv+0.5)/256.0).yx;"\
- "return mix( rg.x, rg.y, f.z );" \
-"}" \
-"" \
-"const mat3 m = mat3( 0.00, 0.80, 0.60," \
- "-0.80, 0.36, -0.48," \
- "-0.60, -0.48, 0.64 );" \
-"" \
-"float fractalNoise( vec3 x )" \
-"{" \
- "vec3 q = 8.0*x;" \
- "float f;" \
- "f = 0.5000*noise( q ); q = m*q*2.01;" \
- "f += 0.2500*noise( q ); q = m*q*2.02;" \
- "f += 0.1250*noise( q ); q = m*q*2.03;" \
- "f += 0.0625*noise( q ); q = m*q*2.01;" \
- "return f;" \
-"}"
-
-SHADER_DEFINE( shader_debug_vcol,
-
- /*Include*/ VERTEX_STANDARD_ATTRIBUTES
-
- "uniform mat4 uPv;"
- "uniform mat4x3 uMdl;"
- "uniform float uTime;"
- "uniform float uSwayAmt;"
- ""
- "out vec4 aColour;"
- "out vec2 aUv;"
- "out vec3 aNorm;"
- "out vec3 aCo;"
- ""
- "vec3 compute_sway( vec3 pos )"
- "{"
- "vec4 sines = vec4( sin(uTime + pos.x)*1.0,"
- "sin(uTime*1.2 + pos.z*2.0)*1.1,"
- "sin(uTime*2.33)*0.5,"
- "sin(uTime*0.6 + pos.x*0.3)*1.3 );"
-
- "vec3 offset = vec3( sines.x+sines.y*sines.w, 0.0, sines.x+sines.z );"
- "return pos + offset*a_colour.r*uSwayAmt;"
- "}"
- ""
- "void main()"
- "{"
- "vec3 swaypos = compute_sway( a_co );"
- "gl_Position = uPv * vec4(uMdl * vec4(swaypos,1.0), 1.0 );"
- "aColour = a_colour;"
- "aUv = a_uv;"
- "aNorm = normalize(mat3(uMdl) * a_norm);"
- "aCo = a_co;"
- "}",
- /* Fragment */
- "out vec4 FragColor;"
- ""
- "uniform int uMode;"
- "uniform sampler2D uTexMain;"
- "uniform sampler2D uTexGradients;"
- ""
- /*Include*/ SHADER_VALUE_NOISE_3D
- ""
- "in vec4 aColour;"
- "in vec2 aUv;"
- "in vec3 aNorm;"
- "in vec3 aCo;"
- ""
- "void main()"
- "{"
- "vec4 colour = vec4(1.0,0.0,0.5,1.0);"
- "vec4 diffuse = texture( uTexMain, aUv );"
-
- "if( uMode == 1 )"
- "{"
- "colour = vec4(aNorm * 0.5 + 0.5, 1.0);"
- "}"
- "if( uMode == 2 )"
- "{"
- "colour = aColour;"
- "}"
- "if( uMode == 3 )"
- "{"
- "float light = dot(aNorm, vec3(0.2,0.8,0.1));"
- "vec3 grid3 = fract(aCo);"
-
- "colour = vec4(vec3(light)*(1.0-grid3*0.3),1.0);"
- "}"
- "if( uMode == 4 )"
- "{"
- "colour = vec4( aUv, 0.0, 1.0 );"
- "}"
- "if( uMode == 5 )"
- "{"
- "if( diffuse.a < 0.45 ) discard;"
- "colour = diffuse;"
- "}"
- "if( uMode == 6 )"
- "{"
- "float r1 = fractalNoise(aCo);"
- "colour = vec4( vec3(r1), 1.0 );"
- "}"
- "if( uMode == 7 )"
- "{"
- "if( diffuse.a < 0.2 ) discard;"
- "float lighting = 1.0 - aColour.g*0.8;"
-
- "float light1 = max(0.0,dot(-vec3(0.5,-0.8,0.25), aNorm));"
- "float light2 = max(0.0,dot(-vec3(-0.8,0.5,-0.25), aNorm));"
- "vec3 lt = vec3(0.2,0.2,0.2 ) + "
- "vec3(1.0,1.0,0.9)*light1 + "
- "vec3(0.1,0.3,0.4 )*light2;"
-
-
- "colour = vec4(vec3(pow(lighting,1.6)*(diffuse.r*0.7+0.5)),1.0);"
- "colour = vec4(colour.rgb*lt,1.0);"
-
- "vec2 gradUV = vec2(lighting*1.9,aColour.b*0.8);"
- "vec4 gradient_sample = texture( uTexGradients, gradUV );"
- "colour = colour*gradient_sample;"
- "}"
- "if( uMode == 8 )"
- "{"
- "if( diffuse.a < 0.45 ) discard;"
- "float light = 1.0 - aColour.g;"
- "light = pow(light,1.6)*(diffuse.r*0.7+0.5);"
- "float r1 = fractalNoise(aCo*0.01);"
-
- "vec2 gradUV = vec2(light*1.9,r1+aColour.b);"
- "vec4 gradient_sample = texture( uTexGradients, gradUV );"
- "colour = gradient_sample*light;"
- "}"
-
- "FragColor = colour;"
- "}"
- ,
- UNIFORMS({ "uPv", "uMode", "uTexMain", "uTexGradients", "uTexNoise", \
- "uTime", "uSwayAmt", "uMdl" })
-)
-
-SHADER_DEFINE( shader_standard_lit,
-
- /*Include*/ VERTEX_STANDARD_ATTRIBUTES
-
- "uniform mat4 uPv;"
- "uniform mat4x3 uMdl;"
- ""
- "out vec4 aColour;"
- "out vec2 aUv;"
- "out vec3 aNorm;"
- "out vec3 aCo;"
- ""
- "void main()"
- "{"
- "gl_Position = uPv * vec4( uMdl * vec4(a_co,1.0), 1.0 );"
- "aColour = a_colour;"
- "aUv = a_uv;"
- "aNorm = mat3(uMdl) * a_norm;"
- "aCo = a_co;"
- "}",
- /* Fragment */
- "out vec4 FragColor;"
- ""
- "uniform sampler2D uTexMain;"
- "uniform vec4 uColour;"
- ""
- "in vec4 aColour;"
- "in vec2 aUv;"
- "in vec3 aNorm;"
- "in vec3 aCo;"
- ""
- "void main()"
- "{"
- "vec3 diffuse = texture( uTexMain, aUv ).rgb;"
-
- "float light1 = max(0.0,dot(-vec3(0.5,-0.8,0.25), aNorm));"
- "float light2 = max(0.0,dot(-vec3(-0.8,0.5,-0.25), aNorm));"
- "diffuse += vec3(0.2,0.2,0.2) + "
- "vec3(1.0,1.0,0.9)*light1 + "
- "vec3(0.1,0.3,0.4)*light2;"
-
- "FragColor = vec4(diffuse*uColour.rgb, aColour.a*uColour.a);"
- "}"
- ,
- UNIFORMS({ "uColour","uTexMain","uPv","uMdl" })
-)
-
-SHADER_DEFINE( shader_unlit,
-
- /*Include*/ VERTEX_STANDARD_ATTRIBUTES
-
- "uniform mat4 uPv;"
- "uniform mat4x3 uMdl;"
- ""
- "out vec4 aColour;"
- "out vec2 aUv;"
- "out vec3 aNorm;"
- "out vec3 aCo;"
- ""
- "void main()"
- "{"
- "gl_Position = uPv * vec4(uMdl * vec4(a_co,1.0), 1.0);"
- "aColour = a_colour;"
- "aUv = a_uv;"
- "aNorm = mat3(uMdl) * a_norm;"
- "aCo = a_co;"
- "}",
- /* Fragment */
- "out vec4 FragColor;"
- ""
- "uniform sampler2D uTexMain;"
- "uniform vec4 uColour;"
- ""
- "in vec4 aColour;"
- "in vec2 aUv;"
- "in vec3 aNorm;"
- "in vec3 aCo;"
- ""
- "void main()"
- "{"
- "vec3 diffuse = texture( uTexMain, aUv ).rgb;"
- "FragColor = vec4(pow(diffuse,vec3(1.0)),1.0);"
- "}"
- ,
- UNIFORMS({ "uTexMain", "uPv", "uMdl" })
-)
-
-static void *buffer_reserve( void *buffer, u32 count, u32 *cap, u32 amount,
- size_t emsize )
-{
- if( count+amount > *cap )
- {
- *cap = VG_MAX( (*cap)*2, (*cap)+amount );
-
- return realloc( buffer, (*cap) * emsize );
- }
-
- return buffer;
-}
-
-/*
- * Append a model into the scene with a given transform
- */
-static void scene_add_model( scene *pscene, model *mdl, submodel *submodel,
- v3f pos, float yaw, float scale )
-{
- pscene->verts = buffer_reserve( pscene->verts, pscene->vertex_count,
- &pscene->vertex_cap, submodel->vertex_count, sizeof(model_vert) );
- pscene->indices = buffer_reserve( pscene->indices, pscene->indice_count,
- &pscene->indice_cap, submodel->indice_count, sizeof(u32) );
-
- if( submodel->sdf_type )
- {
- pscene->shadowers = buffer_reserve( pscene->shadowers,
- pscene->shadower_count, &pscene->shadower_cap, 1,
- sizeof( struct shadower ));
-
- struct shadower *shadower =
- &pscene->shadowers[ pscene->shadower_count ++ ];
-
- shadower->sdf = submodel->sdf;
- shadower->sdf_type = submodel->sdf_type;
-
- v2_muls( shadower->sdf.info, scale, shadower->sdf.info );
- v3_muls( shadower->sdf.origin, scale, shadower->sdf.origin );
- v3_add( pos, shadower->sdf.origin, shadower->sdf.origin );
- }
-
- /* Transform and place vertices */
- model_vert *src_verts = submodel_vert_data( mdl, submodel );
- u32 *src_indices = submodel_indice_data( mdl, submodel );
-
- m4x3f mtx;
- m4x3_identity( mtx );
- m4x3_translate( mtx, pos );
- m4x3_rotate_y( mtx, yaw );
- m4x3_scale( mtx, scale );
-
- boxf bbxnew;
- box_copy( submodel->bbx, bbxnew );
- m4x3_transform_aabb( mtx, bbxnew );
- box_concat( pscene->bbx, bbxnew );
-
- m3x3f rotation;
- m4x3_to_3x3( mtx, rotation );
-
- float rand_hue = vg_randf();
-
- for( u32 i=0; i<submodel->vertex_count; i++ )
- {
- model_vert *pvert = &pscene->verts[ pscene->vertex_count+i ],
- *src = &src_verts[ i ];
-
- m4x3_mulv( mtx, src->co, pvert->co );
- m3x3_mulv( rotation, src->norm, pvert->norm );
-
- v4_copy( src->colour, pvert->colour );
- v2_copy( src->uv, pvert->uv );
-
- float rel_y = src->co[1] / submodel->bbx[1][1];
- pvert->colour[0] = rel_y;
- pvert->colour[2] = rand_hue;
- }
-
- for( u32 i=0; i<submodel->indice_count; i++ )
- {
- u32 *pidx = &pscene->indices[ pscene->indice_count+i ];
- *pidx = src_indices[i] + pscene->vertex_count;
- }
-
- pscene->vertex_count += submodel->vertex_count;
- pscene->indice_count += submodel->indice_count;
-}
-
-static void scene_copy_slice( scene *pscene, submodel *sm )
-{
- sm->indice_start = pscene->submesh.indice_start;
- sm->indice_count = pscene->indice_count - sm->indice_start;
-
- pscene->submesh.indice_start = pscene->indice_count;
-}
-
-static void scene_shadow_sphere( scene *pscene, v3f sphere,
- v4f params, v3f lightdir )
-{
- for( int i=0; i<pscene->vertex_count; i++ )
- {
- model_vert *vert = &pscene->verts[i];
-
- v3f delta;
- v3_sub( sphere, vert->co, delta );
-
- float d = v3_dot( lightdir, delta );
- v3f closest;