From cf1fe8edc2c70e55989e4723de34598be8e78485 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Sun, 7 Sep 2003 14:18:22 +0000 Subject: [PATCH] From Boris Bralo, addition of support for osgSim::LightPoint's into TXP plugin. --- VisualStudio/VisualStudio.dsw | 3 + src/osgPlugins/txp/GNUmakefile | 2 +- src/osgPlugins/txp/TerrapageNode.cpp | 3 + src/osgPlugins/txp/TrPageArchive.cpp | 114 +++++++++++++++++++++++ src/osgPlugins/txp/TrPageArchive.h | 24 ++++- src/osgPlugins/txp/TrPageParser.cpp | 82 ++++++++++++++++ src/osgPlugins/txp/TrPageParser.h | 19 +++- src/osgPlugins/txp/trPagePageManager.cpp | 2 +- 8 files changed, 243 insertions(+), 6 deletions(-) diff --git a/VisualStudio/VisualStudio.dsw b/VisualStudio/VisualStudio.dsw index 9516ed039..afa5f8acc 100644 --- a/VisualStudio/VisualStudio.dsw +++ b/VisualStudio/VisualStudio.dsw @@ -2102,6 +2102,9 @@ Package=<4> Begin Project Dependency Project_Dep_Name Core osgUtil End Project Dependency + Begin Project Dependency + Project_Dep_Name Core osgSim + End Project Dependency }}} ############################################################################### diff --git a/src/osgPlugins/txp/GNUmakefile b/src/osgPlugins/txp/GNUmakefile index 2b59c6ace..d78aed448 100644 --- a/src/osgPlugins/txp/GNUmakefile +++ b/src/osgPlugins/txp/GNUmakefile @@ -37,7 +37,7 @@ CXXFILES =\ INC += -I$(THISDIR) -LIBS += $(OSG_LIBS) $(OTHER_LIBS) +LIBS += -losgSim $(OSG_LIBS) $(OTHER_LIBS) PLUGIN_TARGET_BASENAME = txp include $(TOPDIR)/Make/cygwin_plugin_def diff --git a/src/osgPlugins/txp/TerrapageNode.cpp b/src/osgPlugins/txp/TerrapageNode.cpp index 2ccc7bed3..76f29b0f9 100644 --- a/src/osgPlugins/txp/TerrapageNode.cpp +++ b/src/osgPlugins/txp/TerrapageNode.cpp @@ -63,6 +63,9 @@ bool TerrapageNode::loadDatabase() // Note: Should be checking the return values txpArchive->LoadModels(); + // Note: Should be checking the return values + txpArchive->LoadLightAttributes(); + // get the exents of the archive const trpgHeader *head = txpArchive->GetHeader(); trpg2dPoint sw,ne; diff --git a/src/osgPlugins/txp/TrPageArchive.cpp b/src/osgPlugins/txp/TrPageArchive.cpp index d80f7d85c..8aa989393 100644 --- a/src/osgPlugins/txp/TrPageArchive.cpp +++ b/src/osgPlugins/txp/TrPageArchive.cpp @@ -23,6 +23,13 @@ #include #include +#include +#include +#include +#include +#include +#include + #include "trpage_geom.h" #include "trpage_read.h" #include "trpage_write.h" @@ -369,6 +376,113 @@ void TrPageArchive::LoadMaterials() } } +void TrPageArchive::LoadLightAttributes() +{ + int num; + lightTable.GetNumLightAttrs(num); + for ( int attr_num = 0; attr_num < num; attr_num++ ){ + + trpgLightAttr* ref = const_cast(lightTable.GetLightAttrRef(attr_num)); + + osgSim::LightPointNode* osgLight = new osgSim::LightPointNode(); + + osg::Point* osgPoint = new osg::Point(); + + osgSim::LightPoint lp ; + lp._on = true; + + trpgColor col; + ref->GetFrontColor(col); + lp._color = osg::Vec4(col.red, col.green,col.blue, 1.0); + + float64 inten; + ref->GetFrontIntensity(inten); + lp._intensity = inten; + + trpgLightAttr::PerformerAttr perfAttr; + ref->GetPerformerAttr(perfAttr); + + // point part + osgPoint->setSize(perfAttr.actualSize); + osgPoint->setMaxSize(perfAttr.maxPixelSize); + osgPoint->setMinSize(perfAttr.minPixelSize); + osgPoint->setFadeThresholdSize(perfAttr.transparentFallofExp); + //numbers that are going to appear are "experimental" + osgPoint->setDistanceAttenuation(osg::Vec3(0.0001, 0.0005, 0.00000025)); +// osgPoint->setDistanceAttenuation(osg::Vec3(1.0, 0.0, 1.0)); + + osg::StateSet* stateSet = new osg::StateSet(); + stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF); + stateSet->setMode(GL_POINT_SMOOTH, osg::StateAttribute::ON); + stateSet->setAttributeAndModes(osgPoint, osg::StateAttribute::ON ); + stateSet->setAttributeAndModes(new osg::BlendFunc, osg::StateAttribute::ON); + + osgLight->setMaxPixelSize(perfAttr.maxPixelSize); + osgLight->setMinPixelSize(perfAttr.minPixelSize); + +// float64 clamp; +// ref->GetPerformerTpClamp(clamp); +// osgLight->setMaxVisibleDistance2(clamp); + + trpg3dPoint normal; + ref->GetNormal(normal); + +// lp._radius = clamp; + + trpgLightAttr::LightDirectionality direc; + ref->GetDirectionality(direc); + if( direc == trpgLightAttr::trpg_Unidirectional){ + osgSim::AzimElevationSector* sec = new osgSim::AzimElevationSector(); + float64 tmp; + ref->GetHLobeAngle(tmp); + float64 tmpfade; + ref->GetLobeFalloff(tmpfade); + sec->setAzimuthRange(-tmp/2.0,tmp/2.0,tmpfade); + + ref->GetVLobeAngle(tmp); + sec->setElevationRange(0,tmp, tmpfade); + + lp._sector = sec; + osgLight->addLightPoint(lp); + } + else if( direc == trpgLightAttr::trpg_Bidirectional ){ + osgSim::AzimElevationSector* front = new osgSim::AzimElevationSector(); + float64 tmp; + ref->GetHLobeAngle(tmp); + float64 tmpfade; + ref->GetLobeFalloff(tmpfade); + front->setAzimuthRange(-tmp/2.0,tmp/2.0,tmpfade); + + ref->GetVLobeAngle(tmp); + front->setElevationRange(0,tmp, tmpfade); + + lp._sector = front; + osgLight->addLightPoint(lp); + + osgSim::AzimElevationSector* back = new osgSim::AzimElevationSector(); + back->setAzimuthRange(osg::PI-tmp/2.0,osg::PI+tmp/2.0,tmpfade); + back->setElevationRange(0,tmp, tmpfade); + lp._sector = back; + osgLight->addLightPoint(lp); + } + else{ + osgLight->addLightPoint(lp); + } + + AddLightAttribute(osgLight, stateSet, osg::Vec3(normal.x,normal.y,normal.z)); + } +} + +void TrPageArchive::AddLightAttribute(osgSim::LightPointNode* lpn, osg::StateSet* fallback, const osg::Vec3& att) +{ + DefferedLightAttribute la; + la.lightPoint = lpn; + la.fallback = fallback; + la.attitude = att; + lightAttrTable.push_back(la); +} + + bool TrPageArchive::LoadModels() { int numModel; diff --git a/src/osgPlugins/txp/TrPageArchive.h b/src/osgPlugins/txp/TrPageArchive.h index 908eedf2e..55f4c4f49 100644 --- a/src/osgPlugins/txp/TrPageArchive.h +++ b/src/osgPlugins/txp/TrPageArchive.h @@ -35,8 +35,19 @@ #include #include // for auto_ptr +namespace osgSim{ + class LightPointNode; +} + namespace txp { + // this one handles different placement of light direction in osg and terrapage + struct DefferedLightAttribute{ + // light point at (0,0,0) looking in (0,0,0) direction + osg::ref_ptr lightPoint; + osg::ref_ptr fallback; + osg::Vec3 attitude; + }; /// main class for loading terrapage archives class TrPageArchive : public trpgr_Archive { @@ -53,7 +64,15 @@ namespace txp /// Load and create models, usualy OpenFlight models bool LoadModels(); - + + void LoadLightAttributes(); + + void AddLightAttribute(osgSim::LightPointNode* lpn, osg::StateSet* fallback , const osg::Vec3& attitude); + + DefferedLightAttribute& GetLightAttribute(std::size_t i) { + return lightAttrTable[i]; + }; + /** Load a TXP tile and @param x Tile location input - x dimension. @param y Tile location input - y dimension. @@ -100,6 +119,9 @@ namespace txp std::vector< osg::ref_ptr > m_textures; std::vector< osg::ref_ptr > m_gstates; std::vector< osg::ref_ptr > m_models; + // light attributes vector + std::vector lightAttrTable; + std::string m_alternate_path; trpgMemReadBuffer buf; }; diff --git a/src/osgPlugins/txp/TrPageParser.cpp b/src/osgPlugins/txp/TrPageParser.cpp index ed8f7ce95..4fe90533a 100644 --- a/src/osgPlugins/txp/TrPageParser.cpp +++ b/src/osgPlugins/txp/TrPageParser.cpp @@ -35,6 +35,10 @@ #include #include #include +#include + +#include +#include #include "TrPageParser.h" #include "TrPageArchive.h" @@ -867,6 +871,76 @@ void* tileHeaderRead::Parse(trpgToken /*tok*/,trpgReadBuffer &buf) } +//---------------------------------------------------------------------------- +// +// light Reader Class +// +//---------------------------------------------------------------------------- +lightRead::lightRead (TrPageParser *in_parse) +{ + parse = in_parse; +} + +//---------------------------------------------------------------------------- +void* lightRead::Parse(trpgToken /*tok*/,trpgReadBuffer &buf) +{ + trpgLight light; + if (!light.Read(buf)) + return NULL; + int attr_index; + light.GetAttrIndex(attr_index); + + DefferedLightAttribute& dla = parse->GetLightAttribute(attr_index); + osgSim::LightPointNode* node = dla.lightPoint.get(); + + uint32 nvert; + light.GetNumVertices(nvert); + + if( node->getLightPoint(0)._sector.valid() ) // osgSim::LigthPoint is a must + { + for(unsigned int i = 0; i < nvert; i++){ + trpg3dPoint pt; + light.GetVertex(i, pt); + osg::Matrix matrix; + // matrix.makeTranslate(pt.x,pt.y,pt.z); + matrix.makeRotate(osg::Quat(0.0,dla.attitude)); + matrix.setTrans(pt.x,pt.y,pt.z); + osg::MatrixTransform* trans = new osg::MatrixTransform(); + trans->setMatrix(matrix); + trans->addChild(node); + parse->AddIntoSceneGraph(trans); + } + } + else { //Fall back to osg::Points + Vec3Array* vertices = new Vec3Array(nvert); + Vec4Array* colors = new Vec4Array(nvert); + + for(unsigned int i = 0; i < nvert; i++){ + trpg3dPoint pt; + light.GetVertex(i, pt); + (*vertices)[i] = osg::Vec3(pt.x,pt.y,pt.z); + (*colors)[i] = node->getLightPoint(0)._color; + } + + osg::Geometry* geom = new osg::Geometry(); + geom->addPrimitiveSet(new DrawArrays(PrimitiveSet::POINTS,0,nvert)); + geom->setVertexArray(vertices); + geom->setColorArray(colors); + geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX); + + geom->setUseDisplayList(false); + geom->setStateSet(dla.fallback.get()); + + GeodeGroup *top = parse->GetCurrTop(); + Geode *geode = top->GetGeode(); + geode->addDrawable(geom); + + } + + return (void *) 1; +} + + /* ********************************** */ //---------------------------------------------------------------------------- @@ -889,6 +963,9 @@ TrPageParser::TrPageParser(TrPageArchive* parent) AddCallback(TRPG_MODELREF,new modelRefRead(this)); AddCallback(TRPG_LAYER,new layerRead(this)); AddCallback(TRPGTILEHEADER,new tileHeaderRead(this)); + AddCallback(TRPG_LIGHT,new lightRead(this)); +// AddCallback(TRPGLIGHTATTR,new lightAttrRead(this)); +// AddCallback(TRPGLIGHTTABLE,new lightTableRead(this)); } //---------------------------------------------------------------------------- @@ -1250,3 +1327,8 @@ Layer *TrPageParser::GetCurrLayer() { return currLayer; } + +DefferedLightAttribute& TrPageParser::GetLightAttribute(int idx) +{ + return parent_->GetLightAttribute(idx); +} diff --git a/src/osgPlugins/txp/TrPageParser.h b/src/osgPlugins/txp/TrPageParser.h index 4afa0e0b4..0d062cca4 100644 --- a/src/osgPlugins/txp/TrPageParser.h +++ b/src/osgPlugins/txp/TrPageParser.h @@ -34,9 +34,11 @@ #include #include "trpage_read.h" + namespace txp { class TrPageArchive; + struct DefferedLightAttribute; // Group ID Info // Used to keep track of which groups are which IDs for parents @@ -155,8 +157,9 @@ namespace txp int getBillboardType() { return billboard_type; } void setBillboardCenter(trpg3dPoint center) { billboard_center = center; } osg::Vec3 getBillboardCenter() { return osg::Vec3(billboard_center.x, billboard_center.y, billboard_center.z); } - - + + DefferedLightAttribute& GetLightAttribute(int attr_index); + protected: // Called on start children bool StartChildren(void *); @@ -231,7 +234,7 @@ namespace txp // Geometry Geometry // It is tested on this kind of archive and it works. // nick@terrex.com - std::vector deadNodes; + std::vector deadNodes; }; // Gets local texture via the image helper @@ -311,6 +314,16 @@ namespace txp protected: TrPageParser*parse; }; + + //---------------------------------------------------------------------------- + class lightRead: public trpgr_Callback { + public: + lightRead(TrPageParser*in_parse); + void *Parse(trpgToken tok,trpgReadBuffer &buf); + protected: + TrPageParser*parse; + }; + } // namespace txp #endif diff --git a/src/osgPlugins/txp/trPagePageManager.cpp b/src/osgPlugins/txp/trPagePageManager.cpp index 3ddddd82c..a12523ef6 100644 --- a/src/osgPlugins/txp/trPagePageManager.cpp +++ b/src/osgPlugins/txp/trPagePageManager.cpp @@ -157,7 +157,7 @@ bool OSGPageManager::EndThread() // then wait for the the thread to stop running. while(pagingThread.isRunning()) { - std::cout<<"Waiting for TXP pager thread to cancel"<