3 static int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
);
12 #include "rigidbody.h"
18 #include "traffic.h" /*TODO: -> world_traffic.h */
19 #include "world_routes.h"
21 #include "shaders/terrain.h"
22 #include "shaders/sky.h"
23 #include "shaders/planeinf.h"
24 #include "shaders/standard.h"
25 #include "shaders/vblend.h"
26 #include "shaders/gpos.h"
27 #include "shaders/fscolour.h"
28 #include "shaders/alphatest.h"
42 struct subworld_routes routes
;
45 struct subworld_spawns system_spawns;
46 struct subworld_physics system_physics;
50 traffic_node traffic
[128];
54 traffic_driver van_man
[6];
59 /* Rendering & geometry */
63 mdl_submesh sm_geo_std_oob
, sm_geo_std
, sm_geo_vb
,
64 sm_foliage_main
, sm_foliage_alphatest
;
66 glmesh skybox
, skydome
;
67 mdl_submesh dome_upper
, dome_lower
;
70 mdl_submesh car_holden
;
82 u32 instance_cache_count
,
87 static struct subworld_routes
*subworld_routes(void) { return &world
.routes
; }
90 vg_tex2d tex_terrain_colours
= { .path
= "textures/gradients.qoi",
91 .flags
= VG_TEXTURE_CLAMP
|VG_TEXTURE_NEAREST
};
93 vg_tex2d tex_terrain_noise
= { .path
= "textures/garbage.qoi",
94 .flags
= VG_TEXTURE_NEAREST
};
96 vg_tex2d tex_alphatest
= { .path
= "textures/alphatest.qoi",
97 .flags
= VG_TEXTURE_NEAREST
};
99 static void ray_world_get_tri( ray_hit
*hit
, v3f tri
[3] )
101 for( int i
=0; i
<3; i
++ )
102 v3_copy( world
.geo
.verts
[ hit
->tri
[i
] ].co
, tri
[i
] );
105 static int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
)
107 return scene_raycast( &world
.geo
, pos
, dir
, hit
);
110 static int ray_hit_is_ramp( ray_hit
*hit
)
112 return hit
->tri
[0] > world
.sm_geo_std_oob
.vertex_count
;
115 static void world_register(void)
117 shader_terrain_register();
118 shader_sky_register();
119 shader_planeinf_register();
120 shader_gpos_register();
121 shader_fscolour_register();
122 shader_alphatest_register();
124 world_routes_register();
127 static void world_free(void)
132 static void render_world_depth( m4x4f projection
, m4x3f camera
);
134 static void add_all_if_material( m4x3f transform
, scene
*pscene
,
135 mdl_header
*mdl
, u32 id
)
137 for( int i
=0; i
<mdl
->node_count
; i
++ )
139 mdl_node
*pnode
= mdl_node_from_id( mdl
, i
);
141 for( int j
=0; j
<pnode
->submesh_count
; j
++ )
143 mdl_submesh
*sm
= mdl_node_submesh( mdl
, pnode
, j
);
145 if( sm
->material_id
== id
)
148 mdl_node_transform( pnode
, transform2
);
149 m4x3_mul( transform
, transform2
, transform2
);
151 scene_add_submesh( pscene
, mdl
, sm
, transform2
);
155 if( pnode
->classtype
== k_classtype_instance
)
159 u32 instance_id
= pnode
->sub_uid
-1;
160 struct instance_cache
*cache
= &world
.instance_cache
[instance_id
];
161 mdl_header
*mdl2
= cache
->mdl
;
164 mdl_node_transform( pnode
, transform2
);
165 m4x3_mul( transform
, transform2
, transform2
);
167 add_all_if_material( transform2
, pscene
, mdl2
, id
);
173 static void world_apply_procedural_foliage(void)
175 mdl_header
*mfoliage
= mdl_load("models/rs_foliage.mdl");
178 v3_sub( world
.geo
.bbx
[1], world
.geo
.bbx
[0], volume
);
182 mdl_node
*mblob
= mdl_node_from_name( mfoliage
, "blob" );
183 mdl_submesh
*sm_blob
= mdl_node_submesh( mfoliage
, mblob
, 0 );
185 for( int i
=0;i
<100000;i
++ )
188 v3_mul( volume
, (v3f
){ vg_randf(), 1000.0f
, vg_randf() }, pos
);
190 v3_add( pos
, world
.geo
.bbx
[0], pos
);
195 if( ray_world( pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
197 if( hit
.normal
[1] > 0.8f
&& !ray_hit_is_ramp(&hit
) &&
198 hit
.pos
[1] > water_height()+10.0f
)
200 v4f qsurface
, qrandom
;
203 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, hit
.normal
, axis
);
205 float angle
= v3_dot(hit
.normal
,(v3f
){0.0f
,1.0f
,0.0f
});
206 q_axis_angle( qsurface
, axis
, angle
);
207 q_axis_angle( qrandom
, (v3f
){0.0f
,1.0f
,0.0f
}, vg_randf()*VG_TAUf
);
208 q_mul( qsurface
, qrandom
, qsurface
);
209 q_m3x3( qsurface
, transform
);
211 v3_copy( hit
.pos
, transform
[3] );
212 scene_add_submesh( &world
.foliage
, mfoliage
, sm_blob
, transform
);
219 static void world_load(void)
221 mdl_header
*mworld
= mdl_load( "models/mp_dev.mdl" );
223 world
.spawn_count
= 0;
224 world
.traffic_count
= 0;
225 world
.instance_cache
= NULL
;
230 for( int i
=0; i
<mworld
->node_count
; i
++ )
232 mdl_node
*pnode
= mdl_node_from_id( mworld
, i
);
234 if( pnode
->classtype
== k_classtype_none
)
236 else if( pnode
->classtype
== k_classtype_spawn
)
238 struct respawn_point
*rp
= &world
.spawns
[ world
.spawn_count
++ ];
240 v3_copy( pnode
->co
, rp
->co
);
241 v4_copy( pnode
->q
, rp
->q
);
242 strcpy( rp
->name
, mdl_pstr( mworld
, pnode
->pstr_name
) );
244 else if( pnode
->classtype
== k_classtype_water
)
246 if( wrender
.enabled
)
248 vg_warn( "Multiple water surfaces in level! ('%s')\n",
249 mdl_pstr( mworld
, pnode
->pstr_name
));
253 mdl_submesh
*sm
= mdl_node_submesh( mworld
, pnode
, 0 );
258 mdl_unpack_submesh( mworld
, &surf
, sm
);
260 water_set_surface( &surf
, pnode
->co
[1] );
263 else if( pnode
->classtype
== k_classtype_car_path
)
265 struct classtype_car_path
*p
= mdl_get_entdata( mworld
, pnode
);
266 traffic_node
*tn
= &world
.traffic
[ world
.traffic_count
];
270 if( p
->target
) tn
->mn_next
= mdl_node_from_id( mworld
, p
->target
);
271 if( p
->target1
) tn
->mn_next1
= mdl_node_from_id( mworld
, p
->target1
);
274 mdl_node_transform( pnode
, transform
);
275 m3x3_mulv( transform
, (v3f
){1.0f
,0.0f
,0.0f
}, tn
->h
);
276 v3_copy( transform
[3], tn
->co
);
278 pnode
->sub_uid
= world
.traffic_count
++;
280 else if( pnode
->classtype
== k_classtype_instance
)
282 struct classtype_instance
*inst
= mdl_get_entdata( mworld
, pnode
);
286 for( int i
=0; i
<world
.instance_cache_count
; i
++ )
288 struct instance_cache
*cache
= &world
.instance_cache
[i
];
289 if( inst
->pstr_file
== cache
->pstr_file
)
292 pnode
->sub_uid
= i
+1;
299 world
.instance_cache
= buffer_reserve(
300 world
.instance_cache
, world
.instance_cache_count
,
301 &world
.instance_cache_cap
, 1,
302 sizeof(struct instance_cache
) );
304 struct instance_cache
*cache
=
305 &world
.instance_cache
[world
.instance_cache_count
];
307 const char *filename
= mdl_pstr(mworld
, inst
->pstr_file
);
309 cache
->pstr_file
= inst
->pstr_file
;
310 cache
->mdl
= mdl_load( filename
);
314 world
.instance_cache_count
++;
315 pnode
->sub_uid
= world
.instance_cache_count
;
316 mdl_link_materials( mworld
, cache
->mdl
);
317 vg_success( "Cached %s\n", filename
);
321 vg_warn( "Failed to cache %s\n", filename
);
327 world
.instance_cache
= buffer_fix( world
.instance_cache
,
328 world
.instance_cache_count
,
329 &world
.instance_cache_cap
,
330 sizeof( struct instance_cache
) );
333 traffic_finalize( world
.traffic
, world
.traffic_count
);
334 for( int i
=0; i
<vg_list_size(world
.van_man
); i
++ )
335 world
.van_man
[i
].current
=&world
.traffic
[vg_randint(world
.traffic_count
)];
339 * Compile meshes into the world scenes
341 scene_init( &world
.geo
);
345 mat_vertex_blend
= 0,
348 for( int i
=1; i
<mworld
->material_count
; i
++ )
350 mdl_material
*mat
= mdl_material_from_id( mworld
, i
);
351 const char *mat_name
= mdl_pstr( mworld
, mat
->pstr_name
);
353 if( !strcmp( "surf", mat_name
))
355 else if( !strcmp( "surf_oob", mat_name
))
357 else if( !strcmp( "vertex_blend", mat_name
))
358 mat_vertex_blend
= i
;
359 else if( !strcmp( "alphatest", mat_name
))
364 m4x3_identity( midentity
);
367 add_all_if_material( midentity
, &world
.geo
, mworld
, mat_surf_oob
);
369 vg_warn( "No OOB surface\n" );
370 scene_copy_slice( &world
.geo
, &world
.sm_geo_std_oob
);
373 add_all_if_material( midentity
, &world
.geo
, mworld
, mat_surf
);
374 scene_copy_slice( &world
.geo
, &world
.sm_geo_std
);
376 if( mat_vertex_blend
)
377 add_all_if_material( midentity
, &world
.geo
, mworld
, mat_vertex_blend
);
378 scene_copy_slice( &world
.geo
, &world
.sm_geo_vb
);
380 scene_upload( &world
.geo
);
381 scene_bh_create( &world
.geo
);
384 /* Foliage /nocollide layer.
385 * TODO: Probably should have material traits for this
387 scene_init( &world
.foliage
);
389 world_apply_procedural_foliage();
390 scene_copy_slice( &world
.foliage
, &world
.sm_foliage_main
);
392 add_all_if_material( midentity
, &world
.foliage
, mworld
, mat_alphatest
);
393 scene_copy_slice( &world
.foliage
, &world
.sm_foliage_alphatest
);
395 scene_upload( &world
.foliage
);
396 world_routes_loadfrom( mworld
);
398 for( int i
=0; i
<world
.instance_cache_count
; i
++ )
399 free( world
.instance_cache
[i
].mdl
);
401 free( world
.instance_cache
);
405 * Rendering the depth map
411 v3_sub( world
.geo
.bbx
[1], world
.geo
.bbx
[0], extent
);
413 float fl
= world
.geo
.bbx
[0][0],
414 fr
= world
.geo
.bbx
[1][0],
415 fb
= world
.geo
.bbx
[0][2],
416 ft
= world
.geo
.bbx
[1][2],
421 ortho
[0][0] = 2.0f
* rl
;
422 ortho
[2][1] = 2.0f
* tb
;
423 ortho
[3][0] = (fr
+ fl
) * -rl
;
424 ortho
[3][1] = (ft
+ fb
) * -tb
;
426 m4x3_identity( camera
);
428 glViewport( 0, 0, 1024, 1024 );
429 glDisable(GL_DEPTH_TEST
);
430 glBindFramebuffer( GL_FRAMEBUFFER
, gpipeline
.fb_depthmap
);
431 shader_fscolour_use();
432 shader_fscolour_uColour( (v4f
){-9999.0f
,-9999.0f
,-9999.0f
,-9999.0f
} );
436 glBlendFunc(GL_ONE
, GL_ONE
);
437 glBlendEquation(GL_MAX
);
438 render_world_depth( ortho
, camera
);
440 glEnable(GL_DEPTH_TEST
);
443 * TODO: World settings entity
445 struct ub_world_lighting
*winfo
= &gpipeline
.ub_world_lighting
;
446 v4_copy( wrender
.plane
, winfo
->g_water_plane
);
449 bounds
[0] = world
.geo
.bbx
[0][0];
450 bounds
[1] = world
.geo
.bbx
[0][2];
451 bounds
[2] = 1.0f
/ (world
.geo
.bbx
[1][0]-world
.geo
.bbx
[0][0]);
452 bounds
[3] = 1.0f
/ (world
.geo
.bbx
[1][2]-world
.geo
.bbx
[0][2]);
453 v4_copy( bounds
, winfo
->g_depth_bounds
);
455 winfo
->g_water_fog
= 0.04f
;
456 render_update_lighting_ub();
459 world
.mr_ball
.type
= k_rb_shape_sphere
;
460 world
.mr_ball
.inf
.sphere
.radius
= 2.0f
;
461 v3_copy( (v3f
){ 0.0f
, 110.0f
, 0.0f
}, world
.mr_ball
.co
);
463 q_identity(world
.mr_ball
.q
);
464 rb_init( &world
.mr_ball
);
467 * Setup scene collider
469 v3_zero( world
.rb_geo
.co
);
470 q_identity( world
.rb_geo
.q
);
472 world
.rb_geo
.type
= k_rb_shape_scene
;
473 world
.rb_geo
.inf
.scene
.pscene
= &world
.geo
;
474 world
.rb_geo
.is_world
= 1;
475 rb_init( &world
.rb_geo
);
478 static void world_init(void)
480 vg_tex2d_init( (vg_tex2d
*[]){ &tex_terrain_colours
,
482 &tex_alphatest
}, 3 );
484 mdl_header
*mcars
= mdl_load( "models/rs_cars.mdl" );
485 mdl_unpack_glmesh( mcars
, &world
.cars
);
486 mdl_node
*nholden
= mdl_node_from_name( mcars
, "holden" );
487 world
.car_holden
= *mdl_node_submesh( mcars
, nholden
, 0 );
491 mdl_header
*msky
= mdl_load("models/rs_skydome.mdl");
492 mdl_unpack_glmesh( msky
, &world
.skydome
);
494 mdl_node
*nlower
= mdl_node_from_name( msky
, "dome_lower" ),
495 *nupper
= mdl_node_from_name( msky
, "dome_upper" );
497 world
.dome_lower
= *mdl_node_submesh( msky
, nlower
, 0 );
498 world
.dome_upper
= *mdl_node_submesh( msky
, nupper
, 0 );
502 static void world_update(void)
504 world_routes_debug();
508 rb_build_manifold_terrain_sphere( &world
.mr_ball
);
510 for( int i
=0; i
<5; i
++ )
511 rb_solve_contacts( rb_contact_buffer
, rb_contact_count
);
513 rb_iter( &world
.mr_ball
);
514 rb_update_transform( &world
.mr_ball
);
515 rb_debug( &world
.mr_ball
, 0 );
517 for( int i
=0; i
<vg_list_size(world
.van_man
); i
++ )
519 traffic_drive( &world
.van_man
[i
] );
520 traffic_visualize_car( &world
.van_man
[i
] );
529 static void bind_terrain_textures(void)
531 vg_tex2d_bind( &tex_terrain_noise
, 0 );
532 vg_tex2d_bind( &tex_terrain_colours
, 1 );
535 static void render_world_vb( m4x4f projection
, v3f camera
)
537 m4x3f identity_matrix
;
538 m4x3_identity( identity_matrix
);
541 shader_vblend_uTexGarbage(0);
542 shader_vblend_uTexGradients(1);
543 shader_link_standard_ub( _shader_vblend
.id
, 2 );
544 bind_terrain_textures();
546 shader_vblend_uPv( projection
);
547 shader_vblend_uMdl( identity_matrix
);
548 shader_vblend_uCamera( camera
);
550 scene_bind( &world
.geo
);
551 mdl_draw_submesh( &world
.sm_geo_vb
);
553 mesh_bind( &world
.cars
);
556 for( int i
=0; i
<vg_list_size(world
.van_man
); i
++ )
558 shader_vblend_uMdl( world
.van_man
[i
].transform
);
559 mdl_draw_submesh( &world
.car_holden
);
564 static void render_world_alphatest( m4x4f projection
, v3f camera
)
566 m4x3f identity_matrix
;
567 m4x3_identity( identity_matrix
);
569 shader_alphatest_use();
570 shader_alphatest_uTexGarbage(0);
571 shader_alphatest_uTexMain(1);
572 shader_link_standard_ub( _shader_alphatest
.id
, 2 );
574 vg_tex2d_bind( &tex_terrain_noise
, 0 );
575 vg_tex2d_bind( &tex_alphatest
, 1 );
577 shader_alphatest_uPv( projection
);
578 shader_alphatest_uMdl( identity_matrix
);
579 shader_alphatest_uCamera( camera
);
581 glDisable(GL_CULL_FACE
);
582 scene_bind( &world
.foliage
);
583 mdl_draw_submesh( &world
.sm_foliage_alphatest
);
584 glEnable(GL_CULL_FACE
);
587 static void render_terrain( m4x4f projection
, v3f camera
)
589 m4x3f identity_matrix
;
590 m4x3_identity( identity_matrix
);
592 shader_terrain_use();
593 shader_terrain_uTexGarbage(0);
594 shader_terrain_uTexGradients(1);
595 shader_link_standard_ub( _shader_terrain
.id
, 2 );
596 bind_terrain_textures();
598 shader_terrain_uPv( projection
);
599 shader_terrain_uMdl( identity_matrix
);
600 shader_terrain_uCamera( camera
);
602 scene_bind( &world
.geo
);
603 mdl_draw_submesh( &world
.sm_geo_std_oob
);
604 mdl_draw_submesh( &world
.sm_geo_std
);
606 /* TODO: Dont draw in reflection */
607 glDisable(GL_CULL_FACE
);
608 scene_bind( &world
.foliage
);
609 mdl_draw_submesh( &world
.sm_foliage_main
);
610 glEnable(GL_CULL_FACE
);
613 static void render_lowerdome( m4x3f camera
)
615 m4x4f projection
, full
;
616 pipeline_projection( projection
, 0.4f
, 1000.0f
);
619 m3x3_transpose( camera
, inverse
);
620 v3_copy((v3f
){0.0f
,0.0f
,0.0f
}, inverse
[3]);
621 m4x3_expand( inverse
, full
);
622 m4x4_mul( projection
, full
, full
);
624 m4x3f identity_matrix
;
625 m4x3_identity( identity_matrix
);
627 shader_planeinf_use();
628 shader_planeinf_uMdl(identity_matrix
);
629 shader_planeinf_uPv(full
);
630 shader_planeinf_uCamera(camera
[3]);
631 shader_planeinf_uPlane( (v4f
){0.0f
,1.0f
,0.0f
, water_height()} );
633 mdl_draw_submesh( &world
.dome_lower
);
636 static void render_sky(m4x3f camera
)
638 m4x4f projection
, full
;
639 pipeline_projection( projection
, 0.4f
, 1000.0f
);
642 m3x3_transpose( camera
, inverse
);
643 v3_copy((v3f
){0.0f
,0.0f
,0.0f
}, inverse
[3]);
644 m4x3_expand( inverse
, full
);
645 m4x4_mul( projection
, full
, full
);
647 m4x3f identity_matrix
;
648 m4x3_identity( identity_matrix
);
651 shader_sky_uMdl(identity_matrix
);
652 shader_sky_uPv(full
);
653 shader_sky_uTexGarbage(0);
654 shader_sky_uTime( vg_time
);
656 vg_tex2d_bind( &tex_terrain_noise
, 0 );
658 glDepthMask( GL_FALSE
);
659 glDisable( GL_DEPTH_TEST
);
661 mesh_bind( &world
.skydome
);
662 mdl_draw_submesh( &world
.dome_upper
);
664 glEnable( GL_DEPTH_TEST
);
665 glDepthMask( GL_TRUE
);
668 static void render_world_gates( m4x4f projection
, m4x3f camera
)
671 for( int i
=0; i
<world
.routes
.gate_count
; i
++ )
673 struct route_gate
*rg
= &world
.routes
.gates
[i
];
674 count
+= render_gate( &rg
->gate
, camera
);
681 static void render_world( m4x4f projection
, m4x3f camera
)
683 render_sky( camera
);
684 render_world_routes( projection
, camera
[3] );
685 render_world_vb( projection
, camera
[3] );
686 render_world_alphatest( projection
, camera
[3] );
687 render_terrain( projection
, camera
[3] );
690 static void render_world_depth( m4x4f projection
, m4x3f camera
)
692 m4x3f identity_matrix
;
693 m4x3_identity( identity_matrix
);
696 shader_gpos_uCamera( camera
[3] );
697 shader_gpos_uPv( projection
);
698 shader_gpos_uMdl( identity_matrix
);
700 scene_bind( &world
.geo
);
701 scene_draw( &world
.geo
);
704 glDisable(GL_CULL_FACE
);
705 scene_bind( &world
.foliage
);
706 scene_draw( &world
.foliage
);
707 glEnable(GL_CULL_FACE
);