|
很简单的粒子系统的初始化,更新,和渲染,但是不知道为什么会有堆栈错误,我检查了一个小时了,还是没有发现问题!
另外链表是我自己写的,对<LIST>不熟悉,不敢用!!谢谢了!!
struct CUS_VERTEX
{
D3DXVECTOR3 pos;
D3DCOLOR color;
};
const DWORD FVF = (D3DFVF_XYZ | D3DFVF_DIFFUSE);
struct PARTICLE
{
D3DXVECTOR3 p_pos;
D3DXVECTOR3 p_vel;
D3DCOLOR p_color;
PARTICLE *p_next;
};
PARTICLE *particle;
PARTICLE* initParticle(int numParticle)
{
PARTICLE *particleHead,*particleTemp,*particleEnd;
particleHead=particleTemp=(PARTICLE*)malloc(sizeof(PARTICLE));
particleTemp->p_pos=D3DXVECTOR3(-5.0f,-4.0f,0.0f);
particleTemp->p_vel=D3DXVECTOR3((float)rand()/RAND_MAX,(float)rand()/RAND_MAX,(float)rand()/RAND_MAX);
particleTemp->p_color=D3DCOLOR_ARGB(255,23,25,155);
particleHead->p_next=particleTemp;
if(numParticle==1)
{
particleTemp->p_next=NULL;;
}
else
{
do
{
particleEnd=(PARTICLE*)malloc(sizeof(PARTICLE));
particleTemp->p_next=particleEnd;
particleTemp=particleEnd;
particleTemp->p_pos=D3DXVECTOR3(3.0f,0.0f,0.0f);
particleTemp->p_vel=D3DXVECTOR3((float)rand()/RAND_MAX,(float)rand()/RAND_MAX,(float)rand()/RAND_MAX);
particleTemp->p_color=D3DCOLOR_ARGB(255,23,25,155);
numParticle--;
}while(numParticle>1);
particleTemp->p_next=NULL;
}
return particleHead;
}
void updateParticle(PARTICLE *tempParticle,int numParticle)
{
PARTICLE *particle,*particlet;
particle=tempParticle->p_next;
for(int i=0;i<numParticle;i++)
{
particle->p_pos+=particle->p_vel*0.01f;
particlet=particle->p_next;
particle=particlet;
}
}
void renderParticle(PARTICLE *tempParticle,int numParticle)
{
g_pd3ddevice->SetRenderState( D3DRS_ZWRITEENABLE, FALSE );
g_pd3ddevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
g_pd3ddevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );
// enable pointsprite render states
// turn on pointsprites
g_pd3ddevice->SetRenderState( D3DRS_POINTSPRITEENABLE, TRUE );
// enable scaling
g_pd3ddevice->SetRenderState( D3DRS_POINTSCALEENABLE, TRUE );
// the point size to use when the vertex does not include this information
g_pd3ddevice->SetRenderState( D3DRS_POINTSIZE, FLOAT_TO_DWORD(1.0f) );
// the minimum size of the points
g_pd3ddevice->SetRenderState( D3DRS_POINTSIZE_MIN, FLOAT_TO_DWORD(1.0f) );
// these three renderstates control the scaling of the pointsprite
g_pd3ddevice->SetRenderState( D3DRS_POINTSCALE_A, FLOAT_TO_DWORD(0.0f) );
g_pd3ddevice->SetRenderState( D3DRS_POINTSCALE_B, FLOAT_TO_DWORD(0.0f) );
g_pd3ddevice->SetRenderState( D3DRS_POINTSCALE_C, FLOAT_TO_DWORD(1.0f) );
CUS_VERTEX* tempVertex;
PARTICLE* particle;
particle=tempParticle->p_next;
Cube->Lock(0,numParticle*sizeof(CUS_VERTEX),(void**)&tempVertex,D3DLOCK_DISCARD);
for(int i=0;i<numParticle;i++)
{
tempVertex->pos=particle->p_pos;
tempVertex->color=particle->p_color;
particle=particle->p_next;
tempVertex++;
}
Cube->Unlock();
g_pd3ddevice->SetTexture( 0, Map );
g_pd3ddevice->SetStreamSource( 0, Cube, 0, sizeof(CUS_VERTEX) );
g_pd3ddevice->SetFVF( FVF );
g_pd3ddevice->DrawPrimitive( D3DPT_POINTLIST, 0, numParticle );
// 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 );
} |
|