Vertex Array and Indexes

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.
andreasky
Posts: 30
Joined: Tue Jun 09, 2009 2:36 pm

Vertex Array and Indexes

Post by andreasky »

Hi,

I need to access the verices and indexes list of a mesh. Please, could you explain me how to do that?

Thank you!

Bye-bye,

Andreasky
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

This is all covered in the documentation. The following sections are relevant to your question.
  1. IMesh
  2. IMeshBuffer
  3. CMeshBuffer<T>
  4. S3DVertex
If you have a specific problem, let us know.

Travis
andreasky
Posts: 30
Joined: Tue Jun 09, 2009 2:36 pm

Post by andreasky »

I would like to get vertices list and indexes list of a simple 3D model.
For example, I import a cube mesh based on triangles. Therefore we have 12 triangles (6 faces x 2 triangles each face).

How to get vertices and indexes number? In a cube there are 8 vertices and 36 indexes (3 indexes x 12 triangles), aren't there? Which functions should I call?

And then, how to build the index and vertex list?

Thank you!
lulzfish
Posts: 34
Joined: Sat Aug 15, 2009 8:19 pm

Post by lulzfish »

  1. IMesh
  2. IMeshBuffer
Just do what I did:
Bookmark http://irrlicht.sourceforge.net/docu/index.html and flip through there.
Is the somber dream of the Grim Reaper a shade Darker Than Black?
vins
Posts: 51
Joined: Mon Aug 16, 2004 6:14 pm
Location: Sofia, Bulgaria
Contact:

Post by vins »

You should also look at http://irrlicht.sourceforge.net/docu/cl ... array.html

The MeshBuffer have to public atributes: Vertices and Indices; They are arrays; You could get the number of Verticies with something like: MeshBuffer.Vertices.size(); and you can add a new vertex with MeshBuffer.Vertices.push_back(...);
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

direct access to Vertices and Indices is only possible if you have an SMesh or some other public implementation of the IMesh interface. Otherwise you have to go via the getVertexCount() and getIndexCount() methods, and use getVertices() and getIndices(). Both return raw pointers to the array data, which may be mapped into an array of course.
andreasky
Posts: 30
Joined: Tue Jun 09, 2009 2:36 pm

Post by andreasky »

I tried to implement a simple example using getVertexCount() and getIndexCount() methods.

I imported a polygonal plane mesh based on triangles in Irrlicht. Here is the code:

====================================
IMesh* mesh = smgr->getMesh("../../media/plane.x");

IMeshSceneNode* node = smgr->addMeshSceneNode( mesh );

if (node)
{
node->setMaterialFlag(EMF_LIGHTING, false);
}


smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));

u32 meshBufferCount=mesh->getMeshBufferCount();

//The maximum vales for getMeshBuffer() is meshBufferCount-1;
IMeshBuffer* meshBuffer=mesh->getMeshBuffer(meshBufferCount-1);

u32 vertexNumber=meshBuffer->getVertexCount();

u32 indexesNumber=meshBuffer->getIndexCount();

ofstream out("vertices.txt");
out<<"The number of vertices is: "<<vertexNumber<<endl<<
"The number of indexes is: "<<indexesNumber<<endl;

out.close();

====================================

When I open "vertices.txt", I got this output:

The number of vertices is: 6
The number of indexes is: 6

I cannot understand why there are 6 vertices instead of 4.

So, I opened "plane.x". Here is its code:

====================================
xof 0303txt 0032

