diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 3abccdcee..fb619b42c 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -112,6 +112,7 @@ IF(DYNAMIC_OPENSCENEGRAPH) ADD_SUBDIRECTORY(osgthirdpersonview) ADD_SUBDIRECTORY(osgunittests) ADD_SUBDIRECTORY(osgvertexprogram) + ADD_SUBDIRECTORY(osgvertexattributes) ADD_SUBDIRECTORY(osgvolume) ADD_SUBDIRECTORY(osgwindows) ADD_SUBDIRECTORY(osganimationtimeline) diff --git a/examples/osgvertexattributes/CMakeLists.txt b/examples/osgvertexattributes/CMakeLists.txt new file mode 100644 index 000000000..098763589 --- /dev/null +++ b/examples/osgvertexattributes/CMakeLists.txt @@ -0,0 +1,4 @@ +SET(TARGET_SRC osgvertexattributes.cpp ) + +#### end var setup ### +SETUP_EXAMPLE(osgvertexattributes) diff --git a/examples/osgvertexattributes/osgvertexattributes.cpp b/examples/osgvertexattributes/osgvertexattributes.cpp new file mode 100644 index 000000000..b5af57a76 --- /dev/null +++ b/examples/osgvertexattributes/osgvertexattributes.cpp @@ -0,0 +1,185 @@ +/* OpenSceneGraph example, osgvertexattributes. +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include +#include +#include +#include + +class ConvertToVertexAttibArrays : public osg::NodeVisitor +{ + public: + + typedef std::pair AttributeAlias; + + ConvertToVertexAttibArrays(): + osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) + { + // mappings taken from http://www.opengl.org/registry/specs/NV/vertex_program.txt + _vertexAlias = AttributeAlias(0, "osg_Vertex"); + _normalAlias = AttributeAlias(2, "osg_Normal"); + _colorAlias = AttributeAlias(3, "osg_Color"); + _secondaryColorAlias = AttributeAlias(4, "osg_SecondaryColor"); + _fogCoordAlias = AttributeAlias(5, "osg_FogCoord"); + _texCoordAlias[0] = AttributeAlias(8, "osg_MultiTexCoord0"); + _texCoordAlias[1] = AttributeAlias(9, "osg_MultiTexCoord1"); + _texCoordAlias[2] = AttributeAlias(10, "osg_MultiTexCoord2"); + _texCoordAlias[3] = AttributeAlias(11, "osg_MultiTexCoord3"); + _texCoordAlias[4] = AttributeAlias(12, "osg_MultiTexCoord4"); + _texCoordAlias[5] = AttributeAlias(13, "osg_MultiTexCoord5"); + _texCoordAlias[6] = AttributeAlias(14, "osg_MultiTexCoord6"); + _texCoordAlias[7] = AttributeAlias(15, "osg_MultiTexCoord7"); + } + + + void apply(osg::Geode& geode) + { + for(unsigned int i=0; iasGeometry(); + if (geom) apply(*geom); + } + } + + void apply(osg::Geometry& geom) + { + osg::notify(osg::NOTICE)<<"Found geometry "<<&geom<8) + { + osg::notify(osg::NOTICE)<<"Warning: Ignoring "<setName(name); + geom.setVertexAttribArray(index, array); + geom.setVertexAttribNormalize(index, normalize); + geom.setVertexAttribBinding(index, binding); + + osg::notify(osg::NOTICE)<<" vertex attrib("< loadedModel = osgDB::readNodeFiles(arguments); + if (!loadedModel.get()) + { + osg::notify(osg::NOTICE)<<"No model loaded, please specify a model filename."<accept(sgv); + } + + if (runConvertToVertexAttributes) + { + // find any conventional vertex, colour, normal and tex coords arrays and convert to vertex attributes + ConvertToVertexAttibArrays ctvaa; + loadedModel->accept(ctvaa); + } + + if (!outputFileName.empty()) + { + osgDB::writeNodeFile(*loadedModel, outputFileName); + return 0; + } + + // add a viewport to the viewer and attach the scene graph. + viewer.setSceneData(loadedModel.get()); + + return viewer.run(); +}