HitParticle.cpp
上传用户:hkb425
上传日期:2007-06-16
资源大小:34191k
文件大小:4k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. // HitParticle.cpp: implementation of the CHitParticle class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "HitParticle.h"
  6. #include "heightmap.h"
  7. //////////////////////////////////////////////////////////////////////
  8. // Construction/Destruction
  9. //////////////////////////////////////////////////////////////////////
  10. CHitParticle::CHitParticle()
  11. {
  12. m_numFragment=30;
  13. particle=NULL;
  14. m_Life=1;
  15. m_Fade=0;
  16. }
  17. CHitParticle::~CHitParticle()
  18. {
  19. if(particle!=NULL)delete [] particle;
  20. }
  21. void CHitParticle::InitParticle(VERTEX position ,unsigned int texSmoke,
  22.   int  smokeNumber,int fragmentNumber,
  23.   float size)
  24. {
  25. CSmokeParticle::InitParticle(texSmoke,1/float(smokeNumber),smokeNumber,size);
  26. CSmokeParticle::SetGravity(0.006f);
  27. m_pos=position;
  28. m_numFragment=fragmentNumber;
  29. if(particle==NULL)
  30.     particle = new PARTICLE [ m_numFragment ];
  31.     Reset(position);
  32. }
  33. void CHitParticle::Reset(VERTEX pos)
  34. {
  35.     CSmokeParticle::Reset();
  36. m_Life=1;
  37. m_pos=pos;
  38. for(int i=0;i<m_numFragment;i++)
  39. SetFragParticle(i);
  40. }
  41. void CHitParticle::Render()
  42. {
  43. m_Life-=0.01f;
  44.     glPushMatrix();   
  45.     glTranslated(m_pos.xpos,m_pos.ypos,m_pos.zpos);
  46.     glRotatef(CHeightmap::m_ViewRotY,  0.0f,1.0f,0.0f);
  47.     glRotatef(-CHeightmap::m_ViewRotX,  1.0f,0.0f,0.0f);
  48.   ///////////////////////////////////////////////////////
  49. CSmokeParticle::DrawParticle();
  50. DrawFragment();
  51. ///////////////////////////////////////////////////////
  52.     glPopMatrix();
  53.  }
  54. void CHitParticle::DrawFragment()
  55. {
  56. //  glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  57. glDisable(GL_BLEND);
  58.  
  59. glDisable(GL_TEXTURE_2D);
  60.     
  61. for(int i=0;i<m_numFragment;i++)
  62. {
  63.         if (particle[i].life > 0 )
  64.         {
  65.             particle[i].pos.xpos += particle[i].normal.nx;
  66.             particle[i].pos.ypos += particle[i].normal.ny;
  67.             particle[i].pos.zpos += particle[i].normal.nz;
  68.             particle[i].normal.ny -= 0.02f;  // cheap simulated gravity
  69.             particle[i].normal.nx *= 0.9f;
  70.             particle[i].normal.ny *= 0.90f;
  71.             particle[i].normal.nz *= 0.9f;
  72.             particle[i].life -= particle[i].fade;
  73. particle[i].rotate +=10.0f;
  74.             glColor4f(0,0,0,particle[i].life);
  75. glPushMatrix();
  76.           glTranslatef(particle[i].pos.xpos, particle[i].pos.ypos, particle[i].pos.zpos);
  77.               glRotated(particle[i].rotate,1,1,1);
  78.           glTranslatef(-particle[i].pos.xpos, -particle[i].pos.ypos, -particle[i].pos.zpos);
  79.             glBegin(GL_TRIANGLES);
  80.               glTexCoord2d(1,1); 
  81.   glVertex3d( particle[i].pos.xpos,particle[i].pos.ypos,particle[i].pos.zpos);
  82.               glTexCoord2d(1,0); 
  83.   glVertex3d( particle[i].pos.xpos+particle[i].size,particle[i].pos.ypos,particle[i].pos.zpos);
  84.               glTexCoord2d(0,0); 
  85.   glVertex3d(particle[i].pos.xpos,particle[i].pos.ypos+particle[i].size,particle[i].pos.zpos);
  86.             glEnd();
  87. glPopMatrix();
  88. }
  89. }
  90. }
  91. void CHitParticle::SetFragParticle(int i)
  92. {
  93. if(i>(m_numFragment-1))return;
  94. if(m_Life<0)return ;
  95. /////////////////////////////////////////
  96.    particle[i].size =m_size*( (rand()%50)+20 )*0.025f; 
  97.     particle[i].pos.xpos = m_size*((rand()%40)*0.1f-2)*particle[i].size;//  /75.f;
  98.     particle[i].pos.ypos = m_size*((rand()%40)*0.1f-2)*particle[i].size;
  99.     particle[i].pos.zpos = m_size*((rand()%40)*0.1f-2)*particle[i].size;
  100.     particle[i].rotate = float(rand()%100-50);
  101.     particle[i].normal.nx = float((rand()%50)-25)*0.01f;
  102.     particle[i].normal.ny = float((rand()%50)-0)*0.02f+0.01f;
  103.     particle[i].normal.nz = float((rand()%50)-25)*0.01f;
  104.     particle[i].life = 1;
  105.     particle[i].fade =float(rand()%100)/50000 + 0.01f;
  106. }