AMFImporter_Material.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2016, 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()
  58. {
  59. std::string profile;
  60. CAMFImporter_NodeElement* ne;
  61. // Read attributes for node <color>.
  62. MACRO_ATTRREAD_LOOPBEG;
  63. MACRO_ATTRREAD_CHECK_RET("profile", profile, mReader->getAttributeValue);
  64. MACRO_ATTRREAD_LOOPEND;
  65. // create new color object.
  66. ne = new CAMFImporter_NodeElement_Color(mNodeElement_Cur);
  67. CAMFImporter_NodeElement_Color& als = *((CAMFImporter_NodeElement_Color*)ne);// alias for convenience
  68. als.Profile = profile;
  69. // Check for child nodes
  70. if(!mReader->isEmptyElement())
  71. {
  72. bool read_flag[4] = { false, false, false, false };
  73. ParseHelper_Node_Enter(ne);
  74. MACRO_NODECHECK_LOOPBEGIN("color");
  75. MACRO_NODECHECK_READCOMP_F("r", read_flag[0], als.Color.r);
  76. MACRO_NODECHECK_READCOMP_F("g", read_flag[1], als.Color.g);
  77. MACRO_NODECHECK_READCOMP_F("b", read_flag[2], als.Color.b);
  78. MACRO_NODECHECK_READCOMP_F("a", read_flag[3], als.Color.a);
  79. MACRO_NODECHECK_LOOPEND("color");
  80. ParseHelper_Node_Exit();
  81. // check that all components was defined
  82. if(!(read_flag[0] && read_flag[1] && read_flag[2])) throw DeadlyImportError("Not all color components are defined.");
  83. // check if <a> is absent. Then manualy add "a == 1".
  84. if(!read_flag[3]) als.Color.a = 1;
  85. }// if(!mReader->isEmptyElement())
  86. else
  87. {
  88. mNodeElement_Cur->Child.push_back(ne);// Add element to child list of current element
  89. }// if(!mReader->isEmptyElement()) else
  90. als.Composed = false;
  91. mNodeElement_List.push_back(ne);// and to node element list because its a new object in graph.
  92. }
  93. // <material
  94. // id="" - A unique material id. material ID "0" is reserved to denote no material (void) or sacrificial material.
  95. // >
  96. // </material>
  97. // An available material.
  98. // Multi elements - Yes.
  99. // Parent element - <amf>.
  100. void AMFImporter::ParseNode_Material()
  101. {
  102. std::string id;
  103. CAMFImporter_NodeElement* ne;
  104. // Read attributes for node <color>.
  105. MACRO_ATTRREAD_LOOPBEG;
  106. MACRO_ATTRREAD_CHECK_RET("id", id, mReader->getAttributeValue);
  107. MACRO_ATTRREAD_LOOPEND;
  108. // create new object.
  109. ne = new CAMFImporter_NodeElement_Material(mNodeElement_Cur);
  110. // and assign read data
  111. ((CAMFImporter_NodeElement_Material*)ne)->ID = id;
  112. // Check for child nodes
  113. if(!mReader->isEmptyElement())
  114. {
  115. bool col_read = false;
  116. ParseHelper_Node_Enter(ne);
  117. MACRO_NODECHECK_LOOPBEGIN("material");
  118. if(XML_CheckNode_NameEqual("color"))
  119. {
  120. // Check if data already defined.
  121. if(col_read) Throw_MoreThanOnceDefined("color", "Only one color can be defined for <material>.");
  122. // read data and set flag about it
  123. ParseNode_Color();
  124. col_read = true;
  125. continue;
  126. }
  127. if(XML_CheckNode_NameEqual("metadata")) { ParseNode_Metadata(); continue; }
  128. MACRO_NODECHECK_LOOPEND("material");
  129. ParseHelper_Node_Exit();
  130. }// if(!mReader->isEmptyElement())
  131. else
  132. {
  133. mNodeElement_Cur->Child.push_back(ne);// Add element to child list of current element
  134. }// if(!mReader->isEmptyElement()) else
  135. mNodeElement_List.push_back(ne);// and to node element list because its a new object in graph.
  136. }
  137. // <texture
  138. // id="" - Assigns a unique texture id for the new texture.
  139. // width="" - Width (horizontal size, x) of the texture, in pixels.
  140. // height="" - Height (lateral size, y) of the texture, in pixels.
  141. // depth="" - Depth (vertical size, z) of the texture, in pixels.
  142. // type="" - Encoding of the data in the texture. Currently allowed values are "grayscale" only. In grayscale mode, each pixel is represented by one byte
  143. // 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
  144. // 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
  145. // transparency may require a fourth channel.
  146. // tiled="" - If true then texture repeated when UV-coordinates is greater than 1.
  147. // >
  148. // </triangle>
  149. // 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,
  150. // then layer by layer.
  151. // Multi elements - Yes.
  152. // Parent element - <amf>.
  153. void AMFImporter::ParseNode_Texture()
  154. {
  155. std::string id;
  156. uint32_t width = 0;
  157. uint32_t height = 0;
  158. uint32_t depth = 1;
  159. std::string type;
  160. bool tiled = false;
  161. std::string enc64_data;
  162. CAMFImporter_NodeElement* ne;
  163. // Read attributes for node <color>.
  164. MACRO_ATTRREAD_LOOPBEG;
  165. MACRO_ATTRREAD_CHECK_RET("id", id, mReader->getAttributeValue);
  166. MACRO_ATTRREAD_CHECK_RET("width", width, XML_ReadNode_GetAttrVal_AsU32);
  167. MACRO_ATTRREAD_CHECK_RET("height", height, XML_ReadNode_GetAttrVal_AsU32);
  168. MACRO_ATTRREAD_CHECK_RET("depth", depth, XML_ReadNode_GetAttrVal_AsU32);
  169. MACRO_ATTRREAD_CHECK_RET("type", type, mReader->getAttributeValue);
  170. MACRO_ATTRREAD_CHECK_RET("tiled", tiled, XML_ReadNode_GetAttrVal_AsBool);
  171. MACRO_ATTRREAD_LOOPEND;
  172. // create new texture object.
  173. ne = new CAMFImporter_NodeElement_Texture(mNodeElement_Cur);
  174. CAMFImporter_NodeElement_Texture& als = *((CAMFImporter_NodeElement_Texture*)ne);// alias for convenience
  175. // Check for child nodes
  176. if(!mReader->isEmptyElement()) XML_ReadNode_GetVal_AsString(enc64_data);
  177. // check that all components was defined
  178. if(id.empty()) throw DeadlyImportError("ID for texture must be defined.");
  179. if(width < 1) Throw_IncorrectAttrValue("width");
  180. if(height < 1) Throw_IncorrectAttrValue("height");
  181. if(depth < 1) Throw_IncorrectAttrValue("depth");
  182. if(type != "grayscale") Throw_IncorrectAttrValue("type");
  183. if(enc64_data.empty()) throw DeadlyImportError("Texture data not defined.");
  184. // copy data
  185. als.ID = id;
  186. als.Width = width;
  187. als.Height = height;
  188. als.Depth = depth;
  189. als.Tiled = tiled;
  190. ParseHelper_Decode_Base64(enc64_data, als.Data);
  191. // check data size
  192. if((width * height * depth) != als.Data.size()) throw DeadlyImportError("Texture has incorrect data size.");
  193. mNodeElement_Cur->Child.push_back(ne);// Add element to child list of current element
  194. mNodeElement_List.push_back(ne);// and to node element list because its a new object in graph.
  195. }
  196. // <texmap
  197. // rtexid="" - Texture ID for red color component.
  198. // gtexid="" - Texture ID for green color component.
  199. // btexid="" - Texture ID for blue color component.
  200. // atexid="" - Texture ID for alpha color component. Optional.
  201. // >
  202. // </texmap>, old name: <map>
  203. // Specifies texture coordinates for triangle.
  204. // Multi elements - No.
  205. // Parent element - <triangle>.
  206. // Children elements:
  207. // <utex1>, <utex2>, <utex3>, <vtex1>, <vtex2>, <vtex3>. Old name: <u1>, <u2>, <u3>, <v1>, <v2>, <v3>.
  208. // Multi elements - No.
  209. // Texture coordinates for every vertex of triangle.
  210. void AMFImporter::ParseNode_TexMap(const bool pUseOldName)
  211. {
  212. std::string rtexid, gtexid, btexid, atexid;
  213. CAMFImporter_NodeElement* ne;
  214. // Read attributes for node <color>.
  215. MACRO_ATTRREAD_LOOPBEG;
  216. MACRO_ATTRREAD_CHECK_RET("rtexid", rtexid, mReader->getAttributeValue);
  217. MACRO_ATTRREAD_CHECK_RET("gtexid", gtexid, mReader->getAttributeValue);
  218. MACRO_ATTRREAD_CHECK_RET("btexid", btexid, mReader->getAttributeValue);
  219. MACRO_ATTRREAD_CHECK_RET("atexid", atexid, mReader->getAttributeValue);
  220. MACRO_ATTRREAD_LOOPEND;
  221. // create new texture coordinates object.
  222. ne = new CAMFImporter_NodeElement_TexMap(mNodeElement_Cur);
  223. CAMFImporter_NodeElement_TexMap& als = *((CAMFImporter_NodeElement_TexMap*)ne);// alias for convenience
  224. // check data
  225. if(rtexid.empty() && gtexid.empty() && btexid.empty()) throw DeadlyImportError("ParseNode_TexMap. At least one texture ID must be defined.");
  226. // Check for children nodes
  227. XML_CheckNode_MustHaveChildren();
  228. // read children nodes
  229. bool read_flag[6] = { false, false, false, false, false, false };
  230. ParseHelper_Node_Enter(ne);
  231. if(!pUseOldName)
  232. {
  233. MACRO_NODECHECK_LOOPBEGIN("texmap");
  234. MACRO_NODECHECK_READCOMP_F("utex1", read_flag[0], als.TextureCoordinate[0].x);
  235. MACRO_NODECHECK_READCOMP_F("utex2", read_flag[1], als.TextureCoordinate[1].x);
  236. MACRO_NODECHECK_READCOMP_F("utex3", read_flag[2], als.TextureCoordinate[2].x);
  237. MACRO_NODECHECK_READCOMP_F("vtex1", read_flag[3], als.TextureCoordinate[0].y);
  238. MACRO_NODECHECK_READCOMP_F("vtex2", read_flag[4], als.TextureCoordinate[1].y);
  239. MACRO_NODECHECK_READCOMP_F("vtex3", read_flag[5], als.TextureCoordinate[2].y);
  240. MACRO_NODECHECK_LOOPEND("texmap");
  241. }
  242. else
  243. {
  244. MACRO_NODECHECK_LOOPBEGIN("map");
  245. MACRO_NODECHECK_READCOMP_F("u1", read_flag[0], als.TextureCoordinate[0].x);
  246. MACRO_NODECHECK_READCOMP_F("u2", read_flag[1], als.TextureCoordinate[1].x);
  247. MACRO_NODECHECK_READCOMP_F("u3", read_flag[2], als.TextureCoordinate[2].x);
  248. MACRO_NODECHECK_READCOMP_F("v1", read_flag[3], als.TextureCoordinate[0].y);
  249. MACRO_NODECHECK_READCOMP_F("v2", read_flag[4], als.TextureCoordinate[1].y);
  250. MACRO_NODECHECK_READCOMP_F("v3", read_flag[5], als.TextureCoordinate[2].y);
  251. MACRO_NODECHECK_LOOPEND("map");
  252. }// if(!pUseOldName) else
  253. ParseHelper_Node_Exit();
  254. // check that all components was defined
  255. if(!(read_flag[0] && read_flag[1] && read_flag[2] && read_flag[3] && read_flag[4] && read_flag[5]))
  256. throw DeadlyImportError("Not all texture coordinates are defined.");
  257. // copy attributes data
  258. als.TextureID_R = rtexid;
  259. als.TextureID_G = gtexid;
  260. als.TextureID_B = btexid;
  261. als.TextureID_A = atexid;
  262. mNodeElement_List.push_back(ne);// add to node element list because its a new object in graph.
  263. }
  264. }// namespace Assimp
  265. #endif // !ASSIMP_BUILD_NO_AMF_IMPORTER