View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# How to create a reusable triangle class in directX 9.0

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1MartinSmith160  Members

Posted 28 June 2012 - 03:36 AM

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.

[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.

All the best,
Martin

### #2molehill mountaineer  Members

Posted 28 June 2012 - 04:22 AM

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.

Edited by molehill mountaineer, 28 June 2012 - 04:23 AM.

### #3MartinSmith160  Members

Posted 28 June 2012 - 04:59 AM

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.

### #4MartinSmith160  Members

Posted 28 June 2012 - 07:15 AM

The strange thing is I changed the drawPrimitive() call to use D3DPT_POINTLIST and it draws 3 points in the correct screen coordinates.

### #5turch  Members

Posted 28 June 2012 - 08:57 AM

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.

Edited by turch, 28 June 2012 - 09:02 AM.

### #6MartinSmith160  Members

Posted 03 July 2012 - 01:19 AM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.