• Advertisement
Sign in to follow this  

Drawing lines that wont show as coloured

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys only me smile.png

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.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.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 smile.png

Share this post


Link to post
Share on other sites
Advertisement
Hi guys should i move this thread to the dx xna forum? i was worried about posting it there as im a begginer.
i thought this would be the best place because its a noob friendly forum my lack of understanding wont offend anyone who would potentially help me but on the downside more people who deal with dx would get to see this if i moved it.

anyway i think i like this forum better you guys are cool :)

Share this post


Link to post
Share on other sites
What if you add in a call to:
g_pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
somewhere before rendering your lines?

EDIT: If that fixes it, it's because lighting is on by default, but you're not setting up any lights or materials in your scene.

Also, what does the SetProps() function look like? Are you sure the parameters are in the correct order?

Share this post


Link to post
Share on other sites
Hi Thanks for the reply Evil Steve

im at work so im itching to get home to change the code and see if it works :)

here is the setprops function

void CZenLine::SetProps( float x1, float y1, float z1, D3DCOLOR StartColor,
float x2, float y2, float z2, D3DCOLOR EndColor)
{
m_StartPoint.m_Position.x = x1;
m_StartPoint.m_Position.y = y1;
m_StartPoint.m_Position.z = z1;
m_StartPoint.m_DiffuseColor = StartColor;
m_EndPoint.m_Position.x = x2;
m_EndPoint.m_Position.y = y2;
m_EndPoint.m_Position.z = z2;
m_EndPoint.m_DiffuseColor = EndColor;
}


thanks again for your help :)

Share this post


Link to post
Share on other sites
Evil Steve i do declare you a genius.

for the second time you have fixed my programme with a single line of text.

thanks again for the help its much appriciated. biggrin.png

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement