Just a little question.
How can i stop rendering a particle in this code? When its life is 0?
#include "DarkGDK.h"
#include "d3d9.h"
#include "d3dx9.h"
//for 3d functions
LPDIRECT3DVERTEXBUFFER9 g_pVertexBuffer = NULL;
LPDIRECT3DTEXTURE9 g_pTexture = NULL;
IDirect3DDevice9* g_pd3dDevice = NULL;
D3DVIEWPORT9 g_pViewPort;
struct Vertex
{
D3DXVECTOR3 posit;
D3DCOLOR color;
enum FVF
{
FVF_Flags = D3DFVF_XYZ|D3DFVF_DIFFUSE
};
};
struct Particle
{
D3DXVECTOR3 m_vCurPos;
D3DXVECTOR3 m_vCurVel;
D3DCOLOR m_vColor;
int life;
};
const int MAX_PARTICLES = 15000;
Particle g_particles[MAX_PARTICLES];
// Helper function to stuff a FLOAT into a DWORD argument
inline DWORD FtoDW( FLOAT f ) { return *((DWORD*)&f); }
float time;
float getRandomMinMax( float fMin, float fMax )
{
float fRandNum = (float)rand () / RAND_MAX;
return fMin + (fMax - fMin) * fRandNum;
}
//-----------------------------------------------------------------------------
// Name: initPointSprites()
// Desc:
//-----------------------------------------------------------------------------
void initPointSprites( void )
{
g_pd3dDevice = dbGetDirect3DDevice();
g_pd3dDevice->GetViewport( &g_pViewPort );
// Load up the point sprite's texture...
D3DXCreateTextureFromFile( g_pd3dDevice, "particle.lo", &g_pTexture );
g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
// Create a vertex bufer which can be used with point sprites...
g_pd3dDevice->CreateVertexBuffer( 20048 * sizeof(Vertex),
D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY | D3DUSAGE_POINTS,
Vertex::FVF_Flags, D3DPOOL_DEFAULT,
&g_pVertexBuffer, NULL );
// If you want to know the max size that a point sprite can be set to,
// and whether or not you can change the size of point sprites in hardware
// by sending D3DFVF_PSIZE with the FVF, do this.
float fMaxPointSize = 0.0f;
bool bDeviceSupportsPSIZE = false;
D3DCAPS9 d3dCaps;
g_pd3dDevice->GetDeviceCaps( &d3dCaps );
fMaxPointSize = d3dCaps.MaxPointSize;
if( d3dCaps.FVFCaps & D3DFVFCAPS_PSIZE )
bDeviceSupportsPSIZE = true;
else
bDeviceSupportsPSIZE = false;
// Initialize our particles so they'll start at the origin with some
// random direction and color.
for( int i = 0; i < MAX_PARTICLES; ++i )
{
g_particles[i].m_vCurPos = D3DXVECTOR3(dbRnd(200),dbRnd(200),dbRnd(200));
g_particles[i].m_vCurVel = D3DXVECTOR3(dbRnd(1),dbRnd(1),dbRnd(1));
g_particles[i].m_vColor = D3DCOLOR_COLORVALUE( getRandomMinMax( 0.0f, 1.0f ),
getRandomMinMax( 0.0f, 1.0f ),
getRandomMinMax( 0.0f, 1.0f ),
1.0f );
g_particles[i].life=2500;
}
}
void updatePointSprites( void )
{
//
// To repeat the sample automatically, keep track of the overall app time.
//
static double dStartAppTime = timeGetTime();
float fElpasedAppTime = (float)((timeGetTime() - dStartAppTime) * 0.001);
//
// To move the particles via their velocity, keep track of how much time
// has elapsed since last frame update...
//
static double dLastFrameTime = timeGetTime();
double dCurrenFrameTime = timeGetTime();
double dElpasedFrameTime = (float)((dCurrenFrameTime - dLastFrameTime) * 0.001);
dLastFrameTime = dCurrenFrameTime;
//
// Move each particle via its velocity and elapsed frame time.
//
for( int i = 0; i < MAX_PARTICLES; ++i )
{
g_particles[i].life--;
g_particles[i].m_vCurPos += g_particles[i].m_vCurVel * (float)dElpasedFrameTime;
}
}
//-----------------------------------------------------------------------------
// Name: renderPointSprites()
// Desc:
//-----------------------------------------------------------------------------
void renderPointSprites( void )
{
//
// Setting D3DRS_ZWRITEENABLE to FALSE makes the Z-Buffer read-only, which
// helps remove graphical artifacts generated from rendering a list of
// particles that haven't been sorted by distance to the eye.
//
// Setting D3DRS_ALPHABLENDENABLE to TRUE allows particles, which overlap,
// to alpha blend with each other correctly.
//
g_pd3dDevice->SetTexture( 0, g_pTexture );
g_pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE, FALSE );
g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
g_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );
//
// Set up the render states for using point sprites...
//
g_pd3dDevice->SetRenderState( D3DRS_POINTSPRITEENABLE, TRUE ); // Turn on point sprites
g_pd3dDevice->SetRenderState( D3DRS_POINTSCALEENABLE, TRUE ); // Allow sprites to be scaled with distance
g_pd3dDevice->SetRenderState( D3DRS_POINTSIZE, FtoDW(3.0) ); // Float value that specifies the size to use for point size computation in cases where point size is not specified for each vertex.
g_pd3dDevice->SetRenderState( D3DRS_POINTSIZE_MIN, FtoDW(1.0f) ); // Float value that specifies the minimum size of point primitives. Point primitives are clamped to this size during rendering.
g_pd3dDevice->SetRenderState( D3DRS_POINTSCALE_A, FtoDW(0.0f) ); // Default 1.0
g_pd3dDevice->SetRenderState( D3DRS_POINTSCALE_B, FtoDW(0.0f) ); // Default 0.0
g_pd3dDevice->SetRenderState( D3DRS_POINTSCALE_C, FtoDW(1.0f) ); // Default 0.0
//
// Lock the vertex buffer, and set up our point sprites in accordance with
// our particles that we're keeping track of in our application.
//
Vertex *pPointVertices;
g_pVertexBuffer->Lock( 0, MAX_PARTICLES * sizeof(Vertex),
(void**)&pPointVertices, D3DLOCK_DISCARD );
for( int i = 0; i < MAX_PARTICLES; ++i )
{
if(g_particles[i].life>0)
{
pPointVertices->posit = g_particles[i].m_vCurPos;
pPointVertices->color = g_particles[i].m_vColor;
}
else
pPointVertices->color=0;
pPointVertices++;
}
g_pVertexBuffer->Unlock();
//
// Render point sprites...
//
g_pd3dDevice->SetStreamSource( 0, g_pVertexBuffer, 0, sizeof(Vertex) );
g_pd3dDevice->SetFVF( Vertex::FVF_Flags );
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
g_pd3dDevice->DrawPrimitive( D3DPT_POINTLIST, 0, MAX_PARTICLES );
//
// Reset render states...
//
g_pd3dDevice->SetRenderState( D3DRS_POINTSPRITEENABLE, FALSE );
g_pd3dDevice->SetRenderState( D3DRS_POINTSCALEENABLE, FALSE );
g_pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE, TRUE );
g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
}
They asked me for a proof.
Now i am making one........