Starting DirectX9 - Need some advice on a few things...

Started by
15 comments, last by 21st Century Moose 13 years, 6 months ago
I have been working on a project at work with DirectX9 in Borland, which has been a complete nightmare.

Since Borland is such a pain to get to work with DirectX9, and isn't even supported, I've decided to dive into learning DirectX9 at home this weekend with Visual Studio, which I much prefer, so I have some support, intellisense, documentation, and actual tutorials to go by to build what I need up from the ground up here, where I know it works, and then try and do the convert at work next week.

I've been diving through the samples and tutorials provided in the SDK, and so far I have easily created and initialized the device, displayed the solid blue background, and then also started working with the vertex example to get a triangle to display on the screen. My next step is to make that triangle into a square, which I know I'll simply need to add another vertex so that instead of drawing 3, I'll be drawing 4 to make a square. Once I have that I'll try and load an image texture into the vertex.

So I'm hoping for some advice and suggestions along my progression. My first one, is can someone explain the vertexes and the coordinate system to me a little better?

The example defines their vertex for the triangle as:
Vertex vertices[] =    {        { 150.0f,  50.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color        { 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, },        {  50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, },    };


Which is simply using the structure:
struct Vertex{    FLOAT x, y, z, rhw; // The transformed position for the vertex    DWORD color;        // The vertex color};


How do the x,y,z, rhw work with this? Are the x,y,z the pixel coordinates on the screen? So say 150.0f for the x is merely 150 pixels from the top left, which is 0,0?
Advertisement
They are not screen coordinates, they are whats called object space coordinates. There is alot to take in and if I explained everything you would get lost and not remember what you read.

For now, it is enough to say that those coordinates are in the world. It is easier and quicker to learn if you just modify the values and see what happens. Somewhere in your code, you should create whats called your View matrix. The code to create it should be on a function D3DXMatrixLookAt . In that function you give it coordinates to look at, and your position. So, if you look at (0, 0, 0) and your position passed is (0, 0, -20) then you will look at the center of the world. You should also figure out what the heck the difference between a left handed coordinate system and a right handed coordinate system is; it will help you orient yourself and figure out why positive Z goes into the monitor for Directx. So, try changing your vertices to something around that area and see what happens. Then try messing with the Projection Matrix and your Field of View to see what that does too. After that you should have an idea of what the coordinates mean and you can dive off into other things like object to world transformations, figure out how to rotate objects, and scale them.

I HIGHLY recommend you read a couple of chapters in this book, it will help you SO MUCH

http://books.google.com/books?id=wCfWkc_E3GkC&printsec=frontcover&dq=3d+math+primer+for+graphics+and+game+development&hl=en&ei=Ogy6TJ_FCI6-sAP-1pjcDg&sa=X&oi=book_result&ct=result&resnum=1&ved=0CDIQ6AEwAA#v=onepage&q&f=false
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.
Well, my implementations for this should be in a 2d perspective, based in a windowed mode, and hopefully when I'm done will be displaying in a 1024x512 display window within my application.

I was just trying to figure out how those coordinates related to everything else. I'll take a look at the article you posted and see if any of it makes sense.
I'll like to add, from the example they at least, for the most part, the x,y affect the pixel position of the vertices of the triangle in respect to the window.

Changing it to:
Vertex vertices[] =    {        { 0.0f,  0.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color        { 200.0f, 200.0f, 0.5f, 1.0f, 0xff00ff00, },        {  0.0f, 200.0f, 0.5f, 1.0f, 0xff00ffff, },    };


Places one vertex 100 pixels over, 0 pixels down.
Next one 200 pixels over, 200 pixels down.
0 Pixels over, 200 pixels down.

So I know have a
**         *

Instead of a
     **         *
How come changing the vertex array to:
    Vertex vertices[] =    {        {   0.0f,   0.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color        { 200.0f, 200.0f, 0.5f, 1.0f, 0xff00ff00, },        {   0.0f, 200.0f, 0.5f, 1.0f, 0xff00ffff, },        { 200.0f,   0.0f, 0.5f, 1.0f, 0xff00ffff, }     };

And my vertex buffer definition to:
if( FAILED( d3d.d3d9Device->CreateVertexBuffer( 		4 * sizeof( Vertex ),		//  Length		0,							//  Usage - 		D3DFVF_TLVERTEX,			//  Specifies Vertex Format		D3DPOOL_DEFAULT,			//  D3DPool 		&d3d.d3d9VertexBuffer,		//  Pointer to the Direct3D Vertex Buffer 		NULL						//  HANDLE 	) ) )

Did not result in a square/quad?
Imagine that you have one of the tube monitors ( I still use them ). And inside that tube monitor, its empty. Now, the inside of your monitor the center represents (0, 0, 0) Make sure you are Facing your monitor. Facing your monitor, Positive X goes out the right side of it, Positive Y Goes Up to the ceiling, and Positive Z goes Into the monitor out the back.

Knowing that, try again. Also, make sure your ViewMatrix is set to look at 0 0 0 and set it back some like 100 units back to make sure you can see your object. 100 units back would be a pos of (0, 0, -100) btw.

To know exactly what is what. Change your Colors so you can see where the verts move.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.
Well adding the extra vertex I still see a triangle, not a square. With 4 points should I not see a square?
Are you drawing 3 vertices or 4 ?


You have to remember to set your DrawVertices to 4 now, instead of 3
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.
Well, here is what I did...

Added a vertex to to vertexes:
    Vertex vertices[] =    {        {   0.0f,   0.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color		{ 200.0f,   0.0f, 0.5f, 1.0f, 0xff00ffff, },        { 200.0f, 200.0f, 0.5f, 1.0f, 0xff00ff00, },		{   0.0f, 200.0f, 0.5f, 1.0f, 0xff00ffff, },		    };


I called this with 4 * instead of 3* as such:
if( FAILED( d3d.d3d9Device->CreateVertexBuffer( 		4 * sizeof( Vertex ),		//  Length		0,							//  Usage - 		D3DFVF_TLVERTEX,			//  Specifies Vertex Format		D3DPOOL_DEFAULT,			//  D3DPool 		&d3d.d3d9VertexBuffer,		//  Pointer to the Direct3D Vertex Buffer 		NULL						//  HANDLE 	) ) )    {        //-------------------		return E_FAIL;		//-------------------    }


Then I called the following:
d3d.d3d9Device->SetStreamSource( 0, d3d.d3d9VertexBuffer, 0, sizeof( Vertex ) );        d3d.d3d9Device->SetFVF( D3DFVF_TLVERTEX );        d3d.d3d9Device->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 1 );


I changed it to TriangleStrip so it should support the 4 vertexes, recognize the 4th is alone and connect it to accordingly.

Really not sure what else I need to do beyond that...
On a note to your response, I did that with:
d3d.d3d9Device->CreateVertexBuffer(		4 * sizeof( Vertex ),		//  Length		0,			        //  Usage -		D3DFVF_TLVERTEX,		//  Specifies Vertex Format		D3DPOOL_DEFAULT,		//  D3DPool 		&d3d.d3d9VertexBuffer,		//  Pointer to the Vertex Buffer 		NULL						//  HANDLE 	)

Didn't I? That and changing it to the D3DPT_TRIANGLESTRIP instead of D3DPT_TRIANGLELIST should have taken care of it right?

This topic is closed to new replies.

Advertisement