AMFImporter_Material.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2020, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /// \file AMFImporter_Material.cpp
  35. /// \brief Parsing data from material nodes.
  36. /// \date 2016
  37. /// \author [email protected]
  38. #ifndef ASSIMP_BUILD_NO_AMF_IMPORTER
  39. #include "AMFImporter.hpp"
  40. //#include "AMFImporter_Macro.hpp"
  41. namespace Assimp
  42. {
  43. // <color
  44. // profile="" - The ICC color space used to interpret the three color channels <r>, <g> and <b>.
  45. // >
  46. // </color>
  47. // A color definition.
  48. // Multi elements - No.
  49. // Parent element - <material>, <object>, <volume>, <vertex>, <triangle>.
  50. //
  51. // "profile" can be one of "sRGB", "AdobeRGB", "Wide-Gamut-RGB", "CIERGB", "CIELAB", or "CIEXYZ".
  52. // Children elements:
  53. // <r>, <g>, <b>, <a>
  54. // Multi elements - No.
  55. // Red, Greed, Blue and Alpha (transparency) component of a color in sRGB space, values ranging from 0 to 1. The
  56. // values can be specified as constants, or as a formula depending on the coordinates.
  57. void AMFImporter::ParseNode_Color(XmlNode &node) {
  58. std::string profile = node.attribute("profile").as_string();
  59. // create new color object.
  60. AMFNodeElementBase *ne = new AMFColor(mNodeElement_Cur);
  61. AMFColor& als = *((AMFColor*)ne);// alias for convenience
  62. als.Profile = profile;
  63. if (!node.empty()) {
  64. bool read_flag[4] = { false, false, false, false };
  65. for (pugi::xml_node &child : node.children()) {
  66. if (child.name() == "r") {
  67. read_flag[0] = true;
  68. als.Color.r = atof(child.value());
  69. } else if (child.name() == "g") {
  70. read_flag[1] = true;
  71. als.Color.g = atof(child.value());
  72. } else if (child.name() == "b") {
  73. read_flag[2] = true;
  74. als.Color.b = atof(child.value());
  75. } else if (child.name() == "g") {
  76. read_flag[3] = true;
  77. als.Color.a = atof(child.value());
  78. }
  79. }
  80. // check that all components was defined
  81. if (!(read_flag[0] && read_flag[1] && read_flag[2])) {
  82. throw DeadlyImportError("Not all color components are defined.");
  83. }
  84. // check if <a> is absent. Then manually add "a == 1".
  85. if (!read_flag[3]) {
  86. als.Color.a = 1;
  87. }
  88. } else {
  89. mNodeElement_Cur->Child.push_back(ne);// Add element to child list of current element
  90. }
  91. als.Composed = false;
  92. mNodeElement_List.push_back(ne);// and to node element list because its a new object in graph.
  93. }
  94. // <material
  95. // id="" - A unique material id. material ID "0" is reserved to denote no material (void) or sacrificial material.
  96. // >
  97. // </material>
  98. // An available material.
  99. // Multi elements - Yes.
  100. // Parent element - <amf>.
  101. void AMFImporter::ParseNode_Material(XmlNode &node) {
  102. // create new object and assign read data
  103. std::string id = node.attribute("id").as_string();
  104. AMFNodeElementBase *ne = new AMFMaterial(mNodeElement_Cur);
  105. ((AMFMaterial*)ne)->ID = id;
  106. // Check for child nodes
  107. if (!node.empty()) {
  108. bool col_read = false;
  109. for (pugi::xml_node &child : node.children()) {
  110. if (child.name() == "color") {
  111. col_read = true;
  112. ParseNode_Color(child);
  113. } else if (child.name() == "metadata") {
  114. ParseNode_Metadata(child);
  115. }
  116. }
  117. } else {
  118. mNodeElement_Cur->Child.push_back(ne);// Add element to child list of current element
  119. }
  120. mNodeElement_List.push_back(ne);// and to node element list because its a new object in graph.
  121. }
  122. // <texture
  123. // id="" - Assigns a unique texture id for the new texture.
  124. // width="" - Width (horizontal size, x) of the texture, in pixels.
  125. // height="" - Height (lateral size, y) of the texture, in pixels.
  126. // depth="" - Depth (vertical size, z) of the texture, in pixels.
  127. // type="" - Encoding of the data in the texture. Currently allowed values are "grayscale" only. In grayscale mode, each pixel is represented by one byte
  128. // in the range of 0-255. When the texture is referenced using the tex function, these values are converted into a single floating point number in the
  129. // range of 0-1 (see Annex 2). A full color graphics will typically require three textures, one for each of the color channels. A graphic involving
  130. // transparency may require a fourth channel.
  131. // tiled="" - If true then texture repeated when UV-coordinates is greater than 1.
  132. // >
  133. // </triangle>
  134. // Specifies an texture data to be used as a map. Lists a sequence of Base64 values specifying values for pixels from left to right then top to bottom,
  135. // then layer by layer.
  136. // Multi elements - Yes.
  137. // Parent element - <amf>.
  138. void AMFImporter::ParseNode_Texture(XmlNode &node) {
  139. std::string id = node.attribute("id").as_string();
  140. uint32_t width = node.attribute("width").as_uint();
  141. uint32_t height = node.attribute("height").as_uint();
  142. uint32_t depth = node.attribute("depth").as_uint();
  143. std::string type = node.attribute("type").as_string();
  144. bool tiled = node.attribute("tiled").as_bool();
  145. // create new texture object.
  146. AMFNodeElementBase *ne = new AMFTexture(mNodeElement_Cur);
  147. AMFTexture& als = *((AMFTexture*)ne);// alias for convenience
  148. if (node.empty()) {
  149. return;
  150. }
  151. std::string enc64_data = node.value();
  152. // Check for child nodes
  153. //if (!mXmlParser->isEmptyElement()) {
  154. // XML_ReadNode_GetVal_AsString(enc64_data);
  155. //}
  156. // check that all components was defined
  157. if (id.empty()) {
  158. throw DeadlyImportError("ID for texture must be defined.");
  159. }
  160. if (width < 1) {
  161. throw DeadlyImportError("INvalid width for texture.");
  162. }
  163. if (height < 1) {
  164. throw DeadlyImportError("Invalid height for texture.");
  165. }
  166. if (depth < 1) {
  167. throw DeadlyImportError("Invalid depth for texture.");
  168. }
  169. if (type != "grayscale") {
  170. throw DeadlyImportError("Invalid type for texture.");
  171. }
  172. if (enc64_data.empty()) {
  173. throw DeadlyImportError("Texture data not defined.");
  174. }
  175. // copy data
  176. als.ID = id;
  177. als.Width = width;
  178. als.Height = height;
  179. als.Depth = depth;
  180. als.Tiled = tiled;
  181. ParseHelper_Decode_Base64(enc64_data, als.Data);
  182. // check data size
  183. if ((width * height * depth) != als.Data.size()) {
  184. throw DeadlyImportError("Texture has incorrect data size.");
  185. }
  186. mNodeElement_Cur->Child.push_back(ne);// Add element to child list of current element
  187. mNodeElement_List.push_back(ne);// and to node element list because its a new object in graph.
  188. }
  189. // <texmap
  190. // rtexid="" - Texture ID for red color component.
  191. // gtexid="" - Texture ID for green color component.
  192. // btexid="" - Texture ID for blue color component.
  193. // atexid="" - Texture ID for alpha color component. Optional.
  194. // >
  195. // </texmap>, old name: <map>
  196. // Specifies texture coordinates for triangle.
  197. // Multi elements - No.
  198. // Parent element - <triangle>.
  199. // Children elements:
  200. // <utex1>, <utex2>, <utex3>, <vtex1>, <vtex2>, <vtex3>. Old name: <u1>, <u2>, <u3>, <v1>, <v2>, <v3>.
  201. // Multi elements - No.
  202. // Texture coordinates for every vertex of triangle.
  203. void AMFImporter::ParseNode_TexMap(XmlNode &node, const bool pUseOldName) {
  204. // Read attributes for node <color>.
  205. std::string rtexid = node.attribute("rtexid").as_string();
  206. std::string gtexid = node.attribute("gtexid").as_string();
  207. std::string btexid = node.attribute("btexid").as_string();
  208. std::string atexid = node.attribute("atexid").as_string();
  209. // create new texture coordinates object, alias for convenience
  210. AMFNodeElementBase *ne = new AMFTexMap(mNodeElement_Cur);
  211. AMFTexMap& als = *((AMFTexMap*)ne);//
  212. // check data
  213. if (rtexid.empty() && gtexid.empty() && btexid.empty()) {
  214. throw DeadlyImportError("ParseNode_TexMap. At least one texture ID must be defined.");
  215. }
  216. // Check for children nodes
  217. //XML_CheckNode_MustHaveChildren();
  218. if (node.children().begin() == node.children().end()) {
  219. throw DeadlyImportError("Invalid children definition.");
  220. }
  221. // read children nodes
  222. bool read_flag[6] = { false, false, false, false, false, false };
  223. if (!pUseOldName) {
  224. for (pugi::xml_attribute &attr : node.attributes()) {
  225. if (attr.name() == "utex1") {
  226. read_flag[0] = true;
  227. als.TextureCoordinate[0].x = attr.as_float();
  228. } else if (attr.name() == "utex2") {
  229. read_flag[1] = true;
  230. als.TextureCoordinate[1].x = attr.as_float();
  231. } else if (attr.name() == "utex3") {
  232. read_flag[2] = true;
  233. als.TextureCoordinate[2].x = attr.as_float();
  234. } else if (attr.name() == "vtex1") {
  235. read_flag[3] = true;
  236. als.TextureCoordinate[0].y = attr.as_float();
  237. } else if (attr.name() == "vtex2") {
  238. read_flag[4] = true;
  239. als.TextureCoordinate[1].y = attr.as_float();
  240. } else if (attr.name() == "vtex3") {
  241. read_flag[5] = true;
  242. als.TextureCoordinate[0].y = attr.as_float();
  243. }
  244. }
  245. } else {
  246. for (pugi::xml_attribute &attr : node.attributes()) {
  247. if (attr.name() == "u") {
  248. read_flag[0] = true;
  249. als.TextureCoordinate[0].x = attr.as_float();
  250. } else if (attr.name() == "u2") {
  251. read_flag[1] = true;
  252. als.TextureCoordinate[1].x = attr.as_float();
  253. } else if (attr.name() == "u3") {
  254. read_flag[2] = true;
  255. als.TextureCoordinate[2].x = attr.as_float();
  256. } else if (attr.name() == "v1") {
  257. read_flag[3] = true;
  258. als.TextureCoordinate[0].y = attr.as_float();
  259. } else if (attr.name() == "v2") {
  260. read_flag[4] = true;
  261. als.TextureCoordinate[1].y = attr.as_float();
  262. } else if (attr.name() == "v3") {
  263. read_flag[5] = true;
  264. als.TextureCoordinate[0].y = attr.as_float();
  265. }
  266. }
  267. }
  268. // check that all components was defined
  269. if (!(read_flag[0] && read_flag[1] && read_flag[2] && read_flag[3] && read_flag[4] && read_flag[5])) {
  270. throw DeadlyImportError("Not all texture coordinates are defined.");
  271. }
  272. // copy attributes data
  273. als.TextureID_R = rtexid;
  274. als.TextureID_G = gtexid;
  275. als.TextureID_B = btexid;
  276. als.TextureID_A = atexid;
  277. mNodeElement_List.push_back(ne);
  278. }
  279. }// namespace Assimp
  280. #endif // !ASSIMP_BUILD_NO_AMF_IMPORTER