From 6c22afa51cd4e0a21b85cf010988c2401448ed45 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 10 Sep 2003 12:38:28 +0000 Subject: [PATCH] Fixes for osgFX. --- Make/makedirdefs | 1 + examples/osgpagedlod/GNUmakefile | 18 +++ examples/osgpagedlod/GNUmakefile.inst | 13 +++ examples/osgpagedlod/osgpagedlod.cpp | 156 ++++++++++++++++++++++++++ include/osg/Vec2 | 8 +- include/osg/Vec3 | 7 +- include/osg/Vec4 | 12 +- src/osgFX/BumpMapping.cpp | 6 +- src/osgPlugins/osg/Geometry.cpp | 4 +- 9 files changed, 211 insertions(+), 14 deletions(-) create mode 100644 examples/osgpagedlod/GNUmakefile create mode 100644 examples/osgpagedlod/GNUmakefile.inst create mode 100644 examples/osgpagedlod/osgpagedlod.cpp diff --git a/Make/makedirdefs b/Make/makedirdefs index dad57e940..9c0dcf0a6 100644 --- a/Make/makedirdefs +++ b/Make/makedirdefs @@ -142,6 +142,7 @@ EXAMPLE_DIRS = \ osgmultitexture\ osgoccluder\ osgparticle\ + osgpagedlod\ osgpick\ osgpoints\ osgprerender\ diff --git a/examples/osgpagedlod/GNUmakefile b/examples/osgpagedlod/GNUmakefile new file mode 100644 index 000000000..5e2a9e526 --- /dev/null +++ b/examples/osgpagedlod/GNUmakefile @@ -0,0 +1,18 @@ +TOPDIR = ../.. +include $(TOPDIR)/Make/makedefs + +CXXFILES =\ + osgpagedlod.cpp\ + +LIBS += -losgProducer -lProducer -losgText -losgGA -losgDB -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS) + +INSTFILES = \ + $(CXXFILES)\ + GNUmakefile.inst=GNUmakefile + +EXEC = osgpagedlod + +INC += $(X_INC) + +include $(TOPDIR)/Make/makerules + diff --git a/examples/osgpagedlod/GNUmakefile.inst b/examples/osgpagedlod/GNUmakefile.inst new file mode 100644 index 000000000..0d916cb07 --- /dev/null +++ b/examples/osgpagedlod/GNUmakefile.inst @@ -0,0 +1,13 @@ +TOPDIR = ../.. +include $(TOPDIR)/Make/makedefs + +CXXFILES =\ + osgpagedlod.cpp\ + +LIBS += -losgProducer -lProducer -losgDB -losgText -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS) + +EXEC = osgpagedlod + +INC += $(X_INC) + +include $(TOPDIR)/Make/makerules diff --git a/examples/osgpagedlod/osgpagedlod.cpp b/examples/osgpagedlod/osgpagedlod.cpp new file mode 100644 index 000000000..229cd921f --- /dev/null +++ b/examples/osgpagedlod/osgpagedlod.cpp @@ -0,0 +1,156 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include + +osg::Geode* createTile(const osg::Vec3& lb, const osg::Vec3& rb, + const osg::Vec3& lt, const osg::Vec3& rt, + const std::string& imageFile) +{ + + // create the geometry. + osg::Geometry* geom = new osg::Geometry; + + osg::Vec3Array* coords = new osg::Vec3Array(4); + (*coords)[0] = lt; + (*coords)[1] = lb; + (*coords)[2] = rb; + (*coords)[3] = rt; + geom->setVertexArray(coords); + + osg::Vec2Array* tcoords = new osg::Vec2Array(4); + (*tcoords)[0].set(0.0f,1.0f); + (*tcoords)[1].set(0.0f,0.0f); + (*tcoords)[2].set(1.0f,0.0f); + (*tcoords)[3].set(1.0f,1.0f); + geom->setTexCoordArray(0,tcoords); + + osg::Vec4Array* colours = new osg::Vec4Array(1); + (*colours)[0].set(1.0f,1.0f,1.0,1.0f); + geom->setColorArray(colours); + geom->setColorBinding(osg::Geometry::BIND_OVERALL); + + osg::Vec3Array* normals = new osg::Vec3Array(1); + (*normals)[0] = (rb-lb)^(lt-lb); + (*normals)[0].normalize(); + geom->setNormalArray(normals); + geom->setNormalBinding(osg::Geometry::BIND_OVERALL); + + geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4)); + + + // add the texture to the geometry. + geom->getOrCreateStateSet()->setTextureAttributeAndModes( + 0, // texture unit 0. + new osg::Texture2D(osgDB::readImageFile(imageFile)), + osg::StateAttribute::ON); + + + // create the geode. + osg::Geode* geode = new osg::Geode; + geode->addDrawable(geom); + + return geode; + +} + + +osg::Node* createPagedModel() +{ + osg::PagedLOD* level_0 = new osg::PagedLOD; + + float distance = 1000.0f; + + osg::Vec3 lb(0.0f,0.0f,0.0f); + osg::Vec3 rb(distance,0.0f,0.0f); + osg::Vec3 rt(distance,0.0f,distance); + osg::Vec3 lt(0.0f,0.0f,distance); + + level_0->addChild(createTile(lb,rb,lt,rt,"lz.rgb"),distance,1e5,""); + level_0->addChild(createTile(lb,rb,lt,rt,"land_shallow_topo_2048.jpg"),0,distance,"level_1.osg"); + + return level_0; +} + +class WriteOutPagedLODSubgraphsVistor : public osg::NodeVisitor +{ +public: + WriteOutPagedLODSubgraphsVistor(): + osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) + { + } + + virtual void apply(osg::PagedLOD& plod) + { + // go through all the named children and write them out to disk. + for(unsigned int i=0;isetApplicationName(arguments.getApplicationName()); + arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" creates a hierachy of files for paging which can be later loaded by viewers."); + arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); + arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); + + // if user request help write it out to cout. + if (arguments.read("-h") || arguments.read("--help")) + { + arguments.getApplicationUsage()->write(std::cout); + return 1; + } + + // any option left unread are converted into errors to write out later. + arguments.reportRemainingOptionsAsUnrecognized(); + + // report any errors if they have occured when parsing the program aguments. + if (arguments.errors()) + { + arguments.writeErrorMessages(std::cout); + return 1; + } + +// if (arguments.argc()<=1) +// { +// arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); +// return 1; +// } + + + osg::ref_ptr model = createPagedModel(); + + if (model.valid()) + { + osgDB::writeNodeFile(*model,"level_0.osg"); + + WriteOutPagedLODSubgraphsVistor woplsv; + model->accept(woplsv); + } + + return 0; +} diff --git a/include/osg/Vec2 b/include/osg/Vec2 index 57e685a0c..76621d570 100644 --- a/include/osg/Vec2 +++ b/include/osg/Vec2 @@ -150,8 +150,12 @@ class Vec2 inline float normalize() { float norm = Vec2::length(); - _v[0] /= norm; - _v[1] /= norm; + if (norm>0.0f) + { + float inv = 1.0f/norm; + _v[0] *= inv; + _v[1] *= inv; + } return( norm ); } diff --git a/include/osg/Vec3 b/include/osg/Vec3 index 485d30d9f..f36d85829 100644 --- a/include/osg/Vec3 +++ b/include/osg/Vec3 @@ -170,9 +170,10 @@ class Vec3 float norm = Vec3::length(); if (norm>0.0f) { - _v[0] /= norm; - _v[1] /= norm; - _v[2] /= norm; + float inv = 1.0f/norm; + _v[0] *= inv; + _v[1] *= inv; + _v[2] *= inv; } return( norm ); } diff --git a/include/osg/Vec4 b/include/osg/Vec4 index 87cdf9c02..ea37ebb3a 100644 --- a/include/osg/Vec4 +++ b/include/osg/Vec4 @@ -207,10 +207,14 @@ class Vec4 inline float normalize() { float norm = Vec4::length(); - _v[0] /= norm; - _v[1] /= norm; - _v[2] /= norm; - _v[3] /= norm; + if (norm>0.0f) + { + float inv = 1.0f/norm; + _v[0] *= inv; + _v[1] *= inv; + _v[2] *= inv; + _v[3] *= inv; + } return( norm ); } diff --git a/src/osgFX/BumpMapping.cpp b/src/osgFX/BumpMapping.cpp index 369e99a2f..59ddb05ee 100644 --- a/src/osgFX/BumpMapping.cpp +++ b/src/osgFX/BumpMapping.cpp @@ -586,11 +586,11 @@ void BumpMapping::prepareGeometry(osg::Geometry *geo) osg::ref_ptr tsg = new osgUtil::TangentSpaceGenerator; tsg->generate(geo, normalunit_); if (!geo->getVertexAttribArray(6)) - geo->setVertexAttribData(6, osg::Geometry::ArrayData(tsg->getTangentArray(), osg::Geometry::BIND_PER_VERTEX,GL_TRUE)); + geo->setVertexAttribData(6, osg::Geometry::ArrayData(tsg->getTangentArray(), osg::Geometry::BIND_PER_VERTEX,GL_FALSE)); if (!geo->getVertexAttribArray(7)) - geo->setVertexAttribData(7, osg::Geometry::ArrayData(tsg->getBinormalArray(), osg::Geometry::BIND_PER_VERTEX, GL_TRUE)); + geo->setVertexAttribData(7, osg::Geometry::ArrayData(tsg->getBinormalArray(), osg::Geometry::BIND_PER_VERTEX, GL_FALSE)); if (!geo->getVertexAttribArray(15)) - geo->setVertexAttribData(15, osg::Geometry::ArrayData(tsg->getNormalArray(), osg::Geometry::BIND_PER_VERTEX, GL_TRUE)); + geo->setVertexAttribData(15, osg::Geometry::ArrayData(tsg->getNormalArray(), osg::Geometry::BIND_PER_VERTEX, GL_FALSE)); } void BumpMapping::prepareNode(osg::Node *node) diff --git a/src/osgPlugins/osg/Geometry.cpp b/src/osgPlugins/osg/Geometry.cpp index a13765447..06b98235b 100644 --- a/src/osgPlugins/osg/Geometry.cpp +++ b/src/osgPlugins/osg/Geometry.cpp @@ -314,7 +314,7 @@ bool Geometry_readLocalData(Object& obj, Input& fr) } Geometry::AttributeBinding vertexAttribBinding=Geometry::BIND_OFF; - if (fr.matchSequence("VertexAttribBinding %i %s") && Geometry_matchBindingTypeStr(fr[2].getStr(),vertexAttribBinding)) + if (fr.matchSequence("VertexAttribBinding %i %w") && Geometry_matchBindingTypeStr(fr[2].getStr(),vertexAttribBinding)) { int unit=0; fr[1].getInt(unit); @@ -323,7 +323,7 @@ bool Geometry_readLocalData(Object& obj, Input& fr) iteratorAdvanced = true; } - if (fr.matchSequence("VertexAttribNormalize %i %s")) + if (fr.matchSequence("VertexAttribNormalize %i %w")) { int unit=0; fr[1].getInt(unit);