#include <maya/MPlug.h>
#include "MTextureCache.h"
#include "NodeMonitor.h"
MTextureCache* MTextureCache::m_instance = NULL;
int MTextureCache::refcount = 0;
MTextureCacheElement::~MTextureCacheElement()
{
if (m_texture)
{
delete m_texture;
m_texture = NULL;
}
}
MTextureCache::~MTextureCache()
{
if (m_textureTable.empty())
return;
string_to_cacheElement_map::iterator p = m_textureTable.begin();
for ( ; p != m_textureTable.end(); ++p)
{
delete p->second;
}
}
MTexture* MTextureCache::texture(
MObject textureObj,
MTexture::Type type ,
bool mipmapped ,
GLenum target )
{
MString textureName = getNameFromObj(textureObj);
textureName == "")
return NULL;
MTextureCacheElement *texCacheElement =
m_textureTable[textureName.
asChar()];
bool newTexture = !texCacheElement;
bool textureDirty = texCacheElement && texCacheElement->fMonitor.dirty();
if (textureDirty)
{
texCacheElement->fMonitor.stopWatching();
delete texCacheElement->m_texture;
texCacheElement->m_texture = NULL;
}
if (newTexture)
{
texCacheElement = new MTextureCacheElement;
texCacheElement->fMonitor.setManager(this);
m_textureTable[textureName.
asChar()] = texCacheElement;
}
if (textureDirty || newTexture)
{
MPlug filenamePlug( textureObj,
textureNode.attribute(
MString(
"fileTextureName")) );
texCacheElement->m_texture = new MTexture;
texCacheElement->fMonitor.watch(textureObj);
if (texCacheElement->m_texture->load(textureFilename,
type, mipmapped, target) == false)
{
delete texCacheElement;
texCacheElement = NULL;
m_textureTable.erase(textureName.
asChar());
return NULL;
}
}
texCacheElement->lastAccessedTimestamp = m_currentTimestamp;
return texCacheElement->texture();
}
bool MTextureCache::bind(
MObject textureObj,
MTexture::Type type ,
bool mipmapped ,
GLenum target )
{
MTexture* pTex = texture(textureObj, type, mipmapped, target);
if (pTex)
{
pTex->bind();
return true;
}
return false;
}
{
MTextureCacheElement *texCacheElement = m_textureTable[oldName.
asChar()];
delete texCacheElement->m_texture;
texCacheElement->m_texture = NULL;
}
void MTextureCache::incrementTimestamp(unsigned int increment )
{
m_currentTimestamp += increment;
}