colladaExtensions.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _COLLADA_EXTENSIONS_H_
  23. #define _COLLADA_EXTENSIONS_H_
  24. #ifndef _TSSHAPE_LOADER_H_
  25. #include "ts/loader/tsShapeLoader.h"
  26. #endif
  27. #ifndef _COLLADA_UTILS_H_
  28. #include "ts/collada/colladaUtils.h"
  29. #endif
  30. //-----------------------------------------------------------------------------
  31. // Collada allows custom data to be included with many elements using the <extra>
  32. // tag, followed by one or more named technique profiles. eg.
  33. // <some_element>
  34. // <extra>
  35. // <technique profile="SOME_PROFILE">
  36. // <custom_element0>value0</custom_element0>
  37. // <custom_element1>value1</custom_element1>
  38. // ...
  39. // <technique profile="ANOTHER_PROFILE">
  40. // <custom_element0>value0</custom_element0>
  41. // <custom_element1>value1</custom_element1>
  42. // ...
  43. //
  44. // This class provides an easy way to read the custom parameters into a strongly
  45. // typed subclass.
  46. class ColladaExtension
  47. {
  48. // Helper macro to simplify getting named parameters
  49. #define GET_EXTRA_PARAM(param, defaultVal) \
  50. get(#param, param, defaultVal)
  51. protected:
  52. const domTechnique* pTechnique;
  53. /// Find the technique with the named profile
  54. template<class T> const domTechnique* findExtraTechnique(const T* element, const char* name) const
  55. {
  56. if (element) {
  57. for (S32 iExt = 0; iExt < element->getExtra_array().getCount(); iExt++) {
  58. for (S32 iTech = 0; iTech < element->getExtra_array()[iExt]->getTechnique_array().getCount(); iTech++) {
  59. if (dStrEqual(element->getExtra_array()[iExt]->getTechnique_array()[iTech]->getProfile(), name))
  60. return element->getExtra_array()[iExt]->getTechnique_array()[iTech];
  61. }
  62. }
  63. }
  64. return NULL;
  65. }
  66. /// The <texture> element does not define an extra_array, so need a specialized
  67. /// version of the template
  68. const domTechnique* findExtraTechnique(
  69. const domCommon_color_or_texture_type_complexType::domTexture* element, const char* name) const
  70. {
  71. if (element && element->getExtra()) {
  72. for (S32 iTech = 0; iTech < element->getExtra()->getTechnique_array().getCount(); iTech++) {
  73. if (dStrEqual(element->getExtra()->getTechnique_array()[iTech]->getProfile(), name))
  74. return element->getExtra()->getTechnique_array()[iTech];
  75. }
  76. }
  77. return NULL;
  78. }
  79. /// Find the parameter with the given name
  80. const domAny* findParam(const char* name)
  81. {
  82. if (pTechnique) {
  83. // search the technique contents for the desired parameter
  84. for (S32 iParam = 0; iParam < pTechnique->getContents().getCount(); iParam++) {
  85. const domAny* param = daeSafeCast<domAny>(pTechnique->getContents()[iParam]);
  86. if (param && !String::compare(param->getElementName(), name))
  87. return param;
  88. }
  89. }
  90. return NULL;
  91. }
  92. /// Get the value of the named parameter (use defaultVal if parameter not found)
  93. template<typename T> void get(const char* name, T& value, T defaultVal)
  94. {
  95. value = defaultVal;
  96. if (const domAny* param = findParam(name))
  97. value = convert<T>(param->getValue());
  98. }
  99. /// Get the value of the named animated parameter (use defaultVal if parameter not found)
  100. template<typename T> void get(const char* name, AnimatedElement<T>& value, T defaultVal)
  101. {
  102. value.defaultVal = defaultVal;
  103. if (const domAny* param = findParam(name))
  104. value.element = param;
  105. }
  106. public:
  107. ColladaExtension() : pTechnique(0) { }
  108. virtual ~ColladaExtension() { }
  109. };
  110. /// Extensions for the <effect> element (and its children)
  111. class ColladaExtension_effect : public ColladaExtension
  112. {
  113. // Cached texture transform
  114. F32 lastAnimTime;
  115. MatrixF textureTransform;
  116. public:
  117. //----------------------------------
  118. // <effect>
  119. // MAX3D profile elements
  120. bool double_sided;
  121. //----------------------------------
  122. // <effect>.<profile_COMMON>
  123. // GOOGLEEARTH profile elements
  124. //bool double_sided;
  125. //----------------------------------
  126. // <effect>.<profile_COMMON>.<technique>.<blinn/phong/lambert>.<diffuse>.<texture>
  127. // MAYA profile elements
  128. bool wrapU, wrapV;
  129. bool mirrorU, mirrorV;
  130. AnimatedFloat coverageU, coverageV;
  131. AnimatedFloat translateFrameU, translateFrameV;
  132. AnimatedFloat rotateFrame;
  133. AnimatedBool stagger; // @todo: not supported yet
  134. AnimatedFloat repeatU, repeatV;
  135. AnimatedFloat offsetU, offsetV;
  136. AnimatedFloat rotateUV;
  137. AnimatedFloat noiseU, noiseV;
  138. //----------------------------------
  139. // <effect>.<profile_COMMON>.<technique>
  140. // FCOLLADA profile elements
  141. domFx_sampler2D_common_complexType* bumpSampler;
  142. public:
  143. ColladaExtension_effect(const domEffect* effect)
  144. : lastAnimTime(TSShapeLoader::DefaultTime-1), textureTransform(true), bumpSampler(0)
  145. {
  146. //----------------------------------
  147. // <effect>
  148. // MAX3D profile
  149. pTechnique = findExtraTechnique(effect, "MAX3D");
  150. GET_EXTRA_PARAM(double_sided, false);
  151. //----------------------------------
  152. // <effect>.<profile_COMMON>
  153. const domProfile_COMMON* profileCommon = ColladaUtils::findEffectCommonProfile(effect);
  154. // GOOGLEEARTH profile (same double_sided element)
  155. pTechnique = findExtraTechnique(profileCommon, "GOOGLEEARTH");
  156. GET_EXTRA_PARAM(double_sided, double_sided);
  157. //----------------------------------
  158. // <effect>.<profile_COMMON>.<technique>.<blinn/phong/lambert>.<diffuse>.<texture>
  159. const domCommon_color_or_texture_type_complexType* domDiffuse = ColladaUtils::findEffectDiffuse(effect);
  160. const domFx_sampler2D_common_complexType* sampler2D = ColladaUtils::getTextureSampler(effect, domDiffuse);
  161. // Use the sampler2D to set default values for wrap/mirror flags
  162. wrapU = wrapV = true;
  163. mirrorU = mirrorV = false;
  164. if (sampler2D) {
  165. domFx_sampler2D_common_complexType::domWrap_s* wrap_s = sampler2D->getWrap_s();
  166. domFx_sampler2D_common_complexType::domWrap_t* wrap_t = sampler2D->getWrap_t();
  167. mirrorU = (wrap_s && wrap_s->getValue() == FX_SAMPLER_WRAP_COMMON_MIRROR);
  168. wrapU = (mirrorU || !wrap_s || (wrap_s->getValue() == FX_SAMPLER_WRAP_COMMON_WRAP));
  169. mirrorV = (wrap_t && wrap_t->getValue() == FX_SAMPLER_WRAP_COMMON_MIRROR);
  170. wrapV = (mirrorV || !wrap_t || (wrap_t->getValue() == FX_SAMPLER_WRAP_COMMON_WRAP));
  171. }
  172. // MAYA profile
  173. pTechnique = findExtraTechnique(domDiffuse ? domDiffuse->getTexture() : 0, "MAYA");
  174. GET_EXTRA_PARAM(wrapU, wrapU); GET_EXTRA_PARAM(wrapV, wrapV);
  175. GET_EXTRA_PARAM(mirrorU, mirrorU); GET_EXTRA_PARAM(mirrorV, mirrorV);
  176. GET_EXTRA_PARAM(coverageU, 1.0); GET_EXTRA_PARAM(coverageV, 1.0);
  177. GET_EXTRA_PARAM(translateFrameU, 0.0); GET_EXTRA_PARAM(translateFrameV, 0.0);
  178. GET_EXTRA_PARAM(rotateFrame, 0.0);
  179. GET_EXTRA_PARAM(stagger, false);
  180. GET_EXTRA_PARAM(repeatU, 1.0); GET_EXTRA_PARAM(repeatV, 1.0);
  181. GET_EXTRA_PARAM(offsetU, 0.0); GET_EXTRA_PARAM(offsetV, 0.0);
  182. GET_EXTRA_PARAM(rotateUV, 0.0);
  183. GET_EXTRA_PARAM(noiseU, 0.0); GET_EXTRA_PARAM(noiseV, 0.0);
  184. // FCOLLADA profile
  185. if (profileCommon) {
  186. pTechnique = findExtraTechnique((const domProfile_COMMON::domTechnique*)profileCommon->getTechnique(), "FCOLLADA");
  187. if (pTechnique) {
  188. domAny* bump = daeSafeCast<domAny>(const_cast<domTechnique*>(pTechnique)->getChild("bump"));
  189. if (bump) {
  190. domAny* bumpTexture = daeSafeCast<domAny>(bump->getChild("texture"));
  191. if (bumpTexture) {
  192. daeSIDResolver resolver(const_cast<domEffect*>(effect), bumpTexture->getAttribute("texture").c_str());
  193. domCommon_newparam_type* param = daeSafeCast<domCommon_newparam_type>(resolver.getElement());
  194. if (param)
  195. bumpSampler = param->getSampler2D();
  196. }
  197. }
  198. }
  199. }
  200. }
  201. /// Check if any of the MAYA texture transform elements are animated within
  202. /// the interval
  203. bool animatesTextureTransform(F32 start, F32 end);
  204. /// Apply the MAYA texture transform to the given UV coordinates
  205. void applyTextureTransform(Point2F& uv, F32 time);
  206. };
  207. /// Extensions for the <node> element
  208. class ColladaExtension_node : public ColladaExtension
  209. {
  210. public:
  211. // FCOLLADA or OpenCOLLADA profile elements
  212. AnimatedFloat visibility;
  213. const char* user_properties;
  214. ColladaExtension_node(const domNode* node)
  215. {
  216. // FCOLLADA profile
  217. pTechnique = findExtraTechnique(node, "FCOLLADA");
  218. GET_EXTRA_PARAM(visibility, 1.0);
  219. GET_EXTRA_PARAM(user_properties, "");
  220. // OpenCOLLADA profile
  221. pTechnique = findExtraTechnique(node, "OpenCOLLADA");
  222. if (!visibility.element)
  223. GET_EXTRA_PARAM(visibility, 1.0);
  224. GET_EXTRA_PARAM(user_properties, user_properties);
  225. }
  226. };
  227. /// Extensions for the <geometry> element
  228. class ColladaExtension_geometry : public ColladaExtension
  229. {
  230. public:
  231. // MAYA profile elements
  232. bool double_sided;
  233. ColladaExtension_geometry(const domGeometry* geometry)
  234. {
  235. // MAYA profile
  236. pTechnique = findExtraTechnique(geometry, "MAYA");
  237. GET_EXTRA_PARAM(double_sided, false);
  238. }
  239. };
  240. // Extensions for the <animation_clip> element
  241. class ColladaExtension_animation_clip : public ColladaExtension
  242. {
  243. public:
  244. struct Trigger {
  245. F32 time;
  246. S32 state;
  247. };
  248. // Torque profile elements (none of these are animatable)
  249. S32 num_triggers;
  250. Vector<Trigger> triggers;
  251. bool cyclic;
  252. bool blend;
  253. F32 blendReferenceTime;
  254. F32 priority;
  255. ColladaExtension_animation_clip(const domAnimation_clip* clip)
  256. {
  257. // Torque profile
  258. pTechnique = findExtraTechnique(clip, "Torque");
  259. GET_EXTRA_PARAM(num_triggers, 0);
  260. for (S32 iTrigger = 0; iTrigger < num_triggers; iTrigger++) {
  261. triggers.increment();
  262. get(avar("trigger_time%d", iTrigger), triggers.last().time, 0.0f);
  263. get(avar("trigger_state%d", iTrigger), triggers.last().state, 0);
  264. }
  265. GET_EXTRA_PARAM(cyclic, false);
  266. GET_EXTRA_PARAM(blend, false);
  267. GET_EXTRA_PARAM(blendReferenceTime, 0.0f);
  268. GET_EXTRA_PARAM(priority, 5.0f);
  269. }
  270. };
  271. #endif // _COLLADA_EXTENSIONS_H_