1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-15 23:19:56 +00:00
openmw-tes3mp/apps/openmw/mwrender/util.cpp

68 lines
2.2 KiB
C++
Raw Normal View History

2015-04-19 15:55:56 +00:00
#include "util.hpp"
#include <osg/Node>
#include <osg/ValueObject>
2015-04-19 15:55:56 +00:00
#include <components/resource/resourcesystem.hpp>
2016-02-05 22:03:53 +00:00
#include <components/resource/imagemanager.hpp>
2015-04-19 15:55:56 +00:00
#include <components/misc/resourcehelpers.hpp>
#include <components/sceneutil/visitor.hpp>
2015-04-19 15:55:56 +00:00
namespace MWRender
{
class TextureOverrideVisitor : public osg::NodeVisitor
{
public:
2016-09-18 19:44:30 +00:00
TextureOverrideVisitor(std::string texture, Resource::ResourceSystem* resourcesystem)
: osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
, mTexture(texture)
, mResourcesystem(resourcesystem)
{
}
virtual void apply(osg::Node& node)
{
int index;
osg::ref_ptr<osg::Node> nodePtr(&node);
2016-09-18 19:44:30 +00:00
if (node.getUserValue("overrideFx", index))
{
2016-09-18 19:44:30 +00:00
if (index == 1)
overrideTexture(mTexture, mResourcesystem, nodePtr);
}
traverse(node);
}
std::string mTexture;
Resource::ResourceSystem* mResourcesystem;
};
void overrideFirstRootTexture(const std::string &texture, Resource::ResourceSystem *resourceSystem, osg::ref_ptr<osg::Node> node)
{
2016-09-18 19:44:30 +00:00
TextureOverrideVisitor overrideVisitor(texture, resourceSystem);
node->accept(overrideVisitor);
}
2015-04-19 15:55:56 +00:00
void overrideTexture(const std::string &texture, Resource::ResourceSystem *resourceSystem, osg::ref_ptr<osg::Node> node)
{
if (texture.empty())
return;
std::string correctedTexture = Misc::ResourceHelpers::correctTexturePath(texture, resourceSystem->getVFS());
// Not sure if wrap settings should be pulled from the overridden texture?
osg::ref_ptr<osg::Texture2D> tex = new osg::Texture2D(resourceSystem->getImageManager()->getImage(correctedTexture));
tex->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
tex->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
tex->setName("diffuseMap");
2015-04-19 15:55:56 +00:00
osg::ref_ptr<osg::StateSet> stateset;
if (node->getStateSet())
stateset = new osg::StateSet(*node->getStateSet(), osg::CopyOp::SHALLOW_COPY);
2015-04-19 15:55:56 +00:00
else
stateset = new osg::StateSet;
2016-09-14 14:18:29 +00:00
stateset->setTextureAttribute(0, tex, osg::StateAttribute::OVERRIDE);
2015-04-19 15:55:56 +00:00
node->setStateSet(stateset);
}
}