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 "OSGOSBMaterialPoolElement.h"
41 #include "OSGOSBRootElement.h"
47 /*-------------------------------------------------------------------------*/
48 /* OSBMaterialPoolElement */
49 /*-------------------------------------------------------------------------*/
51 /*! Reads the removed MaterialPool from an osb and converts it to a Group
52 with a ContainerCollection attachment.
55 /*-------------------------------------------------------------------------*/
58 OSBElementRegistrationHelper
<OSBMaterialPoolElement
>
59 OSBMaterialPoolElement::_regHelper
=
60 OSBElementRegistrationHelper
<OSBMaterialPoolElement
>("MaterialPool");
62 /*-------------------------------------------------------------------------*/
65 OSBMaterialPoolElement::OSBMaterialPoolElement(OSBRootElement
*root
) :
67 Inherited (root
, OSGOSBHeaderVersion200
),
72 /*-------------------------------------------------------------------------*/
75 OSBMaterialPoolElement::~OSBMaterialPoolElement(void)
79 /*-------------------------------------------------------------------------*/
83 OSBMaterialPoolElement::read(const std::string
&typeName
)
85 OSG_OSB_LOG(("OSBMaterialPoolElement::read: [%s]\n", typeName
.c_str()));
87 BinaryReadHandler
*rh
= editRoot()->getReadHandler();
92 rh
->getValue(ptrTypeId
);
93 rh
->getValue(version
);
95 GroupUnrecPtr group
= Group ::create();
96 _pCollection
= ContainerCollection::create();
100 // set attachment on the Group
101 group
->addAttachment(_pCollection
);
102 _pCollection
->setName ("MaterialPool");
104 std::string fieldName
;
105 std::string fieldTypeName
;
107 PtrFieldListIt ptrFieldIt
;
109 while(readFieldHeader("", fieldName
, fieldTypeName
, fieldSize
))
111 if(fieldName
== "materials")
113 // materials from MaterialPool need to be stored in the
114 // ContainerCollection
115 readMaterialsField();
119 // all other fields can be read directly
120 readFieldContent(fieldName
, fieldTypeName
,
121 fieldSize
, "", ptrFieldIt
);
127 OSBMaterialPoolElement::postRead(void)
129 Inherited::postRead();
132 /*-------------------------------------------------------------------------*/
136 OSBMaterialPoolElement::preWrite(FieldContainer
* const fc
)
138 OSG_OSB_LOG(("OSBMaterialPoolElement::preWrite\n"));
140 preWriteFieldContainer(fc
, "");
144 OSBMaterialPoolElement::write(void)
146 OSG_OSB_LOG(("OSBMaterialPoolElement::write\n"));
148 if(getContainer() == NULL
)
150 FWARNING(("OSBMaterialPoolElement::write: Attempt to write NULL.\n"));
154 BinaryWriteHandler
*wh
= editRoot()->getWriteHandler();
156 wh
->putValue(getFCPtrType(getContainer()));
157 wh
->putValue(getVersion() );
159 writeFields("", true);
162 /*! Reads MFMaterials from MaterialPool and stores the pointer ids so that
163 they get put into ContainerCollection MFContainers.
166 OSBMaterialPoolElement::readMaterialsField(void)
168 OSG_OSB_LOG(("OSBMaterialPoolElement::readMaterialsField\n"));
172 OSBRootElement
*root
= editRoot();
173 BinaryReadHandler
*rh
= editRoot()->getReadHandler();
175 FieldDescriptionBase
*contFieldDesc
=
176 _pCollection
->getFieldDescription("containers");
177 UInt32 contFieldId
= contFieldDesc
->getFieldId();
179 root
->editPtrFieldList().push_back(PtrFieldInfo(_pCollection
,
181 PtrFieldInfo
&pfi
= root
->editPtrFieldList().back();
183 rh
->getValue(numElements
);
185 for(UInt32 i
= 0; i
< numElements
; ++i
)
188 pfi
.editIdStore().push_back(ptrId
);