Changeset 4140 in orxonox.OLD for orxonox/branches
- Timestamp:
- May 10, 2005, 11:16:39 AM (20 years ago)
- Location:
- orxonox/branches/md2_loader/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/branches/md2_loader/src/lib/graphics/importer/md2Model.cc
r4138 r4140 32 32 \brief standard constructor 33 33 34 34 creates a new model 35 35 */ 36 36 MD2Model::MD2Model () 37 37 { 38 38 this->setClassName ("MD2Model"); 39 39 } 40 40 … … 50 50 51 51 /** 52 \brief draw function52 \brief draw function 53 53 54 these function will take NO argument in the final version, just for testing54 these function will take NO argument in the final version, just for testing 55 55 */ 56 56 void MD2Model::draw(t3DModel *pModel) 57 57 { 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 58 if( pModel->objectList->getSize() <= 0) return; 59 60 t3DObject *pObject = pModel->objectList->firstElement(); 61 glBegin(GL_TRIANGLES); 62 for(int j = 0; j < pObject->numOfFaces; j++) 63 { 64 for(int whichVertex = 0; whichVertex < 3; whichVertex++) 65 { 66 int index = pObject->pFaces[j].vertIndex[whichVertex]; 67 int index2 = pObject->pFaces[j].coordIndex[whichVertex]; 68 /* we invert the normals since the md2 file format uses different style */ 69 glNormal3f(-pObject->pNormals[ index ].x, -pObject->pNormals[ index ].y, -pObject->pNormals[ index ].z); 70 if(pObject->pTexVerts) 71 { 72 glTexCoord2f(pObject->pTexVerts[ index2 ].x, pObject->pTexVerts[ index2 ].y); 73 } 74 glVertex3f(pObject->pVerts[ index ].x, pObject->pVerts[ index ].y, pObject->pVerts[ index ].z); 75 } 76 } 77 glEnd(); 78 78 } 79 79 … … 84 84 /** 85 85 \brief standard deconstructor 86 86 creates a new model loader 87 87 */ 88 88 MD2Loader::MD2Loader() 89 89 { 90 91 92 93 94 95 96 90 this->setClassName ("MD2Loader"); 91 /* initialize all data to initial state */ 92 memset(&this->header, 0, sizeof(tMd2Header)); 93 this->pSkins = NULL; 94 this->pTexCoords = NULL; 95 this->pTriangles = NULL; 96 this->pFrames = NULL; 97 97 } 98 98 … … 105 105 106 106 /** 107 108 109 110 107 \brief this is called by the client to open the .Md2 file, read it, then clean up 108 \param model to load in 109 \param file name to load 110 \param texture name to load 111 111 */ 112 112 bool MD2Loader::importMD2(t3DModel *pModel, char *fileName, char *textureName) 113 113 { 114 115 116 117 118 119 120 121 122 123 124 125 126 114 this->pFile = fopen(fileName, "rb"); 115 if( unlikely(!pFile)) 116 { 117 PRINTF(1)("Couldn't open the MD2 File for loading. Exiting.\n"); 118 return false; 119 } 120 fread(&this->header, 1, sizeof(tMd2Header), pFile); 121 /* check for the header version: make sure its a md2 file :) */ 122 if( likely(this->header.version != 8)) 123 { 124 PRINTF(1)("Couldn't load file %s: invalid file format: stop loading\n", fileName); 125 return false; 126 } 127 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 128 this->readMD2Data(); 129 this->convertDataStructures(pModel); 130 131 if( likely((int)textureName)) 132 { 133 tMaterialInfo* textureInfo = new tMaterialInfo; 134 strcpy(textureInfo->strFile, textureName); 135 /* since there is only one texture for a .Md2 file, the ID is always 0 */ 136 textureInfo->texureId = 0; 137 textureInfo->uTile = 1; 138 /* we only have 1 material for a model */ 139 pModel->numOfMaterials = 1; 140 pModel->materialList->add(textureInfo); 141 } 142 142 143 144 145 } 146 147 /** 148 143 this->cleanUp(); 144 return true; 145 } 146 147 /** 148 \brief This function reads in all of the model's data, except the animation frames 149 149 */ 150 150 void MD2Loader::readMD2Data() 151 151 { 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 } 190 191 /** 192 193 152 unsigned char buffer[MD2_MAX_FRAMESIZE]; 153 this->pSkins = new tMd2Skin[this->header.numSkins]; 154 this->pTexCoords = new tMd2TexCoord[this->header.numTexCoords]; 155 this->pTriangles = new tMd2Face[this->header.numTriangles]; 156 this->pFrames = new tMd2Frame[this->header.numFrames]; 157 158 /* we read the skins */ 159 fseek(this->pFile, this->header.offsetSkins, SEEK_SET); 160 fread(this->pSkins, sizeof(tMd2Skin), this->header.numSkins, this->pFile); 161 /* read all vertex data */ 162 fseek(this->pFile, this->header.offsetTexCoords, SEEK_SET); 163 fread(this->pTexCoords, sizeof(tMd2TexCoord), this->header.numTexCoords, this->pFile); 164 /* read face data for each triangle (normals) */ 165 fseek(this->pFile, this->header.offsetTriangles, SEEK_SET); 166 fread(this->pTriangles, sizeof(tMd2Face), this->header.numTriangles, this->pFile); 167 /* reading all frame data */ 168 fseek(this->pFile, this->header.offsetFrames, SEEK_SET); 169 for(int i = 0; i < this->header.numFrames; i++) 170 { 171 tMd2AliasFrame *pFrame = (tMd2AliasFrame *) buffer; 172 this->pFrames[i].pVertices = new tMd2Triangle [this->header.numVertices]; 173 174 /* read the frame animation data */ 175 fread(pFrame, 1, this->header.frameSize, this->pFile); 176 strcpy(this->pFrames[i].strName, pFrame->name); 177 tMd2Triangle *pVertices = this->pFrames[i].pVertices; 178 /* 179 scale translations, store vertices: since id used a non-opengl xyz notation, we have to swap y and z 180 and negate z axis 181 */ 182 for(int j = 0; j < this->header.numVertices; j++) 183 { 184 pVertices[j].vertex[0] = pFrame->aliasVertices[j].vertex[0] * pFrame->scale[0] + pFrame->translate[0]; 185 pVertices[j].vertex[2] = -1 * (pFrame->aliasVertices[j].vertex[1] * pFrame->scale[1] + pFrame->translate[1]); 186 pVertices[j].vertex[1] = pFrame->aliasVertices[j].vertex[2] * pFrame->scale[2] + pFrame->translate[2]; 187 } 188 } 189 } 190 191 /** 192 \brief this function fills in the animation list for each animation by name and frame 193 \param model 194 194 */ 195 195 void MD2Loader::parseAnimations(t3DModel *pModel) 196 196 { 197 198 199 200 201 202 203 204 205 206 207 208 197 tAnimationInfo* animationInfo = new tAnimationInfo; 198 string strLastName = ""; 199 200 /* the animation parse process looks a little bit weired: this is because there are no 201 fix bounds for the animation lengths, so the frames are destingushed using their names 202 which is normaly composed of <animationname><number> 203 */ 204 205 for(int i = 0; i < pModel->numOfObjects; i++) 206 { 207 string strName = this->pFrames[i].strName; 208 int frameNum = 0; 209 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 } 236 237 /** 238 239 210 for(unsigned int j = 0; j < strName.length(); j++) 211 { 212 if( isdigit(strName[j]) && j >= strName.length() - 2) 213 { 214 frameNum = atoi(&strName[j]); 215 strName.erase(j, strName.length() - j); 216 break; 217 } 218 } 219 220 /* animations are sorted through their names: this is how its been done: */ 221 if(strName != strLastName || i == pModel->numOfObjects - 1) 222 { 223 if(strLastName != "") 224 { 225 strcpy(animationInfo->strName, strLastName.c_str()); 226 animationInfo->endFrame = i; 227 pModel->animationList->add(animationInfo); 228 memset(&animationInfo, 0, sizeof(tAnimationInfo)); 229 pModel->numOfAnimations++; 230 } 231 animationInfo->startFrame = frameNum - 1 + i; 232 } 233 strLastName = strName; 234 } 235 } 236 237 /** 238 \brief this function converts the .md2 structures to our own model and object structures: decompress 239 \param model 240 240 */ 241 241 void MD2Loader::convertDataStructures(t3DModel *pModel) 242 242 { 243 243 int j = 0, i = 0; 244 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 245 memset(pModel, 0, sizeof(t3DModel)); 246 pModel->numOfObjects = this->header.numFrames; 247 248 this->parseAnimations(pModel); 249 250 for (i = 0; i < pModel->numOfObjects; i++) 251 { 252 t3DObject* currentFrame = new t3DObject; 253 254 currentFrame->numOfVerts = this->header.numVertices; 255 currentFrame->numTexVertex = this->header.numTexCoords; 256 currentFrame->numOfFaces = this->header.numTriangles; 257 258 currentFrame->pVerts = new CVector3[currentFrame->numOfVerts]; 259 260 for (j = 0; j < currentFrame->numOfVerts; j++) 261 { 262 currentFrame->pVerts[j].x = this->pFrames[i].pVertices[j].vertex[0]; 263 currentFrame->pVerts[j].y = this->pFrames[i].pVertices[j].vertex[1]; 264 currentFrame->pVerts[j].z = this->pFrames[i].pVertices[j].vertex[2]; 265 } 266 267 delete this->pFrames[i].pVertices; 268 269 if( likely(i > 0)) 270 { 271 pModel->objectList->add(currentFrame); 272 continue; 273 } 274 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 } 297 298 299 /** 300 301 275 currentFrame->pTexVerts = new CVector2[currentFrame->numTexVertex]; 276 currentFrame->pFaces = new tFace[currentFrame->numOfFaces]; 277 278 for(j = 0; j < currentFrame->numTexVertex; j++) 279 { 280 currentFrame->pTexVerts[j].x = this->pTexCoords[j].u / float(this->header.skinWidth); 281 currentFrame->pTexVerts[j].y = 1 - this->pTexCoords[j].v / float(this->header.skinHeight); 282 } 283 284 for(j = 0; j < currentFrame->numOfFaces; j++) 285 { 286 currentFrame->pFaces[j].vertIndex[0] = this->pTriangles[j].vertexIndices[0]; 287 currentFrame->pFaces[j].vertIndex[1] = this->pTriangles[j].vertexIndices[1]; 288 currentFrame->pFaces[j].vertIndex[2] = this->pTriangles[j].vertexIndices[2]; 289 290 currentFrame->pFaces[j].coordIndex[0] = this->pTriangles[j].textureIndices[0]; 291 currentFrame->pFaces[j].coordIndex[1] = this->pTriangles[j].textureIndices[1]; 292 currentFrame->pFaces[j].coordIndex[2] = this->pTriangles[j].textureIndices[2]; 293 } 294 pModel->objectList->add(currentFrame); 295 } 296 } 297 298 299 /** 300 \brief this function conputes the normals of the model 301 \param model 302 302 */ 303 303 void MD2Loader::computeNormals(t3DModel *pModel) … … 306 306 307 307 /** 308 308 \brief This function cleans up our allocated memory and closes the file 309 309 */ 310 310 void MD2Loader::cleanUp() 311 311 { 312 312 fclose(this->pFile); 313 313 314 315 316 317 318 } 314 if( this->pSkins) delete [] this->pSkins; 315 if( this->pTexCoords) delete this->pTexCoords; 316 if( this->pTriangles) delete this->pTriangles; 317 if( this->pFrames) delete this->pFrames; 318 } -
orxonox/branches/md2_loader/src/story_entities/world.cc
r4139 r4140 491 491 this->model = new t3DModel; 492 492 this->md2Model = new MD2Model(); 493 494 md2loader->importMD2(model, " test.md2", "test.bmp");493 494 md2loader->importMD2(model, "../data/models/jack/tris.md2", "test.bmp"); 495 495 496 496 } … … 953 953 954 954 /* TESTGING TESTING TESTING */ 955 this->md2Model->draw(this->model);955 //this->md2Model->draw(this->model); 956 956 957 957 glCallList (objectList);
Note: See TracChangeset
for help on using the changeset viewer.