1 /* 2 Copyright (c) 2008-2009 NetAllied Systems GmbH 3 4 This file is part of COLLADASaxFrameworkLoader. 5 6 Licensed under the MIT Open Source License, 7 for details please see LICENSE file or the website 8 http://www.opensource.org/licenses/mit-license.php 9 */ 10 11 #ifndef __COLLADASAXFWL_LIBRARYEFFECTSSLOADER_H__ 12 #define __COLLADASAXFWL_LIBRARYEFFECTSSLOADER_H__ 13 14 #include "COLLADASaxFWLLibraryImagesLoader.h" 15 16 #include "COLLADAFWEffectCommon.h" 17 #include "COLLADAFWTypes.h" 18 #include "COLLADAFWColorOrTexture.h" 19 #include "COLLADAFWPointerArray.h" 20 21 22 namespace COLLADAFW 23 { 24 class Effect; 25 class Sampler; 26 } 27 28 29 namespace COLLADASaxFWL 30 { 31 32 /** TODO Documentation */ 33 class LibraryEffectsLoader : public LibraryImagesLoader 34 { 35 public: 36 enum ShaderParameterTypes 37 { 38 SHADER_PARAMETER_EMISSION, 39 SHADER_PARAMETER_AMBIENT, 40 SHADER_PARAMETER_DIFFUSE, 41 SHADER_PARAMETER_SPECULAR, 42 SHADER_PARAMETER_SHININESS, 43 SHADER_PARAMETER_REFLECTIVE, 44 SHADER_PARAMETER_REFLECTIVITY, 45 SHADER_PARAMETER_TRANSPARENT, 46 SHADER_PARAMETER_TRANSPARENCY, 47 SHADER_PARAMETER_INDEX_OF_REFRACTION, 48 UNKNOWN_SHADER_TYPE 49 }; 50 51 private: 52 enum Profile 53 { 54 PROFILE_BRIDGE, 55 PROFILE_CG, 56 PROFILE_GLES, 57 PROFILE_GLES2, 58 PROFILE_GLSL, 59 PROFILE_COMMON, 60 PROFILE_NONE 61 }; 62 63 struct Surface 64 { 65 ENUM__fx_surface_type surfaceType; 66 67 /** The unique id of the image used by the surface.*/ 68 COLLADAFW::UniqueId imageUniqueId; 69 }; 70 71 typedef std::map<String, Surface> SidSurfaceMap; 72 73 typedef std::map<String, size_t> StringIndexMap; 74 75 struct SamplerInfo 76 { 77 /** The sampler.*/ 78 COLLADAFW::Sampler* sampler; 79 80 /** The index of the sampler.*/ 81 COLLADAFW::SamplerID id; 82 83 /** The source of the sampler.*/ 84 String surfaceSid; 85 }; 86 87 typedef std::map<String, SamplerInfo> SidSamplerInfoMap; 88 89 /** 90 * There is a transparent color and a transparency value with an opaque mode in collada. 91 * We have to calculate with this the opaque color for the framework. 92 */ 93 enum OpaqueMode 94 { 95 UNSPECIFIED_OPAQUE, 96 A_ONE, 97 RGB_ZERO, 98 A_ZERO, 99 RGB_ONE 100 }; 101 102 private: 103 104 /** The effect currently being imported.*/ 105 COLLADAFW::Effect* mCurrentEffect; 106 107 /** 108 * There is a transparent color and a transparency value with an opaque mode in collada. 109 * We have to calculate with this the opaque color for the framework. 110 */ 111 COLLADAFW::ColorOrTexture mTransparent; 112 113 /** 114 * There is a transparent color and a transparency value with an opaque mode in collada. 115 * We have to calculate with this the opaque color for the framework. 116 */ 117 COLLADAFW::FloatOrParam mTransparency; 118 119 /** 120 * There is a transparent color and a transparency value with an opaque mode in collada. 121 * We have to calculate with this the opaque color for the framework. 122 */ 123 OpaqueMode mOpaqueMode; 124 125 /** The current profile.*/ 126 Profile mCurrentProfile; 127 128 /** The type of the current shader parameter.*/ 129 ShaderParameterTypes mCurrentShaderParameterType; 130 131 /** The index of the next color component to read.*/ 132 size_t mCurrentColorValueIndex; 133 134 /** The sid of the current new param.*/ 135 String mCurrentNewParamSid; 136 137 /** The current surface.*/ 138 Surface mCurrentSurface; 139 140 /** The init from string of the current surface.*/ 141 String mCurrentSurfaceInitFrom; 142 143 /** Maps sids of surfaces directly defined under an effect to the surface.*/ 144 SidSurfaceMap mEffectSidSurfaceMap; 145 146 /** Maps sids of surfaces defined under an effect profile to the surface.*/ 147 SidSurfaceMap mEffectProfileSidSurfaceMap; 148 149 /** The source of the current sampler.*/ 150 String mCurrentSamplerSource; 151 152 /** The wrapS mode of the current sampler.*/ 153 COLLADAFW::Sampler::WrapMode mCurrentSamplerWrapS; 154 155 /** The wrapT mode of the current sampler.*/ 156 COLLADAFW::Sampler::WrapMode mCurrentSamplerWrapT; 157 158 /** The current sampler.*/ 159 COLLADAFW::Sampler* mCurrentSampler; 160 161 /** Maps sids of samplers directly defined under an effect to the samplers info.*/ 162 SidSamplerInfoMap mEffectSidSamplerInfoMap; 163 164 /** Maps sids of samplers defined under an effect profile to the samplers info.*/ 165 SidSamplerInfoMap mEffectProfileSidSamplerInfoMap; 166 167 /**List of used texture sids in the current effect profile. */ 168 StringIndexMap mEffectProfileSamplersMap; 169 170 /** The index of the next sampler to add to mEffectProfileSamplersMap.*/ 171 size_t mNextSamplerIndex; 172 173 /** True, if the parser is in the technique element under profile_COMMON. */ 174 bool mInProfileCommonTechnique; 175 176 /** True, if the parser is a texture element. */ 177 bool mInTexture; 178 179 /** True, if the parser is a surface element. */ 180 bool mInSurface; 181 unsigned int mSurfaceIndex; 182 183 /** True, if the parser is a sampler 2d element. */ 184 bool mInSampler2D; 185 186 /** The variable to store the name of the second extra key. */ 187 String mSecondKey; 188 189 public: 190 191 /** Constructor. */ 192 LibraryEffectsLoader( IFilePartLoader* callingFilePartLoader ); 193 194 /** Destructor. */ 195 virtual ~LibraryEffectsLoader(); 196 197 /** Returns the unique id of the current parsed object. */ 198 virtual const COLLADAFW::UniqueId& getUniqueId(); 199 200 /** Returns the current parsed object. */ 201 virtual COLLADAFW::Object* getObject(); 202 203 /** Creates a new current effect.*/ 204 virtual bool begin__effect( const effect__AttributeData& attributeData ); 205 206 /** Sends current material to the writer an deletes it afterwards.*/ 207 virtual bool end__effect(); 208 209 210 /** Store the sid of the new param.*/ 211 virtual bool begin__newparam____fx_newparam_common( const newparam____fx_newparam_common__AttributeData& attributeData ); 212 213 /** Clear the sid of the new param.*/ 214 virtual bool end__newparam____fx_newparam_common(); 215 216 /** Store the sid of the new param.*/ 217 virtual bool begin__newparam____cg_newparam( const newparam____cg_newparam__AttributeData& attributeData ); 218 219 /** Set the current profile to PROFILE_COMMON. Create and append common effect to current 220 effect.*/ 221 virtual bool begin__profile_COMMON( const profile_COMMON__AttributeData& attributeData ); 222 223 /** Set the current profile to unknown.*/ 224 virtual bool end__profile_COMMON(); 225 226 227 /** Store the sid of the new param.*/ 228 virtual bool begin__newparam____common_newparam_type( const newparam____common_newparam_type__AttributeData& attributeData ); 229 230 /** Clear the sid of the new param.*/ 231 virtual bool end__newparam____common_newparam_type(); 232 233 234 /** Set the surface type.*/ 235 virtual bool begin__surface____fx_surface_common( const surface____fx_surface_common__AttributeData& attributeData ); 236 237 /** Adds the surface to the map of surfaces.*/ 238 virtual bool end__surface____fx_surface_common(); 239 240 241 virtual bool begin__init_from____fx_surface_init_from_common( const init_from____fx_surface_init_from_common__AttributeData& attributeData); 242 243 /** Assign the unique id of the surface image.*/ 244 virtual bool end__init_from____fx_surface_init_from_common(); 245 246 /** Store data in mCurrentSurfaceInitFrom.*/ 247 virtual bool data__init_from____fx_surface_init_from_common( const ParserChar* data, size_t length ); 248 249 250 /** Creates a new sampler2D.*/ 251 virtual bool begin__sampler2D____fx_sampler2D_common(); 252 253 /** Adds the sampler to the map of samplers.*/ 254 virtual bool end__sampler2D____fx_sampler2D_common(); 255 begin__minfilter()256 virtual bool begin__minfilter(){return true;} end__minfilter()257 virtual bool end__minfilter(){return true;} 258 virtual bool data__minfilter( const ENUM__fx_sampler_filter_common value ); 259 260 /** We don't need to do anything here.*/ begin__source____NCName()261 virtual bool begin__source____NCName(){return true;} 262 263 /** We don't need to do anything here.*/ end__source____NCName()264 virtual bool end__source____NCName(){return true;} 265 266 /** Store data in mCurrentSamplerSource.*/ 267 virtual bool data__source____NCName( const ParserChar* data, size_t length ); 268 269 /** We don't need to do anything here.*/ begin__wrap_s____fx_sampler_wrap_common()270 virtual bool begin__wrap_s____fx_sampler_wrap_common(){return true;} 271 272 /** We don't need to do anything here.*/ end__wrap_s____fx_sampler_wrap_common()273 virtual bool end__wrap_s____fx_sampler_wrap_common(){return true;} 274 275 /** Store data in mCurrentSamplerWrapS.*/ 276 virtual bool data__wrap_s____fx_sampler_wrap_common( const ENUM__fx_sampler_wrap_common value ); 277 278 /** We don't need to do anything here.*/ begin__wrap_t____fx_sampler_wrap_common()279 virtual bool begin__wrap_t____fx_sampler_wrap_common(){return true;} 280 281 /** We don't need to do anything here.*/ end__wrap_t____fx_sampler_wrap_common()282 virtual bool end__wrap_t____fx_sampler_wrap_common(){return true;} 283 284 /** Store data in mCurrentSamplerWrapS.*/ 285 virtual bool data__wrap_t____fx_sampler_wrap_common( const ENUM__fx_sampler_wrap_common value ); 286 287 /** Resolve all the samplers and copy them to the current effect.*/ 288 virtual bool begin__profile_COMMON__technique( const profile_COMMON__technique__AttributeData& attributeData ); 289 290 /** Iterate over the list of used samplers in the current effect profile and push them 291 in the sampler array. */ 292 bool fillSamplerArray(); 293 294 /** Delete all temporary samplers.*/ 295 virtual bool end__profile_COMMON__technique(); 296 297 298 /** Set the shader type of the current profile.*/ 299 virtual bool begin__profile_COMMON__technique__constant(); 300 301 /** We don't need to do anything here.*/ end__profile_COMMON__technique__constant()302 virtual bool end__profile_COMMON__technique__constant(){return true;} 303 304 305 /** Set the shader type of the current profile.*/ 306 virtual bool begin__lambert(); 307 308 /** We don't need to do anything here.*/ end__lambert()309 virtual bool end__lambert(){return true;} 310 311 312 /** Set the shader type of the current profile.*/ 313 virtual bool begin__phong(); 314 315 /** We don't need to do anything here.*/ end__phong()316 virtual bool end__phong(){return true;} 317 318 /** Set the color or texture kind.*/ 319 virtual bool begin__emission(); 320 virtual bool end__emission(); 321 virtual bool begin__ambient____common_color_or_texture_type(); 322 virtual bool end__ambient____common_color_or_texture_type(); 323 virtual bool begin__diffuse(); 324 virtual bool end__diffuse(); 325 virtual bool begin__specular(); 326 virtual bool end__specular(); 327 virtual bool begin__shininess(); 328 virtual bool end__shininess(); 329 virtual bool begin__reflective(); 330 virtual bool end__reflective(); 331 virtual bool begin__reflectivity(); 332 virtual bool end__reflectivity(); 333 /** Stores the opaque mode attribute. */ 334 virtual bool begin__transparent( const transparent__AttributeData& attributeData ); 335 virtual bool end__transparent(); 336 virtual bool begin__transparency(); 337 virtual bool end__transparency(); 338 virtual bool begin__index_of_refraction(); 339 virtual bool end__index_of_refraction(); 340 341 342 /** Set the shader type of the current profile.*/ 343 virtual bool begin__blinn(); 344 345 /** We don't need to do anything here.*/ end__blinn()346 virtual bool end__blinn(){return true;} 347 348 349 /** Sets the shader parameter type.*/ 350 virtual bool begin__common_color_or_texture_type____color( const common_color_or_texture_type____color__AttributeData& attributeData ); 351 /** Resets the shader parameter type.*/ 352 virtual bool end__common_color_or_texture_type____color(); 353 /** Stores color data into the correct color object.*/ 354 virtual bool data__common_color_or_texture_type____color( const float* data, size_t length ); 355 356 /** Sets the shader parameter type.*/ 357 virtual bool begin__common_float_or_param_type____float( const common_float_or_param_type____float__AttributeData& attributeData ); 358 /** Resets the shader parameter type.*/ 359 virtual bool end__common_float_or_param_type____float(); 360 /** Stores float data into the correct float object.*/ 361 virtual bool data__common_float_or_param_type____float( float value ); 362 363 364 /** Stores texture data into the correct texture object.*/ 365 virtual bool begin__texture( const texture__AttributeData& attributeData ); 366 /** We don't need to do anything here.*/ 367 virtual bool end__texture(); 368 369 /** Handles COLLADA 1.5 specific textures. */ 370 virtual bool begin__instance_image( const instance_image__AttributeData& attributeData ); end__instance_image()371 virtual bool end__instance_image(){return true;} 372 373 /** Finishes loading a library effects.*/ 374 virtual bool end__library_effects(); 375 376 private: 377 /** Set the shader type of the current profile.*/ 378 bool setCommonEffectShaderType( COLLADAFW::EffectCommon::ShaderType shaderType); 379 380 /** Stores color data into the correct color object.*/ 381 bool handleColorData( const float* value, size_t length ); 382 383 /** Stores color data into the @a color object.*/ 384 bool handleColorData( const float* value, size_t length, COLLADAFW::Color& color ); 385 386 /** Stores texture data into the @a shaderParameterType texture object.*/ 387 bool handleTexture( const texture__AttributeData& attributeData); 388 389 bool handleExtraEffectTextures( const COLLADAFW::PointerArray<COLLADAFW::TextureAttributes>& effectTextures ); 390 391 /** 392 * Luminance is the function, based on the ISO/CIE color standards (see ITU-R 393 * Recommendation BT.709-4), that averages the color channels into one value. 394 */ 395 double calculateLuminance ( const COLLADAFW::Color& color ); 396 397 /** Calculates the framework opacity value from the collada transparent and transparency values. */ 398 void calculateOpacity (); 399 400 /** Disable default copy ctor. */ 401 LibraryEffectsLoader( const LibraryEffectsLoader& pre ); 402 403 /** Disable default assignment operator. */ 404 const LibraryEffectsLoader& operator= ( const LibraryEffectsLoader& pre ); 405 406 /** Returns the current color or texture element. */ 407 COLLADAFW::ColorOrTexture* getCurrentColorOrTexture ( const bool forTexture = false ); 408 }; 409 410 } // namespace COLLADASAXFWL 411 412 #endif // __COLLADASAXFWL_LIBRARYEFFECTSSLOADER_H__ 413