Copying vertex buffers,........need help
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
You can do a direct memcpy:
This is pseudo, have to fill in the parameters yourself.
Hope this helps.
ace
LPDIRECT3DVERTEXBUFFER9 vb;LPDIRECT3DINDEXBUFFER9 ib;// Lock everythingvoid* pVbVerts;void* pIbInds;void* pMeshVbVerts;void* pMeshIbInds;Mesh->LockVertexBuffer();Mesh->LockIndexBuffer();vb->Lock();ib->Lock();memcpy(); // vertices from one buffer to the othermemcpy(); // indices from one buffer to the otherMesh->UnlockVertexBuffer();Mesh->UnlockIndexBuffer();vb->Unlock();ib->Unlock();
This is pseudo, have to fill in the parameters yourself.
Hope this helps.
ace
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?
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?
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
ace
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
thanks for the help so far
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
What is the unexpected output here?
Post your entire code inside source markers and i'll have another look.
ace
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement