2013-06-21 16:49:29 +00:00
|
|
|
#include "TextureResource.h"
|
|
|
|
#include "../Log.h"
|
|
|
|
#include "../platform.h"
|
|
|
|
#include GLHEADER
|
|
|
|
#include "../ImageIO.h"
|
|
|
|
#include "../Renderer.h"
|
|
|
|
|
2014-03-20 01:13:59 +00:00
|
|
|
#include "SVGResource.h"
|
|
|
|
|
2014-01-19 18:23:01 +00:00
|
|
|
std::map< TextureResource::TextureKeyType, std::weak_ptr<TextureResource> > TextureResource::sTextureMap;
|
2013-07-09 05:44:24 +00:00
|
|
|
|
2014-01-19 18:23:01 +00:00
|
|
|
TextureResource::TextureResource(const std::string& path, bool tile) :
|
|
|
|
mTextureID(0), mPath(path), mTextureSize(Eigen::Vector2i::Zero()), mTile(tile)
|
2013-06-21 16:49:29 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TextureResource::~TextureResource()
|
|
|
|
{
|
|
|
|
deinit();
|
|
|
|
}
|
|
|
|
|
2013-10-04 23:09:54 +00:00
|
|
|
void TextureResource::unload(std::shared_ptr<ResourceManager>& rm)
|
2013-07-09 05:44:24 +00:00
|
|
|
{
|
|
|
|
deinit();
|
|
|
|
}
|
|
|
|
|
2013-10-04 23:09:54 +00:00
|
|
|
void TextureResource::reload(std::shared_ptr<ResourceManager>& rm)
|
2013-07-09 05:44:24 +00:00
|
|
|
{
|
|
|
|
if(!mPath.empty())
|
2014-03-20 01:13:59 +00:00
|
|
|
{
|
|
|
|
const ResourceData& data = rm->getFileData(mPath);
|
|
|
|
initFromMemory((const char*)data.ptr.get(), data.length);
|
|
|
|
}
|
2013-07-09 05:44:24 +00:00
|
|
|
}
|
|
|
|
|
2014-03-20 01:13:59 +00:00
|
|
|
void TextureResource::initFromPixels(const unsigned char* dataRGBA, size_t width, size_t height)
|
2013-06-21 16:49:29 +00:00
|
|
|
{
|
|
|
|
deinit();
|
|
|
|
|
2014-03-20 01:13:59 +00:00
|
|
|
assert(width > 0 && height > 0);
|
2013-06-21 16:49:29 +00:00
|
|
|
|
|
|
|
//now for the openGL texture stuff
|
|
|
|
glGenTextures(1, &mTextureID);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, mTextureID);
|
|
|
|
|
2014-03-20 01:13:59 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, dataRGBA);
|
2013-06-21 16:49:29 +00:00
|
|
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
|
2014-01-19 18:23:01 +00:00
|
|
|
const GLint wrapMode = mTile ? GL_REPEAT : GL_CLAMP_TO_EDGE;
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode);
|
2013-06-21 16:49:29 +00:00
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
mTextureSize << width, height;
|
2013-06-21 16:49:29 +00:00
|
|
|
}
|
|
|
|
|
2013-09-20 23:55:05 +00:00
|
|
|
void TextureResource::initFromMemory(const char* data, size_t length)
|
|
|
|
{
|
|
|
|
size_t width, height;
|
|
|
|
std::vector<unsigned char> imageRGBA = ImageIO::loadFromMemoryRGBA32((const unsigned char*)(data), length, width, height);
|
|
|
|
|
|
|
|
if(imageRGBA.size() == 0)
|
|
|
|
{
|
2014-03-24 21:29:56 +00:00
|
|
|
LOG(LogError) << "Could not initialize texture from memory, invalid data! (file path: " << mPath << ", data ptr: " << (size_t)data << ", reported size: " << length << ")";
|
2013-09-20 23:55:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-20 01:13:59 +00:00
|
|
|
initFromPixels(imageRGBA.data(), width, height);
|
2013-09-20 23:55:05 +00:00
|
|
|
}
|
|
|
|
|
2013-07-09 05:44:24 +00:00
|
|
|
void TextureResource::deinit()
|
|
|
|
{
|
|
|
|
if(mTextureID != 0)
|
|
|
|
{
|
|
|
|
glDeleteTextures(1, &mTextureID);
|
|
|
|
mTextureID = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-20 01:13:59 +00:00
|
|
|
const Eigen::Vector2i& TextureResource::getSize() const
|
2013-07-09 05:44:24 +00:00
|
|
|
{
|
|
|
|
return mTextureSize;
|
|
|
|
}
|
|
|
|
|
2014-01-19 18:23:01 +00:00
|
|
|
bool TextureResource::isTiled() const
|
|
|
|
{
|
|
|
|
return mTile;
|
|
|
|
}
|
|
|
|
|
2013-07-09 05:44:24 +00:00
|
|
|
void TextureResource::bind() const
|
|
|
|
{
|
|
|
|
if(mTextureID != 0)
|
|
|
|
glBindTexture(GL_TEXTURE_2D, mTextureID);
|
|
|
|
else
|
|
|
|
LOG(LogError) << "Tried to bind uninitialized texture!";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-19 18:23:01 +00:00
|
|
|
std::shared_ptr<TextureResource> TextureResource::get(const std::string& path, bool tile)
|
2013-07-09 05:44:24 +00:00
|
|
|
{
|
2013-10-04 23:09:54 +00:00
|
|
|
std::shared_ptr<ResourceManager>& rm = ResourceManager::getInstance();
|
|
|
|
|
2013-07-09 05:44:24 +00:00
|
|
|
if(path.empty())
|
2013-08-07 04:35:06 +00:00
|
|
|
{
|
2014-01-19 18:23:01 +00:00
|
|
|
std::shared_ptr<TextureResource> tex(new TextureResource("", tile));
|
|
|
|
rm->addReloadable(tex); //make sure we get properly deinitialized even though we do nothing on reinitialization
|
2013-08-07 04:35:06 +00:00
|
|
|
return tex;
|
|
|
|
}
|
2013-07-09 05:44:24 +00:00
|
|
|
|
2014-01-19 18:23:01 +00:00
|
|
|
TextureKeyType key(path, tile);
|
|
|
|
auto foundTexture = sTextureMap.find(key);
|
2013-07-09 05:44:24 +00:00
|
|
|
if(foundTexture != sTextureMap.end())
|
|
|
|
{
|
|
|
|
if(!foundTexture->second.expired())
|
|
|
|
return foundTexture->second.lock();
|
|
|
|
}
|
|
|
|
|
2014-03-20 01:13:59 +00:00
|
|
|
// need to create it
|
|
|
|
std::shared_ptr<TextureResource> tex;
|
|
|
|
|
|
|
|
// is it an SVG?
|
|
|
|
if(path.substr(path.size() - 4, std::string::npos) == ".svg")
|
|
|
|
{
|
|
|
|
// probably
|
2014-03-22 18:04:14 +00:00
|
|
|
// don't add it to our map because 2 svgs might be rasterized at different sizes
|
2014-03-20 01:13:59 +00:00
|
|
|
tex = std::shared_ptr<SVGResource>(new SVGResource(path, tile));
|
2014-03-22 18:04:14 +00:00
|
|
|
rm->addReloadable(tex);
|
|
|
|
tex->reload(rm);
|
|
|
|
return tex;
|
2014-03-20 01:13:59 +00:00
|
|
|
}else{
|
|
|
|
tex = std::shared_ptr<TextureResource>(new TextureResource(path, tile));
|
|
|
|
}
|
|
|
|
|
2014-01-19 18:23:01 +00:00
|
|
|
sTextureMap[key] = std::weak_ptr<TextureResource>(tex);
|
2013-10-04 23:09:54 +00:00
|
|
|
rm->addReloadable(tex);
|
2014-03-20 01:13:59 +00:00
|
|
|
tex->reload(ResourceManager::getInstance());
|
2013-07-09 05:44:24 +00:00
|
|
|
return tex;
|
|
|
|
}
|
2014-03-24 21:29:56 +00:00
|
|
|
|
|
|
|
bool TextureResource::isInitialized() const
|
|
|
|
{
|
|
|
|
return mTextureID != 0;
|
|
|
|
}
|