I have a little design problem.
I have a Material class, that always has a VertexShader and PixelShader and additionally tons of states (Textures, Culling etc.)
The problem is: Many objects will use the same Material but the Material should only created ONCE (the first time the Material is acquired) and all
other requests to that material the already created Material should just be returned.
The only way I can think of is this:
Material* mat = manager->getMaterial("MyMaterial");
if(mat == NULL) {
// This is the first time someone wants to use this material. So create it
mat = manager->getMaterial("MyMaterial");
VertexShader* vs = manager->loadVertexShader("shader.vsh");
PixelShader* ps = manager->loadVertexShader("shader.psh");
mat->setVS(vs);
mat->setPS(ps);
mat->setTexture(0, myTexture);
object->setMaterial(mat);
}
else {
object->setMaterial(mat);
}
The problem is: I have to use this whole code block EVERYTIME I need this material. Cause I can never be sure if the material is already loaded.
This is really ugly:(
Does anyone has an idea how I could simplify this?