Sign in to follow this  
ta0soft

DataStream.Write question

Recommended Posts

ta0soft    127
I'm trying to convert my C++/D3D9 code to C#/SlimDX but I keep getting the error "Attempted to read past the end of the stream" when I build the mesh. Am I writing to the DataStream incorrectly?

[source]
// C++
if (FAILED(D3DXCreateMeshFVF((DWORD)meshVertices.size() / 3, (DWORD)meshVertices.size(), D3DXMESH_MANAGED, FVF_MESHVERTEX, p_Device, &p_Mesh))) return false;

void* vertices;
if (FAILED(p_Mesh->LockVertexBuffer(D3DLOCK_NOSYSLOCK, (void**)&vertices))) return false;
memcpy(vertices, &(meshVertices[0]), meshVertices.size() * sizeof(MeshVertex));
if (FAILED(p_Mesh->UnlockVertexBuffer())) return false;
// Fill the index buffer

void* indices;
if (FAILED(p_Mesh->LockIndexBuffer(D3DLOCK_NOSYSLOCK, (void**)&indices))) return false;
memcpy(indices, &(meshIndices[0]), meshIndices.size() * sizeof(WORD));
if (FAILED(p_Mesh->UnlockIndexBuffer())) return false;

DWORD* attributes;
if (FAILED(p_Mesh->LockAttributeBuffer(D3DLOCK_NOSYSLOCK, (DWORD**)&attributes))) return false;
memcpy(attributes, &(meshAttributes[0]), meshAttributes.size() * sizeof(DWORD));
if (FAILED(p_Mesh->UnlockAttributeBuffer())) return false;
[/source]

[source]
// C#
p_Mesh = new Mesh(p_Direct3D9.Device, meshVertices.Count / 3, meshVertices.Count, MeshFlags.Managed, MeshVertex.Format);

DataStream dataStream = p_Mesh.LockVertexBuffer(LockFlags.NoSystemLock);
dataStream.WriteRange<MeshVertex>(meshVertices.ToArray());
if (p_Mesh.UnlockVertexBuffer().IsFailure) return false;

dataStream = p_Mesh.LockIndexBuffer(LockFlags.NoSystemLock);
dataStream.WriteRange(meshIndices.ToArray());
if (p_Mesh.UnlockIndexBuffer().IsFailure) return false;

dataStream = p_Mesh.LockAttributeBuffer(LockFlags.NoSystemLock);
dataStream.WriteRange(meshAttributes.ToArray());
if (p_Mesh.UnlockAttributeBuffer().IsFailure) return false;
[/source]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this