//----------------------------------------------------------------------------- // Copyright (c) 2012 GarageGames, LLC // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to // deal in the Software without restriction, including without limitation the // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or // sell copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS // IN THE SOFTWARE. //----------------------------------------------------------------------------- #ifndef _COLLADA_EXTENSIONS_H_ #define _COLLADA_EXTENSIONS_H_ #ifndef _TSSHAPE_LOADER_H_ #include "ts/loader/tsShapeLoader.h" #endif #ifndef _COLLADA_UTILS_H_ #include "ts/collada/colladaUtils.h" #endif //----------------------------------------------------------------------------- // Collada allows custom data to be included with many elements using the // tag, followed by one or more named technique profiles. eg. // // // // value0 // value1 // ... // // value0 // value1 // ... // // This class provides an easy way to read the custom parameters into a strongly // typed subclass. class ColladaExtension { // Helper macro to simplify getting named parameters #define GET_EXTRA_PARAM(param, defaultVal) \ get(#param, param, defaultVal) protected: const domTechnique* pTechnique; /// Find the technique with the named profile template const domTechnique* findExtraTechnique(const T* element, const char* name) const { if (element) { for (S32 iExt = 0; iExt < element->getExtra_array().getCount(); iExt++) { for (S32 iTech = 0; iTech < element->getExtra_array()[iExt]->getTechnique_array().getCount(); iTech++) { if (dStrEqual(element->getExtra_array()[iExt]->getTechnique_array()[iTech]->getProfile(), name)) return element->getExtra_array()[iExt]->getTechnique_array()[iTech]; } } } return NULL; } /// The element does not define an extra_array, so need a specialized /// version of the template const domTechnique* findExtraTechnique( const domCommon_color_or_texture_type_complexType::domTexture* element, const char* name) const { if (element && element->getExtra()) { for (S32 iTech = 0; iTech < element->getExtra()->getTechnique_array().getCount(); iTech++) { if (dStrEqual(element->getExtra()->getTechnique_array()[iTech]->getProfile(), name)) return element->getExtra()->getTechnique_array()[iTech]; } } return NULL; } /// Find the parameter with the given name const domAny* findParam(const char* name) { if (pTechnique) { // search the technique contents for the desired parameter for (S32 iParam = 0; iParam < pTechnique->getContents().getCount(); iParam++) { const domAny* param = daeSafeCast(pTechnique->getContents()[iParam]); if (param && !String::compare(param->getElementName(), name)) return param; } } return NULL; } /// Get the value of the named parameter (use defaultVal if parameter not found) template void get(const char* name, T& value, T defaultVal) { value = defaultVal; if (const domAny* param = findParam(name)) value = convert(param->getValue()); } /// Get the value of the named animated parameter (use defaultVal if parameter not found) template void get(const char* name, AnimatedElement& value, T defaultVal) { value.defaultVal = defaultVal; if (const domAny* param = findParam(name)) value.element = param; } public: ColladaExtension() : pTechnique(0) { } virtual ~ColladaExtension() { } }; /// Extensions for the element (and its children) class ColladaExtension_effect : public ColladaExtension { // Cached texture transform F32 lastAnimTime; MatrixF textureTransform; public: //---------------------------------- // // MAX3D profile elements bool double_sided; //---------------------------------- // . // GOOGLEEARTH profile elements //bool double_sided; //---------------------------------- // ..... // MAYA profile elements bool wrapU, wrapV; bool mirrorU, mirrorV; AnimatedFloat coverageU, coverageV; AnimatedFloat translateFrameU, translateFrameV; AnimatedFloat rotateFrame; AnimatedBool stagger; // @todo: not supported yet AnimatedFloat repeatU, repeatV; AnimatedFloat offsetU, offsetV; AnimatedFloat rotateUV; AnimatedFloat noiseU, noiseV; //---------------------------------- // .. // FCOLLADA profile elements domFx_sampler2D_common_complexType* bumpSampler; public: ColladaExtension_effect(const domEffect* effect) : lastAnimTime(TSShapeLoader::DefaultTime-1), textureTransform(true), bumpSampler(0) { //---------------------------------- // // MAX3D profile pTechnique = findExtraTechnique(effect, "MAX3D"); GET_EXTRA_PARAM(double_sided, false); //---------------------------------- // . const domProfile_COMMON* profileCommon = ColladaUtils::findEffectCommonProfile(effect); // GOOGLEEARTH profile (same double_sided element) pTechnique = findExtraTechnique(profileCommon, "GOOGLEEARTH"); GET_EXTRA_PARAM(double_sided, double_sided); //---------------------------------- // ..... const domCommon_color_or_texture_type_complexType* domDiffuse = ColladaUtils::findEffectDiffuse(effect); const domFx_sampler2D_common_complexType* sampler2D = ColladaUtils::getTextureSampler(effect, domDiffuse); // Use the sampler2D to set default values for wrap/mirror flags wrapU = wrapV = true; mirrorU = mirrorV = false; if (sampler2D) { domFx_sampler2D_common_complexType::domWrap_s* wrap_s = sampler2D->getWrap_s(); domFx_sampler2D_common_complexType::domWrap_t* wrap_t = sampler2D->getWrap_t(); mirrorU = (wrap_s && wrap_s->getValue() == FX_SAMPLER_WRAP_COMMON_MIRROR); wrapU = (mirrorU || !wrap_s || (wrap_s->getValue() == FX_SAMPLER_WRAP_COMMON_WRAP)); mirrorV = (wrap_t && wrap_t->getValue() == FX_SAMPLER_WRAP_COMMON_MIRROR); wrapV = (mirrorV || !wrap_t || (wrap_t->getValue() == FX_SAMPLER_WRAP_COMMON_WRAP)); } // MAYA profile pTechnique = findExtraTechnique(domDiffuse ? domDiffuse->getTexture() : 0, "MAYA"); GET_EXTRA_PARAM(wrapU, wrapU); GET_EXTRA_PARAM(wrapV, wrapV); GET_EXTRA_PARAM(mirrorU, mirrorU); GET_EXTRA_PARAM(mirrorV, mirrorV); GET_EXTRA_PARAM(coverageU, 1.0); GET_EXTRA_PARAM(coverageV, 1.0); GET_EXTRA_PARAM(translateFrameU, 0.0); GET_EXTRA_PARAM(translateFrameV, 0.0); GET_EXTRA_PARAM(rotateFrame, 0.0); GET_EXTRA_PARAM(stagger, false); GET_EXTRA_PARAM(repeatU, 1.0); GET_EXTRA_PARAM(repeatV, 1.0); GET_EXTRA_PARAM(offsetU, 0.0); GET_EXTRA_PARAM(offsetV, 0.0); GET_EXTRA_PARAM(rotateUV, 0.0); GET_EXTRA_PARAM(noiseU, 0.0); GET_EXTRA_PARAM(noiseV, 0.0); // FCOLLADA profile if (profileCommon) { pTechnique = findExtraTechnique((const domProfile_COMMON::domTechnique*)profileCommon->getTechnique(), "FCOLLADA"); if (pTechnique) { domAny* bump = daeSafeCast(const_cast(pTechnique)->getChild("bump")); if (bump) { domAny* bumpTexture = daeSafeCast(bump->getChild("texture")); if (bumpTexture) { daeSIDResolver resolver(const_cast(effect), bumpTexture->getAttribute("texture").c_str()); domCommon_newparam_type* param = daeSafeCast(resolver.getElement()); if (param) bumpSampler = param->getSampler2D(); } } } } } /// Check if any of the MAYA texture transform elements are animated within /// the interval bool animatesTextureTransform(F32 start, F32 end); /// Apply the MAYA texture transform to the given UV coordinates void applyTextureTransform(Point2F& uv, F32 time); }; /// Extensions for the element class ColladaExtension_node : public ColladaExtension { public: // FCOLLADA or OpenCOLLADA profile elements AnimatedFloat visibility; const char* user_properties; ColladaExtension_node(const domNode* node) { // FCOLLADA profile pTechnique = findExtraTechnique(node, "FCOLLADA"); GET_EXTRA_PARAM(visibility, 1.0); GET_EXTRA_PARAM(user_properties, ""); // OpenCOLLADA profile pTechnique = findExtraTechnique(node, "OpenCOLLADA"); if (!visibility.element) GET_EXTRA_PARAM(visibility, 1.0); GET_EXTRA_PARAM(user_properties, user_properties); } }; /// Extensions for the element class ColladaExtension_geometry : public ColladaExtension { public: // MAYA profile elements bool double_sided; ColladaExtension_geometry(const domGeometry* geometry) { // MAYA profile pTechnique = findExtraTechnique(geometry, "MAYA"); GET_EXTRA_PARAM(double_sided, false); } }; // Extensions for the element class ColladaExtension_animation_clip : public ColladaExtension { public: struct Trigger { F32 time; S32 state; }; // Torque profile elements (none of these are animatable) S32 num_triggers; Vector triggers; bool cyclic; bool blend; F32 blendReferenceTime; F32 priority; ColladaExtension_animation_clip(const domAnimation_clip* clip) { // Torque profile pTechnique = findExtraTechnique(clip, "Torque"); GET_EXTRA_PARAM(num_triggers, 0); for (S32 iTrigger = 0; iTrigger < num_triggers; iTrigger++) { triggers.increment(); get(avar("trigger_time%d", iTrigger), triggers.last().time, 0.0f); get(avar("trigger_state%d", iTrigger), triggers.last().state, 0); } GET_EXTRA_PARAM(cyclic, false); GET_EXTRA_PARAM(blend, false); GET_EXTRA_PARAM(blendReferenceTime, 0.0f); GET_EXTRA_PARAM(priority, 5.0f); } }; #endif // _COLLADA_EXTENSIONS_H_