-void Model::loadModel(const std::string &file) {
- Assimp::Importer importer;
- const aiScene *scene = importer.ReadFile(file,
- aiProcess_Triangulate | aiProcess_PreTransformVertices |
+Model::Node::Node(const aiNode &node, GLuint progId): ai(node), progId(progId) {
+ for (int i = 0; i < node.mNumMeshes; i++) {
+ meshIndices.push_back(node.mMeshes[i]);
+ }
+ for (int i = 0; i < node.mNumChildren; i++) {
+ const aiNode *child = node.mChildren[i];
+ children.push_back(new Node(*child, progId));
+ }
+}
+
+glm::mat4 aiMatrixToMat4(aiMatrix4x4 from) {
+ glm::mat4 to;
+ for (int i = 0; i < 4; i++)
+ for (int j = 0; j < 4; j++)
+ to[i][j] = from[j][i];
+ return to;
+}
+
+void Model::Node::draw(const std::vector<Mesh> &meshes, glm::mat4 parentTrans = glm::mat4(1)) const {
+ GLuint modelLoc = glGetUniformLocation(progId, "model");
+ glm::mat4 m = parentTrans * aiMatrixToMat4(ai.mTransformation) * model;
+
+ for (unsigned int i: meshIndices) {
+ const Mesh &mesh = meshes[i];
+
+ glBindVertexArray(mesh.vao);
+
+ glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(m));
+
+ glDrawElements(GL_TRIANGLES, mesh.numIndices, GL_UNSIGNED_INT, 0);
+ }
+ for (Node *child: children) child->draw(meshes, m);
+}
+
+Model::Model(const std::string &path, GLuint progId): progId(progId) {
+ const aiScene *scene = importer.ReadFile(path,
+ aiProcess_Triangulate |