Model.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/MeshLoader.h"
  6. #include "anki/resource/Skeleton.h"
  7. #include "anki/resource/ShaderProgramResource.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. // ModelPatchBase =
  16. //==============================================================================
  17. //==============================================================================
  18. Vao* ModelPatchBase::createNewVao(const Material& mtl,
  19. const MeshBase& meshb,
  20. const PassLevelKey& key)
  21. {
  22. Vao* vao = new Vao;
  23. vao->create();
  24. if(mtl.getShaderProgram(key).findAttributeVariableByName("position"))
  25. {
  26. const Vbo* vbo = meshb.getVbo(Mesh::VBO_POSITIONS);
  27. ANKI_ASSERT(vbo != NULL);
  28. vao->attachArrayBufferVbo(*vbo, 0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  29. }
  30. if(mtl.getShaderProgram(key).findAttributeVariableByName("normal"))
  31. {
  32. const Vbo* vbo = meshb.getVbo(Mesh::VBO_NORMALS);
  33. ANKI_ASSERT(vbo != NULL);
  34. vao->attachArrayBufferVbo(*vbo, 1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  35. }
  36. if(mtl.getShaderProgram(key).findAttributeVariableByName("tangent"))
  37. {
  38. const Vbo* vbo = meshb.getVbo(Mesh::VBO_TANGENTS);
  39. ANKI_ASSERT(vbo != NULL);
  40. vao->attachArrayBufferVbo(*vbo, 2, 4, GL_FLOAT, GL_FALSE, 0, NULL);
  41. }
  42. if(mtl.getShaderProgram(key).findAttributeVariableByName("texCoords"))
  43. {
  44. const Vbo* vbo = meshb.getVbo(Mesh::VBO_TEX_COORDS);
  45. ANKI_ASSERT(vbo != NULL);
  46. vao->attachArrayBufferVbo(*vbo, 3, 2, GL_FLOAT, GL_FALSE, 0, NULL);
  47. }
  48. vao->attachElementArrayBufferVbo(*meshb.getVbo(Mesh::VBO_INDICES));
  49. return vao;
  50. }
  51. //==============================================================================
  52. void ModelPatchBase::createVaos(const Material& mtl,
  53. const MeshBase& meshb,
  54. VaosContainer& vaos,
  55. PassLevelToVaoMap& vaosMap)
  56. {
  57. for(uint level = 0; level < mtl.getLevelsOfDetail(); ++level)
  58. {
  59. for(uint pass = 0; pass < mtl.getPasses().size(); ++pass)
  60. {
  61. PassLevelKey key(pass, level);
  62. Vao* vao = createNewVao(mtl, meshb, key);
  63. vaos.push_back(vao);
  64. vaosMap[key] = vao;
  65. }
  66. }
  67. }
  68. //==============================================================================
  69. // ModelPatch =
  70. //==============================================================================
  71. //==============================================================================
  72. ModelPatch::ModelPatch(const char* meshFName, const char* mtlFName)
  73. {
  74. // Load
  75. mesh.load(meshFName);
  76. mtl.load(mtlFName);
  77. /// Create VAOs
  78. create();
  79. }
  80. //==============================================================================
  81. ModelPatch::~ModelPatch()
  82. {}
  83. //==============================================================================
  84. // Model =
  85. //==============================================================================
  86. //==============================================================================
  87. void Model::load(const char* filename)
  88. {
  89. try
  90. {
  91. //
  92. // Load
  93. //
  94. using namespace boost::property_tree;
  95. ptree pt_;
  96. read_xml(filename, pt_);
  97. const ptree& pt = pt_.get_child("model");
  98. // modelPatches
  99. BOOST_FOREACH(const ptree::value_type& v, pt.get_child("modelPatches"))
  100. {
  101. const std::string& mesh = v.second.get<std::string>("mesh");
  102. const std::string& material = v.second.get<std::string>("material");
  103. ModelPatch* patch = new ModelPatch(mesh.c_str(), material.c_str());
  104. modelPatches.push_back(patch);
  105. }
  106. if(modelPatches.size() < 1)
  107. {
  108. throw ANKI_EXCEPTION("Zero number of model patches");
  109. }
  110. // Calculate compound bounding volume
  111. visibilityShape = modelPatches[0].getMeshBase().getBoundingShape();
  112. for(ModelPatchesContainer::const_iterator it = modelPatches.begin() + 1;
  113. it != modelPatches.end();
  114. ++it)
  115. {
  116. visibilityShape = visibilityShape.getCompoundShape(
  117. (*it).getMeshBase().getBoundingShape());
  118. }
  119. }
  120. catch(std::exception& e)
  121. {
  122. throw ANKI_EXCEPTION("Model loading failed: " + filename) << e;
  123. }
  124. }
  125. } // end namespace