Terrain mesh has 0 indicies but 65536 vertices

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
hach-que
Posts: 33
Joined: Thu Apr 19, 2007 10:11 am
Location: Australia

Terrain mesh has 0 indicies but 65536 vertices

Post by hach-que »

When I get the mesh from a terrain scene node, and I check the mesh buffers, it ends up with a total of 0 indicies in the mesh, and 65536 vertices.

This means I can't convert it to a Newton mesh. The code I'm using to check is shown below. Is there something I'm doing wrong?

Code: Select all

                                // Check for validity
                                test_mbc = mesh->getMeshBufferCount();
                                if (test_mbc <= 0)
                                {
                                        nonlua_debug("Invalid terrain specified for physics object (no mesh buffers).",LVL_WARN);
                                        physType = E_TYPE_IGNORE;
                                        break;
                                }
                                has_indicies = false;
                                for (int i = 0; i < mesh->getMeshBufferCount(); i++)
                                {
                                        mb = mesh->getMeshBuffer(i);
 
                                        if (mb)
                                                if (mb->getIndexCount() > 0)
                                                {
                                                        has_indicies = true;
                                                        break;
                                                }
                                }
                                if (!has_indicies)
                                {
                                        nonlua_debug("Invalid terrain specified for physics object (no indicies).",LVL_WARN);
                                        physType = E_TYPE_IGNORE;
                                        break;
                                }
Image
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: Terrain mesh has 0 indicies but 65536 vertices

Post by randomMesh »

I am using ODE, but the task should be the same.
This works:

Code: Select all

void Terrain::makeTriMeshData()
{
	irr::scene::CDynamicMeshBuffer* buffer = 0;
	buffer = new irr::scene::CDynamicMeshBuffer(irr::video::EVT_2TCOORDS, irr::video::EIT_16BIT);
	terrain->getMeshBufferForLOD(*buffer, 0);

	const irr::u32 vertexCount = buffer->getVertexCount();
	const irr::u32 indexCount = buffer->getIndexCount();

	this->vertices = new dVector3[vertexCount];
	this->indices = new dTriIndex[indexCount];

	const irr::core::vector3df& scale = terrain->getScale();


	irr::video::S3DVertex2TCoords* data = (irr::video::S3DVertex2TCoords*)buffer->getVertexBuffer().getData();

	irr::u32 i;
	for (i = 0; i < vertexCount; i++)
	{
		this->vertices[i][0] = data[i].Pos.X*scale.X;
		this->vertices[i][1] = data[i].Pos.Y*scale.Y;
		this->vertices[i][2] = data[i].Pos.Z*scale.Z;
	}

	irr::u32 j;
	for (j = 0; j < indexCount; ++j)
		this->indices[j] = (dTriIndex)(buffer->getIndexBuffer()[j]);

	buffer->drop();

	this->tdata = dGeomTriMeshDataCreate();
	dGeomTriMeshDataBuildSimple(
		this->tdata, // the dTriMeshDataID to use
		(const dReal*)this->vertices, vertexCount,
		(const dTriIndex*)indices, indexCount
	);
}
hach-que
Posts: 33
Joined: Thu Apr 19, 2007 10:11 am
Location: Australia

Post by hach-que »

Okay, I've updated my code to use getMeshBufferForLOD, but objects still won't collide with my Newton terrain.

