• Advertisement
Sign in to follow this  

[XNA 3.1] Extracting vertex elements using the model content pipeline?

This topic is 2621 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to extract the vertex elements out of a ModelMeshContent class in the model content pipeline.

The problem is that I have to use VertexBufferContent and not VertexBuffer. Here is my vertex element accessor class:


public class VertexDataAccessor
{
private VertexBufferContent[] buffers;
private VertexElement[] elements;

public VertexDataAccessor(VertexBufferContent vertexBuffer, VertexElement[] vertexElements)
{
buffers = new VertexBufferContent[1];
buffers[0] = vertexBuffer;
elements = vertexElements;
}

public VertexDataAccessor(VertexBufferContent[] vertexBuffers, VertexElement[] vertexElements)
{
buffers = vertexBuffers;
elements = vertexElements;
}

public T[] GetData<T>(VertexElementUsage usage) where T : struct
{
bool isElementUsage = false;

VertexElement requestedElement = new VertexElement();

// Check if requested data is available
for (int i = 0; i < elements.Length; ++i)
{
if (elements.VertexElementUsage == usage)
{
// If found, set the element describing the request data
requestedElement = elements;
isElementUsage = true;
break;
}
}

if (!isElementUsage)
{
return null;
}

// Generate the values we need to get the data out of the vertex buffer
int offset = requestedElement.Offset;
int startIndex = 0; // Always copy the complete buffer
int vertexStride = VertexDeclaration.GetVertexStrideSize(elements, requestedElement.Stream); // The Stream defined which vertex buffer contains the data
int elementCount = buffers[requestedElement.Stream].SizeInBytes / vertexStride;

T[] data = new T[elementCount];

// Get the actual data out of the vertex buffer
buffers[requestedElement.Stream].GetData<T>(offset, data, startIndex, elementCount, vertexStride);

return data;
}
}




The problem is that the following property and method:

.SizeInBytes
.GetData<T>()

are not available to me.

How can I extract the data out of the buffer (VertexBufferContent) if this is the case?

Share this post


Link to post
Share on other sites
Advertisement
I think what you're trying to do would be a lot easier in XNA 4.0, since VertexBufferContent now has a VertexDeclarationContent that has a collection of VertexElement. Also, in both 3.1 and 4.0 the VBC has a VertexData property which is just an array of raw bytes - so GetData<T> wouldn't be needed.

But that's besides the point. Whenever I worked with manipulating the vertex data, I never bothered with the VBC - I always went up a level and used the GeometryContent(s) attached to a MeshContent. All the vertex data in a GeometryContent is stored in a VertexContent object, which in turn has a collection of VertexChannels.

E.g. Accessing normal and boneweight data:

GeometryContent geo;

...

VertexChannel<BoneWeightCollection> boneweights = (VertexChannel<BoneWeightCollection>) geo.Vertices.Channels[VertexChannelNames.Weights(0)];

VertexChannel<Vector3> normals = (VertexChannel<Vector3>) geo.Vertices.Channels[VertexChannelNames.Normal(0)]

In the VertexContent object, your position data is stored in its own collection. For any other types of vertex elements you can search for them in the vertex channel via the VertexChannelNames enumeration. It's a win-win, since the vertex channels will have the data you want, and all the information about the element itself (to build up a vertex declaration if you needed it).

I would take a look at the MSDN documentation too:

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.content.pipeline.graphics.geometrycontent%28XNAGameStudio.31%29.aspx

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.content.pipeline.graphics.meshcontent%28XNAGameStudio.31%29.aspx

Hope that helps.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement