| 1 | |
|---|
| 2 | #include "Q3BSPLoad.h" |
|---|
| 3 | #include <osgDB/fstream> |
|---|
| 4 | |
|---|
| 5 | using namespace bsp; |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | bool 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 | |
|---|
| 14 | return false; |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | file.read((char*)&m_header, sizeof(BSP_HEADER)); |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 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 | |
|---|
| 26 | return false; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | LoadVertices(file); |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | int numMeshIndices=m_header.m_directoryEntries[bspMeshIndices].m_length/sizeof(int); |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | m_loadMeshIndices.resize(numMeshIndices); |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 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 | |
|---|
| 46 | LoadFaces(file, curveTessellation); |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | LoadTextures(file); |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | LoadLightmaps(file); |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | LoadBSPData(file); |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | m_entityString.resize(m_header.m_directoryEntries[bspEntities].m_length); |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 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 | |
|---|
| 65 | return true; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | void Q3BSPLoad::LoadVertices(std::ifstream& aFile) |
|---|
| 74 | { |
|---|
| 75 | |
|---|
| 76 | int num_vertices=m_header.m_directoryEntries[bspVertices].m_length/sizeof(BSP_LOAD_VERTEX); |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | m_loadVertices.resize(num_vertices); |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | aFile.seekg(m_header.m_directoryEntries[bspVertices].m_offset,std::ios::beg); |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 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 | |
|---|
| 97 | void Q3BSPLoad::LoadFaces(std::ifstream& aFile, int ) |
|---|
| 98 | { |
|---|
| 99 | |
|---|
| 100 | int numTotalFaces=m_header.m_directoryEntries[bspFaces].m_length/sizeof(BSP_LOAD_FACE); |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | m_loadFaces.resize(numTotalFaces); |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | aFile.seekg(m_header.m_directoryEntries[bspFaces].m_offset,std::ios::beg); |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 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 | |
|---|
| 124 | void Q3BSPLoad::LoadTextures(std::ifstream& aFile) |
|---|
| 125 | { |
|---|
| 126 | |
|---|
| 127 | int num_textures=m_header.m_directoryEntries[bspTextures].m_length/sizeof(BSP_LOAD_TEXTURE); |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | m_loadTextures.resize(num_textures); |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 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 | |
|---|
| 147 | void Q3BSPLoad::LoadLightmaps(std::ifstream& aFile) |
|---|
| 148 | { |
|---|
| 149 | |
|---|
| 150 | int num_lightmaps=m_header.m_directoryEntries[bspLightmaps].m_length/sizeof(BSP_LOAD_LIGHTMAP); |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | m_loadLightmaps.resize(num_lightmaps); |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 182 | scale*=255.0f; |
|---|
| 183 | r*=scale; |
|---|
| 184 | g*=scale; |
|---|
| 185 | b*=scale; |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 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 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | } |
|---|
| 195 | } |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | void Q3BSPLoad::LoadBSPData(std::ifstream& aFile) |
|---|
| 208 | { |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | int numLeaves=m_header.m_directoryEntries[bspLeaves].m_length/sizeof(BSP_LOAD_LEAF); |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | m_loadLeaves.resize(numLeaves); |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 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 | |
|---|
| 224 | int num_leaf_faces=m_header.m_directoryEntries[bspLeafFaces].m_length/sizeof(int); |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | m_loadLeafFaces.resize(num_leaf_faces); |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 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 | |
|---|
| 238 | int num_planes=m_header.m_directoryEntries[bspPlanes].m_length/sizeof(BSP_LoadPlane); |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 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 | |
|---|
| 249 | int num_nodes=m_header.m_directoryEntries[bspNodes].m_length/sizeof(BSP_NODE); |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 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 | |
|---|
| 260 | |
|---|
| 261 | aFile.seekg(m_header.m_directoryEntries[bspVisData].m_offset,std::ios::beg); |
|---|
| 262 | aFile.read((char*)&m_loadVisibilityData, 2 * sizeof(int)); |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | int bitsetSize=m_loadVisibilityData.m_numClusters*m_loadVisibilityData.m_bytesPerCluster; |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | m_loadVisibilityData.m_bitset.resize(bitsetSize); |
|---|
| 269 | |
|---|
| 270 | aFile.read((char*)&m_loadVisibilityData.m_bitset[0], bitsetSize); |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|