GeometryPtr Platform::getGeometry(const string & file) { static std::map<string, GeometryPtr> cache; if (!cache.count(file)) { CTMimporter importer; std::string data = Files::read(file); importer.LoadData(data); GL::VertexBufferPtr in_vertices(new VertexBuffer()); GL::IndexBufferPtr in_indices(new IndexBuffer()); (*in_vertices) << CtmFloatLoader(importer); (*in_indices) << CtmIndexLoader(importer); unsigned int triangles = importer.GetInteger(CTM_TRIANGLE_COUNT); unsigned int flags = importer.GetInteger(CTM_HAS_NORMALS) ? GL::Geometry::HAS_NORMAL : 0; cache[file] = GeometryPtr(new Geometry(in_vertices, in_indices, triangles, flags)); } return cache[file]; }
void Mesh::loadCtm(const std::string & data) { clear(); CTMimporter importer; importer.LoadData(data); int vertexCount = importer.GetInteger(CTM_VERTEX_COUNT); positions.resize(vertexCount); const float * ctmData = importer.GetFloatArray(CTM_VERTICES); for (int i = 0; i < vertexCount; ++i) { positions[i] = glm::vec4(glm::make_vec3(ctmData + (i * 3)), 1); } if (importer.GetInteger(CTM_UV_MAP_COUNT) > 0) { const float * ctmData = importer.GetFloatArray(CTM_UV_MAP_1); texCoords.resize(vertexCount); for (int i = 0; i < vertexCount; ++i) { texCoords[i] = glm::make_vec2(ctmData + (i * 2)); } } bool hasNormals = importer.GetInteger(CTM_HAS_NORMALS) ? true : false; if (hasNormals) { normals.resize(vertexCount); ctmData = importer.GetFloatArray(CTM_NORMALS); for (int i = 0; i < vertexCount; ++i) { normals[i] = glm::vec4(glm::make_vec3(ctmData + (i * 3)), 1); } } int indexCount = 3 * importer.GetInteger(CTM_TRIANGLE_COUNT); const CTMuint * ctmIntData = importer.GetIntegerArray(CTM_INDICES); indices.resize(indexCount); for (int i = 0; i < indexCount; ++i) { indices[i] = *(ctmIntData + i); } }