Hi.
I'm trying to alter the mesh of a simple cube wich I created with ManualObject (whereas the vertices, normals and indices are copied from this codesnippet:
http://www.ogre3d.org/wiki/index.php/GeneratingAMesh)
Before setting the vertices and normals I set dynamic 'true' and after that I set the indices. The cube is built correctly - so far, so good.
Now I made a function to move a vertex of this cube to test ManualObject::beginUpdate(). I call this function after attaching the manual cube mesh to a scene node. But the mesh doesn't change
Here is my code:
# Create the mesh:
Code: Select all
void MeshGenerator::createManualObj(Ogre::String name) {
using namespace Ogre;
String objMaterial = xmlConf->getValue("manObjMaterial");
/* Set the vertices and normals of the cube */
const float sqrt13 = 0.577350269f; /* sqrt(1/3) */
int i = 0;
cubeVertices.push_back(Vector3(-100.0,100.0,-100.0));
cubeNormals.push_back(Vector3(-sqrt13,sqrt13,-sqrt13));
cubeVertices.push_back(Vector3(100.0,100.0,-100.0));
cubeNormals.push_back(Vector3(sqrt13,sqrt13,-sqrt13));
cubeVertices.push_back(Vector3(100.0,-100.0,-100.0));
cubeNormals.push_back(Vector3(sqrt13,-sqrt13,-sqrt13));
cubeVertices.push_back(Vector3(-100.0,-100.0,-100.0));
cubeNormals.push_back(Vector3(-sqrt13,-sqrt13,-sqrt13));
cubeVertices.push_back(Vector3(-100.0,100.0,100.0));
cubeNormals.push_back(Vector3(-sqrt13,sqrt13,sqrt13));
cubeVertices.push_back(Vector3(100.0,100.0,100.0));
cubeNormals.push_back(Vector3(sqrt13,sqrt13,sqrt13));
cubeVertices.push_back(Vector3(100.0,-100.0,100.0));
cubeNormals.push_back(Vector3(sqrt13,-sqrt13,sqrt13));
cubeVertices.push_back(Vector3(-100.0,-100.0,100.0));
cubeNormals.push_back(Vector3(-sqrt13,-sqrt13,sqrt13));
/* Define 12 triangles (two triangles per cube face) */
const size_t ibufCount = 36;
unsigned short faces[ibufCount] = {
0,2,3,
0,1,2,
1,6,2,
1,5,6,
4,6,5,
4,7,6,
0,7,4,
0,3,7,
0,5,1,
0,4,5,
2,7,3,
2,6,7
};
/* Create a ManualObject with the given name */
obj = new ManualObject(name);
/* setDynamic = true - to alter the geometry after creating manualObject */
obj->setDynamic(true);
/* Start a new section - CUBE */
obj->begin(objMaterial, RenderOperation::OT_TRIANGLE_LIST);
/* Set the vertices and normals of the cube in the object*/
for(i = 0; i < 8; i++) {
obj->position(cubeVertices[i]);
obj->normal(cubeNormals[i]);
} // for(i = 0; i < 8; i++)
/* Set the faces (triangles) of the cube */
for (i = 0; i < ibufCount; i++) {
obj->index(faces[i]);
}
/* Finish object creation */
obj->end();
/* Convert object to a mesh, so we can create many instances of it */
MeshPtr mesh = obj->convertToMesh(name);
}
# Manipulate the mesh
Code: Select all
void MeshGenerator::manipulateObject() {
using namespace Ogre;
const size_t ibufCount = 36;
unsigned short faces[ibufCount] = {
0,2,3,
0,1,2,
1,6,2,
1,5,6,
4,6,5,
4,7,6,
0,7,4,
0,3,7,
0,5,1,
0,4,5,
2,7,3,
2,6,7
};
/* Move some vertices for testing */
cubeVertices[1] = Vector3(1000.0,1000.0,-1000.0);
cubeVertices[2] = Vector3(500.0,-500.0,-500.0);
obj->beginUpdate(0);
/* Set the vertices and normals of the cube in the object*/
for(int i = 0; i < 8; i++) {
obj->position(cubeVertices[i]);
obj->normal(cubeNormals[i]);
} // for(i = 0; i < 8; i++)
/* Set the faces (triangles) of the cube */
for (int i = 0; i < ibufCount; i++) {
obj->index(faces[i]);
}
obj->end();
}
# In createScene():
Code: Select all
[...]
mg->createManualObj("manualObject");
ent1 = mSceneMgr->createEntity("manualObject_1", "manualObject");
node1 = mSceneMgr->getRootSceneNode()->createChildSceneNode();
node1->attachObject(ent1);
mg->manipulateObject();
[...]
The manipulateOject() function works fine, if I convert the manualObject ( MeshPtr mesh = obj->convertToMesh("manualObject"); ) at the end of this function instead of the end of createManualObj() and call mg->manipulateObject() directly after mg->createManualObj("manualObject") - then the vertices of the cube mesh are moved as expected.
Have I missed anything?