forked from mirror/openmw-tes3mp
Move MyGUI texture to a separate file
parent
62847f0489
commit
db7fe1952d
@ -0,0 +1,164 @@
|
|||||||
|
#include "myguitexture.hpp"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <osg/Texture2D>
|
||||||
|
|
||||||
|
#include <components/resource/texturemanager.hpp>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
#ifndef OPENMW_MWGUI_MYGUITEXTURE_H
|
||||||
|
#define OPENMW_MWGUI_MYGUITEXTURE_H
|
||||||
|
|
||||||
|
#include <MyGUI_ITexture.h>
|
||||||
|
|
||||||
|
#include <osg/ref_ptr>
|
||||||
|
|
||||||
|
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<osg::Image> mLockedImage;
|
||||||
|
osg::ref_ptr<osg::Texture2D> 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
|
Loading…
Reference in New Issue