• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
MartinSmith160

How to create a reusable triangle class in directX 9.0

5 posts in this topic

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
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
The strange thing is I changed the drawPrimitive() call to use D3DPT_POINTLIST and it draws 3 points in the correct screen coordinates.
0

Share this post


Link to post
Share on other sites
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:

[code]
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;
}
[/code]


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
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0