1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2009 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 #if __GNUC__ >= 4 || __GNUC_MINOR__ >=3
40 #pragma GCC diagnostic ignored "-Wold-style-cast"
43 #include "OSGColladaTexture.h"
45 #if defined(OSG_WITH_COLLADA) || defined(OSG_DO_DOC)
47 #include "OSGColladaLog.h"
48 #include "OSGColladaGlobal.h"
49 #include "OSGColladaEffect.h"
50 #include "OSGColladaSurface.h"
51 #include "OSGColladaImage.h"
52 #include "OSGColladaExtraHandler.h"
53 #include "OSGColladaHandlerFactory.h"
55 #include <dom/domFx_sampler2D_common.h>
59 ColladaElementRegistrationHelper
ColladaTexture::_regHelper(
60 &ColladaTexture::create
,
64 ColladaElementTransitPtr
ColladaTexture::create(daeElement
*elem
,
65 ColladaGlobal
*global
)
67 return ColladaElementTransitPtr(new ColladaTexture(elem
, global
));
70 void ColladaTexture::read(ColladaElement
*colElemParent
)
72 OSG_COLLADA_LOG(("OSGColladaTexture::read\n"));
74 domCommon_color_or_texture_type::domTextureRef texture
=
75 dynamic_cast< domCommon_color_or_texture_type::domTexture
* >(
76 this->getDOMElement());
78 if(_colEffect
== NULL
)
80 SWARNING
<< "ColladaTexture::read: No effect set, can not resolve "
81 << "sampler." << std::endl
;
85 xsNCName texId
= texture
->getTexture();
86 ColladaElement
*colElem
= _colEffect
->findParam(texId
);
87 ColladaSampler2D
*colSampler
= dynamic_cast<ColladaSampler2D
*>(colElem
);
89 if(colSampler
!= NULL
)
91 this->readSampler(colSampler
);
95 this->readImage(texId
);
99 const domExtraRef
&extra
= texture
->getExtra();
101 if(extra
== NULL
|| _extraHandlers
.size() == 0)
104 ExtraHandlerStoreIt ehIt
= _extraHandlers
.begin();
105 ExtraHandlerStoreConstIt ehEnd
= _extraHandlers
.end ();
107 for(; ehIt
!= ehEnd
; ++ehIt
)
109 (*ehIt
)->readTextureExtraElements(this, extra
);
114 SWARNING
<< "ColladaTexture::read: can not resolve "
115 << "image source." << std::endl
;
119 ColladaEffect
*ColladaTexture::getEffect(void) const
124 void ColladaTexture::setEffect(ColladaEffect
*colEffect
)
126 _colEffect
= colEffect
;
129 TextureObjChunk
*ColladaTexture::getTexture(void) const
134 TextureEnvChunk
*ColladaTexture::getTexEnv(void) const
139 TextureEnvChunk
*ColladaTexture::editTexEnv(void)
142 _texEnv
= TextureEnvChunk::create();
147 TextureTransformChunk
*ColladaTexture::getTexTransform (void) const
149 return _texTransform
;
152 TextureTransformChunk
*ColladaTexture::editTexTransform(void)
154 if(_texTransform
== NULL
)
155 _texTransform
= TextureTransformChunk::create();
157 return _texTransform
;
160 bool ColladaTexture::hasAlpha(void) const
164 if(_texObj
!= NULL
&& _texObj
->getImage() != NULL
)
165 retVal
= _texObj
->getImage()->hasAlphaChannel();
170 bool ColladaTexture::hasBinaryAlpha(void) const
174 if(_texObj
!= NULL
&& _texObj
->getImage() != NULL
)
176 retVal
= _texObj
->getImage()->isAlphaBinary () ||
177 _texObj
->getImage()->calcIsAlphaBinary();
183 const std::string
ColladaTexture::getSemantic(void) const
185 domCommon_color_or_texture_type::domTextureRef texture
=
186 dynamic_cast< domCommon_color_or_texture_type::domTexture
* >(
187 this->getDOMElement());
190 return texture
->getTexcoord();
192 return std::string();
195 ColladaTexture::ColladaTexture(daeElement
*elem
,
196 ColladaGlobal
*global
) :
202 _texTransform (NULL
),
205 ColladaHandlerFactory::the()->createExtraHandlers(_extraHandlers
);
208 ColladaTexture::~ColladaTexture(void)
210 _extraHandlers
.clear();
213 void ColladaTexture::readSampler(ColladaSampler2D
*colSampler
)
215 ColladaSurface
*colSurface
= colSampler
->getSurface();
217 if(colSurface
== NULL
)
219 SWARNING
<< "ColladaTexture::read: Could not resolve surface"
225 _texObj
= TextureObjChunk::create();
226 _texObj
->setImage(colSurface
->getImage());
228 getGlobal()->getStatCollector()->getElem(
229 ColladaGlobal::statNTextureCreated
)->inc();
231 colSampler
->setupTexObj(_texObj
);
234 void ColladaTexture::readImage(const Char8
*texId
)
236 domCOLLADA
*pDoc
= this->getGlobal()->getDocRoot();
238 const domLibrary_images_Array
&imagesLib
= pDoc
->getLibrary_images_array();
240 for(UInt32 i
= 0; i
< imagesLib
.getCount(); ++i
)
242 domImage_Array
&imgArray
= imagesLib
[i
]->getImage_array();
244 for(UInt32 j
= 0; j
< imgArray
.getCount(); ++j
)
246 domImageRef pImg
= imgArray
[j
];
248 if(osgStringCmp(pImg
->getId(), texId
) == 0)
250 ColladaImageRefPtr colImage
=
251 getUserDataAs
<ColladaImage
>(pImg
);
255 colImage
= dynamic_pointer_cast
<ColladaImage
>(
256 ColladaElementFactory::the()->create(pImg
,
259 colImage
->read(this);
262 _texObj
= TextureObjChunk::create();
263 _texObj
->setImage(colImage
->getImage());
273 #endif // OSG_WITH_COLLADA