[SOLVED] DynamicMesh crash


photo

Recommended Posts

Hello,

I'm following the sample found in the documentation to create a dynamic mesh with a large number of vertices: https://developer.unigine.com/en/docs/2.9/code/usage/dynamic_meshes/index?rlang=cpp#big_number

I tried the following code, but it crashes with an out of bound exception. (trying to create a simple hollow cylinder, with 4 vertices on each side and no caps)

int AppWorldLogic::init()
{
	mesh = ObjectMeshDynamic::create();

	mesh->setWorldTransform(translate(Vec3(0.0f, 0.0f, 2.0f)));
	mesh->setMaterial("mesh_base", "*");

	mesh->allocateVertex(8);
	mesh->allocateIndices(24);

	createQuadVertices(vec3(0, 0, 0));
	createQuadVertices(vec3(0, 3, 0));
	createTube();

	// calculate tangent vectors
	mesh->updateTangents();

	// calculate a mesh bounding box
	mesh->updateBounds();

	return 1;
}

void AppWorldLogic::createQuadVertices(vec3 const& pos)
{
	mesh->addVertex(pos + vec3(0, 0, 1));
	mesh->addVertex(pos + vec3(1, 0, 0));
	mesh->addVertex(pos + vec3(0, 0, -1));
	mesh->addVertex(pos + vec3(-1, 0, 0));
}
void AppWorldLogic::createTube()
{
	int i = mesh->getNumVertex() - 4;
	for (int k=0; k < 4; ++k, ++i) {
		mesh->addIndex(i);
		mesh->addIndex(i + 1);
		mesh->addIndex(i + 1 - 4);
		mesh->addIndex(i);
		mesh->addIndex(i + 1 - 4);
		mesh->addIndex(i - 4);
	}
}

I guess I'm missing something, but the code is very similar to the sample, so maybe the sample too miss something?

Link to post

Hi Stephane,

are you sure you added the right vertex indices to the mesh? For your createTube()-function check, when k ==3. The line mesh->addIndex(i+1) results in adding the index 8, which is not valid, because you only added 8 vertices in the beginning (so range index 0-7).

The object didn't check, if your indices are valids or out of bounds. But when using them afterwards.

 

Best,

Christian

  • Like 1
Link to post

Ooooh ! The silliest and oldest of error, forgetting the modulo. Thanks for pointing it out! (and thinking that I put a bunch of log::message in my code and didn't catch it, I feel ashamed..)

Thanks Christian!

Link to post
  • morbid changed the title to [SOLVED] DynamicMesh crash