Creating a mesh (ManualObject) and assigning it a material

durium

30-10-2012 14:19:24

Hello world !

I have a problem when using mogre. When i try to create a mesh via ManualObject like this :

ManualObject block = this.mSceneMgr.CreateManualObject("block");
block.Begin("Cube", RenderOperation.OperationTypes.OT_TRIANGLE_LIST);

block.Position(new Vector3(0, 0, 0));
block.Position(new Vector3(0, 100, 0));
block.Position(new Vector3(100, 100, 0));
block.Position(new Vector3(100, 0, 0));

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

block.End();
block.ConvertToMesh("blockMesh");


I then display it on the scene and assign it a material:

Entity ent = mSceneMgr.CreateEntity("Head", "blockMesh");
ent.SetMaterialName("Cube");

SceneNode node = mSceneMgr.RootSceneNode.CreateChildSceneNode("HeadNode");
node.AttachObject(ent);
node.Yaw(new Degree(180));

However, the material doesn't seem to be assigned. It is found : it don't have the have you forgotten to define it in a .material file error.
Here is my .material file :
material Cube
{
technique
{
pass
{
ambient 1.0 1.0 1.0
diffuse 1.0 1.0 1.0

texture_unit
{
grass_1024.jpg
}
}
}
}

What is wrong with my code ?

durium

30-10-2012 22:45:51

Hi all !
I've been having having some troubles with Manual objects but i near the end...
I successfully created a face. I am now trying to apply a texture on it. But that feature seems to bug on Mogre (tested it on Ogre) :


Any idea on how i could make the texture apply only on the whole face ?
Here is my code :
ManualObject block = new ManualObject("front");

block.Begin("", RenderOperation.OperationTypes.OT_TRIANGLE_LIST);
block.Position(new Vector3(0, 0, 0));
block.TextureCoord(0, 0);

block.Position(new Vector3(0, 100, 0));
block.TextureCoord(1, 0);

block.Position(new Vector3(100, 100, 0));
block.TextureCoord(0, 1);

block.Position(new Vector3(100, 0, 0));
block.TextureCoord(1, 1);

block.Quad(0, 1, 2, 3);
block.End();
block.ConvertToMesh("blockMesh");
Entity ent = mSceneMgr.CreateEntity("Head", "blockMesh");
ent.SetMaterialName("Cube");

SceneNode node = mSceneMgr.RootSceneNode.CreateChildSceneNode("HeadNode");
node.AttachObject(ent);
node.Yaw(new Degree(-180));

smiley80

31-10-2012 11:57:40

Your texture coordinates are wrong:
block.Position(new Vector3(0, 0, 0));
block.TextureCoord(1, 1);
block.Position(new Vector3(0, 100, 0));
block.TextureCoord(1, 0);
block.Position(new Vector3(100, 100, 0));
block.TextureCoord(0, 0);
block.Position(new Vector3(100, 0, 0));
block.TextureCoord(0, 1);


And please don't create new topics when a follow-up post would be sufficient.

durium

31-10-2012 13:50:44

Ok thanks smiley80 !

And please don't create new topics when a follow-up post would be sufficient.
You're right i won't do it again.

I got another question about ManualObject :
How can i create subentity ?
Example :
ManualObject block = new ManualObject("front");

block.Begin("Cube", RenderOperation.OperationTypes.OT_TRIANGLE_LIST);
block.Position(new Vector3(0, 0, 0));
block.TextureCoord(1, 1);

block.Position(new Vector3(0, 100, 0));
block.TextureCoord(1, 0);

block.Position(new Vector3(100, 100, 0));
block.TextureCoord(0, 0);

block.Position(new Vector3(100, 0, 0));
block.TextureCoord(0, 1);

block.Quad(3, 2, 1, 0);
block.End();
LogManager.Singleton.DefaultLog.LogMessage("Block sub1 is named : " + block.Name);
block.Position(new Vector3(0, 0, -100));
block.TextureCoord(1, 1);

block.Position(new Vector3(0, 100, -100));
block.TextureCoord(1, 0);

block.Position(new Vector3(100, 100, -100));
block.TextureCoord(0, 0);

block.Position(new Vector3(100, 0, -100));
block.TextureCoord(0, 1);

block.Quad(0, 1, 2, 3);
block.End();
block.ConvertToMesh("blockMesh");

With this code, the second face isn't a subentity.
I can still set the material for each face before converting it to a mesh. But once it is done, i i try to set the material name it'll set it on the 2 faces.
If i also try to get the subentity, i need a name. However, the two faces belong to the entity which is named "front" and can't be set a name...

Thanks again !

durium

01-11-2012 13:26:45

Solved here :
http://www.ogre3d.org/forums/viewtopic.php?f=1&t=74148

Beauty

01-11-2012 14:10:15

block.ConvertToMesh("blockMesh");
You don't need to convert a ManualObject to a mesh. Just attach the ManOb to the SceneNode.
Converting to a mesh can be useful if you want to create several instances of a ManualObject.

However, the material doesn't seem to be assigned.
When you have a problem, have a look to the ogre.log file. There can be useful information.
For example if you try use a material, which doesn't exists, then there is a warning, which tells you the name of the missing material.

You have the second begin() call missing. Otherwise begin/end() blocks with different materials is exactly what creates sub-entities.
In this case you'll get an exception and ogre.log should tell you the reason.

I suppose you know the wiki page ManualObject.
If not, have a look. There I added useful information and links.