DataStream.Write question

Started by
0 comments, last by ta0soft 12 years, 2 months ago
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]
Advertisement
Nevermind I figured it out, I was using int to store indices and SlimDX was expecting short :D

This topic is closed to new replies.

Advertisement