From db7fe1952dcf8033628b7a4bca7f9430a9590d50 Mon Sep 17 00:00:00 2001 From: scrawl Date: Fri, 1 May 2015 00:59:41 +0200 Subject: [PATCH] Move MyGUI texture to a separate file --- apps/openmw/CMakeLists.txt | 2 +- apps/openmw/mwgui/myguirendermanager.cpp | 182 +---------------------- apps/openmw/mwgui/myguitexture.cpp | 164 ++++++++++++++++++++ apps/openmw/mwgui/myguitexture.hpp | 64 ++++++++ 4 files changed, 231 insertions(+), 181 deletions(-) create mode 100644 apps/openmw/mwgui/myguitexture.cpp create mode 100644 apps/openmw/mwgui/myguitexture.hpp diff --git a/apps/openmw/CMakeLists.txt b/apps/openmw/CMakeLists.txt index a36dd4e2a..680ea9c86 100644 --- a/apps/openmw/CMakeLists.txt +++ b/apps/openmw/CMakeLists.txt @@ -44,7 +44,7 @@ add_openmw_dir (mwgui itemmodel containeritemmodel inventoryitemmodel sortfilteritemmodel itemview tradeitemmodel companionitemmodel pickpocketitemmodel controllers savegamedialog recharge mode videowidget backgroundimage itemwidget screenfader debugwindow spellmodel spellview - draganddrop timeadvancer jailscreen myguiplatform myguirendermanager myguidatamanager + draganddrop timeadvancer jailscreen myguiplatform myguirendermanager myguidatamanager myguitexture ) add_openmw_dir (mwdialogue diff --git a/apps/openmw/mwgui/myguirendermanager.cpp b/apps/openmw/mwgui/myguirendermanager.cpp index f218d9ff0..e5160bb5b 100644 --- a/apps/openmw/mwgui/myguirendermanager.cpp +++ b/apps/openmw/mwgui/myguirendermanager.cpp @@ -19,6 +19,8 @@ #include +#include "myguitexture.hpp" + #define MYGUI_PLATFORM_LOG_SECTION "Platform" #define MYGUI_PLATFORM_LOG(level, text) MYGUI_LOGGING(MYGUI_PLATFORM_LOG_SECTION, level, text) @@ -217,186 +219,6 @@ void OSGVertexBuffer::create() // --------------------------------------------------------------------------- -class OSGTexture : public MyGUI::ITexture { - std::string mName; - Resource::TextureManager* mTextureManager; - - osg::ref_ptr mLockedImage; - osg::ref_ptr mTexture; - MyGUI::PixelFormat mFormat; - MyGUI::TextureUsage mUsage; - size_t mNumElemBytes; - -public: - OSGTexture(const std::string &name, Resource::TextureManager* textureManager); - virtual ~OSGTexture(); - - virtual const std::string& getName() const { return mName; } - - virtual void createManual(int width, int height, MyGUI::TextureUsage usage, MyGUI::PixelFormat format); - virtual void loadFromFile(const std::string &fname); - virtual void saveToFile(const std::string &fname); - - virtual void destroy(); - - virtual void* lock(MyGUI::TextureUsage access); - virtual void unlock(); - virtual bool isLocked(); - - virtual int getWidth(); - virtual int getHeight(); - - virtual MyGUI::PixelFormat getFormat() { return mFormat; } - virtual MyGUI::TextureUsage getUsage() { return mUsage; } - virtual size_t getNumElemBytes() { return mNumElemBytes; } - - virtual MyGUI::IRenderTarget *getRenderTarget(); - -/*internal:*/ - osg::Texture2D *getTexture() const { return mTexture.get(); } -}; - -OSGTexture::OSGTexture(const std::string &name, Resource::TextureManager* textureManager) - : mName(name) - , mTextureManager(textureManager) - , mFormat(MyGUI::PixelFormat::Unknow) - , mUsage(MyGUI::TextureUsage::Default) - , mNumElemBytes(0) -{ -} - -OSGTexture::~OSGTexture() -{ -} - -void OSGTexture::createManual(int width, int height, MyGUI::TextureUsage usage, MyGUI::PixelFormat format) -{ - GLenum glfmt = GL_NONE; - size_t numelems = 0; - switch(format.getValue()) - { - case MyGUI::PixelFormat::L8: - glfmt = GL_LUMINANCE; - numelems = 1; - break; - case MyGUI::PixelFormat::L8A8: - glfmt = GL_LUMINANCE_ALPHA; - numelems = 2; - break; - case MyGUI::PixelFormat::R8G8B8: - glfmt = GL_RGB; - numelems = 3; - break; - case MyGUI::PixelFormat::R8G8B8A8: - glfmt = GL_RGBA; - numelems = 4; - break; - } - if(glfmt == GL_NONE) - throw std::runtime_error("Texture format not supported"); - - mTexture = new osg::Texture2D(); - mTexture->setTextureSize(width, height); - mTexture->setSourceFormat(glfmt); - mTexture->setSourceType(GL_UNSIGNED_BYTE); - - mTexture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); - mTexture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); - mTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); - mTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); - - mFormat = format; - mUsage = usage; - mNumElemBytes = numelems; -} - -void OSGTexture::destroy() -{ - mTexture = nullptr; - mFormat = MyGUI::PixelFormat::Unknow; - mUsage = MyGUI::TextureUsage::Default; - mNumElemBytes = 0; -} - -void OSGTexture::loadFromFile(const std::string &fname) -{ - mTexture = mTextureManager->getTexture2D(fname, osg::Texture2D::CLAMP, osg::Texture2D::CLAMP); - - // FIXME - mFormat = MyGUI::PixelFormat::R8G8B8; - mUsage = MyGUI::TextureUsage::Static | MyGUI::TextureUsage::Write; - mNumElemBytes = 3; // FIXME -} - -void OSGTexture::saveToFile(const std::string &fname) -{ - std::cerr << "Would save image to file " << fname << std::endl; -} - - -int OSGTexture::getWidth() -{ - if(!mTexture.valid()) - return 0; - osg::Image *image = mTexture->getImage(); - if(image) return image->s(); - return mTexture->getTextureWidth(); -} - -int OSGTexture::getHeight() -{ - if(!mTexture.valid()) - return 0; - osg::Image *image = mTexture->getImage(); - if(image) return image->t(); - return mTexture->getTextureHeight(); -} - - -void *OSGTexture::lock(MyGUI::TextureUsage /*access*/) -{ - MYGUI_PLATFORM_ASSERT(mTexture.valid(), "Texture is not created"); - MYGUI_PLATFORM_ASSERT(!mLockedImage.valid(), "Texture already locked"); - - mLockedImage = mTexture->getImage(); - if(!mLockedImage.valid()) - { - mLockedImage = new osg::Image(); - mLockedImage->allocateImage( - mTexture->getTextureWidth(), mTexture->getTextureHeight(), mTexture->getTextureDepth(), - mTexture->getSourceFormat(), mTexture->getSourceType() - ); - } - return mLockedImage->data(); -} - -void OSGTexture::unlock() -{ - MYGUI_PLATFORM_ASSERT(mLockedImage.valid(), "Texture not locked"); - - // Tell the texture it can get rid of the image for static textures (since - // they aren't expected to update much at all). - mTexture->setImage(mLockedImage.get()); - mTexture->setUnRefImageDataAfterApply(mUsage.isValue(MyGUI::TextureUsage::Static) ? true : false); - mTexture->dirtyTextureObject(); - - mLockedImage = nullptr; -} - -bool OSGTexture::isLocked() -{ - return mLockedImage.valid(); -} - - -// FIXME: Render-to-texture not currently implemented. -MyGUI::IRenderTarget* OSGTexture::getRenderTarget() -{ - return nullptr; -} - -// --------------------------------------------------------------------------- - RenderManager::RenderManager(osgViewer::Viewer *viewer, osg::Group *sceneroot, Resource::TextureManager* textureManager) : mViewer(viewer) , mSceneRoot(sceneroot) diff --git a/apps/openmw/mwgui/myguitexture.cpp b/apps/openmw/mwgui/myguitexture.cpp new file mode 100644 index 000000000..d61e396f1 --- /dev/null +++ b/apps/openmw/mwgui/myguitexture.cpp @@ -0,0 +1,164 @@ +#include "myguitexture.hpp" + +#include +#include + +#include + +#include + +namespace MWGui +{ + + OSGTexture::OSGTexture(const std::string &name, Resource::TextureManager* textureManager) + : mName(name) + , mTextureManager(textureManager) + , mFormat(MyGUI::PixelFormat::Unknow) + , mUsage(MyGUI::TextureUsage::Default) + , mNumElemBytes(0) + { + } + + OSGTexture::OSGTexture(osg::Texture2D *texture) + : mTextureManager(NULL) + , mTexture(texture) + , mFormat(MyGUI::PixelFormat::Unknow) + , mUsage(MyGUI::TextureUsage::Default) + , mNumElemBytes(0) + { + } + + OSGTexture::~OSGTexture() + { + } + + void OSGTexture::createManual(int width, int height, MyGUI::TextureUsage usage, MyGUI::PixelFormat format) + { + GLenum glfmt = GL_NONE; + size_t numelems = 0; + switch(format.getValue()) + { + case MyGUI::PixelFormat::L8: + glfmt = GL_LUMINANCE; + numelems = 1; + break; + case MyGUI::PixelFormat::L8A8: + glfmt = GL_LUMINANCE_ALPHA; + numelems = 2; + break; + case MyGUI::PixelFormat::R8G8B8: + glfmt = GL_RGB; + numelems = 3; + break; + case MyGUI::PixelFormat::R8G8B8A8: + glfmt = GL_RGBA; + numelems = 4; + break; + } + if(glfmt == GL_NONE) + throw std::runtime_error("Texture format not supported"); + + mTexture = new osg::Texture2D(); + mTexture->setTextureSize(width, height); + mTexture->setSourceFormat(glfmt); + mTexture->setSourceType(GL_UNSIGNED_BYTE); + + mTexture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); + mTexture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); + mTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); + mTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); + + mFormat = format; + mUsage = usage; + mNumElemBytes = numelems; + } + + void OSGTexture::destroy() + { + mTexture = nullptr; + mFormat = MyGUI::PixelFormat::Unknow; + mUsage = MyGUI::TextureUsage::Default; + mNumElemBytes = 0; + } + + void OSGTexture::loadFromFile(const std::string &fname) + { + if (!mTextureManager) + throw std::runtime_error("No texturemanager set"); + + mTexture = mTextureManager->getTexture2D(fname, osg::Texture2D::CLAMP, osg::Texture2D::CLAMP); + + // FIXME + mFormat = MyGUI::PixelFormat::R8G8B8; + mUsage = MyGUI::TextureUsage::Static | MyGUI::TextureUsage::Write; + mNumElemBytes = 3; // FIXME + } + + void OSGTexture::saveToFile(const std::string &fname) + { + std::cerr << "Would save image to file " << fname << std::endl; + } + + int OSGTexture::getWidth() + { + if(!mTexture.valid()) + return 0; + osg::Image *image = mTexture->getImage(); + if(image) return image->s(); + return mTexture->getTextureWidth(); + } + + int OSGTexture::getHeight() + { + if(!mTexture.valid()) + return 0; + osg::Image *image = mTexture->getImage(); + if(image) return image->t(); + return mTexture->getTextureHeight(); + } + + void *OSGTexture::lock(MyGUI::TextureUsage /*access*/) + { + if (!mTexture.valid()) + throw std::runtime_error("Texture is not created"); + if (mLockedImage.valid()) + throw std::runtime_error("Texture already locked"); + + mLockedImage = mTexture->getImage(); + if(!mLockedImage.valid()) + { + mLockedImage = new osg::Image(); + mLockedImage->allocateImage( + mTexture->getTextureWidth(), mTexture->getTextureHeight(), mTexture->getTextureDepth(), + mTexture->getSourceFormat(), mTexture->getSourceType() + ); + } + return mLockedImage->data(); + } + + void OSGTexture::unlock() + { + if (!mLockedImage.valid()) + throw std::runtime_error("Texture not locked"); + + // Tell the texture it can get rid of the image for static textures (since + // they aren't expected to update much at all). + mTexture->setImage(mLockedImage.get()); + mTexture->setUnRefImageDataAfterApply(mUsage.isValue(MyGUI::TextureUsage::Static) ? true : false); + mTexture->dirtyTextureObject(); + + mLockedImage = nullptr; + } + + bool OSGTexture::isLocked() + { + return mLockedImage.valid(); + } + + // Render-to-texture not currently implemented. + MyGUI::IRenderTarget* OSGTexture::getRenderTarget() + { + return nullptr; + } + +} diff --git a/apps/openmw/mwgui/myguitexture.hpp b/apps/openmw/mwgui/myguitexture.hpp new file mode 100644 index 000000000..5ae34a3d6 --- /dev/null +++ b/apps/openmw/mwgui/myguitexture.hpp @@ -0,0 +1,64 @@ +#ifndef OPENMW_MWGUI_MYGUITEXTURE_H +#define OPENMW_MWGUI_MYGUITEXTURE_H + +#include + +#include + +namespace osg +{ + class Image; + class Texture2D; +} + +namespace Resource +{ + class TextureManager; +} + +namespace MWGui +{ + + class OSGTexture : public MyGUI::ITexture { + std::string mName; + Resource::TextureManager* mTextureManager; + + osg::ref_ptr mLockedImage; + osg::ref_ptr mTexture; + MyGUI::PixelFormat mFormat; + MyGUI::TextureUsage mUsage; + size_t mNumElemBytes; + + public: + OSGTexture(const std::string &name, Resource::TextureManager* textureManager); + OSGTexture(osg::Texture2D* texture); + virtual ~OSGTexture(); + + virtual const std::string& getName() const { return mName; } + + virtual void createManual(int width, int height, MyGUI::TextureUsage usage, MyGUI::PixelFormat format); + virtual void loadFromFile(const std::string &fname); + virtual void saveToFile(const std::string &fname); + + virtual void destroy(); + + virtual void* lock(MyGUI::TextureUsage access); + virtual void unlock(); + virtual bool isLocked(); + + virtual int getWidth(); + virtual int getHeight(); + + virtual MyGUI::PixelFormat getFormat() { return mFormat; } + virtual MyGUI::TextureUsage getUsage() { return mUsage; } + virtual size_t getNumElemBytes() { return mNumElemBytes; } + + virtual MyGUI::IRenderTarget *getRenderTarget(); + + /*internal:*/ + osg::Texture2D *getTexture() const { return mTexture.get(); } + }; + +} + +#endif