Jump to content

Procedurally Create A Mesh


photo

Recommended Posts

Hi,

 

I'm creating an application where I wish to create a mesh based on dynamic camera data that is provided by a depth sensor. I have created a similar application in Unity/Ogre/DirectX using the following process:

 

1) Create a Mesh of size depthImageWidth x depthImageHeight * 6 (a triangle mesh where each each depth pixel represents a triangle quad). 

2) Pass DepthImage into a custom vertex/geometry shader that updates location of vertices. The shader requires all the intrinsic/extrinsic (4x4 KRt optics) to be passed into the shader (which has to be broken down into smaller vec4s or floatArrays) while continuously passing in the depthImage (via setProceduralImageTexture I assume) to update the vertices/geometry of the mesh I create in step 1. 

 

So in essence, I just need to create a mesh once that is of the correct size which will get updated by the shader based on the depth sensor data. 

 

I've hit a bit of a snag on the first step. I can create a mesh programmatically, but when the mesh becomes too large (e.g., 32768 in the below simple sample), Unigine seems to slow to a halt. Here's a simple example (a little messy from testing) I've been using for testing:

	Unigine::MeshPtr baseMesh = Unigine::Mesh::create();
	int surfaceId = baseMesh->addSurface("baseMeshSurface");
	int surfaceTarget = 0;
	int index = 0;
	for (int i = 0; i < 32768; i++)
	{
		baseMesh->addVertex(Unigine::vec3(0, 0, 0), surfaceId);
		baseMesh->addVertex(Unigine::vec3(index++, 0, 0), surfaceId);
		baseMesh->addVertex(Unigine::vec3(0, index++, 0), surfaceId);
	}

	int tIndex = 0;
	for (int i = 0; i < 32768; i++)
	{
		baseMesh->addTIndex(tIndex, surfaceTarget);
		tIndex++;
		baseMesh->addTIndex(tIndex, surfaceTarget);
		tIndex++;
		baseMesh->addTIndex(tIndex, surfaceTarget);
		tIndex++;
	}

	int ret = baseMesh->createIndices(surfaceId);
	baseMesh->createBounds();
	baseMesh->createNormals();
	baseMesh->createIntersection();
	baseMesh->createTangents();

	staticMesh = Unigine::ObjectMeshStatic::create("Static Base Mesh", 0);
	staticMesh->setMesh(baseMesh);
	Unigine::mat4 staticMeshTForm;
	staticMeshTForm.setIdentity();
	staticMesh->setWorldTransform(staticMeshTForm);
	staticMesh->setMaterial("mesh_base", "*");
	staticMesh->setProperty("surface_base", "*");

	Unigine::MaterialPtr meshMaterial = staticMesh->getMaterial(0);
	meshMaterial->setTwoSided(1);

I've switched out the mesh type with every option I can find in the c++ API:

	dynamicMesh = Unigine::ObjectMeshDynamic::create(1);
	dynamicMesh->setMesh(baseMesh);
	Unigine::mat4 dynamicMeshTForm;
	dynamicMeshTForm.setIdentity();
	dynamicMesh->setWorldTransform(dynamicMeshTForm);
	dynamicMesh->setMaterial("mesh_base", "*");
	dynamicMesh->setProperty("surface_base", "*");

along with alternate versions of the create/createMesh functions. Saving out a mesh to a fbx file and loading it into Unigine with the fbx importer seems to have no problem displaying a large mesh. 

 

Any thoughts?

Link to comment

Saving out the mesh (dynamicMesh->saveMesh("depthMesh.mesh") offered a clue. When I load it in the unigine mesh viewer, seems to do just fine viewing the mesh. Seems like I'm not setting a flag correctly somewhere. 

Link to comment
×
×
  • Create New...