How to make Terrain Into a mesh? ManualObject questions

Problems building or running the engine, queries about how to use features etc.
Post Reply
User avatar
KungFooMasta
OGRE Contributor
OGRE Contributor
Posts: 2087
Joined: Thu Mar 03, 2005 7:11 am
Location: WA, USA
x 16
Contact:

How to make Terrain Into a mesh? ManualObject questions

Post by KungFooMasta »

I'm trying to get the ETM created Terrain and get it's vertices and indices to pass to OgreOpcode for a collision object. I'm using a ManualObject to try and recreate the terrain, which would confirm that I'm getting the data correctly.

Getting the Vertices of the terrain:

Code: Select all

Ogre::Vector3 offset = mTerrainInfo->getOffset(); 
		Ogre::Vector3 scale = mTerrainInfo->getScaling(); 

		size_t tWidth = mTerrainInfo->getWidth();
		size_t tHeight = mTerrainInfo->getHeight();

		Ogre::ManualObject* tManualObject = new ManualObject("terrainMesh"); 
		tManualObject->estimateVertexCount(tWidth * tHeight); 
		tManualObject->begin("BaseWhiteNoLighting", RenderOperation::OT_TRIANGLE_LIST);

		// Create vertices 
		std::vector<Ogre::Vector3> vertices; 
		for(size_t h = 0; h < tHeight; h++) 
		{ 
			for(size_t w = 0; w < tWidth; w++) 
			{  
				Ogre::Vector3 v3(
						offset.x + w * scale.x,
						offset.y + mTerrainInfo->at(w,h) * scale.y,
						offset.z + h * scale.z
					);
				tManualObject->position(v3);
				vertices.push_back(v3);
			} 
		}

		tManualObject->end();
		mSceneManager->getRootSceneNode()->attachObject(tManualObject);
Image

From this picture the vertices look correct. Its the indices that I give me a hard time.

I have searched for other threads and not found much on indices. Part of the problem is that I don't understand how they work! :(

Code: Select all

Ogre::ManualObject::index(Ogre::uint16 idx)
:?: What is an index? I have read the comments for the function, but I still do not know. I think it's just some way to write lines between points. (Or maybe indices are required in sets of 3, and define a triangle? Which leads to next question..)

:?: How can three idx (uint16) values be used to make a triangle? For example, what does index(0), index(5), and index(10) do for me? (Is this related to the list of Vertices, or the order at which I called "position" on the ManualObject? What is the relation?)

:?: Should I use the "index" function, or the "triangle" or "quad" function? (pros/cons?)

This is my current code. Sorry for the noob code.

Code: Select all

for( size_t h = 0; h < tHeight - 1; h++) 
		{ 
			for( size_t w = 0; w < tWidth - 1; w++) 
			{ 
				/*
				tManualObject->quad(
					(h+1) + (w+1) * tWidth,
					h + (w+1) * tWidth,
					h + w * tWidth,
					(h+1) + w * tWidth);
				*/
				tManualObject->quad(
					(h * tWidth) + w,
					((h+1) * tWidth) + w,
					(h * tWidth) + (w+1),
					((h+1) * tWidth) + (w+1));
				/*
				tManualObject->index((h+1) + (w+1) * tWidth); 
				tManualObject->index((h+1) + w * tWidth); 
				tManualObject->index(h + w * tWidth); 

				tManualObject->index((h+1)+(w+1) * tWidth); 
				tManualObject->index(h + w * tWidth); 
				tManualObject->index(h + (w+1) * tWidth); 
				/*
				tManualObject->index((h * tHeight) + w); 
				tManualObject->index((h * tHeight) + (w + 1)); 
				tManualObject->index((h + 1) * tHeight + w); 

				tManualObject->index((h + 1) * tHeight + w); 
				tManualObject->index((h * tHeight) + (w + 1)); 
				tManualObject->index((h + 1) * tHeight + (w + 1));
				*/
			} 
		} 
The resulting object looks like a small strip of terrain, with parts folded over itself.

If anybody can provide any information on this please do, I'm trying to get ETM compatible with OgreOpcode, and integrate it into a demo for users.

Thanks for any help,

KungFooMasta

Post Reply