MaterialPtr of dynamic ManualObject not updating

Problems building or running the engine, queries about how to use features etc.
Post Reply
FlexVR
Gnoblar
Posts: 1
Joined: Wed Mar 22, 2017 3:20 am

MaterialPtr of dynamic ManualObject not updating

Post by FlexVR »

I'm facing some issues in trying to update the material(s) of a dynamic ManualObject that I am using.

What I'm trying to do

I'm developing a WYSIWYG game engine using OGRE. The physical editor is currently based around manipulating boxes into designing structures, and using the various materials and colors to texture your creations.

These boxes are textured with tiling textures, and can be resized. Therefore, I need to be able to modify the TextureScale of the materials of the different faces of each part depending on how large the part is. I've written the functional code for this, and it worked great before!

The problem

I recently moved my physical object creation from ManualObjects to Dynamic ManualObjects, and instead of recreating objects on every size change (which was incredibly slow and CPU taxing), I moved to dynamic ManualObjects and simply using beginUpdate.

However - whenever I make any changes to the Material pointers that were used to configure the ManualObject, the ManualObject doesn't reflect the change! Any changes I make to the Materials (that would normally affect all objects using that material) aren't reflected in the ManualObject!

Upon initialization, the ManualObject grabs the correct materials, and reflects the correct color, texture, etc, but any changes to those materials aren't reflected! Is this a limitation of ManualObjects, or is something wrong with my code?

Here's my code for ManualObject creation - it's alot, but the gist is, it creates a cube at the correct physical location, and applies the materials to each ManualObjectSection. The materials work, but any changes to any of the materials (rightFace, leftFace, etc) aren't reflected.

Code: Select all

