Black Triangle syndrome

Started by
4 comments, last by thestien 12 years, 1 month ago
Hi Guys i am trying to draw a rotating triangle which uses lighting and materials

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;

}
Advertisement
How was ZENVERTEX and CUSTOM_ZENVERTEX defined? Is ZENVERTEX expecting a texture?
this is how they are defined

#define CUSTOM_ZENVERTEX ( D3DFVF_XYZ | D3DFVF_NORMAL)

struct ZENVERTEX
{
float x, y, z;
float nx, ny, nz;
};
A few options spring to mind:

1) Incorrect lighting setup. Either the materials or lighting isn't setup correctly.
2) Incorrect normals. Either they're pointing the wrong way or aren't normalised correctly.
3) The triangle is off the screen. Either the coordinates are wrong or your camera is pointing the wrong direction, or your projection is incorrect.

When you say, "When it draws to screen its black", are you 100% sure it's actually drawing? Black on black is kind of hard to see. Change the background colour being set in the Clear() call to gray instead of black, and you should be able to tell if the geometry is where you're expecting. Then you can eliminate option 3) and look at your lighting setup/normals.
[size="2"]Currently working on an open world survival RPG - For info check out my Development blog:[size="2"] ByteWrangler
i can say it renders to screen as the triangle passes infront of the fps counter in the corner from time to time, although i will change the background colour :) the book says the normals are just facing in the direction of the vertices of the triangle.

so its looking like the light or materials setup.

any ideas on how it should be done what should be called in game init and what should be called in render?

thanks again for the help:)
hi guys just incase anybody wondered i have fixed this now i didnt
->SetRenderState( D3DRS_LIGHTING, TRUE );

at least now i can continue on my road to becoming a zen programmer... lol :)

This topic is closed to new replies.

Advertisement