Jump to content
  • Advertisement
Sign in to follow this  
wizardpc

Copying vertex buffers,........need help

This topic is 4874 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 was wondering if there was anyway to copy a vertex buffer into another one. i have a vertex and index buffer for my terrain. I want to make it into a mesh. was wondering if there is some way to copy the buffer into the mesh

Share this post


Link to post
Share on other sites
Advertisement
You can do a direct memcpy:



LPDIRECT3DVERTEXBUFFER9 vb;
LPDIRECT3DINDEXBUFFER9 ib;

// Lock everything
void* pVbVerts;
void* pIbInds;
void* pMeshVbVerts;
void* pMeshIbInds;

Mesh->LockVertexBuffer();
Mesh->LockIndexBuffer();
vb->Lock();
ib->Lock();

memcpy(); // vertices from one buffer to the other
memcpy(); // indices from one buffer to the other

Mesh->UnlockVertexBuffer();
Mesh->UnlockIndexBuffer();
vb->Unlock();
ib->Unlock();




This is pseudo, have to fill in the parameters yourself.

Hope this helps.

ace

Share this post


Link to post
Share on other sites
ok i tried to do it but it didn't work. here is what i wrote can you see if i did anyting wrong

HRESULT hr = 0;

// _vb = previously stored vertex buffer
// _ib = Previously stored Index buffer




hr = D3DXCreateMeshFVF(
_numTriangles,
_numVertices,
D3DXMESH_MANAGED,
TerrainVertex::FVF,
device,
&m);



void* pVbVerts;
void* pIbInds;


TerrainVertex* v =0;

m->LockVertexBuffer(0, (void**)&v);
_vb->Lock(0,0,(void**)&pVbVerts,0);
memcpy(&v,pVbVerts,sizeof(_vb) );
_vb->Unlock();
m->UnlockVertexBuffer();


//index buffer

WORD* indices = 0;
m->LockIndexBuffer(0, (void**)&indices);
_ib->Lock(0,0,(void**)&pIbInds,0);

memcpy(&indices, pIbInds, sizeof(_ib) );
_ib->Unlock();


m->UnlockIndexBuffer();




is there anymore to making a id3dxmesh then this?

Share this post


Link to post
Share on other sites
With this you are supposed to know how many you are copying, so when you memcpy() the last parameter is the size to copy, so its value is n*sizeof(vertex). Same applies for the index buffer copy.

ace

Share this post


Link to post
Share on other sites
it seems im having trouble still. do i need a for loop to memcpy each vertex, or does it memcopy copy the entire buffer .

thanks for the help so far

Share this post


Link to post
Share on other sites
The only other thing i can think of is that i always specifiy the size of everything, the locks and unlocks and the memcpy. Make sure that the FVF of everything involved is the same. Make sure that the size of the mesh you are creating is the exact size needed.

What is the unexpected output here?

Post your entire code inside source markers and i'll have another look.

ace

Share this post


Link to post
Share on other sites
sorry first time posting here, don't know what source makers is but here is my code. i made a function that takes a id3dxmesh* and should create the mesh using it.

in my main gameloop i have.
i created a class Terrain.


//main.cpp
ID3DXMesh* tMesh = 0;
Terrain *TheTerrain = 0;


//Terrain.h
class Terrain
{
public:
Terrain(
IDirect3DDevice9* device,
std::string heightmapFileName,
int numVertsPerRow,
int numVertsPerCol,
int cellSpacing, // space between cells
float heightScale);

~Terrain();

int getHeightmapEntry(int row, int col);
void setHeightmapEntry(int row, int col, int value);

float getHeight(float x, float z);

bool loadTexture(std::string fileName);
bool genTexture(D3DXVECTOR3* directionToLight);
bool draw(IDirect3DDevice9* device, D3DXMATRIX* world, bool drawTris);
bool mesh(IDirect3DDevice9* device, ID3DXMesh* ) ;

cWorldPosition m_Pos;
BOOL Move(float XPos, float YPos, float ZPos);

IDirect3DTexture9* _tex;
private:
IDirect3DDevice9* _device;
// IDirect3DTexture9* _tex;
IDirect3DVertexBuffer9* _vb;
IDirect3DIndexBuffer9* _ib;

int _numVertsPerRow;
int _numVertsPerCol;
int _cellSpacing;

int _numCellsPerRow;
int _numCellsPerCol;
int _width;
int _depth;
int _numVertices;
int _numTriangles;

float _heightScale;

std::vector<int> _heightmap;

// helper methods
bool readRawFile(std::string fileName);
bool computeVertices();
bool computeIndices();
bool lightTerrain(D3DXVECTOR3* directionToLight);
float computeShade(int cellRow, int cellCol, D3DXVECTOR3* directionToLight);

/* struct TerrainVertex
{
TerrainVertex(){}
TerrainVertex(float x, float y, float z, float u, float v)
{
_x = x; _y = y; _z = z; _u = u; _v = v;
}
float _x, _y, _z;
float _u, _v;

static const DWORD FVF;
};*/
};


ok now im only going to post the function that is from terrain.cpp

bool mesh(IDirect3DDevice9* device, ID3DXMesh* )




HRESULT hr = 0;

if(FAILED(hr))
{
::MessageBox(0, "D3DXCreateMeshFVF() - FAILED", 0, 0);
return false;
}

hr = D3DXCreateMeshFVF(
_numTriangles,
_numVertices,
D3DXMESH_MANAGED,
TerrainVertex::FVF,
device,
&m);



void* pVbVerts;
void* pIbInds;


TerrainVertex* v =0;


m->LockVertexBuffer(0, (void**)&v);
_vb->Lock(0,0,(void**)&pVbVerts,0);
memcpy(&v,pVbVerts,sizeof(_vb) );
_vb->Unlock();
m->UnlockVertexBuffer();

//index buffer

WORD* indices = 0;
m->LockIndexBuffer(0, (void**)&indices);

_ib->Lock(0,0,(void**)&pIbInds,0);

memcpy(&indices, pIbInds, sizeof(_ib) );
_ib->Unlock();

m->UnlockIndexBuffer();

//end of function


Do i have to do anything else to create the mesh. Remember im not texturing it.

and thanks for all your help ace.



Share this post


Link to post
Share on other sites
Also is there a max number of verticies a mesh can have?

so far my vertex buffer has 16384
and my number of faces is 32258

can id3dxmesh hold all that information?

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!