effect.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include <wallpaperengine/fs/utils.h>
  2. #include "shaders/compiler.h"
  3. #include "effect.h"
  4. #include "irrlicht.h"
  5. extern irr::f32 g_Time;
  6. namespace wp
  7. {
  8. effect::effect (json json_data)
  9. {
  10. json::const_iterator file = json_data.find ("file");
  11. json::const_iterator pass = json_data.find ("passes");
  12. if (file != json_data.end () && (*file).is_string () == true)
  13. {
  14. this->m_file = (*file).get <std::string> ().c_str ();
  15. }
  16. // passes list is optional
  17. if (pass == json_data.end () || (*pass).is_array () == false)
  18. return;
  19. json::const_iterator curpass = (*pass).begin ();
  20. json::const_iterator endpass = (*pass).end ();
  21. if (curpass == endpass)
  22. return;
  23. json::const_iterator textures = (*curpass).find ("textures");
  24. if (textures == (*curpass).end () || (*textures).is_array () == false)
  25. return;
  26. json::const_iterator curtex = (*textures).begin ();
  27. json::const_iterator endtex = (*textures).end ();
  28. for (; curtex != endtex; curtex ++)
  29. {
  30. if ((*curtex).is_null () == true)
  31. {
  32. this->m_textures.push_back (nullptr);
  33. }
  34. else if ((*curtex).is_string () == true)
  35. {
  36. irr::io::path texturepath = ("materials/" + (*curtex).get <std::string> () + ".tex").c_str ();
  37. this->m_textures.push_back (new wp::texture (texturepath));
  38. }
  39. }
  40. this->m_content = wp::fs::utils::loadFullFile (this->m_file);
  41. this->m_json = json::parse (this->m_content);
  42. json::const_iterator passes = this->m_json.find ("passes");
  43. if (passes == this->m_json.end () || (*passes).is_array () == false)
  44. return;
  45. json::const_iterator curpassmaterial = (*passes).begin ();
  46. json::const_iterator endpassmaterial = (*passes).end ();
  47. for (; curpassmaterial != endpassmaterial; curpassmaterial ++)
  48. {
  49. json::const_iterator material_it = (*curpassmaterial).find ("material");
  50. if (material_it == (*curpassmaterial).end () || (*material_it).is_string () == false)
  51. continue;
  52. irr::io::path material = (*material_it).get <std::string> ().c_str ();
  53. std::string content = wp::fs::utils::loadFullFile (material);
  54. json material_json = json::parse (content);
  55. json::const_iterator shader_passes = material_json.find ("passes");
  56. if (shader_passes == material_json.end () || (*shader_passes).is_array () == false)
  57. continue;
  58. json::const_iterator shaderpasscur = (*shader_passes).begin ();
  59. json::const_iterator shader = (*shaderpasscur).find ("shader");
  60. if (shader == (*shaderpasscur).end () || (*shader).is_string () == false)
  61. continue;
  62. irr::io::path shaderpath = ("shaders/" + (*shader).get <std::string> ()).c_str ();
  63. irr::io::path fragpath = shaderpath + ".frag";
  64. irr::io::path vertpath = shaderpath + ".vert";
  65. wp::shaders::compiler* fragcompiler = new wp::shaders::compiler (fragpath, wp::shaders::compiler::Type::Type_Pixel, false);
  66. wp::shaders::compiler* vertcompiler = new wp::shaders::compiler (vertpath, wp::shaders::compiler::Type::Type_Vertex, false);
  67. this->m_materialType = wp::irrlicht::driver->getGPUProgrammingServices ()
  68. ->addHighLevelShaderMaterial (
  69. vertcompiler->precompile ().c_str (), "main", irr::video::EVST_VS_2_0,
  70. fragcompiler->precompile ().c_str (), "main", irr::video::EPST_PS_2_0,
  71. this, irr::video::EMT_TRANSPARENT_ALPHA_CHANNEL, 0, irr::video::EGSL_DEFAULT
  72. );
  73. delete fragcompiler;
  74. delete vertcompiler;
  75. }
  76. }
  77. std::vector<wp::texture*>& effect::getTextureList ()
  78. {
  79. return this->m_textures;
  80. }
  81. irr::s32 effect::getMaterialType ()
  82. {
  83. return this->m_materialType;
  84. }
  85. void effect::OnSetConstants (irr::video::IMaterialRendererServices* services, int32_t userData)
  86. {
  87. // TODO: IMPLEMENT PROPER SHADER CODE
  88. irr::f32 g_AnimationSpeed = 0.1f;
  89. irr::f32 g_Scale = 2.5f;
  90. irr::f32 g_ScrollSpeed = 0.0f;
  91. irr::f32 g_Direction = 0.0f;
  92. irr::f32 g_Strength = 0.07f;
  93. irr::f32 g_SpecularPower = 1.0f;
  94. irr::f32 g_SpecularStrength = 1.0f;
  95. irr::f32 g_SpecularColor [3] = {1.0f, 1.0f, 1.0f};
  96. irr::f32 g_Texture1Resolution [4] = {1.0f, 1.0f, 1.0f, 1.0f};
  97. irr::f32 g_Texture0 = 0;
  98. irr::f32 g_Texture1 = 1;
  99. irr::f32 g_Texture2 = 2;
  100. irr::video::IVideoDriver* driver = services->getVideoDriver ();
  101. irr::core::matrix4 worldViewProj;
  102. worldViewProj = driver->getTransform(irr::video::ETS_PROJECTION);
  103. worldViewProj *= driver->getTransform(irr::video::ETS_VIEW);
  104. worldViewProj *= driver->getTransform(irr::video::ETS_WORLD);
  105. services->setVertexShaderConstant ("g_AnimationSpeed", &g_AnimationSpeed, 1);
  106. services->setVertexShaderConstant ("g_Scale", &g_Scale, 1);
  107. services->setVertexShaderConstant ("g_ScrollSpeed", &g_ScrollSpeed, 1);
  108. services->setVertexShaderConstant ("g_Direction", &g_Direction, 1);
  109. services->setVertexShaderConstant ("g_Time", &g_Time, 1);
  110. services->setVertexShaderConstant ("g_ModelViewProjectionMatrix", worldViewProj.pointer(), 16);
  111. services->setVertexShaderConstant ("g_Texture0Resolution", g_Texture1Resolution, 4);
  112. services->setVertexShaderConstant ("g_Texture1Resolution", g_Texture1Resolution, 4);
  113. services->setVertexShaderConstant ("g_Texture2Resolution", g_Texture1Resolution, 4);
  114. // TODO: Support up to 7 materials (as wallpaper engine)
  115. services->setPixelShaderConstant ("g_Strength", &g_Strength, 1);
  116. services->setPixelShaderConstant ("g_SpecularPower", &g_SpecularPower, 1);
  117. services->setPixelShaderConstant ("g_SpecularStrength", &g_SpecularStrength, 1);
  118. services->setPixelShaderConstant ("g_SpecularColor", g_SpecularColor, 3);
  119. services->setPixelShaderConstant ("g_Texture0", &g_Texture0, 1);
  120. services->setPixelShaderConstant ("g_Texture1", &g_Texture1, 1);
  121. services->setPixelShaderConstant ("g_Texture2", &g_Texture2, 1);
  122. }
  123. };