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];
53 /* Rendering & geometry */
54 scene geo
, foliage
, props
;
55 mdl_submesh sm_surface
, sm_other
;
57 glmesh skybox
, skydome
;
58 mdl_submesh dome_upper
, dome_lower
;
61 mdl_submesh car_holden
;
65 vg_tex2d tex_terrain_colours
= { .path
= "textures/gradients.qoi",
66 .flags
= VG_TEXTURE_CLAMP
|VG_TEXTURE_NEAREST
};
68 vg_tex2d tex_terrain_noise
= { .path
= "textures/garbage.qoi",
69 .flags
= VG_TEXTURE_NEAREST
};
71 static void ray_world_get_tri( ray_hit
*hit
, v3f tri
[3] )
73 for( int i
=0; i
<3; i
++ )
74 v3_copy( world
.geo
.verts
[ hit
->tri
[i
] ].co
, tri
[i
] );
77 static int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
)
79 return scene_raycast( &world
.geo
, pos
, dir
, hit
);
82 static int ray_hit_is_ramp( ray_hit
*hit
)
84 return hit
->tri
[0] < world
.sm_surface
.vertex_count
;
87 static void world_register(void)
89 shader_terrain_register();
90 shader_sky_register();
91 shader_planeinf_register();
92 shader_gpos_register();
93 shader_fscolour_register();
96 static void world_free(void)
101 static void render_world_depth( m4x4f projection
, m4x3f camera
);
103 static void add_all_if_material( scene
*pscene
, mdl_header
*mdl
, u32 id
)
105 for( int i
=0; i
<mdl
->node_count
; i
++ )
107 mdl_node
*pnode
= mdl_node_from_id( mdl
, i
);
109 for( int j
=0; j
<pnode
->submesh_count
; j
++ )
111 mdl_submesh
*sm
= mdl_node_submesh( mdl
, pnode
, j
);
113 if( sm
->material_id
== id
)
116 mdl_node_transform( pnode
, transform
);
117 scene_add_submesh( pscene
, mdl
, sm
, transform
);
123 static void world_apply_foliage(void)
125 scene_init( &world
.foliage
);
126 mdl_header
*mfoliage
= mdl_load("models/rs_foliage.mdl");
129 v3_sub( world
.geo
.bbx
[1], world
.geo
.bbx
[0], volume
);
133 mdl_node
*mblob
= mdl_node_from_name( mfoliage
, "blob" );
134 mdl_submesh
*sm_blob
= mdl_node_submesh( mfoliage
, mblob
, 0 );
136 for( int i
=0;i
<100000;i
++ )
139 v3_mul( volume
, (v3f
){ vg_randf(), 1000.0f
, vg_randf() }, pos
);
141 v3_add( pos
, world
.geo
.bbx
[0], pos
);
146 if( ray_world( pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
148 if( hit
.normal
[1] > 0.8f
&& !ray_hit_is_ramp(&hit
) &&
149 hit
.pos
[1] > water_height()+10.0f
)
151 v4f qsurface
, qrandom
;
154 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, hit
.normal
, axis
);
156 float angle
= v3_dot(hit
.normal
,(v3f
){0.0f
,1.0f
,0.0f
});
157 q_axis_angle( qsurface
, axis
, angle
);
158 q_axis_angle( qrandom
, (v3f
){0.0f
,1.0f
,0.0f
}, vg_randf()*VG_TAUf
);
159 q_mul( qsurface
, qrandom
, qsurface
);
160 q_m3x3( qsurface
, transform
);
162 v3_copy( hit
.pos
, transform
[3] );
163 scene_add_submesh( &world
.foliage
, mfoliage
, sm_blob
, transform
);
168 scene_upload( &world
.foliage
);
172 static void world_load(void)
174 mdl_header
*mworld
= mdl_load( "models/mp_dev.mdl" );
176 world
.spawn_count
= 0;
177 world
.gate_count
= 0;
179 world
.traffic_count
= 0;
181 scene_init( &world
.geo
);
182 scene_init( &world
.props
);
185 * Compile meshes into the world scenes
189 mat_vertex_blend
= 0;
191 for( int i
=1; i
<mworld
->material_count
; i
++ )
193 mdl_material
*mat
= mdl_material_from_id( mworld
, i
);
194 const char *mat_name
= mdl_pstr( mworld
, mat
->pstr_name
);
196 vg_info( "%d %s\n", mat
->pstr_name
, mat_name
);
198 if( !strcmp( "surf", mat_name
))
200 else if( !strcmp( "surf_oob", mat_name
))
202 else if( !strcmp( "vertex_blend", mat_name
))
203 mat_vertex_blend
= i
;
207 add_all_if_material( &world
.geo
, mworld
, mat_surf
);
208 if( mat_vertex_blend
)
209 add_all_if_material( &world
.geo
, mworld
, mat_vertex_blend
);
212 scene_copy_slice( &world
.geo
, &world
.sm_surface
);
215 add_all_if_material( &world
.geo
, mworld
, mat_surf_oob
);
217 vg_warn( "No OOB surface\n" );
219 scene_bh_create( &world
.geo
);
220 scene_upload( &world
.geo
);
222 if( mat_vertex_blend
)
223 add_all_if_material( &world
.props
, mworld
, mat_vertex_blend
);
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_gate
)
238 struct classtype_gate
*entgate
= mdl_get_entdata( mworld
, pnode
);
240 if( entgate
->target
)
242 mdl_node
*pother
= mdl_node_from_id( mworld
, entgate
->target
);
244 teleport_gate
*gate
= &world
.gates
[ world
.gate_count
++ ];
246 v3_copy( pnode
->co
, gate
->co
[0] );
247 v3_copy( pother
->co
, gate
->co
[1] );
248 v4_copy( pnode
->q
, gate
->q
[0] );
249 v4_copy( pother
->q
, gate
->q
[1] );
250 v2_copy( pnode
->s
, gate
->dims
);
252 gate_transform_update( gate
);
255 else if( pnode
->classtype
== k_classtype_block
)
257 struct classtype_block
*block
= mdl_get_entdata( mworld
, pnode
);
260 mdl_node_transform( pnode
, transform
);
262 rigidbody
*rb
= &world
.temp_rbs
[ world
.rb_count
++ ];
264 box_copy( block
->bbx
, rb
->bbx
); /* TODO: apply scale */
265 v3_copy( pnode
->co
, rb
->co
);
267 v4_copy( pnode
->q
, rb
->q
);
268 rb_update_transform( rb
);
270 else if( pnode
->classtype
== k_classtype_spawn
)
272 struct respawn_point
*rp
= &world
.spawns
[ world
.spawn_count
++ ];
274 v3_copy( pnode
->co
, rp
->co
);
275 v4_copy( pnode
->q
, rp
->q
);
276 strcpy( rp
->name
, mdl_pstr( mworld
, pnode
->pstr_name
) );
278 else if( pnode
->classtype
== k_classtype_water
)
280 if( wrender
.enabled
)
282 vg_warn( "Multiple water surfaces in level! ('%s')\n",
283 mdl_pstr( mworld
, pnode
->pstr_name
));
287 mdl_submesh
*sm
= mdl_node_submesh( mworld
, pnode
, 0 );
292 mdl_unpack_submesh( mworld
, &surf
, sm
);
294 water_set_surface( &surf
, pnode
->co
[1] );
297 else if( pnode
->classtype
== k_classtype_car_path
)
299 struct classtype_car_path
*p
= mdl_get_entdata( mworld
, pnode
);
300 traffic_node
*tn
= &world
.traffic
[ world
.traffic_count
];
304 if( p
->target
) tn
->mn_next
= mdl_node_from_id( mworld
, p
->target
);
305 if( p
->target1
) tn
->mn_next1
= mdl_node_from_id( mworld
, p
->target1
);
308 mdl_node_transform( pnode
, transform
);
309 m3x3_mulv( transform
, (v3f
){1.0f
,0.0f
,0.0f
}, tn
->h
);
310 v3_copy( transform
[3], tn
->co
);
312 pnode
->sub_uid
= world
.traffic_count
++;
316 traffic_finalize( world
.traffic
, world
.traffic_count
);
317 for( int i
=0; i
<vg_list_size(world
.van_man
); i
++ )
318 world
.van_man
[i
].current
=&world
.traffic
[vg_randint(world
.traffic_count
)];
320 scene_upload( &world
.props
);
322 bh_create( &world
.bhcubes
,
323 &bh_system_rigidbodies
, world
.temp_rbs
, world
.rb_count
);
325 world_apply_foliage();
329 * Rendering the depth map
335 v3_sub( world
.geo
.bbx
[1], world
.geo
.bbx
[0], extent
);
337 float fl
= world
.geo
.bbx
[0][0],
338 fr
= world
.geo
.bbx
[1][0],
339 fb
= world
.geo
.bbx
[0][2],
340 ft
= world
.geo
.bbx
[1][2],
345 ortho
[0][0] = 2.0f
* rl
;
346 ortho
[2][1] = 2.0f
* tb
;
347 ortho
[3][0] = (fr
+ fl
) * -rl
;
348 ortho
[3][1] = (ft
+ fb
) * -tb
;
350 m4x3_identity( camera
);
352 glViewport( 0, 0, 1024, 1024 );
353 glDisable(GL_DEPTH_TEST
);
354 glBindFramebuffer( GL_FRAMEBUFFER
, gpipeline
.fb_depthmap
);
355 shader_fscolour_use();
356 shader_fscolour_uColour( (v4f
){-9999.0f
,-9999.0f
,-9999.0f
,-9999.0f
} );
360 glBlendFunc(GL_ONE
, GL_ONE
);
361 glBlendEquation(GL_MAX
);
362 render_world_depth( ortho
, camera
);
364 glEnable(GL_DEPTH_TEST
);
367 * TODO: World settings entity
369 struct ub_world_lighting
*winfo
= &gpipeline
.ub_world_lighting
;
370 v4_copy( wrender
.plane
, winfo
->g_water_plane
);
373 bounds
[0] = world
.geo
.bbx
[0][0];
374 bounds
[1] = world
.geo
.bbx
[0][2];
375 bounds
[2] = 1.0f
/ (world
.geo
.bbx
[1][0]-world
.geo
.bbx
[0][0]);
376 bounds
[3] = 1.0f
/ (world
.geo
.bbx
[1][2]-world
.geo
.bbx
[0][2]);
377 v4_copy( bounds
, winfo
->g_depth_bounds
);
379 winfo
->g_water_fog
= 0.04f
;
380 render_update_lighting_ub();
384 static void world_init(void)
386 vg_tex2d_init( (vg_tex2d
*[]){ &tex_terrain_colours
,
387 &tex_terrain_noise
}, 2 );
390 mdl_header
*msky
= mdl_load("models/rs_skydome.mdl");
391 mdl_unpack_glmesh( msky
, &world
.skydome
);
393 mdl_node
*nlower
= mdl_node_from_name( msky
, "dome_lower" ),
394 *nupper
= mdl_node_from_name( msky
, "dome_upper" );
396 world
.dome_lower
= *mdl_node_submesh( msky
, nlower
, 0 );
397 world
.dome_upper
= *mdl_node_submesh( msky
, nupper
, 0 );
400 mdl_header
*mcars
= mdl_load( "models/rs_cars.mdl" );
401 mdl_unpack_glmesh( mcars
, &world
.cars
);
402 mdl_node
*nholden
= mdl_node_from_name( mcars
, "holden" );
403 world
.car_holden
= *mdl_node_submesh( mcars
, nholden
, 0 );
407 static void world_update(void)
409 for( int i
=0; i
<vg_list_size(world
.van_man
); i
++ )
411 traffic_drive( &world
.van_man
[i
] );
412 traffic_visualize_car( &world
.van_man
[i
] );
420 static void bind_terrain_textures(void)
422 vg_tex2d_bind( &tex_terrain_noise
, 0 );
423 vg_tex2d_bind( &tex_terrain_colours
, 1 );
426 static void render_props( m4x4f projection
, v3f camera
)
428 m4x3f identity_matrix
;
429 m4x3_identity( identity_matrix
);
432 shader_vblend_uTexGarbage(0);
433 shader_vblend_uTexGradients(1);
434 shader_link_standard_ub( _shader_vblend
.id
, 2 );
435 bind_terrain_textures();
437 shader_vblend_uPv( projection
);
438 shader_vblend_uMdl( identity_matrix
);
439 shader_vblend_uCamera( camera
);
441 scene_bind( &world
.props
);
442 scene_draw( &world
.props
);
444 mesh_bind( &world
.cars
);
446 for( int i
=0; i
<vg_list_size(world
.van_man
); i
++ )
448 shader_vblend_uMdl( world
.van_man
[i
].transform
);
449 mdl_draw_submesh( &world
.car_holden
);
453 static void render_terrain( m4x4f projection
, v3f camera
)
455 m4x3f identity_matrix
;
456 m4x3_identity( identity_matrix
);
458 shader_terrain_use();
459 shader_terrain_uTexGarbage(0);
460 shader_terrain_uTexGradients(1);
461 shader_link_standard_ub( _shader_terrain
.id
, 2 );
462 bind_terrain_textures();
464 shader_terrain_uPv( projection
);
465 shader_terrain_uMdl( identity_matrix
);
466 shader_terrain_uCamera( camera
);
468 scene_bind( &world
.geo
);
469 scene_draw( &world
.geo
);
471 glDisable(GL_CULL_FACE
);
472 scene_bind( &world
.foliage
);
473 scene_draw( &world
.foliage
);
474 glEnable(GL_CULL_FACE
);
477 static void render_lowerdome( m4x3f camera
)
479 m4x4f projection
, full
;
480 pipeline_projection( projection
, 0.4f
, 1000.0f
);
483 m3x3_transpose( camera
, inverse
);
484 v3_copy((v3f
){0.0f
,0.0f
,0.0f
}, inverse
[3]);
485 m4x3_expand( inverse
, full
);
486 m4x4_mul( projection
, full
, full
);
488 m4x3f identity_matrix
;
489 m4x3_identity( identity_matrix
);
491 shader_planeinf_use();
492 shader_planeinf_uMdl(identity_matrix
);
493 shader_planeinf_uPv(full
);
494 shader_planeinf_uCamera(camera
[3]);
495 shader_planeinf_uPlane( (v4f
){0.0f
,1.0f
,0.0f
, water_height()} );
497 mdl_draw_submesh( &world
.dome_lower
);
500 static void render_sky(m4x3f camera
)
502 m4x4f projection
, full
;
503 pipeline_projection( projection
, 0.4f
, 1000.0f
);
506 m3x3_transpose( camera
, inverse
);
507 v3_copy((v3f
){0.0f
,0.0f
,0.0f
}, inverse
[3]);
508 m4x3_expand( inverse
, full
);
509 m4x4_mul( projection
, full
, full
);
511 m4x3f identity_matrix
;
512 m4x3_identity( identity_matrix
);
515 shader_sky_uMdl(identity_matrix
);
516 shader_sky_uPv(full
);
517 shader_sky_uTexGarbage(0);
518 shader_sky_uTime( vg_time
);
520 vg_tex2d_bind( &tex_terrain_noise
, 0 );
522 glDepthMask( GL_FALSE
);
523 glDisable( GL_DEPTH_TEST
);
525 mesh_bind( &world
.skydome
);
526 mdl_draw_submesh( &world
.dome_upper
);
528 glEnable( GL_DEPTH_TEST
);
529 glDepthMask( GL_TRUE
);
532 static void render_world( m4x4f projection
, m4x3f camera
)
534 render_sky( camera
);
535 render_props( projection
, camera
[3] );
536 render_terrain( projection
, camera
[3] );
539 static void render_world_depth( m4x4f projection
, m4x3f camera
)
541 m4x3f identity_matrix
;
542 m4x3_identity( identity_matrix
);
545 shader_gpos_uCamera( camera
[3] );
546 shader_gpos_uPv( projection
);
547 shader_gpos_uMdl( identity_matrix
);
549 scene_bind( &world
.geo
);
550 scene_draw( &world
.geo
);
553 glDisable(GL_CULL_FACE
);
554 scene_bind( &world
.foliage
);
555 scene_draw( &world
.foliage
);
556 glEnable(GL_CULL_FACE
);
559 scene_bind( &world
.props
);
560 scene_draw( &world
.props
);