I am currently writing a 3D engine based on the official Direct3D 9 tutorial by Microsoft. I adopted the code to my engine, but I cannot get triangles get rendered. It would be kind if someone could take a look at my structure, maybe he sees the mistake that I cannot. I know it's much code, so I tried to leave unimportant stuff away. The mesh seems to be alright, so I guess the error appears when copying the mesh into the vertex buffer.
The types I use are all based on the native ones.
The vertices I use
---------------------------
struct Vertex
{
type::real position[3]; // float
type::color color; // unsigned long
};
My Mesh class
---------------------------
class Mesh
{
private:
type::uint16 _lastvertex;
Vertex *_vertices;
type::uint16 _vertexcount;
public:
IDirect3DVertexBuffer9 *_data_direct3d9;
Mesh(type::uint16 vertexcount);
virtual ~Mesh();
inline void addVertex(const Vertex &v) { _vertices[_lastvertex++]= v; }
inline void addVertex(const type::real posx, const type::real posy, const type::real posz, const type::color color)
{
_vertices[_lastvertex].position[0]= posx;
_vertices[_lastvertex].position[1]= posy;
_vertices[_lastvertex].position[2]= posz;
_vertices[_lastvertex].color= color;
++_lastvertex;
}
inline type::uint16 getVertexCount() { return _vertexcount; }
inline Vertex* getVertices() { return _vertices; }
inline type::uint32 getVertexMemorySize() { return _vertexcount * sizeof(Vertex); }
};
My Mesh object
---------------------------
// create our test mesh
type::color clr= D3DCOLOR_XRGB(255, 0, 0);
__testmesh= new Mesh(3);
__testmesh->addVertex(150.0f, 50.0f, 0.5f, clr);
__testmesh->addVertex(250.0f, 250.0f, 0.5f, clr);
__testmesh->addVertex( 50.0f, 250.0f, 0.5f, clr);
The Direct3D 9 Renderer Class
---------------------------
class RendererDirect3D9 : public Renderer
{
protected:
const static DWORD __vertex_format= D3DFVF_XYZ|D3DFVF_DIFFUSE;
protected:
IDirect3D9 *_direct3d;
IDirect3DDevice9 *_device;
public:
virtual void update();
virtual void acquireMesh(Mesh *m);
};
The Direct3D 9 Renderer - The created mesh is copied to the vertex buffer
---------------------------
void RendererDirect3D9::acquireMesh(Mesh *m)
{
if(_device->CreateVertexBuffer(m->getVertexMemorySize(), 0, __vertex_format, D3DPOOL_DEFAULT, &m->_data_direct3d9, 0) !=D3D_OK)
{
showError(L"Could not create vertex buffer");
return;
}
void* verts;
if(m->_data_direct3d9->Lock(0, m->getVertexMemorySize(), &verts, 0) !=D3D_OK)
{
showError(L"Could not lock vertex buffer");
return;
}
memcpy(verts, m->getVertices(), m->getVertexMemorySize());
m->_data_direct3d9->Unlock();
}
The Direct3D 9 Renderer - The mesh is rendered
---------------------------
void RendererDirect3D9::update()
{
_device->Clear(0, 0, D3DCLEAR_TARGET, 0, 1.0f, 0);
_device->BeginScene();
//render
_device->SetStreamSource(0, __testmesh->_data_direct3d9, 0, sizeof(Vertex));
_device->SetFVF(__vertex_format);
_device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, __testmesh->getVertexCount() -2);
_device->EndScene();
_device->Present(0, 0, 0, 0);
}
cheers
Daniel