fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FileIO / OSB / OSGOSBCubeTextureChunkElement.cpp
blobc2a9eaaf1f4844641305dbbfb11bba91f306ccc8
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2008 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 #include "OSGOSBCubeTextureChunkElement.h"
41 #include "OSGOSBRootElement.h"
43 #include "OSGChunkMaterial.h"
44 #include "OSGCubeTextureObjChunk.h"
45 #include "OSGTextureEnvChunk.h"
47 OSG_USING_NAMESPACE
49 /*-------------------------------------------------------------------------*/
50 /* OSBCubeTextureChunkElement */
51 /*-------------------------------------------------------------------------*/
53 /*! Reads the depreciated TextureChunk from an osb and converts it to the
54 CubeTextureObjChunk + TextureEnvChunk combination that is now the preferred
55 way to store the same information.
58 /*-------------------------------------------------------------------------*/
59 /* Static members */
61 OSBElementRegistrationHelper<OSBCubeTextureChunkElement>
62 OSBCubeTextureChunkElement::_regHelper =
63 OSBElementRegistrationHelper<OSBCubeTextureChunkElement>(
64 "CubeTextureChunk");
66 /*-------------------------------------------------------------------------*/
67 /* Constructor */
69 OSBCubeTextureChunkElement::OSBCubeTextureChunkElement(OSBRootElement *root) :
70 Inherited (root, OSGOSBHeaderVersion200),
71 _pCubeTexObj(NULL ),
72 _pTexEnv (NULL )
76 /*-------------------------------------------------------------------------*/
77 /* Destructor */
79 OSBCubeTextureChunkElement::~OSBCubeTextureChunkElement(void)
83 /*-------------------------------------------------------------------------*/
84 /* Reading */
86 // TextureChunk is being split into CubeTextureObjChunk and TextureEnvChunk when
87 // reading it from an .osb
88 // The CubeTextureObjChunk will be returned by the getContainer function for
89 // this element.
91 void OSBCubeTextureChunkElement::read(const std::string &typeName)
93 OSG_OSB_LOG(("OSBCubeTextureChunkElement::read: [%s]\n", typeName.c_str()));
95 BinaryReadHandler *rh = editRoot()->getReadHandler();
97 UInt8 ptrTypeId;
98 UInt16 version;
100 rh->getValue(ptrTypeId);
101 rh->getValue(version );
103 OSG_OSB_LOG(("OSBCubeTextureChunkElement::read: version: [%u]\n", version));
105 // create the two replacement chunks
106 _pCubeTexObj = CubeTextureObjChunk::create();
107 _pTexEnv = TextureEnvChunk ::create();
109 std::string fieldName;
110 std::string fieldTypeName;
111 UInt32 fieldSize;
112 PtrFieldListIt ptrFieldIt;
114 while(readFieldHeader("", fieldName, fieldTypeName, fieldSize))
116 // some fields need to be duplicated for the two replacement chunks
117 if(fieldName == "parents")
119 // parent fields are ignored
120 rh->skip(fieldSize);
122 else if(fieldName == "internal")
124 bool fieldValue;
125 rh->getValue(fieldValue);
127 _pCubeTexObj->setInternal(fieldValue);
128 _pTexEnv ->setInternal(fieldValue);
130 else if(fieldName == "ignore")
132 bool fieldValue;
133 rh->getValue(fieldValue);
135 _pCubeTexObj->setIgnore(fieldValue);
136 _pTexEnv ->setIgnore(fieldValue);
138 else if(isTexObjField(fieldName))
140 // set TexObj as container for reading the field
141 setContainer(_pCubeTexObj);
142 readFieldContent(fieldName, fieldTypeName, fieldSize, "", ptrFieldIt);
144 else if(isTexEnvField(fieldName))
146 // set TexEnv as container for reading the field
147 setContainer(_pTexEnv);
148 readFieldContent(fieldName, fieldTypeName, fieldSize, "", ptrFieldIt);
150 else
152 FWARNING(("OSBCubeTextureChunkElement::read: Skipping unrecognized "
153 "field [%s].\n", fieldName.c_str()));
155 rh->skip(fieldSize);
159 // set TexObj as "the" container
160 setContainer(_pCubeTexObj);
163 // All uses of (i.e. pointers to) the TextureChunk need to be replaced with
164 // pointers to TexObj and TexEnv.
166 void
167 OSBCubeTextureChunkElement::postRead(void)
169 OSG_OSB_LOG(("OSBCubeTextureChunkElement::postRead:\n"));
170 OSBRootElement *pRoot = editRoot();
172 // for the id remapping TexEnv needs an id that is not used in the file
173 UInt32 texEnvIdFile = pRoot->getIdMap().rbegin()->first;
174 while(pRoot->getIdMap().find(texEnvIdFile) != pRoot->getIdMap().end())
176 ++texEnvIdFile;
179 // add mapping entry for TexEnv
180 pRoot->editIdMap()[texEnvIdFile] = _pTexEnv->getId();
182 PtrFieldListIt ptrFieldIt = pRoot->editPtrFieldList().begin();
183 PtrFieldListIt ptrFieldEnd = pRoot->editPtrFieldList().end ();
185 for(; ptrFieldIt != ptrFieldEnd; ++ptrFieldIt)
187 ChunkMaterial *chkMat =
188 dynamic_cast<ChunkMaterial *>(ptrFieldIt->getContainer());
190 if(chkMat != NULL)
192 // OSBChunkMaterialElement takes care of handling the two chunks
193 // replacing TextureChunk
194 continue;
196 else
198 UInt32 numIds = UInt32(ptrFieldIt->getIdStore ().size());
199 UInt32 numBind = UInt32(ptrFieldIt->getBindingStore().size());
201 if(numBind > 0)
203 // TextureChunk is pointed to from an attachment map
204 for(UInt32 i = 0; (i < numIds) && (i < numBind); ++i)
206 if(ptrFieldIt->getIdStore()[i] == getFCIdFile())
208 // insert a pointer to TexEnv right after the TexObj
209 ptrFieldIt->editIdStore().insert(
210 ptrFieldIt->editIdStore().begin() + i + 1,
211 texEnvIdFile );
213 // duplicate the binding of the TexObj
214 ptrFieldIt->editBindingStore().insert(
215 ptrFieldIt->editBindingStore().begin() + i + 1,
216 ptrFieldIt->getBindingStore()[i] );
218 ++numIds;
219 ++numBind;
223 else
225 for(UInt32 i = 0; i < numIds; ++i)
227 if(ptrFieldIt->getIdStore()[i] == getFCIdFile())
229 // insert a pointer to TexEnv right after the TexObj
230 ptrFieldIt->editIdStore().insert(
231 ptrFieldIt->editIdStore().begin() + i + 1,
232 texEnvIdFile );
234 ++numIds;
242 /*-------------------------------------------------------------------------*/
243 /* Writing */
245 void
246 OSBCubeTextureChunkElement::preWrite(FieldContainer * const fc)
248 OSG_OSB_LOG(("OSBCubeTextureChunkElement::preWrite\n"));
250 preWriteFieldContainer(fc, "");
253 void
254 OSBCubeTextureChunkElement::write(void)
256 OSG_OSB_LOG(("OSBCubeTextureChunkElement::write\n"));
258 if(getContainer() == NULL)
260 FWARNING((
261 "OSBCubeTextureChunkElement::write: Attempt to write NULL.\n"));
262 return;
265 BinaryWriteHandler *wh = editRoot()->getWriteHandler();
267 wh->putValue(getFCPtrType(getContainer()));
268 wh->putValue(getVersion() );
270 writeFields("", true);
274 bool
275 OSBCubeTextureChunkElement::isTexObjField(const std::string &fieldName) const
277 return CubeTextureObjChunk::getClassType().getFieldDesc(fieldName.c_str()) != NULL;
280 bool
281 OSBCubeTextureChunkElement::isTexEnvField(const std::string &fieldName) const
283 return TextureEnvChunk::getClassType().getFieldDesc(fieldName.c_str()) != NULL;