fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FileIO / OpenFlight / OSGOFPrimaryRecords.cpp
blob2919c7049d1d29925b18285232d19fdb471f1e26
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2011 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 "OSGOFPrimaryRecords.h"
32 #include "OSGBlendChunk.h"
33 #include "OSGChunkMaterial.h"
34 #include "OSGDepthChunk.h"
35 #include "OSGGeometry.h"
36 #include "OSGGeoFunctions.h"
37 #include "OSGGroup.h"
38 #include "OSGPolygonChunk.h"
39 #include "OSGRangeLOD.h"
40 #include "OSGSceneFileHandler.h"
41 #include "OSGTextureEnvChunk.h"
43 #include "OSGOFAncillaryRecords.h"
44 #include "OSGOFDatabase.h"
45 #include "OSGOpenFlightSceneFileType.h"
46 #include "OSGOpenFlightLog.h"
49 OSG_BEGIN_NAMESPACE
51 // constants for Faces and Meshes
52 namespace
54 // Flags
55 const Int32 FlagTerrain = 0x80000000;
56 const Int32 FlagNoColor = 0x40000000;
57 const Int32 FlagNoAltColor = 0x20000000;
58 const Int32 FlagPackedColor = 0x10000000;
59 const Int32 FlagTerrainFootprint = 0x08000000;
60 const Int32 FlagHidden = 0x04000000;
61 const Int32 FlagRoofline = 0x02000000;
63 // DrawType constants
64 const Int8 DTSolidCullBack = 0;
65 const Int8 DTSolid = 1;
66 const Int8 DTWireframeClosed = 2;
67 const Int8 DTWireframe = 3;
68 const Int8 DTWireframeSurround = 4;
69 const Int8 DTOmnidirectionalLight = 8;
70 const Int8 DTUnidirectionalLight = 9;
71 const Int8 DTBidirectionalLight = 10;
73 // LightMode constants
74 const UInt8 LMFaceColor = 0; // used in FaceRecord
75 const UInt8 LMMeshColor = 0; // used in MeshRecord
76 const UInt8 LMVertexColor = 1;
77 const UInt8 LMFaceColorLit = 2; // used in FaceRecord
78 const UInt8 LMMeshColorLit = 2; // used in MeshRecord
79 const UInt8 LMVertexColorLit = 3;
81 Real32 unitToMetersScale(OFDatabase::VertexUnits unit);
85 Real32 unitToMetersScale(OFDatabase::VertexUnits unit)
87 Real32 returnValue = 1.f;
89 switch(unit)
91 case OFDatabase::VU_Meters:
92 returnValue = 1.f;
93 break;
95 case OFDatabase::VU_Kilometers:
96 returnValue = 1000.f;
97 break;
99 case OFDatabase::VU_Feet:
100 returnValue = 0.3048f;
101 break;
103 case OFDatabase::VU_Inches:
104 returnValue = 0.0254f;
105 break;
107 case OFDatabase::VU_NauticalMiles:
108 returnValue = 1852.f;
109 break;
112 return returnValue;
115 } // namespace
118 //---------------------------------------------------------------------
119 // OFHeaderRecord
120 //---------------------------------------------------------------------
122 OFHeaderRecord::OFHeaderRecord(const OFRecordHeader &oHeader,
123 OFDatabase &oDB ) :
124 Inherited(oHeader, oDB)
128 OFHeaderRecord::~OFHeaderRecord(void)
132 /* static */
133 OFRecordTransitPtr OFHeaderRecord::create(const OFRecordHeader &oHeader,
134 OFDatabase &oDB )
136 return OFRecordTransitPtr(new OFHeaderRecord(oHeader, oDB));
139 bool OFHeaderRecord::read(std::istream &is)
141 OSG_OPENFLIGHT_LOG(("OFHeaderRecord::read len [%u]\n", _sLength));
143 Char8 szAsciiId[8]; Inherited::readChar8(is, szAsciiId, 8);
144 Int32 formatRev; Inherited::readVal(is, formatRev);
145 Int32 editRev; Inherited::readVal(is, editRev);
146 Char8 lastEditTime[32]; Inherited::readChar8(is, lastEditTime, 32);
147 Int16 nextGroupNodeId; Inherited::readVal(is, nextGroupNodeId);
148 Int16 nextLODNodeId; Inherited::readVal(is, nextLODNodeId);
149 Int16 nextObjectNodeId; Inherited::readVal(is, nextObjectNodeId);
150 Int16 nextFaceNodeId; Inherited::readVal(is, nextFaceNodeId);
151 Int16 unitMult; Inherited::readVal(is, unitMult);
152 Int8 vertUnits; Inherited::readVal(is, vertUnits);
153 Int8 faceTexWhite; Inherited::readVal(is, faceTexWhite);
154 Int32 flags; Inherited::readVal(is, flags);
156 #if 0
157 OSG_OPENFLIGHT_LOG(("OFHeaderRecord::read szAsciiId [%s]\n", szAsciiId));
158 OSG_OPENFLIGHT_LOG(("OFHeaderRecord::read formatRev [%u]\n", formatRev));
159 OSG_OPENFLIGHT_LOG(("OFHeaderRecord::read editRev [%u]\n", editRev));
160 OSG_OPENFLIGHT_LOG(("OFHeaderRecord::read lastEditTime [%s]\n", lastEditTime));
161 OSG_OPENFLIGHT_LOG(("OFHeaderRecord::read nextGroupNodeId [%u]\n", nextGroupNodeId));
162 OSG_OPENFLIGHT_LOG(("OFHeaderRecord::read nextLODNodeId [%u]\n", nextLODNodeId));
163 OSG_OPENFLIGHT_LOG(("OFHeaderRecord::read nextObjectNodeId [%u]\n", nextObjectNodeId));
164 OSG_OPENFLIGHT_LOG(("OFHeaderRecord::read nextFaceNodeId [%u]\n", nextFaceNodeId));
165 OSG_OPENFLIGHT_LOG(("OFHeaderRecord::read unitMult [%u]\n", unitMult));
166 OSG_OPENFLIGHT_LOG(("OFHeaderRecord::read vertUnits [%u]\n", vertUnits));
167 OSG_OPENFLIGHT_LOG(("OFHeaderRecord::read faceTexWhite [%u]\n", faceTexWhite));
168 OSG_OPENFLIGHT_LOG(("OFHeaderRecord::read flags [%x]\n", flags));
169 #endif
171 OFDatabase::VertexUnits fileUnits =
172 static_cast<OFDatabase::VertexUnits>(vertUnits);
173 Real32 unitScale = unitToMetersScale(fileUnits) /
174 unitToMetersScale(_oDB.getTargetUnits());
175 _oDB.setUnitScale(unitScale);
177 // read and discard remainder of header record
178 std::vector<char> tmpBuf;
180 tmpBuf.resize(_sLength);
182 is.read(&(tmpBuf.front()), _sLength - 68);
184 return is.good();
187 NodeTransitPtr OFHeaderRecord::convertToNode(void)
189 NodeUnrecPtr returnValue(NULL);
191 if(_primaryChildren.empty() == false)
193 returnValue = makeCoredNode<Group>();
195 for(UInt32 i = 0; i < _primaryChildren.size(); ++i)
197 NodeTransitPtr pChild = _primaryChildren[i]->convertToNode();
199 if(pChild != NULL)
201 returnValue->addChild(pChild);
203 else
205 FFATAL(("OFHeaderRecord::convertToNode: "
206 "No child created for record [%u - %s].\n",
207 _primaryChildren[i]->getOpCode(),
208 getOpCodeString(_primaryChildren[i]->getOpCode())));
213 return NodeTransitPtr(returnValue);
216 bool OFHeaderRecord::addChild(OFRecord *pChild)
218 if(pChild == NULL)
219 return false;
221 bool returnValue = false;
223 switch(pChild->getOpCode())
225 case OFColorPaletteRecord::OpCode:
227 OFColorPaletteRecord *pCol =
228 dynamic_cast<OFColorPaletteRecord *>(pChild);
230 _oDB.addColorPaletteRecord(pCol);
232 returnValue = true;
234 break;
236 case OFTexturePaletteRecord::OpCode:
238 OFTexturePaletteRecord *pTex =
239 dynamic_cast<OFTexturePaletteRecord *>(pChild);
241 _oDB.addTexturePaletteRecord(pTex);
243 returnValue = true;
245 break;
247 case OFVertexPaletteRecord::OpCode:
249 OFVertexPaletteRecord *pVert =
250 dynamic_cast<OFVertexPaletteRecord *>(pChild);
252 _oDB.addVertexPaletteRecord(pVert);
254 returnValue = true;
256 break;
258 case OFMaterialPaletteRecord::OpCode:
260 OFMaterialPaletteRecord *pMat =
261 dynamic_cast<OFMaterialPaletteRecord *>(pChild);
263 _oDB.addMaterialPaletteRecord(pMat);
265 returnValue = true;
267 break;
269 default:
270 break;
273 if(returnValue == false)
274 returnValue = Inherited::addChild(pChild);
276 return returnValue;
279 UInt16 OFHeaderRecord::getOpCode(void) const
281 return OpCode;
284 void OFHeaderRecord::dump(UInt32 uiIndent) const
286 indentLog(uiIndent, PLOG);
287 PLOG << "HeaderRecord" << std::endl;
289 indentLog(uiIndent, PLOG);
290 PLOG << "{" << std::endl;
292 uiIndent += 2;
294 if(_oDB.getColorPalette() != NULL)
295 _oDB.getColorPalette()->dump(uiIndent);
297 if(_oDB.getVertexPalette() != NULL)
298 _oDB.getVertexPalette()->dump(uiIndent);
300 uiIndent -= 2;
302 indentLog(uiIndent, PLOG);
303 PLOG << "}" << std::endl;
305 Inherited::dump(uiIndent);
308 //---------------------------------------------------------------------
309 // OFGroupRecord
310 //---------------------------------------------------------------------
312 OFRecordTransitPtr OFGroupRecord::create(const OFRecordHeader &oHeader,
313 OFDatabase &oDB )
315 return OFRecordTransitPtr(new OFGroupRecord(oHeader, oDB));
318 OFGroupRecord::OFGroupRecord(const OFRecordHeader &oHeader,
319 OFDatabase &oDB ) :
320 Inherited(oHeader, oDB)
324 OFGroupRecord::~OFGroupRecord(void)
328 bool OFGroupRecord::read(std::istream &is)
330 OSG_OPENFLIGHT_LOG(("OFGroupRecord::read len [%u]\n",
331 _sLength));
333 Inherited::readChar8(is, szASCIIId, 8);
335 is.ignore(_sLength - 12); // skip rest of record
337 return is.good();
340 NodeTransitPtr OFGroupRecord::convertToNode(void)
342 NodeUnrecPtr returnValue(NULL);
344 if(_primaryChildren.empty() == false)
346 if(_primaryChildren.size() > 1)
348 returnValue = makeCoredNode<Group>();
350 for(UInt32 i = 0; i < _primaryChildren.size(); ++i)
352 NodeTransitPtr pChild = _primaryChildren[i]->convertToNode();
354 if(pChild != NULL)
356 returnValue->addChild(pChild);
360 else
362 returnValue = _primaryChildren[0]->convertToNode();
366 returnValue = convertAncillaryChildren(returnValue);
368 return NodeTransitPtr(returnValue);
371 UInt16 OFGroupRecord::getOpCode(void) const
373 return OpCode;
376 void OFGroupRecord::dump(UInt32 uiIndent) const
378 indentLog(uiIndent, PLOG);
379 PLOG << "GroupRecord" << std::endl;
381 indentLog(uiIndent, PLOG);
382 PLOG << "{" << std::endl;
384 indentLog(uiIndent, PLOG);
385 PLOG << "}" << std::endl;
387 Inherited::dump(uiIndent);
390 NodeTransitPtr OFGroupRecord::convertAncillaryChildren(Node *pNode)
392 NodeUnrecPtr returnValue(pNode);
394 for(UInt32 i = 0; i < _ancillaryChildren.size(); ++i)
396 returnValue = _ancillaryChildren[i]->convert(returnValue);
399 return NodeTransitPtr(returnValue);
402 OFRecordFactoryBase::RegisterRecord OFGroupRecord::_regHelper(
403 &OFGroupRecord::create,
404 OFGroupRecord::OpCode);
407 //---------------------------------------------------------------------
408 // OFObjectRecord
409 //---------------------------------------------------------------------
411 OFRecordTransitPtr OFObjectRecord::create(const OFRecordHeader &oHeader,
412 OFDatabase &oDB )
414 return OFRecordTransitPtr(new OFObjectRecord(oHeader, oDB));
417 OFObjectRecord::OFObjectRecord(const OFRecordHeader &oHeader,
418 OFDatabase &oDB ) :
419 Inherited (oHeader, oDB),
420 szASCIIId (),
421 flags (0),
422 relPriority (0),
423 transparency (0),
424 specialEffect1(0),
425 specialEffect2(0),
426 significance (0),
427 reserved (0),
428 _vFaces (),
429 _vMeshes ()
433 OFObjectRecord::~OFObjectRecord(void)
437 void OFObjectRecord::dump(UInt32 uiIndent) const
439 indentLog(uiIndent, PLOG);
440 PLOG << "ObjectRecord" << std::endl;
442 indentLog(uiIndent, PLOG);
443 PLOG << "{" << std::endl;
445 uiIndent += 2;
447 indentLog(uiIndent, PLOG);
448 PLOG << "#Faces " << _vFaces.size() << std::endl;
450 indentLog(uiIndent, PLOG);
451 PLOG << "[" << std::endl;
453 uiIndent += 2;
455 for(UInt32 i = 0; i < _vFaces.size(); ++i)
457 _vFaces[i]->dump(uiIndent);
460 uiIndent -= 2;
462 indentLog(uiIndent, PLOG);
463 PLOG << "]" << std::endl;
465 indentLog(uiIndent, PLOG);
466 PLOG << "#Meshes " << _vMeshes.size() << std::endl;
468 indentLog(uiIndent, PLOG);
469 PLOG << "[" << std::endl;
471 uiIndent += 2;
473 for(UInt32 i = 0; i < _vMeshes.size(); ++i)
475 _vMeshes[i]->dump(uiIndent);
478 uiIndent -= 2;
480 indentLog(uiIndent, PLOG);
481 PLOG << "]" << std::endl;
483 uiIndent -= 2;
485 indentLog(uiIndent, PLOG);
486 PLOG << "}" << std::endl;
488 Inherited::dump(uiIndent);
491 bool OFObjectRecord::read(std::istream &is)
493 OSG_OPENFLIGHT_LOG(("OFObjectRecord::read len [%u]\n", _sLength));
495 Inherited::readChar8(is, szASCIIId, 8);
496 Inherited::readVal (is, flags);
497 Inherited::readVal (is, relPriority);
498 Inherited::readVal (is, transparency);
499 Inherited::readVal (is, specialEffect1);
500 Inherited::readVal (is, specialEffect2);
501 Inherited::readVal (is, significance);
502 Inherited::readVal (is, reserved);
504 return is.good();
508 bool OFObjectRecord::addChild(OFRecord *pChild)
510 if(pChild == NULL)
511 return false;
513 bool returnValue = false;
515 switch(pChild->getOpCode())
517 case OFFaceRecord::OpCode:
519 _vFaces.push_back(dynamic_cast<OFFaceRecord *>(pChild));
521 returnValue = true;
523 break;
525 case OFMeshRecord::OpCode:
527 _vMeshes.push_back(dynamic_cast<OFMeshRecord *>(pChild));
529 returnValue = true;
531 break;
533 case OFLocalVertexPoolRecord::OpCode:
535 _vMeshes.back()->addChild(pChild);
537 returnValue = true;
539 break;
541 default:
542 break;
545 if(returnValue == false)
546 returnValue = Inherited::addChild(pChild);
548 return returnValue;
551 UInt16 OFObjectRecord::getOpCode(void) const
553 return OpCode;
556 NodeTransitPtr OFObjectRecord::convertToNode(void)
558 NodeTransitPtr returnValue(NULL);
560 NodeUnrecPtr pGeo = convertGeometry();
562 returnValue = convertAncillaryChildren(pGeo);
564 return returnValue;
567 void OFObjectRecord::groupFaces(
568 std::vector< std::vector<OFFaceRecord *> > &vFaceGroups)
570 if(_vFaces.size() == 0)
571 return;
573 vFaceGroups.clear();
575 std::vector<OFFaceRecord *> tmpVec;
576 tmpVec.push_back(_vFaces[0]);
578 vFaceGroups.push_back(tmpVec);
580 UInt32 j = 0;
582 for(UInt32 i = 1; i < _vFaces.size(); ++i)
584 for(j = 0; j < vFaceGroups.size(); ++j)
586 if(*(_vFaces[i]) == *(vFaceGroups[j][0]))
588 vFaceGroups[j].push_back(_vFaces[i]);
589 break;
593 if(j == vFaceGroups.size())
595 tmpVec.clear();
596 tmpVec.push_back(_vFaces[i]);
598 vFaceGroups.push_back(tmpVec);
603 NodeTransitPtr OFObjectRecord::convertFaceGroup(
604 std::vector<OFFaceRecord *> &vFaceGroup)
606 NodeTransitPtr returnValue(NULL);
608 if(vFaceGroup.size() == 0)
609 return returnValue;
611 typedef OFVertexPaletteRecord::VertexInfo VertexInfo;
613 returnValue = Node ::create();
614 GeometryUnrecPtr pGeo = Geometry::create();
616 returnValue->setCore(pGeo);
618 GeoUInt32PropertyUnrecPtr pindex = GeoUInt32Property::create();
619 GeoUInt32PropertyUnrecPtr nindex = NULL;
620 GeoUInt32PropertyUnrecPtr tindex = NULL;
621 GeoUInt32PropertyUnrecPtr lens = GeoUInt32Property::create();
622 GeoUInt8PropertyUnrecPtr types = GeoUInt8Property ::create();
624 GeoUInt32Property::StoredFieldType *pPI = pindex->editFieldPtr();
625 GeoUInt32Property::StoredFieldType *pNI = NULL;
626 GeoUInt32Property::StoredFieldType *pTI = NULL;
627 GeoUInt32Property::StoredFieldType *pL = lens ->editFieldPtr();
628 GeoUInt8Property ::StoredFieldType *pT = types->editFieldPtr();
630 const OFVertexPaletteRecord *pVertexPal = _oDB.getVertexPalette();
632 UInt32 uiNumValid;
634 GeoPnt3fPropertyUnrecPtr pnts = GeoPnt3fProperty ::create();
635 GeoVec3fPropertyUnrecPtr norms = NULL;
636 GeoVec2fPropertyUnrecPtr tex = NULL;
638 GeoPnt3fProperty::StoredFieldType *pPos = pnts ->editFieldPtr();
639 GeoVec3fProperty::StoredFieldType *pNorm = NULL;
640 GeoVec2fProperty::StoredFieldType *pTX = NULL;
642 UInt16 uiFirstVType = 0;
643 bool bSingleIdx = true;
645 for(UInt32 i = 0; i < vFaceGroup.size(); ++i)
647 OFFaceRecord *pFace = vFaceGroup[i];
648 OFVertexListRecord *pVList = vFaceGroup[i]->getVertexList();
650 if(pVList != NULL)
652 const std::vector<Int32> &vIndices = pVList->getIndices();
654 uiNumValid = 0;
656 for(UInt32 j = 0; j < vIndices.size(); ++j)
658 const VertexInfo *vInfo =
659 pVertexPal->getVertexInfo(vIndices[j]);
660 UInt16 uiVType = 0;
662 if(vInfo != NULL)
664 uiVType = vInfo->uiType;
666 if(pFace->getLightMode() == 0 ||
667 pFace->getLightMode() == 1 )
669 uiVType &= ~OFVertexPaletteRecord::HasNorm;
672 // First Vertex
673 if(i == 0 && j == 0)
675 uiFirstVType = uiVType;
677 if(uiFirstVType & OFVertexPaletteRecord::HasNorm)
679 norms = GeoVec3fProperty ::create();
680 pNorm = norms->editFieldPtr();
682 nindex = GeoUInt32Property::create();
683 pNI = nindex->editFieldPtr();
686 if(uiFirstVType & OFVertexPaletteRecord::HasTexCoord)
688 tex = GeoVec2fProperty ::create();
689 pTX = tex ->editFieldPtr();
691 tindex = GeoUInt32Property::create();
692 pTI = tindex->editFieldPtr();
696 if(uiFirstVType != uiVType)
698 FWARNING(("OFObjectRecord::convertFaceGroup: "
699 "Found different vtypes - first [0x%x] "
700 "current [0x%x] face [%u/%" PRIUSize "] "
701 "vert [%u]\n",
702 uiFirstVType, uiVType,
703 i, vFaceGroup.size(), j));
706 UInt32 uiPosIdx =
707 vInfo->uiIdx[OFVertexPaletteRecord::PosIdx];
709 pPI->push_back(pPos->size32());
711 pPos->push_back(pVertexPal->getPos(uiPosIdx));
713 if(uiFirstVType & OFVertexPaletteRecord::HasNorm)
715 // XXX TODO:
716 // there are models where some vertices have normals
717 // and others don't.
718 // For now we insert a dummy normal
720 if((uiVType & OFVertexPaletteRecord::HasNorm) != 0)
722 UInt32 uiNormIdx =
723 vInfo->uiIdx[OFVertexPaletteRecord::NormIdx];
725 pNI->push_back(pNorm->size32());
727 pNorm->push_back(pVertexPal->getNormal(uiNormIdx));
729 if(uiNormIdx != uiPosIdx)
731 bSingleIdx = false;
734 else
736 pNI->push_back(pNorm->size32());
737 pNorm->push_back(Vec3f(0.f, 0.f, 1.f));
739 bSingleIdx = false;
743 if(uiFirstVType & OFVertexPaletteRecord::HasTexCoord)
745 UInt32 uiTXIdx =
746 vInfo->uiIdx[OFVertexPaletteRecord::TexCoordIdx];
748 pTI->push_back(pTX->size32());
750 pTX->push_back(pVertexPal->getTexCoord(uiTXIdx));
752 if(uiTXIdx != uiPosIdx)
754 bSingleIdx = false;
758 ++uiNumValid;
762 pL->push_back(uiNumValid);
763 pT->push_back(GL_POLYGON);
767 pGeo->setIndex (pindex, Geometry::PositionsIndex);
768 pGeo->setProperty(pnts, Geometry::PositionsIndex);
770 if(norms != NULL)
772 if(bSingleIdx == true)
774 pGeo->setIndex(pindex, Geometry::NormalsIndex);
776 else
778 pGeo->setIndex(nindex, Geometry::NormalsIndex);
781 pGeo->setProperty(norms, Geometry::NormalsIndex);
784 if(tex != NULL)
786 if(bSingleIdx == true)
788 pGeo->setIndex(pindex, Geometry::TexCoordsIndex);
790 else
792 pGeo->setIndex(tindex, Geometry::TexCoordsIndex);
795 pGeo->setProperty(tex, Geometry::TexCoordsIndex);
798 pGeo->setTypes (types);
799 pGeo->setLengths(lens );
801 ChunkMaterialUnrecPtr pChunkMat = ChunkMaterial::create();
803 if(vFaceGroup[0]->getTextureWhite() &&
804 vFaceGroup[0]->getTexIdx () != -1)
806 MaterialChunkUnrecPtr pMatChunk = MaterialChunk::create();
807 Color4f colDiffuse(1.f, 1.f, 1.f, 1.f);
809 pMatChunk->setDiffuse(colDiffuse);
811 pChunkMat->addChunk(pMatChunk);
813 else if(vFaceGroup[0]->getMatIdx() != -1)
815 const OFMaterialPaletteRecord *pMatRec =
816 _oDB.getMatRecord(vFaceGroup[0]->getMatIdx());
818 if(pMatRec != NULL)
820 MaterialChunkUnrecPtr pMatChunk = MaterialChunk::create();
822 if((vFaceGroup[0]->getFlags() & FlagNoColor) != 0)
824 // use material only
825 Color4f colMat;
827 pMatChunk->setAmbient(pMatRec->getAmbient());
829 colMat = pMatRec->getDiffuse();
830 colMat[3] = pMatRec->getAlpha() * (1.f - (vFaceGroup[0]->getTransparency() / 65535.f));
831 pMatChunk->setDiffuse(colMat);
833 pMatChunk->setSpecular (pMatRec->getSpecular ());
834 pMatChunk->setShininess(pMatRec->getShininess());
835 pMatChunk->setEmission (pMatRec->getEmissive ());
837 pChunkMat->addChunk(pMatChunk);
839 else
841 // use uiPackedPrimCol/uiPrimColIdx and material
842 Color4f colGeo;
843 Color4f colMat;
845 colGeo = vFaceGroup[0]->getPrimColor();
847 colMat = pMatRec->getAmbient();
848 colMat[0] = colMat[0] * colGeo[0];
849 colMat[1] = colMat[1] * colGeo[1];
850 colMat[2] = colMat[2] * colGeo[2];
852 pMatChunk->setAmbient(colMat);
854 colMat = pMatRec->getDiffuse();
855 colMat[0] = colMat[0] * colGeo[0];
856 colMat[1] = colMat[1] * colGeo[1];
857 colMat[2] = colMat[2] * colGeo[2];
858 colMat[3] = pMatRec->getAlpha() * (1.f - (vFaceGroup[0]->getTransparency() / 65535.f));
860 OSG_OPENFLIGHT_LOG(("OFObjectRecord::convertFaceGroup: "
861 "PC pMatRec->getAlpha() [%f] "
862 "Transparency [%f] colMat[3] [%f]\n",
863 pMatRec->getAlpha(),
864 vFaceGroup[0]->getTransparency(),
865 colMat[3]));
867 pMatChunk->setDiffuse (colMat );
868 pMatChunk->setSpecular (pMatRec->getSpecular ());
869 pMatChunk->setShininess(pMatRec->getShininess());
870 pMatChunk->setEmission (pMatRec->getEmissive ());
872 pChunkMat->addChunk(pMatChunk);
875 if(pMatChunk->isTransparent() &&
876 pChunkMat->find(BlendChunk::getClassType()) == NULL)
878 BlendChunkUnrecPtr pBlendChunk = BlendChunk::create();
880 pBlendChunk->setSrcFactor (GL_SRC_ALPHA );
881 pBlendChunk->setDestFactor(GL_ONE_MINUS_SRC_ALPHA);
883 pChunkMat->addChunk(pBlendChunk);
886 else
888 FWARNING(("OFObjectRecord::convertFaceGroup: "
889 "No material record for index [%u].\n",
890 vFaceGroup[0]->getMatIdx()));
894 if(vFaceGroup[0]->getTexIdx() != -1)
896 const OFTexturePaletteRecord *pTexRec =
897 _oDB.getTexRecord(vFaceGroup[0]->getTexIdx());
899 if(pTexRec != NULL)
901 TextureObjChunk *pTexObj = pTexRec->getTexObj();
902 TextureEnvChunk *pTexEnv = pTexRec->getTexEnv();
904 if(pTexObj != NULL)
906 pChunkMat->addChunk(pTexObj);
908 if(pTexObj->getImage()->hasAlphaChannel() &&
909 pChunkMat->find(BlendChunk::getClassType()) == NULL)
911 BlendChunkUnrecPtr pBlendChunk = BlendChunk::create();
913 pBlendChunk->setSrcFactor (GL_SRC_ALPHA );
914 pBlendChunk->setDestFactor(GL_ONE_MINUS_SRC_ALPHA);
916 pChunkMat->addChunk(pBlendChunk);
920 if(pTexEnv != NULL)
922 pChunkMat->addChunk(pTexEnv);
924 else
926 TextureEnvChunkUnrecPtr pNewTexEnv = TextureEnvChunk::create();
928 pNewTexEnv->setEnvMode(GL_MODULATE);
930 pChunkMat->addChunk(pNewTexEnv);
933 else
935 FFATAL(("OFObjectRecord::convertFaceGroup: "
936 "No texture record for index [%d].\n", vFaceGroup[0]->getTexIdx()));
940 if(vFaceGroup[0]->getLightMode() == LMMeshColor ||
941 vFaceGroup[0]->getLightMode() == LMMeshColorLit )
943 MaterialChunk *pMatChunk = dynamic_cast<MaterialChunk *>(
944 pChunkMat->find(MaterialChunk::getClassType()));
946 if(pMatChunk != NULL)
948 pMatChunk->setColorMaterial(GL_NONE);
950 if(vFaceGroup[0]->getLightMode() == LMMeshColor)
951 pMatChunk->setLit(false);
955 if(vFaceGroup[0]->getLightMode() == LMMeshColorLit ||
956 vFaceGroup[0]->getLightMode() == LMVertexColorLit )
958 if(pGeo->getProperty(Geometry::NormalsIndex) == NULL)
959 calcVertexNormals(pGeo);
962 switch(vFaceGroup[0]->getDrawType())
964 case DTSolidCullBack:
966 PolygonChunkUnrecPtr pPolyChunk = PolygonChunk::create();
967 pPolyChunk->setCullFace(GL_BACK);
969 pChunkMat->addChunk(pPolyChunk);
970 break;
973 case DTSolid:
975 // nothing to do
976 break;
979 case DTWireframeClosed:
980 case DTWireframe:
982 PolygonChunkUnrecPtr pPolyChunk = PolygonChunk::create();
983 pPolyChunk->setFrontMode(GL_LINE);
984 pPolyChunk->setBackMode (GL_LINE);
986 pChunkMat->addChunk(pPolyChunk);
987 break;
990 default:
992 FWARNING(("OFMeshRecord::convertToNode: Unhandled draw "
993 "type [%d].\n", vFaceGroup[0]->getDrawType()));
994 break;
998 pGeo->setMaterial(pChunkMat);
1000 return returnValue;
1003 NodeTransitPtr OFObjectRecord::convertGeometry(void)
1005 NodeTransitPtr returnValue(NULL);
1007 std::vector< std::vector<OFFaceRecord *> > vFaceGroups;
1009 groupFaces(vFaceGroups);
1011 if(vFaceGroups.size() == 1 && _vMeshes.size() == 0)
1013 returnValue = convertFaceGroup(vFaceGroups[0]);
1015 else if(vFaceGroups.size() == 0 && _vMeshes.size() == 1)
1017 returnValue = _vMeshes[0]->convertToNode();
1019 else if(vFaceGroups.size() > 0 || _vMeshes.size() > 0)
1021 returnValue = Node::create();
1022 returnValue->setCore(Group::create());
1024 for(UInt32 i = 0; i < vFaceGroups.size(); ++i)
1026 returnValue->addChild(convertFaceGroup(vFaceGroups[i]));
1029 for(UInt32 i = 0; i < _vMeshes.size(); ++i)
1031 returnValue->addChild(_vMeshes[i]->convertToNode());
1035 return returnValue;
1038 NodeTransitPtr OFObjectRecord::convertAncillaryChildren(Node *pNode)
1040 NodeUnrecPtr returnValue(pNode);
1042 for(UInt32 i = 0; i < _ancillaryChildren.size(); ++i)
1044 returnValue = _ancillaryChildren[i]->convert(returnValue);
1047 return NodeTransitPtr(returnValue);
1050 OFRecordFactoryBase::RegisterRecord OFObjectRecord::_regHelper(
1051 &OFObjectRecord::create,
1052 OFObjectRecord::OpCode);
1054 //---------------------------------------------------------------------
1055 // OFLocalVertexPoolRecord
1056 //---------------------------------------------------------------------
1058 OFLocalVertexPoolRecord::OFLocalVertexPoolRecord(
1059 const OFRecordHeader &oHeader, OFDatabase &oDB) :
1061 Inherited (oHeader,
1062 oDB ),
1063 uiNumVerts (0 ),
1064 uiAttribMask(0 ),
1065 _pPositions (NULL ),
1066 _pColors (NULL ),
1067 _pNormals (NULL ),
1068 _texCoords ( ),
1069 _vTriStrips ( ),
1070 _vTriFans ( ),
1071 _vQuadStrips ( ),
1072 _vPolygons ( )
1074 for(UInt32 i = 0; i < 8; ++i)
1075 _texCoords[i] = NULL;
1078 OFLocalVertexPoolRecord::~OFLocalVertexPoolRecord(void)
1082 OFRecordTransitPtr OFLocalVertexPoolRecord::create(
1083 const OFRecordHeader &oHeader, OFDatabase &oDB)
1085 return OFRecordTransitPtr(new OFLocalVertexPoolRecord(oHeader, oDB));
1088 bool OFLocalVertexPoolRecord::read(std::istream &is)
1090 OSG_OPENFLIGHT_LOG(("OFLocalVertexPoolRecord::read len [%u]\n",
1091 _sLength));
1093 Inherited::readVal(is, uiNumVerts );
1094 Inherited::readVal(is, uiAttribMask);
1096 return readContinue(is, _sLength - 12);
1099 bool OFLocalVertexPoolRecord::readContinue(
1100 std::istream &is, UInt16 uiLength)
1102 OSG_OPENFLIGHT_LOG(("OFLocalVertexPoolRecord::readContinue len [%u] "
1103 "cont len [%u]\n", _sLength, uiLength));
1105 if((uiAttribMask & AMHasColorIndex) != 0)
1107 FWARNING(("OFLocalVertexPoolRecord::readContinue: "
1108 "Color Index mode not supported.\n"));
1111 // make sure the needed properties exist
1112 if((uiAttribMask & AMHasPosition) != 0 && _pPositions == NULL)
1113 _pPositions = GeoPnt3dProperty::create();
1115 if((uiAttribMask & AMHasColorValue) != 0 && _pColors == NULL)
1116 _pColors = GeoColor4ubProperty::create();
1118 if((uiAttribMask & AMHasNormal) != 0 && _pNormals == NULL)
1119 _pNormals = GeoVec3fProperty::create();
1121 for(UInt32 i = 0; i < 8; ++i)
1123 if((uiAttribMask & AMHasTexCoords[i]) != 0 && _texCoords[i] == NULL)
1124 _texCoords[i] = GeoVec2fProperty::create();
1127 // read data
1128 for(Int32 bytesLeft = uiLength; bytesLeft > 0;)
1130 if((uiAttribMask & AMHasPosition) != 0)
1132 Pnt3d tmpPnt;
1133 Inherited::readVal(is, tmpPnt[0]);
1134 Inherited::readVal(is, tmpPnt[1]);
1135 Inherited::readVal(is, tmpPnt[2]);
1137 _pPositions->push_back(tmpPnt);
1139 bytesLeft -= 3 * 8;
1142 if((uiAttribMask & AMHasColorIndex) != 0)
1144 UInt32 tmpVal;
1145 Inherited::readVal(is, tmpVal);
1147 // TODO fetch the actual color from the palette
1149 bytesLeft -= 4;
1152 if((uiAttribMask & AMHasColorValue) != 0)
1154 Color4ub tmpVal;
1155 Inherited::readVal(is, tmpVal[3]);
1156 Inherited::readVal(is, tmpVal[0]);
1157 Inherited::readVal(is, tmpVal[1]);
1158 Inherited::readVal(is, tmpVal[2]);
1160 _pColors->push_back(tmpVal);
1162 bytesLeft -= 4;
1165 if((uiAttribMask & AMHasNormal) != 0)
1167 Vec3f tmpVec;
1168 Inherited::readVal(is, tmpVec[0]);
1169 Inherited::readVal(is, tmpVec[1]);
1170 Inherited::readVal(is, tmpVec[2]);
1172 tmpVec.normalize();
1174 _pNormals->push_back(tmpVec);
1176 bytesLeft -= 3 * 4;
1179 for(UInt32 i = 0; i < 8; ++i)
1181 if((uiAttribMask & AMHasTexCoords[i]) != 0)
1183 Vec2f tmpVal;
1184 Inherited::readVal(is, tmpVal[0]);
1185 Inherited::readVal(is, tmpVal[1]);
1187 _texCoords[i]->push_back(tmpVal);
1189 bytesLeft -= 2 * 4;
1194 return is.good();
1197 bool OFLocalVertexPoolRecord::addChild(OFRecord *pChild)
1199 if(pChild == NULL)
1200 return false;
1202 bool rc = false;
1204 switch(pChild->getOpCode())
1206 case OFMeshPrimitiveRecord::OpCode:
1208 OFMeshPrimitiveRecord *pPrim =
1209 dynamic_cast<OFMeshPrimitiveRecord *>(pChild);
1211 rc = true;
1213 switch(pPrim->uiPrimType)
1215 case OFMeshPrimitiveRecord::PTTriStrip:
1217 _vTriStrips.push_back(pPrim);
1218 break;
1221 case OFMeshPrimitiveRecord::PTTriFan:
1223 _vTriFans.push_back(pPrim);
1224 break;
1227 case OFMeshPrimitiveRecord::PTQuadStrip:
1229 _vQuadStrips.push_back(pPrim);
1230 break;
1233 case OFMeshPrimitiveRecord::PTPolygon:
1235 _vPolygons.push_back(pPrim);
1236 break;
1239 default:
1241 FWARNING(("OFMeshRecord::addChild: Unexpected primitive type [%u] "
1242 "for MeshPrimitiveRecord.\n", pPrim->uiPrimType));
1243 rc = Inherited::addChild(pChild);
1244 break;
1247 break;
1250 default:
1252 FWARNING(("OFMeshRecord::addChild: Unexpected child type [%u - %s].\n",
1253 pChild->getOpCode(), getOpCodeString(pChild->getOpCode())));
1254 rc = Inherited::addChild(pChild);
1255 break;
1259 return rc;
1262 UInt16 OFLocalVertexPoolRecord::getOpCode(void) const
1264 return OpCode;
1267 void OFLocalVertexPoolRecord::dump(UInt32 uiIndent) const
1269 indentLog(uiIndent, PLOG);
1270 PLOG << "LocalVertexPoolRecord : " << std::endl;
1272 indentLog(uiIndent, PLOG);
1273 PLOG << "{" << std::endl;
1275 uiIndent += 2;
1277 indentLog(uiIndent, PLOG);
1278 PLOG << "NumVerts : " << uiNumVerts << std::endl;
1280 PLOG.setf (std::ios::hex, std::ios::basefield);
1282 indentLog(uiIndent, PLOG);
1283 PLOG << "FullAttribMask : " << uiAttribMask << std::endl;
1285 PLOG.setf (std::ios::dec, std::ios::basefield);
1287 indentLog(uiIndent, PLOG);
1288 PLOG << "AttribMask P:" << ((uiAttribMask & AMHasPosition ) >> 31)
1289 << " Ci:" << ((uiAttribMask & AMHasColorIndex ) >> 30)
1290 << " Cv:" << ((uiAttribMask & AMHasColorValue ) >> 29)
1291 << " N:" << ((uiAttribMask & AMHasNormal ) >> 28)
1292 << " UV0:" << ((uiAttribMask & AMHasTexCoords[0]) >> 27)
1293 << " UV1:" << ((uiAttribMask & AMHasTexCoords[1]) >> 26)
1294 << " UV2:" << ((uiAttribMask & AMHasTexCoords[2]) >> 25)
1295 << " UV3:" << ((uiAttribMask & AMHasTexCoords[3]) >> 24)
1296 << " UV4:" << ((uiAttribMask & AMHasTexCoords[4]) >> 23)
1297 << " UV5:" << ((uiAttribMask & AMHasTexCoords[5]) >> 22)
1298 << " UV6:" << ((uiAttribMask & AMHasTexCoords[6]) >> 21)
1299 << " UV7:" << ((uiAttribMask & AMHasTexCoords[7]) >> 20)
1300 << std::endl;
1302 uiIndent -= 2;
1304 indentLog(uiIndent, PLOG);
1305 PLOG << "}" << std::endl;
1307 indentLog(uiIndent, PLOG);
1308 PLOG << "[" << std::endl;
1310 uiIndent += 2;
1312 for(UInt32 i = 0; i < _vTriStrips.size(); ++i)
1313 _vTriStrips[i]->dump(uiIndent);
1315 for(UInt32 i = 0; i < _vTriFans.size(); ++i)
1316 _vTriFans[i]->dump(uiIndent);
1318 for(UInt32 i = 0; i < _vQuadStrips.size(); ++i)
1319 _vQuadStrips[i]->dump(uiIndent);
1321 for(UInt32 i = 0; i < _vPolygons.size(); ++i)
1322 _vPolygons[i]->dump(uiIndent);
1324 uiIndent -= 2;
1326 indentLog(uiIndent, PLOG);
1327 PLOG << "]" << std::endl;
1330 void OFLocalVertexPoolRecord::convertPrimitives(Geometry *pGeo)
1332 GeoUInt32PropertyUnrecPtr pInd = GeoUInt32Property::create();
1334 for(UInt32 i = 0; i < _vTriStrips.size(); ++i)
1336 pGeo->getTypes ()->push_back(GL_TRIANGLE_STRIP );
1337 pGeo->getLengths()->push_back(_vTriStrips[i]->uiVertexCount);
1339 for(UInt32 j = 0; j < _vTriStrips[i]->_vIndices.size(); ++j)
1341 pInd->push_back(_vTriStrips[i]->_vIndices[j]);
1345 for(UInt32 i = 0; i < _vTriFans.size(); ++i)
1347 pGeo->getTypes ()->push_back(GL_TRIANGLE_FAN );
1348 pGeo->getLengths()->push_back(_vTriFans[i]->uiVertexCount);
1350 for(UInt32 j = 0; j < _vTriFans[i]->_vIndices.size(); ++j)
1352 pInd->push_back(_vTriFans[i]->_vIndices[j]);
1356 for(UInt32 i = 0; i < _vQuadStrips.size(); ++i)
1358 pGeo->getTypes ()->push_back(GL_QUAD_STRIP );
1359 pGeo->getLengths()->push_back(_vQuadStrips[i]->uiVertexCount);
1361 for(UInt32 j = 0; j < _vQuadStrips[i]->_vIndices.size(); ++j)
1363 pInd->push_back(_vQuadStrips[i]->_vIndices[j]);
1367 for(UInt32 i = 0; i < _vPolygons.size(); ++i)
1369 pGeo->getTypes ()->push_back(GL_POLYGON );
1370 pGeo->getLengths()->push_back(_vPolygons[i]->uiVertexCount);
1372 for(UInt32 j = 0; j < _vPolygons[i]->_vIndices.size(); ++j)
1374 pInd->push_back(_vPolygons[i]->_vIndices[j]);
1378 if((uiAttribMask & AMHasPosition) != 0)
1380 pGeo->setProperty(_pPositions, Geometry::PositionsIndex);
1381 pGeo->setIndex (pInd, Geometry::PositionsIndex);
1384 if((uiAttribMask & AMHasColorIndex) != 0)
1386 ; // TODO
1389 if((uiAttribMask & AMHasColorValue) != 0)
1391 pGeo->setProperty(_pColors, Geometry::ColorsIndex);
1392 pGeo->setIndex (pInd, Geometry::ColorsIndex);
1395 if((uiAttribMask & AMHasNormal) != 0)
1397 pGeo->setProperty(_pNormals, Geometry::NormalsIndex);
1398 pGeo->setIndex (pInd, Geometry::NormalsIndex);
1401 for(UInt32 i = 0; i < 8; ++i)
1403 if((uiAttribMask & AMHasTexCoords[i]) != 0)
1405 pGeo->setProperty(_texCoords[i], Geometry::TexCoordsIndex + i);
1406 pGeo->setIndex (pInd, Geometry::TexCoordsIndex + i);
1411 GeoPnt3dProperty *OFLocalVertexPoolRecord::getPositions(void) const
1413 return _pPositions;
1416 GeoColor4ubProperty *OFLocalVertexPoolRecord::getColors(void) const
1418 return _pColors;
1421 GeoVec3fProperty *OFLocalVertexPoolRecord::getNormals(void) const
1423 return _pNormals;
1426 GeoVec2fProperty *OFLocalVertexPoolRecord::getTexCoords(UInt32 idx) const
1428 return _texCoords[idx];
1431 OFRecordFactoryBase::RegisterRecord OFLocalVertexPoolRecord::_regHelper(
1432 &OFLocalVertexPoolRecord::create,
1433 OFLocalVertexPoolRecord::OpCode );
1435 const UInt32 OFLocalVertexPoolRecord::AMHasPosition = 0x80000000;
1436 const UInt32 OFLocalVertexPoolRecord::AMHasColorIndex = 0x40000000;
1437 const UInt32 OFLocalVertexPoolRecord::AMHasColorValue = 0x20000000;
1438 const UInt32 OFLocalVertexPoolRecord::AMHasNormal = 0x10000000;
1439 const UInt32 OFLocalVertexPoolRecord::AMHasTexCoords[8] =
1441 0x08000000,
1442 0x04000000,
1443 0x02000000,
1444 0x01000000,
1445 0x00800000,
1446 0x00400000,
1447 0x00200000,
1448 0x00100000
1451 //---------------------------------------------------------------------
1452 // OFMeshPrimitiveRecord
1453 //---------------------------------------------------------------------
1455 OFMeshPrimitiveRecord::OFMeshPrimitiveRecord(const OFRecordHeader &oHeader,
1456 OFDatabase &oDB ) :
1457 Inherited (oHeader, oDB),
1458 uiPrimType (),
1459 uiIndexSize (),
1460 uiVertexCount(),
1461 _vIndices ()
1465 OFMeshPrimitiveRecord::~OFMeshPrimitiveRecord(void)
1469 OFRecordTransitPtr OFMeshPrimitiveRecord::create(
1470 const OFRecordHeader &oHeader, OFDatabase &oDB)
1472 return OFRecordTransitPtr(new OFMeshPrimitiveRecord(oHeader, oDB));
1475 bool OFMeshPrimitiveRecord::read(std::istream &is)
1477 OSG_OPENFLIGHT_LOG(("OFMeshPrimitiveRecord::read len [%u]\n",
1478 _sLength));
1480 Inherited::readVal(is, uiPrimType );
1481 Inherited::readVal(is, uiIndexSize );
1482 Inherited::readVal(is, uiVertexCount);
1484 return readContinue(is, _sLength - 12);
1487 bool OFMeshPrimitiveRecord::readContinue(
1488 std::istream &is, UInt16 uiLength)
1490 switch(uiIndexSize)
1492 case 1:
1494 for(Int32 bytesLeft = uiLength; bytesLeft > 0; bytesLeft -= 1)
1496 UInt8 tmpIdx;
1497 Inherited::readVal(is, tmpIdx);
1499 _vIndices.push_back(tmpIdx);
1501 break;
1504 case 2:
1506 for(Int32 bytesLeft = uiLength; bytesLeft > 0; bytesLeft -= 2)
1508 UInt16 tmpIdx;
1509 Inherited::readVal(is, tmpIdx);
1511 _vIndices.push_back(tmpIdx);
1513 break;
1516 case 4:
1518 for(Int32 bytesLeft = uiLength; bytesLeft > 0; bytesLeft -= 4)
1520 UInt32 tmpIdx;
1521 Inherited::readVal(is, tmpIdx);
1523 _vIndices.push_back(tmpIdx);
1525 break;
1528 default:
1530 FWARNING(("OFMeshPrimitiveRecord::readContinue: IndexSize has "
1531 "unrecognized value [%u].\n", uiIndexSize));
1533 return Inherited::readContinue(is, uiLength);
1537 return is.good();
1540 UInt16 OFMeshPrimitiveRecord::getOpCode(void) const
1542 return OpCode;
1545 void OFMeshPrimitiveRecord::dump(UInt32 uiIndent) const
1547 indentLog(uiIndent, PLOG);
1548 PLOG << "MeshPrimitiveRecord : " << std::endl;
1550 indentLog(uiIndent, PLOG);
1551 PLOG << "{" << std::endl;
1553 uiIndent += 2;
1555 indentLog(uiIndent, PLOG);
1556 PLOG << "PrimType : " << uiPrimType << std::endl;
1558 indentLog(uiIndent, PLOG);
1559 PLOG << "IndexSize : " << uiIndexSize << std::endl;
1561 indentLog(uiIndent, PLOG);
1562 PLOG << "VertexCount : " << uiVertexCount << std::endl;
1564 uiIndent -= 2;
1566 indentLog(uiIndent, PLOG);
1567 PLOG << "}" << std::endl;
1570 OFMeshPrimitiveRecord::IndexStore &OFMeshPrimitiveRecord::editIndices(void)
1572 return _vIndices;
1575 const OFMeshPrimitiveRecord::IndexStore &OFMeshPrimitiveRecord::getIndices(
1576 void) const
1578 return _vIndices;
1581 OFRecordFactoryBase::RegisterRecord OFMeshPrimitiveRecord::_regHelper(
1582 &OFMeshPrimitiveRecord::create,
1583 OFMeshPrimitiveRecord::OpCode );
1585 //---------------------------------------------------------------------
1586 // OFMeshRecord
1587 //---------------------------------------------------------------------
1589 OFMeshRecord::OFMeshRecord(const OFRecordHeader &oHeader,
1590 OFDatabase &oDB ) :
1591 Inherited (oHeader,
1592 oDB ),
1593 iPad1 (0 ),
1594 iIRColorCode (0 ),
1595 iRelPrio (0 ),
1596 iDrawType (0 ),
1597 iTextureWhite (0 ),
1598 uiColorNameIdx (0 ),
1599 uiAltColorNameIdx(0 ),
1600 iPad2 (0 ),
1601 iTemplate (0 ),
1602 iDetailTexIdx (0 ),
1603 iTexIdx (0 ),
1604 iMatIdx (0 ),
1605 iSurfMatCode (0 ),
1606 iFeatureId (0 ),
1607 iIRMatCode (0 ),
1608 uiTransparency (0 ),
1609 uiLODGenControl (0 ),
1610 uiLineStyle (0 ),
1611 iFlags (0 ),
1612 uiLightMode (0 ),
1613 uiPackedPrimCol (0 ),
1614 uiPackedAltCol (0 ),
1615 iTexMapIdx (0 ),
1616 iPad4 (0 ),
1617 uiPrimColIdx (0 ),
1618 uiAltColIdx (0 ),
1619 iPad5 (0 ),
1620 iShaderIdx (0 ),
1621 _pVertexPool (NULL )
1625 OFMeshRecord::~OFMeshRecord(void)
1629 OFRecordTransitPtr OFMeshRecord::create(const OFRecordHeader &oHeader,
1630 OFDatabase &oDB )
1632 return OFRecordTransitPtr(new OFMeshRecord(oHeader, oDB));
1635 bool OFMeshRecord::read(std::istream &is)
1637 OSG_OPENFLIGHT_LOG(("OFMeshRecord::read len [%u]\n",
1638 _sLength));
1640 Inherited::readChar8(is, szASCIIId, 8 );
1641 Inherited::readVal (is, iPad1 );
1642 Inherited::readVal (is, iIRColorCode );
1643 Inherited::readVal (is, iRelPrio );
1644 Inherited::readVal (is, iDrawType );
1645 Inherited::readVal (is, iTextureWhite );
1646 Inherited::readVal (is, uiColorNameIdx );
1647 Inherited::readVal (is, uiAltColorNameIdx);
1648 Inherited::readVal (is, iPad2 );
1649 Inherited::readVal (is, iTemplate );
1650 Inherited::readVal (is, iDetailTexIdx );
1651 Inherited::readVal (is, iTexIdx );
1652 Inherited::readVal (is, iMatIdx );
1653 Inherited::readVal (is, iSurfMatCode );
1654 Inherited::readVal (is, iFeatureId );
1655 Inherited::readVal (is, iIRMatCode );
1656 Inherited::readVal (is, uiTransparency );
1657 Inherited::readVal (is, uiLODGenControl );
1658 Inherited::readVal (is, uiLineStyle );
1659 Inherited::readVal (is, iFlags );
1660 Inherited::readVal (is, uiLightMode );
1661 Inherited::readChar8(is, szPad3, 7 );
1662 Inherited::readVal (is, uiPackedPrimCol );
1663 Inherited::readVal (is, uiPackedAltCol );
1664 Inherited::readVal (is, iTexMapIdx );
1665 Inherited::readVal (is, iPad4 );
1666 Inherited::readVal (is, uiPrimColIdx );
1667 Inherited::readVal (is, uiAltColIdx );
1668 Inherited::readVal (is, iPad5 );
1669 Inherited::readVal (is, iShaderIdx );
1671 return is.good();
1674 bool OFMeshRecord::addChild(OFRecord *pChild)
1676 if(pChild == NULL)
1677 return false;
1679 bool rc = false;
1681 switch(pChild->getOpCode())
1683 case OFLocalVertexPoolRecord::OpCode:
1685 if(_pVertexPool != NULL)
1686 FWARNING(("OFMeshRecord::addChild: VertexPool already set.\n"));
1688 _pVertexPool = dynamic_cast<OFLocalVertexPoolRecord *>(pChild);
1689 rc = true;
1690 break;
1693 default:
1695 FWARNING(("OFMeshRecord::addChild: Unexpected child type [%u - %s].\n",
1696 pChild->getOpCode(), getOpCodeString(pChild->getOpCode())));
1697 rc = Inherited::addChild(pChild);
1698 break;
1702 return rc;
1705 UInt16 OFMeshRecord::getOpCode(void) const
1707 return OpCode;
1710 void OFMeshRecord::dump(UInt32 uiIndent) const
1712 indentLog(uiIndent, PLOG);
1713 PLOG << "MeshRecord : " << std::endl;
1715 indentLog(uiIndent, PLOG);
1716 PLOG << "{" << std::endl;
1718 uiIndent += 2;
1720 indentLog(uiIndent, PLOG);
1721 PLOG << "ASCIIId : " << szASCIIId << std::endl;
1723 indentLog(uiIndent, PLOG);
1724 PLOG << "TextureWhite : " << static_cast<UInt32>(iTextureWhite) << std::endl;
1726 indentLog(uiIndent, PLOG);
1727 PLOG << "Template : " << static_cast<UInt32>(iTemplate) << std::endl;
1729 indentLog(uiIndent, PLOG);
1730 PLOG << "Transparency : " << uiTransparency << std::endl;
1732 indentLog(uiIndent, PLOG);
1733 PLOG << "DetailTexIdx : " << iDetailTexIdx << std::endl;
1734 indentLog(uiIndent, PLOG);
1735 PLOG << "TexIdx : " << iTexIdx << std::endl;
1736 indentLog(uiIndent, PLOG);
1737 PLOG << "TexMapIdx : " << iTexMapIdx << std::endl;
1738 indentLog(uiIndent, PLOG);
1739 PLOG << "MatIdx : " << iMatIdx << std::endl;
1741 PLOG.setf (std::ios::hex, std::ios::basefield);
1743 indentLog(uiIndent, PLOG);
1744 PLOG << "FullFlags : " << iFlags << std::endl;
1746 PLOG.setf (std::ios::dec, std::ios::basefield);
1748 indentLog(uiIndent, PLOG);
1749 PLOG << "Flags : "
1750 << "(T : " << ((iFlags & 0x80000000) >> 31) << ") "
1751 << "(NC : " << ((iFlags & 0x40000000) >> 30) << ") "
1752 << "(NAC : " << ((iFlags & 0x20000000) >> 29) << ") "
1753 << "(PC : " << ((iFlags & 0x10000000) >> 28) << ") "
1754 << std::endl;
1756 indentLog(uiIndent, PLOG);
1757 PLOG << "LightMode : " << UInt32(uiLightMode) << std::endl;
1759 if(uiLightMode == 0 || uiLightMode == 2)
1761 PLOG.setf (std::ios::hex, std::ios::basefield);
1763 indentLog(uiIndent, PLOG);
1764 PLOG << "ColorPacked : " << uiPackedPrimCol << std::endl;
1766 indentLog(uiIndent, PLOG);
1767 PLOG << "Color : B : "
1768 << (uiPackedPrimCol & 0x000000FF)
1769 << " G : "
1770 << ((uiPackedPrimCol & 0x0000FF00) >> 8)
1771 << " R : "
1772 << ((uiPackedPrimCol & 0x00FF0000) >> 16)
1773 << std::endl;
1775 PLOG.setf (std::ios::dec, std::ios::basefield);
1777 indentLog(uiIndent, PLOG);
1778 PLOG << "ColorIdx : " << uiPrimColIdx << std::endl;
1781 uiIndent -= 2;
1783 indentLog(uiIndent, PLOG);
1784 PLOG << "}" << std::endl;
1786 indentLog(uiIndent, PLOG);
1787 PLOG << "[" << std::endl;
1789 uiIndent += 2;
1791 if(_pVertexPool != NULL)
1792 _pVertexPool->dump(uiIndent);
1794 uiIndent -= 2;
1796 indentLog(uiIndent, PLOG);
1797 PLOG << "]" << std::endl;
1800 NodeTransitPtr OFMeshRecord::convertToNode(void)
1802 NodeUnrecPtr returnValue(NULL);
1804 if(_pVertexPool == NULL)
1805 return NodeTransitPtr(returnValue);
1807 returnValue = Node ::create();
1808 GeometryUnrecPtr pGeo = Geometry ::create();
1809 GeoUInt8PropertyUnrecPtr pTypes = GeoUInt8Property ::create();
1810 GeoUInt32PropertyUnrecPtr pLengths = GeoUInt32Property::create();
1812 pGeo->setTypes (pTypes );
1813 pGeo->setLengths(pLengths);
1815 returnValue->setCore(pGeo);
1817 _pVertexPool->convertPrimitives(pGeo);
1819 ChunkMaterialUnrecPtr pChunkMat = ChunkMaterial::create();
1821 if(iTextureWhite && iTexIdx != -1)
1823 MaterialChunkUnrecPtr pMatChunk = MaterialChunk::create();
1824 Color4f colDiffuse(1.f, 1.f, 1.f, 1.f);
1826 pMatChunk->setDiffuse(colDiffuse);
1828 pChunkMat->addChunk(pMatChunk);
1830 else if(iMatIdx != -1)
1832 const OFMaterialPaletteRecord *pMatRec = _oDB.getMatRecord(iMatIdx);
1834 if(pMatRec != NULL)
1836 MaterialChunkUnrecPtr pMatChunk = MaterialChunk::create();
1838 if((iFlags & FlagNoColor) != 0)
1840 // use material only
1841 Color4f colMat;
1843 pMatChunk->setAmbient(pMatRec->getAmbient());
1845 colMat = pMatRec->getDiffuse();
1846 colMat[3] = pMatRec->getAlpha() * (1.f - (uiTransparency / 65535.f));
1847 pMatChunk->setDiffuse(colMat);
1849 pMatChunk->setSpecular (pMatRec->getSpecular ());
1850 pMatChunk->setShininess(pMatRec->getShininess());
1851 pMatChunk->setEmission (pMatRec->getEmissive ());
1853 pChunkMat->addChunk(pMatChunk);
1855 else if((iFlags & FlagPackedColor) != 0)
1857 // use uiPackedPrimCol and material
1858 Color4f colGeo;
1859 Color4f colMat;
1861 colGeo[0] = ((uiPackedPrimCol & 0x00FF0000) >> 16) / 255.f;
1862 colGeo[1] = ((uiPackedPrimCol & 0x0000FF00) >> 8) / 255.f;
1863 colGeo[2] = ((uiPackedPrimCol & 0x000000FF) ) / 255.f;
1864 colGeo[3] = 1.f;
1866 colMat = pMatRec->getAmbient();
1867 colMat[0] = colMat[0] * colGeo[0];
1868 colMat[1] = colMat[1] * colGeo[1];
1869 colMat[2] = colMat[2] * colGeo[2];
1871 pMatChunk->setAmbient(colMat);
1873 colMat = pMatRec->getDiffuse();
1874 colMat[0] = colMat[0] * colGeo[0];
1875 colMat[1] = colMat[1] * colGeo[1];
1876 colMat[2] = colMat[2] * colGeo[2];
1877 colMat[3] = pMatRec->getAlpha() * (1.f - (uiTransparency / 65535.f));
1879 pMatChunk->setDiffuse (colMat );
1880 pMatChunk->setSpecular (pMatRec->getSpecular ());
1881 pMatChunk->setShininess(pMatRec->getShininess());
1882 pMatChunk->setEmission (pMatRec->getEmissive ());
1884 pChunkMat->addChunk(pMatChunk);
1886 else
1888 // TODO: use uiPrimColIdx
1891 if(pMatChunk->isTransparent() &&
1892 pChunkMat->find(BlendChunk::getClassType()) == NULL)
1894 BlendChunkUnrecPtr pBlendChunk = BlendChunk::create();
1896 pBlendChunk->setSrcFactor (GL_SRC_ALPHA );
1897 pBlendChunk->setDestFactor(GL_ONE_MINUS_SRC_ALPHA);
1899 pChunkMat->addChunk(pBlendChunk);
1904 if(iTexIdx != -1)
1906 const OFTexturePaletteRecord *pTexRec = _oDB.getTexRecord(iTexIdx);
1908 if(pTexRec != NULL)
1910 TextureObjChunk *pTexObj = pTexRec->getTexObj();
1911 TextureEnvChunk *pTexEnv = pTexRec->getTexEnv();
1913 if(pTexObj != NULL)
1915 pChunkMat->addChunk(pTexObj);
1917 if(pTexObj->getImage()->hasAlphaChannel() &&
1918 pChunkMat->find(BlendChunk::getClassType()) == NULL)
1920 BlendChunkUnrecPtr pBlendChunk = BlendChunk::create();
1922 pBlendChunk->setSrcFactor (GL_SRC_ALPHA );
1923 pBlendChunk->setDestFactor(GL_ONE_MINUS_SRC_ALPHA);
1925 pChunkMat->addChunk(pBlendChunk);
1929 if(pTexEnv != NULL)
1931 pChunkMat->addChunk(pTexEnv);
1933 else
1935 TextureEnvChunkUnrecPtr pNewTexEnv = TextureEnvChunk::create();
1937 pNewTexEnv->setEnvMode(GL_MODULATE);
1939 pChunkMat->addChunk(pNewTexEnv);
1942 else
1944 FFATAL(("OFMeshRecord::convertToNode: "
1945 "No texture record for index [%d].\n", iTexIdx));
1949 if(uiLightMode == LMMeshColor || uiLightMode == LMMeshColorLit)
1951 MaterialChunk *pMatChunk = dynamic_cast<MaterialChunk *>(
1952 pChunkMat->find(MaterialChunk::getClassType()));
1954 if(pMatChunk != NULL)
1956 pMatChunk->setColorMaterial(GL_NONE);
1958 if(uiLightMode == LMMeshColor)
1959 pMatChunk->setLit(false);
1963 if(uiLightMode == LMMeshColorLit || uiLightMode == LMVertexColorLit)
1965 if(pGeo->getProperty(Geometry::NormalsIndex) == NULL)
1966 calcVertexNormals(pGeo);
1969 switch(iDrawType)
1971 case DTSolidCullBack:
1973 PolygonChunkUnrecPtr pPolyChunk = PolygonChunk::create();
1974 pPolyChunk->setCullFace(GL_BACK);
1976 pChunkMat->addChunk(pPolyChunk);
1977 break;
1980 case DTSolid:
1982 // nothing to do
1983 break;
1986 case DTWireframeClosed:
1987 case DTWireframe:
1989 PolygonChunkUnrecPtr pPolyChunk = PolygonChunk::create();
1990 pPolyChunk->setFrontMode(GL_LINE);
1991 pPolyChunk->setBackMode (GL_LINE);
1993 pChunkMat->addChunk(pPolyChunk);
1994 break;
1997 default:
1999 FWARNING(("OFMeshRecord::convertToNode: Unhandled draw "
2000 "type [%d].\n", iDrawType));
2001 break;
2006 pGeo->setMaterial(pChunkMat);
2008 return NodeTransitPtr(returnValue);
2011 OFRecordFactoryBase::RegisterRecord OFMeshRecord::_regHelper(
2012 &OFMeshRecord::create,
2013 OFMeshRecord::OpCode );
2015 //---------------------------------------------------------------------
2016 // OFFaceRecord
2017 //---------------------------------------------------------------------
2019 OFRecordTransitPtr OFFaceRecord::create(const OFRecordHeader &oHeader,
2020 OFDatabase &oDB )
2022 return OFRecordTransitPtr(new OFFaceRecord(oHeader, oDB));
2025 OFFaceRecord::OFFaceRecord(const OFRecordHeader &oHeader,
2026 OFDatabase &oDB ) :
2027 Inherited (oHeader,
2028 oDB ),
2029 iIRColorCode (0 ),
2030 iRelPrio (0 ),
2031 iDrawType (0 ),
2032 iTextureWhite (0 ),
2033 uiColorNameIdx (0 ),
2034 uiAltColorNameIdx(0 ),
2035 iPad1 (0 ),
2036 iTemplate (0 ),
2037 iDetailTexIdx (0 ),
2038 iTexIdx (0 ),
2039 iMatIdx (0 ),
2040 iSurfMatCode (0 ),
2041 iFeatureId (0 ),
2042 iIRMatCode (0 ),
2043 uiTransparency (0 ),
2044 uiLODGenControl (0 ),
2045 uiLineStyle (0 ),
2046 iFlags (0 ),
2047 uiLightMode (0 ),
2048 uiPackedPrimCol (0 ),
2049 uiPackedAltCol (0 ),
2050 iTexMapIdx (0 ),
2051 iPad3 (0 ),
2052 uiPrimColIdx (0 ),
2053 uiAltColIdx (0 ),
2054 iPad4 (0 ),
2055 iShaderIdx (0 ),
2056 _pVList (NULL )
2060 OFFaceRecord::~OFFaceRecord(void)
2062 _pVList = NULL;
2065 bool OFFaceRecord::read(std::istream &is)
2067 OSG_OPENFLIGHT_LOG(("OFFaceRecord::read len [%u]\n",
2068 _sLength));
2070 Inherited::readChar8(is, szASCIIId, 8 );
2071 Inherited::readVal (is, iIRColorCode );
2072 Inherited::readVal (is, iRelPrio );
2073 Inherited::readVal (is, iDrawType );
2074 Inherited::readVal (is, iTextureWhite );
2075 Inherited::readVal (is, uiColorNameIdx );
2076 Inherited::readVal (is, uiAltColorNameIdx);
2077 Inherited::readVal (is, iPad1 );
2078 Inherited::readVal (is, iTemplate );
2079 Inherited::readVal (is, iDetailTexIdx );
2080 Inherited::readVal (is, iTexIdx );
2081 Inherited::readVal (is, iMatIdx );
2082 Inherited::readVal (is, iSurfMatCode );
2083 Inherited::readVal (is, iFeatureId );
2084 Inherited::readVal (is, iIRMatCode );
2085 Inherited::readVal (is, uiTransparency );
2086 Inherited::readVal (is, uiLODGenControl );
2087 Inherited::readVal (is, uiLineStyle );
2088 Inherited::readVal (is, iFlags );
2089 Inherited::readVal (is, uiLightMode );
2090 Inherited::readChar8(is, szPad2, 7 );
2091 Inherited::readVal (is, uiPackedPrimCol );
2092 Inherited::readVal (is, uiPackedAltCol );
2093 Inherited::readVal (is, iTexMapIdx );
2094 Inherited::readVal (is, iPad3 );
2095 Inherited::readVal (is, uiPrimColIdx );
2096 Inherited::readVal (is, uiAltColIdx );
2097 Inherited::readVal (is, iPad4 );
2098 Inherited::readVal (is, iShaderIdx );
2100 return is.good();
2103 bool OFFaceRecord::addChild(OFRecord *pChild)
2105 if(pChild == NULL)
2106 return false;
2108 switch(pChild->getOpCode())
2110 case OFVertexListRecord::OpCode:
2112 if(_pVList != NULL)
2114 FWARNING(("OFFaceRecord::addChild: Vertex List already set.\n"));
2116 else
2118 _pVList = dynamic_cast<OFVertexListRecord *>(pChild);
2121 break;
2123 default:
2125 Inherited::addChild(pChild);
2127 break;
2130 return true;
2133 UInt16 OFFaceRecord::getOpCode(void) const
2135 return OpCode;
2138 OFVertexListRecord *OFFaceRecord::getVertexList(void)
2140 return _pVList;
2143 void OFFaceRecord::dump(UInt32 uiIndent) const
2145 indentLog(uiIndent, PLOG);
2146 PLOG << "FaceRecord : " << std::endl;
2148 indentLog(uiIndent, PLOG);
2149 PLOG << "{" << std::endl;
2151 uiIndent += 2;
2153 indentLog(uiIndent, PLOG);
2154 PLOG << "TextureWhite : " << UInt32(iTextureWhite) << std::endl;
2156 indentLog(uiIndent, PLOG);
2157 PLOG << "TexIdx : " << iTexIdx << std::endl;
2158 indentLog(uiIndent, PLOG);
2159 PLOG << "TexMapIdx : " << iTexMapIdx << std::endl;
2160 indentLog(uiIndent, PLOG);
2161 PLOG << "MatIdx : " << iMatIdx << std::endl;
2162 indentLog(uiIndent, PLOG);
2163 PLOG << "Transparency : " << uiTransparency << std::endl;
2165 PLOG.setf (std::ios::hex, std::ios::basefield);
2167 indentLog(uiIndent, PLOG);
2168 PLOG << "FullFlags : " << iFlags << std::endl;
2170 PLOG.setf (std::ios::dec, std::ios::basefield);
2172 indentLog(uiIndent, PLOG);
2173 PLOG << "Flags : "
2174 << "(T : " << ((iFlags & 0x80000000) >> 31) << ") "
2175 << "(NC : " << ((iFlags & 0x40000000) >> 30) << ") "
2176 << "(NAC : " << ((iFlags & 0x20000000) >> 29) << ") "
2177 << "(PC : " << ((iFlags & 0x10000000) >> 28) << ") "
2178 << std::endl;
2180 indentLog(uiIndent, PLOG);
2181 PLOG << "LightMode : " << UInt32(uiLightMode) << std::endl;
2183 if(uiLightMode == 0 || uiLightMode == 2)
2185 PLOG.setf (std::ios::hex, std::ios::basefield);
2187 indentLog(uiIndent, PLOG);
2188 PLOG << "ColorPacked : " << uiPackedPrimCol << std::endl;
2190 indentLog(uiIndent, PLOG);
2191 PLOG << "Color : B : "
2192 << (uiPackedPrimCol & 0x000000FF)
2193 << " G : "
2194 << ((uiPackedPrimCol & 0x0000FF00) >> 8)
2195 << " R : "
2196 << ((uiPackedPrimCol & 0x00FF0000) >> 16)
2197 << std::endl;
2199 PLOG.setf (std::ios::dec, std::ios::basefield);
2201 indentLog(uiIndent, PLOG);
2202 PLOG << "ColorIdx : " << uiPrimColIdx << std::endl;
2205 uiIndent -= 2;
2207 indentLog(uiIndent, PLOG);
2208 PLOG << "}" << std::endl;
2211 bool OFFaceRecord::operator ==(const OFFaceRecord &rhs) const
2213 bool returnValue = (iDrawType == rhs.iDrawType &&
2214 iTextureWhite == rhs.iTextureWhite &&
2215 iTexIdx == rhs.iTexIdx &&
2216 iMatIdx == rhs.iMatIdx &&
2217 uiLightMode == rhs.uiLightMode );
2219 if(uiLightMode == 0 || uiLightMode == 2)
2221 if((iFlags & FlagPackedColor) != 0)
2223 returnValue &= (uiPackedPrimCol == rhs.uiPackedPrimCol);
2225 else
2227 returnValue &= (uiPrimColIdx == rhs.uiPrimColIdx);
2231 return returnValue;
2234 Int8 OFFaceRecord::getDrawType(void) const
2236 return iDrawType;
2239 Int8 OFFaceRecord::getTextureWhite(void) const
2241 return iTextureWhite;
2244 Int16 OFFaceRecord::getTexIdx(void) const
2246 return iTexIdx;
2249 Int16 OFFaceRecord::getMatIdx(void) const
2251 return iMatIdx;
2254 UInt16 OFFaceRecord::getTransparency(void) const
2256 return uiTransparency;
2259 Int32 OFFaceRecord::getFlags(void) const
2261 return iFlags;
2264 UInt8 OFFaceRecord::getLightMode(void) const
2266 return uiLightMode;
2269 Color4f OFFaceRecord::getPrimColor(void) const
2271 Color4f returnValue;
2273 if((iFlags & FlagPackedColor) != 0)
2275 returnValue[0] = ((uiPackedPrimCol & 0x00FF0000) >> 16) / 255.f;
2276 returnValue[1] = ((uiPackedPrimCol & 0x0000FF00) >> 8) / 255.f;
2277 returnValue[2] = ((uiPackedPrimCol & 0x000000FF) ) / 255.f;
2278 returnValue[3] = 1.f;
2280 else if((iFlags & FlagNoColor) == 0)
2282 const OFColorPaletteRecord *colPal = _oDB.getColorPalette();
2284 returnValue = colPal->getColor(uiPrimColIdx);
2287 return returnValue;
2290 Color4f OFFaceRecord::getAltColor(void) const
2292 Color4f returnValue;
2294 if((iFlags & FlagPackedColor) != 0)
2296 returnValue[0] = ((uiPackedAltCol & 0x00FF0000) >> 16) / 255.f;
2297 returnValue[1] = ((uiPackedAltCol & 0x0000FF00) >> 8) / 255.f;
2298 returnValue[2] = ((uiPackedAltCol & 0x000000FF) ) / 255.f;
2299 returnValue[3] = 1.f;
2301 else if((iFlags & FlagNoColor) == 0)
2303 const OFColorPaletteRecord *colPal = _oDB.getColorPalette();
2305 returnValue = colPal->getColor(uiAltColIdx);
2308 return returnValue;
2311 OFRecordFactoryBase::RegisterRecord OFFaceRecord::_regHelper(
2312 &OFFaceRecord::create,
2313 OFFaceRecord::OpCode);
2315 //---------------------------------------------------------------------
2316 // OFVertexListRecord
2317 //---------------------------------------------------------------------
2320 OFRecordTransitPtr OFVertexListRecord::create(const OFRecordHeader &oHeader,
2321 OFDatabase &oDB )
2323 return OFRecordTransitPtr(new OFVertexListRecord(oHeader, oDB));
2326 OFVertexListRecord::OFVertexListRecord(const OFRecordHeader &oHeader,
2327 OFDatabase &oDB ) :
2328 Inherited(oHeader, oDB),
2329 _vIndices ( )
2333 OFVertexListRecord::~OFVertexListRecord(void)
2337 bool OFVertexListRecord::read(std::istream &is)
2339 UInt32 uiListSize = (_sLength - 4) / 4;
2341 OSG_OPENFLIGHT_LOG(("OFVertexListRecord::read len [%u], indices [%u]\n",
2342 _sLength, uiListSize));
2344 _vIndices.resize(uiListSize);
2346 for(UInt32 i = 0; i < uiListSize; ++i)
2348 Inherited::readVal(is, _vIndices[i]);
2351 return is.good();
2354 UInt16 OFVertexListRecord::getOpCode(void) const
2356 return OpCode;
2359 const std::vector<Int32> &OFVertexListRecord::getIndices(void)
2361 return _vIndices;
2364 OFRecordFactoryBase::RegisterRecord OFVertexListRecord::_regHelper(
2365 &OFVertexListRecord::create,
2366 OFVertexListRecord::OpCode);
2368 //---------------------------------------------------------------------
2369 // OFLODRecord
2370 //---------------------------------------------------------------------
2372 OFRecordTransitPtr OFLODRecord::create(const OFRecordHeader &oHeader,
2373 OFDatabase &oDB )
2375 return OFRecordTransitPtr(new OFLODRecord(oHeader, oDB));
2378 OFLODRecord::OFLODRecord(const OFRecordHeader &oHeader,
2379 OFDatabase &oDB ) :
2380 Inherited (oHeader,
2381 oDB ),
2382 iPad1 (0 ),
2383 rSwitchIn (0 ),
2384 rSwitchOut (0 ),
2385 iSpecialEff1(0 ),
2386 iSpecialEff2(0 ),
2387 iFlags (0 ),
2388 rCenterX (0 ),
2389 rCenterY (0 ),
2390 rCenterZ (0 ),
2391 rTransRange (0 ),
2392 rSigSize (0 )
2396 OFLODRecord::~OFLODRecord(void)
2400 bool OFLODRecord::read(std::istream &is)
2402 OSG_OPENFLIGHT_LOG(("OFLODRecord::read len [%u]\n",
2403 _sLength));
2405 Inherited::readChar8(is, szASCIIId, 8);
2406 Inherited::readVal (is, iPad1 );
2407 Inherited::readVal (is, rSwitchIn );
2408 Inherited::readVal (is, rSwitchOut );
2409 Inherited::readVal (is, iSpecialEff1);
2410 Inherited::readVal (is, iSpecialEff2);
2411 Inherited::readVal (is, iFlags );
2412 Inherited::readVal (is, rCenterX );
2413 Inherited::readVal (is, rCenterY );
2414 Inherited::readVal (is, rCenterZ );
2415 Inherited::readVal (is, rTransRange );
2417 if(_sLength >= 80)
2418 Inherited::readVal(is, rSigSize);
2420 rSwitchIn *= _oDB.getUnitScale();
2421 rSwitchOut *= _oDB.getUnitScale();
2423 rCenterX *= _oDB.getUnitScale();
2424 rCenterY *= _oDB.getUnitScale();
2425 rCenterZ *= _oDB.getUnitScale();
2427 return is.good();
2430 UInt16 OFLODRecord::getOpCode(void) const
2432 return OpCode;
2435 void OFLODRecord::dump(UInt32 uiIndent) const
2437 indentLog(uiIndent, PLOG);
2438 PLOG << "LODRecord" << std::endl;
2440 indentLog(uiIndent, PLOG);
2441 PLOG << "{" << std::endl;
2443 uiIndent += 2;
2445 indentLog(uiIndent, PLOG);
2446 PLOG << "rSwitchIn : " << rSwitchIn << std::endl;
2448 indentLog(uiIndent, PLOG);
2449 PLOG << "rSwitchOut : " << rSwitchOut << std::endl;
2451 uiIndent -= 2;
2453 indentLog(uiIndent, PLOG);
2454 PLOG << "}" << std::endl;
2456 Inherited::dump(uiIndent);
2459 NodeTransitPtr OFLODRecord::convertToNode(void)
2461 NodeUnrecPtr returnValue(NULL);
2463 if(_primaryChildren.empty() == false)
2465 RangeLODUnrecPtr pCore = RangeLOD::create();
2466 NodeUnrecPtr pNode = makeNodeFor(pCore);
2468 pCore->setSwitchIn (this->getSwitchIn ());
2469 pCore->setSwitchOut(this->getSwitchOut());
2471 pCore->setCenter (this->getCenter ());
2473 if(returnValue != NULL)
2475 returnValue->addChild(pNode);
2477 else
2479 returnValue = pNode;
2482 for(UInt32 i = 0; i < _primaryChildren.size(); ++i)
2484 NodeTransitPtr pChild = _primaryChildren[i]->convertToNode();
2486 if(pChild != NULL)
2488 returnValue->addChild(pChild);
2493 returnValue = convertAncillaryChildren(returnValue);
2495 return NodeTransitPtr(returnValue);
2498 Real64 OFLODRecord::getSwitchIn (void)
2500 return rSwitchIn;
2503 Real64 OFLODRecord::getSwitchOut(void)
2505 return rSwitchOut;
2508 Pnt3f OFLODRecord::getCenter(void)
2510 return Pnt3f(rCenterX, rCenterY, rCenterZ);
2513 NodeTransitPtr OFLODRecord::convertAncillaryChildren(Node *pNode)
2515 NodeUnrecPtr returnValue(pNode);
2517 for(UInt32 i = 0; i < _ancillaryChildren.size(); ++i)
2519 returnValue = _ancillaryChildren[i]->convert(returnValue);
2522 return NodeTransitPtr(returnValue);
2525 OFRecordFactoryBase::RegisterRecord OFLODRecord::_regHelper(
2526 &OFLODRecord::create,
2527 OFLODRecord::OpCode);
2529 //---------------------------------------------------------------------
2530 // OFSwitchRecord
2531 //---------------------------------------------------------------------
2533 OFRecordTransitPtr OFSwitchRecord::create(const OFRecordHeader &oHeader,
2534 OFDatabase &oDB )
2536 return OFRecordTransitPtr(new OFSwitchRecord(oHeader, oDB));
2539 OFSwitchRecord::OFSwitchRecord(const OFRecordHeader &oHeader,
2540 OFDatabase &oDB ) :
2541 Inherited (oHeader,
2542 oDB ),
2543 szPad1 (0 ),
2544 iCurrMask (0 ),
2545 iNumMask (0 ),
2546 iMaskWords(0 ),
2547 vMask (0 )
2551 OFSwitchRecord::~OFSwitchRecord(void)
2555 bool OFSwitchRecord::read(std::istream &is)
2557 OSG_OPENFLIGHT_LOG(("OFSwitchRecord::read len [%u]\n",
2558 _sLength));
2560 Inherited::readChar8(is, szASCIIID, 8);
2561 Inherited::readVal (is, szPad1 );
2562 Inherited::readVal (is, iCurrMask );
2563 Inherited::readVal (is, iNumMask );
2564 Inherited::readVal (is, iMaskWords );
2566 vMask.resize(iNumMask);
2568 for(Int32 i = 0; i < iNumMask; ++i)
2570 vMask[i].resize(iMaskWords);
2573 for(Int32 i = 0; i < iNumMask; ++i)
2575 for(Int32 j = 0; j < iMaskWords; ++j)
2577 Inherited::readVal(is, vMask[i][j]);
2581 return is.good();
2584 UInt16 OFSwitchRecord::getOpCode(void) const
2586 return OpCode;
2590 void OFSwitchRecord::dump(UInt32 uiIndent) const
2592 indentLog(uiIndent, PLOG);
2593 PLOG << "SwitchRecord" << std::endl;
2595 indentLog(uiIndent, PLOG);
2596 PLOG << "{" << std::endl;
2598 uiIndent += 2;
2600 indentLog(uiIndent, PLOG);
2601 PLOG << "iCurrMask : " << iCurrMask << std::endl;
2602 indentLog(uiIndent, PLOG);
2603 PLOG << "iNumMask : " << iNumMask << std::endl;
2604 indentLog(uiIndent, PLOG);
2605 PLOG << "iMaskWords : " << iMaskWords << std::endl;
2607 PLOG.setf (std::ios::hex, std::ios::basefield);
2609 for(Int32 i = 0; i < iNumMask; ++i)
2611 indentLog(uiIndent, PLOG);
2612 PLOG << "Mask[" << i << "] : ";
2614 for(Int32 j = 0; j < iMaskWords; ++j)
2616 PLOG << vMask[i][j] << " ";
2618 PLOG << std::endl;
2621 PLOG.setf(std::ios::dec, std::ios::basefield);
2623 uiIndent -= 2;
2625 indentLog(uiIndent, PLOG);
2626 PLOG << "}" << std::endl;
2628 Inherited::dump(uiIndent);
2631 NodeTransitPtr OFSwitchRecord::convertToNode(void)
2633 NodeUnrecPtr returnValue(NULL);
2635 if(_primaryChildren.empty() == false)
2637 GroupUnrecPtr pCore = Group::create();
2638 NodeUnrecPtr pNode = makeNodeFor(pCore);
2640 if(returnValue != NULL)
2642 returnValue->addChild(pNode);
2644 else
2646 returnValue = pNode;
2649 for(UInt32 i = 0; i < _primaryChildren.size(); ++i)
2651 NodeTransitPtr pChild = _primaryChildren[i]->convertToNode();
2653 if(pChild != NULL)
2655 returnValue->addChild(pChild);
2660 returnValue = convertAncillaryChildren(returnValue);
2662 return NodeTransitPtr(returnValue);
2665 NodeTransitPtr OFSwitchRecord::convertAncillaryChildren(Node *pNode)
2667 NodeUnrecPtr returnValue(pNode);
2669 for(UInt32 i = 0; i < _ancillaryChildren.size(); ++i)
2671 returnValue = _ancillaryChildren[i]->convert(returnValue);
2674 return NodeTransitPtr(returnValue);
2677 OFRecordFactoryBase::RegisterRecord OFSwitchRecord::_regHelper(
2678 &OFSwitchRecord::create,
2679 OFSwitchRecord::OpCode);
2681 //---------------------------------------------------------------------
2682 // OFExternalReferenceRecord
2683 //---------------------------------------------------------------------
2685 OFRecordTransitPtr OFExternalReferenceRecord::create(
2686 const OFRecordHeader &oHeader, OFDatabase &oDB)
2688 return OFRecordTransitPtr(new OFExternalReferenceRecord(oHeader, oDB));
2691 OFExternalReferenceRecord::OFExternalReferenceRecord(
2692 const OFRecordHeader &oHeader, OFDatabase &oDB) :
2694 Inherited (oHeader,
2695 oDB ),
2696 iPad1 (0 ),
2697 iFlags (0 ),
2698 iViewAsBBox(0 ),
2699 iPad2 (0 )
2703 OFExternalReferenceRecord::~OFExternalReferenceRecord(void)
2707 bool OFExternalReferenceRecord::read(std::istream &is)
2709 Inherited::readChar8(is, szFilename, 200);
2710 Inherited::readVal (is, iPad1 );
2711 Inherited::readVal (is, iFlags );
2712 Inherited::readVal (is, iViewAsBBox );
2713 Inherited::readVal (is, iPad2 );
2715 OSG_OPENFLIGHT_LOG(("OFExternalReferenceRecord::read len [%u] - file [%s]\n",
2716 _sLength, szFilename));
2718 return is.good();
2721 UInt16 OFExternalReferenceRecord::getOpCode(void) const
2723 return OpCode;
2726 void OFExternalReferenceRecord::dump(UInt32 uiIndent) const
2728 indentLog(uiIndent, PLOG);
2729 PLOG << "ExternalReference : " << std::endl;
2731 indentLog(uiIndent, PLOG);
2732 PLOG << "{" << std::endl;
2734 uiIndent += 2;
2736 indentLog(uiIndent, PLOG);
2737 PLOG << "Filename : " << szFilename << std::endl;
2739 PLOG.setf (std::ios::hex, std::ios::basefield);
2741 indentLog(uiIndent, PLOG);
2742 PLOG << "FullFlags : " << iFlags << std::endl;
2744 PLOG.setf (std::ios::dec, std::ios::basefield);
2746 indentLog(uiIndent, PLOG);
2747 PLOG << "Flags : "
2748 << "(CP : " << ((iFlags & 0x80000000) >> 31) << ") "
2749 << "(MP : " << ((iFlags & 0x40000000) >> 30) << ") "
2750 << "(TP : " << ((iFlags & 0x20000000) >> 29) << ") "
2751 << "(LP : " << ((iFlags & 0x10000000) >> 28) << ") "
2752 << "(SP : " << ((iFlags & 0x08000000) >> 27) << ") "
2753 << "(LSP : " << ((iFlags & 0x04000000) >> 26) << ") "
2754 << "(LPP : " << ((iFlags & 0x02000000) >> 25) << ") "
2755 << "(SHP : " << ((iFlags & 0x01000000) >> 24) << ") "
2756 << std::endl;
2758 uiIndent -= 2;
2760 indentLog(uiIndent, PLOG);
2761 PLOG << "}" << std::endl;
2763 Inherited::dump(uiIndent);
2766 NodeTransitPtr OFExternalReferenceRecord::convertToNode(void)
2768 NodeUnrecPtr returnValue;
2770 PathHandler *pHandler = SceneFileHandler::the()->getPathHandler();
2772 if(pHandler != NULL)
2774 std::string szFilenameResolved = pHandler->findFile(szFilename);
2776 if(szFilenameResolved.empty() == true)
2778 FWARNING(("OFExternalReferenceRecord::convertToNode: Could not "
2779 "find file [%s].\n", szFilename));
2781 return NodeTransitPtr(returnValue);
2784 OSG_OPENFLIGHT_LOG(("OFExternalReferenceRecord::convertToNode: "
2785 "reading file [%s]\n",
2786 szFilenameResolved.c_str()));
2788 pHandler->pushState();
2790 pHandler->setBaseFile(szFilenameResolved.c_str());
2792 std::ifstream is;
2794 is.open(szFilenameResolved.c_str(), std::ios::binary);
2796 NodeUnrecPtr pExtNode =
2797 OpenFlightSceneFileType::the().read(is,
2798 szFilenameResolved.c_str());
2800 if(pExtNode != NULL && returnValue != NULL)
2802 returnValue->addChild(pExtNode);
2804 else if(pExtNode != NULL)
2806 returnValue = pExtNode;
2809 pHandler->popState();
2811 else
2813 FFATAL(("OFExternalReferenceRecord::convertToNode: No PathHandler.\n"));
2816 returnValue = convertAncillaryChildren(returnValue);
2818 return NodeTransitPtr(returnValue);
2821 NodeTransitPtr OFExternalReferenceRecord::convertAncillaryChildren(Node *pNode)
2823 NodeUnrecPtr returnValue(pNode);
2825 for(UInt32 i = 0; i < _ancillaryChildren.size(); ++i)
2827 returnValue = _ancillaryChildren[i]->convert(returnValue);
2830 return NodeTransitPtr(returnValue);
2833 OFRecordFactoryBase::RegisterRecord OFExternalReferenceRecord::_regHelper(
2834 &OFExternalReferenceRecord::create,
2835 OFExternalReferenceRecord::OpCode);
2837 OSG_END_NAMESPACE