LightRsrc.cpp 4.2 KB

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