Fixed inheriting materials using textures

stable-5.2
Armin Burgmeier 2009-07-08 22:27:29 +02:00
parent 639b40bbb6
commit dfcc0207b3
2 changed files with 60 additions and 7 deletions

View File

@ -56,12 +56,30 @@ public:
class StdMeshMaterialTextureUnit
{
public:
// Ref-counted texture. When a meterial inherits from one which contains
// a TextureUnit, then they will share the same CTexRef.
class TexRef
{
public:
TexRef(unsigned int size);
~TexRef();
unsigned int RefCount;
CTexRef Tex;
};
StdMeshMaterialTextureUnit();
StdMeshMaterialTextureUnit(const StdMeshMaterialTextureUnit& other);
~StdMeshMaterialTextureUnit();
StdMeshMaterialTextureUnit& operator=(const StdMeshMaterialTextureUnit&);
void Load(StdMeshMaterialParserCtx& ctx);
CTexRef* Texture;
const CTexRef& GetTexture() const { return Texture->Tex; }
private:
TexRef* Texture;
};
class StdMeshMaterialPass

View File

@ -181,8 +181,42 @@ void StdMeshMaterialParserCtx::ErrorUnexpectedIdentifier(const StdStrBuf& identi
Error(StdStrBuf("Unexpected identifier: '") + identifier + "'");
}
StdMeshMaterialTextureUnit::StdMeshMaterialTextureUnit(): Texture(NULL) {}
StdMeshMaterialTextureUnit::~StdMeshMaterialTextureUnit() { delete Texture; }
StdMeshMaterialTextureUnit::TexRef::TexRef(unsigned int size):
RefCount(1), Tex(size, false)
{
}
StdMeshMaterialTextureUnit::TexRef::~TexRef()
{
assert(RefCount == 0);
}
StdMeshMaterialTextureUnit::StdMeshMaterialTextureUnit():
Texture(NULL)
{
}
StdMeshMaterialTextureUnit::StdMeshMaterialTextureUnit(const StdMeshMaterialTextureUnit& other):
Texture(other.Texture)
{
if(Texture)
++Texture->RefCount;
}
StdMeshMaterialTextureUnit::~StdMeshMaterialTextureUnit()
{
if(Texture && !--Texture->RefCount)
delete Texture;
}
StdMeshMaterialTextureUnit& StdMeshMaterialTextureUnit::operator=(const StdMeshMaterialTextureUnit& other)
{
if(this == &other) return *this;
if(Texture) if(!--Texture->RefCount) delete Texture;
Texture = other.Texture;
if(Texture) ++Texture->RefCount;
return *this;
}
void StdMeshMaterialTextureUnit::Load(StdMeshMaterialParserCtx& ctx)
{
@ -201,12 +235,13 @@ void StdMeshMaterialTextureUnit::Load(StdMeshMaterialParserCtx& ctx)
if(png.iWdt != png.iHgt)
ctx.Error(StdStrBuf("Texture '") + token_name + "' is not quadratic");
Texture = new CTexRef(png.iWdt, false);
Texture->Lock();
Texture = new TexRef(png.iWdt);
Texture->Tex.Lock();
for(unsigned int y = 0; y < png.iHgt; ++y)
for(unsigned int x = 0; x < png.iWdt; ++x)
Texture->SetPix4(x, y, png.GetPix(x, y));
Texture->Unlock();
Texture->Tex.SetPix4(x, y, png.GetPix(x, y));
Texture->Tex.Unlock();
}
else
ctx.ErrorUnexpectedIdentifier(token_name);