image.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include <irrlicht/irrlicht.h>
  2. #include <fstream>
  3. #include <WallpaperEngine/fs/utils.h>
  4. #include <WallpaperEngine/object3d.h>
  5. #include <WallpaperEngine/image.h>
  6. #include <WallpaperEngine/Irrlicht/Irrlicht.h>
  7. #include <WallpaperEngine/Core/Core.h>
  8. namespace WallpaperEngine
  9. {
  10. image::image (json json_data, WallpaperEngine::object* parent) : object3d (object3d::Type::Type_Material, parent)
  11. {
  12. this->m_parent = parent;
  13. json::const_iterator visible_it = json_data.find ("visible");
  14. json::const_iterator file_it = json_data.find ("image");
  15. json::const_iterator origin_it = json_data.find ("origin");
  16. if (visible_it != json_data.end () && (*visible_it).is_boolean () == true)
  17. {
  18. this->m_visible = *visible_it;
  19. }
  20. // basic texture, main thing to assign first
  21. if (file_it != json_data.end () && (*file_it).is_string () == true)
  22. {
  23. this->m_file = (*file_it).get <std::string> ().c_str ();
  24. this->m_content = WallpaperEngine::FileSystem::loadFullFile (this->m_file);
  25. json content = json::parse (this->m_content);
  26. json::const_iterator it = content.find ("material");
  27. if (it != content.end () && (*it).is_string () == true)
  28. {
  29. irr::io::path materialfile = (*it).get <std::string> ().c_str ();
  30. std::string texturejson_content = WallpaperEngine::FileSystem::loadFullFile (materialfile);
  31. json materialcontent = json::parse (texturejson_content);
  32. // now try to read the texture if any
  33. json::const_iterator it = materialcontent.find ("passes");
  34. if (it != materialcontent.end () && (*it).is_array() == true)
  35. {
  36. json::const_iterator passesCur = (*it).begin ();
  37. json::const_iterator passesEnd = (*it).end ();
  38. for (; passesCur != passesEnd; passesCur ++)
  39. {
  40. json::const_iterator texture = (*passesCur).find ("textures");
  41. if (texture != (*passesCur).end () && (*texture).is_array () == true)
  42. {
  43. json::const_iterator texturesCur = (*texture).begin ();
  44. json::const_iterator texturesEnd = (*texture).end ();
  45. for (; texturesCur != texturesEnd; texturesCur ++)
  46. {
  47. irr::io::path texturepath = ("materials/" + (*texturesCur).get <std::string> () + ".tex").c_str ();
  48. this->m_textures.push_back (new WallpaperEngine::texture (texturepath));
  49. }
  50. }
  51. }
  52. }
  53. }
  54. }
  55. // initialize actual material properties
  56. irr::f32 xright = this->m_parent->getOrigin ().X;
  57. irr::f32 xleft = -this->m_parent->getOrigin ().X;
  58. irr::f32 ztop = this->m_parent->getOrigin ().Y;
  59. irr::f32 zbottom = -this->m_parent->getOrigin ().Y;
  60. irr::f32 z = this->m_parent->getScene ()->getCamera ()->getEye ().Z;
  61. m_vertices [0].Pos = irr::core::vector3df (xleft, ztop, z); // top left
  62. m_vertices [1].Pos = irr::core::vector3df (xright, ztop, z); // top right
  63. m_vertices [2].Pos = irr::core::vector3df (xright, zbottom, z); // bottom right
  64. m_vertices [3].Pos = irr::core::vector3df (xleft, zbottom, z); // bottom left
  65. m_vertices [0].TCoords = irr::core::vector2df (1.0f, 0.0f);
  66. m_vertices [1].TCoords = irr::core::vector2df (0.0f, 0.0f);
  67. m_vertices [2].TCoords = irr::core::vector2df (0.0f, 1.0f);
  68. m_vertices [3].TCoords = irr::core::vector2df (1.0f, 1.0f);
  69. m_vertices [0].Color = irr::video::SColor (255, 255, 255, 255);
  70. m_vertices [1].Color = irr::video::SColor (255, 255, 255, 255);
  71. m_vertices [2].Color = irr::video::SColor (255, 255, 255, 255);
  72. m_vertices [3].Color = irr::video::SColor (255, 255, 255, 255);
  73. // once textures are defined, prepare them to be displayed
  74. std::vector<WallpaperEngine::texture*>::const_iterator cur = this->m_textures.begin ();
  75. std::vector<WallpaperEngine::texture*>::const_iterator end = this->m_textures.end ();
  76. for (irr::u32 i = 0; cur != end; cur ++, i ++)
  77. {
  78. this->getMaterial ().setTexture (i, (*cur)->getIrrTexture ());
  79. }
  80. std::vector<WallpaperEngine::effect*>::const_iterator effect = this->m_parent->getEffects ().begin ();
  81. if (effect != this->m_parent->getEffects ().end ())
  82. {
  83. // loop through the textures the effect is using
  84. std::vector<WallpaperEngine::texture*> list = (*effect)->getTextureList ();
  85. std::vector<WallpaperEngine::texture*>::const_iterator curtex = list.begin ();
  86. std::vector<WallpaperEngine::texture*>::const_iterator endtex = list.end ();
  87. for (int current = 0; curtex != endtex; curtex ++, current ++)
  88. {
  89. if ((*curtex) == nullptr)
  90. continue;
  91. this->getMaterial ().setTexture (current, (*curtex)->getIrrTexture ());
  92. }
  93. this->setType ((irr::video::E_MATERIAL_TYPE) (*effect)->getMaterialType ());
  94. }
  95. else
  96. {
  97. this->setType (irr::video::EMT_TRANSPARENT_ALPHA_CHANNEL);
  98. }
  99. // set basic material flags
  100. this->setFlag (irr::video::EMF_LIGHTING, false);
  101. this->setFlag (irr::video::EMF_BLEND_OPERATION, true);
  102. }
  103. void image::setFlag(irr::video::E_MATERIAL_FLAG flag, bool newvalue)
  104. {
  105. this->getMaterial ().setFlag (flag, newvalue);
  106. }
  107. void image::setType(irr::video::E_MATERIAL_TYPE newType)
  108. {
  109. this->getMaterial ().MaterialType = newType;
  110. }
  111. irr::video::SMaterial& image::getMaterial ()
  112. {
  113. return this->m_material;
  114. }
  115. void image::render ()
  116. {
  117. if (this->m_visible == false) return;
  118. uint16_t indices[] =
  119. {
  120. 0, 1, 2, 3
  121. };
  122. WallpaperEngine::Irrlicht::driver->setMaterial (this->getMaterial ());
  123. WallpaperEngine::Irrlicht::driver->drawVertexPrimitiveList (this->m_vertices, 4, indices, 1, irr::video::EVT_STANDARD, irr::scene::EPT_QUADS, irr::video::EIT_16BIT);
  124. }
  125. }