Original Post
Hi All,
I am new at using d3d and need a little help. I am making a particle engine and I am having trouble whenever I add D3DFVF_DIFFUSE as a flag and to my struct. It crashes my program, whenever I have this flag. I want to be able to change the color of my particles but I am unable to do so whenever I try to use D3DFVF_DIFFUSE. Maybe I am forgetting to do something. Here is my code that is relevant to my problem:
struct PARTICLEVERTEX
{
D3DXVECTOR3 vecPos;
D3DXCOLOR clrDiffuse;
FLOAT tu;
FLOAT tv;
};
#define D3DFVF_PARTICLEVERTEX ( D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1 )
//RestoreDevice Code
HRESULT hr;
PARTICLEVERTEX v[4];
UINT k = 0;
v[k].vecPos = D3DXVECTOR3( -1.0f, -1.0f, 0.0f );
v[k].clrDiffuse = D3DCOLOR_ARGB( 255, 0, 0, 255 );
v[k].tu = 0.0f;
v[k].tv = 1.0f;
k++;
v[k].vecPos = D3DXVECTOR3( -1.0f, 1.0f, 0.0f );
v[k].clrDiffuse = D3DCOLOR_ARGB( 255, 0, 0, 255 );
v[k].tu = 0.0f;
v[k].tv = 0.0f;
k++;
v[k].vecPos = D3DXVECTOR3( 1.0f, 1.0f, 0.0f );
v[k].clrDiffuse = D3DCOLOR_ARGB( 255, 0, 0, 255 );
v[k].tu = 1.0f;
v[k].tv = 0.0f;
k++;
v[k].vecPos = D3DXVECTOR3( 1.0f, -1.0f, 0.0f );
v[k].clrDiffuse = D3DCOLOR_ARGB( 255, 0, 0, 255 );
v[k].tu = 1.0f;
v[k].tv = 1.0f;
k++;
if( FAILED( hr = m_pd3dDevice->CreateVertexBuffer( sizeof(v), D3DUSAGE_WRITEONLY, D3DFVF_PARTICLEVERTEX,
D3DPOOL_MANAGED, &m_pParticleVB ) ) )
{
return hr;
}
VOID* pVertices;
// Fill in the grid vertex data
m_pParticleVB->Lock( 0, sizeof(v), (BYTE**)&pVertices, 0 );
memcpy( pVertices, v, sizeof(v) );
m_pParticleVB->Unlock();
return S_OK;
//RENDER CODE
D3DXMATRIX ViewMatrix;
D3DXMatrixIdentity( &ViewMatrix );
// get the ViewMatrix from D3D
m_pd3dDevice->GetTransform( D3DTS_VIEW, &ViewMatrix );
D3DXMATRIX InvViewMatrix;
D3DXMatrixIdentity( &InvViewMatrix );
D3DXMatrixInverse( &InvViewMatrix, NULL, &ViewMatrix );
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_ONE );
m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );
m_pd3dDevice->SetStreamSource( 0, m_pParticleVB, sizeof( PARTICLEVERTEX ) );
m_pd3dDevice->SetVertexShader( D3DFVF_PARTICLEVERTEX );
m_pd3dDevice->SetTexture( 0, m_pParticleTexture );
for( int i = 0; i < m_iNumberParticles; i++ )
{
InvViewMatrix._41 = m_pParticles.m_vPosition.x;
InvViewMatrix._42 = m_pParticles[i].m_vPosition.y;
InvViewMatrix._43 = m_pParticles[i].m_vPosition.z;
// Set the new world transform and render the particle
m_pd3dDevice->SetTransform( D3DTS_WORLD, &InvViewMatrix );
m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2 );
}
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
m_pd3dDevice->SetTexture( 0, NULL );
return S_OK;