Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

260 řádky
7.4 KiB

  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, 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 FBXMaterial.cpp
  34. * @brief Assimp::FBX::Material and Assimp::FBX::Texture implementation
  35. */
  36. #include "AssimpPCH.h"
  37. #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
  38. #include "FBXParser.h"
  39. #include "FBXDocument.h"
  40. #include "FBXImporter.h"
  41. #include "FBXImportSettings.h"
  42. #include "FBXDocumentUtil.h"
  43. #include "FBXProperties.h"
  44. namespace Assimp {
  45. namespace FBX {
  46. using namespace Util;
  47. // ------------------------------------------------------------------------------------------------
  48. Material::Material(uint64_t id, const Element& element, const Document& doc, const std::string& name)
  49. : Object(id,element,name)
  50. {
  51. const Scope& sc = GetRequiredScope(element);
  52. const Element* const ShadingModel = sc["ShadingModel"];
  53. const Element* const MultiLayer = sc["MultiLayer"];
  54. if(MultiLayer) {
  55. multilayer = !!ParseTokenAsInt(GetRequiredToken(*MultiLayer,0));
  56. }
  57. if(ShadingModel) {
  58. shading = ParseTokenAsString(GetRequiredToken(*ShadingModel,0));
  59. }
  60. else {
  61. DOMWarning("shading mode not specified, assuming phong",&element);
  62. shading = "phong";
  63. }
  64. std::string templateName;
  65. const char* const sh = shading.c_str();
  66. if(!strcmp(sh,"phong")) {
  67. templateName = "Material.FbxSurfacePhong";
  68. }
  69. else if(!strcmp(sh,"lambert")) {
  70. templateName = "Material.FbxSurfaceLambert";
  71. }
  72. else {
  73. DOMWarning("shading mode not recognized: " + shading,&element);
  74. }
  75. props = GetPropertyTable(doc,templateName,element,sc);
  76. // resolve texture links
  77. const std::vector<const Connection*>& conns = doc.GetConnectionsByDestinationSequenced(ID());
  78. BOOST_FOREACH(const Connection* con, conns) {
  79. // texture link to properties, not objects
  80. if (!con->PropertyName().length()) {
  81. continue;
  82. }
  83. const Object* const ob = con->SourceObject();
  84. if(!ob) {
  85. DOMWarning("failed to read source object for texture link, ignoring",&element);
  86. continue;
  87. }
  88. const Texture* const tex = dynamic_cast<const Texture*>(ob);
  89. if(!tex) {
  90. const LayeredTexture* const layeredTexture = dynamic_cast<const LayeredTexture*>(ob);
  91. if(!layeredTexture) {
  92. DOMWarning("source object for texture link is not a texture or layered texture, ignoring",&element);
  93. continue;
  94. }
  95. const std::string& prop = con->PropertyName();
  96. if (layeredTextures.find(prop) != layeredTextures.end()) {
  97. DOMWarning("duplicate layered texture link: " + prop,&element);
  98. }
  99. layeredTextures[prop] = layeredTexture;
  100. ((LayeredTexture*)layeredTexture)->fillTexture(doc);
  101. }
  102. else
  103. {
  104. const std::string& prop = con->PropertyName();
  105. if (textures.find(prop) != textures.end()) {
  106. DOMWarning("duplicate texture link: " + prop,&element);
  107. }
  108. textures[prop] = tex;
  109. }
  110. }
  111. }
  112. // ------------------------------------------------------------------------------------------------
  113. Material::~Material()
  114. {
  115. }
  116. // ------------------------------------------------------------------------------------------------
  117. Texture::Texture(uint64_t id, const Element& element, const Document& doc, const std::string& name)
  118. : Object(id,element,name)
  119. , uvScaling(1.0f,1.0f)
  120. {
  121. const Scope& sc = GetRequiredScope(element);
  122. const Element* const Type = sc["Type"];
  123. const Element* const FileName = sc["FileName"];
  124. const Element* const RelativeFilename = sc["RelativeFilename"];
  125. const Element* const ModelUVTranslation = sc["ModelUVTranslation"];
  126. const Element* const ModelUVScaling = sc["ModelUVScaling"];
  127. const Element* const Texture_Alpha_Source = sc["Texture_Alpha_Source"];
  128. const Element* const Cropping = sc["Cropping"];
  129. if(Type) {
  130. type = ParseTokenAsString(GetRequiredToken(*Type,0));
  131. }
  132. if(FileName) {
  133. fileName = ParseTokenAsString(GetRequiredToken(*FileName,0));
  134. }
  135. if(RelativeFilename) {
  136. relativeFileName = ParseTokenAsString(GetRequiredToken(*RelativeFilename,0));
  137. }
  138. if(ModelUVTranslation) {
  139. uvTrans = aiVector2D(ParseTokenAsFloat(GetRequiredToken(*ModelUVTranslation,0)),
  140. ParseTokenAsFloat(GetRequiredToken(*ModelUVTranslation,1))
  141. );
  142. }
  143. if(ModelUVScaling) {
  144. uvScaling = aiVector2D(ParseTokenAsFloat(GetRequiredToken(*ModelUVScaling,0)),
  145. ParseTokenAsFloat(GetRequiredToken(*ModelUVScaling,1))
  146. );
  147. }
  148. if(Cropping) {
  149. crop[0] = ParseTokenAsInt(GetRequiredToken(*Cropping,0));
  150. crop[1] = ParseTokenAsInt(GetRequiredToken(*Cropping,1));
  151. crop[2] = ParseTokenAsInt(GetRequiredToken(*Cropping,2));
  152. crop[3] = ParseTokenAsInt(GetRequiredToken(*Cropping,3));
  153. }
  154. else {
  155. // vc8 doesn't support the crop() syntax in initialization lists
  156. // (and vc9 WARNS about the new (i.e. compliant) behaviour).
  157. crop[0] = crop[1] = crop[2] = crop[3] = 0;
  158. }
  159. if(Texture_Alpha_Source) {
  160. alphaSource = ParseTokenAsString(GetRequiredToken(*Texture_Alpha_Source,0));
  161. }
  162. props = GetPropertyTable(doc,"Texture.FbxFileTexture",element,sc);
  163. }
  164. Texture::~Texture()
  165. {
  166. }
  167. LayeredTexture::LayeredTexture(uint64_t id, const Element& element, const Document& doc, const std::string& name)
  168. : Object(id,element,name)
  169. ,texture(0)
  170. ,blendMode(BlendMode_Modulate)
  171. ,alpha(1)
  172. {
  173. const Scope& sc = GetRequiredScope(element);
  174. const Element* const BlendModes = sc["BlendModes"];
  175. const Element* const Alphas = sc["Alphas"];
  176. if(BlendModes!=0)
  177. {
  178. blendMode = (BlendMode)ParseTokenAsInt(GetRequiredToken(*BlendModes,0));
  179. }
  180. if(Alphas!=0)
  181. {
  182. alpha = ParseTokenAsFloat(GetRequiredToken(*Alphas,0));
  183. }
  184. }
  185. LayeredTexture::~LayeredTexture()
  186. {
  187. }
  188. void LayeredTexture::fillTexture(const Document& doc)
  189. {
  190. const std::vector<const Connection*>& conns = doc.GetConnectionsByDestinationSequenced(ID());
  191. for(size_t i = 0; i < conns.size();++i)
  192. {
  193. const Connection* con = conns.at(i);
  194. const Object* const ob = con->SourceObject();
  195. if(!ob) {
  196. DOMWarning("failed to read source object for texture link, ignoring",&element);
  197. continue;
  198. }
  199. const Texture* const tex = dynamic_cast<const Texture*>(ob);
  200. texture = tex;
  201. }
  202. }
  203. } //!FBX
  204. } //!Assimp
  205. #endif