("layer_count",c_uint32),
("marker_count",c_uint32)]
-class sdf_primative(Structure):
- _pack_ = 1
- _fields_ = [("origin",c_float*4),
- ("info",c_float*4)]
-
class submodel(Structure):
_pack_ = 1
_fields_ = [("indice_start",c_uint32),
("vertex_count",c_uint32),
("bbx",(c_float*3)*2),
("pivot",c_float*3),
- ("sdf",sdf_primative),
- ("sdf_type",c_int32),
- ("name",c_char*32)]
+ ("q",c_float*4),
+ ("name",c_char*32),
+ ("material",c_char*32)]
class marker(Structure):
_pack_ = 1
def v4_length( a ):
return math.sqrt( v4_dot(a,a) )
+def v2_eq( a, b ):
+ if abs(a[0]-b[0]) < 0.0001:
+ if abs(a[1]-b[1]) < 0.0001:
+ return True
+ return False
+
+def v3_eq( a, b ):
+ if abs(a[0]-b[0]) < 0.0001:
+ if abs(a[1]-b[1]) < 0.0001:
+ if abs(a[2]-b[2]) < 0.0001:
+ return True
+ return False
+
+def v4_eq( a, b ):
+ if abs(a[0]-b[0]) < 0.0001:
+ if abs(a[1]-b[1]) < 0.0001:
+ if abs(a[2]-b[2]) < 0.0001:
+ if abs(a[3]-b[3]) < 0.0001:
+ return True
+ return False
+
def m3x3_mul( a, b, d ):
a00 = a[0][0]
a01 = a[0][1]
vertex_buffer = []
indice_buffer = []
+ print( F"Create mode {name}" )
+
for obj in collection.objects:
if obj.type == 'EMPTY':
mk = marker()
data.calc_loop_triangles()
data.calc_normals_split()
- sm = submodel()
- sm.indice_start = header.indice_count
- sm.vertex_start = header.vertex_count
- sm.vertex_count = len(data.vertices)
- sm.indice_count = len(data.loop_triangles)*3
- sm.sdf_type = 0
- sm.pivot[0] = obj.matrix_world.translation[0]
- sm.pivot[1] = obj.matrix_world.translation[2]
- sm.pivot[2] = -obj.matrix_world.translation[1]
-
- for i in range(3):
- sm.bbx[0][i] = 999999
- sm.bbx[1][i] = -999999
-
- if F"{obj.name}.sdf_cone" in bpy.data.objects:
- cone = bpy.data.objects[F"{obj.name}.sdf_cone"]
- sm.sdf.origin[0] = cone.location[0]
- sm.sdf.origin[1] = cone.location[2] + cone.scale[1]*2.0
- sm.sdf.origin[2] = -cone.location[1]
- sm.sdf.origin[3] = 0.0
-
- lo = cone.scale[0]
- la = cone.scale[1]*2.0
- lh = math.sqrt(lo*lo+la*la)
-
- sm.sdf.info[0] = lo
- sm.sdf.info[1] = la
- sm.sdf.info[2] = lo/lh
- sm.sdf.info[3] = la/lh
-
- sm.sdf_type = 1
-
- sm.name = obj.name.encode('utf-8')
-
- for vert in data.vertices:
- v = model_vert()
- v.co[0] = vert.co[0]
- v.co[1] = vert.co[2]
- v.co[2] = -vert.co[1]
- v.colour[0] = 1.0
- v.colour[1] = 1.0
- v.colour[2] = 1.0
- v.colour[3] = 1.0
- vertex_buffer += [v]
-
+ for material_id, mat in enumerate(data.materials):
+ sm = submodel()
+ sm.indice_start = header.indice_count
+ sm.vertex_start = header.vertex_count
+ sm.vertex_count = 0
+ sm.indice_count = 0
+ sm.pivot[0] = obj.matrix_world.translation[0]
+ sm.pivot[1] = obj.matrix_world.translation[2]
+ sm.pivot[2] = -obj.matrix_world.translation[1]
+
+ quat = obj.matrix_world.to_quaternion()
+ sm.q[0] = quat[1]
+ sm.q[1] = quat[3]
+ sm.q[2] = -quat[2]
+ sm.q[3] = quat[0]
+
for i in range(3):
- sm.bbx[0][i] = min( sm.bbx[0][i], v.co[i] )
- sm.bbx[1][i] = max( sm.bbx[1][i], v.co[i] )
-
- for l in data.loops:
- pvert = vertex_buffer[l.vertex_index + sm.vertex_start]
- norm = l.normal
- pvert.norm[0] = norm[0]
- pvert.norm[1] = norm[2]
- pvert.norm[2] = -norm[1]
-
- #if data.vertex_colors:
- # colour = data.vertex_colors.active.data[ l.index ].color
- # pvert.colour[0] = colour[0]
-
- if data.uv_layers:
- uv = data.uv_layers.active.data[ l.index ].uv
- pvert.uv[0] = uv[0]
- pvert.uv[1] = uv[1]
-
- for tri in data.loop_triangles:
- indice_buffer += [c_uint32(tri.vertices[_]) for _ in range(3)]
-
- layers += [sm]
- header.layer_count += 1
- header.vertex_count += sm.vertex_count
- header.indice_count += sm.indice_count
+ sm.bbx[0][i] = 999999
+ sm.bbx[1][i] = -999999
+
+ sm.name = obj.name.encode('utf-8')
+ sm.material = mat.name.encode('utf-8')
+ print( F" Creating submesh '{obj.name}:{mat.name}'" )
+ boffa = {}
+
+ hit_count = 0
+ miss_count = 0
+
+ # Write the vertex / indice data
+ #
+ for tri_index, tri in enumerate(data.loop_triangles):
+ if tri.material_index != material_id:
+ continue
+
+ for j in range(3):
+ vert = data.vertices[tri.vertices[j]]
+
+ co = vert.co
+ norm = data.loops[tri.loops[j]].normal
+ uv = (0,0)
+ if data.uv_layers:
+ uv = data.uv_layers.active.data[tri.loops[j]].uv
+
+ key = (round(co[0],4),round(co[1],4),round(co[2],4),\
+ round(norm[0],4),round(norm[1],4),round(norm[2],4),\
+ round(uv[0],4),round(uv[1],4))
+
+ if key in boffa:
+ indice_buffer += [boffa[key]]
+ hit_count += 1
+ else:
+ miss_count += 1
+ index = c_uint32(sm.vertex_count)
+ sm.vertex_count += 1
+
+ boffa[key] = index
+
+ indice_buffer += [index]
+
+ v = model_vert()
+ v.co[0] = co[0]
+ v.co[1] = co[2]
+ v.co[2] = -co[1]
+ v.norm[0] = norm[0]
+ v.norm[1] = norm[2]
+ v.norm[2] = -norm[1]
+ v.uv[0] = uv[0]
+ v.uv[1] = uv[1]
+ v.colour[0] = 1.0
+ v.colour[1] = 1.0
+ v.colour[2] = 1.0
+ v.colour[3] = 1.0
+ vertex_buffer += [v]
+
+ for i in range(3):
+ sm.bbx[0][i] = min( sm.bbx[0][i], v.co[i] )
+ sm.bbx[1][i] = max( sm.bbx[1][i], v.co[i] )
+
+ sm.indice_count += 1
+
+ layers += [sm]
+ header.layer_count += 1
+ header.vertex_count += sm.vertex_count
+ header.indice_count += sm.indice_count
fp.write( bytearray( header ) )
for l in layers:
boxf bbx;
- struct shadower
- {
- sdf_primative sdf;
- esdf_type sdf_type;
- }
- *shadowers;
-
u32 shadower_count,
shadower_cap;
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->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 );
pscene->submesh.vertex_start = pscene->vertex_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;
-
- v3_muls( lightdir, d, closest );
- float dist = v3_dist( closest, delta ),
- shading = vg_maxf( dist - params[0], 0.0f );
-
- shading = vg_minf( shading * params[1], 1.0f );
- vert->colour[1] *= shading;
- }
-}
-
-static void scene_shadow_gradient( scene *pscene, int comp,
- float start, float length )
-{
- float scale = 1.0f / length;
-
- for( int i=0; i<pscene->vertex_count; i++ )
- {
- model_vert *vert = &pscene->verts[i];
- float shading = start + vert->co[comp] * scale;
-
- vert->colour[1] = shading;
- }
-}
-
-
-/*
- * Experimental SDF based shadows
- *
- * https://iquilezles.org/articles/distfunctions/
- */
-static float sd_cone( v3f co, sdf_primative *prim )
-{
- float bound = prim->info[1]*1.75f;
- if( v3_dist2( prim->origin, co ) > bound*bound )
- return 999999.9f;
-
- v3f p;
- v3_sub( co, prim->origin, p );
-
- float h = prim->info[1];
- v2f c = { prim->info[2], prim->info[3] };
-
- v2f q, w, a, b;
- v2_muls( (v2f){ c[0]/c[1], -1.0f }, h, q );
-
- w[0] = v2_length( (v2f){ p[0], p[2] } );
- w[1] = p[1];
-
- v2_muladds( w, q, -vg_clampf( v2_dot(w,q)/v2_dot(q,q), 0.0f, 1.0f ), a );
- v2_muladd( w, q, (v2f){ vg_clampf( w[0]/q[0], 0.0f, 1.0f ), 1.0f }, b );
-
- float k = vg_signf( q[1] ),
- d = vg_minf( v2_dot( a,a ), v2_dot( b,b ) ),
- s = vg_maxf( k*(w[0]*q[1]-w[1]*q[0]), k*(w[1]-q[1]) );
-
- return sqrtf(d)*vg_signf(s);
-}
-
-#define CACHE_AMBIENT_SHAPES
-
-static float scene_ambient_sample( scene *pscene, v3f pos, v3f dir )
-{
- float accum = 0.0f;
-
-#ifdef CACHE_AMBIENT_SHAPES
- static struct shadower *local_shadowers[32];
- static int local_shadower_count = 0;
- static v3f local_shadower_last = { -99999.9f, -999999.9f, -9999999.9f };
-
- if( v3_dist2( pos, local_shadower_last ) > 10.0f*10.0f )
- {
- local_shadower_count = 0;
- v3_copy( pos, local_shadower_last );
-
- for( int k=0; k<pscene->shadower_count; k++ )
- {
- struct shadower *shadower = &pscene->shadowers[k];
-
- if( sd_cone( pos, &shadower->sdf ) <= 20.0f )
- {
- local_shadowers[ local_shadower_count ++ ] = shadower;
- if( local_shadower_count == vg_list_size( local_shadowers ) )
- break;
- }
- }
- }
-#endif
-
- for( int j=0; j<5; j++ )
- {
- v3f tracepos;
- v3_muladds( pos, dir, 1.5f*(float)j, tracepos );
-
- float mindist = 99999.9f;
-
-#ifndef CACHE_AMBIENT_SHAPES
-
- for( int k=0; k<pscene->shadower_count; k++ ){
- struct shadower *shadower = &pscene->shadowers[k];
-#else
-
- for( int k=0; k<local_shadower_count; k++ ){
- struct shadower *shadower = local_shadowers[k];
-#endif
-
- float dist = vg_maxf( 0.0f, sd_cone( tracepos, &shadower->sdf ));
- mindist = vg_minf( mindist, dist );
- }
-
-
- accum += vg_clampf( 1.0f - mindist, 0.0f, 1.0f )*0.2f;
- }
-
- return accum;
-}
-
-#define DYNAMIC_GRID
-#define JUST_DO_EVERY_VERT
-
-static void scene_compute_occlusion( scene *pscene )
-{
- v3f sundir = { 0.2f, 0.9f, 0.2f };
- v3_normalize( sundir );
-
- /* TODO: Make this sample grid be dynamically required.
- *
- * 1. Only resample the light grid (1x1x1), when a vertex is outside the
- * current cube
- *
- * 2. Reorder all vertices so that each group of vertices that fit in a
- * cube are next to eachother in the buffer. This will save cache
- * misses.
- *
- * for the sorting algorithm, i think we can already assume that *most
- * vertices will be quite close to eachother. so instead of doing an
- * exhaustive search we can reorder 1k chunks at a time.
- */
-
- v3f sample_area;
- v3_sub( pscene->bbx[1], pscene->bbx[0], sample_area );
- v3_ceil( sample_area, sample_area );
- int ax = sample_area[0],
- ay = sample_area[1],
- az = sample_area[2];
-
-#ifndef DYNAMIC_GRID
- float *samplegrid = malloc( ax*ay*az* sizeof(float) );
-
- for( int x=0; x<ax; x++ ){
- for( int y=0; y<ay; y++ ){
- for( int z=0; z<az; z++ )
- {
- v3f sample_pos = { x,y,z };
- v3_add( pscene->bbx[0], sample_pos, sample_pos );
- float accum = scene_ambient_sample( pscene, sample_pos, sundir );
-
- samplegrid[x + y*ax + z*ax*ay] = accum;
- }}}
-#else
- v3i cube_pos = { -999999, -999999, -999999 };
- int cube_resamples = 0, hits = 0, misses = 0;
-
- 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;
-#endif
-
- for( int i=0; i<pscene->vertex_count; i++ )
- {
- model_vert *vert = &pscene->verts[i];
- v3f rel, q;
-
-#ifndef DYNAMIC_GRID
- v3_sub( vert->co, pscene->bbx[0], q );
-#else
- v3_copy( vert->co, q );
-#endif
-
- v3_floor( q, rel );
- v3_sub( q, rel, q );
-
- int x=rel[0],
- y=rel[1],
- z=rel[2];
-
-#ifndef JUST_DO_EVERY_VERT
-#ifndef DYNAMIC_GRID
- x = VG_MIN(x,ax-2);
- y = VG_MIN(y,ay-2);
- z = VG_MIN(z,az-2);
- x = VG_MAX(x,0);
- y = VG_MAX(y,0);
- z = VG_MAX(z,0);
-
- float
- s0 = samplegrid[ x + y*ax + z*ax*ay],
- s1 = samplegrid[(x+1) + y*ax + z*ax*ay],
- s2 = samplegrid[ x + (y+1)*ax + z*ax*ay],
- s3 = samplegrid[(x+1) + (y+1)*ax + z*ax*ay],
- s4 = samplegrid[ x + y*ax + (z+1)*ax*ay],
- s5 = samplegrid[(x+1) + y*ax + (z+1)*ax*ay],
- s6 = samplegrid[ x + (y+1)*ax + (z+1)*ax*ay],
- s7 = samplegrid[(x+1) + (y+1)*ax + (z+1)*ax*ay],
-#else
- if( x!=cube_pos[0] || y!=cube_pos[1] || z!=cube_pos[2] )
- {
- cube_pos[0] = x;
- cube_pos[1] = y;
- cube_pos[2] = z;
-
- s0 = scene_ambient_sample( pscene, (v3f){ x,y,z }, sundir );
- s1 = scene_ambient_sample( pscene, (v3f){ x+1,y,z }, sundir );
- s2 = scene_ambient_sample( pscene, (v3f){ x,y+1,z }, sundir );
- s3 = scene_ambient_sample( pscene, (v3f){ x+1,y+1,z }, sundir );
- s4 = scene_ambient_sample( pscene, (v3f){ x,y,z+1 }, sundir );
- s5 = scene_ambient_sample( pscene, (v3f){ x+1,y,z+1 }, sundir );
- s6 = scene_ambient_sample( pscene, (v3f){ x,y+1,z+1 }, sundir );
- s7 = scene_ambient_sample( pscene, (v3f){ x+1,y+1,z+1 }, sundir );
-
- cube_resamples += 8;
- misses ++;
- }
- else
- hits ++;
-
- float
-#endif
-
- s0_s1 = vg_lerpf( s0, s1, q[0] ),
- s2_s3 = vg_lerpf( s2, s3, q[0] ),
- s4_s5 = vg_lerpf( s4, s5, q[0] ),
- s6_s7 = vg_lerpf( s6, s7, q[0] ),
-
- s0s1_s2s3 = vg_lerpf( s0_s1, s2_s3, q[1] ),
- s4s5_s6s7 = vg_lerpf( s4_s5, s6_s7, q[1] ),
- s0s1s2s3_s4s5s6s7 = vg_lerpf( s0s1_s2s3, s4s5_s6s7, q[2] );
-
- vert->colour[1] = s0s1s2s3_s4s5s6s7;
-#else
- vert->colour[1] = scene_ambient_sample( pscene, vert->co, sundir );
-#endif
- }
-
-#ifndef DYNAMIC_GRID
- int cube_resamples = -1, misses = 0, hits = 0;
-#endif
-
- int static_samples = ax*ay*az,
- vertex_samples = pscene->vertex_count;
-
- if( cube_resamples < static_samples )
- vg_success( "Walking cube beat static grid (%d<%d. %d)!\n",
- cube_resamples, static_samples, vertex_samples );
- else
- vg_warn( "Walking cube was worse than static grid (%d<%d. %d).\n",
- cube_resamples, static_samples, vertex_samples );
-
- vg_info( "Hits; %d, misses: %d\n", hits, misses );
-
-#ifndef DYNAMIC_GRID
- free( samplegrid );
-#endif
-
- return;
-
- for( int i=0; i<pscene->vertex_count; i++ )
- {
- model_vert *vert = &pscene->verts[i];
- float accum = 0.0f;
-
- for( int j=0; j<5; j++ )
- {
- v3f tracepos;
- v3_copy( vert->co, tracepos );
- v3_muladds( tracepos, sundir, 1.5f*(float)j, tracepos );
-
- float mindist = 99999.9f;
-
- for( int k=0; k<pscene->shadower_count; k++ )
- {
- struct shadower *shadower = &pscene->shadowers[k];
- float dist = vg_maxf( 0.0f, sd_cone( tracepos, &shadower->sdf ));
- mindist = vg_minf( mindist, dist );
- }
-
- accum += vg_clampf( 1.0f - mindist, 0.0f, 1.0f )*0.2f;
- }
-
- vert->colour[1] = vg_minf( accum, 1.0f );
- }
-}
-
static void scene_upload( scene *pscene )
{
mesh_upload( &pscene->mesh,
mesh_drawn( 0, pscene->indice_count );
}
-static void scene_debugsdf( scene *pscene )
-{
- for( int i=0; i<pscene->shadower_count; i++ )
- {
- struct shadower *shadower = &pscene->shadowers[i];
-
- v3f base, side;
- v3_copy( shadower->sdf.origin, base );
- base[1] -= shadower->sdf.info[1];
- v3_copy( base, side );
- side[0] += shadower->sdf.info[0];
-
- vg_line2( shadower->sdf.origin, base, 0xff00ff00, 0xff0000ff );
- vg_line2( side, base, 0xff00ff00, 0xff0000ff );
- vg_line( side, shadower->sdf.origin, 0xff00ff00 );
- }
-
- v3f p0 = { pscene->bbx[0][0], pscene->bbx[0][1], pscene->bbx[0][2] },
- p1 = { pscene->bbx[0][0], pscene->bbx[1][1], pscene->bbx[0][2] },
- p2 = { pscene->bbx[1][0], pscene->bbx[1][1], pscene->bbx[0][2] },
- p3 = { pscene->bbx[1][0], pscene->bbx[0][1], pscene->bbx[0][2] },
-
- p4 = { pscene->bbx[0][0], pscene->bbx[0][1], pscene->bbx[1][2] },
- p5 = { pscene->bbx[0][0], pscene->bbx[1][1], pscene->bbx[1][2] },
- p6 = { pscene->bbx[1][0], pscene->bbx[1][1], pscene->bbx[1][2] },
- p7 = { pscene->bbx[1][0], pscene->bbx[0][1], pscene->bbx[1][2] };
-
- u32 col = 0xffff00c8;
- vg_line( p0, p1, col );
- vg_line( p1, p2, col );
- vg_line( p2, p3, col );
- vg_line( p3, p0, col );
-
- vg_line( p4, p5, col );
- vg_line( p5, p6, col );
- vg_line( p6, p7, col );
- vg_line( p7, p4, col );
-
- vg_line( p0, p4, col );
- vg_line( p1, p5, col );
- vg_line( p2, p6, col );
- vg_line( p3, p7, col );
-}
-
static void scene_register(void)
{
}