6 * If we have multiple levels, write an unloader
11 static void world_add_all_if_material( m4x3f transform
, scene
*pscene
,
12 mdl_header
*mdl
, u32 id
)
14 for( int i
=0; i
<mdl
->node_count
; i
++ )
16 mdl_node
*pnode
= mdl_node_from_id( mdl
, i
);
18 for( int j
=0; j
<pnode
->submesh_count
; j
++ )
20 mdl_submesh
*sm
= mdl_node_submesh( mdl
, pnode
, j
);
22 if( sm
->material_id
== id
)
25 mdl_node_transform( pnode
, transform2
);
26 m4x3_mul( transform
, transform2
, transform2
);
28 scene_add_submesh( pscene
, mdl
, sm
, transform2
);
32 if( pnode
->classtype
== k_classtype_instance
)
36 u32 instance_id
= pnode
->sub_uid
-1;
37 struct instance_cache
*cache
= &world
.instance_cache
[instance_id
];
38 mdl_header
*mdl2
= cache
->mdl
;
41 mdl_node_transform( pnode
, transform2
);
42 m4x3_mul( transform
, transform2
, transform2
);
44 world_add_all_if_material( transform2
, pscene
, mdl2
, id
);
50 static void world_apply_procedural_foliage(void)
52 mdl_header
*mfoliage
= mdl_load("models/rs_foliage.mdl");
55 v3_sub( world
.geo
.bbx
[1], world
.geo
.bbx
[0], volume
);
59 mdl_node
*mblob
= mdl_node_from_name( mfoliage
, "blob" );
60 mdl_submesh
*sm_blob
= mdl_node_submesh( mfoliage
, mblob
, 0 );
62 for( int i
=0;i
<100000;i
++ )
65 v3_mul( volume
, (v3f
){ vg_randf(), 1000.0f
, vg_randf() }, pos
);
67 v3_add( pos
, world
.geo
.bbx
[0], pos
);
72 if( ray_world( pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
74 if( (hit
.normal
[1] > 0.8f
) && ray_hit_is_terrain(&hit
) &&
75 (hit
.pos
[1] > 0.0f
+10.0f
) )
77 v4f qsurface
, qrandom
;
80 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, hit
.normal
, axis
);
82 float angle
= v3_dot(hit
.normal
,(v3f
){0.0f
,1.0f
,0.0f
});
83 q_axis_angle( qsurface
, axis
, angle
);
84 q_axis_angle( qrandom
, (v3f
){0.0f
,1.0f
,0.0f
}, vg_randf()*VG_TAUf
);
85 q_mul( qsurface
, qrandom
, qsurface
);
86 q_m3x3( qsurface
, transform
);
88 v3_copy( hit
.pos
, transform
[3] );
89 scene_add_submesh( &world
.foliage
, mfoliage
, sm_blob
, transform
);
97 static void world_load(void)
99 mdl_header
*mworld
= mdl_load( "models/mp_dev.mdl" );
100 vg_info( "Loading world: models/mp_dev.mdl\n" );
102 world
.spawn_count
= 0;
103 world
.traffic_count
= 0;
104 world
.instance_cache
= NULL
;
109 for( int i
=0; i
<mworld
->node_count
; i
++ )
111 mdl_node
*pnode
= mdl_node_from_id( mworld
, i
);
113 if( pnode
->classtype
== k_classtype_none
)
115 else if( pnode
->classtype
== k_classtype_spawn
)
117 struct respawn_point
*rp
= &world
.spawns
[ world
.spawn_count
++ ];
119 v3_copy( pnode
->co
, rp
->co
);
120 v4_copy( pnode
->q
, rp
->q
);
121 strcpy( rp
->name
, mdl_pstr( mworld
, pnode
->pstr_name
) );
123 else if( pnode
->classtype
== k_classtype_water
)
125 if( wrender
.enabled
)
127 vg_warn( "Multiple water surfaces in level! ('%s')\n",
128 mdl_pstr( mworld
, pnode
->pstr_name
));
132 mdl_submesh
*sm
= mdl_node_submesh( mworld
, pnode
, 0 );
136 vg_acquire_thread_sync();
139 mdl_unpack_submesh( mworld
, &surf
, sm
);
140 water_set_surface( &surf
, pnode
->co
[1] );
142 vg_release_thread_sync();
145 else if( pnode
->classtype
== k_classtype_car_path
)
147 struct classtype_car_path
*p
= mdl_get_entdata( mworld
, pnode
);
148 traffic_node
*tn
= &world
.traffic
[ world
.traffic_count
];
152 if( p
->target
) tn
->mn_next
= mdl_node_from_id( mworld
, p
->target
);
153 if( p
->target1
) tn
->mn_next1
= mdl_node_from_id( mworld
, p
->target1
);
156 mdl_node_transform( pnode
, transform
);
157 m3x3_mulv( transform
, (v3f
){1.0f
,0.0f
,0.0f
}, tn
->h
);
158 v3_copy( transform
[3], tn
->co
);
160 pnode
->sub_uid
= world
.traffic_count
++;
162 else if( pnode
->classtype
== k_classtype_instance
)
164 struct classtype_instance
*inst
= mdl_get_entdata( mworld
, pnode
);
168 for( int i
=0; i
<world
.instance_cache_count
; i
++ )
170 struct instance_cache
*cache
= &world
.instance_cache
[i
];
171 if( inst
->pstr_file
== cache
->pstr_file
)
174 pnode
->sub_uid
= i
+1;
181 world
.instance_cache
= buffer_reserve(
182 world
.instance_cache
, world
.instance_cache_count
,
183 &world
.instance_cache_cap
, 1,
184 sizeof(struct instance_cache
) );
186 struct instance_cache
*cache
=
187 &world
.instance_cache
[world
.instance_cache_count
];
189 const char *filename
= mdl_pstr(mworld
, inst
->pstr_file
);
191 cache
->pstr_file
= inst
->pstr_file
;
192 cache
->mdl
= mdl_load( filename
);
196 world
.instance_cache_count
++;
197 pnode
->sub_uid
= world
.instance_cache_count
;
198 mdl_link_materials( mworld
, cache
->mdl
);
199 vg_success( "Cached %s\n", filename
);
203 vg_warn( "Failed to cache %s\n", filename
);
209 world
.instance_cache
= buffer_fix( world
.instance_cache
,
210 world
.instance_cache_count
,
211 &world
.instance_cache_cap
,
212 sizeof( struct instance_cache
) );
215 traffic_finalize( world
.traffic
, world
.traffic_count
);
216 for( int i
=0; i
<vg_list_size(world
.van_man
); i
++ )
217 world
.van_man
[i
].current
=&world
.traffic
[vg_randint(world
.traffic_count
)];
221 * Compile meshes into the world scenes
223 scene_init( &world
.geo
);
227 mat_vertex_blend
= 0,
233 for( int i
=1; i
<mworld
->material_count
; i
++ )
235 mdl_material
*mat
= mdl_material_from_id( mworld
, i
);
236 const char *mat_name
= mdl_pstr( mworld
, mat
->pstr_name
);
238 if( !strcmp( "surf", mat_name
))
240 else if( !strcmp( "surf_oob", mat_name
))
242 else if( !strcmp( "vertex_blend", mat_name
))
243 mat_vertex_blend
= i
;
244 else if( !strcmp( "alphatest", mat_name
))
246 else if( !strcmp( "graffitibox", mat_name
))
248 else if( !strcmp( "terrain", mat_name
) )
253 m4x3_identity( midentity
);
256 world_add_all_if_material( midentity
, &world
.geo
, mworld
, mat_terrain
);
257 scene_copy_slice( &world
.geo
, &world
.sm_terrain
);
260 world_add_all_if_material( midentity
, &world
.geo
, mworld
, mat_surf_oob
);
262 vg_warn( "No OOB surface\n" );
263 scene_copy_slice( &world
.geo
, &world
.sm_geo_std_oob
);
266 world_add_all_if_material( midentity
, &world
.geo
, mworld
, mat_surf
);
267 scene_copy_slice( &world
.geo
, &world
.sm_geo_std
);
269 if( mat_vertex_blend
)
270 world_add_all_if_material( midentity
, &world
.geo
,mworld
,mat_vertex_blend
);
271 scene_copy_slice( &world
.geo
, &world
.sm_geo_vb
);
273 vg_acquire_thread_sync();
274 scene_upload( &world
.geo
);
275 vg_release_thread_sync();
277 scene_bh_create( &world
.geo
);
280 /* Foliage /nocollide layer.
281 * TODO: Probably should have material traits for this
283 scene_init( &world
.foliage
);
285 world_apply_procedural_foliage();
286 scene_copy_slice( &world
.foliage
, &world
.sm_foliage_main
);
288 world_add_all_if_material( midentity
, &world
.foliage
, mworld
, mat_alphatest
);
289 scene_copy_slice( &world
.foliage
, &world
.sm_foliage_alphatest
);
291 world_add_all_if_material( midentity
, &world
.foliage
, mworld
, mat_graffiti
);
292 scene_copy_slice( &world
.foliage
, &world
.sm_graffiti
);
295 vg_acquire_thread_sync();
297 scene_upload( &world
.foliage
);
300 * Rendering the depth map
306 v3_sub( world
.geo
.bbx
[1], world
.geo
.bbx
[0], extent
);
308 float fl
= world
.geo
.bbx
[0][0],
309 fr
= world
.geo
.bbx
[1][0],
310 fb
= world
.geo
.bbx
[0][2],
311 ft
= world
.geo
.bbx
[1][2],
316 ortho
[0][0] = 2.0f
* rl
;
317 ortho
[2][1] = 2.0f
* tb
;
318 ortho
[3][0] = (fr
+ fl
) * -rl
;
319 ortho
[3][1] = (ft
+ fb
) * -tb
;
321 m4x3_identity( camera
);
323 glViewport( 0, 0, 1024, 1024 );
324 glDisable(GL_DEPTH_TEST
);
325 glBindFramebuffer( GL_FRAMEBUFFER
, gpipeline
.fb_depthmap
);
326 shader_fscolour_use();
327 shader_fscolour_uColour( (v4f
){-9999.0f
,-9999.0f
,-9999.0f
,-9999.0f
} );
331 glBlendFunc(GL_ONE
, GL_ONE
);
332 glBlendEquation(GL_MAX
);
333 render_world_depth( ortho
, camera
);
335 glEnable(GL_DEPTH_TEST
);
336 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
340 * TODO: World settings entity
342 struct ub_world_lighting
*winfo
= &gpipeline
.ub_world_lighting
;
343 v4_copy( wrender
.plane
, winfo
->g_water_plane
);
346 bounds
[0] = world
.geo
.bbx
[0][0];
347 bounds
[1] = world
.geo
.bbx
[0][2];
348 bounds
[2] = 1.0f
/ (world
.geo
.bbx
[1][0]-world
.geo
.bbx
[0][0]);
349 bounds
[3] = 1.0f
/ (world
.geo
.bbx
[1][2]-world
.geo
.bbx
[0][2]);
350 v4_copy( bounds
, winfo
->g_depth_bounds
);
352 winfo
->g_water_fog
= 0.04f
;
353 render_update_lighting_ub();
356 vg_release_thread_sync();
358 world_routes_loadfrom( mworld
);
360 for( int i
=0; i
<world
.instance_cache_count
; i
++ )
361 vg_free( world
.instance_cache
[i
].mdl
);
363 vg_free( world
.instance_cache
);
365 scene_free_offline_buffers( &world
.foliage
);
368 * Setup scene collider
370 v3_zero( world
.rb_geo
.co
);
371 q_identity( world
.rb_geo
.q
);
373 world
.rb_geo
.type
= k_rb_shape_scene
;
374 world
.rb_geo
.inf
.scene
.pscene
= &world
.geo
;
375 world
.rb_geo
.is_world
= 1;
376 rb_init( &world
.rb_geo
);
379 #endif /* WORLD_GEN_H */