1 #ifndef __VBSP_READER_H_
2 #define __VBSP_READER_H_
3 
4 
5 #include <osg/Geometry>
6 #include <osg/Node>
7 #include <osg/Object>
8 #include <osg/StateSet>
9 #include <osgDB/Registry>
10 #include <osgDB/FileNameUtils>
11 #include <osg/Referenced>
12 
13 #include "VBSPData.h"
14 
15 namespace bsp
16 {
17 
18 
19 // The magic number for a Valve BSP file is 'VBSP' in little-endian
20 // order
21 const int MAGIC_NUMBER = (('P'<<24)+('S'<<16)+('B'<<8)+'V');
22 
23 
24 enum LumpType
25 {
26     ENTITIES_LUMP,
27     PLANES_LUMP,
28     TEXDATA_LUMP,
29     VERTICES_LUMP,
30     VISIBILITY_LUMP,
31     NODES_LUMP,
32     TEXINFO_LUMP,
33     FACES_LUMP,
34     LIGHTING_LUMP,
35     OCCLUSION_LUMP,
36     LEAFS_LUMP,
37     UNUSED_LUMP_11,
38     EDGES_LUMP,
39     SURFEDGES_LUMP,
40     MODELS_LUMP,
41     WORLD_LIGHTS_LUMP,
42     LEAF_FACES_LUMP,
43     LEAF_BRUSHES_LUMP,
44     BRUSHES_LUMP,
45     BRUSH_SIDES_LUMP,
46     AREAS_LUMP,
47     AREA_PORTALS_LUMP,
48     PORTALS_LUMP,
49     CLUSTERS_LUMP,
50     PORTAL_VERTS_LUMP,
51     CLUSTER_PORTALS_LUMP,
52     DISPINFO_LUMP,
53     ORIGINAL_FACES_LUMP,
54     UNUSED_LUMP_28,
55     PHYS_COLLIDE_LUMP,
56     VERT_NORMALS_LUMP,
57     VERT_NORMAL_INDICES_LUMP,
58     DISP_LIGHTMAP_ALPHAS_LUMP,
59     DISP_VERTS_LUMP,
60     DISP_LIGHTMAP_SAMPLE_POS_LUMP,
61     GAME_LUMP,
62     LEAF_WATER_DATA_LUMP,
63     PRIMITIVES_LUMP,
64     PRIM_VERTS_LUMP,
65     PRIM_INDICES_LUMP,
66     PAK_FILE_LUMP,
67     CLIP_PORTAL_VERTS_LUMP,
68     CUBEMAPS_LUMP,
69     TEXDATA_STRING_DATA_LUMP,
70     TEXDATA_STRING_TABLE_LUMP,
71     OVERLAYS_LUMP,
72     LEAF_MIN_DIST_TO_WATER_LUMP,
73     FACE_MACRO_TEXTURE_INFO_LUMP,
74     DISP_TRIS_LUMP,
75     PHYS_COLLIDE_SURFACE_LUMP,
76     UNUSED_LUMP_50,
77     UNUSED_LUMP_51,
78     UNUSED_LUMP_52,
79     LIGHTING_HDR_LUMP,
80     WORLD_LIGHTS_HDR_LUMP,
81     LEAF_LIGHT_HDR_1_LUMP,
82     LEAF_LIGHT_HDR_2_LUMP,
83     UNUSED_LUMP_57,
84     UNUSED_LUMP_58,
85     UNUSED_LUMP_59,
86     UNUSED_LUMP_60,
87     UNUSED_LUMP_61,
88     UNUSED_LUMP_62,
89     UNUSED_LUMP_63,
90     MAX_LUMPS
91 };
92 
93 
94 const char LUMP_DESCRIPTION[][64] =
95 {
96     "ENTITIES_LUMP",
97     "PLANES_LUMP",
98     "TEXDATA_LUMP",
99     "VERTICES_LUMP",
100     "VISIBILITY_LUMP",
101     "NODES_LUMP",
102     "TEXINFO_LUMP",
103     "FACES_LUMP",
104     "LIGHTING_LUMP",
105     "OCCLUSION_LUMP",
106     "LEAFS_LUMP",
107     "UNUSED_LUMP_11",
108     "EDGES_LUMP",
109     "SURFEDGES_LUMP",
110     "MODELS_LUMP",
111     "WORLD_LIGHTS_LUMP",
112     "LEAF_FACES_LUMP",
113     "LEAF_BRUSHES_LUMP",
114     "BRUSHES_LUMP",
115     "BRUSH_SIDES_LUMP",
116     "AREAS_LUMP",
117     "AREA_PORTALS_LUMP",
118     "PORTALS_LUMP",
119     "CLUSTERS_LUMP",
120     "PORTAL_VERTS_LUMP",
121     "CLUSTER_PORTALS_LUMP",
122     "DISPINFO_LUMP",
123     "ORIGINAL_FACES_LUMP",
124     "UNUSED_LUMP_28",
125     "PHYS_COLLIDE_LUMP",
126     "VERT_NORMALS_LUMP",
127     "VERT_NORMAL_INDICES_LUMP",
128     "DISP_LIGHTMAP_ALPHAS_LUMP",
129     "DISP_VERTS_LUMP",
130     "DISP_LIGHTMAP_SAMPLE_POS_LUMP",
131     "GAME_LUMP",
132     "LEAF_WATER_DATA_LUMP",
133     "PRIMITIVES_LUMP",
134     "PRIM_VERTS_LUMP",
135     "PRIM_INDICES_LUMP",
136     "PAK_FILE_LUMP",
137     "CLIP_PORTAL_VERTS_LUMP",
138     "CUBEMAPS_LUMP",
139     "TEXDATA_STRING_DATA_LUMP",
140     "TEXDATA_STRING_TABLE_LUMP",
141     "OVERLAYS_LUMP",
142     "LEAF_MIN_DIST_TO_WATER_LUMP",
143     "FACE_MACRO_TEXTURE_INFO_LUMP",
144     "DISP_TRIS_LUMP",
145     "PHYS_COLLIDE_SURFACE_LUMP",
146     "UNUSED_LUMP_50",
147     "UNUSED_LUMP_51",
148     "UNUSED_LUMP_52",
149     "LIGHTING_HDR_LUMP",
150     "WORLD_LIGHTS_HDR_LUMP",
151     "LEAF_LIGHT_HDR_1_LUMP",
152     "LEAF_LIGHT_HDR_2_LUMP",
153     "UNUSED_LUMP_57",
154     "UNUSED_LUMP_58",
155     "UNUSED_LUMP_59",
156     "UNUSED_LUMP_60",
157     "UNUSED_LUMP_61",
158     "UNUSED_LUMP_62",
159     "UNUSED_LUMP_63"
160 };
161 
162 
163 struct LumpEntry
164 {
165     int   file_offset;
166     int   lump_length;
167     int   lump_version;
168     char  ident_code[4];
169 };
170 
171 
172 struct Header
173 {
174     int        magic_number;
175     int        bsp_version;
176     LumpEntry  lump_table[MAX_LUMPS];
177     int        map_revision;
178 };
179 
180 
181 struct GameHeader
182 {
183     int              num_lumps;
184 
185     // This is followed by this many GameLump entries (see below)
186 };
187 
188 
189 struct GameLump
190 {
191     int               lump_id;
192     unsigned short    lump_flags;
193     unsigned short    lump_version;
194     int               lump_offset;
195     int               lump_length;
196 };
197 
198 
199 // This is the ID for the static prop game lump
200 const int STATIC_PROP_ID = (('s'<<24)+('p'<<16)+('r'<<8)+'p');
201 
202 
203 struct StaticPropModelNames
204 {
205     int    num_model_names;
206 
207     // This is followed by this many names, each 128 characters long
208 };
209 
210 
211 struct StaticPropLeaves
212 {
213     int   num_leaf_entries;
214 
215     // This is followed by this many unsigned shorts, indicating which BSP
216     // leaves this prop occupies
217 };
218 
219 
220 struct StaticProps
221 {
222     int   num_static_props;
223 
224     // This is followed by this many StaticProp entries (see VBSPData.h), note
225     // that there are two possible StaticProp versions, depending on the
226     // version of the GameLump
227 };
228 
229 
230 class VBSPReader
231 {
232 protected:
233 
234     std::string                map_name;
235 
236     osg::ref_ptr<VBSPData>     bsp_data;
237 
238     osg::ref_ptr<osg::Node>    root_node;
239 
240     char *                     texdata_string;
241     int *                      texdata_string_table;
242     int                        num_texdata_string_table_entries;
243 
244 
245     void   processEntities(std::istream & str, int offset, int length);
246     void   processModels(std::istream & str, int offset, int length);
247     void   processPlanes(std::istream & str, int offset, int length);
248     void   processVertices(std::istream & str, int offset, int length);
249     void   processEdges(std::istream & str, int offset, int length);
250     void   processSurfEdges(std::istream & str, int offset, int length);
251     void   processFaces(std::istream & str, int offset, int length);
252     void   processTexInfo(std::istream & str, int offset, int length);
253     void   processTexData(std::istream & str, int offset, int length);
254     void   processTexDataStringTable(std::istream & str, int offset,
255                                      int length);
256     void   processTexDataStringData(std::istream & str, int offset, int length);
257     void   processDispInfo(std::istream & str, int offset, int length);
258     void   processDispVerts(std::istream & str, int offset, int length);
259     void   processGameData(std::istream & str, int offset, int length);
260     void   processStaticProps(std::istream & str, int offset, int length,
261                               int lumpVersion);
262 
263     std::string       getToken(std::string str, const char * delim,
264                                size_t & index);
265 
266     osg::ref_ptr<osg::StateSet>   createBlendShader(osg::Texture * tex1,
267                                                     osg::Texture * tex2);
268 
269     osg::ref_ptr<osg::Texture>    readTextureFile(std::string file);
270     osg::ref_ptr<osg::StateSet>   readMaterialFile(std::string file);
271 
272     void   createScene();
273 
274 public:
275 
276     VBSPReader();
277     virtual ~VBSPReader();
278 
279     bool   readFile(const std::string & file);
280 
281     osg::ref_ptr<osg::Node>   getRootNode();
282 };
283 
284 
285 }
286 
287 #endif
288