| 1 | #include <osg/BlendFunc> |
|---|
| 2 | #include <osg/BoundingSphere> |
|---|
| 3 | #include <osg/Geometry> |
|---|
| 4 | #include <osg/Group> |
|---|
| 5 | #include <osg/Object> |
|---|
| 6 | #include <osg/Material> |
|---|
| 7 | #include <osg/Math> |
|---|
| 8 | #include <osg/MatrixTransform> |
|---|
| 9 | #include <osg/Node> |
|---|
| 10 | #include <osg/Notify> |
|---|
| 11 | #include <osg/Quat> |
|---|
| 12 | #include <osg/StateSet> |
|---|
| 13 | #include <osg/Texture1D> |
|---|
| 14 | #include <osg/Texture2D> |
|---|
| 15 | #include <osg/Texture3D> |
|---|
| 16 | #include <osg/TexEnv> |
|---|
| 17 | #include <osg/TexEnvCombine> |
|---|
| 18 | #include <osgDB/Registry> |
|---|
| 19 | #include <osgDB/FileUtils> |
|---|
| 20 | #include <osgDB/ReadFile> |
|---|
| 21 | #include <osg/io_utils> |
|---|
| 22 | #include <iostream> |
|---|
| 23 | #include <string.h> |
|---|
| 24 | |
|---|
| 25 | #include "VBSPReader.h" |
|---|
| 26 | #include "VBSPEntity.h" |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | using namespace bsp; |
|---|
| 30 | using namespace osg; |
|---|
| 31 | using namespace osgDB; |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | #ifdef _MSC_VER |
|---|
| 36 | #define strcasecmp _stricmp |
|---|
| 37 | #endif |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | VBSPReader::VBSPReader() |
|---|
| 41 | { |
|---|
| 42 | |
|---|
| 43 | root_node = NULL; |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | bsp_data = new VBSPData(); |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | texdata_string = NULL; |
|---|
| 50 | texdata_string_table = NULL; |
|---|
| 51 | num_texdata_string_table_entries = 0; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | VBSPReader::~VBSPReader() |
|---|
| 56 | { |
|---|
| 57 | |
|---|
| 58 | delete [] texdata_string; |
|---|
| 59 | delete [] texdata_string_table; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | void VBSPReader::processEntities(std::istream & str, int offset, |
|---|
| 64 | int length) |
|---|
| 65 | { |
|---|
| 66 | char * entities; |
|---|
| 67 | char * startPtr; |
|---|
| 68 | char * endPtr; |
|---|
| 69 | int numEntities; |
|---|
| 70 | int i; |
|---|
| 71 | std::string entityStr; |
|---|
| 72 | size_t entityLen; |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | entities = new char[length]; |
|---|
| 76 | memset(entities, 0, length * sizeof(char)); |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | str.seekg(offset); |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | str.read((char *) entities, sizeof(char) * length); |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | startPtr = entities; |
|---|
| 86 | endPtr = strchr(entities, '}'); |
|---|
| 87 | numEntities = 0; |
|---|
| 88 | while ((startPtr != NULL) && (endPtr != NULL)) |
|---|
| 89 | { |
|---|
| 90 | |
|---|
| 91 | numEntities++; |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | startPtr = strchr(endPtr, '{'); |
|---|
| 95 | if (startPtr != NULL) |
|---|
| 96 | endPtr = strchr(startPtr, '}'); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | startPtr = entities; |
|---|
| 101 | endPtr = strchr(entities, '}'); |
|---|
| 102 | for (i = 0; i < numEntities; i++) |
|---|
| 103 | { |
|---|
| 104 | |
|---|
| 105 | entityLen = endPtr - startPtr + 1; |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | entityStr = std::string(startPtr, entityLen); |
|---|
| 109 | bsp_data->addEntity(entityStr); |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | startPtr = strchr(endPtr, '{'); |
|---|
| 113 | if (startPtr != NULL) |
|---|
| 114 | endPtr = strchr(startPtr, '}'); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | delete [] entities; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | void VBSPReader::processModels(std::istream & str, int offset, int length) |
|---|
| 123 | { |
|---|
| 124 | int numModels; |
|---|
| 125 | int i; |
|---|
| 126 | Model * models; |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | numModels = length / sizeof(Model); |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | str.seekg(offset); |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | models = new Model[numModels]; |
|---|
| 136 | str.read((char *) models, sizeof(Model) * numModels); |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | for (i = 0; i < numModels; i++) |
|---|
| 140 | bsp_data->addModel(models[i]); |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | delete [] models; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | void VBSPReader::processPlanes(std::istream & str, int offset, int length) |
|---|
| 148 | { |
|---|
| 149 | int numPlanes; |
|---|
| 150 | int i; |
|---|
| 151 | Plane * planes; |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | numPlanes = length / sizeof(Plane); |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | str.seekg(offset); |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | planes = new Plane[numPlanes]; |
|---|
| 161 | str.read((char *) planes, sizeof(Plane) * numPlanes); |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | for (i = 0; i < numPlanes; i++) |
|---|
| 165 | bsp_data->addPlane(planes[i]); |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | delete [] planes; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | void VBSPReader::processVertices(std::istream & str, int offset, int length) |
|---|
| 173 | { |
|---|
| 174 | int numVertices; |
|---|
| 175 | int i; |
|---|
| 176 | Vec3f * vertices; |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | numVertices = length / 3 / sizeof(float); |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | str.seekg(offset); |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | vertices = new Vec3f[numVertices]; |
|---|
| 186 | str.read((char *) vertices, sizeof(Vec3f) * numVertices); |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | for (i = 0; i < numVertices; i++) |
|---|
| 190 | bsp_data->addVertex(vertices[i]); |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | delete [] vertices; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | void VBSPReader::processEdges(std::istream & str, int offset, int length) |
|---|
| 198 | { |
|---|
| 199 | int numEdges; |
|---|
| 200 | int i; |
|---|
| 201 | Edge * edges; |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | numEdges = length / sizeof(Edge); |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | str.seekg(offset); |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | edges = new Edge[numEdges]; |
|---|
| 211 | str.read((char *) edges, sizeof(Edge) * numEdges); |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | for (i = 0; i < numEdges; i++) |
|---|
| 215 | bsp_data->addEdge(edges[i]); |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | delete [] edges; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | void VBSPReader::processSurfEdges(std::istream & str, int offset, int length) |
|---|
| 223 | { |
|---|
| 224 | int numSurfEdges; |
|---|
| 225 | int i; |
|---|
| 226 | int * surfEdges; |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | numSurfEdges = length / sizeof(int); |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | str.seekg(offset); |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | surfEdges = new int[numSurfEdges]; |
|---|
| 236 | str.read((char *) surfEdges, sizeof(int) * numSurfEdges); |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | for (i = 0; i < numSurfEdges; i++) |
|---|
| 240 | bsp_data->addSurfaceEdge(surfEdges[i]); |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | delete [] surfEdges; |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | void VBSPReader::processFaces(std::istream & str, int offset, int length) |
|---|
| 248 | { |
|---|
| 249 | int numFaces; |
|---|
| 250 | int i; |
|---|
| 251 | Face * faces; |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | numFaces = length / sizeof(Face); |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | str.seekg(offset); |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | faces = new Face[numFaces]; |
|---|
| 261 | str.read((char *) faces, sizeof(Face) * numFaces); |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | for (i = 0; i < numFaces; i++) |
|---|
| 265 | bsp_data->addFace(faces[i]); |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | delete [] faces; |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | void VBSPReader::processTexInfo(std::istream & str, int offset, int length) |
|---|
| 273 | { |
|---|
| 274 | int numTexInfos; |
|---|
| 275 | int i; |
|---|
| 276 | TexInfo * texinfos; |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | numTexInfos = length / sizeof(TexInfo); |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | str.seekg(offset); |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | texinfos = new TexInfo[numTexInfos]; |
|---|
| 286 | str.read((char *) texinfos, sizeof(TexInfo) * numTexInfos); |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | for (i = 0; i < numTexInfos; i++) |
|---|
| 290 | bsp_data->addTexInfo(texinfos[i]); |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | delete [] texinfos; |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | void VBSPReader::processTexData(std::istream & str, int offset, int length) |
|---|
| 298 | { |
|---|
| 299 | int numTexDatas; |
|---|
| 300 | int i; |
|---|
| 301 | TexData * texdatas; |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | numTexDatas = length / sizeof(TexData); |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | str.seekg(offset); |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | texdatas = new TexData[numTexDatas]; |
|---|
| 311 | str.read((char *) texdatas, sizeof(TexData) * numTexDatas); |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | for (i = 0; i < numTexDatas; i++) |
|---|
| 315 | bsp_data->addTexData(texdatas[i]); |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | delete [] texdatas; |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | void VBSPReader::processTexDataStringTable(std::istream & str, int offset, |
|---|
| 323 | int length) |
|---|
| 324 | { |
|---|
| 325 | int i; |
|---|
| 326 | int index; |
|---|
| 327 | std::string texStr; |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | num_texdata_string_table_entries = length / sizeof(int); |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | texdata_string_table = new int[num_texdata_string_table_entries]; |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | str.seekg(offset); |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | str.read((char *) texdata_string_table, |
|---|
| 340 | sizeof(int) * num_texdata_string_table_entries); |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | if (texdata_string != NULL) |
|---|
| 344 | { |
|---|
| 345 | for (i = 0; i < num_texdata_string_table_entries; i++) |
|---|
| 346 | { |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | index = texdata_string_table[i]; |
|---|
| 350 | texStr = std::string(&texdata_string[index]); |
|---|
| 351 | bsp_data->addTexDataString(texStr); |
|---|
| 352 | } |
|---|
| 353 | } |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | void VBSPReader::processTexDataStringData(std::istream & str, int offset, |
|---|
| 358 | int length) |
|---|
| 359 | { |
|---|
| 360 | int i; |
|---|
| 361 | int index; |
|---|
| 362 | std::string texStr; |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | texdata_string = new char[length]; |
|---|
| 366 | memset(texdata_string, 0, length * sizeof(char)); |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | str.seekg(offset); |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | str.read((char *) texdata_string, sizeof(char) * length); |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | for (i = 0; i < num_texdata_string_table_entries; i++) |
|---|
| 379 | { |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | index = texdata_string_table[i]; |
|---|
| 383 | texStr = std::string(&texdata_string[index]); |
|---|
| 384 | bsp_data->addTexDataString(texStr); |
|---|
| 385 | } |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | |
|---|
| 389 | void VBSPReader::processDispInfo(std::istream & str, int offset, int length) |
|---|
| 390 | { |
|---|
| 391 | int numDispInfos; |
|---|
| 392 | int i; |
|---|
| 393 | DisplaceInfo * dispinfos; |
|---|
| 394 | |
|---|
| 395 | |
|---|
| 396 | numDispInfos = length / sizeof(DisplaceInfo); |
|---|
| 397 | |
|---|
| 398 | |
|---|
| 399 | str.seekg(offset); |
|---|
| 400 | |
|---|
| 401 | |
|---|
| 402 | dispinfos = new DisplaceInfo[numDispInfos]; |
|---|
| 403 | str.read((char *) dispinfos, sizeof(DisplaceInfo) * numDispInfos); |
|---|
| 404 | |
|---|
| 405 | |
|---|
| 406 | for (i = 0; i < numDispInfos; i++) |
|---|
| 407 | bsp_data->addDispInfo(dispinfos[i]); |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | delete [] dispinfos; |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | |
|---|
| 414 | void VBSPReader::processDispVerts(std::istream & str, int offset, int length) |
|---|
| 415 | { |
|---|
| 416 | int numDispVerts; |
|---|
| 417 | int i; |
|---|
| 418 | DisplacedVertex * dispverts; |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | numDispVerts = length / sizeof(DisplacedVertex); |
|---|
| 422 | |
|---|
| 423 | |
|---|
| 424 | str.seekg(offset); |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | dispverts = new DisplacedVertex[numDispVerts]; |
|---|
| 428 | str.read((char *) dispverts, sizeof(DisplacedVertex) * numDispVerts); |
|---|
| 429 | |
|---|
| 430 | |
|---|
| 431 | for (i = 0; i < numDispVerts; i++) |
|---|
| 432 | bsp_data->addDispVertex(dispverts[i]); |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | delete [] dispverts; |
|---|
| 436 | } |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | void VBSPReader::processGameData(std::istream & str, int offset, int length) |
|---|
| 440 | { |
|---|
| 441 | GameHeader gameHeader; |
|---|
| 442 | GameLump * gameLumps; |
|---|
| 443 | int i; |
|---|
| 444 | |
|---|
| 445 | |
|---|
| 446 | str.seekg(offset); |
|---|
| 447 | str.read((char *) &gameHeader, sizeof(GameHeader)); |
|---|
| 448 | |
|---|
| 449 | |
|---|
| 450 | gameLumps = new GameLump[gameHeader.num_lumps]; |
|---|
| 451 | str.read((char *) gameLumps, sizeof(GameLump) * gameHeader.num_lumps); |
|---|
| 452 | |
|---|
| 453 | |
|---|
| 454 | for (i = 0; i < gameHeader.num_lumps; i++) |
|---|
| 455 | { |
|---|
| 456 | |
|---|
| 457 | if (gameLumps[i].lump_id == STATIC_PROP_ID) |
|---|
| 458 | { |
|---|
| 459 | processStaticProps(str, gameLumps[i].lump_offset, |
|---|
| 460 | gameLumps[i].lump_length, |
|---|
| 461 | gameLumps[i].lump_version); |
|---|
| 462 | } |
|---|
| 463 | } |
|---|
| 464 | |
|---|
| 465 | |
|---|
| 466 | delete [] gameLumps; |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | |
|---|
| 470 | void VBSPReader::processStaticProps(std::istream & str, int offset, int length, |
|---|
| 471 | int lumpVersion) |
|---|
| 472 | { |
|---|
| 473 | StaticPropModelNames sprpModelNames; |
|---|
| 474 | char modelName[130]; |
|---|
| 475 | std::string modelStr; |
|---|
| 476 | int i; |
|---|
| 477 | StaticPropLeaves sprpLeaves; |
|---|
| 478 | StaticProps sprpHeader; |
|---|
| 479 | StaticPropV4 sprp4; |
|---|
| 480 | StaticProp sprp5; |
|---|
| 481 | |
|---|
| 482 | |
|---|
| 483 | str.seekg(offset); |
|---|
| 484 | str.read((char *) &sprpModelNames, sizeof(StaticPropModelNames)); |
|---|
| 485 | for (i = 0; i < sprpModelNames.num_model_names; i++) |
|---|
| 486 | { |
|---|
| 487 | str.read(modelName, 128); |
|---|
| 488 | modelName[128] = 0; |
|---|
| 489 | modelStr = std::string(modelName); |
|---|
| 490 | bsp_data->addStaticPropModel(modelStr); |
|---|
| 491 | } |
|---|
| 492 | |
|---|
| 493 | |
|---|
| 494 | str.read((char *) &sprpLeaves, sizeof(StaticPropLeaves)); |
|---|
| 495 | str.seekg(sprpLeaves.num_leaf_entries * sizeof(unsigned short), |
|---|
| 496 | std::istream::cur); |
|---|
| 497 | |
|---|
| 498 | |
|---|
| 499 | str.read((char *) &sprpHeader, sizeof(StaticProps)); |
|---|
| 500 | for (i = 0; i < sprpHeader.num_static_props; i++) |
|---|
| 501 | { |
|---|
| 502 | |
|---|
| 503 | if (lumpVersion == 4) |
|---|
| 504 | { |
|---|
| 505 | |
|---|
| 506 | str.read((char *) &sprp4, sizeof(StaticPropV4)); |
|---|
| 507 | bsp_data->addStaticProp(sprp4); |
|---|
| 508 | } |
|---|
| 509 | else if (lumpVersion == 5) |
|---|
| 510 | { |
|---|
| 511 | |
|---|
| 512 | str.read((char *) &sprp5, sizeof(StaticProp)); |
|---|
| 513 | bsp_data->addStaticProp(sprp5); |
|---|
| 514 | } |
|---|
| 515 | } |
|---|
| 516 | } |
|---|
| 517 | |
|---|
| 518 | |
|---|
| 519 | std::string VBSPReader::getToken(std::string str, const char * delim, |
|---|
| 520 | size_t & index) |
|---|
| 521 | { |
|---|
| 522 | std::string token; |
|---|
| 523 | size_t end = std::string::npos; |
|---|
| 524 | |
|---|
| 525 | |
|---|
| 526 | size_t start = str.find_first_not_of(delim, index); |
|---|
| 527 | if (start != std::string::npos) |
|---|
| 528 | { |
|---|
| 529 | |
|---|
| 530 | end = str.find_first_of(delim, start+1); |
|---|
| 531 | if (end != std::string::npos) |
|---|
| 532 | { |
|---|
| 533 | |
|---|
| 534 | token = str.substr(start, end-start); |
|---|
| 535 | } |
|---|
| 536 | else |
|---|
| 537 | { |
|---|
| 538 | |
|---|
| 539 | |
|---|
| 540 | token = str.substr(start); |
|---|
| 541 | } |
|---|
| 542 | } |
|---|
| 543 | else |
|---|
| 544 | { |
|---|
| 545 | |
|---|
| 546 | token = ""; |
|---|
| 547 | } |
|---|
| 548 | |
|---|
| 549 | |
|---|
| 550 | |
|---|
| 551 | if (end != std::string::npos) |
|---|
| 552 | index = end+1; |
|---|
| 553 | else |
|---|
| 554 | index = std::string::npos; |
|---|
| 555 | |
|---|
| 556 | |
|---|
| 557 | return token; |
|---|
| 558 | } |
|---|
| 559 | |
|---|
| 560 | |
|---|
| 561 | ref_ptr<Texture> VBSPReader::readTextureFile(std::string textureName) |
|---|
| 562 | { |
|---|
| 563 | std::string texFile; |
|---|
| 564 | std::string texPath; |
|---|
| 565 | osg::ref_ptr<Image> texImage; |
|---|
| 566 | osg::ref_ptr<Texture> texture; |
|---|
| 567 | |
|---|
| 568 | |
|---|
| 569 | texFile = std::string(textureName) + ".vtf"; |
|---|
| 570 | texPath = findDataFile(texFile, CASE_INSENSITIVE); |
|---|
| 571 | |
|---|
| 572 | |
|---|
| 573 | if (texPath.empty()) |
|---|
| 574 | { |
|---|
| 575 | texFile = "materials/" + std::string(textureName) + ".vtf"; |
|---|
| 576 | texPath = findDataFile(texFile, CASE_INSENSITIVE); |
|---|
| 577 | |
|---|
| 578 | |
|---|
| 579 | |
|---|
| 580 | |
|---|
| 581 | if (texPath.empty()) |
|---|
| 582 | { |
|---|
| 583 | texFile = "../materials/" + std::string(textureName) + ".vtf"; |
|---|
| 584 | texPath = findDataFile(texFile, CASE_INSENSITIVE); |
|---|
| 585 | } |
|---|
| 586 | } |
|---|
| 587 | |
|---|
| 588 | |
|---|
| 589 | if (!texPath.empty()) |
|---|
| 590 | { |
|---|
| 591 | texImage = readRefImageFile(texPath); |
|---|
| 592 | |
|---|
| 593 | |
|---|
| 594 | if (texImage != NULL) |
|---|
| 595 | { |
|---|
| 596 | |
|---|
| 597 | if (texImage->t() == 1) |
|---|
| 598 | { |
|---|
| 599 | texture = new Texture1D(texImage.get()); |
|---|
| 600 | } |
|---|
| 601 | else if (texImage->r() == 1) |
|---|
| 602 | { |
|---|
| 603 | texture = new Texture2D(texImage.get()); |
|---|
| 604 | } |
|---|
| 605 | else |
|---|
| 606 | { |
|---|
| 607 | texture = new Texture3D(texImage.get()); |
|---|
| 608 | } |
|---|
| 609 | |
|---|
| 610 | |
|---|
| 611 | texture->setWrap(Texture::WRAP_S, Texture::REPEAT); |
|---|
| 612 | texture->setWrap(Texture::WRAP_T, Texture::REPEAT); |
|---|
| 613 | texture->setWrap(Texture::WRAP_R, Texture::REPEAT); |
|---|
| 614 | texture->setFilter(Texture::MAG_FILTER, Texture::LINEAR); |
|---|
| 615 | texture->setFilter(Texture::MIN_FILTER, |
|---|
| 616 | Texture::LINEAR_MIPMAP_LINEAR); |
|---|
| 617 | } |
|---|
| 618 | else |
|---|
| 619 | { |
|---|
| 620 | |
|---|
| 621 | OSG_WARN << "Couldn't find texture " << textureName; |
|---|
| 622 | OSG_WARN << std::endl; |
|---|
| 623 | |
|---|
| 624 | |
|---|
| 625 | texture = NULL; |
|---|
| 626 | } |
|---|
| 627 | } |
|---|
| 628 | else |
|---|
| 629 | { |
|---|
| 630 | |
|---|
| 631 | OSG_WARN << "Couldn't find texture " << textureName; |
|---|
| 632 | OSG_WARN << std::endl; |
|---|
| 633 | |
|---|
| 634 | |
|---|
| 635 | texture = NULL; |
|---|
| 636 | } |
|---|
| 637 | |
|---|
| 638 | return texture; |
|---|
| 639 | } |
|---|
| 640 | |
|---|
| 641 | |
|---|
| 642 | ref_ptr<StateSet> VBSPReader::readMaterialFile(std::string materialName) |
|---|
| 643 | { |
|---|
| 644 | std::string mtlFileName; |
|---|
| 645 | std::string mtlPath; |
|---|
| 646 | osgDB::ifstream * mtlFile; |
|---|
| 647 | std::string line; |
|---|
| 648 | std::string::size_type start = std::string::npos; |
|---|
| 649 | std::string token; |
|---|
| 650 | bool found = false; |
|---|
| 651 | ref_ptr<StateSet> stateSet; |
|---|
| 652 | std::string shaderName; |
|---|
| 653 | std::string texName; |
|---|
| 654 | std::string tex2Name; |
|---|
| 655 | ref_ptr<Texture> texture; |
|---|
| 656 | ref_ptr<Texture> texture2; |
|---|
| 657 | ref_ptr<TexEnvCombine> combiner0; |
|---|
| 658 | ref_ptr<TexEnvCombine> combiner1; |
|---|
| 659 | ref_ptr<Material> material; |
|---|
| 660 | ref_ptr<BlendFunc> blend; |
|---|
| 661 | bool translucent; |
|---|
| 662 | double alpha; |
|---|
| 663 | |
|---|
| 664 | |
|---|
| 665 | mtlFileName = std::string(materialName) + ".vmt"; |
|---|
| 666 | mtlPath = findDataFile(mtlFileName, CASE_INSENSITIVE); |
|---|
| 667 | |
|---|
| 668 | |
|---|
| 669 | if (mtlPath.empty()) |
|---|
| 670 | { |
|---|
| 671 | mtlFileName = "materials/" + std::string(materialName) + ".vmt"; |
|---|
| 672 | mtlPath = findDataFile(mtlFileName, CASE_INSENSITIVE); |
|---|
| 673 | |
|---|
| 674 | |
|---|
| 675 | |
|---|
| 676 | |
|---|
| 677 | if (mtlPath.empty()) |
|---|
| 678 | { |
|---|
| 679 | mtlFileName = "../materials/" + std::string(materialName) + ".vmt"; |
|---|
| 680 | mtlPath = findDataFile(mtlFileName, CASE_INSENSITIVE); |
|---|
| 681 | } |
|---|
| 682 | } |
|---|
| 683 | |
|---|
| 684 | |
|---|
| 685 | if (!mtlPath.empty()) |
|---|
| 686 | { |
|---|
| 687 | |
|---|
| 688 | mtlFile = new osgDB::ifstream(mtlPath.c_str(), std::ifstream::in); |
|---|
| 689 | if (!mtlFile) |
|---|
| 690 | return NULL; |
|---|
| 691 | } |
|---|
| 692 | else |
|---|
| 693 | { |
|---|
| 694 | |
|---|
| 695 | OSG_WARN << "Can't find material " << materialName << std::endl; |
|---|
| 696 | return NULL; |
|---|
| 697 | } |
|---|
| 698 | |
|---|
| 699 | |
|---|
| 700 | found = false; |
|---|
| 701 | while ((!found) && (!mtlFile->eof())) |
|---|
| 702 | { |
|---|
| 703 | |
|---|
| 704 | std::getline(*mtlFile, line); |
|---|
| 705 | |
|---|
| 706 | |
|---|
| 707 | start = 0; |
|---|
| 708 | token = getToken(line, " \t\n\r\"", start); |
|---|
| 709 | |
|---|
| 710 | |
|---|
| 711 | if ((!token.empty()) && (token.compare(0, 2, "//") != 0)) |
|---|
| 712 | { |
|---|
| 713 | shaderName = token; |
|---|
| 714 | found = true; |
|---|
| 715 | } |
|---|
| 716 | } |
|---|
| 717 | |
|---|
| 718 | |
|---|
| 719 | if (!found) |
|---|
| 720 | { |
|---|
| 721 | mtlFile->close(); |
|---|
| 722 | OSG_WARN << "Material " << materialName << " isn't valid."; |
|---|
| 723 | OSG_WARN << std::endl; |
|---|
| 724 | return NULL; |
|---|
| 725 | } |
|---|
| 726 | |
|---|
| 727 | |
|---|
| 728 | texture = NULL; |
|---|
| 729 | texture2 = NULL; |
|---|
| 730 | |
|---|
| 731 | |
|---|
| 732 | translucent = false; |
|---|
| 733 | |
|---|
| 734 | |
|---|
| 735 | alpha = 1.0; |
|---|
| 736 | |
|---|
| 737 | |
|---|
| 738 | while (!mtlFile->eof()) |
|---|
| 739 | { |
|---|
| 740 | |
|---|
| 741 | std::getline(*mtlFile, line); |
|---|
| 742 | |
|---|
| 743 | |
|---|
| 744 | start = 0; |
|---|
| 745 | token = getToken(line, " \t\n\r\"", start); |
|---|
| 746 | |
|---|
| 747 | while ((!token.empty()) && (token.compare(0, 2, "//") != 0)) |
|---|
| 748 | { |
|---|
| 749 | if (equalCaseInsensitive(token, "$basetexture")) |
|---|
| 750 | { |
|---|
| 751 | |
|---|
| 752 | token = getToken(line, " \t\n\r\"", start); |
|---|
| 753 | |
|---|
| 754 | |
|---|
| 755 | if (!token.empty()) |
|---|
| 756 | texture = readTextureFile(token); |
|---|
| 757 | } |
|---|
| 758 | else if (equalCaseInsensitive(token, "$basetexture2")) |
|---|
| 759 | { |
|---|
| 760 | |
|---|
| 761 | token = getToken(line, " \t\n\r\"", start); |
|---|
| 762 | |
|---|
| 763 | |
|---|
| 764 | if (!token.empty()) |
|---|
| 765 | texture2 = readTextureFile(token); |
|---|
| 766 | } |
|---|
| 767 | else if ((equalCaseInsensitive(token, "$translucent")) || |
|---|
| 768 | (equalCaseInsensitive(token, "$alphatest"))) |
|---|
| 769 | { |
|---|
| 770 | |
|---|
| 771 | token = getToken(line, " \t\n\r\"", start); |
|---|
| 772 | |
|---|
| 773 | |
|---|
| 774 | if ((token == "1") || (token == "true")) |
|---|
| 775 | translucent = true; |
|---|
| 776 | } |
|---|
| 777 | else if (equalCaseInsensitive(token, "$alpha")) |
|---|
| 778 | { |
|---|
| 779 | |
|---|
| 780 | token = getToken(line, " \t\n\r\"", start); |
|---|
| 781 | |
|---|
| 782 | |
|---|
| 783 | if (!token.empty()) |
|---|
| 784 | { |
|---|
| 785 | alpha = osg::asciiToDouble(token.c_str()); |
|---|
| 786 | } |
|---|
| 787 | } |
|---|
| 788 | |
|---|
| 789 | |
|---|
| 790 | token = getToken(line, " \t\n\r\"", start); |
|---|
| 791 | } |
|---|
| 792 | } |
|---|
| 793 | |
|---|
| 794 | |
|---|
| 795 | stateSet = NULL; |
|---|
| 796 | |
|---|
| 797 | |
|---|
| 798 | if (equalCaseInsensitive(shaderName, "WorldVertexTransition")) |
|---|
| 799 | { |
|---|
| 800 | |
|---|
| 801 | if (texture.valid() && texture2.valid()) |
|---|
| 802 | { |
|---|
| 803 | |
|---|
| 804 | stateSet = new osg::StateSet(); |
|---|
| 805 | |
|---|
| 806 | |
|---|
| 807 | stateSet->setTextureAttributeAndModes(0, texture.get(), |
|---|
| 808 | osg::StateAttribute::ON); |
|---|
| 809 | stateSet->setTextureAttributeAndModes(1, texture2.get(), |
|---|
| 810 | osg::StateAttribute::ON); |
|---|
| 811 | |
|---|
| 812 | |
|---|
| 813 | |
|---|
| 814 | |
|---|
| 815 | |
|---|
| 816 | combiner0 = new osg::TexEnvCombine(); |
|---|
| 817 | combiner0->setConstantColor(osg::Vec4f(1.0, 1.0, 1.0, 1.0)); |
|---|
| 818 | |
|---|
| 819 | combiner0->setCombine_RGB(osg::TexEnvCombine::INTERPOLATE); |
|---|
| 820 | combiner0->setSource0_RGB(osg::TexEnvCombine::TEXTURE0); |
|---|
| 821 | combiner0->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR); |
|---|
| 822 | combiner0->setSource1_RGB(osg::TexEnvCombine::TEXTURE1); |
|---|
| 823 | combiner0->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR); |
|---|
| 824 | combiner0->setSource2_RGB(osg::TexEnvCombine::PRIMARY_COLOR); |
|---|
| 825 | combiner0->setOperand2_RGB(osg::TexEnvCombine::SRC_ALPHA); |
|---|
| 826 | |
|---|
| 827 | combiner0->setCombine_Alpha(osg::TexEnvCombine::REPLACE); |
|---|
| 828 | combiner0->setSource0_Alpha(osg::TexEnvCombine::CONSTANT); |
|---|
| 829 | combiner0->setOperand0_Alpha(osg::TexEnvCombine::SRC_ALPHA); |
|---|
| 830 | |
|---|
| 831 | combiner0->setScale_RGB(1.0); |
|---|
| 832 | combiner0->setScale_Alpha(1.0); |
|---|
| 833 | |
|---|
| 834 | stateSet->setTextureAttributeAndModes(0, combiner0.get(), |
|---|
| 835 | osg::StateAttribute::ON); |
|---|
| 836 | |
|---|
| 837 | |
|---|
| 838 | |
|---|
| 839 | |
|---|
| 840 | |
|---|
| 841 | combiner1 = new osg::TexEnvCombine(); |
|---|
| 842 | combiner1->setConstantColor(osg::Vec4f(1.0, 1.0, 1.0, 1.0)); |
|---|
| 843 | |
|---|
| 844 | combiner1->setCombine_RGB(osg::TexEnvCombine::MODULATE); |
|---|
| 845 | combiner1->setSource0_RGB(osg::TexEnvCombine::PREVIOUS); |
|---|
| 846 | combiner1->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR); |
|---|
| 847 | combiner1->setSource1_RGB(osg::TexEnvCombine::PRIMARY_COLOR); |
|---|
| 848 | combiner1->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR); |
|---|
| 849 | |
|---|
| 850 | combiner1->setCombine_Alpha(osg::TexEnvCombine::REPLACE); |
|---|
| 851 | combiner1->setSource0_Alpha(osg::TexEnvCombine::CONSTANT); |
|---|
| 852 | combiner1->setOperand0_Alpha(osg::TexEnvCombine::SRC_ALPHA); |
|---|
| 853 | |
|---|
| 854 | combiner1->setScale_RGB(1.0); |
|---|
| 855 | combiner1->setScale_Alpha(1.0); |
|---|
| 856 | |
|---|
| 857 | stateSet->setTextureAttributeAndModes(1, combiner1.get(), |
|---|
| 858 | osg::StateAttribute::ON); |
|---|
| 859 | |
|---|
| 860 | |
|---|
| 861 | material = new Material(); |
|---|
| 862 | material->setAmbient(Material::FRONT_AND_BACK, |
|---|
| 863 | Vec4(1.0, 1.0, 1.0, 1.0) ); |
|---|
| 864 | material->setDiffuse(Material::FRONT_AND_BACK, |
|---|
| 865 | Vec4(1.0, 1.0, 1.0, 1.0) ); |
|---|
| 866 | material->setSpecular(Material::FRONT_AND_BACK, |
|---|
| 867 | Vec4(0.0, 0.0, 0.0, 1.0) ); |
|---|
| 868 | material->setShininess(Material::FRONT_AND_BACK, 1.0); |
|---|
| 869 | material->setEmission(Material::FRONT_AND_BACK, |
|---|
| 870 | Vec4(0.0, 0.0, 0.0, 1.0) ); |
|---|
| 871 | material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE); |
|---|
| 872 | stateSet->setAttributeAndModes(material.get(), StateAttribute::ON); |
|---|
| 873 | } |
|---|
| 874 | } |
|---|
| 875 | else if (equalCaseInsensitive(shaderName, "UnlitGeneric")) |
|---|
| 876 | { |
|---|
| 877 | |
|---|
| 878 | stateSet = new StateSet(); |
|---|
| 879 | |
|---|
| 880 | |
|---|
| 881 | stateSet->setMode(GL_LIGHTING, StateAttribute::OFF); |
|---|
| 882 | |
|---|
| 883 | |
|---|
| 884 | if (texture != NULL) |
|---|
| 885 | { |
|---|
| 886 | stateSet->setTextureAttributeAndModes(0, texture.get(), |
|---|
| 887 | StateAttribute::ON); |
|---|
| 888 | stateSet->setTextureAttributeAndModes(0, |
|---|
| 889 | new TexEnv(TexEnv::MODULATE), |
|---|
| 890 | StateAttribute::ON); |
|---|
| 891 | |
|---|
| 892 | |
|---|
| 893 | if (translucent) |
|---|
| 894 | { |
|---|
| 895 | |
|---|
| 896 | |
|---|
| 897 | blend = new BlendFunc(BlendFunc::SRC_ALPHA, |
|---|
| 898 | BlendFunc::ONE_MINUS_SRC_ALPHA); |
|---|
| 899 | stateSet->setAttributeAndModes(blend.get(), StateAttribute::ON); |
|---|
| 900 | |
|---|
| 901 | |
|---|
| 902 | stateSet->setRenderingHint(StateSet::TRANSPARENT_BIN); |
|---|
| 903 | } |
|---|
| 904 | } |
|---|
| 905 | else |
|---|
| 906 | { |
|---|
| 907 | OSG_WARN << "No base texture for material " << materialName; |
|---|
| 908 | OSG_WARN << std::endl; |
|---|
| 909 | stateSet->setTextureMode(0, GL_TEXTURE_2D, StateAttribute::OFF); |
|---|
| 910 | } |
|---|
| 911 | } |
|---|
| 912 | else |
|---|
| 913 | { |
|---|
| 914 | |
|---|
| 915 | |
|---|
| 916 | |
|---|
| 917 | |
|---|
| 918 | stateSet = new StateSet(); |
|---|
| 919 | |
|---|
| 920 | |
|---|
| 921 | material = new Material(); |
|---|
| 922 | material->setAmbient(Material::FRONT_AND_BACK, |
|---|
| 923 | Vec4(1.0, 1.0, 1.0, 1.0) ); |
|---|
| 924 | material->setDiffuse(Material::FRONT_AND_BACK, |
|---|
| 925 | Vec4(1.0, 1.0, 1.0, 1.0) ); |
|---|
| 926 | material->setSpecular(Material::FRONT_AND_BACK, |
|---|
| 927 | Vec4(0.0, 0.0, 0.0, 1.0) ); |
|---|
| 928 | material->setShininess(Material::FRONT_AND_BACK, 1.0); |
|---|
| 929 | material->setEmission(Material::FRONT_AND_BACK, |
|---|
| 930 | Vec4(0.0, 0.0, 0.0, 1.0) ); |
|---|
| 931 | material->setAlpha(Material::FRONT_AND_BACK, alpha); |
|---|
| 932 | stateSet->setAttributeAndModes(material.get(), StateAttribute::ON); |
|---|
| 933 | |
|---|
| 934 | |
|---|
| 935 | if (texture != NULL) |
|---|
| 936 | { |
|---|
| 937 | stateSet->setTextureAttributeAndModes(0, texture.get(), |
|---|
| 938 | StateAttribute::ON); |
|---|
| 939 | stateSet->setTextureAttributeAndModes(0, |
|---|
| 940 | new TexEnv(TexEnv::MODULATE), |
|---|
| 941 | StateAttribute::ON); |
|---|
| 942 | |
|---|
| 943 | |
|---|
| 944 | if (translucent) |
|---|
| 945 | { |
|---|
| 946 | |
|---|
| 947 | |
|---|
| 948 | blend = new BlendFunc(BlendFunc::SRC_ALPHA, |
|---|
| 949 | BlendFunc::ONE_MINUS_SRC_ALPHA); |
|---|
| 950 | stateSet->setAttributeAndModes(blend.get(), StateAttribute::ON); |
|---|
| 951 | |
|---|
| 952 | |
|---|
| 953 | stateSet->setRenderingHint(StateSet::TRANSPARENT_BIN); |
|---|
| 954 | } |
|---|
| 955 | } |
|---|
| 956 | else |
|---|
| 957 | { |
|---|
| 958 | OSG_WARN << "No base texture for material " << materialName; |
|---|
| 959 | OSG_WARN << std::endl; |
|---|
| 960 | stateSet->setTextureMode(0, GL_TEXTURE_2D, StateAttribute::OFF); |
|---|
| 961 | } |
|---|
| 962 | } |
|---|
| 963 | |
|---|
| 964 | |
|---|
| 965 | mtlFile->close(); |
|---|
| 966 | |
|---|
| 967 | |
|---|
| 968 | return stateSet; |
|---|
| 969 | } |
|---|
| 970 | |
|---|
| 971 | |
|---|
| 972 | void VBSPReader::createScene() |
|---|
| 973 | { |
|---|
| 974 | ref_ptr<Group> group; |
|---|
| 975 | ref_ptr<Group> subGroup; |
|---|
| 976 | TexData currentTexData; |
|---|
| 977 | const char * texName; |
|---|
| 978 | char currentTexName[256]; |
|---|
| 979 | char prefix[64]; |
|---|
| 980 | char * mtlPtr; |
|---|
| 981 | char * tmpPtr; |
|---|
| 982 | char tempTex[256]; |
|---|
| 983 | std::string entityText; |
|---|
| 984 | VBSPEntity * currentEntity; |
|---|
| 985 | int i; |
|---|
| 986 | ref_ptr<StateSet> stateSet; |
|---|
| 987 | StaticProp staticProp; |
|---|
| 988 | Matrixf transMat, rotMat; |
|---|
| 989 | Quat yaw, pitch, roll; |
|---|
| 990 | ref_ptr<MatrixTransform> propXform; |
|---|
| 991 | std::string propModel; |
|---|
| 992 | std::string propFile; |
|---|
| 993 | ref_ptr<Node> propNode; |
|---|
| 994 | |
|---|
| 995 | |
|---|
| 996 | for (i = 0; i < bsp_data->getNumTexDatas(); i++) |
|---|
| 997 | { |
|---|
| 998 | |
|---|
| 999 | currentTexData = bsp_data->getTexData(i); |
|---|
| 1000 | texName = bsp_data-> |
|---|
| 1001 | getTexDataString(currentTexData.name_string_table_id).c_str(); |
|---|
| 1002 | strcpy(currentTexName, texName); |
|---|
| 1003 | |
|---|
| 1004 | |
|---|
| 1005 | |
|---|
| 1006 | sprintf(prefix, "maps/%s/", map_name.c_str()); |
|---|
| 1007 | if (strncmp(currentTexName, prefix, strlen(prefix)) == 0) |
|---|
| 1008 | { |
|---|
| 1009 | |
|---|
| 1010 | |
|---|
| 1011 | |
|---|
| 1012 | |
|---|
| 1013 | mtlPtr = currentTexName; |
|---|
| 1014 | mtlPtr += strlen(prefix); |
|---|
| 1015 | |
|---|
| 1016 | |
|---|
| 1017 | |
|---|
| 1018 | strcpy(tempTex, mtlPtr); |
|---|
| 1019 | |
|---|
| 1020 | |
|---|
| 1021 | |
|---|
| 1022 | |
|---|
| 1023 | |
|---|
| 1024 | |
|---|
| 1025 | |
|---|
| 1026 | tmpPtr = strrchr(tempTex, '/'); |
|---|
| 1027 | mtlPtr = strrchr(tempTex, '_'); |
|---|
| 1028 | if ((mtlPtr != NULL) && (mtlPtr > tmpPtr)) |
|---|
| 1029 | *mtlPtr = 0; |
|---|
| 1030 | mtlPtr = strrchr(tempTex, '_'); |
|---|
| 1031 | if ((mtlPtr != NULL) && (mtlPtr > tmpPtr)) |
|---|
| 1032 | *mtlPtr = 0; |
|---|
| 1033 | mtlPtr = strrchr(tempTex, '_'); |
|---|
| 1034 | if ((mtlPtr != NULL) && (mtlPtr > tmpPtr)) |
|---|
| 1035 | *mtlPtr = 0; |
|---|
| 1036 | |
|---|
| 1037 | |
|---|
| 1038 | strcpy(currentTexName, tempTex); |
|---|
| 1039 | } |
|---|
| 1040 | |
|---|
| 1041 | |
|---|
| 1042 | stateSet = readMaterialFile(currentTexName); |
|---|
| 1043 | |
|---|
| 1044 | |
|---|
| 1045 | |
|---|
| 1046 | bsp_data->addStateSet(stateSet.get()); |
|---|
| 1047 | } |
|---|
| 1048 | |
|---|
| 1049 | |
|---|
| 1050 | group = new Group(); |
|---|
| 1051 | |
|---|
| 1052 | |
|---|
| 1053 | |
|---|
| 1054 | for (i = 0; i < bsp_data->getNumEntities(); i++) |
|---|
| 1055 | { |
|---|
| 1056 | |
|---|
| 1057 | entityText = bsp_data->getEntity(i); |
|---|
| 1058 | currentEntity = new VBSPEntity(entityText, bsp_data.get()); |
|---|
| 1059 | |
|---|
| 1060 | |
|---|
| 1061 | if (currentEntity->isVisible()) |
|---|
| 1062 | { |
|---|
| 1063 | |
|---|
| 1064 | subGroup = currentEntity->createGeometry(); |
|---|
| 1065 | |
|---|
| 1066 | |
|---|
| 1067 | if (subGroup.valid()) |
|---|
| 1068 | group->addChild(subGroup.get()); |
|---|
| 1069 | } |
|---|
| 1070 | |
|---|
| 1071 | |
|---|
| 1072 | delete currentEntity; |
|---|
| 1073 | } |
|---|
| 1074 | |
|---|
| 1075 | |
|---|
| 1076 | |
|---|
| 1077 | for (i = 0; i < bsp_data->getNumStaticProps(); i++) |
|---|
| 1078 | { |
|---|
| 1079 | |
|---|
| 1080 | staticProp = bsp_data->getStaticProp(i); |
|---|
| 1081 | |
|---|
| 1082 | |
|---|
| 1083 | |
|---|
| 1084 | transMat.makeTranslate(staticProp.prop_origin * 0.0254); |
|---|
| 1085 | pitch.makeRotate(osg::DegreesToRadians(staticProp.prop_angles.x()), |
|---|
| 1086 | Vec3f(0.0, 1.0, 0.0)); |
|---|
| 1087 | yaw.makeRotate(osg::DegreesToRadians(staticProp.prop_angles.y()), |
|---|
| 1088 | Vec3f(0.0, 0.0, 1.0)); |
|---|
| 1089 | roll.makeRotate(osg::DegreesToRadians(staticProp.prop_angles.z()), |
|---|
| 1090 | Vec3f(1.0, 0.0, 0.0)); |
|---|
| 1091 | rotMat.makeRotate(roll * pitch * yaw); |
|---|
| 1092 | propXform = new MatrixTransform(); |
|---|
| 1093 | propXform->setMatrix(rotMat * transMat); |
|---|
| 1094 | |
|---|
| 1095 | |
|---|
| 1096 | propModel = bsp_data->getStaticPropModel(staticProp.prop_type); |
|---|
| 1097 | propNode = osgDB::readNodeFile(propModel); |
|---|
| 1098 | |
|---|
| 1099 | |
|---|
| 1100 | if (propNode.valid()) |
|---|
| 1101 | { |
|---|
| 1102 | |
|---|
| 1103 | |
|---|
| 1104 | propXform->addChild(propNode.get()); |
|---|
| 1105 | group->addChild(propXform.get()); |
|---|
| 1106 | |
|---|
| 1107 | |
|---|
| 1108 | propXform->setName(std::string("prop_static:" + propModel)); |
|---|
| 1109 | } |
|---|
| 1110 | else |
|---|
| 1111 | { |
|---|
| 1112 | OSG_WARN << "Couldn't find static prop \"" << propModel; |
|---|
| 1113 | OSG_WARN << "\"." << std::endl; |
|---|
| 1114 | |
|---|
| 1115 | |
|---|
| 1116 | propXform = NULL; |
|---|
| 1117 | } |
|---|
| 1118 | } |
|---|
| 1119 | |
|---|
| 1120 | |
|---|
| 1121 | root_node = group.get(); |
|---|
| 1122 | } |
|---|
| 1123 | |
|---|
| 1124 | |
|---|
| 1125 | bool VBSPReader::readFile(const std::string & file) |
|---|
| 1126 | { |
|---|
| 1127 | osgDB::ifstream * mapFile = 0; |
|---|
| 1128 | Header header; |
|---|
| 1129 | int i = 0; |
|---|
| 1130 | |
|---|
| 1131 | |
|---|
| 1132 | map_name = getStrippedName(file); |
|---|
| 1133 | |
|---|
| 1134 | mapFile = new osgDB::ifstream(file.c_str(), std::ios::binary); |
|---|
| 1135 | if (!mapFile) |
|---|
| 1136 | return false; |
|---|
| 1137 | |
|---|
| 1138 | |
|---|
| 1139 | mapFile->read((char *) &header, sizeof(Header)); |
|---|
| 1140 | |
|---|
| 1141 | |
|---|
| 1142 | for (i = 0; i < MAX_LUMPS; i++) |
|---|
| 1143 | { |
|---|
| 1144 | if ((header.lump_table[i].file_offset != 0) && |
|---|
| 1145 | (header.lump_table[i].lump_length != 0)) |
|---|
| 1146 | { |
|---|
| 1147 | |
|---|
| 1148 | switch (i) |
|---|
| 1149 | { |
|---|
| 1150 | case ENTITIES_LUMP: |
|---|
| 1151 | processEntities(*mapFile, header.lump_table[i].file_offset, |
|---|
| 1152 | header.lump_table[i].lump_length); |
|---|
| 1153 | break; |
|---|
| 1154 | case PLANES_LUMP: |
|---|
| 1155 | processPlanes(*mapFile, header.lump_table[i].file_offset, |
|---|
| 1156 | header.lump_table[i].lump_length); |
|---|
| 1157 | break; |
|---|
| 1158 | case VERTICES_LUMP: |
|---|
| 1159 | processVertices(*mapFile, header.lump_table[i].file_offset, |
|---|
| 1160 | header.lump_table[i].lump_length); |
|---|
| 1161 | break; |
|---|
| 1162 | case EDGES_LUMP: |
|---|
| 1163 | processEdges(*mapFile, header.lump_table[i].file_offset, |
|---|
| 1164 | header.lump_table[i].lump_length); |
|---|
| 1165 | break; |
|---|
| 1166 | case SURFEDGES_LUMP: |
|---|
| 1167 | processSurfEdges(*mapFile, header.lump_table[i].file_offset, |
|---|
| 1168 | header.lump_table[i].lump_length); |
|---|
| 1169 | break; |
|---|
| 1170 | case MODELS_LUMP: |
|---|
| 1171 | processModels(*mapFile, header.lump_table[i].file_offset, |
|---|
| 1172 | header.lump_table[i].lump_length); |
|---|
| 1173 | break; |
|---|
| 1174 | case FACES_LUMP: |
|---|
| 1175 | processFaces(*mapFile, header.lump_table[i].file_offset, |
|---|
| 1176 | header.lump_table[i].lump_length); |
|---|
| 1177 | break; |
|---|
| 1178 | case TEXINFO_LUMP: |
|---|
| 1179 | processTexInfo(*mapFile, header.lump_table[i].file_offset, |
|---|
| 1180 | header.lump_table[i].lump_length); |
|---|
| 1181 | break; |
|---|
| 1182 | case TEXDATA_LUMP: |
|---|
| 1183 | processTexData(*mapFile, header.lump_table[i].file_offset, |
|---|
| 1184 | header.lump_table[i].lump_length); |
|---|
| 1185 | break; |
|---|
| 1186 | case TEXDATA_STRING_TABLE_LUMP: |
|---|
| 1187 | processTexDataStringTable(*mapFile, |
|---|
| 1188 | header.lump_table[i].file_offset, |
|---|
| 1189 | header.lump_table[i].lump_length); |
|---|
| 1190 | break; |
|---|
| 1191 | case TEXDATA_STRING_DATA_LUMP: |
|---|
| 1192 | processTexDataStringData(*mapFile, |
|---|
| 1193 | header.lump_table[i].file_offset, |
|---|
| 1194 | header.lump_table[i].lump_length); |
|---|
| 1195 | break; |
|---|
| 1196 | case DISPINFO_LUMP: |
|---|
| 1197 | processDispInfo(*mapFile, header.lump_table[i].file_offset, |
|---|
| 1198 | header.lump_table[i].lump_length); |
|---|
| 1199 | break; |
|---|
| 1200 | case DISP_VERTS_LUMP: |
|---|
| 1201 | processDispVerts(*mapFile, header.lump_table[i].file_offset, |
|---|
| 1202 | header.lump_table[i].lump_length); |
|---|
| 1203 | break; |
|---|
| 1204 | case GAME_LUMP: |
|---|
| 1205 | processGameData(*mapFile, header.lump_table[i].file_offset, |
|---|
| 1206 | header.lump_table[i].lump_length); |
|---|
| 1207 | break; |
|---|
| 1208 | } |
|---|
| 1209 | } |
|---|
| 1210 | } |
|---|
| 1211 | |
|---|
| 1212 | |
|---|
| 1213 | createScene(); |
|---|
| 1214 | return true; |
|---|
| 1215 | } |
|---|
| 1216 | |
|---|
| 1217 | |
|---|
| 1218 | ref_ptr<Node> VBSPReader::getRootNode() |
|---|
| 1219 | { |
|---|
| 1220 | return root_node; |
|---|
| 1221 | } |
|---|
| 1222 | |
|---|