So basically I’m struggling to get a generic implementation of a Vertex type. I’d like to be able to import models with several different Vertex types (PositionColor, PositionNormalTexture, etc) and then pull their vertex data and refer to it all in the same way (throw them into a list, call setdata on the VertexBuffers, etc). My initial thought with this was to create a Vertex struct that was simply an array of bytes and then load them all into that but I’m running into problems where it says it can’t be marshalled.
Does anybody have experience with create a base representation for Vertices? Or does anyone have advice on how to do this? Thanks so much!
Ummm, are you looking for something like this: http://pastebin.com/XiWF6Zwq ?
MonoGame uses Marshal.SizeOf(T) in VertexBuffer.SetDataInternal(…) to determine the size of a vertex. Unforunately Marshal.SizeOf does not work on generically constructed vertices.
I know this was possible in XNA, so it should probably be an issue on GitHub. They must have used a different method to get the vertex size.
If you don’t mind changing some MonoGame code, there’s a quick fix to get this working. Just get the size of your vertex struct and pass it as an additional parameter to the VertexBuffer.SetData(…) functions.
Then use your vertex size parameter instead of Marshal.SizeOf(typeof(T)) in VertexBuffer.SetDataInternal(…).