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 #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"
52 /*-------------------------------------------------------------------------*/
53 /* OSBChunkBlockElement */
54 /*-------------------------------------------------------------------------*/
56 /*-------------------------------------------------------------------------*/
59 OSBElementRegistrationHelper
<OSBChunkBlockElement
>
60 OSBChunkBlockElement::_regHelper
=
61 OSBElementRegistrationHelper
<OSBChunkBlockElement
>("ChunkBlock");
64 /*-------------------------------------------------------------------------*/
67 OSBChunkBlockElement::OSBChunkBlockElement(OSBRootElement
*root
) :
69 OSGOSBHeaderVersion200
),
71 _chunksPtrFieldIt ( ),
72 _chunksPtrFieldItValid(false )
76 /*-------------------------------------------------------------------------*/
79 OSBChunkBlockElement::~OSBChunkBlockElement(void)
83 /*-------------------------------------------------------------------------*/
86 void OSBChunkBlockElement::read(const std::string
&typeName
)
88 OSG_OSB_LOG(("OSBChunkBlockElement::read [%s]\n", typeName
.c_str()));
90 BinaryReadHandler
*rh
= editRoot()->getReadHandler();
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
;
103 PtrFieldListIt ptrFieldIt
;
105 ChunkBlockUnrecPtr pMat
= dynamic_pointer_cast
<ChunkBlock
>(
106 FieldContainerFactory::the()->createContainer(typeName
.c_str()));
110 while(readFieldHeader("", fieldName
, fieldTypeName
, fieldSize
))
112 if(fieldName
== "chunks")
114 // keep an interator to the _mfChunks field contents
115 readFieldContent(fieldName
, fieldTypeName
, fieldSize
, "",
118 _chunksPtrFieldItValid
= true;
122 readFieldContent(fieldName
, fieldTypeName
, fieldSize
, "",
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.
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());
142 if(_chunksPtrFieldItValid
== false)
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())
163 OSBElementBase
*chunkElem
= mapIt
->second
;
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 /*-------------------------------------------------------------------------*/
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"));
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"));
204 BinaryWriteHandler
*wh
= editRoot()->getWriteHandler();
206 wh
->putValue(getFCPtrType(getContainer()));
207 wh
->putValue(getVersion() );
209 ChunkBlock
*pCBlock
= dynamic_cast<ChunkBlock
*>(getContainer());
215 ChunkBlock::MFChunksType::const_iterator cIt
=
216 pCBlock
->getMFChunks()->begin();
217 ChunkBlock::MFChunksType::const_iterator cEnd
=
218 pCBlock
->getMFChunks()->end();
222 for(; cIt
!= cEnd
; ++cIt
, ++cIdx
)
226 UInt32 uiId
= (*cIt
)->getId();
227 Int32 iSlot
= cIdx
- (*cIt
)->getClassId();
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);