private static ManualObject GenerateCubeHelper(float x, float y, float z, String name, MaterialPtr frontFace, MaterialPtr backFace, MaterialPtr leftFace, MaterialPtr rightFace, MaterialPtr topFace, MaterialPtr bottomFace, Vector3 positionOffset)
{
            float xOffset = positionOffset.x;
            float yOffset = positionOffset.y;
            float zOffset = positionOffset.z;

            x /= 2;
            y /= 2;
            z /= 2;

            ManualObject cube = Engine.Renderer.Scene.CreateManualObject(name);

            //cube.Dynamic = true;

            //START GENERATION 

            cube.Begin(bottomFace.Name);

            cube.Position(x + xOffset, -y + yOffset, z + zOffset);
            cube.Normal(0.408248f, -0.816497f, 0.408248f);
            cube.TextureCoord(1, 0);

            //1
            cube.Position(-x + xOffset, -y + yOffset, -z + zOffset);
            cube.Normal(-0.408248f, -0.816497f, -0.408248f);
            cube.TextureCoord(0, 1);

            //2
            cube.Position(x + xOffset, -y + yOffset, -z + zOffset);
            cube.Normal(0.666667f, -0.333333f, -0.666667f);
            cube.TextureCoord(1, 1);

            //3
            cube.Position(-x + xOffset, -y + yOffset, z + zOffset);
            cube.Normal(-0.666667f, -0.333333f, 0.666667f);
            cube.TextureCoord(0, 0);

            cube.Triangle(0, 1, 2);
            cube.Triangle(3, 1, 0);

            cube.End();
            cube.Begin(backFace.Name);

            //4
            cube.Position(x + xOffset, y + yOffset, z + zOffset);
            cube.Normal(0.666667f, 0.333333f, 0.666667f);
            cube.TextureCoord(1, 0);

            //5
            cube.Position(-x + xOffset, -y + yOffset, z + zOffset);
            cube.Normal(-0.666667f, -0.333333f, 0.666667f);
            cube.TextureCoord(0, 1);

            //6
            cube.Position(x + xOffset, -y + yOffset, z + zOffset);
            cube.Normal(0.408248f, -0.816497f, 0.408248f);
            cube.TextureCoord(1, 1);

            //7
            cube.Position(-x + xOffset, y + yOffset, z + zOffset);
            cube.Normal(-0.408248f, 0.816497f, 0.408248f);
            cube.TextureCoord(0, 0);

            cube.Triangle(0, 1, 2);
            cube.Triangle(0, 3, 1);

            cube.End();
            cube.Begin(leftFace.Name);

            //7
            cube.Position(-x + xOffset, y + yOffset, z + zOffset);
            cube.Normal(-0.408248f, 0.816497f, 0.408248f);
            cube.TextureCoord(0, 0);

            //8
            cube.Position(-x + xOffset, y + yOffset, -z + zOffset);
            cube.Normal(-0.666667f, 0.333333f, -0.666667f);
            cube.TextureCoord(0, 1);

            //9
            cube.Position(-x + xOffset, -y + yOffset, -z + zOffset);
            cube.Normal(-0.408248f, -0.816497f, -0.408248f);
            cube.TextureCoord(1, 1);

            //10
            cube.Position(-x + xOffset, -y + yOffset, z + zOffset);
            cube.Normal(-0.666667f, -0.333333f, 0.666667f);
            cube.TextureCoord(1, 0);

            cube.Triangle(1, 2, 3);
            cube.Triangle(3, 0, 1);

            cube.End();
            cube.Begin(rightFace.Name);

            //4
            cube.Position(x + xOffset, y + yOffset, z + zOffset);
            cube.Normal(0.666667f, 0.333333f, 0.666667f);
            cube.TextureCoord(1, 0);

            //11
            cube.Position(x + xOffset, -y + yOffset, -z + zOffset);
            cube.Normal(0.666667f, -0.333333f, -0.666667f);
            cube.TextureCoord(0, 1);

            //12
            cube.Position(x + xOffset, y + yOffset, -z + zOffset);
            cube.Normal(0.408248f, 0.816497f, -0.408248f);
            cube.TextureCoord(1, 1);

            //13
            cube.Position(x + xOffset, -y + yOffset, z + zOffset);
            cube.Normal(0.408248f, -0.816497f, 0.408248f);
            cube.TextureCoord(0, 0);

            cube.Triangle(0, 1, 2);
            cube.Triangle(0, 3, 1);

            cube.End();
            cube.Begin(frontFace.Name);

            //8
            cube.Position(-x + xOffset, y + yOffset, -z + zOffset);
            cube.Normal(-0.666667f, 0.333333f, -0.666667f);
            cube.TextureCoord(0, 1);

            //12
            cube.Position(x + xOffset, y + yOffset, -z + zOffset);
            cube.Normal(0.408248f, 0.816497f, -0.408248f);
            cube.TextureCoord(1, 1);

            //14
            cube.Position(x + xOffset, -y + yOffset, -z + zOffset);
            cube.Normal(0.666667f, -0.333333f, -0.666667f);
            cube.TextureCoord(1, 0);

            //15
            cube.Position(-x + xOffset, -y + yOffset, -z + zOffset);
            cube.Normal(-0.408248f, -0.816497f, -0.408248f);
            cube.TextureCoord(0, 0);

            cube.Triangle(2, 0, 1);
            cube.Triangle(2, 3, 0);

            cube.End();
            cube.Begin(topFace.Name);

            //16
            cube.Position(-x + xOffset, y + yOffset, z + zOffset);
            cube.Normal(-0.408248f, 0.816497f, 0.408248f);
            cube.TextureCoord(1, 0);

            //17
            cube.Position(x + xOffset, y + yOffset, -z + zOffset);
            cube.Normal(0.408248f, 0.816497f, -0.408248f);
            cube.TextureCoord(0, 1);

            //18
            cube.Position(-x + xOffset, y + yOffset, -z + zOffset);
            cube.Normal(-0.666667f, 0.333333f, -0.666667f);
            cube.TextureCoord(1, 1);

            //19
            cube.Position(x + xOffset, y + yOffset, z + zOffset);
            cube.Normal(0.666667f, 0.333333f, 0.666667f);
            cube.TextureCoord(0, 0);

            cube.Triangle(0, 1, 2);
            cube.Triangle(0, 3, 1);

            cube.End();

            return cube;
        }
