image.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include <irrlicht/irrlicht.h>
  2. #include <fstream>
  3. #include <wallpaperengine/fs/fileResolver.h>
  4. #include <wallpaperengine/object3d.h>
  5. #include <wallpaperengine/image.h>
  6. #include <wallpaperengine/irrlicht.h>
  7. #include <wallpaperengine/core.h>
  8. namespace wp
  9. {
  10. image::image (json json_data, wp::scene* scene) : object3d (object3d::Type::Type_Material, scene)
  11. {
  12. json::const_iterator visible_it = json_data.find ("visible");
  13. json::const_iterator file_it = json_data.find ("image");
  14. json::const_iterator origin_it = json_data.find ("origin");
  15. if (visible_it != json_data.end () && (*visible_it).is_boolean () == true)
  16. {
  17. this->m_visible = *visible_it;
  18. }
  19. // basic texture, main thing to assign first
  20. if (file_it != json_data.end () && (*file_it).is_string () == true)
  21. {
  22. this->m_file = wp::fs::resolver.resolve (*file_it);
  23. this->m_content = "";
  24. std::ifstream _in (this->m_file.c_str ());
  25. this->m_content.append (std::istreambuf_iterator<char> (_in), std::istreambuf_iterator<char> ());
  26. json content = json::parse (this->m_content);
  27. json::const_iterator it = content.find ("material");
  28. if (it != content.end () && (*it).is_string () == true)
  29. {
  30. irr::io::path materialfile = wp::fs::resolver.resolveOnWorkingDirectory ((*it));
  31. std::ifstream _in (materialfile.c_str ());
  32. std::string texturejson_content = "";
  33. texturejson_content.append (std::istreambuf_iterator<char> (_in), std::istreambuf_iterator<char> ());
  34. json materialcontent = json::parse (texturejson_content);
  35. // change working directory for the resolver to the materials folder
  36. this->m_resolver = wp::fs::resolver.clone ();
  37. this->m_resolver.changeWorkingDirectory(this->m_resolver.resolveOnWorkingDirectory ("materials"));
  38. // now try to read the texture if any
  39. json::const_iterator it = materialcontent.find ("passes");
  40. if (it != materialcontent.end () && (*it).is_array() == true)
  41. {
  42. json::const_iterator passesCur = (*it).begin ();
  43. json::const_iterator passesEnd = (*it).end ();
  44. for (; passesCur != passesEnd; passesCur ++)
  45. {
  46. json::const_iterator texture = (*passesCur).find ("textures");
  47. if (texture != (*passesCur).end () && (*texture).is_array () == true)
  48. {
  49. json::const_iterator texturesCur = (*texture).begin ();
  50. json::const_iterator texturesEnd = (*texture).end ();
  51. for (; texturesCur != texturesEnd; texturesCur ++)
  52. {
  53. // TODO: SUPPORT PROPER WALLPAPERENGINE FORMATS
  54. std::string name = (*texturesCur);
  55. name += ".tex";
  56. irr::io::path texturePath = this->m_resolver.resolveOnWorkingDirectory (name);
  57. std::string basename = (*texturesCur);
  58. this->m_textures.push_back (new wp::texture (texturePath));
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }
  65. // TODO: CHECK EFFECT PASSES HERE SO WE CAN TAKE IN ACCOUNT THE EXTRA TEXTURES FOR SHADERS, ETC
  66. json::const_iterator effects = json_data.find ("effects");
  67. if (effects != json_data.end ())
  68. {
  69. // TODO: SUPPORT MULTIPLE EFFECTS
  70. json::const_iterator effectsItem = (*effects).begin ();
  71. if (effectsItem != (*effects).end ())
  72. {
  73. // TODO: SUPPORT MULTIPLE PASSES FOR EFFECTS
  74. json::const_iterator passes = (*effectsItem).find ("passes");
  75. json::const_iterator file = (*effectsItem).find ("file");
  76. }
  77. }
  78. if (origin_it != json_data.end ())
  79. {
  80. std::string origin = *origin_it;
  81. this->m_origin = wp::core::ato3vf (origin.c_str ());
  82. }
  83. else
  84. {
  85. // TOOD: CHANGE TO CENTER?
  86. this->m_origin = irr::core::vector3df (0.0f, 0.0f, 0.0f);
  87. }
  88. // initialize actual material properties
  89. irr::f32 xright = this->m_origin.X;
  90. irr::f32 xleft = -this->m_origin.X;
  91. irr::f32 ztop = this->m_origin.Y;
  92. irr::f32 zbottom = -this->m_origin.Y;
  93. irr::f32 z = this->m_scene->getCamera ()->getEye ().Z;
  94. m_vertices [0].Pos = irr::core::vector3df (xleft, ztop, z); // top left
  95. m_vertices [1].Pos = irr::core::vector3df (xright, ztop, z); // top right
  96. m_vertices [2].Pos = irr::core::vector3df (xright, zbottom, z); // bottom right
  97. m_vertices [3].Pos = irr::core::vector3df (xleft, zbottom, z); // bottom left
  98. m_vertices [0].TCoords = irr::core::vector2df (1.0f, 0.0f);
  99. m_vertices [1].TCoords = irr::core::vector2df (0.0f, 0.0f);
  100. m_vertices [2].TCoords = irr::core::vector2df (0.0f, 1.0f);
  101. m_vertices [3].TCoords = irr::core::vector2df (1.0f, 1.0f);
  102. m_vertices [0].Color = irr::video::SColor (255, 255, 255, 255);
  103. m_vertices [1].Color = irr::video::SColor (255, 255, 255, 255);
  104. m_vertices [2].Color = irr::video::SColor (255, 255, 255, 255);
  105. m_vertices [3].Color = irr::video::SColor (255, 255, 255, 255);
  106. // once textures are defined, prepare them to be displayed
  107. std::vector<wp::texture*>::const_iterator cur = this->m_textures.begin ();
  108. std::vector<wp::texture*>::const_iterator end = this->m_textures.end ();
  109. for (irr::u32 i = 0; cur != end; cur ++, i ++)
  110. {
  111. this->getMaterial ().setTexture (i, (*cur)->getIrrTexture ());
  112. }
  113. // set basic material flags
  114. this->setType (irr::video::EMT_TRANSPARENT_ALPHA_CHANNEL);
  115. this->setFlag (irr::video::EMF_LIGHTING, false);
  116. this->setFlag (irr::video::EMF_BLEND_OPERATION, true);
  117. }
  118. void image::setFlag(irr::video::E_MATERIAL_FLAG flag, bool newvalue)
  119. {
  120. this->getMaterial ().setFlag (flag, newvalue);
  121. }
  122. void image::setType(irr::video::E_MATERIAL_TYPE newType)
  123. {
  124. this->getMaterial ().MaterialType = newType;
  125. }
  126. irr::video::SMaterial& image::getMaterial ()
  127. {
  128. return this->m_material;
  129. }
  130. void image::render ()
  131. {
  132. if (this->m_visible == false) return;
  133. uint16_t indices[] =
  134. {
  135. 0, 1, 2, 3
  136. };
  137. wp::irrlicht::driver->setMaterial (this->getMaterial ());
  138. wp::irrlicht::driver->drawVertexPrimitiveList (this->m_vertices, 4, indices, 1, irr::video::EVT_STANDARD, irr::scene::EPT_QUADS, irr::video::EIT_16BIT);
  139. }
  140. }