Strange values when extracting 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
Masterhawk
Posts: 299
Joined: Mon Nov 27, 2006 6:52 pm
Location: GERMANY
Contact:

Strange values when extracting vertices...

Post by Masterhawk »

Hey guys,
I want to extract the vertices of a my3d-mesh to create a newton-collision with these values. The mesh is a flat plane with a shape of an circle. The circle is rendered correctely, but my ouput is strange. So the newton collision is corrupted, too.

Code: Select all

IMeshBuffer* mb = dPortals->getMesh()->getMesh(0)->getMeshBuffer(mbc);
video::S3DVertex* mb_vertices = (irr::video::S3DVertex*)mb->getVertices();
                                     
u16* mb_indices  = mb->getIndices();
                                     
                                     
vector3df cloud [mb->getIndexCount()];
printf("IndexCount: %i\n",mb->getIndexCount());
                                     
int v1i, v2i, v3i;
                                      
// add each triangle from the mesh
for (j=0; j<mb->getIndexCount(); j++)
{
      v1i = mb_indices[j];
                                   
      cloud[j] = vector3df(mb_vertices[v1i].Pos.X,mb_vertices[v1i].Pos.Y,mb_vertices[v1i].Pos.Z);
      
      //Output
      printf("Vertex#%i: X:%f Y:%f Z:%f\n,j,cloud[j].X,cloud[j].Y,cloud[j].Z);
}
collision = NewtonCreateConvexHull (nWorld, mb->getIndexCount(), &cloud[0].X, sizeof (vector3df), NULL);
But something goes wrong with this code snippet because I get a strange output.

Image

As you see there're some NAN's in there. Is this a valid output for a mesh?
If yes, what do I have to do to only get the native vertices-coordinates?
Image
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Your code only works correctly if the vertex type is actually video::S3DVertex. Irrlicht supports several other vertex types [video::S3DVertexTCoords or video::S3DVertexTangents], and you must account for that.

You could fix that with something like this

Code: Select all

// variable length arrays are not supported by all compilers. i'm
// opting to use an actual array instead.
//vector3df cloud [mb->getIndexCount()]; 
core::array<core::vector3df> cloud;
cloud.reallocate(mb->getIndexCount());
                                      
// pointer to the 0th vertex, cast to a byte pointer so we can increment by strides
const u8* mb_verts = (const char*)mb->getVertices();
const u16* mb_indices  = mb->getIndices();
const u32 stride = getVertexPitchFromType(mb->getVertexType());
const u32 idx_cnt = mb->getIndexCount();

for (u32 j = 0; j < idx_cnt; ++j) 
{
      // offset to the vertex in bytes
      const u32 offset = stride * mb_indices[j];

      video::S3DVertex* v = (video::S3DVertex*)(mb_verts + offset);
      cloud.push_back(v->Pos);
      
      //Output 
      printf("Vertex#%i: X:%f Y:%f Z:%f\n,j,cloud[j].X,cloud[j].Y,cloud[j].Z); 
} 
collision = NewtonCreateConvexHull (nWorld, idx_cnt, &cloud[0].X, sizeof (core::vector3df), NULL);
Travis
Masterhawk
Posts: 299
Joined: Mon Nov 27, 2006 6:52 pm
Location: GERMANY
Contact:

Post by Masterhawk »

Thx vitek, I will have a look at this and tell you if it works...thx
Image
Masterhawk
Posts: 299
Joined: Mon Nov 27, 2006 6:52 pm
Location: GERMANY
Contact:

Post by Masterhawk »

I've tested it, but didn't get this piece of code work.

My irrlicht-version doesn't know "getVertexPitchFromType()". I didn't found it in the API(1.3.1) either.
So I've tried to change this line

Code: Select all

const u32 stride = getVertexPitchFromType(mb->getVertexType()); 
to that line

Code: Select all

const u32 stride = mb->getVertexPitch();
But my MeshBuffer doesn't even know about "getVertexPitch". I'm using irrlicht 1.1 and hope that I've just done a stupid mistake.

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

Post by vitek »

Copy the getVertexPitchFromType() function from SVN. I assumed that you were using the SVN version, which you are not.
Masterhawk
Posts: 299
Joined: Mon Nov 27, 2006 6:52 pm
Location: GERMANY
Contact:

Post by Masterhawk »

Thx Travis, now it's working
Image
classawarrior
Posts: 5
Joined: Fri Jan 18, 2008 2:57 am

Post by classawarrior »

vitek wrote:Your code only works correctly if the vertex type is actually video::S3DVertex. Irrlicht supports several other vertex types [video::S3DVertexTCoords or video::S3DVertexTangents], and you must account for that.

You could fix that with something like this

Code: Select all

// variable length arrays are not supported by all compilers. i'm
// opting to use an actual array instead.
//vector3df cloud [mb->getIndexCount()]; 
core::array<core::vector3df> cloud;
cloud.reallocate(mb->getIndexCount());
                                      
// pointer to the 0th vertex, cast to a byte pointer so we can increment by strides
const u8* mb_verts = (const char*)mb->getVertices();
const u16* mb_indices  = mb->getIndices();
const u32 stride = getVertexPitchFromType(mb->getVertexType());
const u32 idx_cnt = mb->getIndexCount();

for (u32 j = 0; j < idx_cnt; ++j) 
{
      // offset to the vertex in bytes
      const u32 offset = stride * mb_indices[j];

      video::S3DVertex* v = (video::S3DVertex*)(mb_verts + offset);
      cloud.push_back(v->Pos);
      
      //Output 
      printf("Vertex#%i: X:%f Y:%f Z:%f\n,j,cloud[j].X,cloud[j].Y,cloud[j].Z); 
} 
collision = NewtonCreateConvexHull (nWorld, idx_cnt, &cloud[0].X, sizeof (core::vector3df), NULL);
Travis

I'm unable to run this example and have also been having trouble getting the vertex data I need.

error C2440: 'initializing' : cannot convert from 'const char *' to 'const irr::u8 *'

It doesnt like the declaration of the pointer used for striding through the vertex list. Can anybody help?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

You should cast to the type you actually use. So put (const u8*) on both sides.
Post Reply