Here is the updating ManualObject code - it is very similar to the code above:

Code: Select all

private static ManualObject UpdateCubeHelper(ManualObject updating, float x, float y, float z, MaterialPtr frontFace, MaterialPtr backFace, MaterialPtr leftFace, MaterialPtr rightFace, MaterialPtr topFace, MaterialPtr bottomFace, Face updatedFaces, Vector3 positionOffset)
        {
            float xOffset = positionOffset.x;
            float yOffset = positionOffset.y;
            float zOffset = positionOffset.z;

            x /= 2;
            y /= 2;
            z /= 2;

            //START GENERATION 

            updating.BeginUpdate(0);

            updating.Position(x + xOffset, -y + yOffset, z + zOffset);
            updating.Normal(0.408248f, -0.816497f, 0.408248f);
            updating.TextureCoord(1, 0);

            //1
            updating.Position(-x + xOffset, -y + yOffset, -z + zOffset);
            updating.Normal(-0.408248f, -0.816497f, -0.408248f);
            updating.TextureCoord(0, 1);

            //2
            updating.Position(x + xOffset, -y + yOffset, -z + zOffset);
            updating.Normal(0.666667f, -0.333333f, -0.666667f);
            updating.TextureCoord(1, 1);

            //3
            updating.Position(-x + xOffset, -y + yOffset, z + zOffset);
            updating.Normal(-0.666667f, -0.333333f, 0.666667f);
            updating.TextureCoord(0, 0);

            updating.Triangle(0, 1, 2);
            updating.Triangle(3, 1, 0);

            updating.End();
            updating.BeginUpdate(1);

            //4
            updating.Position(x + xOffset, y + yOffset, z + zOffset);
            updating.Normal(0.666667f, 0.333333f, 0.666667f);
            updating.TextureCoord(1, 0);

            //5
            updating.Position(-x + xOffset, -y + yOffset, z + zOffset);
            updating.Normal(-0.666667f, -0.333333f, 0.666667f);
            updating.TextureCoord(0, 1);

            //6
            updating.Position(x + xOffset, -y + yOffset, z + zOffset);
            updating.Normal(0.408248f, -0.816497f, 0.408248f);
            updating.TextureCoord(1, 1);

            //7
            updating.Position(-x + xOffset, y + yOffset, z + zOffset);
            updating.Normal(-0.408248f, 0.816497f, 0.408248f);
            updating.TextureCoord(0, 0);

            updating.Triangle(0, 1, 2);
            updating.Triangle(0, 3, 1);

            updating.End();
            updating.BeginUpdate(2);

            //7
            updating.Position(-x + xOffset, y + yOffset, z + zOffset);
            updating.Normal(-0.408248f, 0.816497f, 0.408248f);
            updating.TextureCoord(0, 0);

            //8
            updating.Position(-x + xOffset, y + yOffset, -z + zOffset);
            updating.Normal(-0.666667f, 0.333333f, -0.666667f);
            updating.TextureCoord(0, 1);

            //9
            updating.Position(-x + xOffset, -y + yOffset, -z + zOffset);
            updating.Normal(-0.408248f, -0.816497f, -0.408248f);
            updating.TextureCoord(1, 1);

            //10
            updating.Position(-x + xOffset, -y + yOffset, z + zOffset);
            updating.Normal(-0.666667f, -0.333333f, 0.666667f);
            updating.TextureCoord(1, 0);

            updating.Triangle(1, 2, 3);
            updating.Triangle(3, 0, 1);

            updating.End();
            updating.BeginUpdate(3);

            //4
            updating.Position(x + xOffset, y + yOffset, z + zOffset);
            updating.Normal(0.666667f, 0.333333f, 0.666667f);
            updating.TextureCoord(1, 0);

            //11
            updating.Position(x + xOffset, -y + yOffset, -z + zOffset);
            updating.Normal(0.666667f, -0.333333f, -0.666667f);
            updating.TextureCoord(0, 1);

            //12
            updating.Position(x + xOffset, y + yOffset, -z + zOffset);
            updating.Normal(0.408248f, 0.816497f, -0.408248f);
            updating.TextureCoord(1, 1);

            //13
            updating.Position(x + xOffset, -y + yOffset, z + zOffset);
            updating.Normal(0.408248f, -0.816497f, 0.408248f);
            updating.TextureCoord(0, 0);

            updating.Triangle(0, 1, 2);
            updating.Triangle(0, 3, 1);

            updating.End();
            updating.BeginUpdate(4);

            //8
            updating.Position(-x + xOffset, y + yOffset, -z + zOffset);
            updating.Normal(-0.666667f, 0.333333f, -0.666667f);
            updating.TextureCoord(0, 1);

            //12
            updating.Position(x + xOffset, y + yOffset, -z + zOffset);
            updating.Normal(0.408248f, 0.816497f, -0.408248f);
            updating.TextureCoord(1, 1);

            //14
            updating.Position(x + xOffset, -y + yOffset, -z + zOffset);
            updating.Normal(0.666667f, -0.333333f, -0.666667f);
            updating.TextureCoord(1, 0);

            //15
            updating.Position(-x + xOffset, -y + yOffset, -z + zOffset);
            updating.Normal(-0.408248f, -0.816497f, -0.408248f);
            updating.TextureCoord(0, 0);

            updating.Triangle(2, 0, 1);
            updating.Triangle(2, 3, 0);

            updating.End();
            updating.BeginUpdate(5);

            //16
            updating.Position(-x + xOffset, y + yOffset, z + zOffset);
            updating.Normal(-0.408248f, 0.816497f, 0.408248f);
            updating.TextureCoord(1, 0);

            //17
            updating.Position(x + xOffset, y + yOffset, -z + zOffset);
            updating.Normal(0.408248f, 0.816497f, -0.408248f);
            updating.TextureCoord(0, 1);

            //18
            updating.Position(-x + xOffset, y + yOffset, -z + zOffset);
            updating.Normal(-0.666667f, 0.333333f, -0.666667f);
            updating.TextureCoord(1, 1);

            //19
            updating.Position(x + xOffset, y + yOffset, z + zOffset);
            updating.Normal(0.666667f, 0.333333f, 0.666667f);
            updating.TextureCoord(0, 0);

            updating.Triangle(0, 1, 2);
            updating.Triangle(0, 3, 1);

            updating.End();

            return updating;
}
This is the code that actually initializes the ManualObject. At this point, all of the MaterialPtr objects are properly initialized and loaded with material data, and is initialized properly.

