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;
49 teleport_gate gates
[64];
53 traffic_node traffic
[128];
57 traffic_driver van_man
[6];
62 /* Rendering & geometry */
66 mdl_submesh sm_geo_std_oob
, sm_geo_std
, sm_geo_vb
,
67 sm_foliage_main
, sm_foliage_alphatest
;
69 glmesh skybox
, skydome
;
70 mdl_submesh dome_upper
, dome_lower
;
73 mdl_submesh car_holden
;
85 u32 instance_cache_count
,
90 static struct subworld_routes
*subworld_routes(void) { return &world
.routes
; }
93 vg_tex2d tex_terrain_colours
= { .path
= "textures/gradients.qoi",
94 .flags
= VG_TEXTURE_CLAMP
|VG_TEXTURE_NEAREST
};
96 vg_tex2d tex_terrain_noise
= { .path
= "textures/garbage.qoi",
97 .flags
= VG_TEXTURE_NEAREST
};
99 vg_tex2d tex_alphatest
= { .path
= "textures/alphatest.qoi",
100 .flags
= VG_TEXTURE_NEAREST
};
102 static void ray_world_get_tri( ray_hit
*hit
, v3f tri
[3] )
104 for( int i
=0; i
<3; i
++ )
105 v3_copy( world
.geo
.verts
[ hit
->tri
[i
] ].co
, tri
[i
] );
108 static int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
)
110 return scene_raycast( &world
.geo
, pos
, dir
, hit
);
113 static int ray_hit_is_ramp( ray_hit
*hit
)
115 return hit
->tri
[0] > world
.sm_geo_std_oob
.vertex_count
;
118 static void world_register(void)
120 shader_terrain_register();
121 shader_sky_register();
122 shader_planeinf_register();
123 shader_gpos_register();
124 shader_fscolour_register();
125 shader_alphatest_register();
128 static void world_free(void)
133 static void render_world_depth( m4x4f projection
, m4x3f camera
);
135 static void add_all_if_material( m4x3f transform
, scene
*pscene
,
136 mdl_header
*mdl
, u32 id
)
138 for( int i
=0; i
<mdl
->node_count
; i
++ )
140 mdl_node
*pnode
= mdl_node_from_id( mdl
, i
);
142 for( int j
=0; j
<pnode
->submesh_count
; j
++ )
144 mdl_submesh
*sm
= mdl_node_submesh( mdl
, pnode
, j
);
146 if( sm
->material_id
== id
)
149 mdl_node_transform( pnode
, transform2
);
150 m4x3_mul( transform
, transform2
, transform2
);
152 scene_add_submesh( pscene
, mdl
, sm
, transform2
);
156 if( pnode
->classtype
== k_classtype_instance
)
160 u32 instance_id
= pnode
->sub_uid
-1;
161 struct instance_cache
*cache
= &world
.instance_cache
[instance_id
];
162 mdl_header
*mdl2
= cache
->mdl
;
165 mdl_node_transform( pnode
, transform2
);
166 m4x3_mul( transform
, transform2
, transform2
);
168 add_all_if_material( transform2
, pscene
, mdl2
, id
);
174 static void world_apply_procedural_foliage(void)
176 mdl_header
*mfoliage
= mdl_load("models/rs_foliage.mdl");
179 v3_sub( world
.geo
.bbx
[1], world
.geo
.bbx
[0], volume
);
183 mdl_node
*mblob
= mdl_node_from_name( mfoliage
, "blob" );
184 mdl_submesh
*sm_blob
= mdl_node_submesh( mfoliage
, mblob
, 0 );
186 for( int i
=0;i
<100000;i
++ )
189 v3_mul( volume
, (v3f
){ vg_randf(), 1000.0f
, vg_randf() }, pos
);
191 v3_add( pos
, world
.geo
.bbx
[0], pos
);
196 if( ray_world( pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
198 if( hit
.normal
[1] > 0.8f
&& !ray_hit_is_ramp(&hit
) &&
199 hit
.pos
[1] > water_height()+10.0f
)
201 v4f qsurface
, qrandom
;
204 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, hit
.normal
, axis
);
206 float angle
= v3_dot(hit
.normal
,(v3f
){0.0f
,1.0f
,0.0f
});
207 q_axis_angle( qsurface
, axis
, angle
);
208 q_axis_angle( qrandom
, (v3f
){0.0f
,1.0f
,0.0f
}, vg_randf()*VG_TAUf
);
209 q_mul( qsurface
, qrandom
, qsurface
);
210 q_m3x3( qsurface
, transform
);
212 v3_copy( hit
.pos
, transform
[3] );
213 scene_add_submesh( &world
.foliage
, mfoliage
, sm_blob
, transform
);
220 static void world_load(void)
222 mdl_header
*mworld
= mdl_load( "models/mp_dev.mdl" );
224 world
.spawn_count
= 0;
225 world
.gate_count
= 0;
226 world
.traffic_count
= 0;
227 world
.instance_cache
= NULL
;
232 for( int i
=0; i
<mworld
->node_count
; i
++ )
234 mdl_node
*pnode
= mdl_node_from_id( mworld
, i
);
236 if( pnode
->classtype
== k_classtype_none
)
239 else if( pnode
->classtype
== k_classtype_gate
)
241 struct classtype_gate
*entgate
= mdl_get_entdata( mworld
, pnode
);
243 if( entgate
->target
)
245 mdl_node
*pother
= mdl_node_from_id( mworld
, entgate
->target
);
247 teleport_gate
*gate
= &world
.gates
[ world
.gate_count
++ ];
249 v3_copy( pnode
->co
, gate
->co
[0] );
250 v3_copy( pother
->co
, gate
->co
[1] );
251 v4_copy( pnode
->q
, gate
->q
[0] );
252 v4_copy( pother
->q
, gate
->q
[1] );
253 v2_copy( pnode
->s
, gate
->dims
);
255 gate_transform_update( gate
);
259 else if( pnode
->classtype
== k_classtype_spawn
)
261 struct respawn_point
*rp
= &world
.spawns
[ world
.spawn_count
++ ];
263 v3_copy( pnode
->co
, rp
->co
);
264 v4_copy( pnode
->q
, rp
->q
);
265 strcpy( rp
->name
, mdl_pstr( mworld
, pnode
->pstr_name
) );
267 else if( pnode
->classtype
== k_classtype_water
)
269 if( wrender
.enabled
)
271 vg_warn( "Multiple water surfaces in level! ('%s')\n",
272 mdl_pstr( mworld
, pnode
->pstr_name
));
276 mdl_submesh
*sm
= mdl_node_submesh( mworld
, pnode
, 0 );
281 mdl_unpack_submesh( mworld
, &surf
, sm
);
283 water_set_surface( &surf
, pnode
->co
[1] );
286 else if( pnode
->classtype
== k_classtype_car_path
)
288 struct classtype_car_path
*p
= mdl_get_entdata( mworld
, pnode
);
289 traffic_node
*tn
= &world
.traffic
[ world
.traffic_count
];
293 if( p
->target
) tn
->mn_next
= mdl_node_from_id( mworld
, p
->target
);
294 if( p
->target1
) tn
->mn_next1
= mdl_node_from_id( mworld
, p
->target1
);
297 mdl_node_transform( pnode
, transform
);
298 m3x3_mulv( transform
, (v3f
){1.0f
,0.0f
,0.0f
}, tn
->h
);
299 v3_copy( transform
[3], tn
->co
);
301 pnode
->sub_uid
= world
.traffic_count
++;
303 else if( pnode
->classtype
== k_classtype_instance
)
305 struct classtype_instance
*inst
= mdl_get_entdata( mworld
, pnode
);
309 for( int i
=0; i
<world
.instance_cache_count
; i
++ )
311 struct instance_cache
*cache
= &world
.instance_cache
[i
];
312 if( inst
->pstr_file
== cache
->pstr_file
)
315 pnode
->sub_uid
= i
+1;
322 world
.instance_cache
= buffer_reserve(
323 world
.instance_cache
, world
.instance_cache_count
,
324 &world
.instance_cache_cap
, 1,
325 sizeof(struct instance_cache
) );
327 struct instance_cache
*cache
=
328 &world
.instance_cache
[world
.instance_cache_count
];
330 const char *filename
= mdl_pstr(mworld
, inst
->pstr_file
);
332 cache
->pstr_file
= inst
->pstr_file
;
333 cache
->mdl
= mdl_load( filename
);
337 world
.instance_cache_count
++;
338 pnode
->sub_uid
= world
.instance_cache_count
;
339 mdl_link_materials( mworld
, cache
->mdl
);
340 vg_success( "Cached %s\n", filename
);
344 vg_warn( "Failed to cache %s\n", filename
);
350 world
.instance_cache
= buffer_fix( world
.instance_cache
,
351 world
.instance_cache_count
,
352 &world
.instance_cache_cap
,
353 sizeof( struct instance_cache
) );
356 traffic_finalize( world
.traffic
, world
.traffic_count
);
357 for( int i
=0; i
<vg_list_size(world
.van_man
); i
++ )
358 world
.van_man
[i
].current
=&world
.traffic
[vg_randint(world
.traffic_count
)];
362 * Compile meshes into the world scenes
364 scene_init( &world
.geo
);
368 mat_vertex_blend
= 0,
371 for( int i
=1; i
<mworld
->material_count
; i
++ )
373 mdl_material
*mat
= mdl_material_from_id( mworld
, i
);
374 const char *mat_name
= mdl_pstr( mworld
, mat
->pstr_name
);
376 if( !strcmp( "surf", mat_name
))
378 else if( !strcmp( "surf_oob", mat_name
))
380 else if( !strcmp( "vertex_blend", mat_name
))
381 mat_vertex_blend
= i
;
382 else if( !strcmp( "alphatest", mat_name
))
387 m4x3_identity( midentity
);
390 add_all_if_material( midentity
, &world
.geo
, mworld
, mat_surf_oob
);
392 vg_warn( "No OOB surface\n" );
393 scene_copy_slice( &world
.geo
, &world
.sm_geo_std_oob
);
396 add_all_if_material( midentity
, &world
.geo
, mworld
, mat_surf
);
397 scene_copy_slice( &world
.geo
, &world
.sm_geo_std
);
399 if( mat_vertex_blend
)
400 add_all_if_material( midentity
, &world
.geo
, mworld
, mat_vertex_blend
);
401 scene_copy_slice( &world
.geo
, &world
.sm_geo_vb
);
403 scene_upload( &world
.geo
);
404 scene_bh_create( &world
.geo
);
407 /* Foliage /nocollide layer.
408 * TODO: Probably should have material traits for this
410 scene_init( &world
.foliage
);
412 world_apply_procedural_foliage();
413 scene_copy_slice( &world
.foliage
, &world
.sm_foliage_main
);
415 add_all_if_material( midentity
, &world
.foliage
, mworld
, mat_alphatest
);
416 scene_copy_slice( &world
.foliage
, &world
.sm_foliage_alphatest
);
418 scene_upload( &world
.foliage
);
419 world_routes_init( mworld
);
421 for( int i
=0; i
<world
.instance_cache_count
; i
++ )
422 free( world
.instance_cache
[i
].mdl
);
424 free( world
.instance_cache
);
428 * Rendering the depth map
434 v3_sub( world
.geo
.bbx
[1], world
.geo
.bbx
[0], extent
);
436 float fl
= world
.geo
.bbx
[0][0],
437 fr
= world
.geo
.bbx
[1][0],
438 fb
= world
.geo
.bbx
[0][2],
439 ft
= world
.geo
.bbx
[1][2],
444 ortho
[0][0] = 2.0f
* rl
;
445 ortho
[2][1] = 2.0f
* tb
;
446 ortho
[3][0] = (fr
+ fl
) * -rl
;
447 ortho
[3][1] = (ft
+ fb
) * -tb
;
449 m4x3_identity( camera
);
451 glViewport( 0, 0, 1024, 1024 );
452 glDisable(GL_DEPTH_TEST
);
453 glBindFramebuffer( GL_FRAMEBUFFER
, gpipeline
.fb_depthmap
);
454 shader_fscolour_use();
455 shader_fscolour_uColour( (v4f
){-9999.0f
,-9999.0f
,-9999.0f
,-9999.0f
} );
459 glBlendFunc(GL_ONE
, GL_ONE
);
460 glBlendEquation(GL_MAX
);
461 render_world_depth( ortho
, camera
);
463 glEnable(GL_DEPTH_TEST
);
466 * TODO: World settings entity
468 struct ub_world_lighting
*winfo
= &gpipeline
.ub_world_lighting
;
469 v4_copy( wrender
.plane
, winfo
->g_water_plane
);
472 bounds
[0] = world
.geo
.bbx
[0][0];
473 bounds
[1] = world
.geo
.bbx
[0][2];
474 bounds
[2] = 1.0f
/ (world
.geo
.bbx
[1][0]-world
.geo
.bbx
[0][0]);
475 bounds
[3] = 1.0f
/ (world
.geo
.bbx
[1][2]-world
.geo
.bbx
[0][2]);
476 v4_copy( bounds
, winfo
->g_depth_bounds
);
478 winfo
->g_water_fog
= 0.04f
;
479 render_update_lighting_ub();
482 world
.mr_ball
.type
= k_rb_shape_sphere
;
483 world
.mr_ball
.inf
.sphere
.radius
= 2.0f
;
484 v3_copy( (v3f
){ 0.0f
, 110.0f
, 0.0f
}, world
.mr_ball
.co
);
486 q_identity(world
.mr_ball
.q
);
487 rb_init( &world
.mr_ball
);
490 * Setup scene collider
492 v3_zero( world
.rb_geo
.co
);
493 q_identity( world
.rb_geo
.q
);
495 world
.rb_geo
.type
= k_rb_shape_scene
;
496 world
.rb_geo
.inf
.scene
.pscene
= &world
.geo
;
497 world
.rb_geo
.is_world
= 1;
498 rb_init( &world
.rb_geo
);
501 static void world_init(void)
503 vg_tex2d_init( (vg_tex2d
*[]){ &tex_terrain_colours
,
505 &tex_alphatest
}, 3 );
507 mdl_header
*mcars
= mdl_load( "models/rs_cars.mdl" );
508 mdl_unpack_glmesh( mcars
, &world
.cars
);
509 mdl_node
*nholden
= mdl_node_from_name( mcars
, "holden" );
510 world
.car_holden
= *mdl_node_submesh( mcars
, nholden
, 0 );
514 mdl_header
*msky
= mdl_load("models/rs_skydome.mdl");
515 mdl_unpack_glmesh( msky
, &world
.skydome
);
517 mdl_node
*nlower
= mdl_node_from_name( msky
, "dome_lower" ),
518 *nupper
= mdl_node_from_name( msky
, "dome_upper" );
520 world
.dome_lower
= *mdl_node_submesh( msky
, nlower
, 0 );
521 world
.dome_upper
= *mdl_node_submesh( msky
, nupper
, 0 );
525 static void world_update(void)
527 world_routes_debug();
531 rb_build_manifold_terrain_sphere( &world
.mr_ball
);
533 for( int i
=0; i
<5; i
++ )
534 rb_solve_contacts( rb_contact_buffer
, rb_contact_count
);
536 rb_iter( &world
.mr_ball
);
537 rb_update_transform( &world
.mr_ball
);
538 rb_debug( &world
.mr_ball
, 0 );
540 for( int i
=0; i
<vg_list_size(world
.van_man
); i
++ )
542 traffic_drive( &world
.van_man
[i
] );
543 traffic_visualize_car( &world
.van_man
[i
] );
552 static void bind_terrain_textures(void)
554 vg_tex2d_bind( &tex_terrain_noise
, 0 );
555 vg_tex2d_bind( &tex_terrain_colours
, 1 );
558 static void render_world_vb( m4x4f projection
, v3f camera
)
560 m4x3f identity_matrix
;
561 m4x3_identity( identity_matrix
);
564 shader_vblend_uTexGarbage(0);
565 shader_vblend_uTexGradients(1);
566 shader_link_standard_ub( _shader_vblend
.id
, 2 );
567 bind_terrain_textures();
569 shader_vblend_uPv( projection
);
570 shader_vblend_uMdl( identity_matrix
);
571 shader_vblend_uCamera( camera
);
573 scene_bind( &world
.geo
);
574 mdl_draw_submesh( &world
.sm_geo_vb
);
576 mesh_bind( &world
.cars
);
579 for( int i
=0; i
<vg_list_size(world
.van_man
); i
++ )
581 shader_vblend_uMdl( world
.van_man
[i
].transform
);
582 mdl_draw_submesh( &world
.car_holden
);
587 static void render_world_alphatest( m4x4f projection
, v3f camera
)
589 m4x3f identity_matrix
;
590 m4x3_identity( identity_matrix
);
592 shader_alphatest_use();
593 shader_alphatest_uTexGarbage(0);
594 shader_alphatest_uTexMain(1);
595 shader_link_standard_ub( _shader_alphatest
.id
, 2 );
597 vg_tex2d_bind( &tex_terrain_noise
, 0 );
598 vg_tex2d_bind( &tex_alphatest
, 1 );
600 shader_alphatest_uPv( projection
);
601 shader_alphatest_uMdl( identity_matrix
);
602 shader_alphatest_uCamera( camera
);
604 glDisable(GL_CULL_FACE
);
605 scene_bind( &world
.foliage
);
606 mdl_draw_submesh( &world
.sm_foliage_alphatest
);
607 glEnable(GL_CULL_FACE
);
610 static void render_terrain( m4x4f projection
, v3f camera
)
612 m4x3f identity_matrix
;
613 m4x3_identity( identity_matrix
);
615 shader_terrain_use();
616 shader_terrain_uTexGarbage(0);
617 shader_terrain_uTexGradients(1);
618 shader_link_standard_ub( _shader_terrain
.id
, 2 );
619 bind_terrain_textures();
621 shader_terrain_uPv( projection
);
622 shader_terrain_uMdl( identity_matrix
);
623 shader_terrain_uCamera( camera
);
625 scene_bind( &world
.geo
);
626 mdl_draw_submesh( &world
.sm_geo_std_oob
);
627 mdl_draw_submesh( &world
.sm_geo_std
);
629 /* TODO: Dont draw in reflection */
630 glDisable(GL_CULL_FACE
);
631 scene_bind( &world
.foliage
);
632 mdl_draw_submesh( &world
.sm_foliage_main
);
633 glEnable(GL_CULL_FACE
);
636 static void render_lowerdome( 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
);
650 shader_planeinf_use();
651 shader_planeinf_uMdl(identity_matrix
);
652 shader_planeinf_uPv(full
);
653 shader_planeinf_uCamera(camera
[3]);
654 shader_planeinf_uPlane( (v4f
){0.0f
,1.0f
,0.0f
, water_height()} );
656 mdl_draw_submesh( &world
.dome_lower
);
659 static void render_sky(m4x3f camera
)
661 m4x4f projection
, full
;
662 pipeline_projection( projection
, 0.4f
, 1000.0f
);
665 m3x3_transpose( camera
, inverse
);
666 v3_copy((v3f
){0.0f
,0.0f
,0.0f
}, inverse
[3]);
667 m4x3_expand( inverse
, full
);
668 m4x4_mul( projection
, full
, full
);
670 m4x3f identity_matrix
;
671 m4x3_identity( identity_matrix
);
674 shader_sky_uMdl(identity_matrix
);
675 shader_sky_uPv(full
);
676 shader_sky_uTexGarbage(0);
677 shader_sky_uTime( vg_time
);
679 vg_tex2d_bind( &tex_terrain_noise
, 0 );
681 glDepthMask( GL_FALSE
);
682 glDisable( GL_DEPTH_TEST
);
684 mesh_bind( &world
.skydome
);
685 mdl_draw_submesh( &world
.dome_upper
);
687 glEnable( GL_DEPTH_TEST
);
688 glDepthMask( GL_TRUE
);
691 static void render_world( m4x4f projection
, m4x3f camera
)
693 render_sky( camera
);
694 render_world_vb( projection
, camera
[3] );
695 render_world_alphatest( projection
, camera
[3] );
696 render_terrain( projection
, camera
[3] );
699 static void render_world_depth( m4x4f projection
, m4x3f camera
)
701 m4x3f identity_matrix
;
702 m4x3_identity( identity_matrix
);
705 shader_gpos_uCamera( camera
[3] );
706 shader_gpos_uPv( projection
);
707 shader_gpos_uMdl( identity_matrix
);
709 scene_bind( &world
.geo
);
710 scene_draw( &world
.geo
);
713 glDisable(GL_CULL_FACE
);
714 scene_bind( &world
.foliage
);
715 scene_draw( &world
.foliage
);
716 glEnable(GL_CULL_FACE
);