1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2008 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 "OSGOSBTextureChunkElement.h"
41 #include "OSGOSBRootElement.h"
43 #include "OSGChunkMaterial.h"
44 #include "OSGTextureObjChunk.h"
45 #include "OSGTextureEnvChunk.h"
49 /*-------------------------------------------------------------------------*/
50 /* OSBTextureChunkElement */
51 /*-------------------------------------------------------------------------*/
53 /*! Reads the depreciated TextureChunk from an osb and converts it to the
54 TextureObjChunk + TextureEnvChunk combination that is now the preferred
55 way to store the same information.
58 /*-------------------------------------------------------------------------*/
61 OSBElementRegistrationHelper
<OSBTextureChunkElement
>
62 OSBTextureChunkElement::_regHelper
=
63 OSBElementRegistrationHelper
<OSBTextureChunkElement
>("TextureChunk");
65 /*-------------------------------------------------------------------------*/
68 OSBTextureChunkElement::OSBTextureChunkElement(OSBRootElement
*root
) :
69 Inherited(root
, OSGOSBHeaderVersion200
),
75 /*-------------------------------------------------------------------------*/
78 OSBTextureChunkElement::~OSBTextureChunkElement(void)
82 /*-------------------------------------------------------------------------*/
85 // TextureChunk is being split into TextureObjChunk and TextureEnvChunk when
86 // reading it from an .osb
87 // The TextureObjChunk will be returned by the getContainer function for this
91 OSBTextureChunkElement::read(const std::string
&typeName
)
93 OSG_OSB_LOG(("OSBTextureChunkElement::read: [%s]\n", typeName
.c_str()));
95 BinaryReadHandler
*rh
= editRoot()->getReadHandler();
100 rh
->getValue(ptrTypeId
);
101 rh
->getValue(version
);
103 OSG_OSB_LOG(("OSBTextureChunkElement::read: version: [%u]\n", version
));
105 // create the two replacement chunks
106 _pTexObj
= TextureObjChunk::create();
107 _pTexEnv
= TextureEnvChunk::create();
109 std::string fieldName
;
110 std::string fieldTypeName
;
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
122 else if(fieldName
== "internal")
125 rh
->getValue(fieldValue
);
127 _pTexObj
->setInternal(fieldValue
);
128 _pTexEnv
->setInternal(fieldValue
);
130 else if(fieldName
== "ignore")
133 rh
->getValue(fieldValue
);
135 _pTexObj
->setIgnore(fieldValue
);
136 _pTexEnv
->setIgnore(fieldValue
);
138 else if(isTexObjField(fieldName
))
140 // set TexObj as container for reading the field
141 setContainer(_pTexObj
);
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
);
152 FWARNING(("OSBTextureChunkElement::read: Skipping unrecognized "
153 "field [%s].\n", fieldName
.c_str()));
159 // set TexObj as "the" container
160 setContainer(_pTexObj
);
163 // All uses of (i.e. pointers to) the TextureChunk need to be replaced with
164 // pointers to TexObj and TexEnv.
167 OSBTextureChunkElement::postRead(void)
169 OSG_OSB_LOG(("OSBTextureChunkElement::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())
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());
192 // OSBChunkMaterialElement takes care of handling the two chunks
193 // replacing TextureChunk
198 UInt32 numIds
= UInt32(ptrFieldIt
->getIdStore ().size());
199 UInt32 numBind
= UInt32(ptrFieldIt
->getBindingStore().size());
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,
213 // duplicate the binding of the TexObj
214 ptrFieldIt
->editBindingStore().insert(
215 ptrFieldIt
->editBindingStore().begin() + i
+ 1,
216 ptrFieldIt
->getBindingStore()[i
] );
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,
242 /*-------------------------------------------------------------------------*/
246 OSBTextureChunkElement::preWrite(FieldContainer
* const fc
)
248 OSG_OSB_LOG(("OSBTextureChunkElement::preWrite\n"));
250 preWriteFieldContainer(fc
, "");
254 OSBTextureChunkElement::write(void)
256 OSG_OSB_LOG(("OSBTextureChunkElement::write\n"));
258 if(getContainer() == NULL
)
260 FWARNING(("OSBTextureChunkElement::write: Attempt to write NULL.\n"));
264 BinaryWriteHandler
*wh
= editRoot()->getWriteHandler();
266 wh
->putValue(getFCPtrType(getContainer()));
267 wh
->putValue(getVersion() );
269 writeFields("", true);
274 OSBTextureChunkElement::isTexObjField(const std::string
&fieldName
) const
276 return TextureObjChunk::getClassType().getFieldDesc(fieldName
.c_str()) != NULL
;
280 OSBTextureChunkElement::isTexEnvField(const std::string
&fieldName
) const
282 return TextureEnvChunk::getClassType().getFieldDesc(fieldName
.c_str()) != NULL
;