From a6eb0a820971833f5e455a2cfe75c05213bbdeff Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 25 Jul 2002 15:21:18 +0000 Subject: [PATCH] Fix for colors in old flt models from Brede. --- src/osgPlugins/flt/Pool.cpp | 52 ++++++++++++++++++++++++++++++---- src/osgPlugins/flt/Pool.h | 3 +- src/osgPlugins/flt/flt2osg.cpp | 32 ++++++++++++--------- src/osgPlugins/flt/flt2osg.h | 2 +- 4 files changed, 67 insertions(+), 22 deletions(-) diff --git a/src/osgPlugins/flt/Pool.cpp b/src/osgPlugins/flt/Pool.cpp index 3f939d257..76aab52c3 100644 --- a/src/osgPlugins/flt/Pool.cpp +++ b/src/osgPlugins/flt/Pool.cpp @@ -14,19 +14,21 @@ using namespace flt; -osg::Vec4 ColorPool::getColor(int nColorIndex) +osg::Vec4 ColorPool::getColor(int nColorIntensity) { + // nColorIntensity: + // bit 0-6: intensity + // bit 7-15 color index + osg::Vec4 col(1,1,1,1); - if (nColorIndex >= 0) + if (nColorIntensity >= 0) { - int index = nColorIndex / 128; // = nColorIndex >> 7 - float intensity = (nColorIndex % 128) / 128.0f; // = nColorIndex & 0x7f - - ColorName* cn = getColorName(index); + ColorName* cn = getColorName(nColorIntensity >> 7); if (cn) col = cn->getColor(); + float intensity = (float)(nColorIntensity & 0x7f)/127.f; col[0] *= intensity; col[1] *= intensity; col[2] *= intensity; @@ -36,6 +38,44 @@ osg::Vec4 ColorPool::getColor(int nColorIndex) } +// getColor for version 11, 12 & 13. +osg::Vec4 ColorPool::getOldColor(int nColorIntensity) +{ + // nColorIntensity: + // bit 0-6: intensity + // bit 7-11 color index + // bit 12 fixed intensity bit + + osg::Vec4 col(1,1,1,1); + + if (nColorIntensity >= 0) + { + int nIndex; + bool bFixedIntensity = (nColorIntensity & 0x1000) ? true : false; + + if (bFixedIntensity) + nIndex = (nColorIntensity & 0x0fff)+(4096>>7); + else + nIndex = nColorIntensity >> 7; + + ColorName* cn = getColorName(nIndex); + if (cn) + col = cn->getColor(); + + // intensity + if (!bFixedIntensity) + { + float intensity = (float)(nColorIntensity & 0x7f)/127.f; + col[0] *= intensity; + col[1] *= intensity; + col[2] *= intensity; + } + } + + return col; +} + + void ColorPool::addColor(int nIndex, const osg::Vec4& color) { if (nIndex >= 0) diff --git a/src/osgPlugins/flt/Pool.h b/src/osgPlugins/flt/Pool.h index 30d215093..4f8f22d07 100644 --- a/src/osgPlugins/flt/Pool.h +++ b/src/osgPlugins/flt/Pool.h @@ -24,7 +24,8 @@ class ColorPool : public osg::Referenced ColorPool() {} - osg::Vec4 getColor(int nColorIndex); + osg::Vec4 getColor(int nColorIntensity); + osg::Vec4 getOldColor(int nColorIntensity); void addColor(int nIndex, const osg::Vec4& color); protected : diff --git a/src/osgPlugins/flt/flt2osg.cpp b/src/osgPlugins/flt/flt2osg.cpp index ec2d549de..b0baf6099 100644 --- a/src/osgPlugins/flt/flt2osg.cpp +++ b/src/osgPlugins/flt/flt2osg.cpp @@ -378,7 +378,7 @@ void ConvertFromFLT::visitColorPalette(osg::Group& , ColorPaletteRecord* rec) { osg::Vec4 color(pCol->FixedColors[i].get()); color[3] = 1.0f; // Force alpha to one - pColorPool->addColor(i+4096, color); + pColorPool->addColor(i+(4096>>7), color); } } } @@ -627,26 +627,30 @@ osg::Group* ConvertFromFLT::visitDOF(osg::Group& osgParent, DofRecord* rec) visitAncillary(osgParent, *transform, rec)->addChild( transform ); visitPrimaryNode(*transform, (PrimNodeRecord*)rec); + // note for Judd (and others) shouldn't there be code in here to set up the transform matrix? + // as a transform with an identity matrix is effectively only a + // a Group... I will leave for other more familiar with the + // DofRecord to create the matrix as I don't have any Open Flight + // documentation. RO August 2001. + + // below is DOF code submitted by Sasa Bistrovic SDegreeOfFreedom* p_data = rec->getData(); - osg::Vec3 trans(_unitScale*p_data->dfX._dfCurrent, - _unitScale*p_data->dfY._dfCurrent, - _unitScale*p_data->dfZ._dfCurrent); + osg::Matrix mat; + + mat.makeTranslate(_unitScale*p_data->dfX._dfCurrent, + _unitScale*p_data->dfY._dfCurrent, + _unitScale*p_data->dfZ._dfCurrent); float roll_rad = osg::inDegrees(p_data->dfRoll._dfCurrent); float pitch_rad = osg::inDegrees(p_data->dfPitch._dfCurrent); float yaw_rad = osg::inDegrees(p_data->dfYaw._dfCurrent); - float sx = rec->getData()->dfXscale.current(); - float sy = rec->getData()->dfYscale.current(); - float sz = rec->getData()->dfZscale.current(); + osg::Matrix mat_rot = osg::Matrix::rotate(-yaw_rad, pitch_rad,roll_rad); - transform->setMatrix( osg::Matrix::scale(sx, sy, sz)* - osg::Matrix::rotate(-yaw_rad, 0.0f,0.0f,1.0f)* - osg::Matrix::rotate(roll_rad, 0.0f,1.0f,0.0f)* - osg::Matrix::rotate(pitch_rad, 1.0f,0.0f,0.0f)* - osg::Matrix::translate(trans) - ); + mat.preMult(mat_rot); + + transform->setMatrix(mat); return transform; } @@ -853,7 +857,7 @@ void ConvertFromFLT::visitFace(GeoSetBuilder* pBuilder, FaceRecord* rec) if (bPackedColor) _faceColor = pSFace->PrimaryPackedColor.get(); else - _faceColor = pColorPool->getColor(pSFace->wPrimaryNameIndex); + _faceColor = pColorPool->getOldColor(pSFace->wPrimaryNameIndex); } } diff --git a/src/osgPlugins/flt/flt2osg.h b/src/osgPlugins/flt/flt2osg.h index d6a104a1a..c5eb9c8a7 100644 --- a/src/osgPlugins/flt/flt2osg.h +++ b/src/osgPlugins/flt/flt2osg.h @@ -82,7 +82,7 @@ struct SMaterial; #define ADD_OLD_COLOR(DGSET,VERTEX,COLOR_POOL) \ { \ if (COLOR_POOL) \ - (DGSET)->addColor((COLOR_POOL)->getColor((VERTEX)->color_index)); \ + (DGSET)->addColor((COLOR_POOL)->getOldColor((VERTEX)->color_index)); \ else \ (DGSET)->addColor(osg::Vec4(1,1,1,1)); \ }