passes.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // Created by almamu on 13/08/19.
  3. //
  4. #include <nlohmann/json.hpp>
  5. #include <utility>
  6. #include "passes.h"
  7. using namespace wp::core::objects::images::materials;
  8. passes::passes (std::string blending, std::string cullmode, std::string depthtest, std::string depthwrite, std::string shader) :
  9. m_blending (std::move(blending)),
  10. m_cullmode (std::move(cullmode)),
  11. m_depthtest (std::move(depthtest)),
  12. m_depthwrite (std::move(depthwrite)),
  13. m_shader (std::move(shader))
  14. {
  15. }
  16. std::vector<std::string>* passes::getTextures ()
  17. {
  18. return &this->m_textures;
  19. }
  20. passes* passes::fromJSON (json data)
  21. {
  22. json::const_iterator blending_it = data.find ("blending");
  23. json::const_iterator cullmode_it = data.find ("cullmode");
  24. json::const_iterator depthtest_it = data.find ("depthtest");
  25. json::const_iterator depthwrite_it = data.find ("depthwrite");
  26. json::const_iterator shader_it = data.find ("shader");
  27. json::const_iterator textures_it = data.find ("textures");
  28. if (blending_it == data.end ())
  29. {
  30. throw std::runtime_error ("Material pass must have blending specified");
  31. }
  32. if (cullmode_it == data.end ())
  33. {
  34. throw std::runtime_error ("Material pass must have cullmode specified");
  35. }
  36. if (depthtest_it == data.end ())
  37. {
  38. throw std::runtime_error ("Material pass must have depthtest specified");
  39. }
  40. if (depthwrite_it == data.end ())
  41. {
  42. throw std::runtime_error ("Material pass must have depthwrite specified");
  43. }
  44. if (shader_it == data.end ())
  45. {
  46. throw std::runtime_error ("Material pass must have shader specified");
  47. }
  48. if (textures_it != data.end ())
  49. {
  50. if ((*textures_it).is_array () == false)
  51. {
  52. throw std::runtime_error ("Textures for material must be a list");
  53. }
  54. }
  55. passes* pass = new passes (
  56. *blending_it,
  57. *cullmode_it,
  58. *depthtest_it,
  59. *depthwrite_it,
  60. *shader_it
  61. );
  62. if (textures_it != data.end ())
  63. {
  64. json::const_iterator cur = (*textures_it).begin ();
  65. json::const_iterator end = (*textures_it).end ();
  66. for (;cur != end; cur ++)
  67. {
  68. if ((*cur).is_null () == true)
  69. {
  70. pass->insertTexture ("");
  71. }
  72. else
  73. {
  74. pass->insertTexture (*cur);
  75. }
  76. }
  77. }
  78. return pass;
  79. }
  80. void passes::insertTexture (const std::string& texture)
  81. {
  82. this->m_textures.push_back (texture);
  83. }