int XFileLoader::ReadMesh(istream& s) { vector<vec3> positions; vector<vec2> texCoords; vector<unsigned short> indices; vector<vec3> normals; vector<unsigned short> nindices; XFileToken t = XFileToken::NextToken(s); if (t.m_type == XFileToken::Identifier) { //m_meshes[meshIdx].m_name = t.m_content; t = XFileToken::NextToken(s); } if (t.m_type != XFileToken::LeftBrace) THROW_EXCEPTION_T("Parsing error", ParsingException); int nElems; ReadMember(s, nElems); positions.resize(nElems); ReadArray(s, positions.begin(), positions.end()); ReadFaceArray(s, indices); int materialIdx = -1; while (true) { t = XFileToken::NextToken(s); if (t.m_type == XFileToken::Identifier) { if (t.m_content == "MeshNormals") ReadMeshNormals(s, normals, nindices); else if(t.m_content == "MeshMaterialList") materialIdx = ReadMeshMaterials(s); else if(t.m_content == "MeshTextureCoords") ReadTexCoords(s, texCoords); else SkipDataObject(s); } else if (t.m_type == XFileToken::LeftBrace) SkipDataReference(s); else if (t.m_type == XFileToken::RightBrace) break; else THROW_EXCEPTION_T("Parsing error", ParsingException); } if (!texCoords.empty() && texCoords.size() != positions.size()) THROW_EXCEPTION_T("Parsing error", ParsingException); return CreateMesh(positions, texCoords, indices, normals, nindices, materialIdx); }
void C3DS::ParseChunk(stChunk* Chunk){ while(Chunk->bytesRead < Chunk->length){ stChunk tempChunk = {0, 0, 0}; ReadChunk(&tempChunk); switch( tempChunk.ID){ // HEADER OUR ENTRY POINT case EDIT3DS: //0x3D3D ParseChunk(&tempChunk); break; // MATERIALS case MATERIAL: //0xAFFF stMaterial newMaterial; m_pMaterials.push_back(newMaterial); m_iNumMaterials++; ParseChunk(&tempChunk); break; case MAT_NAME: //0xA000 - sz for hte material name "e.g. default 2" GetMaterialName(&tempChunk); break; case MAT_DIFFUSE: // Diffuse Colour //0xA020 GetDiffuseColour(&tempChunk); break; case MAT_TEXMAP: //0xA200 - if there's a texture wrapped to it where here ParseChunk(&tempChunk); break; case MAT_TEXFLNM: // 0xA300 - get filename of the material GetTexFileName(&tempChunk); break; // OBJECT - MESH'S case NAMED_OBJECT:{ stMesh newMesh; m_pMeshs.push_back(newMesh); m_iNumMeshs++; GetMeshObjectName(&tempChunk); } break; case OBJ_MESH: //0x4100 ParseChunk(&tempChunk); break; case MESH_VERTICES: //0x4110 ReadMeshVertices(&tempChunk); break; case MESH_FACES: //0x4120 ReadMeshFaces(&tempChunk); break; case MESH_TEX_VERT: //0x4140 ReadMeshTexCoords(&tempChunk); break; case MESH_MATER: //0x4130 ReadMeshMaterials(&tempChunk); break; default: SkipChunk(&tempChunk); } Chunk->bytesRead += tempChunk.length; } }
void C3DS::ParseChunk(stChunk* Chunk) { while(Chunk->bytesRead < Chunk->length) { stChunk tempChunk = {0}; ReadChunk(&tempChunk); // DEBUG CHUNKS //char buf[2000]; //sprintf(buf, "Chunk ID: %.4x\t Size: %d\n", tempChunk.ID, tempChunk.length); //debug_op(buf); switch( tempChunk.ID) { // HEADER OUR ENTRY POINT case EDIT3DS: //0x3D3D ParseChunk(&tempChunk); break; // MATERIALS case MATERIAL: //0xAFFF { stMaterial newMaterial; m_pMaterials.push_back(newMaterial); m_iNumMaterials++; } ParseChunk(&tempChunk); break; case MAT_NAME: //0xA000 - sz for hte material name "e.g. default 2" GetMaterialName(&tempChunk); break; case MAT_AMBIENT: GetAmbientColour(&tempChunk); break; case MAT_SPECULAR: GetSpecularColour(&tempChunk); break; case MAT_DIFFUSE: // Diffuse Colour //0xA020 GetDiffuseColour(&tempChunk); break; case MAT_TEXMAP: //0xA200 - if there's a texture wrapped to it where here ParseChunk(&tempChunk); break; case MAT_TEXFLNM: // 0xA300 - get filename of the material GetTexFileName(&tempChunk); break; // OBJECT - MESH'S case NAMED_OBJECT: //0x4000 { stMesh newMesh; m_pMeshs.push_back(newMesh); m_iNumMeshs++; GetMeshObjectName(&tempChunk); } break; case OBJ_MESH: //0x4100 ParseChunk(&tempChunk); break; case MESH_VERTICES: //0x4110 ReadMeshVertices(&tempChunk); break; case MESH_FACES: //0x4120 ReadMeshFaces(&tempChunk); break; case MESH_TEX_VERT: //0x4140 ReadMeshTexCoords(&tempChunk); break; case MESH_MATER: //0x4130 ReadMeshMaterials(&tempChunk); break; // ANIMATION case KEYF3DS: //0xB000 ParseChunk(&tempChunk); break; case ANIM_S_E_TIME: //0xB008 StartEndFrames(&tempChunk); break; case ANIM_OBJ: //0xB002 { stAnimation newAnimation; m_pAnimation.push_back(newAnimation); m_iNumAnimObjects++; } ParseChunk(&tempChunk); break; case ANIM_NAME: ReadNameOfObjectToAnimate(&tempChunk); break; case ANIM_PIVOT: // 0xB013 ReadPivotPoint(&tempChunk); break; case ANIM_POS: // 0xB020 ReadAnimPos(&tempChunk); break; case ANIM_ROT: // 0xB021 ReadAnimRot(&tempChunk); break; case ANIM_SCALE: // 0xB022 ReadAnimScale(&tempChunk); break; default: SkipChunk(&tempChunk); } Chunk->bytesRead += tempChunk.length; } }