X3DImporter_Light.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /// \file X3DImporter_Light.cpp
  34. /// \brief Parsing data from nodes of "Lighting" set of X3D.
  35. /// \date 2015-2016
  36. /// \author [email protected]
  37. #ifndef ASSIMP_BUILD_NO_X3D_IMPORTER
  38. #include "X3DImporter.hpp"
  39. #include "X3DImporter_Macro.hpp"
  40. #include "StringUtils.h"
  41. namespace Assimp {
  42. // <DirectionalLight
  43. // DEF="" ID
  44. // USE="" IDREF
  45. // ambientIntensity="0" SFFloat [inputOutput]
  46. // color="1 1 1" SFColor [inputOutput]
  47. // direction="0 0 -1" SFVec3f [inputOutput]
  48. // global="false" SFBool [inputOutput]
  49. // intensity="1" SFFloat [inputOutput]
  50. // on="true" SFBool [inputOutput]
  51. // />
  52. void X3DImporter::ParseNode_Lighting_DirectionalLight()
  53. {
  54. std::string def, use;
  55. float ambientIntensity = 0;
  56. aiColor3D color(1, 1, 1);
  57. aiVector3D direction(0, 0, -1);
  58. bool global = false;
  59. float intensity = 1;
  60. bool on = true;
  61. CX3DImporter_NodeElement* ne( nullptr );
  62. MACRO_ATTRREAD_LOOPBEG;
  63. MACRO_ATTRREAD_CHECKUSEDEF_RET(def, use);
  64. MACRO_ATTRREAD_CHECK_RET("ambientIntensity", ambientIntensity, XML_ReadNode_GetAttrVal_AsFloat);
  65. MACRO_ATTRREAD_CHECK_REF("color", color, XML_ReadNode_GetAttrVal_AsCol3f);
  66. MACRO_ATTRREAD_CHECK_REF("direction", direction, XML_ReadNode_GetAttrVal_AsVec3f);
  67. MACRO_ATTRREAD_CHECK_RET("global", global, XML_ReadNode_GetAttrVal_AsBool);
  68. MACRO_ATTRREAD_CHECK_RET("intensity", intensity, XML_ReadNode_GetAttrVal_AsFloat);
  69. MACRO_ATTRREAD_CHECK_RET("on", on, XML_ReadNode_GetAttrVal_AsBool);
  70. MACRO_ATTRREAD_LOOPEND;
  71. // if "USE" defined then find already defined element.
  72. if(!use.empty())
  73. {
  74. MACRO_USE_CHECKANDAPPLY(def, use, ENET_DirectionalLight, ne);
  75. }
  76. else
  77. {
  78. if(on)
  79. {
  80. // create and if needed - define new geometry object.
  81. ne = new CX3DImporter_NodeElement_Light(CX3DImporter_NodeElement::ENET_DirectionalLight, NodeElement_Cur);
  82. if(!def.empty())
  83. ne->ID = def;
  84. else
  85. ne->ID = "DirectionalLight_" + to_string((size_t)ne);// make random name
  86. ((CX3DImporter_NodeElement_Light*)ne)->AmbientIntensity = ambientIntensity;
  87. ((CX3DImporter_NodeElement_Light*)ne)->Color = color;
  88. ((CX3DImporter_NodeElement_Light*)ne)->Direction = direction;
  89. ((CX3DImporter_NodeElement_Light*)ne)->Global = global;
  90. ((CX3DImporter_NodeElement_Light*)ne)->Intensity = intensity;
  91. // Assimp want a node with name similar to a light. "Why? I don't no." )
  92. ParseHelper_Group_Begin(false);
  93. NodeElement_Cur->ID = ne->ID;// assign name to node and return to light element.
  94. ParseHelper_Node_Exit();
  95. // check for child nodes
  96. if(!mReader->isEmptyElement())
  97. ParseNode_Metadata(ne, "DirectionalLight");
  98. else
  99. NodeElement_Cur->Child.push_back(ne);// add made object as child to current element
  100. NodeElement_List.push_back(ne);// add element to node element list because its a new object in graph
  101. }// if(on)
  102. }// if(!use.empty()) else
  103. }
  104. // <PointLight
  105. // DEF="" ID
  106. // USE="" IDREF
  107. // ambientIntensity="0" SFFloat [inputOutput]
  108. // attenuation="1 0 0" SFVec3f [inputOutput]
  109. // color="1 1 1" SFColor [inputOutput]
  110. // global="true" SFBool [inputOutput]
  111. // intensity="1" SFFloat [inputOutput]
  112. // location="0 0 0" SFVec3f [inputOutput]
  113. // on="true" SFBool [inputOutput]
  114. // radius="100" SFFloat [inputOutput]
  115. // />
  116. void X3DImporter::ParseNode_Lighting_PointLight()
  117. {
  118. std::string def, use;
  119. float ambientIntensity = 0;
  120. aiVector3D attenuation( 1, 0, 0 );
  121. aiColor3D color( 1, 1, 1 );
  122. bool global = true;
  123. float intensity = 1;
  124. aiVector3D location( 0, 0, 0 );
  125. bool on = true;
  126. float radius = 100;
  127. CX3DImporter_NodeElement* ne( nullptr );
  128. MACRO_ATTRREAD_LOOPBEG;
  129. MACRO_ATTRREAD_CHECKUSEDEF_RET(def, use);
  130. MACRO_ATTRREAD_CHECK_RET("ambientIntensity", ambientIntensity, XML_ReadNode_GetAttrVal_AsFloat);
  131. MACRO_ATTRREAD_CHECK_REF("attenuation", attenuation, XML_ReadNode_GetAttrVal_AsVec3f);
  132. MACRO_ATTRREAD_CHECK_REF("color", color, XML_ReadNode_GetAttrVal_AsCol3f);
  133. MACRO_ATTRREAD_CHECK_RET("global", global, XML_ReadNode_GetAttrVal_AsBool);
  134. MACRO_ATTRREAD_CHECK_RET("intensity", intensity, XML_ReadNode_GetAttrVal_AsFloat);
  135. MACRO_ATTRREAD_CHECK_REF("location", location, XML_ReadNode_GetAttrVal_AsVec3f);
  136. MACRO_ATTRREAD_CHECK_RET("on", on, XML_ReadNode_GetAttrVal_AsBool);
  137. MACRO_ATTRREAD_CHECK_RET("radius", radius, XML_ReadNode_GetAttrVal_AsFloat);
  138. MACRO_ATTRREAD_LOOPEND;
  139. // if "USE" defined then find already defined element.
  140. if(!use.empty())
  141. {
  142. MACRO_USE_CHECKANDAPPLY(def, use, ENET_PointLight, ne);
  143. }
  144. else
  145. {
  146. if(on)
  147. {
  148. // create and if needed - define new geometry object.
  149. ne = new CX3DImporter_NodeElement_Light(CX3DImporter_NodeElement::ENET_PointLight, NodeElement_Cur);
  150. if(!def.empty()) ne->ID = def;
  151. ((CX3DImporter_NodeElement_Light*)ne)->AmbientIntensity = ambientIntensity;
  152. ((CX3DImporter_NodeElement_Light*)ne)->Attenuation = attenuation;
  153. ((CX3DImporter_NodeElement_Light*)ne)->Color = color;
  154. ((CX3DImporter_NodeElement_Light*)ne)->Global = global;
  155. ((CX3DImporter_NodeElement_Light*)ne)->Intensity = intensity;
  156. ((CX3DImporter_NodeElement_Light*)ne)->Location = location;
  157. ((CX3DImporter_NodeElement_Light*)ne)->Radius = radius;
  158. // Assimp want a node with name similar to a light. "Why? I don't no." )
  159. ParseHelper_Group_Begin(false);
  160. // make random name
  161. if(ne->ID.empty()) ne->ID = "PointLight_" + to_string((size_t)ne);
  162. NodeElement_Cur->ID = ne->ID;// assign name to node and return to light element.
  163. ParseHelper_Node_Exit();
  164. // check for child nodes
  165. if(!mReader->isEmptyElement())
  166. ParseNode_Metadata(ne, "PointLight");
  167. else
  168. NodeElement_Cur->Child.push_back(ne);// add made object as child to current element
  169. NodeElement_List.push_back(ne);// add element to node element list because its a new object in graph
  170. }// if(on)
  171. }// if(!use.empty()) else
  172. }
  173. // <SpotLight
  174. // DEF="" ID
  175. // USE="" IDREF
  176. // ambientIntensity="0" SFFloat [inputOutput]
  177. // attenuation="1 0 0" SFVec3f [inputOutput]
  178. // beamWidth="0.7854" SFFloat [inputOutput]
  179. // color="1 1 1" SFColor [inputOutput]
  180. // cutOffAngle="1.570796" SFFloat [inputOutput]
  181. // direction="0 0 -1" SFVec3f [inputOutput]
  182. // global="true" SFBool [inputOutput]
  183. // intensity="1" SFFloat [inputOutput]
  184. // location="0 0 0" SFVec3f [inputOutput]
  185. // on="true" SFBool [inputOutput]
  186. // radius="100" SFFloat [inputOutput]
  187. // />
  188. void X3DImporter::ParseNode_Lighting_SpotLight()
  189. {
  190. std::string def, use;
  191. float ambientIntensity = 0;
  192. aiVector3D attenuation( 1, 0, 0 );
  193. float beamWidth = 0.7854f;
  194. aiColor3D color( 1, 1, 1 );
  195. float cutOffAngle = 1.570796f;
  196. aiVector3D direction( 0, 0, -1 );
  197. bool global = true;
  198. float intensity = 1;
  199. aiVector3D location( 0, 0, 0 );
  200. bool on = true;
  201. float radius = 100;
  202. CX3DImporter_NodeElement* ne( nullptr );
  203. MACRO_ATTRREAD_LOOPBEG;
  204. MACRO_ATTRREAD_CHECKUSEDEF_RET(def, use);
  205. MACRO_ATTRREAD_CHECK_RET("ambientIntensity", ambientIntensity, XML_ReadNode_GetAttrVal_AsFloat);
  206. MACRO_ATTRREAD_CHECK_REF("attenuation", attenuation, XML_ReadNode_GetAttrVal_AsVec3f);
  207. MACRO_ATTRREAD_CHECK_RET("beamWidth", beamWidth, XML_ReadNode_GetAttrVal_AsFloat);
  208. MACRO_ATTRREAD_CHECK_REF("color", color, XML_ReadNode_GetAttrVal_AsCol3f);
  209. MACRO_ATTRREAD_CHECK_RET("cutOffAngle", cutOffAngle, XML_ReadNode_GetAttrVal_AsFloat);
  210. MACRO_ATTRREAD_CHECK_REF("direction", direction, XML_ReadNode_GetAttrVal_AsVec3f);
  211. MACRO_ATTRREAD_CHECK_RET("global", global, XML_ReadNode_GetAttrVal_AsBool);
  212. MACRO_ATTRREAD_CHECK_RET("intensity", intensity, XML_ReadNode_GetAttrVal_AsFloat);
  213. MACRO_ATTRREAD_CHECK_REF("location", location, XML_ReadNode_GetAttrVal_AsVec3f);
  214. MACRO_ATTRREAD_CHECK_RET("on", on, XML_ReadNode_GetAttrVal_AsBool);
  215. MACRO_ATTRREAD_CHECK_RET("radius", radius, XML_ReadNode_GetAttrVal_AsFloat);
  216. MACRO_ATTRREAD_LOOPEND;
  217. // if "USE" defined then find already defined element.
  218. if(!use.empty())
  219. {
  220. MACRO_USE_CHECKANDAPPLY(def, use, ENET_SpotLight, ne);
  221. }
  222. else
  223. {
  224. if(on)
  225. {
  226. // create and if needed - define new geometry object.
  227. ne = new CX3DImporter_NodeElement_Light(CX3DImporter_NodeElement::ENET_SpotLight, NodeElement_Cur);
  228. if(!def.empty()) ne->ID = def;
  229. if(beamWidth > cutOffAngle) beamWidth = cutOffAngle;
  230. ((CX3DImporter_NodeElement_Light*)ne)->AmbientIntensity = ambientIntensity;
  231. ((CX3DImporter_NodeElement_Light*)ne)->Attenuation = attenuation;
  232. ((CX3DImporter_NodeElement_Light*)ne)->BeamWidth = beamWidth;
  233. ((CX3DImporter_NodeElement_Light*)ne)->Color = color;
  234. ((CX3DImporter_NodeElement_Light*)ne)->CutOffAngle = cutOffAngle;
  235. ((CX3DImporter_NodeElement_Light*)ne)->Direction = direction;
  236. ((CX3DImporter_NodeElement_Light*)ne)->Global = global;
  237. ((CX3DImporter_NodeElement_Light*)ne)->Intensity = intensity;
  238. ((CX3DImporter_NodeElement_Light*)ne)->Location = location;
  239. ((CX3DImporter_NodeElement_Light*)ne)->Radius = radius;
  240. // Assimp want a node with name similar to a light. "Why? I don't no." )
  241. ParseHelper_Group_Begin(false);
  242. // make random name
  243. if(ne->ID.empty()) ne->ID = "SpotLight_" + to_string((size_t)ne);
  244. NodeElement_Cur->ID = ne->ID;// assign name to node and return to light element.
  245. ParseHelper_Node_Exit();
  246. // check for child nodes
  247. if(!mReader->isEmptyElement())
  248. ParseNode_Metadata(ne, "SpotLight");
  249. else
  250. NodeElement_Cur->Child.push_back(ne);// add made object as child to current element
  251. NodeElement_List.push_back(ne);// add element to node element list because its a new object in graph
  252. }// if(on)
  253. }// if(!use.empty()) else
  254. }
  255. }// namespace Assimp
  256. #endif // !ASSIMP_BUILD_NO_X3D_IMPORTER