root/OpenSceneGraph/trunk/src/osgPlugins/bsp/Q3BSPLoad.cpp @ 13041

Revision 13041, 7.0 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1
2#include "Q3BSPLoad.h"
3#include <osgDB/fstream>
4
5using namespace bsp;
6
7
8bool Q3BSPLoad::Load(const std::string& filename, int curveTessellation)
9{
10  osgDB::ifstream file(filename.c_str(),std::ios::binary);
11  if(!file.is_open())
12    {
13      //errorLog.OutputError("Unable to open %s", filename);
14      return false;
15    }
16
17  //read in header
18  file.read((char*)&m_header, sizeof(BSP_HEADER));
19
20  //check header data is correct
21  if( m_header.m_string[0]!='I' || m_header.m_string[1]!='B' ||
22      m_header.m_string[2]!='S' || m_header.m_string[3]!='P' ||
23      m_header.m_version  !=0x2E )
24    {
25      //errorLog.OutputError("%s is not a version 0x2E .bsp map file", filename);
26      return false;
27    }
28
29
30  //Load in vertices
31  LoadVertices(file);
32
33  //Load in mesh indices
34  //Calculate number of indices
35  int numMeshIndices=m_header.m_directoryEntries[bspMeshIndices].m_length/sizeof(int);
36
37  //Create space
38  m_loadMeshIndices.resize(numMeshIndices);
39
40  //read in the mesh indices
41  file.seekg(m_header.m_directoryEntries[bspMeshIndices].m_offset,std::ios::beg);
42  file.read((char*) &m_loadMeshIndices[0], m_header.m_directoryEntries[bspMeshIndices].m_length);
43
44
45  //Load in faces
46  LoadFaces(file, curveTessellation);
47
48  //Load textures
49  LoadTextures(file);
50
51  //Load Lightmaps
52  LoadLightmaps(file);
53
54  //Load BSP Data
55  LoadBSPData(file);
56
57  //Load in entity string
58  m_entityString.resize(m_header.m_directoryEntries[bspEntities].m_length);
59
60  //Go to entity string in file
61  file.seekg(m_header.m_directoryEntries[bspEntities].m_offset,std::ios::beg);
62  file.read(&m_entityString[0], m_header.m_directoryEntries[bspEntities].m_length);
63
64  //errorLog.OutputSuccess("%s Loaded successfully", filename);
65  return true;
66}
67
68
69
70
71
72
73void Q3BSPLoad::LoadVertices(std::ifstream& aFile)
74{
75  //calculate number of vertices
76  int num_vertices=m_header.m_directoryEntries[bspVertices].m_length/sizeof(BSP_LOAD_VERTEX);
77
78  //Create space for this many BSP_LOAD_VERTICES
79  m_loadVertices.resize(num_vertices);
80
81  //go to vertices in file
82  aFile.seekg(m_header.m_directoryEntries[bspVertices].m_offset,std::ios::beg);
83
84  //read in the vertices
85  aFile.read((char*)&m_loadVertices[0], m_header.m_directoryEntries[bspVertices].m_length);
86}
87
88
89
90
91
92
93
94
95
96
97void Q3BSPLoad::LoadFaces(std::ifstream& aFile, int /*curveTessellation*/)
98{
99  //calculate number of load faces
100  int numTotalFaces=m_header.m_directoryEntries[bspFaces].m_length/sizeof(BSP_LOAD_FACE);
101
102  //Create space for this many BSP_LOAD_FACES
103  m_loadFaces.resize(numTotalFaces);
104
105  //go to faces in file
106  aFile.seekg(m_header.m_directoryEntries[bspFaces].m_offset,std::ios::beg);
107
108  //read in the faces
109  aFile.read((char*)&m_loadFaces[0], m_header.m_directoryEntries[bspFaces].m_length);
110}
111
112
113
114
115
116
117
118
119
120
121
122
123
124void Q3BSPLoad::LoadTextures(std::ifstream& aFile)
125{
126    //Calculate number of textures
127    int num_textures=m_header.m_directoryEntries[bspTextures].m_length/sizeof(BSP_LOAD_TEXTURE);
128
129    //Create space for this many BSP_LOAD_TEXTUREs
130    m_loadTextures.resize(num_textures);
131
132    //Load textures
133    aFile.seekg(m_header.m_directoryEntries[bspTextures].m_offset,std::ios::beg);
134    aFile.read((char*)&m_loadTextures[0], m_header.m_directoryEntries[bspTextures].m_length);
135}
136
137
138
139
140
141
142
143
144
145
146
147void Q3BSPLoad::LoadLightmaps(std::ifstream& aFile)
148{
149    //Calculate number of lightmaps
150    int num_lightmaps=m_header.m_directoryEntries[bspLightmaps].m_length/sizeof(BSP_LOAD_LIGHTMAP);
151
152    //Create space for this many BSP_LOAD_LIGHTMAPs
153    m_loadLightmaps.resize(num_lightmaps);
154
155    //Load textures
156    aFile.seekg(m_header.m_directoryEntries[bspLightmaps].m_offset,std::ios::beg);
157    aFile.read((char*)&m_loadLightmaps[0], m_header.m_directoryEntries[bspLightmaps].m_length);
158
159    //Change the gamma settings on the lightmaps (make them brighter)
160    float gamma=2.5f;
161    for(int i=0; i<num_lightmaps; ++i)
162      {
163        for(int j=0; j<128*128; ++j)
164          {
165            float r, g, b;
166            r=m_loadLightmaps[i].m_lightmapData[j*3+0];
167            g=m_loadLightmaps[i].m_lightmapData[j*3+1];
168            b=m_loadLightmaps[i].m_lightmapData[j*3+2];
169
170            r*=gamma/255.0f;
171            g*=gamma/255.0f;
172            b*=gamma/255.0f;
173
174            //find the value to scale back up
175            float scale=1.0f;
176            float temp;
177            if(r > 1.0f && (temp = (1.0f/r)) < scale) scale=temp;
178            if(g > 1.0f && (temp = (1.0f/g)) < scale) scale=temp;
179            if(b > 1.0f && (temp = (1.0f/b)) < scale) scale=temp;
180
181            // scale up color values
182            scale*=255.0f;
183            r*=scale;
184            g*=scale;
185            b*=scale;
186
187            //fill data back in
188            m_loadLightmaps[i].m_lightmapData[j*3+0]=(unsigned char)r;
189            m_loadLightmaps[i].m_lightmapData[j*3+1]=(unsigned char)g;
190            m_loadLightmaps[i].m_lightmapData[j*3+2]=(unsigned char)b;
191            //m_loadLightmaps[i].m_lightmapData[j*3+0]=(GLubyte)255;
192            //m_loadLightmaps[i].m_lightmapData[j*3+1]=(GLubyte)255;
193            //m_loadLightmaps[i].m_lightmapData[j*3+2]=(GLubyte)255;
194          }
195      }
196}
197
198
199
200
201
202
203
204
205
206
207void Q3BSPLoad::LoadBSPData(std::ifstream& aFile)
208{
209  //Load leaves
210  //Calculate number of leaves
211  int numLeaves=m_header.m_directoryEntries[bspLeaves].m_length/sizeof(BSP_LOAD_LEAF);
212
213  //Create space for this many BSP_LOAD_LEAFS
214  m_loadLeaves.resize(numLeaves);
215
216  //Load leaves
217  aFile.seekg(m_header.m_directoryEntries[bspLeaves].m_offset,std::ios::beg);
218  aFile.read((char*)&m_loadLeaves[0], m_header.m_directoryEntries[bspLeaves].m_length);
219
220
221
222
223  //Load leaf faces array
224  int num_leaf_faces=m_header.m_directoryEntries[bspLeafFaces].m_length/sizeof(int);
225
226  //Create space for this many leaf faces
227  m_loadLeafFaces.resize(num_leaf_faces);
228
229  //Load leaf faces
230  aFile.seekg(m_header.m_directoryEntries[bspLeafFaces].m_offset,std::ios::beg);
231  aFile.read((char*)&m_loadLeafFaces[0], m_header.m_directoryEntries[bspLeafFaces].m_length);
232
233
234
235
236
237  //Load Planes
238  int num_planes=m_header.m_directoryEntries[bspPlanes].m_length/sizeof(BSP_LoadPlane);
239
240  //Create space for this many planes
241  m_loadPlanes.resize(num_planes);
242
243  aFile.seekg(m_header.m_directoryEntries[bspPlanes].m_offset,std::ios::beg);
244  aFile.read((char*)&m_loadPlanes[0], m_header.m_directoryEntries[bspPlanes].m_length);
245
246
247
248  //Load nodes
249  int num_nodes=m_header.m_directoryEntries[bspNodes].m_length/sizeof(BSP_NODE);
250
251  //Create space for this many nodes
252  m_loadNodes.resize(num_nodes);
253
254  aFile.seekg(m_header.m_directoryEntries[bspNodes].m_offset,std::ios::beg);
255  aFile.read((char*)&m_loadNodes[0], m_header.m_directoryEntries[bspNodes].m_length);
256
257
258
259  //Load visibility data
260  //load numClusters and bytesPerCluster
261  aFile.seekg(m_header.m_directoryEntries[bspVisData].m_offset,std::ios::beg);
262  aFile.read((char*)&m_loadVisibilityData, 2 * sizeof(int));
263
264  //Calculate the size of the bitset
265  int bitsetSize=m_loadVisibilityData.m_numClusters*m_loadVisibilityData.m_bytesPerCluster;
266
267  //Create space for bitset
268  m_loadVisibilityData.m_bitset.resize(bitsetSize);
269  //read bitset
270  aFile.read((char*)&m_loadVisibilityData.m_bitset[0], bitsetSize);
271}
272
273
274
275
276
277
278
Note: See TracBrowser for help on using the browser.