Mesh &MeshLoader::getMesh(const std::string &path) { // check if mesh is cahced auto it = meshCache.find(path); if (it != meshCache.end()) { return it->second; } const std::string filePath = Environment::getDataDir() + "/meshes/" + path; Assimp::Importer importer; // flags to read mesh // - aiProcess_Triangulate : Triangulates all faces of all meshes. // - aiProcess_GenNormals : Generates normals for all faces of all meshes. // - aiProcess_CalcTangentSpace : Calculates the tangents and bitangents for // the imported meshes. // http://assimp.sourceforge.net/lib_html/postprocess_8h.html unsigned int flags = aiProcess_Triangulate | aiProcess_GenNormals | aiProcess_CalcTangentSpace; const auto container = importer.ReadFile(filePath, flags); const aiMesh *mesh = container->mMeshes[0]; // Upload mesh to GPU Mesh meshObject = uploadMesh(mesh); auto inserted = meshCache.insert(std::make_pair(path, meshObject)); return inserted.first->second; }
void RenderUtil::initializeWireframeBox() { _mesh = new Mesh; const static int numVertices = 8; const static int numIndices = 24; const static glm::vec3 vertices[numVertices] = { glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(1.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(1.0f, 1.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3(0.0f, 1.0f, 1.0f), }; const static GLuint indices[numIndices] = { 0, 1, 1, 2, 2, 3, 3, 0, 0, 4, 1, 5, 2, 6, 3, 7, 4, 5, 5, 6, 6, 7, 7, 4 }; uploadMesh(&_mesh->vboID, &_mesh->iboID, vertices, numVertices, indices, numIndices); _mesh->numIndices = numIndices; }
void initGrid() { static const int WIDTH_DIVISIONS = NUM_WIDTH_PTS - 1; static const int HEIGHT_DIVISIONS = NUM_HEIGHT_PTS - 1; const int numberOfPositions = NUM_WIDTH_PTS * NUM_HEIGHT_PTS; Float32Array positions(2 * numberOfPositions); Uint16Array indices(2 * ((NUM_HEIGHT_PTS * (NUM_WIDTH_PTS - 1)) + (NUM_WIDTH_PTS * (NUM_HEIGHT_PTS - 1)))); int positionsIndex = 0; int indicesIndex = 0; for (int j = 0; j < NUM_WIDTH_PTS; ++j) { positions[positionsIndex++] = (float)(j)/(NUM_WIDTH_PTS - 1); positions[positionsIndex++] = 0.0f; if (j>=1) { unsigned short length = (unsigned short)(positionsIndex / 2); indices[indicesIndex++] = length - 2; indices[indicesIndex++] = length - 1; } } for (int i = 0; i < HEIGHT_DIVISIONS; ++i) { float v = (float)(i + 1)/(NUM_HEIGHT_PTS - 1); positions[positionsIndex++] = 0.0f; positions[positionsIndex++] = v; unsigned short length = (unsigned short)(positionsIndex / 2); indices[indicesIndex++] = length - 1; indices[indicesIndex++] = length - 1 - NUM_WIDTH_PTS; for (int j = 0; j < WIDTH_DIVISIONS; j++) { positions[positionsIndex++] = (float)(j + 1)/(NUM_WIDTH_PTS - 1); positions[positionsIndex++] = v; unsigned short length = (unsigned short)(positionsIndex / 2); unsigned short new_pt = length - 1; indices[indicesIndex++] = new_pt - 1; // Previous side indices[indicesIndex++] = new_pt; indices[indicesIndex++] = new_pt - NUM_WIDTH_PTS; // Previous bottom indices[indicesIndex++] = new_pt; } } uploadMesh(positions, heights, indices); numberOfIndices = indices.length; }
Mesh& MeshLoader::getMesh(const std::string &path) { auto it = meshCache.find(path); if (it != meshCache.end()) { return it->second; } std::string fpath = Environment::getDataDir() + "/meshes/" + path; Assimp::Importer importer; int flags = aiProcess_Triangulate | aiProcess_GenNormals | aiProcess_CalcTangentSpace; const aiMesh *mesh = importer.ReadFile(fpath, flags)->mMeshes[0]; Mesh m = uploadMesh(mesh); auto inserted = meshCache.insert(std::pair<std::string, Mesh>(path, m)); // Return reference to newly inserted Mesh return inserted.first->second; }