1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-16 19:19:56 +00:00
openmw-tes3mp/apps/nifosgtest/test.cpp

127 lines
3.4 KiB
C++
Raw Normal View History

#include <osgViewer/Viewer>
2015-02-26 20:20:07 +00:00
#include <osgViewer/ViewerEventHandlers>
#include <components/bsa/bsa_file.hpp>
#include <components/nif/niffile.hpp>
#include <components/nifosg/nifloader.hpp>
#include <components/vfs/manager.hpp>
#include <components/vfs/bsaarchive.hpp>
#include <osgGA/TrackballManipulator>
#include <osgDB/WriteFile>
#include <osg/PolygonMode>
// EventHandler to toggle wireframe when 'w' key is pressed
2015-02-26 20:20:07 +00:00
class WireframeKeyHandler : public osgGA::GUIEventHandler
{
public:
2015-03-17 17:53:56 +00:00
WireframeKeyHandler()
: mWireframe(false)
{
}
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;
2015-03-17 17:53:56 +00:00
// applying state from an event handler doesn't appear to be safe, so do it in the frame update
return true;
}
default:
break;
}
return false;
}
2015-03-17 17:53:56 +00:00
bool getWireframe() const
{
return mWireframe;
}
private:
bool mWireframe;
};
int main(int argc, char** argv)
{
if (argc < 3)
{
std::cout << "Usage: " << argv[0] << " <BSA file> <NIF file>" << std::endl;
return 1;
}
VFS::Manager resourceMgr (false);
resourceMgr.addArchive(new VFS::BsaArchive(argv[1]));
resourceMgr.buildIndex();
Nif::NIFFilePtr nif(new Nif::NIFFile(resourceMgr.get(argv[2]), std::string(argv[2])));
osgViewer::Viewer viewer;
osg::ref_ptr<osg::Group> 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);
2015-02-26 20:20:07 +00:00
//osgDB::writeNodeFile(*newNode, "out.osg");
std::vector<NifOsg::Controller > controllers;
osg::Group* newNode = new osg::Group;
NifOsg::Loader loader;
loader.resourceManager = &resourceMgr;
loader.loadAsSkeleton(nif, newNode);
2015-02-26 20:20:07 +00:00
for (unsigned int i=0; i<loader.mControllers.size(); ++i)
controllers.push_back(loader.mControllers[i]);
for (int x=0; x<1;++x)
{
root->addChild(newNode);
}
viewer.setSceneData(root);
viewer.setUpViewInWindow(0, 0, 800, 600);
viewer.realize();
viewer.setCameraManipulator(new osgGA::TrackballManipulator());
2015-03-17 17:53:56 +00:00
WireframeKeyHandler* keyHandler = new WireframeKeyHandler;
viewer.addEventHandler(keyHandler);
2015-02-26 20:20:07 +00:00
viewer.addEventHandler(new osgViewer::StatsHandler);
2015-03-17 17:53:56 +00:00
bool wireframe = false;
while (!viewer.done())
{
2015-03-17 17:53:56 +00:00
if (wireframe != keyHandler->getWireframe())
{
wireframe = keyHandler->getWireframe();
osg::PolygonMode* mode = new osg::PolygonMode;
mode->setMode(osg::PolygonMode::FRONT_AND_BACK,
wireframe ? osg::PolygonMode::LINE : osg::PolygonMode::FILL);
root->getOrCreateStateSet()->setAttributeAndModes(mode, osg::StateAttribute::ON);
root->getOrCreateStateSet()->setMode(GL_CULL_FACE, wireframe ? osg::StateAttribute::OFF
: osg::StateAttribute::ON);
}
viewer.frame();
2015-02-26 20:20:07 +00:00
for (unsigned int i=0; i<controllers.size(); ++i)
controllers[i].update();
}
2015-02-26 20:20:07 +00:00
return 0;
}