medfall

A super great game engine
Log | Files | Refs

commit be3368ecb79f599591e5b6a03e1f22847d2a3baf
parent fea1122dfe23c5cb28e8b901071643933b7c1550
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sat, 27 Oct 2018 12:27:35 +0300

Add support for raven BSP

Diffstat:
bsp.cc | 58+++++++++++++++++++++++++++++++++++++++++++++++++++-------
bsp.h | 52++++++++++++++++++++++++++++++++++++++++++++++++----
bsp_renderer.cc | 8++++----
3 files changed, 103 insertions(+), 15 deletions(-)

diff --git a/bsp.cc b/bsp.cc @@ -32,6 +32,10 @@ float point_plane_distance( v3 point, v3 normal, float d ) { void bsp_init( BSP * bsp, const char * filename ) { bsp->contents = file_get_contents( filename, NULL ); + const BSP_Header * header = ( const BSP_Header * ) bsp->contents; + + bool ibsp = header->magic == 0x50534249; + bsp->load_lump( bsp->num_textures, bsp->textures, LUMP_TEXTURES ); bsp->load_lump( bsp->num_planes, bsp->planes, LUMP_PLANES ); bsp->load_lump( bsp->num_nodes, bsp->nodes, LUMP_NODES ); @@ -39,15 +43,33 @@ void bsp_init( BSP * bsp, const char * filename ) { bsp->load_lump( bsp->num_leaf_faces, bsp->leaf_faces, LUMP_LEAFFACES ); bsp->load_lump( bsp->num_leaf_brushes, bsp->leaf_brushes, LUMP_LEAFBRUSHES ); bsp->load_lump( bsp->num_brushes, bsp->brushes, LUMP_BRUSHES ); - bsp->load_lump( bsp->num_brush_sides, bsp->brush_sides, LUMP_BRUSHSIDES ); - bsp->load_lump( bsp->num_vertices, bsp->vertices, LUMP_VERTICES ); bsp->load_lump( bsp->num_mesh_verts, bsp->mesh_verts, LUMP_MESHVERTS ); - bsp->load_lump( bsp->num_faces, bsp->faces, LUMP_FACES ); + + if( ibsp ) { + bsp->load_lump( bsp->num_brush_sides, bsp->brush_sides, LUMP_BRUSHSIDES ); + bsp->load_lump( bsp->num_vertices, bsp->vertices, LUMP_VERTICES ); + bsp->load_lump( bsp->num_faces, bsp->faces, LUMP_FACES ); + bsp->rbsp_brush_sides = NULL; + bsp->rbsp_vertices = NULL; + bsp->rbsp_faces = NULL; + } + else { + bsp->load_lump( bsp->num_brush_sides, bsp->rbsp_brush_sides, LUMP_BRUSHSIDES ); + bsp->load_lump( bsp->num_vertices, bsp->rbsp_vertices, LUMP_VERTICES ); + bsp->load_lump( bsp->num_faces, bsp->rbsp_faces, LUMP_FACES ); + bsp->fixup_rbsp(); + } + bsp->load_vis(); } void bsp_destroy( BSP * bsp ) { free( bsp->contents ); + if( bsp->rbsp_brush_sides != NULL ) { + free( bsp->brush_sides ); + free( bsp->vertices ); + free( bsp->faces ); + } } template< typename T > @@ -63,6 +85,27 @@ void BSP::load_lump( u32 & num_ts, T *& ts, const BSP_Lump lump ) { printf( "%d: ok. off %u len %u num %u\n", lump, hl.off, hl.len, num_ts ); } +void BSP::fixup_rbsp() { + vertices = malloc_array< BSP_Vertex >( num_vertices ); + for( u32 i = 0; i < num_vertices; i++ ) { + vertices[ i ].pos = rbsp_vertices[ i ].pos; + } + + brush_sides = malloc_array< BSP_BrushSide >( num_brush_sides ); + for( u32 i = 0; i < num_brush_sides; i++ ) { + brush_sides[ i ].plane = rbsp_brush_sides[ i ].plane; + } + + faces = malloc_array< BSP_Face >( num_faces ); + for( u32 i = 0; i < num_faces; i++ ) { + faces[ i ].first_vert = rbsp_faces[ i ].first_vert; + faces[ i ].num_verts = rbsp_faces[ i ].num_verts; + faces[ i ].first_mesh_vert = rbsp_faces[ i ].first_mesh_vert; + faces[ i ].num_mesh_verts = rbsp_faces[ i ].num_mesh_verts; + faces[ i ].normal = rbsp_faces[ i ].normal; + } +} + void BSP::load_vis() { const BSP_Header * header = reinterpret_cast< BSP_Header * >( contents ); const BSP_HeaderLump & hl = header->lumps[ LUMP_VISIBILITY ]; @@ -248,7 +291,7 @@ BSP_Leaf & BSP::position_to_leaf( v3 pos ) const { } GAME_INIT( game_init ) { - bsp_init( &game->bsp, "assets/acidwdm2.bsp" ); + bsp_init( &game->bsp, "assets/bomb_carentan.bsp" ); MemoryArena arena = memarena_push_arena( &mem->persistent_arena, megabytes( 10 ) ); @@ -302,9 +345,10 @@ GAME_FRAME( game_frame ) { const v3 right = normalize( cross( forward, world_up ) ); const v3 up = normalize( cross( right, forward ) ); - game->pos += forward * 100.0f * dt * fb; - game->pos += right * 100.0f * dt * lr; - game->pos.z += dz * 100.0f * dt; + float speed = 320.0f; + game->pos += forward * speed * dt * fb; + game->pos += right * speed * dt * lr; + game->pos.z += dz * speed * dt; m4 P = m4_perspective( VERTICAL_FOV, get_aspect_ratio(), NEAR_PLANE_DEPTH, FAR_PLANE_DEPTH ); m4 V = m4_view( forward, right, up, game->pos ); diff --git a/bsp.h b/bsp.h @@ -31,7 +31,7 @@ struct BSP_HeaderLump { }; struct BSP_Header { - char ibsp[ 4 ]; + u32 magic; u32 version; BSP_HeaderLump lumps[ LUMP_MAX ]; @@ -94,13 +94,28 @@ struct BSP_BrushSide { s32 texture; }; +struct RBSP_BrushSide { + u32 plane; + s32 texture; + s32 surface; +}; + struct BSP_Vertex { v3 pos; - float uv[ 2 ][ 2 ]; + v2 albedo_uv; + v2 lightmap_uv; v3 normal; u8 rgba[ 4 ]; }; +struct RBSP_Vertex { + v3 pos; + v2 albedo_uv; + v2 lightmap_uv[ 4 ]; + v3 normal; + u8 rgba[ 4 ][ 4 ]; +}; + typedef s32 BSP_MeshVert; // LUMP_FOG @@ -116,8 +131,33 @@ struct BSP_Face { s32 num_mesh_verts; s32 lightmap; - s32 lmpos[ 2 ]; - s32 lmsize[ 2 ]; + v2s32 lmpos; + v2s32 lmsize; + v3 lmorigin; + v3 a; + v3 b; + + v3 normal; + + s32 c; s32 d; +}; + +struct RBSP_Face { + s32 texture; + s32 effect; + s32 type; + + s32 first_vert; + s32 num_verts; + s32 first_mesh_vert; + s32 num_mesh_verts; + + u8 lightmap_styles[ 4 ]; + u8 vertex_styles[ 4 ]; + + s32 lightmap[ 4 ]; + v2s32 lmpos[ 4 ]; + v2s32 lmsize; v3 lmorigin; v3 a; v3 b; @@ -180,19 +220,23 @@ public: // TODO u32 num_brush_sides; BSP_BrushSide * brush_sides; + RBSP_BrushSide * rbsp_brush_sides; u32 num_vertices; BSP_Vertex * vertices; + RBSP_Vertex * rbsp_vertices; u32 num_mesh_verts; BSP_MeshVert * mesh_verts; u32 num_faces; BSP_Face * faces; + RBSP_Face * rbsp_faces; BSP_Vis * vis; template< typename T > void load_lump( u32 & num_ts, T *& ts, BSP_Lump lump ); + void fixup_rbsp(); void load_vis(); bool trace_seg_brush( const BSP_Brush & brush, v3 start, v3 dir, float tmin, float tmax, float * tout ) const; diff --git a/bsp_renderer.cc b/bsp_renderer.cc @@ -63,10 +63,10 @@ void bspr_init( BSPRenderer * bspr, MemoryArena * arena, const BSP * bsp ) { bspr->meshes = memarena_push_many( arena, Mesh, bsp->num_leaves ); MEMARENA_SCOPED_CHECKPOINT( arena ); - array< v3 > positions = memarena_push_array( arena, v3, 2048 ); - array< v3 > normals = memarena_push_array( arena, v3, 2048 ); - array< v3 > colours = memarena_push_array( arena, v3, 2048 ); - array< u32 > indices = memarena_push_array( arena, u32, 2048 ); + array< v3 > positions = memarena_push_array( arena, v3, 16384 ); + array< v3 > normals = memarena_push_array( arena, v3, 16384 ); + array< v3 > colours = memarena_push_array( arena, v3, 16384 ); + array< u32 > indices = memarena_push_array( arena, u32, 16384 ); size_t num_positions, num_normals, num_colours, num_indices; for( u32 l = 0; l < bsp->num_leaves; l++ ) {