forked from Mirrors/openclonk
Fixed inheriting materials using textures
parent
639b40bbb6
commit
dfcc0207b3
|
@ -56,12 +56,30 @@ public:
|
||||||
class StdMeshMaterialTextureUnit
|
class StdMeshMaterialTextureUnit
|
||||||
{
|
{
|
||||||
public:
|
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();
|
||||||
|
StdMeshMaterialTextureUnit(const StdMeshMaterialTextureUnit& other);
|
||||||
~StdMeshMaterialTextureUnit();
|
~StdMeshMaterialTextureUnit();
|
||||||
|
|
||||||
|
StdMeshMaterialTextureUnit& operator=(const StdMeshMaterialTextureUnit&);
|
||||||
|
|
||||||
void Load(StdMeshMaterialParserCtx& ctx);
|
void Load(StdMeshMaterialParserCtx& ctx);
|
||||||
|
|
||||||
CTexRef* Texture;
|
const CTexRef& GetTexture() const { return Texture->Tex; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
TexRef* Texture;
|
||||||
};
|
};
|
||||||
|
|
||||||
class StdMeshMaterialPass
|
class StdMeshMaterialPass
|
||||||
|
|
|
@ -181,8 +181,42 @@ void StdMeshMaterialParserCtx::ErrorUnexpectedIdentifier(const StdStrBuf& identi
|
||||||
Error(StdStrBuf("Unexpected identifier: '") + identifier + "'");
|
Error(StdStrBuf("Unexpected identifier: '") + identifier + "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
StdMeshMaterialTextureUnit::StdMeshMaterialTextureUnit(): Texture(NULL) {}
|
StdMeshMaterialTextureUnit::TexRef::TexRef(unsigned int size):
|
||||||
StdMeshMaterialTextureUnit::~StdMeshMaterialTextureUnit() { delete Texture; }
|
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)
|
void StdMeshMaterialTextureUnit::Load(StdMeshMaterialParserCtx& ctx)
|
||||||
{
|
{
|
||||||
|
@ -201,12 +235,13 @@ void StdMeshMaterialTextureUnit::Load(StdMeshMaterialParserCtx& ctx)
|
||||||
if(png.iWdt != png.iHgt)
|
if(png.iWdt != png.iHgt)
|
||||||
ctx.Error(StdStrBuf("Texture '") + token_name + "' is not quadratic");
|
ctx.Error(StdStrBuf("Texture '") + token_name + "' is not quadratic");
|
||||||
|
|
||||||
Texture = new CTexRef(png.iWdt, false);
|
Texture = new TexRef(png.iWdt);
|
||||||
Texture->Lock();
|
|
||||||
|
Texture->Tex.Lock();
|
||||||
for(unsigned int y = 0; y < png.iHgt; ++y)
|
for(unsigned int y = 0; y < png.iHgt; ++y)
|
||||||
for(unsigned int x = 0; x < png.iWdt; ++x)
|
for(unsigned int x = 0; x < png.iWdt; ++x)
|
||||||
Texture->SetPix4(x, y, png.GetPix(x, y));
|
Texture->Tex.SetPix4(x, y, png.GetPix(x, y));
|
||||||
Texture->Unlock();
|
Texture->Tex.Unlock();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
ctx.ErrorUnexpectedIdentifier(token_name);
|
ctx.ErrorUnexpectedIdentifier(token_name);
|
||||||
|
|
Loading…
Reference in New Issue