when it draws to screen its black i must have not setup either the light or material properly.
I know i am doing soething wrong or not doing something right. This is not my code design i am copying from a book which is a bit sketchy about which bits of code go where.
what is the correct way to get this kind of thing to work?
here is what my code does atm
int GameInit()
{
HRESULT r = 0;
g_pD3D = Direct3DCreate9( D3D_SDK_VERSION);
if( g_pD3D == NULL)
{
SetError( " Could not create IDIRECT3D9 object");
return E_FAIL;
}
r = InitDirect3DDevice( g_hWndMain, 640, 480, TRUE, D3DFMT_A8R8G8B8, g_pD3D, &g_pDevice);
if( FAILED( r))
{
SetError( "Init of the device failed");
return E_FAIL;
}
CreateViewport();
SetProjectionMatrix();
g_pDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE);
g_pDevice->SetRenderState( D3DRS_LIGHTING, TRUE);
D3DXMATRIX ViewMatrix;
D3DXMatrixLookAtLH( &ViewMatrix, &D3DXVECTOR3(0.0f, 3.0f, -5.0f),
&D3DXVECTOR3(0.0f, 0.0f, 0.0f),
&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
g_pDevice->SetTransform( D3DTS_VIEW, &ViewMatrix);
D3DLIGHT9 Light;
ZeroMemory( &Light, sizeof(D3DLIGHT9));
Light.Type = D3DLIGHT_POINT;
Light.Diffuse.r = 1.0f;
Light.Diffuse.g = 1.0f;
Light.Diffuse.b = 1.0f;
Light.Position = D3DXVECTOR3( 0, 0, -10);
Light.Range = 100.0f;
g_pDevice->SetLight( 0, &Light);
g_pDevice->LightEnable( 0, TRUE);
D3DMATERIAL9 Material;
ZeroMemory( &Material, sizeof(D3DMATERIAL9));
Material.Diffuse.r = 1.0f;
Material.Diffuse.g = 0.0f;
Material.Diffuse.b = 0.0f;
Material.Diffuse.a = 1.0f;
Material.Ambient.r = 1.0f;
Material.Ambient.b = 1.0f;
Material.Ambient.g = 0.0f;
Material.Ambient.a = 1.0f;
g_pDevice->SetMaterial( &Material);
/*D3DXMATRIX RotationX, RotationY, WorldMatrix;
D3DXMatrixRotationY( &RotationY, timeGetTime()/200.0f);
D3DXMatrixRotationX( &RotationX, timeGetTime()/800.0f);
D3DXMatrixMultiply( &WorldMatrix, &RotationX, &RotationY);
g_pDevice->SetTransform( D3DTS_WORLD, &WorldMatrix);
*/
g_pDevice->Clear( 0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 0, 0, 0, 0),10.f, 0);
r = g_pDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &g_pBackSurface);
if( FAILED( r))
{
SetError( "Couldnt get backbuffer");
return E_FAIL;
}
LoadAlphabet( "alpha1632.bmp", 16, 32);
srand( GetTickCount());
InitTiming();
Console.Initialize( g_pDevice, g_pBackSurface);
//Console.SetParserCallback( ConsoleParser);
ZENVERTEX ZenVertices[] =
{
{ -2.0f, -20.0f, 0.0f, -1.0f, -1.0f, 0.0f},
{ 2.0f, -2.0f, 0.0f, 1.0f, -1.0f, 0.0f},
{ 0.0f, 2.0f, 0.0f, 0.0f, 1.0f, 0.0f},
};
r = g_pDevice->CreateVertexBuffer(sizeof(ZENVERTEX)*3, 0, CUSTOM_ZENVERTEX,
D3DPOOL_DEFAULT, &g_pVB, 0);
if( FAILED( r))
{
SetError( " couldnt create the vertex buffer");
return E_FAIL;
}
void* pVertices = 0;
r = g_pVB->Lock( 0, sizeof( ZenVertices), &pVertices, NULL);
if( FAILED( r))
{
SetError( " Could not locl the vertex buffer");
return E_FAIL;
}
CopyMemory( pVertices, &ZenVertices, sizeof(ZenVertices));
g_pVB->Unlock();
return S_OK;
}
and then render
int Render()
{
//Pause(1000);
HRESULT r = 0;
g_pDevice->Clear( 0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 0, 0, 0, 0),10.f, 0);
if(!g_pDevice)
{
SetError( "cannot render because there is no device");
return E_FAIL;
}
/*r = ValidateDevice();
if( FAILED( r));
{
return E_FAIL;
}
*/
D3DLOCKED_RECT Locked;
g_pBackSurface->LockRect(&Locked, 0,0) ;
PrintFrameRate( 500, 400, TRUE, D3DCOLOR_ARGB( 0, 255, 0, 255), (DWORD*)Locked.pBits, Locked.Pitch);
g_pBackSurface->UnlockRect();
g_pDevice->BeginScene();
D3DXMATRIX RotationX, RotationY, WorldMatrix;
D3DXMatrixRotationY( &RotationY, timeGetTime()/200.0f);
D3DXMatrixRotationX( &RotationX, timeGetTime()/800.0f);
D3DXMatrixMultiply( &WorldMatrix, &RotationX, &RotationY);
g_pDevice->SetTransform( D3DTS_WORLD, &WorldMatrix);
g_pDevice->SetStreamSource( 0, g_pVB, 0, sizeof(ZENVERTEX));
g_pDevice->SetFVF( CUSTOM_ZENVERTEX);
g_pDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1);
g_pDevice->EndScene();
Console.Render();
g_pDevice->Present( NULL, NULL, NULL, NULL);
return S_OK;
}