ModelPatch.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "anki/resource/ModelPatch.h"
  2. #include "anki/resource/Mesh.h"
  3. #include "anki/resource/Material.h"
  4. namespace anki {
  5. //==============================================================================
  6. ModelPatch::ModelPatch(const char* meshFName, const char* mtlFName)
  7. {
  8. // Load
  9. mesh.load(meshFName);
  10. mtl.load(mtlFName);
  11. // Create VAOs
  12. VboArray vboArr;
  13. for(uint i = 0; i < Mesh::VBOS_NUM; i++)
  14. {
  15. vboArr[i] = &mesh->getVbo((Mesh::Vbos)i);
  16. }
  17. createVaos(*mtl, vboArr, vaos, vaosHashMap);
  18. }
  19. //==============================================================================
  20. ModelPatch::~ModelPatch()
  21. {}
  22. //==============================================================================
  23. bool ModelPatch::supportsHwSkinning() const
  24. {
  25. return mesh->hasVertWeights();
  26. }
  27. //==============================================================================
  28. Vao* ModelPatch::createVao(const Material& mtl,
  29. const VboArray& vbos,
  30. const PassLevelKey& key)
  31. {
  32. Vao* vao = new Vao;
  33. if(mtl.variableExistsAndInKey("position", key))
  34. {
  35. ANKI_ASSERT(vbos[Mesh::VBO_VERT_POSITIONS] != NULL);
  36. vao->attachArrayBufferVbo(*vbos[Mesh::VBO_VERT_POSITIONS],
  37. 0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  38. }
  39. if(mtl.variableExistsAndInKey("normal", key))
  40. {
  41. ANKI_ASSERT(vbos[Mesh::VBO_VERT_NORMALS] != NULL);
  42. vao->attachArrayBufferVbo(*vbos[Mesh::VBO_VERT_NORMALS],
  43. 1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  44. }
  45. if(mtl.variableExistsAndInKey("tangent", key))
  46. {
  47. ANKI_ASSERT(vbos[Mesh::VBO_VERT_TANGENTS] != NULL);
  48. vao->attachArrayBufferVbo(*vbos[Mesh::VBO_VERT_TANGENTS],
  49. 2, 4, GL_FLOAT, GL_FALSE, 0, NULL);
  50. }
  51. if(mtl.variableExistsAndInKey("texCoords", key))
  52. {
  53. vao->attachArrayBufferVbo(*vbos[Mesh::VBO_TEX_COORDS],
  54. 3, 2, GL_FLOAT, GL_FALSE, 0, NULL);
  55. }
  56. vao->attachElementArrayBufferVbo(*vbos[Mesh::VBO_VERT_INDECES]);
  57. return vao;
  58. }
  59. //==============================================================================
  60. void ModelPatch::createVaos(const Material& mtl,
  61. const VboArray& vbos,
  62. VaosContainer& vaos,
  63. PassLevelToVaoHashMap& vaosHashMap)
  64. {
  65. for(uint level = 0; level < mtl.getLevelsOfDetail(); ++level)
  66. {
  67. for(uint pass = 0; pass < mtl.getPasses().size(); ++pass)
  68. {
  69. PassLevelKey key(pass, level);
  70. Vao* vao = createVao(mtl, vbos, key);
  71. vaos.push_back(vao);
  72. vaosHashMap[key] = vao;
  73. }
  74. }
  75. }
  76. } // end namespace