bsp.h (3706B)
1 #pragma once 2 3 #include "intrinsics.h" 4 #include "linear_algebra.h" 5 6 enum BSP_Lump { 7 LUMP_ENTITIES, 8 LUMP_TEXTURES, 9 LUMP_PLANES, 10 LUMP_NODES, 11 LUMP_LEAVES, 12 LUMP_LEAFFACES, 13 LUMP_LEAFBRUSHES, 14 LUMP_MODELS, 15 LUMP_BRUSHES, 16 LUMP_BRUSHSIDES, 17 LUMP_VERTICES, 18 LUMP_MESHVERTS, 19 LUMP_FOGS, 20 LUMP_FACES, 21 LUMP_LIGHTING, 22 LUMP_LIGHTGRID, 23 LUMP_VISIBILITY, 24 LUMP_LIGHTARRAY, 25 LUMP_MAX, 26 }; 27 28 struct BSP_HeaderLump { 29 u32 off; 30 u32 len; 31 }; 32 33 struct BSP_Header { 34 u32 magic; 35 u32 version; 36 37 BSP_HeaderLump lumps[ LUMP_MAX ]; 38 }; 39 40 struct BSP_Texture { 41 char name[ 64 ]; 42 s32 surface_flags; 43 s32 content_flags; 44 }; 45 46 struct BSP_Plane { 47 v3 n; 48 float d; 49 }; 50 51 struct BSP_Node { 52 u32 plane; 53 54 union { 55 s32 children[ 2 ]; 56 struct { 57 s32 pos_child; 58 s32 neg_child; 59 }; 60 }; 61 62 s32 mins[ 3 ]; 63 s32 maxs[ 3 ]; 64 }; 65 66 struct BSP_Leaf { 67 s32 cluster; 68 s32 area; 69 70 s32 mins[ 3 ]; 71 s32 maxs[ 3 ]; 72 73 u32 first_face; 74 u32 num_faces; 75 76 u32 first_brush; 77 u32 num_brushes; 78 }; 79 80 typedef u32 BSP_LeafFace; 81 typedef u32 BSP_LeafBrush; 82 83 struct BSP_MapModel { 84 v3 mins, maxs; 85 s32 first_face; 86 s32 num_faces; 87 s32 first_brush; 88 s32 num_brushes; 89 }; 90 91 struct BSP_Brush { 92 u32 first_side; 93 u32 num_sides; 94 95 s32 texture; 96 }; 97 98 struct BSP_BrushSide { 99 u32 plane; 100 s32 texture; 101 }; 102 103 struct RBSP_BrushSide { 104 u32 plane; 105 s32 texture; 106 s32 surface; 107 }; 108 109 struct BSP_Vertex { 110 v3 pos; 111 v2 albedo_uv; 112 v2 lightmap_uv; 113 v3 normal; 114 u8 rgba[ 4 ]; 115 }; 116 117 struct RBSP_Vertex { 118 v3 pos; 119 v2 albedo_uv; 120 v2 lightmap_uv[ 4 ]; 121 v3 normal; 122 u8 rgba[ 4 ][ 4 ]; 123 }; 124 125 typedef s32 BSP_MeshVert; 126 127 // LUMP_FOG 128 129 struct BSP_Face { 130 s32 texture; 131 s32 effect; 132 s32 type; 133 134 s32 first_vert; 135 s32 num_verts; 136 s32 first_mesh_vert; 137 s32 num_mesh_verts; 138 139 s32 lightmap; 140 v2s32 lmpos; 141 v2s32 lmsize; 142 v3 lmorigin; 143 v3 a; 144 v3 b; 145 146 v3 normal; 147 148 s32 c; s32 d; 149 }; 150 151 struct RBSP_Face { 152 s32 texture; 153 s32 effect; 154 s32 type; 155 156 s32 first_vert; 157 s32 num_verts; 158 s32 first_mesh_vert; 159 s32 num_mesh_verts; 160 161 u8 lightmap_styles[ 4 ]; 162 u8 vertex_styles[ 4 ]; 163 164 s32 lightmap[ 4 ]; 165 v2s32 lmpos[ 4 ]; 166 v2s32 lmsize; 167 v3 lmorigin; 168 v3 a; 169 v3 b; 170 171 v3 normal; 172 173 s32 c; s32 d; 174 }; 175 176 // LUMP_LIGHTING 177 // LUMP_LIGHTGRID 178 179 struct BSP_Vis { 180 u32 num_clusters; 181 u32 cluster_size; 182 183 u8 * pvs; 184 }; 185 186 // LUMP_LIGHTARRAY 187 188 struct Intersection { 189 const BSP_Plane * plane; 190 v3 pos; 191 float t; 192 }; 193 194 struct BSP_Intersection { 195 Intersection is; 196 v3 start; 197 v3 end; 198 bool hit; 199 }; 200 201 class BSP { 202 private: 203 public: // TODO 204 u8 * contents; 205 206 u32 num_textures; 207 BSP_Texture * textures; 208 209 u32 num_planes; 210 BSP_Plane * planes; 211 212 u32 num_nodes; 213 BSP_Node * nodes; 214 215 u32 num_leaves; 216 BSP_Leaf * leaves; 217 218 u32 num_leaf_faces; 219 BSP_LeafFace * leaf_faces; 220 221 u32 num_leaf_brushes; 222 BSP_LeafBrush * leaf_brushes; 223 224 u32 num_map_models; 225 BSP_MapModel * map_models; 226 227 u32 num_brushes; 228 BSP_Brush * brushes; 229 230 u32 num_brush_sides; 231 BSP_BrushSide * brush_sides; 232 RBSP_BrushSide * rbsp_brush_sides; 233 234 u32 num_vertices; 235 BSP_Vertex * vertices; 236 RBSP_Vertex * rbsp_vertices; 237 238 u32 num_mesh_verts; 239 BSP_MeshVert * mesh_verts; 240 241 u32 num_faces; 242 BSP_Face * faces; 243 RBSP_Face * rbsp_faces; 244 245 BSP_Vis * vis; 246 247 template< typename T > void load_lump( u32 & num_ts, T *& ts, BSP_Lump lump ); 248 void fixup_rbsp(); 249 void load_vis(); 250 251 bool trace_seg_brush( const BSP_Brush & brush, v3 start, v3 dir, float tmin, float tmax, float * tout ) const; 252 void trace_seg_leaf( u32 leaf_idx, v3 start, v3 dir, float tmin, float tmax, BSP_Intersection & bis ) const; 253 void trace_seg_tree( s32 node_idx, v3 start, v3 dir, float tmin, float tmax, BSP_Intersection & bis ) const; 254 255 BSP_Leaf & position_to_leaf( v3 pos ) const; 256 257 public: 258 bool trace_seg( v3 start, v3 end, Intersection & is ) const; 259 }; 260 261 void bsp_init( BSP * bsp, const char * filename ); 262 void bsp_destroy( BSP * bsp );