From ef3b957cb90c541944b78bdd645b9e11d203f143 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 7 Mar 2003 09:04:04 +0000 Subject: [PATCH] From Daniel Sjolie, support for light source. --- src/osgPlugins/flt/FltFile.cpp | 3 + src/osgPlugins/flt/FltFile.h | 3 + .../flt/LightSourcePaletteRecord.cpp | 24 ++++++ src/osgPlugins/flt/Pool.cpp | 16 ++++ src/osgPlugins/flt/Pool.h | 22 +++++ src/osgPlugins/flt/flt2osg.cpp | 80 +++++++++++++++++++ src/osgPlugins/flt/flt2osg.h | 4 + 7 files changed, 152 insertions(+) diff --git a/src/osgPlugins/flt/FltFile.cpp b/src/osgPlugins/flt/FltFile.cpp index 259226840..b984d6318 100644 --- a/src/osgPlugins/flt/FltFile.cpp +++ b/src/osgPlugins/flt/FltFile.cpp @@ -65,6 +65,9 @@ FltFile::FltFile( setMaterialPool( new MaterialPool ); } + // no support for external light palettes + setLightPool( new LightPool ); + // instances are always internally defined setInstancePool( new InstancePool ); } diff --git a/src/osgPlugins/flt/FltFile.h b/src/osgPlugins/flt/FltFile.h index 3e0727180..c86a009c7 100644 --- a/src/osgPlugins/flt/FltFile.h +++ b/src/osgPlugins/flt/FltFile.h @@ -32,11 +32,13 @@ class FltFile : public osg::Referenced ColorPool* getColorPool() { return _colorPool.get(); } TexturePool* getTexturePool() { return _texturePool.get(); } + LightPool* getLightPool() { return _lightPool.get(); } MaterialPool* getMaterialPool() { return _materialPool.get(); } InstancePool* getInstancePool() { return _instancePool.get(); } void setColorPool(ColorPool* colorPool) { _colorPool = colorPool; } void setTexturePool(TexturePool* texturePool) { _texturePool = texturePool; } + void setLightPool(LightPool* lightPool){ _lightPool = lightPool; } void setMaterialPool(MaterialPool* materialPool){ _materialPool = materialPool; } void setInstancePool(InstancePool* instancePool){ _instancePool = instancePool; } @@ -70,6 +72,7 @@ class FltFile : public osg::Referenced osg::ref_ptr _colorPool; osg::ref_ptr _texturePool; + osg::ref_ptr _lightPool; osg::ref_ptr _materialPool; osg::ref_ptr _instancePool; }; diff --git a/src/osgPlugins/flt/LightSourcePaletteRecord.cpp b/src/osgPlugins/flt/LightSourcePaletteRecord.cpp index d67479a5f..291c5a745 100644 --- a/src/osgPlugins/flt/LightSourcePaletteRecord.cpp +++ b/src/osgPlugins/flt/LightSourcePaletteRecord.cpp @@ -28,4 +28,28 @@ LightSourcePaletteRecord::~LightSourcePaletteRecord() // virtual void LightSourcePaletteRecord::endian() { + SLightSourcePalette *pSLightSourcePalette = (SLightSourcePalette*)getData(); + + ENDIAN( pSLightSourcePalette->diReserved_1 ); + ENDIAN( pSLightSourcePalette->diIndex ); + ENDIAN( pSLightSourcePalette->diReserved_2 ); + ENDIAN( pSLightSourcePalette->sfAmbientRGBA[0] ); + ENDIAN( pSLightSourcePalette->sfAmbientRGBA[1] ); + ENDIAN( pSLightSourcePalette->sfAmbientRGBA[2] ); + ENDIAN( pSLightSourcePalette->sfAmbientRGBA[3] ); + ENDIAN( pSLightSourcePalette->sfDiffuseRGBA[0] ); + ENDIAN( pSLightSourcePalette->sfDiffuseRGBA[1] ); + ENDIAN( pSLightSourcePalette->sfDiffuseRGBA[2] ); + ENDIAN( pSLightSourcePalette->sfDiffuseRGBA[3] ); + ENDIAN( pSLightSourcePalette->sfSpecularRGBA[0] ); + ENDIAN( pSLightSourcePalette->sfSpecularRGBA[1] ); + ENDIAN( pSLightSourcePalette->sfSpecularRGBA[2] ); + ENDIAN( pSLightSourcePalette->sfSpecularRGBA[3] ); + ENDIAN( pSLightSourcePalette->sfDropoff ); + ENDIAN( pSLightSourcePalette->sfCutoff ); + ENDIAN( pSLightSourcePalette->sfYaw ); + ENDIAN( pSLightSourcePalette->sfPitch ); + ENDIAN( pSLightSourcePalette->sfConstantAttuenation ); + ENDIAN( pSLightSourcePalette->sfLinearAttuenation ); + ENDIAN( pSLightSourcePalette->sfQuadraticAttuenation ); } diff --git a/src/osgPlugins/flt/Pool.cpp b/src/osgPlugins/flt/Pool.cpp index bf218ffa0..a317a0180 100644 --- a/src/osgPlugins/flt/Pool.cpp +++ b/src/osgPlugins/flt/Pool.cpp @@ -227,6 +227,22 @@ void TexturePool::addTextureName(int nIndex, const std::string& name) //////////////////////////////////////////////////////////////////// +osg::Light* LightPool::getLight(int nIndex) +{ + if (nIndex < 0) return NULL; + LightPaletteMap::iterator fitr = _lightMap.find(nIndex); + if (fitr != _lightMap.end()) + return (*fitr).second.get(); + + return NULL; +} + + +void LightPool::addLight(int nIndex, osg::Light* light) +{ + _lightMap[nIndex] = light; +} + MaterialPool::PoolMaterial* MaterialPool::getMaterial(int nIndex) { if (nIndex < 0) return NULL; diff --git a/src/osgPlugins/flt/Pool.h b/src/osgPlugins/flt/Pool.h index 5dc94f43f..0f317751b 100644 --- a/src/osgPlugins/flt/Pool.h +++ b/src/osgPlugins/flt/Pool.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -78,6 +79,27 @@ class TexturePool : public osg::Referenced +class LightPool : public osg::Referenced +{ + public : + + LightPool() {} + + osg::Light* getLight(int nIndex ); + void addLight(int nIndex, osg::Light* light); + + protected : + + virtual ~LightPool() {} + + private : + + typedef std::map > LightPaletteMap; + LightPaletteMap _lightMap; +}; + + + class MaterialPool : public osg::Referenced { public: diff --git a/src/osgPlugins/flt/flt2osg.cpp b/src/osgPlugins/flt/flt2osg.cpp index 24170a2b7..6337842a9 100644 --- a/src/osgPlugins/flt/flt2osg.cpp +++ b/src/osgPlugins/flt/flt2osg.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -63,6 +64,8 @@ #include "LocalVertexPoolRecord.h" #include "MultiTextureRecord.h" #include "UVListRecord.h" +#include "LightSourceRecord.h" +#include "LightSourcePaletteRecord.h" @@ -180,6 +183,10 @@ osg::Group* ConvertFromFLT::visitAncillary(osg::Group& osgParent, osg::Group& os visitColorPalette(osgPrimary, (ColorPaletteRecord*)child); break; + case LIGHT_SOURCE_PALETTE_OP: + visitLightSourcePalette(osgPrimary, (LightSourcePaletteRecord*)child); + break; + case MATERIAL_PALETTE_OP: visitMaterialPalette(osgPrimary, (MaterialPaletteRecord*)child); break; @@ -266,6 +273,9 @@ osg::Group* ConvertFromFLT::visitPrimaryNode(osg::Group& osgParent, PrimNodeReco case GROUP_OP: osgPrim = visitGroup(osgParent, (GroupRecord*)child); break; + case LIGHT_SOURCE_OP: + osgPrim = visitLightSource(osgParent, (LightSourceRecord*)child); + break; case LOD_OP: osgPrim = visitLOD(osgParent, (LodRecord*)child); break; @@ -469,6 +479,33 @@ void ConvertFromFLT::visitColorPalette(osg::Group& , ColorPaletteRecord* rec) } } + /*osgParent*/ +void ConvertFromFLT::visitLightSourcePalette(osg::Group& , LightSourcePaletteRecord* rec) +{ + + SLightSourcePalette* pLight = (SLightSourcePalette*)rec->getData(); + + osg::Light* light = new osg::Light(); + + light->setAmbient( osg::Vec4( + pLight->sfAmbientRGBA[0], pLight->sfAmbientRGBA[1], + pLight->sfAmbientRGBA[2], pLight->sfAmbientRGBA[3] ) ); + light->setDiffuse( osg::Vec4( + pLight->sfDiffuseRGBA[0], pLight->sfDiffuseRGBA[1], + pLight->sfDiffuseRGBA[2], pLight->sfDiffuseRGBA[3] ) ); + light->setSpecular( osg::Vec4( + pLight->sfSpecularRGBA[0], pLight->sfSpecularRGBA[1], + pLight->sfSpecularRGBA[2], pLight->sfSpecularRGBA[3] ) ); + light->setConstantAttenuation( pLight->sfConstantAttuenation ); + light->setLinearAttenuation( pLight->sfLinearAttuenation ); + light->setQuadraticAttenuation( pLight->sfQuadraticAttuenation ); + //light->setSpotExponent( pLight->sfDropoff ); + //light->setSpotCutoff( pLight->sfCutoff ); + + LightPool* pLightPool = rec->getFltFile()->getLightPool(); + pLightPool->addLight( pLight->diIndex, light ); +} + /*osgParent*/ void ConvertFromFLT::visitMaterialPalette(osg::Group&, MaterialPaletteRecord* rec) { @@ -623,6 +660,49 @@ osg::Group* ConvertFromFLT::visitGroup(osg::Group& osgParent, GroupRecord* rec) } } +osg::Group* ConvertFromFLT::visitLightSource(osg::Group& osgParent, LightSourceRecord* rec) +{ + + static int lightnum = 0; + + LightPool* pLightPool = rec->getFltFile()->getLightPool(); + SLightSource* pLSource = (SLightSource*) rec->getData(); + + /* + if ( !(pLSource->dwFlags & 1) ) { // enabled + return NULL; + } + */ + + osg::LightSource* lightSource = new osg::LightSource(); + + osg::Light* pLight = pLightPool->getLight( pLSource->diIndex ); + osg::Light* light = new osg::Light( *pLight ); + + light->setPosition( osg::Vec4( + pLSource->Coord.x(), pLSource->Coord.y(), + pLSource->Coord.z(), 1 ) ); + light->setLightNum( lightnum ); + + lightSource->setLight( light ); + lightSource->setLocalStateSetModes( osg::StateAttribute::ON ); + osg::Node* node = &osgParent; + if ( 1 ) { //pLSource->dwFlags & 2 ) { // global + while ( !node->getParents().empty() ) { + node = *(node->getParents().begin()); + } + } + lightSource->setStateSetModes( *(node->getOrCreateStateSet()), + osg::StateAttribute::ON ); + + lightnum++; + + osgParent.addChild( lightSource ); + visitPrimaryNode(*lightSource, rec); + + return lightSource; +} + osg::Group* ConvertFromFLT::visitRoadConstruction(osg::Group& osgParent, GroupRecord* rec) { osg::Group* group = new osg::Group; diff --git a/src/osgPlugins/flt/flt2osg.h b/src/osgPlugins/flt/flt2osg.h index 0f9b6295b..309f924c8 100644 --- a/src/osgPlugins/flt/flt2osg.h +++ b/src/osgPlugins/flt/flt2osg.h @@ -55,6 +55,8 @@ class InstanceDefinitionRecord; class InstanceReferenceRecord; class MultiTextureRecord; class UVListRecord; +class LightSourceRecord; +class LightSourcePaletteRecord; struct SFace; //class GeoSetBuilder; @@ -124,6 +126,7 @@ class ConvertFromFLT // Palette records void visitColorPalette(osg::Group& osgParent, ColorPaletteRecord* rec); + void visitLightSourcePalette(osg::Group& osgParent, LightSourcePaletteRecord* rec); void visitMaterialPalette(osg::Group& osgParent, MaterialPaletteRecord* rec); void visitOldMaterialPalette(osg::Group& osgParent, OldMaterialPaletteRecord* rec); void visitTexturePalette(osg::Group& osgParent, TexturePaletteRecord* rec); @@ -136,6 +139,7 @@ class ConvertFromFLT // Primary records osg::Group* visitHeader(HeaderRecord* rec); osg::Group* visitGroup(osg::Group& osgParent, GroupRecord* rec); + osg::Group* visitLightSource(osg::Group& osgParent, LightSourceRecord* rec); osg::Group* visitRoadConstruction(osg::Group& osgParent, GroupRecord* rec); osg::Group* visitLOD(osg::Group& osgParent, LodRecord* rec); osg::Group* visitOldLOD(osg::Group& osgParent, OldLodRecord* rec);