Code: Select all

				total_indicies = 0;
				total_vertexes = 0;
				ter_buffer = new irr::scene::CDynamicMeshBuffer(irr::video::EVT_2TCOORDS, irr::video::EIT_16BIT); 
				terrain->getMeshBufferForLOD(*ter_buffer);
				if (ter_buffer == NULL)
				{
					nonlua_debug("Invalid terrain specified for physics object.",LVL_WARN);
					physType = E_TYPE_IGNORE;
					break;
				}
				has_indicies = false;
				if (ter_buffer)
				{
					total_indicies += ter_buffer->getIndexCount();
					total_vertexes += ter_buffer->getVertexCount();
					if (ter_buffer->getIndexCount() > 0)
					{
						has_indicies = true;
						break;
					}
				}
				if (!has_indicies)
				{
					nonlua_debug("Invalid terrain specified for physics object (no indicies, " << total_vertexes << " vertexes).",LVL_WARN);
					physType = E_TYPE_IGNORE;
					break;
				}

				// Setup Roket Physics Body for terrain
				posdesc.setTranslation(irr::core::vector3df(0,0,0));
				posdesc.setRotationDegrees(self()->getRotation());
				//posdesc.setScale(self()->getScale());
				// Setup Roket Physics Body for sphere
				collision = NewtonCreateTreeCollision(physworld);
				NewtonTreeCollisionBeginBuild(collision);

				// loop through, adding triangles.

				if (ter_buffer->getVertexType() == irr::video::EVT_2TCOORDS)
				{
					// Retrieve vertices and indices.
					mb_vertices_2t = (irr::video::S3DVertex2TCoords*)ter_buffer->getVertices(); 
					mb_indices = ter_buffer->getIndices(); 

					// Add each triangle from the mesh to the tree. 
					for (int j = 0; j < ter_buffer->getIndexCount(); j += 3) 
					{ 
						int index_one = mb_indices[j]; 
						int index_two = mb_indices[j + 1]; 
						int index_three = mb_indices[j + 2]; 

						// First vertice. 
						vertarray[0] = mb_vertices_2t[index_one].Pos.X; 
						vertarray[1] = mb_vertices_2t[index_one].Pos.Y; 
						vertarray[2] = mb_vertices_2t[index_one].Pos.Z; 

						// Second vertice. 
						vertarray[3] = mb_vertices_2t[index_two].Pos.X; 
						vertarray[4] = mb_vertices_2t[index_two].Pos.Y; 
						vertarray[5] = mb_vertices_2t[index_two].Pos.Z; 

						// Third vertice. 
						vertarray[6] = mb_vertices_2t[index_three].Pos.X; 
						vertarray[7] = mb_vertices_2t[index_three].Pos.Y; 
						vertarray[8] = mb_vertices_2t[index_three].Pos.Z; 

						// Add the face to the collision tree. 
						NewtonTreeCollisionAddFace(collision, 3, vertarray, 12, 1); 
					}
				}
				else if (ter_buffer->getVertexType() == irr::video::EVT_STANDARD)
				{
					// Retrieve vertices and indices.
					mb_vertices_normal = (irr::video::S3DVertex*)ter_buffer->getVertices(); 
					mb_indices = ter_buffer->getIndices(); 

					// Add each triangle from the mesh to the tree. 
					for (int j = 0; j < ter_buffer->getIndexCount(); j += 3) 
					{ 
						int index_one = mb_indices[j]; 
						int index_two = mb_indices[j + 1]; 
						int index_three = mb_indices[j + 2]; 

						// First vertice. 
						vertarray[0] = mb_vertices_normal[index_one].Pos.X; 
						vertarray[1] = mb_vertices_normal[index_one].Pos.Y; 
						vertarray[2] = mb_vertices_normal[index_one].Pos.Z; 

						// Second vertice. 
						vertarray[3] = mb_vertices_normal[index_two].Pos.X; 
						vertarray[4] = mb_vertices_normal[index_two].Pos.Y; 
						vertarray[5] = mb_vertices_normal[index_two].Pos.Z; 

						// Third vertice. 
						vertarray[6] = mb_vertices_normal[index_three].Pos.X; 
						vertarray[7] = mb_vertices_normal[index_three].Pos.Y; 
						vertarray[8] = mb_vertices_normal[index_three].Pos.Z; 

						// Add the face to the collision tree. 
						NewtonTreeCollisionAddFace(collision, 3, vertarray, 12, 1); 
					}
				}
				else if (ter_buffer->getVertexType() == irr::video::EVT_TANGENTS)
				{
					// Retrieve vertices and indices.
					mb_vertices_tangent = (irr::video::S3DVertexTangents*)ter_buffer->getVertices(); 
					mb_indices = ter_buffer->getIndices(); 

					// Add each triangle from the mesh to the tree. 
					for (int j = 0; j < ter_buffer->getIndexCount(); j += 3) 
					{ 
						int index_one = mb_indices[j]; 
						int index_two = mb_indices[j + 1]; 
						int index_three = mb_indices[j + 2]; 

						// First vertice. 
						vertarray[0] = mb_vertices_tangent[index_one].Pos.X; 
						vertarray[1] = mb_vertices_tangent[index_one].Pos.Y; 
						vertarray[2] = mb_vertices_tangent[index_one].Pos.Z; 

						// Second vertice. 
						vertarray[3] = mb_vertices_tangent[index_two].Pos.X; 
						vertarray[4] = mb_vertices_tangent[index_two].Pos.Y; 
						vertarray[5] = mb_vertices_tangent[index_two].Pos.Z; 

						// Third vertice. 
						vertarray[6] = mb_vertices_tangent[index_three].Pos.X; 
						vertarray[7] = mb_vertices_tangent[index_three].Pos.Y; 
						vertarray[8] = mb_vertices_tangent[index_three].Pos.Z; 

						// Add the face to the collision tree. 
						NewtonTreeCollisionAddFace(collision, 3, vertarray, 12, 1); 
					}
				}

				NewtonTreeCollisionEndBuild(collision, 0);

				physBody = new physics::Roket_PhysicsBody(
								collision,
								physworld,
								E_TYPE_TERRAIN,
								0
								);
				physBody->attachNode(this);
				physBody->setPosition(position);
				self()->setPosition(position);
Image
Post Reply