image.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. irr::io::path texturePath = this->m_resolver.resolveOnWorkingDirectory ((*texturesCur));
  54. this->m_textures.push_back (new wp::texture (texturePath));
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }
  61. // TODO: CHECK EFFECT PASSES HERE SO WE CAN TAKE IN ACCOUNT THE EXTRA TEXTURES FOR SHADERS, ETC
  62. if (origin_it != json_data.end ())
  63. {
  64. std::string origin = *origin_it;
  65. this->m_origin = wp::core::ato3vf (origin.c_str ());
  66. }
  67. else
  68. {
  69. // TOOD: CHANGE TO CENTER?
  70. this->m_origin = irr::core::vector3df (0.0f, 0.0f, 0.0f);
  71. }
  72. // initialize actual material properties
  73. irr::f32 xright = this->m_origin.X;
  74. irr::f32 xleft = -this->m_origin.X;
  75. irr::f32 ztop = this->m_origin.Y;
  76. irr::f32 zbottom = -this->m_origin.Y;
  77. irr::f32 z = this->m_scene->getCamera ()->getEye ().Z;
  78. m_vertices [0].Pos = irr::core::vector3df (xleft, ztop, z); // top left
  79. m_vertices [1].Pos = irr::core::vector3df (xright, ztop, z); // top right
  80. m_vertices [2].Pos = irr::core::vector3df (xright, zbottom, z); // bottom right
  81. m_vertices [3].Pos = irr::core::vector3df (xleft, zbottom, z); // bottom left
  82. m_vertices [0].TCoords = irr::core::vector2df (1.0f, 0.0f);
  83. m_vertices [1].TCoords = irr::core::vector2df (0.0f, 0.0f);
  84. m_vertices [2].TCoords = irr::core::vector2df (0.0f, 1.0f);
  85. m_vertices [3].TCoords = irr::core::vector2df (1.0f, 1.0f);
  86. m_vertices [0].Color = irr::video::SColor (255, 255, 255, 255);
  87. m_vertices [1].Color = irr::video::SColor (255, 255, 255, 255);
  88. m_vertices [2].Color = irr::video::SColor (255, 255, 255, 255);
  89. m_vertices [3].Color = irr::video::SColor (255, 255, 255, 255);
  90. // once textures are defined, prepare them to be displayed
  91. std::vector<wp::texture*>::const_iterator cur = this->m_textures.begin ();
  92. std::vector<wp::texture*>::const_iterator end = this->m_textures.end ();
  93. for (int i = 0; cur != end; cur ++, i ++)
  94. {
  95. this->getMaterial ().setTexture (i, (*cur)->getIrrTexture ());
  96. }
  97. }
  98. void image::setFlag(irr::video::E_MATERIAL_FLAG flag, bool newvalue)
  99. {
  100. this->getMaterial ().setFlag (flag, newvalue);
  101. }
  102. void image::setType(irr::video::E_MATERIAL_TYPE newType)
  103. {
  104. this->getMaterial ().MaterialType = newType;
  105. }
  106. irr::video::SMaterial& image::getMaterial ()
  107. {
  108. return this->m_material;
  109. }
  110. void image::render ()
  111. {
  112. uint16_t indices[] =
  113. {
  114. 0, 1, 2, 3
  115. };
  116. wp::irrlicht::driver->setMaterial (this->m_material);
  117. wp::irrlicht::driver->drawVertexPrimitiveList (this->m_vertices, 4, indices, 1, irr::video::EVT_STANDARD, irr::scene::EPT_QUADS, irr::video::EIT_16BIT);
  118. }
  119. }