7 static void world_add_all_if_material( m4x3f transform
, scene
*pscene
,
8 mdl_header
*mdl
, u32 id
)
10 for( int i
=0; i
<mdl
->node_count
; i
++ )
12 mdl_node
*pnode
= mdl_node_from_id( mdl
, i
);
14 for( int j
=0; j
<pnode
->submesh_count
; j
++ )
16 mdl_submesh
*sm
= mdl_node_submesh( mdl
, pnode
, j
);
18 if( sm
->material_id
== id
)
21 mdl_node_transform( pnode
, transform2
);
22 m4x3_mul( transform
, transform2
, transform2
);
24 scene_add_submesh( pscene
, mdl
, sm
, transform2
);
28 if( pnode
->classtype
== k_classtype_instance
)
32 u32 instance_id
= pnode
->sub_uid
-1;
33 struct instance_cache
*cache
= &world
.instance_cache
[instance_id
];
34 mdl_header
*mdl2
= cache
->mdl
;
37 mdl_node_transform( pnode
, transform2
);
38 m4x3_mul( transform
, transform2
, transform2
);
40 world_add_all_if_material( transform2
, pscene
, mdl2
, id
);
46 static void world_apply_procedural_foliage(void)
48 mdl_header
*mfoliage
= mdl_load("models/rs_foliage.mdl");
51 v3_sub( world
.geo
.bbx
[1], world
.geo
.bbx
[0], volume
);
55 mdl_node
*mblob
= mdl_node_from_name( mfoliage
, "blob" );
56 mdl_submesh
*sm_blob
= mdl_node_submesh( mfoliage
, mblob
, 0 );
58 for( int i
=0;i
<100000;i
++ )
61 v3_mul( volume
, (v3f
){ vg_randf(), 1000.0f
, vg_randf() }, pos
);
63 v3_add( pos
, world
.geo
.bbx
[0], pos
);
68 if( ray_world( pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
70 if( (hit
.normal
[1] > 0.8f
) && ray_hit_is_terrain(&hit
) &&
71 (hit
.pos
[1] > 0.0f
+10.0f
) )
73 v4f qsurface
, qrandom
;
76 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, hit
.normal
, axis
);
78 float angle
= v3_dot(hit
.normal
,(v3f
){0.0f
,1.0f
,0.0f
});
79 q_axis_angle( qsurface
, axis
, angle
);
80 q_axis_angle( qrandom
, (v3f
){0.0f
,1.0f
,0.0f
}, vg_randf()*VG_TAUf
);
81 q_mul( qsurface
, qrandom
, qsurface
);
82 q_m3x3( qsurface
, transform
);
84 v3_copy( hit
.pos
, transform
[3] );
85 scene_add_submesh( &world
.foliage
, mfoliage
, sm_blob
, transform
);
93 static void world_load(void)
95 mdl_header
*mworld
= mdl_load( "models/mp_dev.mdl" );
96 vg_info( "Loading world: models/mp_dev.mdl\n" );
98 world
.spawn_count
= 0;
99 world
.traffic_count
= 0;
100 world
.instance_cache
= NULL
;
105 for( int i
=0; i
<mworld
->node_count
; i
++ )
107 mdl_node
*pnode
= mdl_node_from_id( mworld
, i
);
109 if( pnode
->classtype
== k_classtype_none
)
111 else if( pnode
->classtype
== k_classtype_spawn
)
113 struct respawn_point
*rp
= &world
.spawns
[ world
.spawn_count
++ ];
115 v3_copy( pnode
->co
, rp
->co
);
116 v4_copy( pnode
->q
, rp
->q
);
117 strcpy( rp
->name
, mdl_pstr( mworld
, pnode
->pstr_name
) );
119 else if( pnode
->classtype
== k_classtype_water
)
121 if( wrender
.enabled
)
123 vg_warn( "Multiple water surfaces in level! ('%s')\n",
124 mdl_pstr( mworld
, pnode
->pstr_name
));
128 mdl_submesh
*sm
= mdl_node_submesh( mworld
, pnode
, 0 );
133 if( vg_acquire_thread_sync(1) )
136 if( mdl_unpack_submesh( mworld
, &surf
, sm
) )
138 water_set_surface( &surf
, pnode
->co
[1] );
141 vg_release_thread_sync(1);
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 if( vg_acquire_thread_sync(1) )
275 if( !scene_upload( &world
.geo
) )
280 vg_release_thread_sync(1);
283 scene_bh_create( &world
.geo
);
286 /* Foliage /nocollide layer.
287 * TODO: Probably should have material traits for this
289 scene_init( &world
.foliage
);
291 world_apply_procedural_foliage();
292 scene_copy_slice( &world
.foliage
, &world
.sm_foliage_main
);
294 world_add_all_if_material( midentity
, &world
.foliage
, mworld
, mat_alphatest
);
295 scene_copy_slice( &world
.foliage
, &world
.sm_foliage_alphatest
);
297 world_add_all_if_material( midentity
, &world
.foliage
, mworld
, mat_graffiti
);
298 scene_copy_slice( &world
.foliage
, &world
.sm_graffiti
);
301 if( vg_acquire_thread_sync(1) )
303 if( !scene_upload( &world
.foliage
) )
309 * Rendering the depth map
315 v3_sub( world
.geo
.bbx
[1], world
.geo
.bbx
[0], extent
);
317 float fl
= world
.geo
.bbx
[0][0],
318 fr
= world
.geo
.bbx
[1][0],
319 fb
= world
.geo
.bbx
[0][2],
320 ft
= world
.geo
.bbx
[1][2],
325 ortho
[0][0] = 2.0f
* rl
;
326 ortho
[2][1] = 2.0f
* tb
;
327 ortho
[3][0] = (fr
+ fl
) * -rl
;
328 ortho
[3][1] = (ft
+ fb
) * -tb
;
330 m4x3_identity( camera
);
332 glViewport( 0, 0, 1024, 1024 );
333 glDisable(GL_DEPTH_TEST
);
334 glBindFramebuffer( GL_FRAMEBUFFER
, gpipeline
.fb_depthmap
);
335 shader_fscolour_use();
336 shader_fscolour_uColour( (v4f
){-9999.0f
,-9999.0f
,-9999.0f
,-9999.0f
} );
340 glBlendFunc(GL_ONE
, GL_ONE
);
341 glBlendEquation(GL_MAX
);
342 render_world_depth( ortho
, camera
);
344 glEnable(GL_DEPTH_TEST
);
345 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
349 * TODO: World settings entity
351 struct ub_world_lighting
*winfo
= &gpipeline
.ub_world_lighting
;
352 v4_copy( wrender
.plane
, winfo
->g_water_plane
);
355 bounds
[0] = world
.geo
.bbx
[0][0];
356 bounds
[1] = world
.geo
.bbx
[0][2];
357 bounds
[2] = 1.0f
/ (world
.geo
.bbx
[1][0]-world
.geo
.bbx
[0][0]);
358 bounds
[3] = 1.0f
/ (world
.geo
.bbx
[1][2]-world
.geo
.bbx
[0][2]);
359 v4_copy( bounds
, winfo
->g_depth_bounds
);
361 winfo
->g_water_fog
= 0.04f
;
362 render_update_lighting_ub();
364 vg_release_thread_sync(1);
367 world_routes_loadfrom( mworld
);
369 for( int i
=0; i
<world
.instance_cache_count
; i
++ )
370 free( world
.instance_cache
[i
].mdl
);
372 free( world
.instance_cache
);
374 scene_free_offline_buffers( &world
.foliage
);
377 * Setup scene collider
379 v3_zero( world
.rb_geo
.co
);
380 q_identity( world
.rb_geo
.q
);
382 world
.rb_geo
.type
= k_rb_shape_scene
;
383 world
.rb_geo
.inf
.scene
.pscene
= &world
.geo
;
384 world
.rb_geo
.is_world
= 1;
385 rb_init( &world
.rb_geo
);
388 #endif /* WORLD_GEN_H */