From e6cf959134d34dcc0cb727dc849848c391d91c95 Mon Sep 17 00:00:00 2001 From: scrawl Date: Sat, 11 Apr 2015 14:46:52 +0200 Subject: [PATCH] Revert "Delete nifosgtest" This reverts commit ba3075dc110b5da47ab5e1b3aa069d54fecd444b. --- CMakeLists.txt | 2 + apps/nifosgtest/CMakeLists.txt | 7 ++ apps/nifosgtest/test.cpp | 162 +++++++++++++++++++++++++++++++++ 3 files changed, 171 insertions(+) create mode 100644 apps/nifosgtest/CMakeLists.txt create mode 100644 apps/nifosgtest/test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e510b29d3..77bea373fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -559,6 +559,8 @@ if (BUILD_NIFTEST) endif(BUILD_NIFTEST) # Apps and tools +add_subdirectory( apps/nifosgtest ) + if (BUILD_OPENMW) add_subdirectory( apps/openmw ) endif() diff --git a/apps/nifosgtest/CMakeLists.txt b/apps/nifosgtest/CMakeLists.txt new file mode 100644 index 0000000000..265577d989 --- /dev/null +++ b/apps/nifosgtest/CMakeLists.txt @@ -0,0 +1,7 @@ +set (FILES + test.cpp +) + +add_executable (test ${FILES}) + +target_link_libraries (test ${OPENSCENEGRAPH_LIBRARIES} "components") diff --git a/apps/nifosgtest/test.cpp b/apps/nifosgtest/test.cpp new file mode 100644 index 0000000000..332867be5d --- /dev/null +++ b/apps/nifosgtest/test.cpp @@ -0,0 +1,162 @@ +#include +#include + +#include +#include + +#include + +#include +#include +#include + +#include + +#include + +#include +#include + +#include + +#include + +// EventHandler to toggle wireframe when 'w' key is pressed +class WireframeKeyHandler : public osgGA::GUIEventHandler +{ +public: + WireframeKeyHandler(osg::Node* node) + : mWireframe(false) + , mNode(node) + { + + } + + virtual bool handle(const osgGA::GUIEventAdapter& adapter,osgGA::GUIActionAdapter& action) + { + switch (adapter.getEventType()) + { + case osgGA::GUIEventAdapter::KEYDOWN: + if (adapter.getKey() == osgGA::GUIEventAdapter::KEY_W) + { + mWireframe = !mWireframe; + osg::PolygonMode* mode = new osg::PolygonMode; + mode->setMode(osg::PolygonMode::FRONT_AND_BACK, + mWireframe ? osg::PolygonMode::LINE : osg::PolygonMode::FILL); + + // Create a new stateset instead of changing the old one, this alleviates the need to set + // the StateSet to DYNAMIC DataVariance, which would have a performance impact. + + osg::StateSet* stateset = new osg::StateSet; + stateset->setAttributeAndModes(mode, osg::StateAttribute::ON); + stateset->setMode(GL_CULL_FACE, mWireframe ? osg::StateAttribute::OFF + : osg::StateAttribute::ON); + + mNode->setStateSet(stateset); + + return true; + } + default: + break; + } + return false; + } + +private: + bool mWireframe; + osg::Node* mNode; +}; + +int main(int argc, char** argv) +{ + if (argc < 2) + { + std::cout << "Usage: " << argv[0] << " " << std::endl; + return 1; + } + + Files::ConfigurationManager cfgMgr; + boost::program_options::options_description desc(""); + desc.add_options() + ("data", boost::program_options::value()->default_value(Files::PathContainer(), "data")->multitoken()->composing()) + ("fs-strict", boost::program_options::value()->implicit_value(true)->default_value(false)) + ("fallback-archive", boost::program_options::value >()-> + default_value(std::vector(), "fallback-archive")->multitoken()); + + boost::program_options::variables_map variables; + cfgMgr.readConfiguration(variables, desc); + + std::vector archives = variables["fallback-archive"].as >(); + bool fsStrict = variables["fs-strict"].as(); + Files::PathContainer dataDirs; + if (!variables["data"].empty()) { + dataDirs = Files::PathContainer(variables["data"].as()); + } + + cfgMgr.processPaths(dataDirs); + + VFS::Manager resourceMgr (fsStrict); + Files::Collections collections (dataDirs, !fsStrict); + + for (std::vector::const_iterator it = archives.begin(); it != archives.end(); ++it) + { + std::string filepath = collections.getPath(*it).string(); + resourceMgr.addArchive(new VFS::BsaArchive(filepath)); + } + for (Files::PathContainer::const_iterator it = dataDirs.begin(); it != dataDirs.end(); ++it) + { + resourceMgr.addArchive(new VFS::FileSystemArchive(it->string())); + } + + resourceMgr.buildIndex(); + + Nif::NIFFilePtr nif(new Nif::NIFFile(resourceMgr.get(argv[1]), std::string(argv[1]))); + + // For NiStencilProperty + osg::DisplaySettings::instance()->setMinimumNumStencilBits(8); + + osgViewer::Viewer viewer; + + osg::ref_ptr root(new osg::Group()); + root->getOrCreateStateSet()->setMode(GL_CULL_FACE, osg::StateAttribute::ON); + // To prevent lighting issues with scaled meshes + root->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON); + + + //osgDB::writeNodeFile(*newNode, "out.osg"); + osg::Group* newNode = new osg::Group; + NifOsg::Loader loader; + Resource::TextureManager texMgr(&resourceMgr); + loader.mTextureManager = &texMgr; + newNode->addChild(loader.load(nif)); + + osg::PositionAttitudeTransform* trans = new osg::PositionAttitudeTransform; + root->addChild(trans); + + for (int x=0; x<1;++x) + { + //root->addChild(newNode); + trans->addChild(newNode); + } + + viewer.setSceneData(root); + + viewer.setUpViewInWindow(0, 0, 800, 600); + viewer.realize(); + viewer.setCameraManipulator(new osgGA::TrackballManipulator()); + viewer.addEventHandler(new WireframeKeyHandler(root)); + + // Mask to separate cull visitors from update visitors + viewer.getCamera()->setCullMask(~(0x1)); + + viewer.addEventHandler(new osgViewer::StatsHandler); + + while (!viewer.done()) + { + //trans->setAttitude(osg::Quat(viewer.getFrameStamp()->getSimulationTime()*5, osg::Vec3f(0,0,1))); + + viewer.frame(); + } + + return 0; +}