# Problems setting Vertex and Index buffers to ID3DXMESH

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

## Recommended Posts

Hi all, hope somebody can help me out! I'm putting together a terrain generation from Perlin Maps engine in c++/dx, and have hit upon some issues when converting my generated terrain to a mesh. Originally, I created an Index and a Vertex buffer for my terrain, and filled them with the relevant data, this rendered with no problems. I then decided to put my buffers into a D3DXMESH (sensibly, I felt so I could utilise some of the built in functions of D3DXMESH). I setup the mesh, then proceeded to lock both buffers and memcpy in the relevant data. This is where my issue arises. Currently, when I run my program I get some strange results. The first time I run it, I get a few polygons rendered at obscure angles and positions. If I then close the program and reopen it straight away, I see a large portion of my terrain, but with plenty of holes and obscurely placed polys. I've got a feeling that my calculations for the number of faces/vertices is incorrect somewhere, but i've spent so long with it today that it's just becoming a garbled mess in my head. So I thought i'd see if anyone else had any ideas :) I've also tried setting the attributes of the mesh, no luck though. Oh, and lastly (although it's kind of obvious) my CUSTOMVERTEX structure holds three floats (x,y,z) and a DWORD (color). Here's the code for the relevant section, I hope somebody can help!
D3DXCreateMeshFVF((int_EnvWidth-1)*(int_EnvHeight-1)*3,int_EnvWidth*int_EnvHeight,D3DXMESH_MANAGED,D3DFVF_XYZ|D3DFVF_DIFFUSE , p_dx_Device, &mesh_terrain);
VOID* p_Index;
VOID* p_Vertices;

// lock the vertex buffer, copy in the vertices, then unlock the buffer
memcpy( p_Vertices, p_dx_VertexBuffer, int_EnvWidth*int_EnvHeight*sizeof(CUSTOMVERTEX));
mesh_terrain->UnlockVertexBuffer();

// Lock the index buffer, copy in the indices, then unlock the buffer
mesh_terrain->LockIndexBuffer( 0, &p_Index );
memcpy( p_Index, p_dx_IndexBuffer, (int_EnvWidth-1)*(int_EnvHeight-1)*6*sizeof(short));
mesh_terrain->UnlockIndexBuffer();



##### Share on other sites
It's always the case isn't it, you post a question to a problem thats been troubling you for days, then you solve it yourself a few hours later ;)

for the sake of anybody with the same issue as me, here's the working code to perform what I was trying to do:

D3DXCreateMeshFVF((int_EnvWidth-1)*(int_EnvHeight-1)*3,int_EnvWidth*int_EnvHeight,D3DXMESH_MANAGED,D3DFVF_XYZ|D3DFVF_DIFFUSE , p_dx_Device, &mesh_terrain);VOID* p_Index;VOID* p_Vertices;VOID* p_aIndex;VOID* p_aVertices;// lock the mesh vertex buffer and the terrains vertex bufer, copy in the vertices, then unlock both buffersmesh_terrain->LockVertexBuffer(D3DLOCK_DISCARD, (void**)&p_Vertices);p_dx_VertexBuffer->Lock(0, 0, (void**)&p_aVertices, D3DLOCK_READONLY);memcpy( p_Vertices, p_aVertices, int_EnvWidth*int_EnvHeight*sizeof(CUSTOMVERTEX));mesh_terrain->UnlockVertexBuffer();p_dx_VertexBuffer->Unlock();// Lock the mesh index buffer and the terrains index buffer, copy in the indices, then unlock both buffersmesh_terrain->LockIndexBuffer( 0, &p_Index );p_dx_IndexBuffer->Lock(0, 0, (void**)&p_aIndex, D3DLOCK_READONLY);memcpy( p_Index, p_aIndex, (int_EnvWidth-1)*(int_EnvHeight-1)*6*sizeof(short));mesh_terrain->UnlockIndexBuffer();p_dx_IndexBuffer->Unlock();

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5

• 13
• 13
• 61
• 14
• 15
• ### Forum Statistics

• Total Topics
632125
• Total Posts
3004252

×