Another simple program causing me headache. it is supposed to draw coloured lines but they only apprear black. ill try to only include relavent code.
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;
}
g_Camera.Reset();
CreateViewport();
SetProjectionMatrix();
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;
}
srand( GetTickCount());
InitTiming();
for( int i = 0; i < g_NumPoints; i++)
{
g_Lines[i].SetProps( 0,0,0,D3DCOLOR_XRGB(rand()%255,rand()%255,rand()%255),
(float)(rand()%10)-rand()%10,
(float)(rand()%10)-rand()%10,
(float)(rand()%10)-rand()%10,
D3DCOLOR_XRGB(rand()%255, rand()%255, rand()%255));
}
return S_OK;
}
this should setup 300 lines of random colour all starting at the origin and extending out randomly.
g_NumPoints = 300;
int Render()
{
//Pause(1000);
HRESULT r = 0;
g_pDevice->Clear( 0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 255, 50, 50, 50),10.f, 0);
if(!g_pDevice)
{
SetError( "cannot render because there is no device");
return E_FAIL;
}
g_pDevice->BeginScene();
D3DXMATRIX RotationX, RotationY, WorldMatrix;
D3DXMatrixRotationY( &RotationY, timeGetTime()/200.0f);
D3DXMatrixRotationX( &RotationX, timeGetTime()/800.0f);
D3DXMatrixMultiply( &WorldMatrix, &RotationX, &RotationY);
/*
static 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 = 10.0f;
g_pDevice->SetLight( 0, &Light);
g_pDevice->LightEnable( 0, TRUE);
g_pDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
D3DMATERIAL9 Material;
ZeroMemory( &Material, sizeof(D3DMATERIAL9));
Material.Diffuse.r = 0.0f;
Material.Diffuse.g = 0.5f;
Material.Diffuse.b = 0.0f;
Material.Diffuse.a = 1.0f;
Material.Ambient.r = 1.0f;
Material.Ambient.b = 0.0f;
Material.Ambient.g = 0.0f;
Material.Ambient.a = 1.0f;
g_pDevice->SetMaterial( &Material);
*/
g_pDevice->SetFVF( ZENVERTEX_TYPE);
g_pDevice->SetTransform( D3DTS_WORLD, &WorldMatrix);
for( int i = 0; i < g_NumPoints; i++)
{
g_Lines[i].Render();
}
g_pDevice->EndScene();
g_pDevice->Present( NULL, NULL, NULL, NULL);
static float z = 0;
static BOOL bForward = FALSE;
if(bForward == TRUE)
{
z += 0.1f;
if(z > 0.0f)
{
z = 0.0f;
bForward=FALSE;
}
}
else
{
z -= 0.1f;
if(z < -50.0f)
{
z = -50.0f;
bForward = TRUE;
}
}
g_Camera.SetPosition(0,0,z);
g_Camera.Update();
return S_OK;
}
now this is supposed to draw those lines to the screen i have no idea whats wrong with it i have tried messing about with light and material which is commented out but i have had no luck with it
HRESULT CZenLine::Render()
{
HRESULT r = 0;
LPDIRECT3DVERTEXBUFFER9 pVB = 0;
r = g_pDevice->CreateVertexBuffer( sizeof( CZenVertex) * 2, D3DUSAGE_WRITEONLY,
ZENVERTEX_TYPE, D3DPOOL_DEFAULT, &pVB, NULL);
if (FAILED(r))
{
SetError("ZenLine Dont Work");
return E_FAIL;
}
BYTE* pData = 0;
r = pVB->Lock( 0, 0, (void**)&pData, 0);
if( FAILED(r))
{
pVB->Release();
return E_FAIL;
}
CopyMemory( pData, (void*)&m_StartPoint, sizeof( CZenVertex));
CopyMemory( pData + sizeof(CZenVertex), (void*)&m_EndPoint, sizeof( CZenVertex));
pVB->Unlock();
g_pDevice->SetStreamSource( 0, pVB, 0, sizeof( CZenVertex));
g_pDevice->DrawPrimitive( D3DPT_LINELIST, 0, 1);
pVB->Release();
return S_OK;
}
this is the render function for the lines.
and finally
#define ZENVERTEX_TYPE ( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_SPECULAR |D3DFVF_TEX1)
class CZenVertex
{
public:
CZenVertex();
CZenVertex( float x, float y, float z, float nx, float ny, float nz, D3DCOLOR DiffuseColor,
D3DCOLOR SpecularColor, float tu, float tv);
~CZenVertex();
public:
void Set( float x, float y, float z, float nx, float ny, float nz, D3DCOLOR DiffuseColor,
D3DCOLOR SpecularColor, float tu, float tv);
protected:
public:
D3DVECTOR m_Position;
D3DVECTOR m_Normal;
D3DCOLOR m_DiffuseColor;
D3DCOLOR m_SpecularColor;
float m_tu, m_tv;
protected:
};
this is the vertex info.
sorry i have posted so much code but hopefully you can help me figure out what the problem is as i have had the same problem with this happening with a similar program drawing points.
thanks in advance, your help is appreciated