Material lambert1 {
0.400000; 0.400000; 0.400000; 1.000000;;
0.000000;
0.000000; 0.000000; 0.000000;;
0.000000; 0.000000; 0.000000;;
}
Material blinn1 {
0.000000; 0.000000; 0.800000; 1.000000;;
0.000000;
0.250000; 0.250000; 0.250000;;
0.000000; 0.000000; 0.000000;;
}
Frame pPlane1 {
FrameTransformMatrix {
1.000000,0.000000,-0.000000,0.000000,0.000000,1.000000,-0.000000,0.000000,-0.000000,-0.000000,1.000000,0.000000,0.000000,0.000000,-0.000000,1.000000;;
}
Mesh pPlaneShape1 {
6;
-5.000000; -0.000000; -5.000000;,
5.000000; -0.000000; -5.000000;,
-5.000000; 0.000000; 5.000000;,
-5.000000; 0.000000; 5.000000;,
5.000000; -0.000000; -5.000000;,
5.000000; 0.000000; 5.000000;;
2;
3;2,1,0;,
3;5,4,3;;
MeshMaterialList {
1;
2;
0,
0;
{lambert1}
}
VertexDuplicationIndices {
6;
4;
0,
1,
2,
2,
1,
5;
}
}
}

====================================

It seems that there are 2 vertices drawn two times. However in Maya, the 3D software I use, there are only 4 vertices. I use "cvxporter" as ".x" exporter from Maya.

Is it likely to be a cvxporter related issue?

Thank you.
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

If it's just a plane, sometimes either the modeler or Irrlicht will split the polygons to have their own vertices, depending on certain conditions. So since there are 2 triangles and each one is given a unique vertex, that gives you 6. To combine them to only have 4 vertices there is a function in IMeshManipulator:

Code: Select all

mesh = smgr->getMeshManipulator()->createMeshWelded(mesh);
andreasky
Posts: 30
Joined: Tue Jun 09, 2009 2:36 pm

Post by andreasky »

I tried with a cube. If the faces are quads I get 24 vertices and 36 indexes. If I make the faces traingles, I get 36 vertices and 36 indexes.

Do you know why?
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

I answered that in my post above.
andreasky
Posts: 30
Joined: Tue Jun 09, 2009 2:36 pm

Post by andreasky »

I tried to use "mesh = smgr->getMeshManipulator()->createMeshWelded(mesh);"

Here is the code:
=============================
IMesh* mesh = smgr->getMesh("../../media/cube.x");

IMeshSceneNode* node = smgr->addMeshSceneNode( mesh );

if (node)
{
node->setMaterialFlag(EMF_LIGHTING, false);
}


smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));

mesh = smgr->getMeshManipulator()->createMeshWelded(mesh);

u32 meshBufferCount=mesh->getMeshBufferCount();

//The maximum vales for getMeshBuffer() is meshBufferCount-1
IMeshBuffer* meshBuffer=mesh->getMeshBuffer(meshBufferCount-1);



u32 vertexNumber=meshBuffer->getVertexCount();

u32 indexesNumber=meshBuffer->getIndexCount();

ofstream out("vertices.txt");
out<<"The number of mesh buffer is: "<<meshBufferCount<<endl<<
"The number of vertices is: "<<vertexNumber<<endl<<
"The number of indexes is: "<<indexesNumber<<endl;

out.close();


=============================

I tried to load a cube. However I found no difference:either using "createMeshWelded" or not I always get 24 vertices and 36 indexes.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

The weld method has a pending bug, so it might not weld anything quite often. The thing is that e.g. the 3ds loader does split nodes unconditionally. So all vertices are duplicated for each face, resulting in 6*4 vertices and 6*6 indices. If you use a different model format, this could be more optimized.
andreasky
Posts: 30
Joined: Tue Jun 09, 2009 2:36 pm

Post by andreasky »

I use Maya as 3D modelling tool and then export the mesh into ".x" format via cvxporter.

I was wondering whether duplicated vertices may cause any memory problem when you store them in a vertex array. Vertex arrays are used to store each vertex only one time, aren't they? So I cannot understand the benefit (in terms of memory) of saving multiple copies of some vertices in an array. In this way you can waste memory.
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

The memory cost is negligible, with each vertex only being 4 bytes(?). Even a thousand duplicates will only add 4k of memory.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

FYI, each verex will be at least 36 bytes on most 32-bit architectures... sizeof (video::S3DVertex) is the same as sizeof(f32) * 3 + sizeof (f32) * 3 + sizeof (u32) + sizeof (f32) * 2, which is 12 + 12 + 4 + 8.

Travis
Post Reply