LightRsrc.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include "anki/resource/LightRsrc.h"
  2. #include "anki/resource/Texture.h"
  3. #include "anki/misc/PropertyTree.h"
  4. #include "anki/core/Logger.h"
  5. #include "anki/core/Globals.h"
  6. #include <cstring>
  7. #include <string>
  8. #include <boost/property_tree/ptree.hpp>
  9. #include <boost/property_tree/xml_parser.hpp>
  10. #include <boost/foreach.hpp>
  11. #include <boost/lexical_cast.hpp>
  12. namespace anki {
  13. //==============================================================================
  14. LightRsrc::LightRsrc()
  15. {
  16. diffuseCol = Vec3(0.5);
  17. specularCol = Vec3(0.5);
  18. castsShadowFlag = false;
  19. radius = 1.0;
  20. distance = 3.0;
  21. fovX = fovY = Math::PI / 4.0;
  22. width = height = 1.0;
  23. spotLightCameraType = SLCT_PERSPECTIVE;
  24. }
  25. //==============================================================================
  26. void LightRsrc::load(const char* filename)
  27. {
  28. try
  29. {
  30. using namespace boost::property_tree;
  31. ptree rpt;
  32. read_xml(filename, rpt);
  33. const ptree& pt = rpt.get_child("light");
  34. //
  35. // type
  36. //
  37. std::string type_ = pt.get<std::string>("type");
  38. if(type_ == "POINT")
  39. {
  40. type = LT_POINT;
  41. }
  42. else if(type_ == "SPOT")
  43. {
  44. type = LT_SPOT;
  45. }
  46. else
  47. {
  48. throw ANKI_EXCEPTION("Incorrect type: " + type_);
  49. }
  50. //
  51. // diffuseCol
  52. //
  53. boost::optional<const ptree&> diffColTree =
  54. pt.get_child_optional("diffuseCol");
  55. if(diffColTree)
  56. {
  57. diffuseCol = PropertyTree::getVec3(diffColTree.get());
  58. }
  59. //
  60. // specularCol
  61. //
  62. boost::optional<const ptree&> specColTree =
  63. pt.get_child_optional("specularCol");
  64. if(specColTree)
  65. {
  66. specularCol = PropertyTree::getVec3(specColTree.get());
  67. }
  68. //
  69. // castsShadow
  70. //
  71. boost::optional<bool> castsShadow_ =
  72. PropertyTree::getBoolOptional(pt, "castsShadow");
  73. if(castsShadow_)
  74. {
  75. castsShadowFlag = castsShadow_.get();
  76. }
  77. //
  78. // radius
  79. //
  80. boost::optional<float> radius_ = pt.get_optional<float>("radius");
  81. if(radius_)
  82. {
  83. radius = radius_.get();
  84. if(type == LT_SPOT)
  85. {
  86. ANKI_WARNING("File \"" << filename <<
  87. "\": No radius for spot lights");
  88. }
  89. }
  90. //
  91. // distance
  92. //
  93. boost::optional<float> distance_ = pt.get_optional<float>("distance");
  94. if(distance_)
  95. {
  96. distance = distance_.get();
  97. if(type == LT_POINT)
  98. {
  99. ANKI_WARNING("File \"" << filename <<
  100. "\": No distance for point lights");
  101. }
  102. }
  103. //
  104. // fovX
  105. //
  106. boost::optional<float> fovX_ = pt.get_optional<float>("fovX");
  107. if(fovX_)
  108. {
  109. fovX = fovX_.get();
  110. if(type == LT_POINT)
  111. {
  112. ANKI_WARNING("File \"" << filename <<
  113. "\": No fovX for point lights");
  114. }
  115. }
  116. //
  117. // fovY
  118. //
  119. boost::optional<float> fovY_ = pt.get_optional<float>("fovY");
  120. if(fovY_)
  121. {
  122. fovY = fovY_.get();
  123. if(type == LT_POINT)
  124. {
  125. ANKI_WARNING("File \"" << filename <<
  126. "\": No fovY for point lights");
  127. }
  128. }
  129. //
  130. // width
  131. //
  132. boost::optional<float> width_ = pt.get_optional<float>("width");
  133. if(width_)
  134. {
  135. width = width_.get();
  136. if(type == LT_POINT)
  137. {
  138. ANKI_WARNING("File \"" << filename <<
  139. "\": No width for point lights");
  140. }
  141. }
  142. //
  143. // height
  144. //
  145. boost::optional<float> height_ = pt.get_optional<float>("height");
  146. if(height_)
  147. {
  148. height = height_.get();
  149. if(type == LT_POINT)
  150. {
  151. ANKI_WARNING("File \"" << filename <<
  152. "\": No height for point lights");
  153. }
  154. }
  155. //
  156. // texture
  157. //
  158. boost::optional<std::string> tex =
  159. pt.get_optional<std::string>("texture");
  160. if(tex)
  161. {
  162. texture.load(tex.get().c_str());
  163. if(type == LT_POINT)
  164. {
  165. ANKI_WARNING("File \"" << filename <<
  166. "\": No texture for point lights");
  167. }
  168. }
  169. //
  170. // cameraType
  171. //
  172. boost::optional<std::string> cam =
  173. pt.get_optional<std::string>("cameraType");
  174. if(cam)
  175. {
  176. if(cam.get() == "PERSPECTIVE")
  177. {
  178. spotLightCameraType = SLCT_PERSPECTIVE;
  179. }
  180. else if(cam.get() == "ORTHOGRAPHIC")
  181. {
  182. spotLightCameraType = SLCT_ORTHOGRAPHIC;
  183. }
  184. else
  185. {
  186. throw ANKI_EXCEPTION("Incorrect cameraYype: " + cam.get());
  187. }
  188. }
  189. }
  190. catch(std::exception& e)
  191. {
  192. throw ANKI_EXCEPTION_R("Material \"" + filename + "\": ", e);
  193. }
  194. }
  195. } // end namespace