Search code examples
c#xnaxna-4.0

C# XNA 4.0: How to get the tangent and binormal of the model?


There's this code snippet I have that I used to try to get those:

VertexBuffer vtxBuffer = new VertexBuffer(graphics.GraphicsDevice, typeof(VertexPositionNormalTextureTangentBinormal), meshPart.VertexBuffer.VertexCount, BufferUsage.WriteOnly);
VertexPositionNormalTextureTangentBinormal[] data = new VertexPositionNormalTextureTangentBinormal[meshPart.VertexBuffer.VertexCount];
meshPart.VertexBuffer.GetData<VertexPositionNormalTextureTangentBinormal>(data);

However, I ended up with the error:

'The array is not the correct size for the amount of data requested.'

So, I tried to debug the problem and did this at one point:

VertexBuffer vtxBuffer = new VertexBuffer(graphics.GraphicsDevice, typeof(VertexPositionNormalTexture), meshPart.VertexBuffer.VertexCount, BufferUsage.WriteOnly);
VertexPositionNormalTexture[] data = new VertexPositionNormalTexture[meshPart.VertexBuffer.VertexCount];
meshPart.VertexBuffer.GetData<VertexPositionNormalTexture>(data);

I replaced VertexPositionNormalTextureTangentBinormal with VertexPositionNormalTexture, and viola! I didn't get the same error! But I did get:

'The current vertex declaration does not include all the elements required by the current vertex shader. Tangent0 is missing.'


I'm not sure if this is a model problem or .FBX pipeline problem or... code problem?

  • If this is a model problem, I'm not sure how to get Blender to export the tangents and binormals, a proper extension of sorts?
  • If this is a pipeline problem, I'd like to see if there is pipeline code that solves this.
  • If this is a code problem, I'd like... a solution of some sort.

Either way, I'd like to know if it is at least one of these problems above, thanks!


I'd re-exported an FBX model with Tangent Space on, and even again with a different Blender extension (Better FBX Exporter), but it still didn't work in getting information for the objects of struct VertexPositionNormalTextureTangentBinormal as the error still ended up as:

'The array is not the correct size for the amount of data requested.'

It can most likely be a model or pipeline problem... though with pipeline I'm a bit confused as VertexElementUsage.Tangent and VertexElementUsage.Binormal exist, though it can be entirely possible that those are just there for... other pipelines.


Solution

  • Check the model, go to Content Processor, and set Generate Tangent Frames to True.

    I can't believe myself... damn it...