d9037c9191fb0e97eb2e59cf5bf8f7c5be9c6ee4
[carveJwlIkooP6JGAAIwe30JlM.git] / world_gen.h
1 #ifndef WORLD_GEN_H
2 #define WORLD_GEN_H
3
4 #include "world.h"
5
6
7 static void world_add_all_if_material( m4x3f transform, scene *pscene,
8 mdl_header *mdl, u32 id )
9 {
10 for( int i=0; i<mdl->node_count; i++ )
11 {
12 mdl_node *pnode = mdl_node_from_id( mdl, i );
13
14 for( int j=0; j<pnode->submesh_count; j++ )
15 {
16 mdl_submesh *sm = mdl_node_submesh( mdl, pnode, j );
17
18 if( sm->material_id == id )
19 {
20 m4x3f transform2;
21 mdl_node_transform( pnode, transform2 );
22 m4x3_mul( transform, transform2, transform2 );
23
24 scene_add_submesh( pscene, mdl, sm, transform2 );
25 }
26 }
27
28 if( pnode->classtype == k_classtype_instance )
29 {
30 if( pnode->sub_uid )
31 {
32 u32 instance_id = pnode->sub_uid -1;
33 struct instance_cache *cache = &world.instance_cache[instance_id];
34 mdl_header *mdl2 = cache->mdl;
35
36 m4x3f transform2;
37 mdl_node_transform( pnode, transform2 );
38 m4x3_mul( transform, transform2, transform2 );
39
40 world_add_all_if_material( transform2, pscene, mdl2, id );
41 }
42 }
43 }
44 }
45
46 static void world_apply_procedural_foliage(void)
47 {
48 mdl_header *mfoliage = mdl_load("models/rs_foliage.mdl");
49
50 v3f volume;
51 v3_sub( world.geo.bbx[1], world.geo.bbx[0], volume );
52 volume[1] = 1.0f;
53
54 m4x3f transform;
55 mdl_node *mblob = mdl_node_from_name( mfoliage, "blob" );
56 mdl_submesh *sm_blob = mdl_node_submesh( mfoliage, mblob, 0 );
57
58 for( int i=0;i<100000;i++ )
59 {
60 v3f pos;
61 v3_mul( volume, (v3f){ vg_randf(), 1000.0f, vg_randf() }, pos );
62 pos[1] = 1000.0f;
63 v3_add( pos, world.geo.bbx[0], pos );
64
65 ray_hit hit;
66 hit.dist = INFINITY;
67
68 if( ray_world( pos, (v3f){0.0f,-1.0f,0.0f}, &hit ))
69 {
70 if( (hit.normal[1] > 0.8f) && ray_hit_is_terrain(&hit) &&
71 (hit.pos[1] > 0.0f+10.0f) )
72 {
73 v4f qsurface, qrandom;
74 v3f axis;
75
76 v3_cross( (v3f){0.0f,1.0f,0.0f}, hit.normal, axis );
77
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 );
83
84 v3_copy( hit.pos, transform[3] );
85 scene_add_submesh( &world.foliage, mfoliage, sm_blob, transform);
86 }
87 }
88 }
89
90 free( mfoliage );
91 }
92
93 static void world_load(void)
94 {
95 mdl_header *mworld = mdl_load( "models/mp_dev.mdl" );
96 vg_info( "Loading world: models/mp_dev.mdl\n" );
97
98 world.spawn_count = 0;
99 world.traffic_count = 0;
100 world.instance_cache = NULL;
101
102 /*
103 * Process entities
104 */
105 for( int i=0; i<mworld->node_count; i++ )
106 {
107 mdl_node *pnode = mdl_node_from_id( mworld, i );
108
109 if( pnode->classtype == k_classtype_none )
110 {}
111 else if( pnode->classtype == k_classtype_spawn )
112 {
113 struct respawn_point *rp = &world.spawns[ world.spawn_count ++ ];
114
115 v3_copy( pnode->co, rp->co );
116 v4_copy( pnode->q, rp->q );
117 strcpy( rp->name, mdl_pstr( mworld, pnode->pstr_name ) );
118 }
119 else if( pnode->classtype == k_classtype_water )
120 {
121 if( wrender.enabled )
122 {
123 vg_warn( "Multiple water surfaces in level! ('%s')\n",
124 mdl_pstr( mworld, pnode->pstr_name ));
125 continue;
126 }
127
128 mdl_submesh *sm = mdl_node_submesh( mworld, pnode, 0 );
129
130 if( sm )
131 {
132
133 if( vg_acquire_thread_sync(1) )
134 {
135 glmesh surf;
136 if( mdl_unpack_submesh( mworld, &surf, sm ) )
137 {
138 water_set_surface( &surf, pnode->co[1] );
139 }
140
141 vg_release_thread_sync(1);
142 }
143 }
144 }
145 else if( pnode->classtype == k_classtype_car_path )
146 {
147 struct classtype_car_path *p = mdl_get_entdata( mworld, pnode );
148 traffic_node *tn = &world.traffic[ world.traffic_count ];
149 tn->mn_next = NULL;
150 tn->mn_next1 = NULL;
151
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 );
154
155 m4x3f transform;
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 );
159
160 pnode->sub_uid = world.traffic_count ++;
161 }
162 else if( pnode->classtype == k_classtype_instance )
163 {
164 struct classtype_instance *inst = mdl_get_entdata( mworld, pnode );
165 pnode->sub_uid = 0;
166
167 int cached = 0;
168 for( int i=0; i<world.instance_cache_count; i++ )
169 {
170 struct instance_cache *cache = &world.instance_cache[i];
171 if( inst->pstr_file == cache->pstr_file )
172 {
173 cached = 1;
174 pnode->sub_uid = i+1;
175 break;
176 }
177 }
178
179 if( !cached )
180 {
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) );
185
186 struct instance_cache *cache =
187 &world.instance_cache[world.instance_cache_count];
188
189 const char *filename = mdl_pstr(mworld, inst->pstr_file);
190
191 cache->pstr_file = inst->pstr_file;
192 cache->mdl = mdl_load( filename );
193
194 if( cache->mdl )
195 {
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 );
200 }
201 else
202 {
203 vg_warn( "Failed to cache %s\n", filename );
204 }
205 }
206 }
207 }
208
209 world.instance_cache = buffer_fix( world.instance_cache,
210 world.instance_cache_count,
211 &world.instance_cache_cap,
212 sizeof( struct instance_cache ) );
213
214 #if 0
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)];
218 #endif
219
220 /*
221 * Compile meshes into the world scenes
222 */
223 scene_init( &world.geo );
224
225 u32 mat_surf = 0,
226 mat_surf_oob = 0,
227 mat_vertex_blend = 0,
228 mat_alphatest = 0,
229 mat_graffiti = 0,
230 mat_subworld = 0,
231 mat_terrain = 0;
232
233 for( int i=1; i<mworld->material_count; i++ )
234 {
235 mdl_material *mat = mdl_material_from_id( mworld, i );
236 const char *mat_name = mdl_pstr( mworld, mat->pstr_name );
237
238 if( !strcmp( "surf", mat_name ))
239 mat_surf = i;
240 else if( !strcmp( "surf_oob", mat_name ))
241 mat_surf_oob = i;
242 else if( !strcmp( "vertex_blend", mat_name ))
243 mat_vertex_blend = i;
244 else if( !strcmp( "alphatest", mat_name ))
245 mat_alphatest = i;
246 else if( !strcmp( "graffitibox", mat_name ))
247 mat_graffiti = i;
248 else if( !strcmp( "terrain", mat_name ) )
249 mat_terrain = i;
250 }
251
252 m4x3f midentity;
253 m4x3_identity( midentity );
254
255 if( mat_terrain )
256 world_add_all_if_material( midentity, &world.geo, mworld, mat_terrain );
257 scene_copy_slice( &world.geo, &world.sm_terrain );
258
259 if( mat_surf_oob )
260 world_add_all_if_material( midentity, &world.geo, mworld, mat_surf_oob );
261 else
262 vg_warn( "No OOB surface\n" );
263 scene_copy_slice( &world.geo, &world.sm_geo_std_oob );
264
265 if( mat_surf )
266 world_add_all_if_material( midentity, &world.geo, mworld, mat_surf );
267 scene_copy_slice( &world.geo, &world.sm_geo_std );
268
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 );
272
273 if( vg_acquire_thread_sync(1) )
274 {
275 if( !scene_upload( &world.geo ) )
276 {
277
278 }
279
280 vg_release_thread_sync(1);
281 }
282
283 scene_bh_create( &world.geo );
284
285
286 /* Foliage /nocollide layer.
287 * TODO: Probably should have material traits for this
288 */
289 scene_init( &world.foliage );
290
291 world_apply_procedural_foliage();
292 scene_copy_slice( &world.foliage, &world.sm_foliage_main );
293
294 world_add_all_if_material( midentity, &world.foliage, mworld, mat_alphatest);
295 scene_copy_slice( &world.foliage, &world.sm_foliage_alphatest );
296
297 world_add_all_if_material( midentity, &world.foliage, mworld, mat_graffiti );
298 scene_copy_slice( &world.foliage, &world.sm_graffiti );
299
300
301 if( vg_acquire_thread_sync(1) )
302 {
303 if( !scene_upload( &world.foliage ) )
304 {
305
306 }
307
308 /*
309 * Rendering the depth map
310 */
311 m4x4f ortho;
312 m4x3f camera;
313
314 v3f extent;
315 v3_sub( world.geo.bbx[1], world.geo.bbx[0], extent );
316
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],
321 rl = 1.0f / (fr-fl),
322 tb = 1.0f / (ft-fb);
323
324 m4x4_zero( ortho );
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;
329 ortho[3][3] = 1.0f;
330 m4x3_identity( camera );
331
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} );
337 render_fsquad();
338
339 glEnable(GL_BLEND);
340 glBlendFunc(GL_ONE, GL_ONE);
341 glBlendEquation(GL_MAX);
342 render_world_depth( ortho, camera );
343 glDisable(GL_BLEND);
344 glEnable(GL_DEPTH_TEST);
345 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
346
347
348 /*
349 * TODO: World settings entity
350 */
351 struct ub_world_lighting *winfo = &gpipeline.ub_world_lighting;
352 v4_copy( wrender.plane, winfo->g_water_plane );
353
354 v4f bounds;
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 );
360
361 winfo->g_water_fog = 0.04f;
362 render_update_lighting_ub();
363
364 vg_release_thread_sync(1);
365 }
366
367 world_routes_loadfrom( mworld );
368
369 for( int i=0; i<world.instance_cache_count; i++ )
370 free( world.instance_cache[i].mdl );
371
372 free( world.instance_cache );
373 free( mworld );
374 scene_free_offline_buffers( &world.foliage );
375
376 /*
377 * Setup scene collider
378 */
379 v3_zero( world.rb_geo.co );
380 q_identity( world.rb_geo.q );
381
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 );
386 }
387
388 #endif /* WORLD_GEN_H */