Model.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "anki/resource/Model.h"
  2. #include "anki/resource/Material.h"
  3. #include "anki/resource/Mesh.h"
  4. #include "anki/resource/SkelAnim.h"
  5. #include "anki/resource/MeshData.h"
  6. #include "anki/resource/Skeleton.h"
  7. #include "anki/resource/ShaderProgram.h"
  8. #include <boost/foreach.hpp>
  9. #include <boost/property_tree/ptree.hpp>
  10. #include <boost/property_tree/xml_parser.hpp>
  11. #include <boost/assign.hpp>
  12. #include <boost/range/iterator_range.hpp>
  13. namespace anki {
  14. //==============================================================================
  15. // ModelPatch =
  16. //==============================================================================
  17. //==============================================================================
  18. ModelPatch::ModelPatch(const char* meshFName, const char* mtlFName)
  19. {
  20. // Load
  21. mesh.load(meshFName);
  22. mtl.load(mtlFName);
  23. // Create VAOs
  24. VboArray vboArr;
  25. for(uint i = 0; i < Mesh::VBOS_NUM; i++)
  26. {
  27. vboArr[i] = &mesh->getVbo((Mesh::Vbos)i);
  28. }
  29. createVaos(*mtl, vboArr, vaos, vaosHashMap);
  30. }
  31. //==============================================================================
  32. ModelPatch::~ModelPatch()
  33. {}
  34. //==============================================================================
  35. bool ModelPatch::supportsHwSkinning() const
  36. {
  37. return mesh->hasVertWeights();
  38. }
  39. //==============================================================================
  40. Vao* ModelPatch::createVao(const Material& mtl,
  41. const VboArray& vbos,
  42. const PassLevelKey& key)
  43. {
  44. Vao* vao = new Vao;
  45. if(mtl.getShaderProgram(key).uniformVariableExists("position"))
  46. {
  47. ANKI_ASSERT(vbos[Mesh::VBO_VERT_POSITIONS] != NULL);
  48. vao->attachArrayBufferVbo(*vbos[Mesh::VBO_VERT_POSITIONS],
  49. 0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  50. }
  51. if(mtl.getShaderProgram(key).uniformVariableExists("normal"))
  52. {
  53. ANKI_ASSERT(vbos[Mesh::VBO_VERT_NORMALS] != NULL);
  54. vao->attachArrayBufferVbo(*vbos[Mesh::VBO_VERT_NORMALS],
  55. 1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  56. }
  57. if(mtl.getShaderProgram(key).uniformVariableExists("tangent"))
  58. {
  59. ANKI_ASSERT(vbos[Mesh::VBO_VERT_TANGENTS] != NULL);
  60. vao->attachArrayBufferVbo(*vbos[Mesh::VBO_VERT_TANGENTS],
  61. 2, 4, GL_FLOAT, GL_FALSE, 0, NULL);
  62. }
  63. if(mtl.getShaderProgram(key).uniformVariableExists("texCoords"))
  64. {
  65. vao->attachArrayBufferVbo(*vbos[Mesh::VBO_TEX_COORDS],
  66. 3, 2, GL_FLOAT, GL_FALSE, 0, NULL);
  67. }
  68. vao->attachElementArrayBufferVbo(*vbos[Mesh::VBO_VERT_INDECES]);
  69. return vao;
  70. }
  71. //==============================================================================
  72. void ModelPatch::createVaos(const Material& mtl,
  73. const VboArray& vbos,
  74. VaosContainer& vaos,
  75. PassLevelToVaoHashMap& vaosHashMap)
  76. {
  77. for(uint level = 0; level < mtl.getLevelsOfDetail(); ++level)
  78. {
  79. for(uint pass = 0; pass < mtl.getPasses().size(); ++pass)
  80. {
  81. PassLevelKey key(pass, level);
  82. Vao* vao = createVao(mtl, vbos, key);
  83. vaos.push_back(vao);
  84. vaosHashMap[key] = vao;
  85. }
  86. }
  87. }
  88. //==============================================================================
  89. // Model =
  90. //==============================================================================
  91. //==============================================================================
  92. void Model::load(const char* filename)
  93. {
  94. try
  95. {
  96. //
  97. // Load
  98. //
  99. using namespace boost::property_tree;
  100. ptree pt_;
  101. read_xml(filename, pt_);
  102. const ptree& pt = pt_.get_child("model");
  103. // modelPatches
  104. BOOST_FOREACH(const ptree::value_type& v, pt.get_child("modelPatches"))
  105. {
  106. const std::string& mesh = v.second.get<std::string>("mesh");
  107. const std::string& material = v.second.get<std::string>("material");
  108. ModelPatch* patch = new ModelPatch(mesh.c_str(), material.c_str());
  109. modelPatches.push_back(patch);
  110. }
  111. if(modelPatches.size() < 1)
  112. {
  113. throw ANKI_EXCEPTION("Zero number of model patches");
  114. }
  115. // Calculate compound bounding volume
  116. visibilityShape = modelPatches[0].getMesh().getVisibilityShape();
  117. for(ModelPatchesContainer::const_iterator it = modelPatches.begin() + 1;
  118. it != modelPatches.end();
  119. ++it)
  120. {
  121. visibilityShape = visibilityShape.getCompoundShape(
  122. (*it).getMesh().getVisibilityShape());
  123. }
  124. }
  125. catch(std::exception& e)
  126. {
  127. throw ANKI_EXCEPTION("Model \"" + filename + "\"") << e;
  128. }
  129. }
  130. } // end namespace