changed: gcc8 base update
[opensg.git] / Source / System / FileIO / OSB / OSGOSBChunkBlockElement.cpp
blob3873d50f4e0936a47524afd0364bbee60b3081c8
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 "OSGOSBChunkBlockElement.h"
41 #include "OSGOSBRootElement.h"
42 #include "OSGChunkBlock.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 /* OSBChunkBlockElement */
54 /*-------------------------------------------------------------------------*/
56 /*-------------------------------------------------------------------------*/
57 /* Static members */
59 OSBElementRegistrationHelper<OSBChunkBlockElement>
60 OSBChunkBlockElement::_regHelper =
61 OSBElementRegistrationHelper<OSBChunkBlockElement>("ChunkBlock");
64 /*-------------------------------------------------------------------------*/
65 /* Constructor */
67 OSBChunkBlockElement::OSBChunkBlockElement(OSBRootElement *root) :
68 Inherited (root,
69 OSGOSBHeaderVersion200),
70 _mfSlots ( ),
71 _chunksPtrFieldIt ( ),
72 _chunksPtrFieldItValid(false )
76 /*-------------------------------------------------------------------------*/
77 /* Destructor */
79 OSBChunkBlockElement::~OSBChunkBlockElement(void)
83 /*-------------------------------------------------------------------------*/
84 /* Reading */
86 void OSBChunkBlockElement::read(const std::string &typeName)
88 OSG_OSB_LOG(("OSBChunkBlockElement::read [%s]\n", typeName.c_str()));
90 BinaryReadHandler *rh = editRoot()->getReadHandler();
92 UInt8 ptrTypeId;
93 UInt16 version;
95 rh->getValue(ptrTypeId);
96 rh->getValue(version );
98 OSG_OSB_LOG(("OSBChunkBlockElement::read: version: [%u]\n", version));
100 std::string fieldName;
101 std::string fieldTypeName;
102 UInt32 fieldSize;
103 PtrFieldListIt ptrFieldIt;
105 ChunkBlockUnrecPtr pMat = dynamic_pointer_cast<ChunkBlock>(
106 FieldContainerFactory::the()->createContainer(typeName.c_str()));
108 setContainer(pMat);
110 while(readFieldHeader("", fieldName, fieldTypeName, fieldSize))
112 if(fieldName == "chunks")
114 // keep an interator to the _mfChunks field contents
115 readFieldContent(fieldName, fieldTypeName, fieldSize, "",
116 _chunksPtrFieldIt);
118 _chunksPtrFieldItValid = true;
120 else
122 readFieldContent(fieldName, fieldTypeName, fieldSize, "",
123 ptrFieldIt);
128 void OSBChunkBlockElement::postRead(void)
130 // _mfChunks and _mfSlots have to be kept consistent for ChunkBlock.
131 // Also TextureChunk is split into TextureObjChunk and TextureObjChunk
132 // on load, so the slot info has to be duplicated.
133 // TODO:
134 // It would be better if that handling could be confined to
135 // TextureChunkElement, but I've not found a good way to do that and keep
136 // the information in _mfSlots correct. -- cneumann
138 const OSBRootElement *root = getRoot();
139 ChunkBlock *chkMat = dynamic_cast<ChunkBlock *>(getContainer());
141 // No chunks loaded
142 if(_chunksPtrFieldItValid == false)
143 return;
145 PtrFieldInfo::PtrIdStoreConstIt idIt =
146 _chunksPtrFieldIt->getIdStore().begin();
147 PtrFieldInfo::PtrIdStoreConstIt idEnd =
148 _chunksPtrFieldIt->getIdStore().end ();
150 for(UInt32 i = 0; idIt != idEnd; ++idIt, ++i)
152 UInt32 uiId = (*idIt) & 0x00FFFFFF;
153 UInt32 uiSlot = ((*idIt) & 0xFF000000) >> 24;
155 OSBRootElement::IdElemMapConstIt mapIt =
156 root->getIdElemMap().find(uiId);
158 if(mapIt == root->getIdElemMap().end())
160 continue;
163 OSBElementBase *chunkElem = mapIt->second;
165 // other chunk
166 StateChunk *chunk =
167 dynamic_cast<StateChunk *>(chunkElem->getContainer());
170 chkMat->addChunk(chunk, uiSlot);
173 // pointer mapping is already done here for _mfChunks, clear info
174 _chunksPtrFieldIt->editIdStore ().clear();
175 _chunksPtrFieldIt->editBindingStore().clear();
178 /*-------------------------------------------------------------------------*/
179 /* Writing */
181 void OSBChunkBlockElement::preWrite(FieldContainer * const fc)
183 OSG_OSB_LOG(("OSBChunkBlockElement::preWrite\n"));
185 if(getContainer() == NULL || fc == NULL)
187 FWARNING(("OSBChunkBlockElement::prewrite: Attempt to write NULL.\n"));
188 return;
191 preWriteFieldContainer(fc, "");
194 void OSBChunkBlockElement::write(void)
196 OSG_OSB_LOG(("OSBChunkBlockElement::write\n"));
198 if(getContainer() == NULL)
200 FWARNING(("OSBChunkBlockElement::write: Attempt to write NULL.\n"));
201 return;
204 BinaryWriteHandler *wh = editRoot()->getWriteHandler();
206 wh->putValue(getFCPtrType(getContainer()));
207 wh->putValue(getVersion() );
209 ChunkBlock *pCBlock = dynamic_cast<ChunkBlock *>(getContainer());
211 if(pCBlock != NULL)
213 _mfSlots.clear();
215 ChunkBlock::MFChunksType::const_iterator cIt =
216 pCBlock->getMFChunks()->begin();
217 ChunkBlock::MFChunksType::const_iterator cEnd =
218 pCBlock->getMFChunks()->end();
220 UInt32 cIdx = 0;
222 for(; cIt != cEnd; ++cIt, ++cIdx)
224 if(*cIt != NULL)
226 UInt32 uiId = (*cIt)->getId();
227 Int32 iSlot = cIdx - (*cIt)->getClassId();
229 if(iSlot != -1)
231 uiId |= iSlot << 24;
234 _mfSlots.push_back(uiId);
238 writeFieldHeader("chunks",
239 "MFUnrecStateChunkPtr",
240 _mfSlots.size() * sizeof(UInt32));
242 wh = editRoot()->getWriteHandler();
244 _mfSlots.copyToBin(*wh);
247 writeFields("'chunks'", true);