| 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/StateSet> |
|---|
| 12 | #include <osg/Texture1D> |
|---|
| 13 | #include <osg/Texture2D> |
|---|
| 14 | #include <osg/Texture3D> |
|---|
| 15 | #include <osgDB/Registry> |
|---|
| 16 | #include <osgDB/FileUtils> |
|---|
| 17 | #include <osgDB/ReadFile> |
|---|
| 18 | #include <osg/io_utils> |
|---|
| 19 | #include <iostream> |
|---|
| 20 | |
|---|
| 21 | #include "MDLReader.h" |
|---|
| 22 | #include "VVDReader.h" |
|---|
| 23 | #include "VTXReader.h" |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | using namespace mdl; |
|---|
| 27 | using namespace osg; |
|---|
| 28 | using namespace osgDB; |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | MDLReader::MDLReader() |
|---|
| 32 | { |
|---|
| 33 | |
|---|
| 34 | root_node = NULL; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | MDLReader::~MDLReader() |
|---|
| 39 | { |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | std::string MDLReader::getToken(std::string str, const char * delim, |
|---|
| 44 | size_t & index) |
|---|
| 45 | { |
|---|
| 46 | size_t start; |
|---|
| 47 | size_t end = std::string::npos; |
|---|
| 48 | std::string token; |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | start = str.find_first_not_of(" \t\n\r\"", index); |
|---|
| 52 | if (start != std::string::npos) |
|---|
| 53 | { |
|---|
| 54 | |
|---|
| 55 | end = str.find_first_of(" \t\n\r\"", start+1); |
|---|
| 56 | if (end != std::string::npos) |
|---|
| 57 | { |
|---|
| 58 | |
|---|
| 59 | token = str.substr(start, end-start); |
|---|
| 60 | } |
|---|
| 61 | else |
|---|
| 62 | { |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | token = str.substr(start); |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | else |
|---|
| 69 | { |
|---|
| 70 | |
|---|
| 71 | token = ""; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | if (end != std::string::npos) |
|---|
| 77 | index = end+1; |
|---|
| 78 | else |
|---|
| 79 | index = std::string::npos; |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | return token; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | ref_ptr<Texture> MDLReader::readTextureFile(std::string textureName) |
|---|
| 87 | { |
|---|
| 88 | std::string texFile; |
|---|
| 89 | std::string texPath; |
|---|
| 90 | osg::ref_ptr<Image> texImage; |
|---|
| 91 | osg::ref_ptr<Texture> texture; |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | texFile = std::string(textureName) + ".vtf"; |
|---|
| 95 | texPath = findDataFile(texFile, CASE_INSENSITIVE); |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | if (texPath.empty()) |
|---|
| 99 | { |
|---|
| 100 | |
|---|
| 101 | if ((textureName[0] == '\\') || (textureName[0] == '/')) |
|---|
| 102 | texFile = "materials" + std::string(textureName) + ".vtf"; |
|---|
| 103 | else |
|---|
| 104 | texFile = "materials/" + std::string(textureName) + ".vtf"; |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | texPath = findDataFile(texFile, CASE_INSENSITIVE); |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | if (texPath.empty()) |
|---|
| 113 | { |
|---|
| 114 | |
|---|
| 115 | if ((textureName[0] == '\\') || (textureName[0] == '/')) |
|---|
| 116 | texFile = "../materials" + std::string(textureName) + ".vtf"; |
|---|
| 117 | else |
|---|
| 118 | texFile = "../materials/" + std::string(textureName) + ".vtf"; |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | texPath = findDataFile(texFile, CASE_INSENSITIVE); |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | if (!texPath.empty()) |
|---|
| 127 | { |
|---|
| 128 | texImage = readRefImageFile(texPath); |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | if (texImage.valid()) |
|---|
| 132 | { |
|---|
| 133 | |
|---|
| 134 | if (texImage->t() == 1) |
|---|
| 135 | { |
|---|
| 136 | texture = new Texture1D(texImage.get()); |
|---|
| 137 | } |
|---|
| 138 | else if (texImage->r() == 1) |
|---|
| 139 | { |
|---|
| 140 | texture = new Texture2D(texImage.get()); |
|---|
| 141 | } |
|---|
| 142 | else |
|---|
| 143 | { |
|---|
| 144 | texture = new Texture3D(texImage.get()); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | texture->setWrap(Texture::WRAP_S, Texture::REPEAT); |
|---|
| 149 | texture->setWrap(Texture::WRAP_T, Texture::REPEAT); |
|---|
| 150 | texture->setWrap(Texture::WRAP_R, Texture::REPEAT); |
|---|
| 151 | texture->setFilter(Texture::MAG_FILTER, Texture::LINEAR); |
|---|
| 152 | texture->setFilter(Texture::MIN_FILTER, |
|---|
| 153 | Texture::LINEAR_MIPMAP_LINEAR); |
|---|
| 154 | } |
|---|
| 155 | else |
|---|
| 156 | { |
|---|
| 157 | |
|---|
| 158 | notify(WARN) << "Couldn't find texture " << textureName; |
|---|
| 159 | notify(WARN) << std::endl; |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | texture = NULL; |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | else |
|---|
| 166 | { |
|---|
| 167 | |
|---|
| 168 | notify(WARN) << "Couldn't find texture " << textureName; |
|---|
| 169 | notify(WARN) << std::endl; |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | texture = NULL; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | return texture; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | ref_ptr<StateSet> MDLReader::readMaterialFile(std::string materialName) |
|---|
| 180 | { |
|---|
| 181 | std::string mtlFileName; |
|---|
| 182 | std::string mtlPath; |
|---|
| 183 | StringList::iterator searchItr; |
|---|
| 184 | std::ifstream * mtlFile; |
|---|
| 185 | std::string line; |
|---|
| 186 | size_t start; |
|---|
| 187 | std::string token; |
|---|
| 188 | bool found; |
|---|
| 189 | ref_ptr<StateSet> stateSet; |
|---|
| 190 | std::string shaderName; |
|---|
| 191 | std::string texName; |
|---|
| 192 | std::string tex2Name; |
|---|
| 193 | ref_ptr<Texture> texture; |
|---|
| 194 | ref_ptr<Texture> texture2; |
|---|
| 195 | ref_ptr<Material> material; |
|---|
| 196 | ref_ptr<BlendFunc> blend; |
|---|
| 197 | bool translucent; |
|---|
| 198 | double alpha; |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | mtlFileName = std::string(materialName) + ".vmt"; |
|---|
| 202 | mtlPath = findDataFile(mtlFileName, CASE_INSENSITIVE); |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | if (mtlPath.empty()) |
|---|
| 206 | { |
|---|
| 207 | searchItr = texture_paths.begin(); |
|---|
| 208 | while ((mtlPath.empty()) && (searchItr != texture_paths.end())) |
|---|
| 209 | { |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | if (((*searchItr)[0] == '\\') || ((*searchItr)[0] == '/')) |
|---|
| 214 | mtlFileName = "materials" + *searchItr + |
|---|
| 215 | std::string(materialName) + ".vmt"; |
|---|
| 216 | else |
|---|
| 217 | mtlFileName = "materials/" + *searchItr + |
|---|
| 218 | std::string(materialName) + ".vmt"; |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | mtlPath = findDataFile(mtlFileName, CASE_INSENSITIVE); |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | searchItr++; |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | if (mtlPath.empty()) |
|---|
| 231 | { |
|---|
| 232 | searchItr = texture_paths.begin(); |
|---|
| 233 | while ((mtlPath.empty()) && (searchItr != texture_paths.end())) |
|---|
| 234 | { |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | if (((*searchItr)[0] == '\\') || ((*searchItr)[0] == '/')) |
|---|
| 239 | mtlFileName = "../materials" + *searchItr + |
|---|
| 240 | std::string(materialName) + ".vmt"; |
|---|
| 241 | else |
|---|
| 242 | mtlFileName = "../materials/" + *searchItr + |
|---|
| 243 | std::string(materialName) + ".vmt"; |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | mtlPath = findDataFile(mtlFileName, CASE_INSENSITIVE); |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | searchItr++; |
|---|
| 250 | } |
|---|
| 251 | } |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | if (!mtlPath.empty()) |
|---|
| 256 | { |
|---|
| 257 | |
|---|
| 258 | mtlFile = new std::ifstream(mtlPath.c_str(), std::ifstream::in); |
|---|
| 259 | if (!mtlFile) |
|---|
| 260 | return NULL; |
|---|
| 261 | } |
|---|
| 262 | else |
|---|
| 263 | { |
|---|
| 264 | |
|---|
| 265 | notify(WARN) << "Can't find material " << materialName << std::endl; |
|---|
| 266 | return NULL; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | found = false; |
|---|
| 271 | while ((!found) && (!mtlFile->eof())) |
|---|
| 272 | { |
|---|
| 273 | |
|---|
| 274 | std::getline(*mtlFile, line); |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | start = 0; |
|---|
| 278 | token = getToken(line, " \t\n\r\"", start); |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | if ((!token.empty()) && (token.compare(0, 2, "//") != 0)) |
|---|
| 282 | { |
|---|
| 283 | shaderName = token; |
|---|
| 284 | found = true; |
|---|
| 285 | } |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | if (!found) |
|---|
| 290 | { |
|---|
| 291 | mtlFile->close(); |
|---|
| 292 | notify(WARN) << "Material " << materialName << " isn't valid."; |
|---|
| 293 | notify(WARN) << std::endl; |
|---|
| 294 | return NULL; |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | |
|---|
| 298 | texture = NULL; |
|---|
| 299 | texture2 = NULL; |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | translucent = false; |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | alpha = 1.0; |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | while (!mtlFile->eof()) |
|---|
| 309 | { |
|---|
| 310 | |
|---|
| 311 | std::getline(*mtlFile, line); |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | start = 0; |
|---|
| 315 | token = getToken(line, " \t\n\r\"", start); |
|---|
| 316 | |
|---|
| 317 | while ((!token.empty()) && (token.compare(0, 2, "//") != 0)) |
|---|
| 318 | { |
|---|
| 319 | if (equalCaseInsensitive(token, "$basetexture")) |
|---|
| 320 | { |
|---|
| 321 | |
|---|
| 322 | token = getToken(line, " \t\n\r\"", start); |
|---|
| 323 | |
|---|
| 324 | |
|---|
| 325 | if (!token.empty()) |
|---|
| 326 | texture = readTextureFile(token); |
|---|
| 327 | } |
|---|
| 328 | else if (equalCaseInsensitive(token, "$basetexture2")) |
|---|
| 329 | { |
|---|
| 330 | |
|---|
| 331 | token = getToken(line, " \t\n\r\"", start); |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | if (!token.empty()) |
|---|
| 335 | texture2 = readTextureFile(token); |
|---|
| 336 | } |
|---|
| 337 | else if ((equalCaseInsensitive(token, "$translucent")) || |
|---|
| 338 | (equalCaseInsensitive(token, "$alphatest"))) |
|---|
| 339 | { |
|---|
| 340 | |
|---|
| 341 | token = getToken(line, " \t\n\r\"", start); |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | if (!token.empty()) |
|---|
| 345 | { |
|---|
| 346 | if ((token == "1") || (token == "true")) |
|---|
| 347 | translucent = true; |
|---|
| 348 | } |
|---|
| 349 | } |
|---|
| 350 | else if (equalCaseInsensitive(token, "$alpha")) |
|---|
| 351 | { |
|---|
| 352 | |
|---|
| 353 | token = getToken(line, " \t\n\r\"", start); |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | if (!token.empty()) |
|---|
| 357 | { |
|---|
| 358 | alpha = osg::asciiToDouble(token.c_str()); |
|---|
| 359 | } |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | token = getToken(line, " \t\n\r\"", start); |
|---|
| 364 | } |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | |
|---|
| 368 | stateSet = NULL; |
|---|
| 369 | |
|---|
| 370 | |
|---|
| 371 | if (equalCaseInsensitive(shaderName, "UnlitGeneric")) |
|---|
| 372 | { |
|---|
| 373 | |
|---|
| 374 | stateSet = new StateSet(); |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | stateSet->setMode(GL_LIGHTING, StateAttribute::OFF); |
|---|
| 378 | |
|---|
| 379 | |
|---|
| 380 | if (texture != NULL) |
|---|
| 381 | { |
|---|
| 382 | stateSet->setTextureAttributeAndModes(0, texture.get(), |
|---|
| 383 | StateAttribute::ON); |
|---|
| 384 | } |
|---|
| 385 | else |
|---|
| 386 | { |
|---|
| 387 | notify(WARN) << "No base texture for material " << materialName; |
|---|
| 388 | notify(WARN) << std::endl; |
|---|
| 389 | stateSet->setTextureMode(0, GL_TEXTURE_2D, StateAttribute::OFF); |
|---|
| 390 | } |
|---|
| 391 | |
|---|
| 392 | |
|---|
| 393 | if (translucent) |
|---|
| 394 | { |
|---|
| 395 | |
|---|
| 396 | blend = new BlendFunc(BlendFunc::SRC_ALPHA, |
|---|
| 397 | BlendFunc::ONE_MINUS_SRC_ALPHA); |
|---|
| 398 | stateSet->setAttributeAndModes(blend.get(), StateAttribute::ON); |
|---|
| 399 | stateSet->setMode(GL_BLEND, StateAttribute::ON); |
|---|
| 400 | |
|---|
| 401 | |
|---|
| 402 | stateSet->setRenderingHint(StateSet::TRANSPARENT_BIN); |
|---|
| 403 | } |
|---|
| 404 | } |
|---|
| 405 | else |
|---|
| 406 | { |
|---|
| 407 | |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | stateSet = new StateSet(); |
|---|
| 411 | |
|---|
| 412 | |
|---|
| 413 | material = new Material(); |
|---|
| 414 | material->setAmbient(Material::FRONT_AND_BACK, |
|---|
| 415 | Vec4(1.0, 1.0, 1.0, 1.0) ); |
|---|
| 416 | material->setDiffuse(Material::FRONT_AND_BACK, |
|---|
| 417 | Vec4(1.0, 1.0, 1.0, 1.0) ); |
|---|
| 418 | material->setSpecular(Material::FRONT_AND_BACK, |
|---|
| 419 | Vec4(0.0, 0.0, 0.0, 1.0) ); |
|---|
| 420 | material->setShininess(Material::FRONT_AND_BACK, 1.0); |
|---|
| 421 | material->setEmission(Material::FRONT_AND_BACK, |
|---|
| 422 | Vec4(0.0, 0.0, 0.0, 1.0) ); |
|---|
| 423 | material->setAlpha(Material::FRONT_AND_BACK, alpha); |
|---|
| 424 | stateSet->setAttributeAndModes(material.get(), StateAttribute::ON); |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | if (texture != NULL) |
|---|
| 428 | { |
|---|
| 429 | stateSet->setTextureAttributeAndModes(0, texture.get(), |
|---|
| 430 | StateAttribute::ON); |
|---|
| 431 | |
|---|
| 432 | |
|---|
| 433 | if ((translucent) || (alpha < 1.0)) |
|---|
| 434 | { |
|---|
| 435 | |
|---|
| 436 | blend = new BlendFunc(BlendFunc::SRC_ALPHA, |
|---|
| 437 | BlendFunc::ONE_MINUS_SRC_ALPHA); |
|---|
| 438 | stateSet->setAttributeAndModes(blend.get(), |
|---|
| 439 | StateAttribute::ON); |
|---|
| 440 | stateSet->setMode(GL_BLEND, StateAttribute::ON); |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | stateSet->setRenderingHint(StateSet::TRANSPARENT_BIN); |
|---|
| 444 | } |
|---|
| 445 | } |
|---|
| 446 | else |
|---|
| 447 | { |
|---|
| 448 | notify(WARN) << "No base texture for material " << materialName; |
|---|
| 449 | notify(WARN) << std::endl; |
|---|
| 450 | stateSet->setTextureMode(0, GL_TEXTURE_2D, StateAttribute::OFF); |
|---|
| 451 | } |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | |
|---|
| 455 | mtlFile->close(); |
|---|
| 456 | |
|---|
| 457 | |
|---|
| 458 | return stateSet; |
|---|
| 459 | } |
|---|
| 460 | |
|---|
| 461 | |
|---|
| 462 | BodyPart * MDLReader::processBodyPart(std::istream * str, int offset) |
|---|
| 463 | { |
|---|
| 464 | int i; |
|---|
| 465 | MDLBodyPart * part; |
|---|
| 466 | BodyPart * partNode; |
|---|
| 467 | Model * modelNode; |
|---|
| 468 | |
|---|
| 469 | |
|---|
| 470 | str->seekg(offset); |
|---|
| 471 | |
|---|
| 472 | |
|---|
| 473 | part = new MDLBodyPart; |
|---|
| 474 | str->read((char *) part, sizeof(MDLBodyPart)); |
|---|
| 475 | |
|---|
| 476 | |
|---|
| 477 | partNode = new BodyPart(part); |
|---|
| 478 | |
|---|
| 479 | |
|---|
| 480 | for (i = 0; i < part->num_models; i++) |
|---|
| 481 | { |
|---|
| 482 | |
|---|
| 483 | modelNode = processModel(str, offset + part->model_offset + |
|---|
| 484 | (i * sizeof(MDLModel))); |
|---|
| 485 | |
|---|
| 486 | |
|---|
| 487 | partNode->addModel(modelNode); |
|---|
| 488 | } |
|---|
| 489 | |
|---|
| 490 | |
|---|
| 491 | return partNode; |
|---|
| 492 | } |
|---|
| 493 | |
|---|
| 494 | |
|---|
| 495 | Model * MDLReader::processModel(std::istream * str, int offset) |
|---|
| 496 | { |
|---|
| 497 | int i; |
|---|
| 498 | MDLModel * model; |
|---|
| 499 | Model * modelNode; |
|---|
| 500 | Mesh * meshNode; |
|---|
| 501 | |
|---|
| 502 | |
|---|
| 503 | str->seekg(offset); |
|---|
| 504 | |
|---|
| 505 | |
|---|
| 506 | model = new MDLModel; |
|---|
| 507 | str->read((char *) model, sizeof(MDLModel)); |
|---|
| 508 | |
|---|
| 509 | |
|---|
| 510 | modelNode = new Model(model); |
|---|
| 511 | |
|---|
| 512 | |
|---|
| 513 | for (i = 0; i < model->num_meshes; i++) |
|---|
| 514 | { |
|---|
| 515 | |
|---|
| 516 | meshNode = processMesh(str, offset + model->mesh_offset + |
|---|
| 517 | (i * sizeof(MDLMesh))); |
|---|
| 518 | |
|---|
| 519 | |
|---|
| 520 | modelNode->addMesh(meshNode); |
|---|
| 521 | } |
|---|
| 522 | |
|---|
| 523 | |
|---|
| 524 | return modelNode; |
|---|
| 525 | } |
|---|
| 526 | |
|---|
| 527 | |
|---|
| 528 | Mesh * MDLReader::processMesh(std::istream * str, int offset) |
|---|
| 529 | { |
|---|
| 530 | MDLMesh * mesh; |
|---|
| 531 | Mesh * meshNode; |
|---|
| 532 | |
|---|
| 533 | |
|---|
| 534 | str->seekg(offset); |
|---|
| 535 | |
|---|
| 536 | |
|---|
| 537 | mesh = new MDLMesh; |
|---|
| 538 | str->read((char *) mesh, sizeof(MDLMesh)); |
|---|
| 539 | |
|---|
| 540 | |
|---|
| 541 | meshNode = new Mesh(mesh); |
|---|
| 542 | |
|---|
| 543 | |
|---|
| 544 | meshNode->setStateSet((state_sets[mesh->material_index]).get()); |
|---|
| 545 | |
|---|
| 546 | |
|---|
| 547 | return meshNode; |
|---|
| 548 | } |
|---|
| 549 | |
|---|
| 550 | |
|---|
| 551 | bool MDLReader::readFile(const std::string & file) |
|---|
| 552 | { |
|---|
| 553 | std::string baseName; |
|---|
| 554 | std::string fileName; |
|---|
| 555 | std::ifstream * mdlFile; |
|---|
| 556 | MDLHeader header; |
|---|
| 557 | int i; |
|---|
| 558 | unsigned int j; |
|---|
| 559 | int offset; |
|---|
| 560 | MDLRoot * mdlRoot; |
|---|
| 561 | BodyPart * partNode; |
|---|
| 562 | std::string vvdFile; |
|---|
| 563 | VVDReader * vvdReader; |
|---|
| 564 | std::string vtxFile; |
|---|
| 565 | VTXReader * vtxReader; |
|---|
| 566 | |
|---|
| 567 | |
|---|
| 568 | mdl_name = getStrippedName(file); |
|---|
| 569 | |
|---|
| 570 | |
|---|
| 571 | fileName = findDataFile(file, CASE_INSENSITIVE); |
|---|
| 572 | mdlFile = new std::ifstream(fileName.c_str(), std::ios::binary); |
|---|
| 573 | if (!mdlFile) |
|---|
| 574 | { |
|---|
| 575 | osg::notify(osg::NOTICE) << "MDL file not found" << std::endl; |
|---|
| 576 | return false; |
|---|
| 577 | } |
|---|
| 578 | |
|---|
| 579 | |
|---|
| 580 | mdlFile->read((char *) &header, sizeof(MDLHeader)); |
|---|
| 581 | |
|---|
| 582 | |
|---|
| 583 | if (header.magic_number != MDL_MAGIC_NUMBER) |
|---|
| 584 | { |
|---|
| 585 | osg::notify(osg::NOTICE) << "This is not a valid .mdl file"; |
|---|
| 586 | osg::notify(osg::NOTICE) << std::endl; |
|---|
| 587 | |
|---|
| 588 | |
|---|
| 589 | mdlFile->close(); |
|---|
| 590 | delete mdlFile; |
|---|
| 591 | |
|---|
| 592 | return false; |
|---|
| 593 | } |
|---|
| 594 | |
|---|
| 595 | |
|---|
| 596 | |
|---|
| 597 | |
|---|
| 598 | |
|---|
| 599 | |
|---|
| 600 | for (i = 0; i < header.num_texture_paths; i++) |
|---|
| 601 | { |
|---|
| 602 | int texPathBase; |
|---|
| 603 | int texPathOffset; |
|---|
| 604 | char texPath[256]; |
|---|
| 605 | |
|---|
| 606 | texPathBase = header.texture_path_offset + (i * sizeof(int)); |
|---|
| 607 | mdlFile->seekg(texPathBase); |
|---|
| 608 | mdlFile->read((char *) &texPathOffset, sizeof(int)); |
|---|
| 609 | mdlFile->seekg(texPathOffset); |
|---|
| 610 | |
|---|
| 611 | |
|---|
| 612 | j = 0; |
|---|
| 613 | do |
|---|
| 614 | { |
|---|
| 615 | mdlFile->get(texPath[j]); |
|---|
| 616 | j++; |
|---|
| 617 | } |
|---|
| 618 | while ((j < sizeof(texPath)) && (texPath[j-1] != 0)); |
|---|
| 619 | |
|---|
| 620 | |
|---|
| 621 | texture_paths.push_back(texPath); |
|---|
| 622 | } |
|---|
| 623 | |
|---|
| 624 | |
|---|
| 625 | |
|---|
| 626 | for (i = 0; i < header.num_textures; i++) |
|---|
| 627 | { |
|---|
| 628 | int texBase; |
|---|
| 629 | MDLTexture tempTex; |
|---|
| 630 | char texName[256]; |
|---|
| 631 | ref_ptr<StateSet> stateSet; |
|---|
| 632 | |
|---|
| 633 | texBase = header.texture_offset + (i * sizeof(MDLTexture)); |
|---|
| 634 | mdlFile->seekg(texBase); |
|---|
| 635 | mdlFile->read((char *) &tempTex, sizeof(MDLTexture)); |
|---|
| 636 | mdlFile->seekg(texBase + tempTex.tex_name_offset); |
|---|
| 637 | j = 0; |
|---|
| 638 | do |
|---|
| 639 | { |
|---|
| 640 | mdlFile->get(texName[j]); |
|---|
| 641 | j++; |
|---|
| 642 | } |
|---|
| 643 | while ((j < sizeof(texName)) && (texName[j-1] != 0)); |
|---|
| 644 | |
|---|
| 645 | |
|---|
| 646 | stateSet = readMaterialFile(texName); |
|---|
| 647 | |
|---|
| 648 | |
|---|
| 649 | state_sets.push_back(stateSet); |
|---|
| 650 | } |
|---|
| 651 | |
|---|
| 652 | |
|---|
| 653 | mdlRoot = new MDLRoot(); |
|---|
| 654 | |
|---|
| 655 | |
|---|
| 656 | for (i = 0; i < header.num_body_parts; i++) |
|---|
| 657 | { |
|---|
| 658 | |
|---|
| 659 | offset = header.body_part_offset + (i * sizeof(MDLBodyPart)); |
|---|
| 660 | |
|---|
| 661 | |
|---|
| 662 | partNode = processBodyPart(mdlFile, offset); |
|---|
| 663 | |
|---|
| 664 | |
|---|
| 665 | mdlRoot->addBodyPart(partNode); |
|---|
| 666 | } |
|---|
| 667 | |
|---|
| 668 | |
|---|
| 669 | vvdFile = findDataFile(getNameLessExtension(file) + ".vvd", |
|---|
| 670 | CASE_INSENSITIVE); |
|---|
| 671 | vvdReader = new VVDReader(); |
|---|
| 672 | vvdReader->readFile(vvdFile); |
|---|
| 673 | |
|---|
| 674 | |
|---|
| 675 | |
|---|
| 676 | |
|---|
| 677 | vtxFile = findDataFile(getNameLessExtension(file) + ".dx90.vtx", |
|---|
| 678 | CASE_INSENSITIVE); |
|---|
| 679 | vtxReader = new VTXReader(vvdReader, mdlRoot); |
|---|
| 680 | vtxReader->readFile(vtxFile); |
|---|
| 681 | |
|---|
| 682 | |
|---|
| 683 | root_node = vtxReader->getModel(); |
|---|
| 684 | |
|---|
| 685 | |
|---|
| 686 | mdlFile->close(); |
|---|
| 687 | delete mdlFile; |
|---|
| 688 | |
|---|
| 689 | |
|---|
| 690 | delete vvdReader; |
|---|
| 691 | delete vtxReader; |
|---|
| 692 | |
|---|
| 693 | |
|---|
| 694 | delete mdlRoot; |
|---|
| 695 | |
|---|
| 696 | |
|---|
| 697 | return true; |
|---|
| 698 | } |
|---|
| 699 | |
|---|
| 700 | |
|---|
| 701 | ref_ptr<Node> MDLReader::getRootNode() |
|---|
| 702 | { |
|---|
| 703 | return root_node; |
|---|
| 704 | } |
|---|
| 705 | |
|---|