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 "OSGOSBCubeTextureChunkElement.h"
41 #include "OSGOSBRootElement.h"
43 #include "OSGChunkMaterial.h"
44 #include "OSGCubeTextureObjChunk.h"
45 #include "OSGTextureEnvChunk.h"
49 /*-------------------------------------------------------------------------*/
50 /* OSBCubeTextureChunkElement */
51 /*-------------------------------------------------------------------------*/
53 /*! Reads the depreciated TextureChunk from an osb and converts it to the
54 CubeTextureObjChunk + TextureEnvChunk combination that is now the preferred
55 way to store the same information.
58 /*-------------------------------------------------------------------------*/
61 OSBElementRegistrationHelper
<OSBCubeTextureChunkElement
>
62 OSBCubeTextureChunkElement::_regHelper
=
63 OSBElementRegistrationHelper
<OSBCubeTextureChunkElement
>(
66 /*-------------------------------------------------------------------------*/
69 OSBCubeTextureChunkElement::OSBCubeTextureChunkElement(OSBRootElement
*root
) :
70 Inherited (root
, OSGOSBHeaderVersion200
),
76 /*-------------------------------------------------------------------------*/
79 OSBCubeTextureChunkElement::~OSBCubeTextureChunkElement(void)
83 /*-------------------------------------------------------------------------*/
86 // TextureChunk is being split into CubeTextureObjChunk and TextureEnvChunk when
87 // reading it from an .osb
88 // The CubeTextureObjChunk will be returned by the getContainer function for
91 void OSBCubeTextureChunkElement::read(const std::string
&typeName
)
93 OSG_OSB_LOG(("OSBCubeTextureChunkElement::read: [%s]\n", typeName
.c_str()));
95 BinaryReadHandler
*rh
= editRoot()->getReadHandler();
100 rh
->getValue(ptrTypeId
);
101 rh
->getValue(version
);
103 OSG_OSB_LOG(("OSBCubeTextureChunkElement::read: version: [%u]\n", version
));
105 // create the two replacement chunks
106 _pCubeTexObj
= CubeTextureObjChunk::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 _pCubeTexObj
->setInternal(fieldValue
);
128 _pTexEnv
->setInternal(fieldValue
);
130 else if(fieldName
== "ignore")
133 rh
->getValue(fieldValue
);
135 _pCubeTexObj
->setIgnore(fieldValue
);
136 _pTexEnv
->setIgnore(fieldValue
);
138 else if(isTexObjField(fieldName
))
140 // set TexObj as container for reading the field
141 setContainer(_pCubeTexObj
);
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(("OSBCubeTextureChunkElement::read: Skipping unrecognized "
153 "field [%s].\n", fieldName
.c_str()));
159 // set TexObj as "the" container
160 setContainer(_pCubeTexObj
);
163 // All uses of (i.e. pointers to) the TextureChunk need to be replaced with
164 // pointers to TexObj and TexEnv.
167 OSBCubeTextureChunkElement::postRead(void)
169 OSG_OSB_LOG(("OSBCubeTextureChunkElement::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 OSBCubeTextureChunkElement::preWrite(FieldContainer
* const fc
)
248 OSG_OSB_LOG(("OSBCubeTextureChunkElement::preWrite\n"));
250 preWriteFieldContainer(fc
, "");
254 OSBCubeTextureChunkElement::write(void)
256 OSG_OSB_LOG(("OSBCubeTextureChunkElement::write\n"));
258 if(getContainer() == NULL
)
261 "OSBCubeTextureChunkElement::write: Attempt to write NULL.\n"));
265 BinaryWriteHandler
*wh
= editRoot()->getWriteHandler();
267 wh
->putValue(getFCPtrType(getContainer()));
268 wh
->putValue(getVersion() );
270 writeFields("", true);
275 OSBCubeTextureChunkElement::isTexObjField(const std::string
&fieldName
) const
277 return CubeTextureObjChunk::getClassType().getFieldDesc(fieldName
.c_str()) != NULL
;
281 OSBCubeTextureChunkElement::isTexEnvField(const std::string
&fieldName
) const
283 return TextureEnvChunk::getClassType().getFieldDesc(fieldName
.c_str()) != NULL
;