MaterialRuntime.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "anki/scene/MaterialRuntime.h"
  2. #include "anki/resource/Material.h"
  3. #include "anki/resource/Texture.h"
  4. #include "anki/resource/Resource.h"
  5. #include <boost/foreach.hpp>
  6. namespace anki {
  7. //==============================================================================
  8. // MaterialRuntimeVariable =
  9. //==============================================================================
  10. //==============================================================================
  11. template <>
  12. void MaterialRuntimeVariable::ConstructVisitor::
  13. operator()<TextureResourcePointer >(const TextureResourcePointer& x) const
  14. {
  15. var.data = &x;
  16. }
  17. //==============================================================================
  18. MaterialRuntimeVariable::MaterialRuntimeVariable(
  19. const MaterialVariable& mvar_)
  20. : mvar(mvar_), buildinId(-1)
  21. {
  22. // Initialize the data using a visitor
  23. boost::apply_visitor(ConstructVisitor(*this), mvar.getVariant());
  24. }
  25. //==============================================================================
  26. MaterialRuntimeVariable::~MaterialRuntimeVariable()
  27. {}
  28. //==============================================================================
  29. template<>
  30. MaterialRuntimeVariable::ConstPtrRsrcPtrTexture&
  31. MaterialRuntimeVariable::getValue<
  32. MaterialRuntimeVariable::ConstPtrRsrcPtrTexture>()
  33. {
  34. throw ANKI_EXCEPTION("You shouldn't call this getter");
  35. return boost::get<ConstPtrRsrcPtrTexture>(data);
  36. }
  37. //==============================================================================
  38. template<>
  39. void MaterialRuntimeVariable::setValue<
  40. MaterialRuntimeVariable::ConstPtrRsrcPtrTexture>(
  41. const ConstPtrRsrcPtrTexture& v)
  42. {
  43. throw ANKI_EXCEPTION("You shouldn't call this setter");
  44. boost::get<ConstPtrRsrcPtrTexture>(data) = v;
  45. }
  46. //==============================================================================
  47. // MaterialRuntime =
  48. //==============================================================================
  49. //==============================================================================
  50. MaterialRuntime::MaterialRuntime(const Material& mtl_)
  51. : mtl(mtl_)
  52. {
  53. // Copy props
  54. MaterialProperties& me = *this;
  55. const MaterialProperties& he = mtl.getBaseClass();
  56. me = he;
  57. // Create vars
  58. BOOST_FOREACH(const MaterialVariable& var, mtl.getVariables())
  59. {
  60. MaterialRuntimeVariable* varr = new MaterialRuntimeVariable(var);
  61. vars.push_back(varr);
  62. varNameToVar[varr->getMaterialVariable().getName().c_str()] = varr;
  63. }
  64. }
  65. //==============================================================================
  66. MaterialRuntime::~MaterialRuntime()
  67. {}
  68. //==============================================================================
  69. MaterialRuntimeVariable& MaterialRuntime::findVariableByName(
  70. const char* name)
  71. {
  72. VariablesHashMap::iterator it = varNameToVar.find(name);
  73. if(it == varNameToVar.end())
  74. {
  75. throw ANKI_EXCEPTION("Cannot get material runtime variable "
  76. "with name \"" + name + '\"');
  77. }
  78. return *(it->second);
  79. }
  80. //==============================================================================
  81. const MaterialRuntimeVariable& MaterialRuntime::findVariableByName(
  82. const char* name) const
  83. {
  84. VariablesHashMap::const_iterator it = varNameToVar.find(name);
  85. if(it == varNameToVar.end())
  86. {
  87. throw ANKI_EXCEPTION("Cannot get material runtime variable "
  88. "with name \"" + name + '\"');
  89. }
  90. return *(it->second);
  91. }
  92. } // end namespace