| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include "daeWriter.h" |
|---|
| 15 | #include "ReaderWriterDAE.h" |
|---|
| 16 | |
|---|
| 17 | #include <dae/domAny.h> |
|---|
| 18 | #include <dom/domCOLLADA.h> |
|---|
| 19 | #include <dom/domConstants.h> |
|---|
| 20 | #include <dom/domProfile_COMMON.h> |
|---|
| 21 | |
|---|
| 22 | #include <sstream> |
|---|
| 23 | #include <osgDB/ConvertUTF> |
|---|
| 24 | #include <osgDB/FileNameUtils> |
|---|
| 25 | #include <osgDB/WriteFile> |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | #ifdef WIN32 |
|---|
| 31 | #include "windows.h" |
|---|
| 32 | #endif |
|---|
| 33 | |
|---|
| 34 | using namespace osgDAE; |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | void daeWriter::processMaterial( osg::StateSet *ss, domBind_material *pDomBindMaterial, const std::string &geoName ) |
|---|
| 38 | { |
|---|
| 39 | osg::ref_ptr<osg::StateSet> ssClean = CleanStateSet(ss); |
|---|
| 40 | domBind_material::domTechnique_common *tc = daeSafeCast< domBind_material::domTechnique_common >( pDomBindMaterial->add( COLLADA_ELEMENT_TECHNIQUE_COMMON ) ); |
|---|
| 41 | domInstance_material *pDomInstanceMaterial = daeSafeCast< domInstance_material >( tc->add( COLLADA_ELEMENT_INSTANCE_MATERIAL ) ); |
|---|
| 42 | const std::string symbol( _pluginOptions.namesUseCodepage ? osgDB::convertStringFromCurrentCodePageToUTF8(geoName + "_material") : (geoName + "_material") ); |
|---|
| 43 | pDomInstanceMaterial->setSymbol( symbol.c_str() ); |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | MaterialMap::iterator iter = materialMap.find( ssClean ); |
|---|
| 47 | if ( iter != materialMap.end() ) |
|---|
| 48 | { |
|---|
| 49 | std::string url = "#" + std::string( iter->second->getId() ); |
|---|
| 50 | pDomInstanceMaterial->setTarget( url.c_str() ); |
|---|
| 51 | return; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | if ( lib_mats == NULL ) |
|---|
| 55 | { |
|---|
| 56 | lib_mats = daeSafeCast< domLibrary_materials >( dom->add( COLLADA_ELEMENT_LIBRARY_MATERIALS ) ); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | domMaterial *mat = daeSafeCast< domMaterial >( lib_mats->add( COLLADA_ELEMENT_MATERIAL ) ); |
|---|
| 60 | std::string name( ssClean->getName() ); |
|---|
| 61 | if ( name.empty() ) |
|---|
| 62 | { |
|---|
| 63 | name = "material"; |
|---|
| 64 | } |
|---|
| 65 | name = uniquify( name ); |
|---|
| 66 | |
|---|
| 67 | mat->setId( name.c_str() ); |
|---|
| 68 | |
|---|
| 69 | std::string url = "#" + name; |
|---|
| 70 | pDomInstanceMaterial->setTarget( url.c_str() ); |
|---|
| 71 | |
|---|
| 72 | domInstance_effect *ie = daeSafeCast<domInstance_effect>( mat->add( COLLADA_ELEMENT_INSTANCE_EFFECT ) ); |
|---|
| 73 | |
|---|
| 74 | if ( lib_effects == NULL ) |
|---|
| 75 | { |
|---|
| 76 | lib_effects = daeSafeCast< domLibrary_effects >( dom->add( COLLADA_ELEMENT_LIBRARY_EFFECTS ) ); |
|---|
| 77 | } |
|---|
| 78 | domEffect *effect = daeSafeCast< domEffect >( lib_effects->add( COLLADA_ELEMENT_EFFECT ) ); |
|---|
| 79 | std::string efName = name + "_effect"; |
|---|
| 80 | |
|---|
| 81 | effect->setId( efName.c_str() ); |
|---|
| 82 | |
|---|
| 83 | url = "#" + efName; |
|---|
| 84 | ie->setUrl( url.c_str() ); |
|---|
| 85 | |
|---|
| 86 | domProfile_COMMON *pc = daeSafeCast< domProfile_COMMON >( effect->add( COLLADA_ELEMENT_PROFILE_COMMON ) ); |
|---|
| 87 | domProfile_COMMON::domTechnique *pc_teq = daeSafeCast< domProfile_COMMON::domTechnique >( pc->add( COLLADA_ELEMENT_TECHNIQUE ) ); |
|---|
| 88 | pc_teq->setSid( "t0" ); |
|---|
| 89 | domProfile_COMMON::domTechnique::domPhong *phong = daeSafeCast< domProfile_COMMON::domTechnique::domPhong >( pc_teq->add( COLLADA_ELEMENT_PHONG ) ); |
|---|
| 90 | |
|---|
| 91 | osg::Texture *tex = static_cast<osg::Texture*>(ssClean->getTextureAttribute( 0, osg::StateAttribute::TEXTURE )); |
|---|
| 92 | if ( ssClean->getTextureAttribute( 1, osg::StateAttribute::TEXTURE ) != NULL ) |
|---|
| 93 | { |
|---|
| 94 | tex = static_cast<osg::Texture*>(ssClean->getTextureAttribute( 1, osg::StateAttribute::TEXTURE )); |
|---|
| 95 | } |
|---|
| 96 | if ( tex != NULL && tex->getImage( 0 ) != NULL ) |
|---|
| 97 | { |
|---|
| 98 | |
|---|
| 99 | domImage *img = daeSafeCast< domImage >( pc->add( COLLADA_ELEMENT_IMAGE ) ); |
|---|
| 100 | std::string iName = efName + "-image"; |
|---|
| 101 | img->setId( iName.c_str() ); |
|---|
| 102 | |
|---|
| 103 | osg::Image *osgimg = tex->getImage( 0 ); |
|---|
| 104 | |
|---|
| 105 | domImage::domInit_from *imgif = daeSafeCast< domImage::domInit_from >( img->add( COLLADA_ELEMENT_INIT_FROM ) ); |
|---|
| 106 | |
|---|
| 107 | std::string fileURI; |
|---|
| 108 | if (_pluginOptions.linkOrignialTextures) |
|---|
| 109 | { |
|---|
| 110 | |
|---|
| 111 | fileURI = osgDB::findDataFile(osgimg->getFileName()); |
|---|
| 112 | if (fileURI=="" && _pluginOptions.forceTexture) |
|---|
| 113 | { |
|---|
| 114 | fileURI = osgDB::getRealPath(osgimg->getFileName()); |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | else |
|---|
| 118 | { |
|---|
| 119 | |
|---|
| 120 | _externalWriter.write(*osgimg, _options, NULL, &fileURI); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | fileURI = ReaderWriterDAE::ConvertFilePathToColladaCompatibleURI(fileURI); |
|---|
| 124 | |
|---|
| 125 | daeURI dd(*dae, fileURI); |
|---|
| 126 | imgif->setValue( dd ); |
|---|
| 127 | |
|---|
| 128 | if (_pluginOptions.linkOrignialTextures) imgif->getValue().makeRelativeTo(doc->getDocumentURI()); |
|---|
| 129 | |
|---|
| 130 | if (!_pluginOptions.earthTex) |
|---|
| 131 | { |
|---|
| 132 | domCommon_newparam_type *np = daeSafeCast< domCommon_newparam_type >( pc->add(COLLADA_ELEMENT_NEWPARAM) ); |
|---|
| 133 | std::string surfName = efName + "-surface"; |
|---|
| 134 | np->setSid( surfName.c_str() ); |
|---|
| 135 | domFx_surface_common *surface = daeSafeCast< domFx_surface_common >( np->add(COLLADA_ELEMENT_SURFACE) ); |
|---|
| 136 | domFx_surface_init_from_common *sif = daeSafeCast< domFx_surface_init_from_common >( surface->add(COLLADA_ELEMENT_INIT_FROM) ); |
|---|
| 137 | sif->setValue( iName.c_str() ); |
|---|
| 138 | surface->setType( FX_SURFACE_TYPE_ENUM_2D ); |
|---|
| 139 | |
|---|
| 140 | np = daeSafeCast< domCommon_newparam_type >( pc->add( COLLADA_ELEMENT_NEWPARAM ) ); |
|---|
| 141 | std::string sampName = efName + "-sampler"; |
|---|
| 142 | np->setSid( sampName.c_str() ); |
|---|
| 143 | domFx_sampler2D_common *sampler = daeSafeCast< domFx_sampler2D_common >( np->add( COLLADA_ELEMENT_SAMPLER2D ) ); |
|---|
| 144 | domFx_sampler2D_common_complexType::domSource *source = daeSafeCast< domFx_sampler2D_common_complexType::domSource >( sampler->add( COLLADA_ELEMENT_SOURCE ) ); |
|---|
| 145 | source->setValue( surfName.c_str() ); |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | domFx_sampler2D_common_complexType::domWrap_s *wrap_s = daeSafeCast< domFx_sampler2D_common_complexType::domWrap_s >( sampler->add( COLLADA_ELEMENT_WRAP_S ) ); |
|---|
| 149 | osg::Texture::WrapMode wrap = tex->getWrap( osg::Texture::WRAP_S ); |
|---|
| 150 | switch( wrap ) |
|---|
| 151 | { |
|---|
| 152 | case osg::Texture::CLAMP: |
|---|
| 153 | case osg::Texture::CLAMP_TO_EDGE: |
|---|
| 154 | wrap_s->setValue( FX_SAMPLER_WRAP_COMMON_CLAMP ); |
|---|
| 155 | break; |
|---|
| 156 | case osg::Texture::CLAMP_TO_BORDER: |
|---|
| 157 | wrap_s->setValue( FX_SAMPLER_WRAP_COMMON_BORDER ); |
|---|
| 158 | break; |
|---|
| 159 | case osg::Texture::REPEAT: |
|---|
| 160 | wrap_s->setValue( FX_SAMPLER_WRAP_COMMON_WRAP ); |
|---|
| 161 | break; |
|---|
| 162 | case osg::Texture::MIRROR: |
|---|
| 163 | wrap_s->setValue( FX_SAMPLER_WRAP_COMMON_MIRROR ); |
|---|
| 164 | break; |
|---|
| 165 | default: |
|---|
| 166 | wrap_s->setValue( FX_SAMPLER_WRAP_COMMON_NONE ); |
|---|
| 167 | break; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | domFx_sampler2D_common_complexType::domWrap_t *wrap_t = daeSafeCast< domFx_sampler2D_common_complexType::domWrap_t >( sampler->add( COLLADA_ELEMENT_WRAP_T ) ); |
|---|
| 171 | wrap = tex->getWrap( osg::Texture::WRAP_T ); |
|---|
| 172 | switch( wrap ) |
|---|
| 173 | { |
|---|
| 174 | case osg::Texture::CLAMP: |
|---|
| 175 | case osg::Texture::CLAMP_TO_EDGE: |
|---|
| 176 | wrap_t->setValue( FX_SAMPLER_WRAP_COMMON_CLAMP ); |
|---|
| 177 | break; |
|---|
| 178 | case osg::Texture::CLAMP_TO_BORDER: |
|---|
| 179 | wrap_t->setValue( FX_SAMPLER_WRAP_COMMON_BORDER ); |
|---|
| 180 | break; |
|---|
| 181 | case osg::Texture::REPEAT: |
|---|
| 182 | wrap_t->setValue( FX_SAMPLER_WRAP_COMMON_WRAP ); |
|---|
| 183 | break; |
|---|
| 184 | case osg::Texture::MIRROR: |
|---|
| 185 | wrap_t->setValue( FX_SAMPLER_WRAP_COMMON_MIRROR ); |
|---|
| 186 | break; |
|---|
| 187 | default: |
|---|
| 188 | wrap_t->setValue( FX_SAMPLER_WRAP_COMMON_NONE ); |
|---|
| 189 | break; |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | const osg::Vec4 &bcol = tex->getBorderColor(); |
|---|
| 193 | domFx_sampler2D_common_complexType::domBorder_color *dbcol = daeSafeCast< domFx_sampler2D_common_complexType::domBorder_color >( sampler->add( COLLADA_ELEMENT_BORDER_COLOR ) ); |
|---|
| 194 | dbcol->getValue().append( bcol.r() ); |
|---|
| 195 | dbcol->getValue().append( bcol.g() ); |
|---|
| 196 | dbcol->getValue().append( bcol.b() ); |
|---|
| 197 | dbcol->getValue().append( bcol.a() ); |
|---|
| 198 | |
|---|
| 199 | domFx_sampler2D_common_complexType::domMinfilter *minfilter = daeSafeCast< domFx_sampler2D_common_complexType::domMinfilter >( sampler->add( COLLADA_ELEMENT_MINFILTER ) ); |
|---|
| 200 | osg::Texture::FilterMode mode = tex->getFilter( osg::Texture::MIN_FILTER ); |
|---|
| 201 | switch( mode ) |
|---|
| 202 | { |
|---|
| 203 | case osg::Texture::LINEAR: |
|---|
| 204 | minfilter->setValue( FX_SAMPLER_FILTER_COMMON_LINEAR ); |
|---|
| 205 | break; |
|---|
| 206 | case osg::Texture::LINEAR_MIPMAP_LINEAR: |
|---|
| 207 | minfilter->setValue( FX_SAMPLER_FILTER_COMMON_LINEAR_MIPMAP_LINEAR ); |
|---|
| 208 | break; |
|---|
| 209 | case osg::Texture::LINEAR_MIPMAP_NEAREST: |
|---|
| 210 | minfilter->setValue( FX_SAMPLER_FILTER_COMMON_LINEAR_MIPMAP_NEAREST ); |
|---|
| 211 | break; |
|---|
| 212 | case osg::Texture::NEAREST: |
|---|
| 213 | minfilter->setValue( FX_SAMPLER_FILTER_COMMON_NEAREST ); |
|---|
| 214 | break; |
|---|
| 215 | case osg::Texture::NEAREST_MIPMAP_LINEAR: |
|---|
| 216 | minfilter->setValue( FX_SAMPLER_FILTER_COMMON_NEAREST_MIPMAP_LINEAR ); |
|---|
| 217 | break; |
|---|
| 218 | case osg::Texture::NEAREST_MIPMAP_NEAREST: |
|---|
| 219 | minfilter->setValue( FX_SAMPLER_FILTER_COMMON_NEAREST_MIPMAP_NEAREST ); |
|---|
| 220 | break; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | domFx_sampler2D_common_complexType::domMagfilter *magfilter = daeSafeCast< domFx_sampler2D_common_complexType::domMagfilter >( sampler->add( COLLADA_ELEMENT_MAGFILTER ) ); |
|---|
| 224 | mode = tex->getFilter( osg::Texture::MAG_FILTER ); |
|---|
| 225 | switch( mode ) |
|---|
| 226 | { |
|---|
| 227 | case osg::Texture::LINEAR: |
|---|
| 228 | magfilter->setValue( FX_SAMPLER_FILTER_COMMON_LINEAR ); |
|---|
| 229 | break; |
|---|
| 230 | case osg::Texture::LINEAR_MIPMAP_LINEAR: |
|---|
| 231 | magfilter->setValue( FX_SAMPLER_FILTER_COMMON_LINEAR_MIPMAP_LINEAR ); |
|---|
| 232 | break; |
|---|
| 233 | case osg::Texture::LINEAR_MIPMAP_NEAREST: |
|---|
| 234 | magfilter->setValue( FX_SAMPLER_FILTER_COMMON_LINEAR_MIPMAP_NEAREST ); |
|---|
| 235 | break; |
|---|
| 236 | case osg::Texture::NEAREST: |
|---|
| 237 | magfilter->setValue( FX_SAMPLER_FILTER_COMMON_NEAREST ); |
|---|
| 238 | break; |
|---|
| 239 | case osg::Texture::NEAREST_MIPMAP_LINEAR: |
|---|
| 240 | magfilter->setValue( FX_SAMPLER_FILTER_COMMON_NEAREST_MIPMAP_LINEAR ); |
|---|
| 241 | break; |
|---|
| 242 | case osg::Texture::NEAREST_MIPMAP_NEAREST: |
|---|
| 243 | magfilter->setValue( FX_SAMPLER_FILTER_COMMON_NEAREST_MIPMAP_NEAREST ); |
|---|
| 244 | break; |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | domCommon_color_or_texture_type *cot = daeSafeCast< domCommon_color_or_texture_type >( phong->add( COLLADA_ELEMENT_DIFFUSE ) ); |
|---|
| 249 | domCommon_color_or_texture_type_complexType::domTexture *dtex = daeSafeCast< domCommon_color_or_texture_type_complexType::domTexture >( cot->add( COLLADA_ELEMENT_TEXTURE ) ); |
|---|
| 250 | dtex->setTexture( sampName.c_str() ); |
|---|
| 251 | dtex->setTexcoord( "texcoord0" ); |
|---|
| 252 | } |
|---|
| 253 | else |
|---|
| 254 | { |
|---|
| 255 | |
|---|
| 256 | domCommon_color_or_texture_type *cot = daeSafeCast< domCommon_color_or_texture_type >( phong->add( COLLADA_ELEMENT_DIFFUSE ) ); |
|---|
| 257 | domCommon_color_or_texture_type_complexType::domTexture *dtex = daeSafeCast< domCommon_color_or_texture_type_complexType::domTexture >( cot->add( COLLADA_ELEMENT_TEXTURE ) ); |
|---|
| 258 | dtex->setTexture( iName.c_str() ); |
|---|
| 259 | dtex->setTexcoord( "texcoord0" ); |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | domInstance_material::domBind_vertex_input *bvi = daeSafeCast< domInstance_material::domBind_vertex_input >( pDomInstanceMaterial->add( COLLADA_ELEMENT_BIND_VERTEX_INPUT ) ); |
|---|
| 263 | bvi->setSemantic( "texcoord0" ); |
|---|
| 264 | bvi->setInput_semantic( COMMON_PROFILE_INPUT_TEXCOORD ); |
|---|
| 265 | bvi->setInput_set( 0 ); |
|---|
| 266 | } |
|---|
| 267 | osg::Material *osgmat = static_cast<osg::Material*>(ssClean->getAttribute( osg::StateAttribute::MATERIAL )); |
|---|
| 268 | if ( osgmat != NULL ) |
|---|
| 269 | { |
|---|
| 270 | const osg::Vec4 &eCol = osgmat->getEmissionFrontAndBack()?osgmat->getEmission( osg::Material::FRONT_AND_BACK ):osgmat->getEmission( osg::Material::FRONT ); |
|---|
| 271 | const osg::Vec4 &aCol = osgmat->getAmbientFrontAndBack()?osgmat->getAmbient( osg::Material::FRONT_AND_BACK ):osgmat->getAmbient( osg::Material::FRONT ); |
|---|
| 272 | const osg::Vec4 &dCol = osgmat->getDiffuseFrontAndBack()?osgmat->getDiffuse( osg::Material::FRONT_AND_BACK ):osgmat->getDiffuse( osg::Material::FRONT ); |
|---|
| 273 | const osg::Vec4 &sCol = osgmat->getSpecularFrontAndBack()?osgmat->getSpecular( osg::Material::FRONT_AND_BACK ):osgmat->getSpecular( osg::Material::FRONT ); |
|---|
| 274 | float shininess = osgmat->getShininessFrontAndBack()?osgmat->getShininess( osg::Material::FRONT_AND_BACK ):osgmat->getShininess( osg::Material::FRONT ); |
|---|
| 275 | |
|---|
| 276 | domCommon_color_or_texture_type *cot = daeSafeCast< domCommon_color_or_texture_type >( phong->add( COLLADA_ELEMENT_EMISSION ) ); |
|---|
| 277 | domCommon_color_or_texture_type_complexType::domColor *col = daeSafeCast< domCommon_color_or_texture_type_complexType::domColor >( cot->add( COLLADA_ELEMENT_COLOR ) ); |
|---|
| 278 | col->getValue().append( eCol.r() ); |
|---|
| 279 | col->getValue().append( eCol.g() ); |
|---|
| 280 | col->getValue().append( eCol.b() ); |
|---|
| 281 | col->getValue().append( eCol.a() ); |
|---|
| 282 | |
|---|
| 283 | cot = daeSafeCast< domCommon_color_or_texture_type >( phong->add( COLLADA_ELEMENT_AMBIENT ) ); |
|---|
| 284 | col = daeSafeCast< domCommon_color_or_texture_type_complexType::domColor >( cot->add( COLLADA_ELEMENT_COLOR ) ); |
|---|
| 285 | col->getValue().append( aCol.r() ); |
|---|
| 286 | col->getValue().append( aCol.g() ); |
|---|
| 287 | col->getValue().append( aCol.b() ); |
|---|
| 288 | col->getValue().append( aCol.a() ); |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | if ( phong->getDiffuse() == NULL ) |
|---|
| 293 | { |
|---|
| 294 | cot = daeSafeCast< domCommon_color_or_texture_type >( phong->add( COLLADA_ELEMENT_DIFFUSE ) ); |
|---|
| 295 | col = daeSafeCast< domCommon_color_or_texture_type_complexType::domColor >( cot->add( COLLADA_ELEMENT_COLOR ) ); |
|---|
| 296 | col->getValue().append( dCol.r() ); |
|---|
| 297 | col->getValue().append( dCol.g() ); |
|---|
| 298 | col->getValue().append( dCol.b() ); |
|---|
| 299 | col->getValue().append( dCol.a() ); |
|---|
| 300 | } |
|---|
| 301 | else |
|---|
| 302 | { |
|---|
| 303 | cot = phong->getDiffuse(); |
|---|
| 304 | |
|---|
| 305 | if (_pluginOptions.writeExtras) |
|---|
| 306 | { |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | domCommon_color_or_texture_type_complexType::domTexture *dtex = cot->getTexture(); |
|---|
| 316 | domExtra *extra = daeSafeCast< domExtra >( dtex->add( COLLADA_ELEMENT_EXTRA ) ); |
|---|
| 317 | extra->setType( "color" ); |
|---|
| 318 | domTechnique *teq = daeSafeCast< domTechnique >( extra->add( COLLADA_ELEMENT_TECHNIQUE ) ); |
|---|
| 319 | teq->setProfile( "SCEI" ); |
|---|
| 320 | domAny *any = (domAny*)(daeElement*)teq->add( COLLADA_ELEMENT_COLOR ); |
|---|
| 321 | |
|---|
| 322 | std::ostringstream colVal; |
|---|
| 323 | colVal << dCol.r() << " " << dCol.g() << " " << dCol.b() << " " << dCol.a(); |
|---|
| 324 | any->setValue( colVal.str().c_str() ); |
|---|
| 325 | } |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | cot = daeSafeCast< domCommon_color_or_texture_type >( phong->add( COLLADA_ELEMENT_SPECULAR ) ); |
|---|
| 329 | col = daeSafeCast< domCommon_color_or_texture_type_complexType::domColor >( cot->add( COLLADA_ELEMENT_COLOR ) ); |
|---|
| 330 | col->getValue().append( sCol.r() ); |
|---|
| 331 | col->getValue().append( sCol.g() ); |
|---|
| 332 | col->getValue().append( sCol.b() ); |
|---|
| 333 | col->getValue().append( sCol.a() ); |
|---|
| 334 | |
|---|
| 335 | domCommon_float_or_param_type *fop = daeSafeCast< domCommon_float_or_param_type >( phong->add( COLLADA_ELEMENT_SHININESS ) ); |
|---|
| 336 | domCommon_float_or_param_type_complexType::domFloat *f = daeSafeCast< domCommon_float_or_param_type_complexType::domFloat >( fop->add(COLLADA_ELEMENT_FLOAT) ); |
|---|
| 337 | f->setValue( shininess ); |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | if (osg::StateSet::TRANSPARENT_BIN == m_CurrentRenderingHint) |
|---|
| 343 | { |
|---|
| 344 | osg::BlendFunc *pBlendFunc = static_cast< osg::BlendFunc * >( ssClean->getAttribute( osg::StateAttribute::BLENDFUNC ) ); |
|---|
| 345 | osg::BlendColor *pBlendColor = static_cast< osg::BlendColor * >( ssClean->getAttribute( osg::StateAttribute::BLENDCOLOR ) ); |
|---|
| 346 | if (pBlendFunc != NULL) |
|---|
| 347 | { |
|---|
| 348 | if (pBlendColor != NULL) |
|---|
| 349 | { |
|---|
| 350 | if ((GL_CONSTANT_ALPHA == pBlendFunc->getSource()) && (GL_ONE_MINUS_CONSTANT_ALPHA == pBlendFunc->getDestination())) |
|---|
| 351 | { |
|---|
| 352 | |
|---|
| 353 | domCommon_transparent_type *pTransparent = daeSafeCast<domCommon_transparent_type>(phong->add(COLLADA_ELEMENT_TRANSPARENT)); |
|---|
| 354 | pTransparent->setOpaque(FX_OPAQUE_ENUM_A_ONE); |
|---|
| 355 | domCommon_color_or_texture_type_complexType::domColor *pColor = daeSafeCast<domCommon_color_or_texture_type_complexType::domColor>(pTransparent->add(COLLADA_ELEMENT_COLOR)); |
|---|
| 356 | domCommon_float_or_param_type *pFop = daeSafeCast<domCommon_float_or_param_type>(phong->add(COLLADA_ELEMENT_TRANSPARENCY)); |
|---|
| 357 | domCommon_float_or_param_type_complexType::domFloat *pTransparency = daeSafeCast<domCommon_float_or_param_type_complexType::domFloat>(pFop->add(COLLADA_ELEMENT_FLOAT)); |
|---|
| 358 | if (_pluginOptions.googleMode) |
|---|
| 359 | { |
|---|
| 360 | pColor->getValue().append(1.0); |
|---|
| 361 | pColor->getValue().append(1.0); |
|---|
| 362 | pColor->getValue().append(1.0); |
|---|
| 363 | pColor->getValue().append(1.0); |
|---|
| 364 | pTransparency->setValue(1.0 - pBlendColor->getConstantColor().a()); |
|---|
| 365 | } |
|---|
| 366 | else |
|---|
| 367 | { |
|---|
| 368 | pColor->getValue().append(pBlendColor->getConstantColor().r()); |
|---|
| 369 | pColor->getValue().append(pBlendColor->getConstantColor().g()); |
|---|
| 370 | pColor->getValue().append(pBlendColor->getConstantColor().b()); |
|---|
| 371 | pColor->getValue().append(pBlendColor->getConstantColor().a()); |
|---|
| 372 | pTransparency->setValue(1.0); |
|---|
| 373 | } |
|---|
| 374 | } |
|---|
| 375 | else if ((GL_ONE_MINUS_CONSTANT_COLOR == pBlendFunc->getSource()) && (GL_CONSTANT_COLOR == pBlendFunc->getDestination())) |
|---|
| 376 | { |
|---|
| 377 | |
|---|
| 378 | domCommon_transparent_type *pTransparent = daeSafeCast<domCommon_transparent_type>(phong->add(COLLADA_ELEMENT_TRANSPARENT)); |
|---|
| 379 | pTransparent->setOpaque(FX_OPAQUE_ENUM_RGB_ZERO); |
|---|
| 380 | domCommon_color_or_texture_type_complexType::domColor *pColor = daeSafeCast<domCommon_color_or_texture_type_complexType::domColor>(pTransparent->add(COLLADA_ELEMENT_COLOR)); |
|---|
| 381 | pColor->getValue().append(pBlendColor->getConstantColor().r()); |
|---|
| 382 | pColor->getValue().append(pBlendColor->getConstantColor().g()); |
|---|
| 383 | pColor->getValue().append(pBlendColor->getConstantColor().b()); |
|---|
| 384 | pColor->getValue().append(pBlendColor->getConstantColor().a()); |
|---|
| 385 | domCommon_float_or_param_type *pFop = daeSafeCast<domCommon_float_or_param_type>(phong->add(COLLADA_ELEMENT_TRANSPARENCY)); |
|---|
| 386 | domCommon_float_or_param_type_complexType::domFloat *pTransparency = daeSafeCast<domCommon_float_or_param_type_complexType::domFloat>(pFop->add(COLLADA_ELEMENT_FLOAT)); |
|---|
| 387 | pTransparency->setValue(1.0); |
|---|
| 388 | } |
|---|
| 389 | else |
|---|
| 390 | OSG_WARN << "Unsupported BlendFunction parameters in transparency processing." << std::endl; |
|---|
| 391 | } |
|---|
| 392 | else if (tex != NULL && tex->getImage( 0 ) != NULL) |
|---|
| 393 | { |
|---|
| 394 | domCommon_transparent_type *ctt = daeSafeCast< domCommon_transparent_type >( phong->add(COLLADA_ELEMENT_TRANSPARENT) ); |
|---|
| 395 | ctt->setOpaque( FX_OPAQUE_ENUM_A_ONE ); |
|---|
| 396 | domCommon_color_or_texture_type_complexType::domTexture * dtex = daeSafeCast< domCommon_color_or_texture_type_complexType::domTexture >( ctt->add(COLLADA_ELEMENT_TEXTURE) ); |
|---|
| 397 | |
|---|
| 398 | if (!_pluginOptions.earthTex) |
|---|
| 399 | { |
|---|
| 400 | std::string sampName = efName + "-sampler"; |
|---|
| 401 | dtex->setTexture( sampName.c_str() ); |
|---|
| 402 | } |
|---|
| 403 | else |
|---|
| 404 | { |
|---|
| 405 | std::string iName = efName + "-image"; |
|---|
| 406 | dtex->setTexture( iName.c_str() ); |
|---|
| 407 | } |
|---|
| 408 | dtex->setTexcoord( "texcoord0" ); |
|---|
| 409 | } |
|---|
| 410 | else |
|---|
| 411 | { |
|---|
| 412 | OSG_WARN << "Transparency processing - No texture or BlendColor." << std::endl; |
|---|
| 413 | } |
|---|
| 414 | } |
|---|
| 415 | else |
|---|
| 416 | { |
|---|
| 417 | OSG_WARN << "Transparency processing - BlendFunction not found." << std::endl; |
|---|
| 418 | } |
|---|
| 419 | } |
|---|
| 420 | |
|---|
| 421 | if (_pluginOptions.writeExtras) |
|---|
| 422 | { |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | |
|---|
| 428 | |
|---|
| 429 | |
|---|
| 430 | |
|---|
| 431 | |
|---|
| 432 | if (osg::StateAttribute::INHERIT != ssClean->getMode(GL_CULL_FACE)) |
|---|
| 433 | { |
|---|
| 434 | domExtra *pExtra = daeSafeCast<domExtra>(pc->add(COLLADA_ELEMENT_EXTRA)); |
|---|
| 435 | domTechnique *pTechnique = daeSafeCast<domTechnique>(pExtra->add( COLLADA_ELEMENT_TECHNIQUE ) ); |
|---|
| 436 | pTechnique->setProfile("GOOGLEEARTH"); |
|---|
| 437 | domAny *pAny = (domAny*)(daeElement*)pTechnique->add("double_sided"); |
|---|
| 438 | if (GL_FALSE == ssClean->getMode(GL_CULL_FACE)) |
|---|
| 439 | pAny->setValue("1"); |
|---|
| 440 | else |
|---|
| 441 | pAny->setValue("0"); |
|---|
| 442 | } |
|---|
| 443 | } |
|---|
| 444 | |
|---|
| 445 | materialMap.insert( std::make_pair( ssClean, mat ) ); |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| 448 | osg::StateSet* daeWriter::CleanStateSet(osg::StateSet* pStateSet) const |
|---|
| 449 | { |
|---|
| 450 | |
|---|
| 451 | osg::StateSet* pCleanedStateSet = new osg::StateSet; |
|---|
| 452 | pCleanedStateSet->setTextureAttributeList(pStateSet->getTextureAttributeList()); |
|---|
| 453 | if (NULL != pStateSet->getAttribute(osg::StateAttribute::BLENDFUNC)) |
|---|
| 454 | pCleanedStateSet->setAttribute(pStateSet->getAttribute(osg::StateAttribute::BLENDFUNC)); |
|---|
| 455 | if (NULL != pStateSet->getAttribute(osg::StateAttribute::BLENDCOLOR)) |
|---|
| 456 | pCleanedStateSet->setAttribute(pStateSet->getAttribute(osg::StateAttribute::BLENDCOLOR)); |
|---|
| 457 | if (NULL != pStateSet->getAttribute(osg::StateAttribute::MATERIAL)) |
|---|
| 458 | pCleanedStateSet->setAttribute(pStateSet->getAttribute(osg::StateAttribute::MATERIAL)); |
|---|
| 459 | |
|---|
| 460 | if (osg::StateAttribute::ON != pStateSet->getMode(GL_CULL_FACE)) |
|---|
| 461 | pCleanedStateSet->setMode(GL_CULL_FACE, pStateSet->getMode(GL_CULL_FACE)); |
|---|
| 462 | return pCleanedStateSet; |
|---|
| 463 | } |
|---|
| 464 | |
|---|