From dfcc0207b3cfa19642249a6a3ea2fcee3f4a582f Mon Sep 17 00:00:00 2001 From: Armin Burgmeier Date: Wed, 8 Jul 2009 22:27:29 +0200 Subject: [PATCH] Fixed inheriting materials using textures --- standard/inc/StdMeshMaterial.h | 20 +++++++++++++- standard/src/StdMeshMaterial.cpp | 47 ++++++++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/standard/inc/StdMeshMaterial.h b/standard/inc/StdMeshMaterial.h index 216ff46f3..cc1def5cf 100644 --- a/standard/inc/StdMeshMaterial.h +++ b/standard/inc/StdMeshMaterial.h @@ -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 diff --git a/standard/src/StdMeshMaterial.cpp b/standard/src/StdMeshMaterial.cpp index 6c0b10f9a..482d60eb8 100644 --- a/standard/src/StdMeshMaterial.cpp +++ b/standard/src/StdMeshMaterial.cpp @@ -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);