fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FileIO / OpenFlight / OSGOFDatabase.cpp
blob759fb67851a72918873c341d2dce4c7e41b7b3fc
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2008 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 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 #include "OSGOFDatabase.h"
41 #include "OSGNode.h"
42 #include "OSGOFPrimaryRecords.h"
43 #include "OSGOpenFlightLog.h"
45 OSG_BEGIN_NAMESPACE
47 OFDatabase::OFDatabase(void) :
48 _pHeader (NULL),
49 _sRecords ( ),
50 _pCurr (NULL),
51 _pCurrPrimary (NULL),
52 _pColorPalette (NULL),
53 _pVertexPalette (NULL),
54 _pTexturePalette (NULL),
55 _pMaterialPalette(NULL),
56 _targetUnits (VU_Meters),
57 _unitScale (1.f)
61 OFDatabase::~OFDatabase(void)
63 _pHeader = NULL;
64 _pColorPalette = NULL;
65 _pVertexPalette = NULL;
66 _pTexturePalette = NULL;
67 _pMaterialPalette = NULL;
70 bool OFDatabase::read(std::istream &is)
72 bool returnValue = true;
74 OFRecordHeader oRHeader;
76 returnValue = oRHeader.read(is);
78 if(returnValue == false)
79 return returnValue;
81 _pHeader = dynamic_pointer_cast<OFHeaderRecord>(
82 OFHeaderRecord::create(oRHeader, *this));
84 returnValue = _pHeader->read(is);
86 if(returnValue == false)
87 return returnValue;
89 _sRecords.push(_pHeader);
91 _pCurr = _pHeader;
92 _pCurrPrimary = _pHeader;
94 while(returnValue == true)
96 returnValue = oRHeader.read(is);
98 if(returnValue == false)
99 break;
101 if(oRHeader.sOpCode == OFContinuationOC)
103 if(_pCurr != NULL)
105 _pCurr->readContinue(is, oRHeader.sLength - 4);
107 else
109 FWARNING(("OFDatabase::read: Found ContinuationRecord, "
110 "without preceding record.\n"));
113 else
115 _pCurr = OFRecordFactory::the()->createRecord(oRHeader, *this);
117 if(_pCurr != NULL)
119 returnValue = _pCurr->read(is);
121 if(_pCurr->getCategory() == OFRecord::RC_Primary)
123 _sRecords.top()->addChild(_pCurr);
124 _pCurrPrimary = _pCurr;
126 else if(_pCurr->getCategory() == OFRecord::RC_Ancillary)
128 _pCurrPrimary->addChild(_pCurr);
131 else
133 FFATAL(("OFDatabase::read: Failed to create record for "
134 "[%u][%s].\n",
135 oRHeader.sOpCode,
136 OFRecord::getOpCodeString(oRHeader.sOpCode)));
141 _pCurr = NULL;
142 _pCurrPrimary = NULL;
144 _sRecords.pop();
146 if(_sRecords.empty() != true)
148 FWARNING(("OFDatabase::read: Record stack inconsistent.\n"));
151 return true;
154 NodeTransitPtr OFDatabase::convert(void)
156 NodeTransitPtr returnValue(NULL);
158 if(_pHeader == NULL)
159 return returnValue;
161 #ifndef OSG_OPENFLIGHT_SILENT
162 _pHeader->dump(0);
163 #endif
165 returnValue = _pHeader->convertToNode();
167 return returnValue;
170 void OFDatabase::pushLevel(void)
172 _sRecords.push(_pCurrPrimary);
175 void OFDatabase::popLevel(void)
177 _sRecords.pop();
179 _pCurrPrimary = _sRecords.top();
182 OFDatabase::VertexUnits OFDatabase::getTargetUnits(void) const
184 return _targetUnits;
187 void OFDatabase::setTargetUnits(VertexUnits units)
189 _targetUnits = units;
192 Real32 OFDatabase::getUnitScale(void) const
194 return _unitScale;
197 void OFDatabase::setUnitScale(Real32 scale)
199 _unitScale = scale;
202 const OFColorPaletteRecord *OFDatabase::getColorPalette(void)
204 if(_pColorPalette == NULL)
205 return NULL;
207 return _pColorPalette->getRecord();
210 const OFVertexPaletteRecord *OFDatabase::getVertexPalette(void)
212 if(_pVertexPalette == NULL)
213 return NULL;
215 return _pVertexPalette->getRecord();
218 const OFTexturePaletteRecord *OFDatabase::getTexRecord(UInt32 uiIdx)
220 if(_pTexturePalette == NULL)
221 return NULL;
223 return _pTexturePalette->getRecord(uiIdx);
226 const OFMaterialPaletteRecord *OFDatabase::getMatRecord(UInt32 uiIdx)
228 if(_pMaterialPalette == NULL)
229 return NULL;
231 return _pMaterialPalette->getRecord(uiIdx);
234 void OFDatabase::addColorPaletteRecord(OFColorPaletteRecord *pColPal)
236 if(_pColorPalette == NULL)
237 _pColorPalette = new OFColorPalette;
239 _pColorPalette->addRecord(pColPal);
242 void OFDatabase::addVertexPaletteRecord(OFVertexPaletteRecord *pVertPal)
244 if(_pVertexPalette == NULL)
245 _pVertexPalette = new OFVertexPalette;
247 _pVertexPalette->addRecord(pVertPal);
250 void OFDatabase::addTexturePaletteRecord(OFTexturePaletteRecord *pTexPal)
252 if(_pTexturePalette == NULL)
253 _pTexturePalette = new OFTexturePalette;
255 _pTexturePalette->addRecord(pTexPal);
258 void OFDatabase::addMaterialPaletteRecord(OFMaterialPaletteRecord *pMatPal)
260 if(_pMaterialPalette == NULL)
261 _pMaterialPalette = new OFMaterialPalette;
263 _pMaterialPalette->addRecord(pMatPal);
266 OSG_END_NAMESPACE