Load cameras and shuffle about model stuff
[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 "material.hpp"
11 #include "program.hpp"
12 #include "skybox.hpp"
13
14 inline glm::mat4 aiMatrixToMat4(aiMatrix4x4 from) {
15         glm::mat4 to;
16         for (int i = 0; i < 4; i++)
17                 for (int j = 0; j < 4; j++)
18                         to[i][j] = from[j][i];
19         return to;
20 }
21
22 class Model {
23
24         struct Animation {
25                 double duration;
26                 std::vector<const aiNodeAnim*> nodeAnims;
27         };
28
29         typedef std::map<std::string, std::pair<unsigned int, glm::mat4>> BoneMap;
30         typedef std::map<std::string, std::vector<const Animation>> AnimMap;
31         typedef std::map<std::string, glm::mat4> BoneTransforms;
32
33         struct VertBones {
34                 unsigned int ids[4] = {0, 0, 0 ,0};
35                 float weights[4] = {1, 0, 0, 0};
36         };
37
38         struct Mesh {
39                 Mesh(const aiMesh *aiMesh, GLuint progId);
40                 GLuint progId, vao, numIndices;
41                 unsigned int materialIndex;
42                 BoneMap boneMap;
43         };
44         
45         public:
46                 Model(const aiScene *scene, Program p);
47                 void draw(Skybox skybox, const float tick) const;
48
49                 class Node {
50                         public:
51                                 Node(const aiNode &aiNode, GLuint progId, AnimMap *animMap);
52                                 void draw(const std::vector<Mesh> &meshes, const std::vector<Material> &materials, const Skybox s, const float tick, glm::mat4 parentModel, BoneTransforms boneTransforms) const;
53                                 const std::vector<Node*> &getChildren() const { return children; }
54                                 Node* findNode(const aiNode &aiNode);
55                                 const aiNode &ai;
56
57                         private:
58                                 const GLuint progId;
59
60                                 const AnimMap *animMap;
61                                 std::vector<Node*> children;
62                                 std::vector<unsigned int> meshIndices;
63                 };
64                 
65                 Node* getRoot() { return root; }
66                 Node* find(const aiString name);
67                 Node* find(const std::string &name);
68         
69         private:
70                 const Program program;
71                 
72                 std::vector<Mesh> meshes;
73                 Node *root;
74
75                 std::vector<Material> materials;
76
77                 void loadModel(const std::string &path);
78 };