Vertex types

Discuss about anything related to the Irrlicht Engine, or read announcements about any significant features or usage changes.
Post Reply
DataCable
Posts: 5
Joined: Sat Jan 07, 2006 6:51 am

Vertex types

Post by DataCable »

I was just curious as to why S3DVertex, S3DVertex2TCoords and S3dVertexTangents were all made separate structs. Wouldn't it be simpler if S3dVertex was a base class, from which the other two were derived?

For example, in the constructor for CTriangleSelector, there's a switch statement for a MeshBuffer's vertex type that wouldn't be necessary if 2TCoords were derived from S3DVertex, since in either case, it's only concerned with the Pos property of each vertex.

Are there issues which the current implimentation avoids, that I'm not seeing?
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

There was some discussion related to this last week... http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=10987

It could be done, but it would be easier for people to screw up. The problem is that the S3DVertex array would probably be passed around by pointer...

Code: Select all

void setVertexColor(S3DVertex* verticies, u32 count, SColor color)
{
  for (u32 v = 0; v < count; ++v)
    verticies[v]->Color = color;
}
That would be all fine and dandy if you pass an actual S3DVertex array...

Code: Select all

S3DVertex vertexBuffer[8];
// ...
setVertexColor(vertexBuffer, 8, SColor(255, 255, 32, 0)); // all is well
If you passed an array of S3DVertex2TCoords it would wreak havok on the vertex buffer.

Code: Select all

S3DVertex2TCoords vertexBuffer[8];
//...
setVertexColor(vertexBuffer, 8, SColor(255, 255, 32, 0)); // oops!
That is because the size of the two structures are different, and the ++v above would not step past the S3DVertex2TCoords, it would step into the middle of it.

In my post I mentioned two possible solutions, templates and a stride size. The template solution is used in several places in Irrlicht. I don't know why the stride isn't used as frequently. It works quite well and reduces the total code.

Travis
Post Reply