2803a7ee3cc7147550af2928ce5978419727f6f8
[opengl.git] / model.hpp
1 #include <vector>
2 #include <map>
3 #ifdef __APPLE__
4 #include <GL/glew.h>
5 #else
6 #include <OpenGL/glew.h>
7 #endif
8 #include <glm/glm.hpp>
9 #include <assimp/scene.h>
10 #include <assimp/Importer.hpp>
11 #include "material.hpp"
12 #include "program.hpp"
13 #include "skybox.hpp"
14
15 class Model {
16
17         struct Animation {
18                 double duration;
19                 std::vector<const aiNodeAnim*> nodeAnims;
20         };
21
22         typedef std::map<std::string, std::pair<unsigned int, glm::mat4>> BoneMap;
23         typedef std::map<std::string, std::vector<const Animation>> AnimMap;
24         typedef std::map<std::string, glm::mat4> BoneTransforms;
25
26         struct VertBones {
27                 unsigned int ids[4] = {0, 0, 0 ,0};
28                 float weights[4] = {1, 0, 0, 0};
29         };
30
31         struct Mesh {
32                 Mesh(const aiMesh *aiMesh, GLuint progId);
33                 GLuint progId, vao, numIndices;
34                 unsigned int materialIndex;
35                 BoneMap boneMap;
36         };
37         
38         public:
39                 Model(const std::string &path, Program p);
40                 void draw(Skybox skybox, const float tick) const;
41
42                 class Node {
43                         public:
44                                 Node(const aiNode &aiNode, GLuint progId, AnimMap *animMap);
45                                 void draw(const std::vector<Mesh> &meshes, const std::vector<Material> &materials, const Skybox s, const float tick, glm::mat4 parentModel, BoneTransforms boneTransforms) const;
46                                 const std::vector<Node*> &getChildren() const { return children; }
47                                 Node* findNode(const aiNode &aiNode);
48                                 const aiNode &ai;
49
50                         private:
51                                 const GLuint progId;
52
53                                 const AnimMap *animMap;
54                                 std::vector<Node*> children;
55                                 std::vector<unsigned int> meshIndices;
56                 };
57                 
58                 Node* getRoot() { return root; }
59                 Node* find(const aiString name);
60                 Node* find(const std::string &name);
61         
62         private:
63                 const Program program;
64                 
65                 std::vector<Mesh> meshes;
66                 Node *root;
67
68                 std::vector<Material> materials;
69
70                 void loadModel(const std::string &path);
71
72                 Assimp::Importer importer;
73 };