C# -- Problem with an array of structs

241 views
Skip to first unread message

Mark Simpson

unread,
Jun 18, 2015, 8:13:38 PM6/18/15
to flatb...@googlegroups.com
Hi,

I can get the C# Monster sample up and running, but I'm running into a problem when I try to use a table that includes an array of structs, for example:

example IDL file

namespace MyGame;

struct Vec3 {
  x
:float;
  y
:float;
  z
:float;
}

table
Mesh {
  positions
:[Vec3]; // array of structs.. (works fine with single instances rather than an array)
  indices
:[int];  
}

root_type
Mesh;...

class Program
   
{
       
static void Main(string[] args)
       
{
           
var fbb = new FlatBufferBuilder(1);
           
Mesh.StartPositionsVector(fbb, 9);
            fbb
.AddOffset(Vec3.CreateVec3(fbb, -1f, +2f, -3f));
            fbb
.AddOffset(Vec3.CreateVec3(fbb, +4f, -5f, +6f));
            fbb
.AddOffset(Vec3.CreateVec3(fbb, -7f, +8f, -9f));
           
int positionsVector = fbb.EndVector();

           
var indicesVector = Mesh.CreateIndicesVector(fbb, new int[] { 1, 2, 3 });

           
int wm = Mesh.CreateMesh(fbb, positionsVector, indicesVector);
           
Mesh.FinishMeshBuffer(fbb, wm);
           
           
var readMesh = Mesh.GetRootAsMesh(fbb.DataBuffer);
           
Vec3 scratch = new Vec3();

           
for (int i = 0; i < readMesh.PositionsLength(); i++)
           
{
               
var v = readMesh.Positions(scratch, i);
               
Console.WriteLine("{0},{1},{2}", v.X().ToString("F2"), v.Y().ToString("F2"), v.Z().ToString("F2"));
           
}

           
var indicesLength = readMesh.IndicesLength();
           
for (int i = 0; i < indicesLength; i++)
           
{
               
Console.WriteLine(readMesh.Indices(i));
           
}
       
}
   
}

Project is attached, too (VS2013).

I've tried 1.1.0 as well as master (as of about ten minutes ago). What am I doing wrong? Any help is appreciated, thanks.
ProtoOhDear.zip

Mark Simpson

unread,
Jun 19, 2015, 9:49:16 AM6/19/15
to flatb...@googlegroups.com
Sorry, totally forgot to say that the output is not what I was expecting (can't check right now, as I'm at work). The indices are fine, but the positions print something like:

0, 2, 3
4, -5, 0
...

I'll post a proper update with the actual output when I'm home again.

Wouter van Oortmerssen

unread,
Jun 19, 2015, 12:56:45 PM6/19/15
to Mark Simpson, flatb...@googlegroups.com
The problem is likely your calls to fbb.AddOffset. You're interleaving struct data with offsets to themselves. Structs are stored in the vector inline, so don't need those offsets.

Also, you're specifying 9 elements but only adding 3.

--
You received this message because you are subscribed to the Google Groups "FlatBuffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flatbuffers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mark Simpson

unread,
Jun 20, 2015, 2:33:54 PM6/20/15
to flatb...@googlegroups.com, mjsi...@gmail.com
Hi Wouter,

Thanks for the reply. I've managed to get something working. Is there a more idiomatic way to write my Vec3 values to the buffer, or is this (i.e. explicit AddFloat() calls rather than any Vec3 usage) what you'd expect to see?

            var fbb = new FlatBufferBuilder(1);

           
Mesh.StartPositionsVector(fbb, 3);
           
            fbb
.AddFloat(-9);
            fbb
.AddFloat(+8);
            fbb
.AddFloat(-7);
           
            fbb
.AddFloat(+6);
            fbb
.AddFloat(-5);
            fbb
.AddFloat(+4);

            fbb
.AddFloat(-3);
            fbb
.AddFloat(+2);
            fbb
.AddFloat(-1);

           
int positionsVector = fbb.EndVector();

           
var indicesVector = Mesh.CreateIndicesVector(fbb, new int[] { 1, 2, 3 });

           
int wm = Mesh.CreateMesh(fbb, positionsVector, indicesVector);

           
Mesh.FinishMeshBuffer(fbb, wm);

Wouter van Oortmerssen

unread,
Jun 22, 2015, 12:17:14 PM6/22/15
to Mark Simpson, flatb...@googlegroups.com
No, you should call CreateVec3. You just shouldn't call AddOffset on the result.
Reply all
Reply to author
Forward
0 new messages