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"
20 #include "world_sfd.h"
22 #include "shaders/terrain.h"
23 #include "shaders/sky.h"
24 #include "shaders/planeinf.h"
25 #include "shaders/standard.h"
26 #include "shaders/vblend.h"
27 #include "shaders/gpos.h"
28 #include "shaders/fscolour.h"
29 #include "shaders/alphatest.h"
43 struct subworld_routes routes
;
44 struct subworld_sfd sfd
;
47 struct subworld_spawns system_spawns;
48 struct subworld_physics system_physics;
52 traffic_node traffic
[128];
56 traffic_driver van_man
[6];
61 /* Rendering & geometry */
65 mdl_submesh sm_geo_std_oob
, sm_geo_std
, sm_geo_vb
,
66 sm_foliage_main
, sm_foliage_alphatest
;
68 glmesh skybox
, skydome
;
69 mdl_submesh dome_upper
, dome_lower
;
72 mdl_submesh car_holden
;
84 u32 instance_cache_count
,
89 static struct subworld_routes
*subworld_routes(void) { return &world
.routes
; }
90 static struct subworld_sfd
*subworld_sfd(void) { return &world
.sfd
; }
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();
127 world_routes_register();
128 world_sfd_register();
131 static void world_free(void)
138 static void render_world_depth( m4x4f projection
, m4x3f camera
);
140 static void add_all_if_material( m4x3f transform
, scene
*pscene
,
141 mdl_header
*mdl
, u32 id
)
143 for( int i
=0; i
<mdl
->node_count
; i
++ )
145 mdl_node
*pnode
= mdl_node_from_id( mdl
, i
);
147 for( int j
=0; j
<pnode
->submesh_count
; j
++ )
149 mdl_submesh
*sm
= mdl_node_submesh( mdl
, pnode
, j
);
151 if( sm
->material_id
== id
)
154 mdl_node_transform( pnode
, transform2
);
155 m4x3_mul( transform
, transform2
, transform2
);
157 scene_add_submesh( pscene
, mdl
, sm
, transform2
);
161 if( pnode
->classtype
== k_classtype_instance
)
165 u32 instance_id
= pnode
->sub_uid
-1;
166 struct instance_cache
*cache
= &world
.instance_cache
[instance_id
];
167 mdl_header
*mdl2
= cache
->mdl
;
170 mdl_node_transform( pnode
, transform2
);
171 m4x3_mul( transform
, transform2
, transform2
);
173 add_all_if_material( transform2
, pscene
, mdl2
, id
);
179 static void world_apply_procedural_foliage(void)
181 mdl_header
*mfoliage
= mdl_load("models/rs_foliage.mdl");
184 v3_sub( world
.geo
.bbx
[1], world
.geo
.bbx
[0], volume
);
188 mdl_node
*mblob
= mdl_node_from_name( mfoliage
, "blob" );
189 mdl_submesh
*sm_blob
= mdl_node_submesh( mfoliage
, mblob
, 0 );
191 for( int i
=0;i
<100000;i
++ )
194 v3_mul( volume
, (v3f
){ vg_randf(), 1000.0f
, vg_randf() }, pos
);
196 v3_add( pos
, world
.geo
.bbx
[0], pos
);
201 if( ray_world( pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
203 if( hit
.normal
[1] > 0.8f
&& !ray_hit_is_ramp(&hit
) &&
204 hit
.pos
[1] > water_height()+10.0f
)
206 v4f qsurface
, qrandom
;
209 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, hit
.normal
, axis
);
211 float angle
= v3_dot(hit
.normal
,(v3f
){0.0f
,1.0f
,0.0f
});
212 q_axis_angle( qsurface
, axis
, angle
);
213 q_axis_angle( qrandom
, (v3f
){0.0f
,1.0f
,0.0f
}, vg_randf()*VG_TAUf
);
214 q_mul( qsurface
, qrandom
, qsurface
);
215 q_m3x3( qsurface
, transform
);
217 v3_copy( hit
.pos
, transform
[3] );
218 scene_add_submesh( &world
.foliage
, mfoliage
, sm_blob
, transform
);
225 static void world_load(void)
227 mdl_header
*mworld
= mdl_load( "models/mp_dev.mdl" );
229 world
.spawn_count
= 0;
230 world
.traffic_count
= 0;
231 world
.instance_cache
= NULL
;
236 for( int i
=0; i
<mworld
->node_count
; i
++ )
238 mdl_node
*pnode
= mdl_node_from_id( mworld
, i
);
240 if( pnode
->classtype
== k_classtype_none
)
242 else if( pnode
->classtype
== k_classtype_spawn
)
244 struct respawn_point
*rp
= &world
.spawns
[ world
.spawn_count
++ ];
246 v3_copy( pnode
->co
, rp
->co
);
247 v4_copy( pnode
->q
, rp
->q
);
248 strcpy( rp
->name
, mdl_pstr( mworld
, pnode
->pstr_name
) );
250 else if( pnode
->classtype
== k_classtype_water
)
252 if( wrender
.enabled
)
254 vg_warn( "Multiple water surfaces in level! ('%s')\n",
255 mdl_pstr( mworld
, pnode
->pstr_name
));
259 mdl_submesh
*sm
= mdl_node_submesh( mworld
, pnode
, 0 );
264 mdl_unpack_submesh( mworld
, &surf
, sm
);
266 water_set_surface( &surf
, pnode
->co
[1] );
269 else if( pnode
->classtype
== k_classtype_car_path
)
271 struct classtype_car_path
*p
= mdl_get_entdata( mworld
, pnode
);
272 traffic_node
*tn
= &world
.traffic
[ world
.traffic_count
];
276 if( p
->target
) tn
->mn_next
= mdl_node_from_id( mworld
, p
->target
);
277 if( p
->target1
) tn
->mn_next1
= mdl_node_from_id( mworld
, p
->target1
);
280 mdl_node_transform( pnode
, transform
);
281 m3x3_mulv( transform
, (v3f
){1.0f
,0.0f
,0.0f
}, tn
->h
);
282 v3_copy( transform
[3], tn
->co
);
284 pnode
->sub_uid
= world
.traffic_count
++;
286 else if( pnode
->classtype
== k_classtype_instance
)
288 struct classtype_instance
*inst
= mdl_get_entdata( mworld
, pnode
);
292 for( int i
=0; i
<world
.instance_cache_count
; i
++ )
294 struct instance_cache
*cache
= &world
.instance_cache
[i
];
295 if( inst
->pstr_file
== cache
->pstr_file
)
298 pnode
->sub_uid
= i
+1;
305 world
.instance_cache
= buffer_reserve(
306 world
.instance_cache
, world
.instance_cache_count
,
307 &world
.instance_cache_cap
, 1,
308 sizeof(struct instance_cache
) );
310 struct instance_cache
*cache
=
311 &world
.instance_cache
[world
.instance_cache_count
];
313 const char *filename
= mdl_pstr(mworld
, inst
->pstr_file
);
315 cache
->pstr_file
= inst
->pstr_file
;
316 cache
->mdl
= mdl_load( filename
);
320 world
.instance_cache_count
++;
321 pnode
->sub_uid
= world
.instance_cache_count
;
322 mdl_link_materials( mworld
, cache
->mdl
);
323 vg_success( "Cached %s\n", filename
);
327 vg_warn( "Failed to cache %s\n", filename
);
333 world
.instance_cache
= buffer_fix( world
.instance_cache
,
334 world
.instance_cache_count
,
335 &world
.instance_cache_cap
,
336 sizeof( struct instance_cache
) );
339 traffic_finalize( world
.traffic
, world
.traffic_count
);
340 for( int i
=0; i
<vg_list_size(world
.van_man
); i
++ )
341 world
.van_man
[i
].current
=&world
.traffic
[vg_randint(world
.traffic_count
)];
345 * Compile meshes into the world scenes
347 scene_init( &world
.geo
);
351 mat_vertex_blend
= 0,
354 for( int i
=1; i
<mworld
->material_count
; i
++ )
356 mdl_material
*mat
= mdl_material_from_id( mworld
, i
);
357 const char *mat_name
= mdl_pstr( mworld
, mat
->pstr_name
);
359 if( !strcmp( "surf", mat_name
))
361 else if( !strcmp( "surf_oob", mat_name
))
363 else if( !strcmp( "vertex_blend", mat_name
))
364 mat_vertex_blend
= i
;
365 else if( !strcmp( "alphatest", mat_name
))
370 m4x3_identity( midentity
);
373 add_all_if_material( midentity
, &world
.geo
, mworld
, mat_surf_oob
);
375 vg_warn( "No OOB surface\n" );
376 scene_copy_slice( &world
.geo
, &world
.sm_geo_std_oob
);
379 add_all_if_material( midentity
, &world
.geo
, mworld
, mat_surf
);
380 scene_copy_slice( &world
.geo
, &world
.sm_geo_std
);
382 if( mat_vertex_blend
)
383 add_all_if_material( midentity
, &world
.geo
, mworld
, mat_vertex_blend
);
384 scene_copy_slice( &world
.geo
, &world
.sm_geo_vb
);
386 scene_upload( &world
.geo
);
387 scene_bh_create( &world
.geo
);
390 /* Foliage /nocollide layer.
391 * TODO: Probably should have material traits for this
393 scene_init( &world
.foliage
);
395 world_apply_procedural_foliage();
396 scene_copy_slice( &world
.foliage
, &world
.sm_foliage_main
);
398 add_all_if_material( midentity
, &world
.foliage
, mworld
, mat_alphatest
);
399 scene_copy_slice( &world
.foliage
, &world
.sm_foliage_alphatest
);
401 scene_upload( &world
.foliage
);
402 world_routes_loadfrom( mworld
);
404 for( int i
=0; i
<world
.instance_cache_count
; i
++ )
405 free( world
.instance_cache
[i
].mdl
);
407 free( world
.instance_cache
);
411 * Rendering the depth map
417 v3_sub( world
.geo
.bbx
[1], world
.geo
.bbx
[0], extent
);
419 float fl
= world
.geo
.bbx
[0][0],
420 fr
= world
.geo
.bbx
[1][0],
421 fb
= world
.geo
.bbx
[0][2],
422 ft
= world
.geo
.bbx
[1][2],
427 ortho
[0][0] = 2.0f
* rl
;
428 ortho
[2][1] = 2.0f
* tb
;
429 ortho
[3][0] = (fr
+ fl
) * -rl
;
430 ortho
[3][1] = (ft
+ fb
) * -tb
;
432 m4x3_identity( camera
);
434 glViewport( 0, 0, 1024, 1024 );
435 glDisable(GL_DEPTH_TEST
);
436 glBindFramebuffer( GL_FRAMEBUFFER
, gpipeline
.fb_depthmap
);
437 shader_fscolour_use();
438 shader_fscolour_uColour( (v4f
){-9999.0f
,-9999.0f
,-9999.0f
,-9999.0f
} );
442 glBlendFunc(GL_ONE
, GL_ONE
);
443 glBlendEquation(GL_MAX
);
444 render_world_depth( ortho
, camera
);
446 glEnable(GL_DEPTH_TEST
);
449 * TODO: World settings entity
451 struct ub_world_lighting
*winfo
= &gpipeline
.ub_world_lighting
;
452 v4_copy( wrender
.plane
, winfo
->g_water_plane
);
455 bounds
[0] = world
.geo
.bbx
[0][0];
456 bounds
[1] = world
.geo
.bbx
[0][2];
457 bounds
[2] = 1.0f
/ (world
.geo
.bbx
[1][0]-world
.geo
.bbx
[0][0]);
458 bounds
[3] = 1.0f
/ (world
.geo
.bbx
[1][2]-world
.geo
.bbx
[0][2]);
459 v4_copy( bounds
, winfo
->g_depth_bounds
);
461 winfo
->g_water_fog
= 0.04f
;
462 render_update_lighting_ub();
465 world
.mr_ball
.type
= k_rb_shape_sphere
;
466 world
.mr_ball
.inf
.sphere
.radius
= 2.0f
;
467 v3_copy( (v3f
){ 0.0f
, 110.0f
, 0.0f
}, world
.mr_ball
.co
);
469 q_identity(world
.mr_ball
.q
);
470 rb_init( &world
.mr_ball
);
473 * Setup scene collider
475 v3_zero( world
.rb_geo
.co
);
476 q_identity( world
.rb_geo
.q
);
478 world
.rb_geo
.type
= k_rb_shape_scene
;
479 world
.rb_geo
.inf
.scene
.pscene
= &world
.geo
;
480 world
.rb_geo
.is_world
= 1;
481 rb_init( &world
.rb_geo
);
484 static void world_init(void)
486 vg_tex2d_init( (vg_tex2d
*[]){ &tex_terrain_colours
,
488 &tex_alphatest
}, 3 );
490 mdl_header
*mcars
= mdl_load( "models/rs_cars.mdl" );
491 mdl_unpack_glmesh( mcars
, &world
.cars
);
492 mdl_node
*nholden
= mdl_node_from_name( mcars
, "holden" );
493 world
.car_holden
= *mdl_node_submesh( mcars
, nholden
, 0 );
497 mdl_header
*msky
= mdl_load("models/rs_skydome.mdl");
498 mdl_unpack_glmesh( msky
, &world
.skydome
);
500 mdl_node
*nlower
= mdl_node_from_name( msky
, "dome_lower" ),
501 *nupper
= mdl_node_from_name( msky
, "dome_upper" );
503 world
.dome_lower
= *mdl_node_submesh( msky
, nlower
, 0 );
504 world
.dome_upper
= *mdl_node_submesh( msky
, nupper
, 0 );
512 static void world_update(void)
514 world_routes_debug();
515 sfd_update( &world
.sfd
.tester
);
519 rb_build_manifold_terrain_sphere( &world
.mr_ball
);
521 for( int i
=0; i
<5; i
++ )
522 rb_solve_contacts( rb_contact_buffer
, rb_contact_count
);
524 rb_iter( &world
.mr_ball
);
525 rb_update_transform( &world
.mr_ball
);
526 rb_debug( &world
.mr_ball
, 0 );
528 for( int i
=0; i
<vg_list_size(world
.van_man
); i
++ )
530 traffic_drive( &world
.van_man
[i
] );
531 traffic_visualize_car( &world
.van_man
[i
] );
540 static void bind_terrain_textures(void)
542 vg_tex2d_bind( &tex_terrain_noise
, 0 );
543 vg_tex2d_bind( &tex_terrain_colours
, 1 );
546 static void render_world_vb( m4x4f projection
, v3f camera
)
548 m4x3f identity_matrix
;
549 m4x3_identity( identity_matrix
);
552 shader_vblend_uTexGarbage(0);
553 shader_vblend_uTexGradients(1);
554 shader_link_standard_ub( _shader_vblend
.id
, 2 );
555 bind_terrain_textures();
557 shader_vblend_uPv( projection
);
558 shader_vblend_uMdl( identity_matrix
);
559 shader_vblend_uCamera( camera
);
561 scene_bind( &world
.geo
);
562 mdl_draw_submesh( &world
.sm_geo_vb
);
564 mesh_bind( &world
.cars
);
567 for( int i
=0; i
<vg_list_size(world
.van_man
); i
++ )
569 shader_vblend_uMdl( world
.van_man
[i
].transform
);
570 mdl_draw_submesh( &world
.car_holden
);
575 static void render_world_alphatest( m4x4f projection
, v3f camera
)
577 m4x3f identity_matrix
;
578 m4x3_identity( identity_matrix
);
580 shader_alphatest_use();
581 shader_alphatest_uTexGarbage(0);
582 shader_alphatest_uTexMain(1);
583 shader_link_standard_ub( _shader_alphatest
.id
, 2 );
585 vg_tex2d_bind( &tex_terrain_noise
, 0 );
586 vg_tex2d_bind( &tex_alphatest
, 1 );
588 shader_alphatest_uPv( projection
);
589 shader_alphatest_uMdl( identity_matrix
);
590 shader_alphatest_uCamera( camera
);
592 glDisable(GL_CULL_FACE
);
593 scene_bind( &world
.foliage
);
594 mdl_draw_submesh( &world
.sm_foliage_alphatest
);
595 glEnable(GL_CULL_FACE
);
598 static void render_terrain( m4x4f projection
, v3f camera
)
600 m4x3f identity_matrix
;
601 m4x3_identity( identity_matrix
);
603 shader_terrain_use();
604 shader_terrain_uTexGarbage(0);
605 shader_terrain_uTexGradients(1);
606 shader_link_standard_ub( _shader_terrain
.id
, 2 );
607 bind_terrain_textures();
609 shader_terrain_uPv( projection
);
610 shader_terrain_uMdl( identity_matrix
);
611 shader_terrain_uCamera( camera
);
613 scene_bind( &world
.geo
);
614 mdl_draw_submesh( &world
.sm_geo_std_oob
);
615 mdl_draw_submesh( &world
.sm_geo_std
);
617 /* TODO: Dont draw in reflection */
618 glDisable(GL_CULL_FACE
);
619 scene_bind( &world
.foliage
);
620 mdl_draw_submesh( &world
.sm_foliage_main
);
621 glEnable(GL_CULL_FACE
);
624 static void render_lowerdome( m4x3f camera
)
626 m4x4f projection
, full
;
627 pipeline_projection( projection
, 0.4f
, 1000.0f
);
630 m3x3_transpose( camera
, inverse
);
631 v3_copy((v3f
){0.0f
,0.0f
,0.0f
}, inverse
[3]);
632 m4x3_expand( inverse
, full
);
633 m4x4_mul( projection
, full
, full
);
635 m4x3f identity_matrix
;
636 m4x3_identity( identity_matrix
);
638 shader_planeinf_use();
639 shader_planeinf_uMdl(identity_matrix
);
640 shader_planeinf_uPv(full
);
641 shader_planeinf_uCamera(camera
[3]);
642 shader_planeinf_uPlane( (v4f
){0.0f
,1.0f
,0.0f
, water_height()} );
644 mdl_draw_submesh( &world
.dome_lower
);
647 static void render_sky(m4x3f camera
)
649 m4x4f projection
, full
;
650 pipeline_projection( projection
, 0.4f
, 1000.0f
);
653 m3x3_transpose( camera
, inverse
);
654 v3_copy((v3f
){0.0f
,0.0f
,0.0f
}, inverse
[3]);
655 m4x3_expand( inverse
, full
);
656 m4x4_mul( projection
, full
, full
);
658 m4x3f identity_matrix
;
659 m4x3_identity( identity_matrix
);
662 shader_sky_uMdl(identity_matrix
);
663 shader_sky_uPv(full
);
664 shader_sky_uTexGarbage(0);
665 shader_sky_uTime( vg_time
);
667 vg_tex2d_bind( &tex_terrain_noise
, 0 );
669 glDepthMask( GL_FALSE
);
670 glDisable( GL_DEPTH_TEST
);
672 mesh_bind( &world
.skydome
);
673 mdl_draw_submesh( &world
.dome_upper
);
675 glEnable( GL_DEPTH_TEST
);
676 glDepthMask( GL_TRUE
);
679 static void render_world_gates( m4x4f projection
, m4x3f camera
)
682 for( int i
=0; i
<world
.routes
.gate_count
; i
++ )
684 struct route_gate
*rg
= &world
.routes
.gates
[i
];
685 count
+= render_gate( &rg
->gate
, camera
);
692 static void render_world( m4x4f projection
, m4x3f camera
)
694 render_sky( camera
);
695 render_world_routes( projection
, camera
[3] );
696 render_world_vb( projection
, camera
[3] );
697 render_world_alphatest( projection
, camera
[3] );
698 render_terrain( projection
, camera
[3] );
700 m4x3f identity_matrix
;
701 m4x3_identity( identity_matrix
);
702 identity_matrix
[3][1] = 125.0f
;
705 q_axis_angle( t
, (v3f
){0.0f
,1.0f
,0.0f
}, 2.3f
);
706 q_m3x3( t
, identity_matrix
);
708 sfd_render( &world
.sfd
.tester
, projection
, camera
[3], identity_matrix
);
711 static void render_world_depth( m4x4f projection
, m4x3f camera
)
713 m4x3f identity_matrix
;
714 m4x3_identity( identity_matrix
);
717 shader_gpos_uCamera( camera
[3] );
718 shader_gpos_uPv( projection
);
719 shader_gpos_uMdl( identity_matrix
);
721 scene_bind( &world
.geo
);
722 scene_draw( &world
.geo
);
725 glDisable(GL_CULL_FACE
);
726 scene_bind( &world
.foliage
);
727 scene_draw( &world
.foliage
);
728 glEnable(GL_CULL_FACE
);