Code: Select all

_movableObject = MeshGenerator.GenerateCube(
                _size.x,
                _size.y,
                _size.z,
                _UUID.ToString(),
                _frontMaterialPtr,
                _backMaterialPtr,
                _leftMaterialPtr,
                _rightMaterialPtr,
                _topMaterialPtr,
                _bottomMaterialPtr);

            _movableObject.QueryFlags = (uint)QueryFlags.INSTANCE_ENTITY;
            _movableObject.CastShadows = true;

            _sceneNode = Engine.Renderer.CreateSceneNode(this);
Now here's the code that doesn't propogate properly. As you can see, I'm modifying the MaterialPtr with a new color and texture scaling. This change doesn't get applied!

Code: Select all

            
_frontMaterialPtr.SetAmbient(new ColourValue(_color.r / 255f, _color.g / 255f, _color.b / 255f, _color.transparency / 255f));
            //_frontMaterialPtr.SetDiffuse(_color.r / 255f, _color.g / 255f, _color.b / 255f, _color.transparency / 255f);
            //_frontMaterialPtr.SetSpecular(_color.r / 255f, _color.g / 255f, _color.b / 255f, _color.transparency / 255f);
            _frontMaterialPtr.GetTechnique(0).GetPass(0).GetTextureUnitState(0).SetTextureScale(_textureScale.x / size.x, _textureScale.y / size.y);
Important to note, I've tried using the GetSection() and SetMaterialName() methods in my update code, and these don't do anything unfortunately :(
Post Reply