How to create a reusable triangle class in directX 9.0

Started by
4 comments, last by MartinSmith160 11 years, 9 months ago
Hi All,

I have created a 2D DirectX 9.0 game engine and the last thing I need to add to is is the ability to draw triangle primitives. I wrote a class after looking at a number of tutorials. The code compiles fine but nothing is ever drawn to the screen.

Here is the Header:

[source lang="cpp"]
typedef struct d3dVert
{
float x,y,z,rhw;
DWORD color;
}D3DVERTEX;


class ST_Prim_Triangle
{
private:
LPDIRECT3DVERTEXBUFFER9 vertex_object;
void * vertex_buffer;
D3DVERTEX vertices[3];
ST_Window *targetWindow;

public:
ST_Prim_Triangle();
~ST_Prim_Triangle();

bool Create(ST_Window *targetWindow, ST_Point2f p1, ST_Point2f p2, ST_Point2f p3, D3DCOLOR col = D3DCOLOR_XRGB(255,0,0));
bool Draw();

}; //class[/source]

Here is the implementation:

[source lang="cpp"]ST_Prim_Triangle::ST_Prim_Triangle()
{
vertices[0].x = 0.0f;
vertices[0].y = 0.0f;
vertices[0].z = 0.0f;
vertices[0].rhw = 1.0f;

vertices[1].x = 0.0f;
vertices[1].y = 0.0f;
vertices[1].z = 0.0f;
vertices[1].rhw = 1.0f;

vertices[2].x = 0.0f;
vertices[2].y = 0.0f;
vertices[2].z = 0.0f;
vertices[2].rhw = 1.0f;

vertex_object = NULL;
vertex_buffer = NULL;
}

ST_Prim_Triangle::~ST_Prim_Triangle()
{
vertex_object->Release();
vertex_buffer = 0;
targetWindow = 0;
}

bool ST_Prim_Triangle::Create(StormEngine::ST_Window *targetWindow, StormEngine::ST_Point2f p1, StormEngine::ST_Point2f p2, StormEngine::ST_Point2f p3, D3DCOLOR col)
{
this->targetWindow = targetWindow;

vertices[0].x = p1.x;
vertices[0].y = p1.y;
vertices[0].z = 0.0f;
vertices[0].rhw = 1.0f;
vertices[0].color = col;

vertices[1].x = p2.x;
vertices[1].y = p2.y;
vertices[1].z = 0.0f;
vertices[1].rhw = 1.0f;
vertices[1].color = col;

vertices[2].x = p3.x;
vertices[2].y = p3.y;
vertices[2].z = 0.0f;
vertices[2].rhw = 1.0f;
vertices[2].color = col;

if(FAILED(targetWindow->GetWindowDevice()->CreateVertexBuffer(3*sizeof(D3DVERTEX), 0,
D3DFVF_XYZRHW|D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &vertex_object, NULL)))
{
return(0);
}

if(FAILED(vertex_object->Lock(0, 3*sizeof(D3DVERTEX), &vertex_buffer, 0)))
return(0);


memcpy(vertex_buffer, vertices, 3*sizeof(D3DVERTEX));
vertex_object->Unlock();

return true;
}

bool ST_Prim_Triangle::Draw()
{
targetWindow->GetWindowDevice()->SetStreamSource(0, vertex_object, 0, sizeof(D3DVERTEX));
targetWindow->GetWindowDevice()->SetFVF(D3DFVF_XYZRHW|D3DFVF_DIFFUSE);
targetWindow->GetWindowDevice()->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

return true;
}[/source]

Im not really sure where im going wrong with this, any help would be so greatly appreciated as its the last aspect of the engine to finish.

Thanks in advance.

All the best,
Martin
Advertisement
I started with dx10 so I am unable to comment on your actual code - Have you tried running it through PIX debugger?
Make sure to set the folder from which to run the executable (otherwise your program might crash when trying to run it through PIX) and double-check your shader code. If you can see the triangle going in/out of the shader correctly in PIX you might try turning off backface culling.
Hi Molehill Mountaineer

Thanks for your input, I have never used PIX before but I ran it through it and nothing seems to be sticking out as wrong. It executes the creation and drawing code of the triangle. I turned lighting and backface culling off and it made no difference.
The strange thing is I changed the drawPrimitive() call to use D3DPT_POINTLIST and it draws 3 points in the correct screen coordinates.
I don't see your actual vertex coordinates, but if it draws correctly as a point list, then it's probably a culling issue. You don't say how you turned off culling before, but try inserting the following into your draw function, to make sure that there is no culling just before the draw call:


bool ST_Prim_Triangle::Draw()
{
targetWindow->GetWindowDevice()->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); // this line
targetWindow->GetWindowDevice()->SetStreamSource(0, vertex_object, 0, sizeof(D3DVERTEX));
targetWindow->GetWindowDevice()->SetFVF(D3DFVF_XYZRHW|D3DFVF_DIFFUSE);
targetWindow->GetWindowDevice()->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

return true;
}



If that doesn't help, in PIX go to the end of a frame, select the color buffer window, right click on a pixel that is within the area the triangle should be drawn, and click "Debug Pixel". That should show each time the pixel shader was run, and if it failed the depth test, stencil test, etc.
Hi Turch

Thanks for the help. I turned off culling before the draw call and it made no difference. I did manage to create a triangle though. I created a vector of points using a flood fill equation at that made it correctly. Managed to make all the needed Primitives using that method. Thanks a lot and im definitely going to look at PIX more now I know of it.

All the best,
Martin

This topic is closed to new replies.

Advertisement