3 static int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
);
12 #include "rigidbody.h"
19 #include "shaders/terrain.h"
20 #include "shaders/sky.h"
21 #include "shaders/planeinf.h"
22 #include "shaders/standard.h"
23 #include "shaders/vblend.h"
24 #include "shaders/gpos.h"
25 #include "shaders/fscolour.h"
39 teleport_gate gates
[64];
43 traffic_node traffic
[128];
46 traffic_driver van_man
[6];
49 rigidbody temp_rbs
[128];
52 /* Rendering & geometry */
55 mdl_submesh sm_geo_std_oob
, sm_geo_std
, sm_geo_vb
;
57 glmesh skybox
, skydome
;
58 mdl_submesh dome_upper
, dome_lower
;
61 mdl_submesh car_holden
;
67 vg_tex2d tex_terrain_colours
= { .path
= "textures/gradients.qoi",
68 .flags
= VG_TEXTURE_CLAMP
|VG_TEXTURE_NEAREST
};
70 vg_tex2d tex_terrain_noise
= { .path
= "textures/garbage.qoi",
71 .flags
= VG_TEXTURE_NEAREST
};
73 static void ray_world_get_tri( ray_hit
*hit
, v3f tri
[3] )
75 for( int i
=0; i
<3; i
++ )
76 v3_copy( world
.geo
.verts
[ hit
->tri
[i
] ].co
, tri
[i
] );
79 static int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
)
81 return scene_raycast( &world
.geo
, pos
, dir
, hit
);
84 static int ray_hit_is_ramp( ray_hit
*hit
)
86 return hit
->tri
[0] > world
.sm_geo_std_oob
.vertex_count
;
89 static void world_register(void)
91 shader_terrain_register();
92 shader_sky_register();
93 shader_planeinf_register();
94 shader_gpos_register();
95 shader_fscolour_register();
98 static void world_free(void)
103 static void render_world_depth( m4x4f projection
, m4x3f camera
);
105 static void add_all_if_material( scene
*pscene
, mdl_header
*mdl
, u32 id
)
107 for( int i
=0; i
<mdl
->node_count
; i
++ )
109 mdl_node
*pnode
= mdl_node_from_id( mdl
, i
);
111 for( int j
=0; j
<pnode
->submesh_count
; j
++ )
113 mdl_submesh
*sm
= mdl_node_submesh( mdl
, pnode
, j
);
115 if( sm
->material_id
== id
)
118 mdl_node_transform( pnode
, transform
);
119 scene_add_submesh( pscene
, mdl
, sm
, transform
);
125 static void world_apply_foliage(void)
127 scene_init( &world
.foliage
);
128 mdl_header
*mfoliage
= mdl_load("models/rs_foliage.mdl");
131 v3_sub( world
.geo
.bbx
[1], world
.geo
.bbx
[0], volume
);
135 mdl_node
*mblob
= mdl_node_from_name( mfoliage
, "blob" );
136 mdl_submesh
*sm_blob
= mdl_node_submesh( mfoliage
, mblob
, 0 );
138 for( int i
=0;i
<100000;i
++ )
141 v3_mul( volume
, (v3f
){ vg_randf(), 1000.0f
, vg_randf() }, pos
);
143 v3_add( pos
, world
.geo
.bbx
[0], pos
);
148 if( ray_world( pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
150 if( hit
.normal
[1] > 0.8f
&& !ray_hit_is_ramp(&hit
) &&
151 hit
.pos
[1] > water_height()+10.0f
)
153 v4f qsurface
, qrandom
;
156 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, hit
.normal
, axis
);
158 float angle
= v3_dot(hit
.normal
,(v3f
){0.0f
,1.0f
,0.0f
});
159 q_axis_angle( qsurface
, axis
, angle
);
160 q_axis_angle( qrandom
, (v3f
){0.0f
,1.0f
,0.0f
}, vg_randf()*VG_TAUf
);
161 q_mul( qsurface
, qrandom
, qsurface
);
162 q_m3x3( qsurface
, transform
);
164 v3_copy( hit
.pos
, transform
[3] );
165 scene_add_submesh( &world
.foliage
, mfoliage
, sm_blob
, transform
);
170 scene_upload( &world
.foliage
);
174 static void world_load(void)
176 mdl_header
*mworld
= mdl_load( "models/mp_dev.mdl" );
178 world
.spawn_count
= 0;
179 world
.gate_count
= 0;
181 world
.traffic_count
= 0;
183 scene_init( &world
.geo
);
186 * Compile meshes into the world scenes
190 mat_vertex_blend
= 0;
192 for( int i
=1; i
<mworld
->material_count
; i
++ )
194 mdl_material
*mat
= mdl_material_from_id( mworld
, i
);
195 const char *mat_name
= mdl_pstr( mworld
, mat
->pstr_name
);
197 vg_info( "%d %s\n", mat
->pstr_name
, mat_name
);
199 if( !strcmp( "surf", mat_name
))
201 else if( !strcmp( "surf_oob", mat_name
))
203 else if( !strcmp( "vertex_blend", mat_name
))
204 mat_vertex_blend
= i
;
208 add_all_if_material( &world
.geo
, mworld
, mat_surf_oob
);
210 vg_warn( "No OOB surface\n" );
211 scene_copy_slice( &world
.geo
, &world
.sm_geo_std_oob
);
214 add_all_if_material( &world
.geo
, mworld
, mat_surf
);
215 scene_copy_slice( &world
.geo
, &world
.sm_geo_std
);
217 if( mat_vertex_blend
)
218 add_all_if_material( &world
.geo
, mworld
, mat_vertex_blend
);
219 scene_copy_slice( &world
.geo
, &world
.sm_geo_vb
);
221 scene_upload( &world
.geo
);
222 scene_bh_create( &world
.geo
);
227 for( int i
=0; i
<mworld
->node_count
; i
++ )
229 mdl_node
*pnode
= mdl_node_from_id( mworld
, i
);
231 if( pnode
->classtype
== k_classtype_none
)
233 else if( pnode
->classtype
== k_classtype_gate
)
235 struct classtype_gate
*entgate
= mdl_get_entdata( mworld
, pnode
);
237 if( entgate
->target
)
239 mdl_node
*pother
= mdl_node_from_id( mworld
, entgate
->target
);
241 teleport_gate
*gate
= &world
.gates
[ world
.gate_count
++ ];
243 v3_copy( pnode
->co
, gate
->co
[0] );
244 v3_copy( pother
->co
, gate
->co
[1] );
245 v4_copy( pnode
->q
, gate
->q
[0] );
246 v4_copy( pother
->q
, gate
->q
[1] );
247 v2_copy( pnode
->s
, gate
->dims
);
249 gate_transform_update( gate
);
252 else if( pnode
->classtype
== k_classtype_block
)
254 struct classtype_block
*block
= mdl_get_entdata( mworld
, pnode
);
257 mdl_node_transform( pnode
, transform
);
259 rigidbody
*rb
= &world
.temp_rbs
[ world
.rb_count
++ ];
261 box_copy( block
->bbx
, rb
->bbx
); /* TODO: apply scale */
262 v3_copy( pnode
->co
, rb
->co
);
264 v4_copy( pnode
->q
, rb
->q
);
265 rb_update_transform( rb
);
267 else if( pnode
->classtype
== k_classtype_spawn
)
269 struct respawn_point
*rp
= &world
.spawns
[ world
.spawn_count
++ ];
271 v3_copy( pnode
->co
, rp
->co
);
272 v4_copy( pnode
->q
, rp
->q
);
273 strcpy( rp
->name
, mdl_pstr( mworld
, pnode
->pstr_name
) );
275 else if( pnode
->classtype
== k_classtype_water
)
277 if( wrender
.enabled
)
279 vg_warn( "Multiple water surfaces in level! ('%s')\n",
280 mdl_pstr( mworld
, pnode
->pstr_name
));
284 mdl_submesh
*sm
= mdl_node_submesh( mworld
, pnode
, 0 );
289 mdl_unpack_submesh( mworld
, &surf
, sm
);
291 water_set_surface( &surf
, pnode
->co
[1] );
294 else if( pnode
->classtype
== k_classtype_car_path
)
296 struct classtype_car_path
*p
= mdl_get_entdata( mworld
, pnode
);
297 traffic_node
*tn
= &world
.traffic
[ world
.traffic_count
];
301 if( p
->target
) tn
->mn_next
= mdl_node_from_id( mworld
, p
->target
);
302 if( p
->target1
) tn
->mn_next1
= mdl_node_from_id( mworld
, p
->target1
);
305 mdl_node_transform( pnode
, transform
);
306 m3x3_mulv( transform
, (v3f
){1.0f
,0.0f
,0.0f
}, tn
->h
);
307 v3_copy( transform
[3], tn
->co
);
309 pnode
->sub_uid
= world
.traffic_count
++;
313 traffic_finalize( world
.traffic
, world
.traffic_count
);
314 for( int i
=0; i
<vg_list_size(world
.van_man
); i
++ )
315 world
.van_man
[i
].current
=&world
.traffic
[vg_randint(world
.traffic_count
)];
317 world_apply_foliage();
321 * Rendering the depth map
327 v3_sub( world
.geo
.bbx
[1], world
.geo
.bbx
[0], extent
);
329 float fl
= world
.geo
.bbx
[0][0],
330 fr
= world
.geo
.bbx
[1][0],
331 fb
= world
.geo
.bbx
[0][2],
332 ft
= world
.geo
.bbx
[1][2],
337 ortho
[0][0] = 2.0f
* rl
;
338 ortho
[2][1] = 2.0f
* tb
;
339 ortho
[3][0] = (fr
+ fl
) * -rl
;
340 ortho
[3][1] = (ft
+ fb
) * -tb
;
342 m4x3_identity( camera
);
344 glViewport( 0, 0, 1024, 1024 );
345 glDisable(GL_DEPTH_TEST
);
346 glBindFramebuffer( GL_FRAMEBUFFER
, gpipeline
.fb_depthmap
);
347 shader_fscolour_use();
348 shader_fscolour_uColour( (v4f
){-9999.0f
,-9999.0f
,-9999.0f
,-9999.0f
} );
352 glBlendFunc(GL_ONE
, GL_ONE
);
353 glBlendEquation(GL_MAX
);
354 render_world_depth( ortho
, camera
);
356 glEnable(GL_DEPTH_TEST
);
359 * TODO: World settings entity
361 struct ub_world_lighting
*winfo
= &gpipeline
.ub_world_lighting
;
362 v4_copy( wrender
.plane
, winfo
->g_water_plane
);
365 bounds
[0] = world
.geo
.bbx
[0][0];
366 bounds
[1] = world
.geo
.bbx
[0][2];
367 bounds
[2] = 1.0f
/ (world
.geo
.bbx
[1][0]-world
.geo
.bbx
[0][0]);
368 bounds
[3] = 1.0f
/ (world
.geo
.bbx
[1][2]-world
.geo
.bbx
[0][2]);
369 v4_copy( bounds
, winfo
->g_depth_bounds
);
371 winfo
->g_water_fog
= 0.04f
;
372 render_update_lighting_ub();
375 world
.mr_ball
.type
= k_rb_shape_sphere
;
376 world
.mr_ball
.inf
.sphere
.radius
= 2.0f
;
377 v3_copy( (v3f
){ 0.0f
, 110.0f
, 0.0f
}, world
.mr_ball
.co
);
379 q_identity(world
.mr_ball
.q
);
380 rb_init( &world
.mr_ball
);
383 static void world_init(void)
385 vg_tex2d_init( (vg_tex2d
*[]){ &tex_terrain_colours
,
386 &tex_terrain_noise
}, 2 );
389 mdl_header
*msky
= mdl_load("models/rs_skydome.mdl");
390 mdl_unpack_glmesh( msky
, &world
.skydome
);
392 mdl_node
*nlower
= mdl_node_from_name( msky
, "dome_lower" ),
393 *nupper
= mdl_node_from_name( msky
, "dome_upper" );
395 world
.dome_lower
= *mdl_node_submesh( msky
, nlower
, 0 );
396 world
.dome_upper
= *mdl_node_submesh( msky
, nupper
, 0 );
399 mdl_header
*mcars
= mdl_load( "models/rs_cars.mdl" );
400 mdl_unpack_glmesh( mcars
, &world
.cars
);
401 mdl_node
*nholden
= mdl_node_from_name( mcars
, "holden" );
402 world
.car_holden
= *mdl_node_submesh( mcars
, nholden
, 0 );
406 static void world_update(void)
409 rb_build_manifold_terrain_sphere( &world
.mr_ball
);
411 for( int i
=0; i
<5; i
++ )
412 rb_solve_contacts( rb_contact_buffer
, rb_contact_count
);
414 rb_iter( &world
.mr_ball
);
415 rb_update_transform( &world
.mr_ball
);
416 rb_debug( &world
.mr_ball
, 0 );
418 for( int i
=0; i
<vg_list_size(world
.van_man
); i
++ )
420 traffic_drive( &world
.van_man
[i
] );
421 traffic_visualize_car( &world
.van_man
[i
] );
429 static void bind_terrain_textures(void)
431 vg_tex2d_bind( &tex_terrain_noise
, 0 );
432 vg_tex2d_bind( &tex_terrain_colours
, 1 );
435 static void render_world_vb( m4x4f projection
, v3f camera
)
437 m4x3f identity_matrix
;
438 m4x3_identity( identity_matrix
);
441 shader_vblend_uTexGarbage(0);
442 shader_vblend_uTexGradients(1);
443 shader_link_standard_ub( _shader_vblend
.id
, 2 );
444 bind_terrain_textures();
446 shader_vblend_uPv( projection
);
447 shader_vblend_uMdl( identity_matrix
);
448 shader_vblend_uCamera( camera
);
450 scene_bind( &world
.geo
);
451 mdl_draw_submesh( &world
.sm_geo_vb
);
453 mesh_bind( &world
.cars
);
455 for( int i
=0; i
<vg_list_size(world
.van_man
); i
++ )
457 shader_vblend_uMdl( world
.van_man
[i
].transform
);
458 mdl_draw_submesh( &world
.car_holden
);
462 static void render_terrain( m4x4f projection
, v3f camera
)
464 m4x3f identity_matrix
;
465 m4x3_identity( identity_matrix
);
467 shader_terrain_use();
468 shader_terrain_uTexGarbage(0);
469 shader_terrain_uTexGradients(1);
470 shader_link_standard_ub( _shader_terrain
.id
, 2 );
471 bind_terrain_textures();
473 shader_terrain_uPv( projection
);
474 shader_terrain_uMdl( identity_matrix
);
475 shader_terrain_uCamera( camera
);
477 scene_bind( &world
.geo
);
478 mdl_draw_submesh( &world
.sm_geo_std_oob
);
479 mdl_draw_submesh( &world
.sm_geo_std
);
481 glDisable(GL_CULL_FACE
);
482 scene_bind( &world
.foliage
);
483 scene_draw( &world
.foliage
);
484 glEnable(GL_CULL_FACE
);
487 static void render_lowerdome( m4x3f camera
)
489 m4x4f projection
, full
;
490 pipeline_projection( projection
, 0.4f
, 1000.0f
);
493 m3x3_transpose( camera
, inverse
);
494 v3_copy((v3f
){0.0f
,0.0f
,0.0f
}, inverse
[3]);
495 m4x3_expand( inverse
, full
);
496 m4x4_mul( projection
, full
, full
);
498 m4x3f identity_matrix
;
499 m4x3_identity( identity_matrix
);
501 shader_planeinf_use();
502 shader_planeinf_uMdl(identity_matrix
);
503 shader_planeinf_uPv(full
);
504 shader_planeinf_uCamera(camera
[3]);
505 shader_planeinf_uPlane( (v4f
){0.0f
,1.0f
,0.0f
, water_height()} );
507 mdl_draw_submesh( &world
.dome_lower
);
510 static void render_sky(m4x3f camera
)
512 m4x4f projection
, full
;
513 pipeline_projection( projection
, 0.4f
, 1000.0f
);
516 m3x3_transpose( camera
, inverse
);
517 v3_copy((v3f
){0.0f
,0.0f
,0.0f
}, inverse
[3]);
518 m4x3_expand( inverse
, full
);
519 m4x4_mul( projection
, full
, full
);
521 m4x3f identity_matrix
;
522 m4x3_identity( identity_matrix
);
525 shader_sky_uMdl(identity_matrix
);
526 shader_sky_uPv(full
);
527 shader_sky_uTexGarbage(0);
528 shader_sky_uTime( vg_time
);
530 vg_tex2d_bind( &tex_terrain_noise
, 0 );
532 glDepthMask( GL_FALSE
);
533 glDisable( GL_DEPTH_TEST
);
535 mesh_bind( &world
.skydome
);
536 mdl_draw_submesh( &world
.dome_upper
);
538 glEnable( GL_DEPTH_TEST
);
539 glDepthMask( GL_TRUE
);
542 static void render_world( m4x4f projection
, m4x3f camera
)
544 render_sky( camera
);
545 render_world_vb( projection
, camera
[3] );
546 render_terrain( projection
, camera
[3] );
549 static void render_world_depth( m4x4f projection
, m4x3f camera
)
551 m4x3f identity_matrix
;
552 m4x3_identity( identity_matrix
);
555 shader_gpos_uCamera( camera
[3] );
556 shader_gpos_uPv( projection
);
557 shader_gpos_uMdl( identity_matrix
);
559 scene_bind( &world
.geo
);
560 scene_draw( &world
.geo
);
563 glDisable(GL_CULL_FACE
);
564 scene_bind( &world
.foliage
);
565 scene_draw( &world
.foliage
);
566 glEnable(GL_CULL_FACE
);