I was trying to use MaterialLock to change the material on an object's mesh in the drawPrepare() method. The problem is that the mesh is just drawn with no material, i.e. it is just white.
I have reproduced the problem using the "Extending Game Object Class" tutorial. Here are the steps to reproduce.
1. Modify the drawPrepare() method in the tutorial like this.
Code:
UInt NewObj::drawPrepare()
{
if(mesh && Frustum(mesh->box,matrix()))
{
MaterialLock = Materials("Mtrl/Brick/0.mtrl");
mesh->draw(matrix());
MaterialLock = NULL;
}
return 0;
}
If you just run the tutorial as is, with just this modification, it should work just fine. You should see the three barrels with the brick texture applied to them.
2. Now go to the Model Editor and create a new mesh (using Add from the Mesh menu - doesn't matter which one, a box is fine). Then save this mesh to the Obj/Item/Misc/Barrel folder as "1.mesh"
3. Finally, change the create() method in the tutorial code to load this mesh instead of the default barrel mesh. Like this.
Code:
void NewObj::create(Game::ObjParams &obj_params)
{
position=obj_params.matrixFinal().pos;
mesh =Meshes("Obj/Item/Misc/Barrel/1.mesh");
}
Now run the tutorial and you'll see the problem. The new mesh will be drawn white (no material). I haven't been able to figure out what the difference is between the barrel mesh and a new mesh that you can create in the model editor. This problem is happening with any mesh that I create or import from 3ds max. I'm wondering if there is some bug here.
I have found a strange work around for the problem though (which makes me believe there is a bug in EE). If you apply a material to the new mesh in the Model Editor first and then save it with this material, the code then works correctly.
For example, load the "1.mesh" that you previously created back into the Model Editor. Now load a texture into the Material editor (doesn't matter which one - I picked Mtrl/Grass/0.mtrl) and click Set to apply it to the mesh. Finally save the mesh and then run the modified tutorial code again. It should now work correctly. You won't see the grass material on the mesh but the brick one instead.
On a side note, setMaterial() on the mesh always works correctly. It is just MaterialLock that isn't working. I would prefer to use MaterialLock as it gives a big performance boost in my situation (lots of texture swapping).