+ int upper_count = VG_MAX( 0, count - lower_count );
+ u32 packed_index_upper = upper_count;
+ packed_index_upper |= indices[3]<<2;
+ packed_index_upper |= indices[4]<<12;
+ packed_index_upper |= indices[5]<<22;
+
+ cubes_index[ base_index*2 + 0 ] = packed_index_lower;
+ cubes_index[ base_index*2 + 1 ] = packed_index_upper;
+ }
+ }
+ }
+
+ vg_acquire_thread_sync();
+
+ glGenTextures( 1, &world->tex_light_cubes );
+ glBindTexture( GL_TEXTURE_3D, world->tex_light_cubes );
+ glTexImage3D( GL_TEXTURE_3D, 0, GL_RG32UI,
+ icubes_count[0], icubes_count[1], icubes_count[2],
+ 0, GL_RG_INTEGER, GL_UNSIGNED_INT, cubes_index );
+ glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
+ glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
+
+ vg_linear_del( world_global.generic_heap, cubes_index );
+
+ vg_release_thread_sync();
+}
+
+VG_STATIC int reset_player( int argc, char const *argv[] );
+VG_STATIC void world_post_process( world_instance *world )
+{
+ /* initialize audio if need be */
+#if 0
+ audio_lock();
+ for( int i=0; i<world->audio_things_count; i++ )
+ {
+ struct world_audio_thing *thingy = &world->audio_things[ i ];
+
+ if( thingy->flags & AUDIO_FLAG_AUTO_START )
+ {
+ audio_channel *ch =
+ audio_request_channel( &thingy->temp_embedded_clip, thingy->flags );
+
+ audio_channel_edit_volume( ch, thingy->volume, 1 );
+ audio_channel_set_spacial( ch, thingy->pos, thingy->range );
+
+ if( !(ch->flags & AUDIO_FLAG_LOOP) )
+ ch = audio_relinquish_channel( ch );
+ }
+ }
+ audio_unlock();
+#endif
+
+ world_compute_light_indices( world );
+
+ vg_acquire_thread_sync();
+ {
+ /* create scene lighting buffer */
+
+ u32 size = VG_MAX(mdl_arrcount(&world->ent_light),1) * sizeof(float)*12;
+ vg_info( "Upload %ubytes (lighting)\n", size );
+
+ glGenBuffers( 1, &world->tbo_light_entities );
+ glBindBuffer( GL_TEXTURE_BUFFER, world->tbo_light_entities );
+ glBufferData( GL_TEXTURE_BUFFER, size, NULL, GL_DYNAMIC_DRAW );
+
+ /* buffer layout
+ *
+ * colour position direction (spots)
+ * | . . . . | . . . . | . . . . |
+ * | Re Ge Be Night | Xco Yco Zco Range | Dx Dy Dz Da |
+ *
+ */
+
+ v4f *light_dst = glMapBuffer( GL_TEXTURE_BUFFER, GL_WRITE_ONLY );
+ for( u32 i=0; i<mdl_arrcount(&world->ent_light); i++ ){
+ ent_light *light = mdl_arritm( &world->ent_light, i );
+
+ /* colour + night */
+ v3_muls( light->colour, light->colour[3] * 2.0f, light_dst[i*3+0] );
+ light_dst[i*3+0][3] = -1.0f;
+
+ if( !light->daytime ){
+ u32 hash = (i * 29986577) & 0xff;
+ float switch_on = hash;
+ switch_on *= (1.0f/255.0f);
+
+ light_dst[i*3+0][3] = 0.44f + switch_on * 0.015f;
+ }
+
+ /* position + 1/range^2 */
+ v3_copy( light->transform.co, light_dst[i*3+1] );
+ light_dst[i*3+1][3] = 1.0f/(light->range*light->range);
+
+ /* direction + angle */
+ q_mulv( light->transform.q, (v3f){0.0f,-1.0f,0.0f}, light_dst[i*3+2]);
+ light_dst[i*3+2][3] = cosf( light->angle );
+ }
+
+ glUnmapBuffer( GL_TEXTURE_BUFFER );
+
+ glGenTextures( 1, &world->tex_light_entities );
+ glBindTexture( GL_TEXTURE_BUFFER, world->tex_light_entities );
+ glTexBuffer( GL_TEXTURE_BUFFER, GL_RGBA32F, world->tbo_light_entities );
+
+ /* Upload lighting uniform buffer */
+ if( world->water.enabled )
+ v4_copy( world->water.plane, world->ub_lighting.g_water_plane );
+
+ v4f info_vec;
+ v3f *bounds = world->scene_geo->bbx;
+
+ info_vec[0] = bounds[0][0];
+ info_vec[1] = bounds[0][2];
+ info_vec[2] = 1.0f/ (bounds[1][0]-bounds[0][0]);
+ info_vec[3] = 1.0f/ (bounds[1][2]-bounds[0][2]);
+ v4_copy( info_vec, world->ub_lighting.g_depth_bounds );
+
+ /*
+ * Rendering the depth map
+ */
+ camera ortho;
+
+ v3f extent;
+ v3_sub( world->scene_geo->bbx[1], world->scene_geo->bbx[0], extent );
+
+ float fl = world->scene_geo->bbx[0][0],
+ fr = world->scene_geo->bbx[1][0],
+ fb = world->scene_geo->bbx[0][2],
+ ft = world->scene_geo->bbx[1][2],
+ rl = 1.0f / (fr-fl),
+ tb = 1.0f / (ft-fb);
+
+ m4x4_zero( ortho.mtx.p );
+ ortho.mtx.p[0][0] = 2.0f * rl;
+ ortho.mtx.p[2][1] = 2.0f * tb;
+ ortho.mtx.p[3][0] = (fr + fl) * -rl;
+ ortho.mtx.p[3][1] = (ft + fb) * -tb;
+ ortho.mtx.p[3][3] = 1.0f;
+ m4x3_identity( ortho.transform );
+ camera_update_view( &ortho );
+ camera_finalize( &ortho );
+
+ glDisable(GL_DEPTH_TEST);
+ glDisable(GL_BLEND);
+ glDisable(GL_CULL_FACE);
+ render_fb_bind( &world->heightmap );
+ shader_blitcolour_use();
+ shader_blitcolour_uColour( (v4f){-9999.0f,-9999.0f,-9999.0f,-9999.0f} );
+ render_fsquad();
+
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_ONE, GL_ONE);
+ glBlendEquation(GL_MAX);
+
+ render_world_position( world, &ortho );
+ glDisable(GL_BLEND);
+ glEnable(GL_DEPTH_TEST);
+ glBindFramebuffer( GL_FRAMEBUFFER, 0 );
+
+ /* upload full buffer */
+ glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
+ glBufferSubData( GL_UNIFORM_BUFFER, 0,
+ sizeof(struct ub_world_lighting), &world->ub_lighting );
+ }
+
+ vg_release_thread_sync();
+
+#if 0