image.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.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 = (*file_it).get <std::string> ().c_str ();
  23. this->m_content = wp::fs::utils::loadFullFile (this->m_file);
  24. json content = json::parse (this->m_content);
  25. json::const_iterator it = content.find ("material");
  26. if (it != content.end () && (*it).is_string () == true)
  27. {
  28. irr::io::path materialfile = (*it).get <std::string> ().c_str ();
  29. std::string texturejson_content = wp::fs::utils::loadFullFile (materialfile);
  30. json materialcontent = json::parse (texturejson_content);
  31. // now try to read the texture if any
  32. json::const_iterator it = materialcontent.find ("passes");
  33. if (it != materialcontent.end () && (*it).is_array() == true)
  34. {
  35. json::const_iterator passesCur = (*it).begin ();
  36. json::const_iterator passesEnd = (*it).end ();
  37. for (; passesCur != passesEnd; passesCur ++)
  38. {
  39. json::const_iterator texture = (*passesCur).find ("textures");
  40. if (texture != (*passesCur).end () && (*texture).is_array () == true)
  41. {
  42. json::const_iterator texturesCur = (*texture).begin ();
  43. json::const_iterator texturesEnd = (*texture).end ();
  44. for (; texturesCur != texturesEnd; texturesCur ++)
  45. {
  46. irr::io::path texturepath = ("materials/" + (*texturesCur).get <std::string> () + ".tex").c_str ();
  47. this->m_textures.push_back (new wp::texture (texturepath));
  48. }
  49. }
  50. }
  51. }
  52. }
  53. }
  54. // TODO: CHECK EFFECT PASSES HERE SO WE CAN TAKE IN ACCOUNT THE EXTRA TEXTURES FOR SHADERS, ETC
  55. json::const_iterator effects = json_data.find ("effects");
  56. if (effects != json_data.end ())
  57. {
  58. // TODO: SUPPORT MULTIPLE EFFECTS
  59. json::const_iterator effectsItem = (*effects).begin ();
  60. if (effectsItem != (*effects).end ())
  61. {
  62. // TODO: SUPPORT MULTIPLE PASSES FOR EFFECTS
  63. json::const_iterator passes = (*effectsItem).find ("passes");
  64. json::const_iterator file = (*effectsItem).find ("file");
  65. }
  66. }
  67. if (origin_it != json_data.end ())
  68. {
  69. std::string origin = *origin_it;
  70. this->m_origin = wp::core::ato3vf (origin.c_str ());
  71. }
  72. else
  73. {
  74. // TOOD: CHANGE TO CENTER?
  75. this->m_origin = irr::core::vector3df (0.0f, 0.0f, 0.0f);
  76. }
  77. // initialize actual material properties
  78. irr::f32 xright = this->m_origin.X;
  79. irr::f32 xleft = -this->m_origin.X;
  80. irr::f32 ztop = this->m_origin.Y;
  81. irr::f32 zbottom = -this->m_origin.Y;
  82. irr::f32 z = this->m_scene->getCamera ()->getEye ().Z;
  83. m_vertices [0].Pos = irr::core::vector3df (xleft, ztop, z); // top left
  84. m_vertices [1].Pos = irr::core::vector3df (xright, ztop, z); // top right
  85. m_vertices [2].Pos = irr::core::vector3df (xright, zbottom, z); // bottom right
  86. m_vertices [3].Pos = irr::core::vector3df (xleft, zbottom, z); // bottom left
  87. m_vertices [0].TCoords = irr::core::vector2df (1.0f, 0.0f);
  88. m_vertices [1].TCoords = irr::core::vector2df (0.0f, 0.0f);
  89. m_vertices [2].TCoords = irr::core::vector2df (0.0f, 1.0f);
  90. m_vertices [3].TCoords = irr::core::vector2df (1.0f, 1.0f);
  91. m_vertices [0].Color = irr::video::SColor (255, 255, 255, 255);
  92. m_vertices [1].Color = irr::video::SColor (255, 255, 255, 255);
  93. m_vertices [2].Color = irr::video::SColor (255, 255, 255, 255);
  94. m_vertices [3].Color = irr::video::SColor (255, 255, 255, 255);
  95. // once textures are defined, prepare them to be displayed
  96. std::vector<wp::texture*>::const_iterator cur = this->m_textures.begin ();
  97. std::vector<wp::texture*>::const_iterator end = this->m_textures.end ();
  98. for (irr::u32 i = 0; cur != end; cur ++, i ++)
  99. {
  100. this->getMaterial ().setTexture (i, (*cur)->getIrrTexture ());
  101. }
  102. // set basic material flags
  103. this->setType (irr::video::EMT_TRANSPARENT_ALPHA_CHANNEL);
  104. this->setFlag (irr::video::EMF_LIGHTING, false);
  105. this->setFlag (irr::video::EMF_BLEND_OPERATION, true);
  106. }
  107. void image::setFlag(irr::video::E_MATERIAL_FLAG flag, bool newvalue)
  108. {
  109. this->getMaterial ().setFlag (flag, newvalue);
  110. }
  111. void image::setType(irr::video::E_MATERIAL_TYPE newType)
  112. {
  113. this->getMaterial ().MaterialType = newType;
  114. }
  115. irr::video::SMaterial& image::getMaterial ()
  116. {
  117. return this->m_material;
  118. }
  119. void image::render ()
  120. {
  121. if (this->m_visible == false) return;
  122. uint16_t indices[] =
  123. {
  124. 0, 1, 2, 3
  125. };
  126. wp::irrlicht::driver->setMaterial (this->getMaterial ());
  127. wp::irrlicht::driver->drawVertexPrimitiveList (this->m_vertices, 4, indices, 1, irr::video::EVT_STANDARD, irr::scene::EPT_QUADS, irr::video::EIT_16BIT);
  128. }
  129. }