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 "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"
52 /*-------------------------------------------------------------------------*/
53 /* OSBChunkMaterialElement */
54 /*-------------------------------------------------------------------------*/
56 /*-------------------------------------------------------------------------*/
59 OSBElementRegistrationHelper
<OSBChunkMaterialElement
>
60 OSBChunkMaterialElement::_regHelper
=
61 OSBElementRegistrationHelper
<OSBChunkMaterialElement
>("ChunkMaterial");
63 OSBElementRegistrationHelper
<OSBChunkMaterialElement
>
64 OSBChunkMaterialElement::_regHelperSimple
=
65 OSBElementRegistrationHelper
<OSBChunkMaterialElement
>("SimpleMaterial");
67 /*-------------------------------------------------------------------------*/
70 OSBChunkMaterialElement::OSBChunkMaterialElement(OSBRootElement
*root
) :
72 OSGOSBHeaderVersion200
),
74 _chunksPtrFieldIt ( ),
75 _chunksPtrFieldItValid(false )
79 /*-------------------------------------------------------------------------*/
82 OSBChunkMaterialElement::~OSBChunkMaterialElement(void)
86 /*-------------------------------------------------------------------------*/
90 OSBChunkMaterialElement::read(const std::string
&typeName
)
92 OSG_OSB_LOG(("OSBChunkMaterialElement::read [%s]\n", typeName
.c_str()));
94 BinaryReadHandler
*rh
= editRoot()->getReadHandler();
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
;
107 PtrFieldListIt ptrFieldIt
;
109 ChunkMaterialUnrecPtr pMat
= dynamic_pointer_cast
<ChunkMaterial
>(
110 FieldContainerFactory::the()->createContainer(typeName
.c_str()));
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
, "",
128 _chunksPtrFieldItValid
= true;
132 readFieldContent(fieldName
, fieldTypeName
, fieldSize
, "",
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.
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());
154 if(_chunksPtrFieldItValid
== false)
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())
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
)
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
]);
189 chkMat
->addChunk(texObj
);
190 chkMat
->addChunk(texEnv
);
193 else if(cubeTexChunkElem
!= NULL
)
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
]);
208 chkMat
->addChunk(cubeTexObj
);
209 chkMat
->addChunk(texEnv
);
215 StateChunk
*chunk
= dynamic_cast<StateChunk
*>(chunkElem
->getContainer());
217 if(i
< _mfSlots
.size())
219 chkMat
->addChunk(chunk
, _mfSlots
[i
]);
223 chkMat
->addChunk(chunk
);
228 // pointer mapping is already done here for _mfChunks, clear info
229 _chunksPtrFieldIt
->editIdStore ().clear();
230 _chunksPtrFieldIt
->editBindingStore().clear();
233 /*-------------------------------------------------------------------------*/
237 OSBChunkMaterialElement::preWrite(FieldContainer
* const fc
)
239 OSG_OSB_LOG(("OSBChunkMaterialElement::preWrite\n"));
241 preWriteFieldContainer(fc
, "");
245 OSBChunkMaterialElement::write(void)
247 OSG_OSB_LOG(("OSBChunkMaterialElement::write\n"));
249 if(getContainer() == NULL
)
251 FWARNING(("OSBChunkMaterialElement::write: Attempt to write NULL.\n"));
255 BinaryWriteHandler
*wh
= editRoot()->getWriteHandler();
257 wh
->putValue(getFCPtrType(getContainer()));
258 wh
->putValue(getVersion() );
260 writeFields("", true);