4906603eda65f04d77bc3d77a6995a1b430944cc
6 static void world_add_all_if_material( m4x3f transform
, scene
*pscene
,
7 mdl_header
*mdl
, u32 id
)
9 for( int i
=0; i
<mdl
->node_count
; i
++ )
11 mdl_node
*pnode
= mdl_node_from_id( mdl
, i
);
13 for( int j
=0; j
<pnode
->submesh_count
; j
++ )
15 mdl_submesh
*sm
= mdl_node_submesh( mdl
, pnode
, j
);
17 if( sm
->material_id
== id
)
20 mdl_node_transform( pnode
, transform2
);
21 m4x3_mul( transform
, transform2
, transform2
);
23 scene_add_submesh( pscene
, mdl
, sm
, transform2
);
27 if( pnode
->classtype
== k_classtype_instance
)
31 u32 instance_id
= pnode
->sub_uid
-1;
32 struct instance_cache
*cache
= &world
.instance_cache
[instance_id
];
33 mdl_header
*mdl2
= cache
->mdl
;
36 mdl_node_transform( pnode
, transform2
);
37 m4x3_mul( transform
, transform2
, transform2
);
39 world_add_all_if_material( transform2
, pscene
, mdl2
, id
);
45 static void world_apply_procedural_foliage(void)
47 mdl_header
*mfoliage
= mdl_load("models/rs_foliage.mdl");
50 v3_sub( world
.geo
.bbx
[1], world
.geo
.bbx
[0], volume
);
54 mdl_node
*mblob
= mdl_node_from_name( mfoliage
, "blob" );
55 mdl_submesh
*sm_blob
= mdl_node_submesh( mfoliage
, mblob
, 0 );
57 for( int i
=0;i
<100000;i
++ )
60 v3_mul( volume
, (v3f
){ vg_randf(), 1000.0f
, vg_randf() }, pos
);
62 v3_add( pos
, world
.geo
.bbx
[0], pos
);
67 if( ray_world( pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
69 if( (hit
.normal
[1] > 0.8f
) && ray_hit_is_terrain(&hit
) &&
70 (hit
.pos
[1] > 0.0f
+10.0f
) )
72 v4f qsurface
, qrandom
;
75 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, hit
.normal
, axis
);
77 float angle
= v3_dot(hit
.normal
,(v3f
){0.0f
,1.0f
,0.0f
});
78 q_axis_angle( qsurface
, axis
, angle
);
79 q_axis_angle( qrandom
, (v3f
){0.0f
,1.0f
,0.0f
}, vg_randf()*VG_TAUf
);
80 q_mul( qsurface
, qrandom
, qsurface
);
81 q_m3x3( qsurface
, transform
);
83 v3_copy( hit
.pos
, transform
[3] );
84 scene_add_submesh( &world
.foliage
, mfoliage
, sm_blob
, transform
);
92 static void world_load(void)
94 mdl_header
*mworld
= mdl_load( "models/mp_dev.mdl" );
95 vg_info( "Loading world: models/mp_dev.mdl\n" );
97 world
.spawn_count
= 0;
98 world
.traffic_count
= 0;
99 world
.instance_cache
= NULL
;
104 for( int i
=0; i
<mworld
->node_count
; i
++ )
106 mdl_node
*pnode
= mdl_node_from_id( mworld
, i
);
108 if( pnode
->classtype
== k_classtype_none
)
110 else if( pnode
->classtype
== k_classtype_spawn
)
112 struct respawn_point
*rp
= &world
.spawns
[ world
.spawn_count
++ ];
114 v3_copy( pnode
->co
, rp
->co
);
115 v4_copy( pnode
->q
, rp
->q
);
116 strcpy( rp
->name
, mdl_pstr( mworld
, pnode
->pstr_name
) );
118 else if( pnode
->classtype
== k_classtype_water
)
120 if( wrender
.enabled
)
122 vg_warn( "Multiple water surfaces in level! ('%s')\n",
123 mdl_pstr( mworld
, pnode
->pstr_name
));
127 mdl_submesh
*sm
= mdl_node_submesh( mworld
, pnode
, 0 );
131 vg_acquire_thread_sync();
134 mdl_unpack_submesh( mworld
, &surf
, sm
);
135 water_set_surface( &surf
, pnode
->co
[1] );
137 vg_release_thread_sync();
140 else if( pnode
->classtype
== k_classtype_car_path
)
142 struct classtype_car_path
*p
= mdl_get_entdata( mworld
, pnode
);
143 traffic_node
*tn
= &world
.traffic
[ world
.traffic_count
];
147 if( p
->target
) tn
->mn_next
= mdl_node_from_id( mworld
, p
->target
);
148 if( p
->target1
) tn
->mn_next1
= mdl_node_from_id( mworld
, p
->target1
);
151 mdl_node_transform( pnode
, transform
);
152 m3x3_mulv( transform
, (v3f
){1.0f
,0.0f
,0.0f
}, tn
->h
);
153 v3_copy( transform
[3], tn
->co
);
155 pnode
->sub_uid
= world
.traffic_count
++;
157 else if( pnode
->classtype
== k_classtype_instance
)
159 struct classtype_instance
*inst
= mdl_get_entdata( mworld
, pnode
);
163 for( int i
=0; i
<world
.instance_cache_count
; i
++ )
165 struct instance_cache
*cache
= &world
.instance_cache
[i
];
166 if( inst
->pstr_file
== cache
->pstr_file
)
169 pnode
->sub_uid
= i
+1;
176 world
.instance_cache
= buffer_reserve(
177 world
.instance_cache
, world
.instance_cache_count
,
178 &world
.instance_cache_cap
, 1,
179 sizeof(struct instance_cache
) );
181 struct instance_cache
*cache
=
182 &world
.instance_cache
[world
.instance_cache_count
];
184 const char *filename
= mdl_pstr(mworld
, inst
->pstr_file
);
186 cache
->pstr_file
= inst
->pstr_file
;
187 cache
->mdl
= mdl_load( filename
);
191 world
.instance_cache_count
++;
192 pnode
->sub_uid
= world
.instance_cache_count
;
193 mdl_link_materials( mworld
, cache
->mdl
);
194 vg_success( "Cached %s\n", filename
);
198 vg_warn( "Failed to cache %s\n", filename
);
204 world
.instance_cache
= buffer_fix( world
.instance_cache
,
205 world
.instance_cache_count
,
206 &world
.instance_cache_cap
,
207 sizeof( struct instance_cache
) );
210 traffic_finalize( world
.traffic
, world
.traffic_count
);
211 for( int i
=0; i
<vg_list_size(world
.van_man
); i
++ )
212 world
.van_man
[i
].current
=&world
.traffic
[vg_randint(world
.traffic_count
)];
216 * Compile meshes into the world scenes
218 scene_init( &world
.geo
);
222 mat_vertex_blend
= 0,
228 for( int i
=1; i
<mworld
->material_count
; i
++ )
230 mdl_material
*mat
= mdl_material_from_id( mworld
, i
);
231 const char *mat_name
= mdl_pstr( mworld
, mat
->pstr_name
);
233 if( !strcmp( "surf", mat_name
))
235 else if( !strcmp( "surf_oob", mat_name
))
237 else if( !strcmp( "vertex_blend", mat_name
))
238 mat_vertex_blend
= i
;
239 else if( !strcmp( "alphatest", mat_name
))
241 else if( !strcmp( "graffitibox", mat_name
))
243 else if( !strcmp( "terrain", mat_name
) )
248 m4x3_identity( midentity
);
251 world_add_all_if_material( midentity
, &world
.geo
, mworld
, mat_terrain
);
252 scene_copy_slice( &world
.geo
, &world
.sm_terrain
);
255 world_add_all_if_material( midentity
, &world
.geo
, mworld
, mat_surf_oob
);
257 vg_warn( "No OOB surface\n" );
258 scene_copy_slice( &world
.geo
, &world
.sm_geo_std_oob
);
261 world_add_all_if_material( midentity
, &world
.geo
, mworld
, mat_surf
);
262 scene_copy_slice( &world
.geo
, &world
.sm_geo_std
);
264 if( mat_vertex_blend
)
265 world_add_all_if_material( midentity
, &world
.geo
,mworld
,mat_vertex_blend
);
266 scene_copy_slice( &world
.geo
, &world
.sm_geo_vb
);
268 vg_acquire_thread_sync();
269 scene_upload( &world
.geo
);
270 vg_release_thread_sync();
272 scene_bh_create( &world
.geo
);
275 /* Foliage /nocollide layer.
276 * TODO: Probably should have material traits for this
278 scene_init( &world
.foliage
);
280 world_apply_procedural_foliage();
281 scene_copy_slice( &world
.foliage
, &world
.sm_foliage_main
);
283 world_add_all_if_material( midentity
, &world
.foliage
, mworld
, mat_alphatest
);
284 scene_copy_slice( &world
.foliage
, &world
.sm_foliage_alphatest
);
286 world_add_all_if_material( midentity
, &world
.foliage
, mworld
, mat_graffiti
);
287 scene_copy_slice( &world
.foliage
, &world
.sm_graffiti
);
290 vg_acquire_thread_sync();
292 scene_upload( &world
.foliage
);
295 * Rendering the depth map
301 v3_sub( world
.geo
.bbx
[1], world
.geo
.bbx
[0], extent
);
303 float fl
= world
.geo
.bbx
[0][0],
304 fr
= world
.geo
.bbx
[1][0],
305 fb
= world
.geo
.bbx
[0][2],
306 ft
= world
.geo
.bbx
[1][2],
311 ortho
[0][0] = 2.0f
* rl
;
312 ortho
[2][1] = 2.0f
* tb
;
313 ortho
[3][0] = (fr
+ fl
) * -rl
;
314 ortho
[3][1] = (ft
+ fb
) * -tb
;
316 m4x3_identity( camera
);
318 glViewport( 0, 0, 1024, 1024 );
319 glDisable(GL_DEPTH_TEST
);
320 glBindFramebuffer( GL_FRAMEBUFFER
, gpipeline
.fb_depthmap
);
321 shader_fscolour_use();
322 shader_fscolour_uColour( (v4f
){-9999.0f
,-9999.0f
,-9999.0f
,-9999.0f
} );
326 glBlendFunc(GL_ONE
, GL_ONE
);
327 glBlendEquation(GL_MAX
);
328 render_world_depth( ortho
, camera
);
330 glEnable(GL_DEPTH_TEST
);
331 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
335 * TODO: World settings entity
337 struct ub_world_lighting
*winfo
= &gpipeline
.ub_world_lighting
;
338 v4_copy( wrender
.plane
, winfo
->g_water_plane
);
341 bounds
[0] = world
.geo
.bbx
[0][0];
342 bounds
[1] = world
.geo
.bbx
[0][2];
343 bounds
[2] = 1.0f
/ (world
.geo
.bbx
[1][0]-world
.geo
.bbx
[0][0]);
344 bounds
[3] = 1.0f
/ (world
.geo
.bbx
[1][2]-world
.geo
.bbx
[0][2]);
345 v4_copy( bounds
, winfo
->g_depth_bounds
);
347 winfo
->g_water_fog
= 0.04f
;
348 render_update_lighting_ub();
351 vg_release_thread_sync();
353 world_routes_loadfrom( mworld
);
355 for( int i
=0; i
<world
.instance_cache_count
; i
++ )
356 vg_free( world
.instance_cache
[i
].mdl
);
358 vg_free( world
.instance_cache
);
360 scene_free_offline_buffers( &world
.foliage
);
363 * Setup scene collider
365 v3_zero( world
.rb_geo
.co
);
366 q_identity( world
.rb_geo
.q
);
368 world
.rb_geo
.type
= k_rb_shape_scene
;
369 world
.rb_geo
.inf
.scene
.pscene
= &world
.geo
;
370 world
.rb_geo
.is_world
= 1;
371 rb_init( &world
.rb_geo
);
374 #endif /* WORLD_GEN_H */