[C++] The IN55 project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
4.6KB

  1. #ifndef __MESH_H__
  2. #define __MESH_H__
  3. #include <iostream>
  4. #include <assimp/cimport.h>
  5. #include <assimp/scene.h>
  6. #include <assimp/postprocess.h>
  7. #include <vectors.h>
  8. #include <vertice.h>
  9. #include <bone.h>
  10. #include <animation.h>
  11. #include <utils.h>
  12. class Face
  13. {
  14. private:
  15. GLenum type;
  16. int indexNumber, maxIndexNumber;
  17. int *indices;
  18. public:
  19. Face(int nb) {
  20. indexNumber = 0;
  21. maxIndexNumber = nb;
  22. switch(nb) {
  23. case 1: type = GL_POINTS; break;
  24. case 2: type = GL_LINES; break;
  25. case 3: type = GL_TRIANGLES; break;
  26. default: type = GL_POLYGON; break;
  27. }
  28. indices = (int*) malloc(sizeof(int) * nb);
  29. };
  30. virtual ~Face() { free(indices); };
  31. int getIndexNumber() { return indexNumber; };
  32. GLenum getType() { return type; };
  33. int getIndex(int idx) { return (idx < indexNumber) ? indices[idx] : -1; };
  34. void addIndex(int idx) {
  35. if (indexNumber < maxIndexNumber) {
  36. indices[indexNumber++] = idx;
  37. }
  38. };
  39. };
  40. class Mesh
  41. {
  42. private:
  43. Vertice **verticeList;
  44. aiVector3D **normalList;
  45. Face **faceList;
  46. Bone **boneList;
  47. aiMatrix4x4 *boneStateList;
  48. Animation **animList;
  49. int vertNb, boneNb, faceNb, animNb, maxFaceNb;
  50. int mesh_id;
  51. int currentAnimation;
  52. GLint shader_bones;
  53. GLint shader_Position;
  54. GLint shader_Normal;
  55. GLint shader_BoneTransform;
  56. GLint shader_Weights;
  57. GLuint shaderProg;
  58. public:
  59. Mesh(int id, int vertSize, int boneSize, int faceSize, GLuint prog){
  60. shaderProg = prog;
  61. vertNb = vertSize;
  62. boneNb = boneSize;
  63. faceNb = 0;
  64. animNb = 0;
  65. maxFaceNb = faceSize;
  66. mesh_id = id;
  67. verticeList = (Vertice**)malloc(vertNb * sizeof(Vertice*));
  68. normalList = (aiVector3D**)malloc(vertNb * sizeof(aiVector3D*));
  69. boneList = (Bone**)malloc(boneNb * sizeof(Bone*));
  70. boneStateList = (aiMatrix4x4*) malloc(boneNb * sizeof(aiMatrix4x4));
  71. faceList = (Face**)malloc(faceSize * sizeof(Face*));
  72. currentAnimation = 0;
  73. shader_bones = glGetUniformLocation(shaderProg, "gBones");
  74. shader_Position = glGetAttribLocation(shaderProg, "Position");
  75. shader_Normal = glGetAttribLocation(shaderProg, "Normal");
  76. shader_BoneTransform = glGetAttribLocation(shaderProg, "BoneTransform");
  77. shader_Weights = glGetAttribLocation(shaderProg, "Weights");
  78. };
  79. void setCurrentAnim(int n) {
  80. if (0 < n && n <= animNb) {
  81. currentAnimation = n-1;
  82. } else {
  83. currentAnimation = 0;
  84. }
  85. };
  86. void initAnimList(const aiScene *scene);
  87. bool updateBoneStateList(float AnimationTime, const aiNode* pNode, const aiMatrix4x4& ParentTransform);
  88. void render(bool anim);
  89. void addVertice(Vertice *vert, aiVector3D *normal, int index) {
  90. if (index < vertNb) {
  91. verticeList[index] = vert;
  92. normalList[index] = normal;
  93. }
  94. };
  95. void setVertBone(int index, aiString boneID, float weight) {
  96. if(index < vertNb) {
  97. verticeList[index]->setBones(boneID, weight);
  98. }
  99. };
  100. void addBone(Bone *bone, int index) {
  101. if (index < boneNb) {
  102. boneList[index] = bone;
  103. }
  104. }
  105. void addFace(Face *f) {
  106. if (faceNb < maxFaceNb) {
  107. faceList[faceNb++] = f;
  108. }
  109. }
  110. int getNbVert(){return vertNb;};
  111. int getNbBone(){return boneNb;};
  112. int getFaceNumber() { return faceNb; };
  113. int getId() { return mesh_id; };
  114. Face *getFace(int i) { return (i < faceNb) ? faceList[i] : NULL; };
  115. Vertice *getVertex(int i) { return (i < vertNb) ? verticeList[i] : NULL; };
  116. aiVector3D *getNormal(int i) { return (i < vertNb) ? normalList[i] : NULL; };
  117. Bone *getBone(int i) { return (i < boneNb) ? boneList[i] : NULL; };
  118. int getBoneIndex(aiString name) {
  119. for (int i = 0; i < boneNb; ++i) {
  120. if (boneList[i]->getName() == name) {
  121. return i;
  122. }
  123. }
  124. return -1;
  125. }
  126. Bone *getBone(aiString name) {
  127. int i = getBoneIndex(name);
  128. return (i != -1) ? boneList[i] : NULL;
  129. };
  130. };
  131. #endif