fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FileIO / OSB / OSGOSBChunkMaterialElement.cpp
blob3d73a6c3d2a0057cd9a2475f4964431316e36f63
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 #include "OSGOSBChunkMaterialElement.h"
41 #include "OSGOSBRootElement.h"
42 #include "OSGChunkMaterial.h"
44 #include "OSGOSBTextureChunkElement.h"
45 #include "OSGOSBCubeTextureChunkElement.h"
46 #include "OSGTextureObjChunk.h"
47 #include "OSGCubeTextureObjChunk.h"
48 #include "OSGTextureEnvChunk.h"
50 OSG_USING_NAMESPACE
52 /*-------------------------------------------------------------------------*/
53 /* OSBChunkMaterialElement */
54 /*-------------------------------------------------------------------------*/
56 /*-------------------------------------------------------------------------*/
57 /* Static members */
59 OSBElementRegistrationHelper<OSBChunkMaterialElement>
60 OSBChunkMaterialElement::_regHelper =
61 OSBElementRegistrationHelper<OSBChunkMaterialElement>("ChunkMaterial");
63 OSBElementRegistrationHelper<OSBChunkMaterialElement>
64 OSBChunkMaterialElement::_regHelperSimple =
65 OSBElementRegistrationHelper<OSBChunkMaterialElement>("SimpleMaterial");
67 /*-------------------------------------------------------------------------*/
68 /* Constructor */
70 OSBChunkMaterialElement::OSBChunkMaterialElement(OSBRootElement *root) :
71 Inherited (root,
72 OSGOSBHeaderVersion200),
73 _mfSlots ( ),
74 _chunksPtrFieldIt ( ),
75 _chunksPtrFieldItValid(false )
79 /*-------------------------------------------------------------------------*/
80 /* Destructor */
82 OSBChunkMaterialElement::~OSBChunkMaterialElement(void)
86 /*-------------------------------------------------------------------------*/
87 /* Reading */
89 void
90 OSBChunkMaterialElement::read(const std::string &typeName)
92 OSG_OSB_LOG(("OSBChunkMaterialElement::read [%s]\n", typeName.c_str()));
94 BinaryReadHandler *rh = editRoot()->getReadHandler();
96 UInt8 ptrTypeId;
97 UInt16 version;
99 rh->getValue(ptrTypeId);
100 rh->getValue(version );
102 OSG_OSB_LOG(("OSBChunkMaterialElement::read: version: [%u]\n", version));
104 std::string fieldName;
105 std::string fieldTypeName;
106 UInt32 fieldSize;
107 PtrFieldListIt ptrFieldIt;
109 ChunkMaterialUnrecPtr pMat = dynamic_pointer_cast<ChunkMaterial>(
110 FieldContainerFactory::the()->createContainer(typeName.c_str()));
112 setContainer(pMat);
114 while(readFieldHeader("", fieldName, fieldTypeName, fieldSize))
116 if(fieldName == "slots")
118 // read slots field into separate field - the real field gets
119 // filled in postRead
120 _mfSlots.copyFromBin(*rh);
122 else if(fieldName == "chunks")
124 // keep an interator to the _mfChunks field contents
125 readFieldContent(fieldName, fieldTypeName, fieldSize, "",
126 _chunksPtrFieldIt);
128 _chunksPtrFieldItValid = true;
130 else
132 readFieldContent(fieldName, fieldTypeName, fieldSize, "",
133 ptrFieldIt);
138 void
139 OSBChunkMaterialElement::postRead(void)
141 // _mfChunks and _mfSlots have to be kept consistent for ChunkMaterial.
142 // Also TextureChunk is split into TextureObjChunk and TextureObjChunk
143 // on load, so the slot info has to be duplicated.
144 // TODO:
145 // It would be better if that handling could be confined to
146 // TextureChunkElement, but I've not found a good way to do that and keep
147 // the information in _mfSlots correct. -- cneumann
149 const OSBRootElement *root = getRoot();
150 ChunkMaterial *chkMat =
151 dynamic_cast<ChunkMaterial *>(getContainer());
153 // No chunks loaded
154 if(_chunksPtrFieldItValid == false)
155 return;
157 PtrFieldInfo::PtrIdStoreConstIt idIt =
158 _chunksPtrFieldIt->getIdStore().begin();
159 PtrFieldInfo::PtrIdStoreConstIt idEnd =
160 _chunksPtrFieldIt->getIdStore().end ();
162 for(UInt32 i = 0; idIt != idEnd; ++idIt, ++i)
164 OSBRootElement::IdElemMapConstIt mapIt =
165 root->getIdElemMap().find(*idIt);
167 if(mapIt == root->getIdElemMap().end())
168 continue;
170 OSBElementBase *chunkElem = mapIt->second;
171 OSBTextureChunkElement *texChunkElem =
172 dynamic_cast<OSBTextureChunkElement *>(chunkElem);
173 OSBCubeTextureChunkElement *cubeTexChunkElem =
174 dynamic_cast<OSBCubeTextureChunkElement *>(chunkElem);
176 if(texChunkElem != NULL)
178 // TextureChunk
179 TextureObjChunk *texObj = texChunkElem->getTexObjChunk();
180 TextureEnvChunk *texEnv = texChunkElem->getTexEnvChunk();
182 if(i < _mfSlots.size())
184 chkMat->addChunk(texObj, _mfSlots[i]);
185 chkMat->addChunk(texEnv, _mfSlots[i]);
187 else
189 chkMat->addChunk(texObj);
190 chkMat->addChunk(texEnv);
193 else if(cubeTexChunkElem != NULL)
195 // TextureChunk
196 CubeTextureObjChunk *cubeTexObj =
197 cubeTexChunkElem->getCubeTexObjChunk();
198 TextureEnvChunk *texEnv =
199 cubeTexChunkElem->getTexEnvChunk();
201 if(i < _mfSlots.size())
203 chkMat->addChunk(cubeTexObj, _mfSlots[i]);
204 chkMat->addChunk(texEnv, _mfSlots[i]);
206 else
208 chkMat->addChunk(cubeTexObj);
209 chkMat->addChunk(texEnv );
212 else
214 // other chunk
215 StateChunk *chunk = dynamic_cast<StateChunk *>(chunkElem->getContainer());
217 if(i < _mfSlots.size())
219 chkMat->addChunk(chunk, _mfSlots[i]);
221 else
223 chkMat->addChunk(chunk);
228 // pointer mapping is already done here for _mfChunks, clear info
229 _chunksPtrFieldIt->editIdStore ().clear();
230 _chunksPtrFieldIt->editBindingStore().clear();
233 /*-------------------------------------------------------------------------*/
234 /* Writing */
236 void
237 OSBChunkMaterialElement::preWrite(FieldContainer * const fc)
239 OSG_OSB_LOG(("OSBChunkMaterialElement::preWrite\n"));
241 preWriteFieldContainer(fc, "");
244 void
245 OSBChunkMaterialElement::write(void)
247 OSG_OSB_LOG(("OSBChunkMaterialElement::write\n"));
249 if(getContainer() == NULL)
251 FWARNING(("OSBChunkMaterialElement::write: Attempt to write NULL.\n"));
252 return;
255 BinaryWriteHandler *wh = editRoot()->getWriteHandler();
257 wh->putValue(getFCPtrType(getContainer()));
258 wh->putValue(getVersion() );
260 writeFields("", true);