fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FileIO / OSB / OSGOSBSHLChunkElement.cpp
blob41fe9ba9db8ef9bd1e43a2f75142e4aad48d8ad7
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2013 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
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. *
18 * *
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. *
23 * *
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. *
27 * *
28 \*---------------------------------------------------------------------------*/
30 #include "OSGOSBSHLChunkElement.h"
32 #include "OSGOSBRootElement.h"
33 #include "OSGOSBShaderParameterBoolElement.h"
34 #include "OSGOSBShaderParameterIntElement.h"
35 #include "OSGOSBShaderParameterMatrixElement.h"
36 #include "OSGOSBShaderParameterMIntElement.h"
37 #include "OSGOSBShaderParameterMRealElement.h"
38 #include "OSGOSBShaderParameterMVec2fElement.h"
39 #include "OSGOSBShaderParameterMVec3fElement.h"
40 #include "OSGOSBShaderParameterMVec4fElement.h"
41 #include "OSGOSBShaderParameterRealElement.h"
42 #include "OSGOSBShaderParameterVec2fElement.h"
43 #include "OSGOSBShaderParameterVec3fElement.h"
44 #include "OSGOSBShaderParameterVec4fElement.h"
46 #include "OSGSimpleSHLChunk.h"
48 OSG_BEGIN_NAMESPACE
50 /*-------------------------------------------------------------------------*/
51 /* OSBSHLChunkElement */
52 /*-------------------------------------------------------------------------*/
54 /*! Reads the SHLChunk from OpenSG 1.x and converts it to a SimpleSHLChunk.
57 /*-------------------------------------------------------------------------*/
58 /* Static members */
60 OSBElementRegistrationHelper<OSBSHLChunkElement>
61 OSBSHLChunkElement::_regHelper =
62 OSBElementRegistrationHelper<OSBSHLChunkElement>("SHLChunk");
64 /*-------------------------------------------------------------------------*/
65 /* Constructor */
67 OSBSHLChunkElement::OSBSHLChunkElement(OSBRootElement *root) :
68 Inherited(root, OSGOSBHeaderVersion200),
69 _pSHLChunk(NULL ),
70 _paramIds ( )
74 /*-------------------------------------------------------------------------*/
75 /* Destructor */
77 OSBSHLChunkElement::~OSBSHLChunkElement(void)
81 /*-------------------------------------------------------------------------*/
82 /* Reading */
84 void
85 OSBSHLChunkElement::read(const std::string &typeName)
87 OSG_OSB_LOG(("OSBSHLChunkElement::read: [%s]\n", typeName.c_str()));
89 BinaryReadHandler *rh = editRoot()->getReadHandler();
91 UInt8 ptrTypeId;
92 UInt16 version;
94 rh->getValue(ptrTypeId);
95 rh->getValue(version );
97 OSG_OSB_LOG(("OSBSHLChunkElement::read: version: [%u]\n", version));
99 // create replacement chunk
100 _pSHLChunk = SimpleSHLChunk::create();
101 setContainer(_pSHLChunk);
103 std::string fieldName;
104 std::string fieldTypeName;
105 UInt32 fieldSize;
106 PtrFieldListIt ptrFieldIt;
108 MFGLenum paramNames;
109 MFUInt32 paramValues;
111 while(readFieldHeader("", fieldName, fieldTypeName, fieldSize))
113 if(isSimpleSHLField(fieldName))
115 readFieldContent(fieldName, fieldTypeName, fieldSize, "", ptrFieldIt);
117 else if(fieldName == "parameters")
119 UInt32 ptrId;
120 UInt32 numElements;
122 rh->getValue(numElements);
123 _paramIds.resize(numElements);
125 for(UInt32 i = 0; i < numElements; ++i)
127 rh->getValue(ptrId);
128 _paramIds[i] = ptrId;
131 else if(fieldName == "programParameterNames")
133 paramNames.copyFromBin(*rh);
135 else if(fieldName == "programParameterValues")
137 paramValues.copyFromBin(*rh);
139 else
141 OSG_OSB_LOG(("Skipping [%d] bytes for field [%s]\n",
142 fieldSize, fieldName.c_str()));
143 rh->skip(fieldSize);
147 // handle paramNames, paramValues
148 if(paramNames.empty() == false)
150 if(paramNames.size() != paramValues.size())
152 FWARNING(("OSBSHLChunkElement::read: Fields "
153 "'MFProgramParamterNamse' and 'MFProgramParameterValues' "
154 "have inconsistent sizes.\n"));
156 else
158 for(UInt32 i = 0; i < paramNames.size(); ++i)
160 _pSHLChunk->setProgramParameter(paramNames[i], paramValues[i]);
166 void
167 OSBSHLChunkElement::postRead(void)
169 OSG_OSB_LOG(("OSBSHLChunkElement::postRead:\n"));
171 const OSBRootElement::IdElemMap& elemMap = getRoot()->getIdElemMap();
173 for(UInt32 i = 0; i < _paramIds.size(); ++i)
175 OSBRootElement::IdElemMapConstIt elemIt = elemMap.find(_paramIds[i]);
177 if(elemIt != elemMap.end())
179 handleParameterElem(elemIt->second);
181 else
183 FWARNING(("No entry in IdElemMap for id [%d]\n", _paramIds[i]));
188 /*-------------------------------------------------------------------------*/
189 /* Writing */
191 void
192 OSBSHLChunkElement::preWrite(FieldContainer * const)
194 // This can not be called, SHLChunk does not exist
195 OSG_ASSERT(false);
198 void
199 OSBSHLChunkElement::write(void)
201 // This can not be called, SHLChunk does not exist
202 OSG_ASSERT(false);
205 bool
206 OSBSHLChunkElement::isSimpleSHLField(const std::string& fieldName) const
208 return SimpleSHLChunk::getClassType().getFieldDesc(fieldName.c_str()) != NULL;
211 void
212 OSBSHLChunkElement::handleParameterElem(OSBElementBase* elem)
214 OSBShaderParameterElement* shElem =
215 dynamic_cast<OSBShaderParameterElement*>(elem);
217 if(shElem != NULL)
219 OSBShaderParameterBoolElement* shElemBool =
220 dynamic_cast<OSBShaderParameterBoolElement*>(shElem);
221 OSBShaderParameterIntElement* shElemInt =
222 dynamic_cast<OSBShaderParameterIntElement*>(shElem);
223 OSBShaderParameterMatrixElement* shElemMatrix =
224 dynamic_cast<OSBShaderParameterMatrixElement*>(shElem);
225 OSBShaderParameterMIntElement* shElemMInt =
226 dynamic_cast<OSBShaderParameterMIntElement*>(shElem);
227 OSBShaderParameterMRealElement* shElemMReal =
228 dynamic_cast<OSBShaderParameterMRealElement*>(shElem);
229 OSBShaderParameterMVec2fElement* shElemMVec2f =
230 dynamic_cast<OSBShaderParameterMVec2fElement*>(shElem);
231 OSBShaderParameterMVec3fElement* shElemMVec3f =
232 dynamic_cast<OSBShaderParameterMVec3fElement*>(shElem);
233 OSBShaderParameterMVec4fElement* shElemMVec4f =
234 dynamic_cast<OSBShaderParameterMVec4fElement*>(shElem);
235 OSBShaderParameterRealElement* shElemReal =
236 dynamic_cast<OSBShaderParameterRealElement*>(shElem);
237 OSBShaderParameterVec2fElement* shElemVec2f =
238 dynamic_cast<OSBShaderParameterVec2fElement*>(shElem);
239 OSBShaderParameterVec3fElement* shElemVec3f =
240 dynamic_cast<OSBShaderParameterVec3fElement*>(shElem);
241 OSBShaderParameterVec4fElement* shElemVec4f =
242 dynamic_cast<OSBShaderParameterVec4fElement*>(shElem);
244 if(shElemBool != NULL)
246 _pSHLChunk->addUniformVariable<bool>(shElemBool->getName (),
247 shElemBool->getValue() );
249 else if(shElemInt != NULL)
251 _pSHLChunk->addUniformVariable<Int32>(shElemInt->getName (),
252 shElemInt->getValue() );
254 else if(shElemMatrix != NULL)
256 _pSHLChunk->addUniformVariable<Matrix>(shElemMatrix->getName (),
257 shElemMatrix->getValue() );
259 else if(shElemMInt != NULL)
261 _pSHLChunk->addUniformVariable<MFInt32>(shElemMInt->getName (),
262 shElemMInt->getValue() );
264 else if(shElemMReal != NULL)
266 _pSHLChunk->addUniformVariable<MFReal32>(shElemMReal->getName (),
267 shElemMReal->getValue() );
269 else if(shElemMVec2f != NULL)
271 _pSHLChunk->addUniformVariable<MFVec2f>(shElemMVec2f->getName (),
272 shElemMVec2f->getValue() );
274 else if(shElemMVec3f != NULL)
276 _pSHLChunk->addUniformVariable<MFVec3f>(shElemMVec3f->getName (),
277 shElemMVec3f->getValue() );
279 else if(shElemMVec4f != NULL)
281 _pSHLChunk->addUniformVariable<MFVec4f>(shElemMVec4f->getName (),
282 shElemMVec4f->getValue() );
284 else if(shElemReal != NULL)
286 _pSHLChunk->addUniformVariable<Real32>(shElemReal->getName (),
287 shElemReal->getValue() );
289 else if(shElemVec2f != NULL)
291 _pSHLChunk->addUniformVariable<Vec2f>(shElemVec2f->getName (),
292 shElemVec2f->getValue() );
294 else if(shElemVec3f != NULL)
296 _pSHLChunk->addUniformVariable<Vec3f>(shElemVec3f->getName (),
297 shElemVec3f->getValue() );
299 else if(shElemVec4f != NULL)
301 _pSHLChunk->addUniformVariable<Vec4f>(shElemVec4f->getName (),
302 shElemVec4f->getValue() );
304 else
306 FWARNING(("OSBSHLChunkElement::handleParameterElem: "
307 "Unknown parameter type for parameter named '%s'\n",
308 shElem->getName()));
313 OSG_END_NAMESPACE