image.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. irr::io::path texturePath = this->m_resolver.resolveOnWorkingDirectory ((*texturesCur));
  55. std::string basename = (*texturesCur);
  56. // TRY NORMAL EXTENSIONS FOR NOW...
  57. if (texturePath == "")
  58. {
  59. texturePath = this->m_resolver.resolveOnWorkingDirectory (basename + ".png");
  60. }
  61. if (texturePath == "")
  62. {
  63. texturePath = this->m_resolver.resolveOnWorkingDirectory (basename + ".jpg");
  64. }
  65. this->m_textures.push_back (new wp::texture (texturePath));
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }
  72. // TODO: CHECK EFFECT PASSES HERE SO WE CAN TAKE IN ACCOUNT THE EXTRA TEXTURES FOR SHADERS, ETC
  73. json::const_iterator effects = json_data.find ("effects");
  74. if (effects != json_data.end ())
  75. {
  76. // TODO: SUPPORT MULTIPLE EFFECTS
  77. json::const_iterator effectsItem = (*effects).begin ();
  78. if (effectsItem != (*effects).end ())
  79. {
  80. // TODO: SUPPORT MULTIPLE PASSES FOR EFFECTS
  81. json::const_iterator passes = (*effectsItem).find ("passes");
  82. json::const_iterator file = (*effectsItem).find ("file");
  83. }
  84. }
  85. if (origin_it != json_data.end ())
  86. {
  87. std::string origin = *origin_it;
  88. this->m_origin = wp::core::ato3vf (origin.c_str ());
  89. }
  90. else
  91. {
  92. // TOOD: CHANGE TO CENTER?
  93. this->m_origin = irr::core::vector3df (0.0f, 0.0f, 0.0f);
  94. }
  95. // initialize actual material properties
  96. irr::f32 xright = this->m_origin.X;
  97. irr::f32 xleft = -this->m_origin.X;
  98. irr::f32 ztop = this->m_origin.Y;
  99. irr::f32 zbottom = -this->m_origin.Y;
  100. irr::f32 z = this->m_scene->getCamera ()->getEye ().Z;
  101. m_vertices [0].Pos = irr::core::vector3df (xleft, ztop, z); // top left
  102. m_vertices [1].Pos = irr::core::vector3df (xright, ztop, z); // top right
  103. m_vertices [2].Pos = irr::core::vector3df (xright, zbottom, z); // bottom right
  104. m_vertices [3].Pos = irr::core::vector3df (xleft, zbottom, z); // bottom left
  105. m_vertices [0].TCoords = irr::core::vector2df (1.0f, 0.0f);
  106. m_vertices [1].TCoords = irr::core::vector2df (0.0f, 0.0f);
  107. m_vertices [2].TCoords = irr::core::vector2df (0.0f, 1.0f);
  108. m_vertices [3].TCoords = irr::core::vector2df (1.0f, 1.0f);
  109. m_vertices [0].Color = irr::video::SColor (255, 255, 255, 255);
  110. m_vertices [1].Color = irr::video::SColor (255, 255, 255, 255);
  111. m_vertices [2].Color = irr::video::SColor (255, 255, 255, 255);
  112. m_vertices [3].Color = irr::video::SColor (255, 255, 255, 255);
  113. // once textures are defined, prepare them to be displayed
  114. std::vector<wp::texture*>::const_iterator cur = this->m_textures.begin ();
  115. std::vector<wp::texture*>::const_iterator end = this->m_textures.end ();
  116. for (irr::u32 i = 0; cur != end; cur ++, i ++)
  117. {
  118. this->getMaterial ().setTexture (i, (*cur)->getIrrTexture ());
  119. }
  120. // set basic material flags
  121. this->setType (irr::video::EMT_TRANSPARENT_ALPHA_CHANNEL);
  122. this->setFlag (irr::video::EMF_LIGHTING, false);
  123. this->setFlag (irr::video::EMF_BLEND_OPERATION, true);
  124. }
  125. void image::setFlag(irr::video::E_MATERIAL_FLAG flag, bool newvalue)
  126. {
  127. this->getMaterial ().setFlag (flag, newvalue);
  128. }
  129. void image::setType(irr::video::E_MATERIAL_TYPE newType)
  130. {
  131. this->getMaterial ().MaterialType = newType;
  132. }
  133. irr::video::SMaterial& image::getMaterial ()
  134. {
  135. return this->m_material;
  136. }
  137. void image::render ()
  138. {
  139. if (this->m_visible == false) return;
  140. uint16_t indices[] =
  141. {
  142. 0, 1, 2, 3
  143. };
  144. wp::irrlicht::driver->setMaterial (this->getMaterial ());
  145. wp::irrlicht::driver->drawVertexPrimitiveList (this->m_vertices, 4, indices, 1, irr::video::EVT_STANDARD, irr::scene::EPT_QUADS, irr::video::EIT_16BIT);
  146. }
  147. }