void compile(const char* path, CompileOptions& opts) { Buffer buf = opts.read(path); TempAllocator4096 ta; JsonObject object(ta); sjson::parse(buf, object); Array<TextureData> texdata(default_allocator()); Array<UniformData> unidata(default_allocator()); Array<char> names(default_allocator()); Array<char> dynblob(default_allocator()); DynamicString shader(ta); sjson::parse_string(object["shader"], shader); parse_textures(object["textures"], texdata, names, dynblob, opts); parse_uniforms(object["uniforms"], unidata, names, dynblob, opts); MaterialResource mr; mr.version = RESOURCE_VERSION_MATERIAL; mr.shader = shader.to_string_id(); mr.num_textures = array::size(texdata); mr.texture_data_offset = sizeof(mr); mr.num_uniforms = array::size(unidata); mr.uniform_data_offset = mr.texture_data_offset + sizeof(TextureData)*array::size(texdata); mr.dynamic_data_size = array::size(dynblob); mr.dynamic_data_offset = mr.uniform_data_offset + sizeof(UniformData)*array::size(unidata); // Write opts.write(mr.version); opts.write(mr.shader); opts.write(mr.num_textures); opts.write(mr.texture_data_offset); opts.write(mr.num_uniforms); opts.write(mr.uniform_data_offset); opts.write(mr.dynamic_data_size); opts.write(mr.dynamic_data_offset); for (u32 i = 0; i < array::size(texdata); i++) { opts.write(texdata[i].sampler_name_offset); opts.write(texdata[i]._pad); opts.write(texdata[i].id); opts.write(texdata[i].data_offset); opts.write(texdata[i]._pad1); } for (u32 i = 0; i < array::size(unidata); i++) { opts.write(unidata[i].type); opts.write(unidata[i].name); opts.write(unidata[i].name_offset); opts.write(unidata[i].data_offset); } opts.write(dynblob); opts.write(names); }
bool CalSaver::saveXmlCoreMesh(const std::string& strFilename, CalCoreMesh *pCoreMesh) { std::stringstream str; vsxTiXmlDocument doc(strFilename); vsxTiXmlElement mesh("MESH"); //mesh.SetAttribute("MAGIC",Cal::MESH_XMLFILE_MAGIC); mesh.SetAttribute("VERSION",Cal::LIBRARY_VERSION); mesh.SetAttribute("NUMSUBMESH",pCoreMesh->getCoreSubmeshCount()); // get the submesh vector std::vector<CalCoreSubmesh *>& vectorCoreSubmesh = pCoreMesh->getVectorCoreSubmesh(); // write all core submeshes int submeshId; for(submeshId = 0; submeshId < (int)vectorCoreSubmesh.size(); ++submeshId) { CalCoreSubmesh *pCoreSubmesh=vectorCoreSubmesh[submeshId]; vsxTiXmlElement submesh("SUBMESH"); submesh.SetAttribute("NUMVERTICES",pCoreSubmesh->getVertexCount()); submesh.SetAttribute("NUMFACES",pCoreSubmesh->getFaceCount()); submesh.SetAttribute("MATERIAL",pCoreSubmesh->getCoreMaterialThreadId()); submesh.SetAttribute("NUMLODSTEPS",pCoreSubmesh->getLodCount()); submesh.SetAttribute("NUMSPRINGS",pCoreSubmesh->getSpringCount()); submesh.SetAttribute("NUMTEXCOORDS",pCoreSubmesh->getVectorVectorTextureCoordinate().size()); // get the vertex, face, physical property and spring vector std::vector<CalCoreSubmesh::Vertex>& vectorVertex = pCoreSubmesh->getVectorVertex(); std::vector<CalCoreSubmesh::Face>& vectorFace = pCoreSubmesh->getVectorFace(); std::vector<CalCoreSubmesh::PhysicalProperty>& vectorPhysicalProperty = pCoreSubmesh->getVectorPhysicalProperty(); std::vector<CalCoreSubmesh::Spring>& vectorSpring = pCoreSubmesh->getVectorSpring(); // get the texture coordinate vector vector std::vector<std::vector<CalCoreSubmesh::TextureCoordinate> >& vectorvectorTextureCoordinate = pCoreSubmesh->getVectorVectorTextureCoordinate(); // write all vertices int vertexId; for(vertexId = 0; vertexId < (int)vectorVertex.size(); ++vertexId) { CalCoreSubmesh::Vertex& Vertex = vectorVertex[vertexId]; vsxTiXmlElement vertex("VERTEX"); vertex.SetAttribute("ID",vertexId); vertex.SetAttribute("NUMINFLUENCES",Vertex.vectorInfluence.size()); // write the vertex data vsxTiXmlElement position("POS"); str.str(""); str << Vertex.position.x << " " << Vertex.position.y << " " << Vertex.position.z; vsxTiXmlText positiondata(str.str()); position.InsertEndChild(positiondata); vertex.InsertEndChild(position); vsxTiXmlElement normal("NORM"); str.str(""); str << Vertex.normal.x << " " << Vertex.normal.y << " " << Vertex.normal.z; vsxTiXmlText normaldata(str.str()); normal.InsertEndChild(normaldata); vertex.InsertEndChild(normal); if(Vertex.collapseId!=-1) { vsxTiXmlElement collapse("COLLAPSEID"); str.str(""); str << Vertex.collapseId; vsxTiXmlText collapseid(str.str()); collapse.InsertEndChild(collapseid); vertex.InsertEndChild(collapse); vsxTiXmlElement collapsecount("COLLAPSECOUNT"); str.str(""); str << Vertex.faceCollapseCount; vsxTiXmlText collapsecountdata(str.str()); collapsecount.InsertEndChild(collapsecountdata); vertex.InsertEndChild(collapsecount); } // write all texture coordinates of this vertex int textureCoordinateId; for(textureCoordinateId = 0; textureCoordinateId < (int)vectorvectorTextureCoordinate.size(); ++textureCoordinateId) { CalCoreSubmesh::TextureCoordinate& textureCoordinate = vectorvectorTextureCoordinate[textureCoordinateId][vertexId]; vsxTiXmlElement tex("TEXCOORD"); str.str(""); str << textureCoordinate.u << " " << textureCoordinate.v; vsxTiXmlText texdata(str.str()); tex.InsertEndChild(texdata); vertex.InsertEndChild(tex); } // write all influences of this vertex int influenceId; for(influenceId = 0; influenceId < (int)Vertex.vectorInfluence.size(); ++influenceId) { CalCoreSubmesh::Influence& Influence = Vertex.vectorInfluence[influenceId]; vsxTiXmlElement influence("INFLUENCE"); influence.SetAttribute("ID",Influence.boneId); str.str(""); str << Influence.weight; vsxTiXmlText influencedata(str.str()); influence.InsertEndChild(influencedata); vertex.InsertEndChild(influence); } // save the physical property of the vertex if there are springs in the core submesh if(pCoreSubmesh->getSpringCount() > 0) { // write the physical property of this vertex if there are springs in the core submesh CalCoreSubmesh::PhysicalProperty& physicalProperty = vectorPhysicalProperty[vertexId]; vsxTiXmlElement physique("PHYSIQUE"); str.str(""); str << physicalProperty.weight; vsxTiXmlText physiquedata(str.str()); physique.InsertEndChild(physiquedata); vertex.InsertEndChild(physique); } submesh.InsertEndChild(vertex); } // write all springs int springId; for(springId = 0; springId < (int)pCoreSubmesh->getSpringCount(); ++springId) { CalCoreSubmesh::Spring& Spring = vectorSpring[springId]; vsxTiXmlElement spring("SPRING"); str.str(""); str << Spring.vertexId[0]<< " " << Spring.vertexId[1]; spring.SetAttribute("VERTEXID",str.str()); str.str(""); str << Spring.springCoefficient; spring.SetAttribute("COEF",str.str()); str.str(""); str << Spring.idleLength; spring.SetAttribute("LENGTH",str.str()); submesh.InsertEndChild(spring); } // write all faces int faceId; for(faceId = 0; faceId < (int)vectorFace.size(); ++faceId) { CalCoreSubmesh::Face& Face = vectorFace[faceId]; vsxTiXmlElement face("FACE"); str.str(""); str << Face.vertexId[0]<< " " << Face.vertexId[1]<< " " << Face.vertexId[2]; face.SetAttribute("VERTEXID",str.str()); submesh.InsertEndChild(face); } mesh.InsertEndChild(submesh); } doc.InsertEndChild(mesh); if(!doc.SaveFile()) { CalError::setLastError(CalError::FILE_WRITING_FAILED, __FILE__, __LINE__, strFilename); return false; } return true; }