Creating mesh from VertexBuffer and IndexBuffer - got some problem

Hello

In my code, I make a terrain from an image. After it is doone, I would like to save it like a Mesh. But there is some problem with my code and I always get some problem, when calling terrainMesh.SetData() with VertexBuffer and also with IndexBuffer.

If somebody could help me, please. Here is the code:

// Create the vertex- and indexbuffer and set their data

vb =newVertexBuffer(typeof(Vertex),numvertices,device,Usage.None,Vertex.Format,Pool.Default);

vb.SetData(verts,0,0);

ib =newIndexBuffer(typeof(int),numindices,device,0,0);

ib.SetData(ind,0,0);

// Create the vertexdeclaration

VertexElement[] v =newVertexElement[] {newVertexElement(0,0,DeclarationType.Float3,DeclarationMethod.Default,DeclarationUsage.Position,0),

newVertexElement(0,12,DeclarationType.Float3,DeclarationMethod.Default,DeclarationUsage.Normal,0),

newVertexElement(0,24,DeclarationType.Float2,DeclarationMethod.Default,DeclarationUsage.TextureCoordinate,0),

newVertexElement(0,32,DeclarationType.Float4,DeclarationMethod.Default,DeclarationUsage.TextureCoordinate,1),

VertexElement.VertexDeclarationEnd};

decl =newVertexDeclaration(device,v);

AttributeRange attributeRange =newAttributeRange();

attributeRange.AttributeId = 0;

attributeRange.FaceStart = 0;

attributeRange.FaceCount = numindices / 3;

attributeRange.VertexStart = 0;

attributeRange.VertexCount = numvertices;

Mesh terrainMesh =newMesh(numindices/3,numvertices,MeshFlags.SystemMemory,v,device);

terrainMesh.VertexBuffer.SetData(vb, 0, 0);

terrainMesh.IndexBuffer.SetData(ib, 0, LockFlags.None);

terrainMesh.SetAttributeTable(newAttributeRange[] { attributeRange });

ExtendedMaterial[] materials =newExtendedMaterial[2];

//materials

int [] adj =newint [terrainMesh.NumberFaces*3];

terrainMesh.GenerateAdjacency(0.01F, adj);

this.mesh.Save(fileName, adj, materials, XFileFormat.Binary);

terrainMesh.Save("terrain", adj, materials, XFileFormat.Binary);

[5451 byte] By [azreon] at [2007-12-22]
# 1

what error do u get?

i dont use dx managed so might be little different.

When i load data into a vertex buffer to set it to mesh later, i copy the vertex buffer data into the mesh vertexbuffer

vb.lock
mesh.vb.lock
...copy vertices
mesh.vb.unlock
vb.unlock

Nightmare_BE at 2007-8-30 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: Graphics...
# 2

The program halt when it's reaching the

terrainMesh.VertexBuffer.SetData(vb, 0, 0); or terrainMesh.IndexBuffer.SetData(ib, 0, LockFlags.None);

lines. But when I comment theese out, then no problems, the program works fine, but witout the save mesh function. And I cannot debug because of DirectX.

azreon at 2007-8-30 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: Graphics...
# 3

Maybe u should try the buffer lock / unlock methods.

I never used SetData before, allways using locks. U will find some info/samples of it in the sdk

Nightmare_BE at 2007-8-30 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: Graphics...
# 4

You are trying to fill the mesh's VB and IB directly from another VB and IB. You need to lock the corresponding VB and IB for read first and use the data pointers, or better yet, just use that original data you used to fill your VB and IB in the first place. In fact, you could just create a mesh in the first place and use it to render your terrain. With Optimize, you might even get better performance. This would simplify things a lot and solve your problem at the same time:

First, don't bother creating the VB and IB yourself. Just use the ones created by the mesh.

Second, you can remove the SystemMemory flag from the mesh, since you will be using this mesh to render.

Third, SetData on the mesh's VB and IB the same way you did with yours (passing verts and ind).

Fourth, you can skip creating and setting the attribute table if you use Optimize. Optimizing the mesh might even yield better performance. But you should do this after generating the adjacency.

Fifth, you can use GenerateAdjacencyFromPointReps, passing NULL for the point reps (meaning identity), if your mesh already shares vertices. (note: C# api might differ slightly from my knowledge with this function, but you should be able to find a way to do what I mean)

TexRiddell at 2007-8-30 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: Graphics...
# 5

azreon,

I had better luck doing this by using the SetIndexBufferData and SetVertexBufferData methods on the mesh instead of the SetData methods on the mesh's VertexBuffer and IndexBuffer. SetIndexBufferData and SetVertexBufferData handle all the locking for you so they are easier to use (matter of fact, I never got it to work using the SetData mehods).

So...

terrainMesh.VertexBuffer.SetData(vb, 0, 0);

terrainMesh.IndexBuffer.SetData(ib, 0, LockFlags.None);

becomes

terrainMesh.SetVertexBufferData(verts, LockFlags.None);

terrainMesh.SetIndexBufferData(ind, LockFlags.None);

Hope this helps, good luck.

Stevezilla2000 at 2007-8-30 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: Graphics...