Problem using index buffers

Hi,

I am trying to draw a simple quad using IDirect3DIndexBuffer9 (im using directX 9 and c++). I have set de RenderState to (D3DRS_CULLMODE, D3DCULL_NONE) and i cannot see the 2 triangles of my quad.

here is my code

//#include <Windows.h>

//#include <mmsystem.h>

#include<d3dx9.h>

#pragmawarning(disable : 4996 )// disable deprecated warning

#include<strsafe.h>

#pragmawarning(default : 4996 )

//--

// Global variables

//--

LPDIRECT3D9 g_pD3D = NULL;// Used to create the D3DDevice

LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;// Our rendering device

LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL;// Buffer to hold Vertices

LPDIRECT3DINDEXBUFFER9 g_pIX =NULL;//Buffer to hold index

// A structure for our custom vertex type

struct CUSTOMVERTEX

{

FLOAT x, y, z;// The transformed 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)

//--

// Name: InitD3D()

// Desc: Initializes Direct3D

//--

HRESULT InitD3D( HWND hWnd )

{

// Create the D3D object.

if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )

return E_FAIL;

// Set up the structure used to create the D3DDevice

D3DPRESENT_PARAMETERS d3dpp;

ZeroMemory( &d3dpp,sizeof(d3dpp) );

d3dpp.Windowed = TRUE;

d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;

d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

// Create the D3DDevice

if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,

D3DCREATE_SOFTWARE_VERTEXPROCESSING,

&d3dpp, &g_pd3dDevice ) ) )

{

return E_FAIL;

}

// Device state would normally be set here

g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );

g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );

return S_OK;

}

HRESULT InitVB()

{

// Initialize three Vertices for rendering a quad

CUSTOMVERTEX Vertices[] =

{

{ 0.0f, 0.0f, 0.0f, 0xffff0000, },//0

{ 1.0f, 0.0f, 0.0f, 0xff0000ff, },//1

{ 0.0f, 1.0f, 0.0f, 0xffff0000, },//2

{ 1.0f, 1.0f, 0.0f, 0xff0000ff, },//3

};

if( FAILED( g_pd3dDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),

0, D3DFVF_CUSTOMVERTEX,

D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )

{

return E_FAIL;

}

VOID* pVertices;

if( FAILED( g_pVB->Lock( 0, 0, (void**)&pVertices, 0 ) ) )

return E_FAIL;

memcpy( pVertices, Vertices,sizeof(Vertices) );

g_pVB->Unlock();

// Crear los Indices

if( FAILED(g_pd3dDevice->CreateIndexBuffer( 6 *sizeof(WORD),D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED,&g_pIX, 0)))

return E_FAIL;

DWORD Indices[]={

3,1,0,

2,3,0

};

VOID *pIX;

if( FAILED(g_pIX->Lock(0, 0, (void**)&pIX, 0)))

return E_FAIL;

memcpy( pIX, Indices,sizeof(Indices) );

sizeof(&pIX);

sizeof(Indices);

g_pIX->Unlock();

DWORD xDevil;

// El view

D3DXVECTOR3 vEyePt( 0.0f, 0.0f,10.0f );

D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );

D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );

D3DXMATRIXA16 matView;

D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );

g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );

D3DXMATRIXA16 matProj;

D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 10.0f, -10.0f );

g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );

return S_OK;

}

//--

// Name: Cleanup()

// Desc: Releases all previously initialized objects

//--

VOID Cleanup()

{

if( g_pVB != NULL )

g_pVB->Release();

if( g_pd3dDevice != NULL )

g_pd3dDevice->Release();

if( g_pD3D != NULL )

g_pD3D->Release();

}

//--

// Name: Render()

// Desc: Draws the scene

//--

VOID Render()

{

// Clear the backbuffer to a blue color

g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,255), 1.0f, 0 );

// Begin the scene

if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )

{

g_pd3dDevice->SetStreamSource( 0, g_pVB, 0,sizeof(CUSTOMVERTEX) );

g_pd3dDevice->SetIndices(g_pIX);

g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );

g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 4, 0, 2);

// End the scene

g_pd3dDevice->EndScene();

}

// Present the backbuffer contents to the display

g_pd3dDevice->Present( NULL, NULL, NULL, NULL );

}

//--

// Name: MsgProc()

// Desc: The window's message handler

//--

LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )

{

switch( msg )

{

case WM_DESTROY:

Cleanup();

PostQuitMessage( 0 );

return 0;

}

return DefWindowProc( hWnd, msg, wParam, lParam );

}

//--

// Name: WinMain()

// Desc: The application's entry point

//--

INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )

{

// Register the window class

WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,

GetModuleHandle(NULL), NULL, NULL, NULL, NULL,

"D3D Tutorial", NULL };

RegisterClassEx( &wc );

// Create the application's window

HWND hWnd = CreateWindow("D3D Tutorial","D3D Tutorial 02: Vertices",

WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,

NULL, NULL, wc.hInstance, NULL );

// Initialize Direct3D

if( SUCCEEDED( InitD3D( hWnd ) ) )

{

// Create the vertex buffer

if( SUCCEEDED( InitVB() ) )

{

// Show the window

ShowWindow( hWnd, SW_SHOWDEFAULT );

UpdateWindow( hWnd );

// Enter the message loop

MSG msg;

ZeroMemory( &msg,sizeof(msg) );

while( msg.message!=WM_QUIT )

{

if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )

{

TranslateMessage( &msg );

DispatchMessage( &msg );

}

else

Render();

}

}

}

UnregisterClass("D3D Tutorial", wc.hInstance );

return 0;

}

[12513 byte] By [DanielMena] at [2007-12-23]
# 1
Hey man,

your indexes are 16 o 32 bits? Look at this:

if( FAILED(g_pd3dDevice->CreateIndexBuffer( 6 * sizeof(WORD),D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED,&g_pIX, 0)))

return E_FAIL;

DWORD Indices[]={

3,1,0,

2,3,0

};


From CreateIndexBuffer seems that they are 16 bits.....
....but from declaration (DWORD indices) seems that they are 32 bits..... :)
Change DWORD to WORD and it works (else the memcpy used to filling the index buffer, run out of bounds...)

- AGPX

AGPX at 2007-8-30 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: Graphics...