changed: gcc8 base update
[opensg.git] / Source / System / FileIO / Collada / OSGColladaTexture.cpp
blob5b3150e7d1c211e227ad1dd2790086b07ef77d14
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2009 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
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. *
18 * *
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. *
23 * *
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. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 #if __GNUC__ >= 4 || __GNUC_MINOR__ >=3
40 #pragma GCC diagnostic ignored "-Wold-style-cast"
41 #endif
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>
57 OSG_BEGIN_NAMESPACE
59 ColladaElementRegistrationHelper ColladaTexture::_regHelper(
60 &ColladaTexture::create,
61 "texture");
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;
82 return;
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);
93 else
95 this->readImage(texId);
99 const domExtraRef &extra = texture->getExtra();
101 if(extra == NULL || _extraHandlers.size() == 0)
102 return;
104 ExtraHandlerStoreIt ehIt = _extraHandlers.begin();
105 ExtraHandlerStoreConstIt ehEnd = _extraHandlers.end ();
107 for(; ehIt != ehEnd; ++ehIt)
109 (*ehIt)->readTextureExtraElements(this, extra);
112 if(_texObj == NULL)
114 SWARNING << "ColladaTexture::read: can not resolve "
115 << "image source." << std::endl;
119 ColladaEffect *ColladaTexture::getEffect(void) const
121 return _colEffect;
124 void ColladaTexture::setEffect(ColladaEffect *colEffect)
126 _colEffect = colEffect;
129 TextureObjChunk *ColladaTexture::getTexture(void) const
131 return _texObj;
134 TextureEnvChunk *ColladaTexture::getTexEnv(void) const
136 return _texEnv;
139 TextureEnvChunk *ColladaTexture::editTexEnv(void)
141 if(_texEnv == NULL)
142 _texEnv = TextureEnvChunk::create();
144 return _texEnv;
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
162 bool retVal = false;
164 if(_texObj != NULL && _texObj->getImage() != NULL)
165 retVal = _texObj->getImage()->hasAlphaChannel();
167 return retVal;
170 bool ColladaTexture::hasBinaryAlpha(void) const
172 bool retVal = false;
174 if(_texObj != NULL && _texObj->getImage() != NULL)
176 retVal = _texObj->getImage()->isAlphaBinary () ||
177 _texObj->getImage()->calcIsAlphaBinary();
180 return retVal;
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());
189 if(texture != NULL)
190 return texture->getTexcoord();
192 return std::string();
195 ColladaTexture::ColladaTexture(daeElement *elem,
196 ColladaGlobal *global) :
197 Inherited (elem,
198 global),
199 _colEffect (NULL ),
200 _texObj (NULL ),
201 _texEnv (NULL ),
202 _texTransform (NULL ),
203 _extraHandlers( )
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"
220 << std::endl;
222 return;
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);
253 if(colImage == NULL)
255 colImage = dynamic_pointer_cast<ColladaImage>(
256 ColladaElementFactory::the()->create(pImg,
257 getGlobal()));
259 colImage->read(this);
262 _texObj = TextureObjChunk::create();
263 _texObj->setImage(colImage->getImage());
265 break;
271 OSG_END_NAMESPACE
273 #endif // OSG_WITH_COLLADA