drawing a triangle issue

This topic is 2901 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hey, i'm following the basics of directx programming and i'm at a point where i'm learning how to create and draw a triangle to the screen. My code works good, but there's just one thing i don't understand. I'll show you some code first. This is basically how i create my triangle: I've created my custom vertex
PDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold vertices

// A structure for our custom vertex type
struct CUSTOMVERTEX
{
FLOAT x, y, z;      // The untransformed, 3D position for the vertex
DWORD color;        // The vertex color
};
// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)


Then i create the triangle like so:
HRESULT PVCore::pvCreateTriangle ( )
{
// Initialize three vertices for rendering a triangle
CUSTOMVERTEX g_Vertices[] =
{
{ -1.0f,-1.0f, 0.0f, 0xffff0000, },
{  1.0f,-1.0f, 0.0f, 0xff0000ff, },
{  0.0f, 1.0f, 0.0f, 0xffffffff, },
};

// Create the vertex buffer.
if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3 * sizeof( CUSTOMVERTEX ),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
{
return E_FAIL;
}

// Fill the vertex buffer.
VOID* pVertices;
if( FAILED( g_pVB->Lock( 0, sizeof( g_Vertices ), ( void** )&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, g_Vertices, sizeof( g_Vertices ) );
g_pVB->Unlock();

return S_OK;
}


And in the renderloop:
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof( CUSTOMVERTEX ) );
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 1 );


Now so far, when i run my code i see a triangle. But i don't get is this part:
    CUSTOMVERTEX g_Vertices[] =
{
{ -1.0f,-1.0f, 0.0f, 0xffff0000, },
{  1.0f,-1.0f, 0.0f, 0xff0000ff, },
{  0.0f, 1.0f, 0.0f, 0xffffffff, },
};

Why am i working with 1 and 0 ? How come i'm not using the actual X,Y coordinates of my window?? My window is 640x480. But when i change it to this:
    CUSTOMVERTEX g_Vertices[] =
{
{ 320.0f, 25.0f, 0.0f, 0xffff0000, },
{ 600.0f, 400.0f, 0.0f, 0xff0000ff, },
{ 40.0f, 400.0f, 0.0f, 0xffffffff, },
};

Then i don't see a triangle. So my question really is, how come? And how can i still use the X,Y pixel location instead of using -1.0 to 1.0??

Share on other sites
Normal 3D work is done by transforming into a view frustum that has a range of -1 to 1 for X and Y (and 0 to 1 for Z). This is then scaled by the viewport size. This allows changing your display size and having the scene scale with it.

(Take a look at "Viewports and Clipping (Direct3D 9)" in the DX docs.)

If you want to draw in screen coordinates (which can be useful for 2D), use D3DFVF_XYZRHW for your FVF component, and add an rhw component to your vertex. Then the vertices will not go through the transform pipeline and will display in screen coordinate.

Again, using XYZRHW is good for 2D, but if you want 3D I'd suggest you use the normal vertex pipeline.

[Edited by - ET3D on February 11, 2010 2:17:50 AM]

Share on other sites
Quote:
 Original post by ET3DNormal 3D work is done by transforming into a view frustum that has a range of -1 to 1 for X and Y (and 0 to 1 for Z). This is then scaled by the viewport size. This allows changing your display size and having the scene scale with it.(Take a look at "Viewports and Clipping (Direct3D 9)" in the DX docs.)If you want to draw in screen coordinates (which can be useful for 2D), use D3DFVF_XYZRHW for your FVF component, and add an rhw component to your vertex. Then the vertices will not go through the transform pipeline and will display in screen coordinate.Again, using XYZRHW is good for 2D, but if you want 3D I'd suggest you use the normal vertex pipeline.

Thanks! I managed to get it working with XYZRHW.