today I started working with DirectX(D3D9), everything went fine until I created a Static-mesh class. This class contains methods for generating the buffer, drawing and releasing the buffer.
The problem is in the buffer generation function. If I want to pass in the vertices array as an argument to the function, at draw time the triangle (testing with a triangle) isn't drawn, however, if the vertices are declared within the function (same way as when passed as an argument), the triangle isn't drawn.
The vertex class:
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
class CUSTOMVERTEX
{
public:
float X, Y, Z, RHW;
DWORD Color;
};
The vertex buffer generating function (that does not work):
void StaticMesh::CreateBuffer(CUSTOMVERTEX Vertices[], LPDIRECT3DDEVICE9 d3ddev)
{
// USING FIXED SIZE WHILE TESTING WITH A SINGLE TRIANGLE.
d3ddev->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &Buffer, NULL);
VOID* p;
Buffer->Lock(0, 0, (void**)&p, 0);
memcpy(p, Vertices, sizeof(Vertices));
Buffer->Unlock();
}
The vertex buffer generation function (that does work):
void StaticMesh::CreateBuffer(LPDIRECT3DDEVICE9 d3ddev)
{
CUSTOMVERTEX vertices[3] =
{
{ 400.0f, 62.5f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ 650.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ 150.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
};
// USING FIXED SIZE WHILE TESTING WITH A SINGLE TRIANGLE.
d3ddev->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &Buffer, NULL);
VOID* p;
Buffer->Lock(0, 0, (void**)&p, 0);
memcpy(p, Vertices, sizeof(Vertices));
Buffer->Unlock();
}
I don't see what the problem can be. Thank you for any help, and if more info is needed, please advise me.
The parameter Vertices of your function
void StaticMesh::CreateBuffer(CUSTOMVERTEX Vertices[], LPDIRECT3DDEVICE9 d3ddev)
is a type pointer. So, sizeof(Vertices) inside the function returns just the size of the pointer, not the size of the entire array as you expect.
Please pass the number of vertices as another parameter to the function and modify the function like this.
void StaticMesh::CreateBuffer(CUSTOMVERTEX Vertices[], int aNumVertices, LPDIRECT3DDEVICE9 d3ddev)
{
// USING FIXED SIZE WHILE TESTING WITH A SINGLE TRIANGLE.
d3ddev->CreateVertexBuffer(aNumVertices*sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &Buffer, NULL);
VOID* p;
Buffer->Lock(0, 0, (void**)&p, 0);
memcpy(p, Vertices, aNumVertices * sizeof(CUSTOMVERTEX));
Buffer->Unlock();
}