IrrNoob wrote:Ok, will do
Awesome. Finally we're making some progress. Comments on your response...
IrrNoob wrote:
I thought this would take 'v', which i believe is a pointer to the vertices in the mesh buffer, and convert its value to S3DVertex*, while the '[0]' is to point to the first vertex in the buffer.
You are exactly right what this does. It
casts the pointer
v to the type
video::S3DVertex*, which is essentially an array of vertices. The
[0] part indexes into the array to get the 0th element.
Sadly, in the grand scheme of things, this has no net effect. It accesses the first vertex, but it does nothing with it. To be useful you could access or modify the vertex position, like so...
Code: Select all
// read the position
const core::vector3df pos = ((video::S3DVertex*)v)[0].Pos;
// write the position
((video::S3DVertex*)v)[0].Pos = pos;
IrrNoob wrote:
This code was introduced by myself :oops: to convert the value of 'v' to int, because the compiler was saying I couldn't put S3DVertex v into the last expression:
If
v is a pointer, then
vb is the (possibly truncated) pointer value as an integer. It is definitely not the number of vertices in the vertex buffer (there is a
IMeshBuffer method that will tell you this). I don't know why you'd write it, but lets move on...
IrrNoob wrote:
This is supposed to take the value of that pointer to the vertices in the vertex buffer, and cast it to a new SPEVertex with that data and assign the value to SPEVertex PVertex.
So you think that this line casts the first vertex
vb to a new
SPEVertex and assigns that new vertex to
pVertex. Well, it doesn't do that.
That line allocates an array of
vb default constructed
SPEVertex objects. If
vb is 1000, you will get 1000 vertex objects. It then assigns
pVertex to point to the start of the array.
I think you wanted something more like this (which is still wrong).
Code: Select all
pVertex = new SPEVertex(*vb); // assuming vb is a pointer to S3DVertex
That says allocate a new
SPEVertex object and construct it from the
S3DVertex object. This would only work if there is some sort of conversion from
S3DVertex to
SPEVertex, but there isn't one, so that would never work.
What you really want is an array of
SPEVertex that has the same number of elements as the mesh buffer has. You can ask the mesh buffer how many vertices it has with
mb->getVertexCount(), right? And creating an array of vertices is pretty easy. You declare a
core::array<video::SPEVertex> and then resize it. So this gets you an array of the right number of vertices.
Code: Select all
const u32 vertex_count = mb->getVertexCount();
core::array<SPEVertex> vertices;
vertices.set_used(vertex_count);
Now you just have to copy the vertex data. That is pretty easy if you loop through the vertices. The only trick is that you need to be careful about how you index into the Irrlicht vertex array. You need to know the vertex type or the vertex stride to find each vertex after the first...
Code: Select all
u32 stride = 0;
switch (mb->getVertexType())
{
case video::EVT_STANDARD:
stride = sizeof (video::S3DVertex); break;
case video::EVT_2TCOORDS:
stride = sizeof (video::S3DVertex2TCoords); break;
case video::EVT_TANGENTS:
stride = sizeof (video::S3DVertexTangents); break;
}
const u8* pv = (const u8*)mb->getVertices();
for (u32 i = 0; i < vertex_count; ++i)
{
// this cast gets the address of the ith vertex
video::S3DVertex* ppv = (video::S3DVertex*)(pv + i * stride);
vertices [i].x = ppv->Pos.x;
vertices [i].y = ppv->Pos.y;
vertices [i].z = ppv->Pos.z;
}
So that gets us a copy of the vertex data.