X3DImporter_Shape.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2019, 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_Shape.cpp
  34. /// \brief Parsing data from nodes of "Shape" 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 "X3DXmlHelper.h"
  41. namespace Assimp {
  42. void X3DImporter::readShape(XmlNode &node) {
  43. std::string use, def;
  44. X3DNodeElementBase *ne(nullptr);
  45. MACRO_ATTRREAD_CHECKUSEDEF_RET(node, def, use);
  46. // if "USE" defined then find already defined element.
  47. if (!use.empty()) {
  48. ne = MACRO_USE_CHECKANDAPPLY(node, def, use, ENET_Shape, ne);
  49. } else {
  50. // create and if needed - define new geometry object.
  51. ne = new X3DNodeElementShape(mNodeElementCur);
  52. if (!def.empty()) ne->ID = def;
  53. // check for child nodes
  54. if (!isNodeEmpty(node)) {
  55. ParseHelper_Node_Enter(ne);
  56. for (auto currentChildNode : node.children()) {
  57. const std::string &currentChildName = currentChildNode.name();
  58. // check for appearance node
  59. if (currentChildName == "Appearance") readAppearance(currentChildNode);
  60. // check for X3DGeometryNodes
  61. else if (currentChildName == "Arc2D")
  62. readArc2D(currentChildNode);
  63. else if (currentChildName == "ArcClose2D")
  64. readArcClose2D(currentChildNode);
  65. else if (currentChildName == "Circle2D")
  66. readCircle2D(currentChildNode);
  67. else if (currentChildName == "Disk2D")
  68. readDisk2D(currentChildNode);
  69. else if (currentChildName == "Polyline2D")
  70. readPolyline2D(currentChildNode);
  71. else if (currentChildName == "Polypoint2D")
  72. readPolypoint2D(currentChildNode);
  73. else if (currentChildName == "Rectangle2D")
  74. readRectangle2D(currentChildNode);
  75. else if (currentChildName == "TriangleSet2D")
  76. readTriangleSet2D(currentChildNode);
  77. else if (currentChildName == "Box")
  78. readBox(currentChildNode);
  79. else if (currentChildName == "Cone")
  80. readCone(currentChildNode);
  81. else if (currentChildName == "Cylinder")
  82. readCylinder(currentChildNode);
  83. else if (currentChildName == "ElevationGrid")
  84. readElevationGrid(currentChildNode);
  85. else if (currentChildName == "Extrusion")
  86. readExtrusion(currentChildNode);
  87. else if (currentChildName == "IndexedFaceSet")
  88. readIndexedFaceSet(currentChildNode);
  89. else if (currentChildName == "Sphere")
  90. readSphere(currentChildNode);
  91. else if (currentChildName == "IndexedLineSet")
  92. readIndexedLineSet(currentChildNode);
  93. else if (currentChildName == "LineSet")
  94. readLineSet(currentChildNode);
  95. else if (currentChildName == "PointSet")
  96. readPointSet(currentChildNode);
  97. else if (currentChildName == "IndexedTriangleFanSet")
  98. readIndexedTriangleFanSet(currentChildNode);
  99. else if (currentChildName == "IndexedTriangleSet")
  100. readIndexedTriangleSet(currentChildNode);
  101. else if (currentChildName == "IndexedTriangleStripSet")
  102. readIndexedTriangleStripSet(currentChildNode);
  103. else if (currentChildName == "TriangleFanSet")
  104. readTriangleFanSet(currentChildNode);
  105. else if (currentChildName == "TriangleSet")
  106. readTriangleSet(currentChildNode);
  107. // check for X3DMetadataObject
  108. else if (!checkForMetadataNode(currentChildNode))
  109. skipUnsupportedNode("Shape", currentChildNode);
  110. }
  111. ParseHelper_Node_Exit();
  112. } // if (!isNodeEmpty(node))
  113. else {
  114. mNodeElementCur->Children.push_back(ne); // add made object as child to current element
  115. }
  116. NodeElement_List.push_back(ne); // add element to node element list because its a new object in graph
  117. } // if(!use.empty()) else
  118. }
  119. // <Appearance
  120. // DEF="" ID
  121. // USE="" IDREF
  122. // >
  123. // <!-- AppearanceChildContentModel -->
  124. // "Child-node content model corresponding to X3DAppearanceChildNode. Appearance can contain FillProperties, LineProperties, Material, any Texture node and
  125. // any TextureTransform node, in any order. No more than one instance of these nodes is allowed. Appearance may also contain multiple shaders (ComposedShader,
  126. // PackagedShader, ProgramShader).
  127. // A ProtoInstance node (with the proper node type) can be substituted for any node in this content model."
  128. // </Appearance>
  129. void X3DImporter::readAppearance(XmlNode &node) {
  130. std::string use, def;
  131. X3DNodeElementBase *ne(nullptr);
  132. MACRO_ATTRREAD_CHECKUSEDEF_RET(node, def, use);
  133. // if "USE" defined then find already defined element.
  134. if (!use.empty()) {
  135. ne = MACRO_USE_CHECKANDAPPLY(node, def, use, ENET_Appearance, ne);
  136. } else {
  137. // create and if needed - define new geometry object.
  138. ne = new X3DNodeElementAppearance(mNodeElementCur);
  139. if (!def.empty()) ne->ID = def;
  140. // check for child nodes
  141. if (!isNodeEmpty(node)) {
  142. ParseHelper_Node_Enter(ne);
  143. for (auto currentChildNode : node.children()) {
  144. const std::string &currentChildName = currentChildNode.name();
  145. if (currentChildName == "Material")
  146. readMaterial(currentChildNode);
  147. else if (currentChildName == "ImageTexture")
  148. readImageTexture(currentChildNode);
  149. else if (currentChildName == "TextureTransform")
  150. readTextureTransform(currentChildNode);
  151. // check for X3DMetadataObject
  152. else if (!checkForMetadataNode(currentChildNode))
  153. skipUnsupportedNode("Appearance", currentChildNode);
  154. }
  155. ParseHelper_Node_Exit();
  156. } // if(!isNodeEmpty(node))
  157. else {
  158. mNodeElementCur->Children.push_back(ne); // add made object as child to current element
  159. }
  160. NodeElement_List.push_back(ne); // add element to node element list because its a new object in graph
  161. } // if(!use.empty()) else
  162. }
  163. // <Material
  164. // DEF="" ID
  165. // USE="" IDREF
  166. // ambientIntensity="0.2" SFFloat [inputOutput]
  167. // diffuseColor="0.8 0.8 0.8" SFColor [inputOutput]
  168. // emissiveColor="0 0 0" SFColor [inputOutput]
  169. // shininess="0.2" SFFloat [inputOutput]
  170. // specularColor="0 0 0" SFColor [inputOutput]
  171. // transparency="0" SFFloat [inputOutput]
  172. // />
  173. void X3DImporter::readMaterial(XmlNode &node) {
  174. std::string use, def;
  175. float ambientIntensity = 0.2f;
  176. float shininess = 0.2f;
  177. float transparency = 0;
  178. aiColor3D diffuseColor(0.8f, 0.8f, 0.8f);
  179. aiColor3D emissiveColor(0, 0, 0);
  180. aiColor3D specularColor(0, 0, 0);
  181. X3DNodeElementBase *ne(nullptr);
  182. MACRO_ATTRREAD_CHECKUSEDEF_RET(node, def, use);
  183. XmlParser::getFloatAttribute(node, "ambientIntensity", ambientIntensity);
  184. XmlParser::getFloatAttribute(node, "shininess", shininess);
  185. XmlParser::getFloatAttribute(node, "transparency", transparency);
  186. X3DXmlHelper::getColor3DAttribute(node, "diffuseColor", diffuseColor);
  187. X3DXmlHelper::getColor3DAttribute(node, "emissiveColor", emissiveColor);
  188. X3DXmlHelper::getColor3DAttribute(node, "specularColor", specularColor);
  189. // if "USE" defined then find already defined element.
  190. if (!use.empty()) {
  191. ne = MACRO_USE_CHECKANDAPPLY(node, def, use, ENET_Material, ne);
  192. } else {
  193. // create and if needed - define new geometry object.
  194. ne = new X3DNodeElementMaterial(mNodeElementCur);
  195. if (!def.empty()) ne->ID = def;
  196. ((X3DNodeElementMaterial *)ne)->AmbientIntensity = ambientIntensity;
  197. ((X3DNodeElementMaterial *)ne)->Shininess = shininess;
  198. ((X3DNodeElementMaterial *)ne)->Transparency = transparency;
  199. ((X3DNodeElementMaterial *)ne)->DiffuseColor = diffuseColor;
  200. ((X3DNodeElementMaterial *)ne)->EmissiveColor = emissiveColor;
  201. ((X3DNodeElementMaterial *)ne)->SpecularColor = specularColor;
  202. // check for child nodes
  203. if (!isNodeEmpty(node))
  204. childrenReadMetadata(node, ne, "Material");
  205. else
  206. mNodeElementCur->Children.push_back(ne); // add made object as child to current element
  207. NodeElement_List.push_back(ne); // add element to node element list because its a new object in graph
  208. } // if(!use.empty()) else
  209. }
  210. } // namespace Assimp
  211. #endif // !ASSIMP_BUILD_NO_X3D_IMPORTER