[8344] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Patrick Boenzli |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER |
---|
| 16 | |
---|
| 17 | #include "md3_data.h" |
---|
| 18 | |
---|
[8357] | 19 | #include "md3_bone_frame.h" |
---|
[8371] | 20 | #include "md3_tag.h" |
---|
[8372] | 21 | #include "md3_mesh.h" |
---|
[8344] | 22 | |
---|
[8357] | 23 | |
---|
[8351] | 24 | namespace md3 |
---|
| 25 | { |
---|
[8344] | 26 | |
---|
| 27 | /******************************************************************************** |
---|
| 28 | * MD3Data * |
---|
| 29 | ********************************************************************************/ |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | \brief simple constructor |
---|
| 33 | */ |
---|
| 34 | MD3Data::MD3Data(const std::string& modelFileName, const std::string& skinFileName, float scale) |
---|
| 35 | { |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | this->loadModel(modelFileName); |
---|
[8354] | 40 | // this->loadSkin(skinFileName); |
---|
[8344] | 41 | } |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | /** |
---|
| 45 | \brief simple destructor |
---|
| 46 | |
---|
| 47 | this will clean out all the necessary data for a specific md2model |
---|
| 48 | */ |
---|
| 49 | MD3Data::~MD3Data() |
---|
| 50 | { |
---|
| 51 | delete this->header; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | \brief this will load the whole model data (vertices, opengl command list, ...) |
---|
| 58 | * @param fileName: the name of the model file |
---|
| 59 | \return true if success |
---|
| 60 | */ |
---|
| 61 | bool MD3Data::loadModel(const std::string& fileName) |
---|
| 62 | { |
---|
| 63 | FILE *pFile; //file stream |
---|
[8353] | 64 | // char* buffer; //buffer for frame data |
---|
[8354] | 65 | int fileOffset = 0; // file data offset |
---|
[8346] | 66 | |
---|
| 67 | |
---|
[8344] | 68 | |
---|
| 69 | //! @todo this chek should include deleting a loaded model (eventually) |
---|
| 70 | if (fileName.empty()) |
---|
| 71 | return false; |
---|
| 72 | |
---|
[8354] | 73 | PRINTF(0)("opening file: %s\n", fileName.c_str()); |
---|
[8344] | 74 | pFile = fopen(fileName.c_str(), "rb"); |
---|
| 75 | if( unlikely(!pFile)) |
---|
| 76 | { |
---|
[8347] | 77 | PRINTF(1)("Couldn't open the MD3 File for loading. Exiting.\n"); |
---|
[8344] | 78 | return false; |
---|
| 79 | } |
---|
[8357] | 80 | fileOffset += this->readHeader(pFile, fileOffset); |
---|
[8344] | 81 | /* check for the header version: make sure its a md2 file :) */ |
---|
[8354] | 82 | if( unlikely(this->header->version != MD3_VERSION) && unlikely(this->header->ident != MD3_IDENT)) |
---|
[8344] | 83 | { |
---|
| 84 | PRINTF(1)("Couldn't load file %s: invalid file format: stop loading\n", fileName.c_str()); |
---|
| 85 | return false; |
---|
| 86 | } |
---|
| 87 | |
---|
[8353] | 88 | |
---|
[8354] | 89 | // check if the filesize is correct |
---|
| 90 | if( this->header->fileSize > this->header->tagStart && |
---|
| 91 | this->header->fileSize >= this->header->meshStart) |
---|
| 92 | { |
---|
| 93 | bool bBoneFrames, bTags, bMeshes; |
---|
| 94 | bBoneFrames = ( this->header->boneFrameNum == 0); |
---|
| 95 | bTags = ( this->header->tagNum == 0); |
---|
| 96 | bMeshes = ( this->header->meshNum == 0); |
---|
| 97 | |
---|
| 98 | // read different parts of the model in correct order |
---|
| 99 | while( !(bBoneFrames && bTags && bMeshes)) |
---|
| 100 | { |
---|
[8357] | 101 | printf("while\n"); |
---|
[8354] | 102 | if( fileOffset == this->header->boneFrameStart && !bBoneFrames) |
---|
[8355] | 103 | { |
---|
| 104 | fileOffset += this->readBoneFrames(pFile, fileOffset); |
---|
| 105 | bBoneFrames = true; |
---|
| 106 | } |
---|
[8354] | 107 | else if( fileOffset == this->header->tagStart && !bTags) |
---|
[8355] | 108 | { |
---|
| 109 | fileOffset += this->readTags(pFile, fileOffset); |
---|
| 110 | bTags = true; |
---|
| 111 | } |
---|
[8354] | 112 | else if( fileOffset == this->header->meshStart && !bMeshes) |
---|
[8355] | 113 | { |
---|
| 114 | fileOffset += this->readMeshes(pFile, fileOffset); |
---|
| 115 | bMeshes = true; |
---|
| 116 | } |
---|
[8354] | 117 | } |
---|
| 118 | |
---|
| 119 | } |
---|
| 120 | |
---|
[8344] | 121 | fclose(pFile); |
---|
[8347] | 122 | |
---|
| 123 | return true; |
---|
[8344] | 124 | } |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | \brief loads the skin/material stuff |
---|
| 129 | * @param fileName: name of the skin file |
---|
| 130 | \return true if success |
---|
| 131 | */ |
---|
| 132 | bool MD3Data::loadSkin(const std::string& fileName) |
---|
| 133 | { |
---|
[8357] | 134 | // if( fileName.empty()) |
---|
| 135 | // { |
---|
| 136 | // this->skinFileName = ""; |
---|
| 137 | // return false; |
---|
| 138 | // } |
---|
| 139 | // |
---|
| 140 | // this->skinFileName = fileName; |
---|
| 141 | // |
---|
| 142 | // this->material.setName("md2ModelMaterial"); |
---|
| 143 | // this->material.setDiffuseMap(fileName); |
---|
| 144 | // this->material.setIllum(3); |
---|
| 145 | // this->material.setAmbient(1.0, 1.0, 1.0); |
---|
[8344] | 146 | |
---|
[8347] | 147 | return true; |
---|
[8344] | 148 | } |
---|
[8346] | 149 | |
---|
| 150 | |
---|
| 151 | /** |
---|
| 152 | * read heaader |
---|
| 153 | */ |
---|
[8355] | 154 | int MD3Data::readHeader(FILE* pFile, int fileOffset) |
---|
| 155 | { |
---|
[8357] | 156 | this->header = new MD3Header; |
---|
| 157 | fread(this->header, 1, sizeof(MD3Header), pFile); |
---|
| 158 | |
---|
| 159 | //header debug: |
---|
| 160 | PRINTF(0)("MD3 Header debug section======================================\n"); |
---|
| 161 | printf("ident: %i\n", this->header->ident); |
---|
| 162 | printf("version: %i\n", this->header->version); |
---|
| 163 | printf("filename: %s\n", this->header->filename); |
---|
| 164 | printf("boneFrameNum: %i\n", this->header->boneFrameNum); |
---|
| 165 | printf("tag number: %i\n", this->header->tagNum); |
---|
| 166 | printf("mesh number: %i\n", this->header->meshNum); |
---|
| 167 | printf("max tex num: %i\n", this->header->maxTexNum); |
---|
| 168 | printf("bone frame start: %i\n", this->header->boneFrameStart); |
---|
| 169 | printf("tag start: %i\n", this->header->tagStart); |
---|
| 170 | printf("mesh start: %i\n", this->header->meshStart); |
---|
| 171 | printf("fileSize: %i\n", this->header->fileSize); |
---|
| 172 | |
---|
| 173 | return sizeof(MD3Header); |
---|
[8355] | 174 | } |
---|
[8346] | 175 | |
---|
| 176 | |
---|
| 177 | /** |
---|
| 178 | * read bone frames |
---|
| 179 | */ |
---|
[8355] | 180 | int MD3Data::readBoneFrames(FILE* pFile, int fileOffset) |
---|
| 181 | { |
---|
[8357] | 182 | this->boneFrames = new MD3BoneFrame*[this->header->boneFrameNum]; |
---|
| 183 | |
---|
| 184 | for( int i = 0; i < this->header->boneFrameNum; i++) |
---|
[8358] | 185 | { |
---|
[8357] | 186 | this->boneFrames[i] = new MD3BoneFrame(i); |
---|
| 187 | |
---|
[8358] | 188 | MD3BoneFrameData* md = new MD3BoneFrameData; |
---|
| 189 | fread(md, 1, sizeof(MD3BoneFrameData), pFile); |
---|
[8372] | 190 | this->boneFrames[i]->data = md; |
---|
[8358] | 191 | } |
---|
| 192 | |
---|
| 193 | return this->header->boneFrameNum * sizeof(MD3BoneFrameData); |
---|
[8355] | 194 | } |
---|
[8346] | 195 | |
---|
| 196 | |
---|
| 197 | /** |
---|
| 198 | * read the tags |
---|
| 199 | */ |
---|
[8355] | 200 | int MD3Data::readTags(FILE* pFile, int fileOffset) |
---|
| 201 | { |
---|
[8359] | 202 | // this->boneFrames = new MD3BoneFrame*[this->header->boneFrameNum]; |
---|
| 203 | |
---|
| 204 | for( int i = 0; i < this->header->boneFrameNum; i++) |
---|
| 205 | { |
---|
[8371] | 206 | this->boneFrames[i]->tags = new MD3Tag*[this->header->tagNum]; |
---|
| 207 | |
---|
[8359] | 208 | for( int j = 0; j < this->header->tagNum; j++) |
---|
| 209 | { |
---|
[8371] | 210 | this->boneFrames[i]->tags[j] = new MD3Tag(); |
---|
| 211 | MD3TagData* md = new MD3TagData; |
---|
| 212 | fread(md, 1, sizeof(MD3TagData), pFile); |
---|
[8372] | 213 | this->boneFrames[i]->tags[j]->data = md; |
---|
[8359] | 214 | } |
---|
| 215 | } |
---|
| 216 | |
---|
[8371] | 217 | return this->header->boneFrameNum * this->header->tagNum * sizeof(MD3TagData); |
---|
[8355] | 218 | } |
---|
[8346] | 219 | |
---|
| 220 | |
---|
| 221 | /** |
---|
| 222 | * read meshes |
---|
| 223 | */ |
---|
[8355] | 224 | int MD3Data::readMeshes(FILE* pFile, int fileOffset) |
---|
| 225 | { |
---|
[8375] | 226 | int localFileOffset = fileOffset; //!< local file offset |
---|
| 227 | |
---|
[8372] | 228 | this->meshes = new MD3Mesh*[this->header->meshNum]; |
---|
| 229 | |
---|
| 230 | for( int i = 0; i < this->header->meshNum; i++) |
---|
| 231 | { |
---|
| 232 | this->meshes[i] = new MD3Mesh(); |
---|
| 233 | |
---|
[8375] | 234 | bool bTriangles, bTexVecs, bVertices, bTextures; //!< the parts that have been read so far |
---|
[8374] | 235 | |
---|
| 236 | //start reading mesh data |
---|
[8375] | 237 | MD3MeshHeader* md = new MD3MeshHeader; |
---|
| 238 | fread(md, 1, sizeof(MD3MeshHeader), pFile); |
---|
| 239 | this->meshes[i]->header = md; |
---|
| 240 | localFileOffset += sizeof(MD3MeshHeader); |
---|
[8374] | 241 | |
---|
[8375] | 242 | if( unlikely(this->meshes[i]->header->id != MD3_IDENT)) |
---|
| 243 | { |
---|
| 244 | PRINTF(1)("Wrong MD3 mesh file tag, file %s could be corrupt\n", this->filename.c_str()); |
---|
| 245 | return false; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | // check which parts to be loaded |
---|
| 249 | bTriangles = ( this->meshes[i]->header->triangleNum == 0); |
---|
| 250 | bTexVecs = ( this->meshes[i]->header->vertexNum == 0); |
---|
| 251 | bVertices = ( this->meshes[i]->header->meshFrameNum == 0); |
---|
| 252 | bTextures = ( this->meshes[i]->header->textureNum == 0); |
---|
| 253 | |
---|
| 254 | // now read the data block whise |
---|
| 255 | while( !(bTriangles && bTexVecs && bVertices && bTextures)) |
---|
| 256 | { |
---|
| 257 | if( localFileOffset == this->meshes[i]->header->triangleStart && !bTriangles) |
---|
| 258 | { |
---|
[8382] | 259 | localFileOffset += this->readMeshTriangles(pFile, localFileOffset, i); |
---|
[8375] | 260 | bTriangles = true; |
---|
| 261 | } |
---|
| 262 | else if( localFileOffset == this->meshes[i]->header->textureStart && !bTextures) |
---|
| 263 | { |
---|
[8382] | 264 | fileOffset += this->readMeshTextures(pFile, localFileOffset, i); |
---|
[8375] | 265 | bTextures = true; |
---|
| 266 | } |
---|
| 267 | else if( localFileOffset == this->meshes[i]->header->texVecStart && !bTexVecs) |
---|
| 268 | { |
---|
[8382] | 269 | localFileOffset += this->readMeshTexVecs(pFile, localFileOffset, i); |
---|
[8375] | 270 | bTexVecs = true; |
---|
| 271 | } |
---|
| 272 | else if( fileOffset == this->meshes[i]->header->vertexStart && !bVertices) |
---|
| 273 | { |
---|
[8382] | 274 | localFileOffset += this->readMeshVertices(pFile, localFileOffset, i); |
---|
[8375] | 275 | bVertices = true; |
---|
| 276 | } |
---|
| 277 | } |
---|
[8372] | 278 | } |
---|
[8375] | 279 | return localFileOffset - fileOffset; |
---|
[8355] | 280 | } |
---|
[8351] | 281 | |
---|
[8382] | 282 | |
---|
| 283 | |
---|
| 284 | int MD3Data::readMeshTriangles(FILE* pFile, int fileOffset, int mesh) |
---|
| 285 | { |
---|
| 286 | this->meshes[mesh]->triangles = new int[this->meshes[mesh]->header->triangleNum][3]; |
---|
[8353] | 287 | } |
---|
| 288 | |
---|
[8382] | 289 | int MD3Data::readMeshTextures(FILE* pFile, int fileOffset, int mesh) |
---|
| 290 | {} |
---|
| 291 | |
---|
| 292 | int MD3Data::readMeshTexVecs(FILE* pFile, int fileOffset, int mesh) |
---|
| 293 | {} |
---|
| 294 | |
---|
| 295 | int MD3Data::readMeshVertices(FILE* pFile, int fileOffset, int mesh) |
---|
| 296 | {} |
---|
| 297 | |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | |
---|
| 301 | |
---|
| 302 | |
---|
| 303 | |
---|
| 304 | |
---|
| 305 | |
---|