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

游戏引擎

开发平台:

Visual C++

  1. // SmokeParticle.cpp: implementation of the CSmokeParticle class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "SmokeParticle.h"
  6. //////////////////////////////////////////////////////////////////////
  7. // Construction/Destruction
  8. //////////////////////////////////////////////////////////////////////
  9. CSmokeParticle::CSmokeParticle()
  10. {
  11. m_numParticle=50;
  12. m_smokeLife=1;
  13. m_smokeFade=0;
  14. m_size     =1;
  15. particle=NULL;
  16. m_color[0]=0.6f;
  17. m_color[1]=0.5f;
  18. m_color[2]=0.1f;
  19. m_gravity=0;
  20. }
  21. CSmokeParticle::~CSmokeParticle()
  22. {
  23. if(particle!=NULL)delete [] particle;
  24. }
  25. void CSmokeParticle::InitParticle(unsigned int texid,float fade,int  particleNumber,float size)
  26. {
  27. m_numParticle = particleNumber;
  28. m_smokeFade   = fade;
  29. m_size        = size;
  30.     m_texID       = texid;
  31.     
  32. if(particle==NULL)
  33.     particle = new PARTICLE [ m_numParticle ];
  34.     Reset();
  35. }
  36. void CSmokeParticle::SetSmokeSize(float size)
  37. {
  38. m_size = size;
  39. }
  40. void CSmokeParticle::SetSmokeColor(float red,float green,float blue)
  41. {
  42. m_color[0]=red;
  43. m_color[1]=green;
  44. m_color[2]=blue;
  45. }
  46. void CSmokeParticle::SetGravity(float gravity)
  47. {
  48.     m_gravity=gravity;
  49. }
  50. void CSmokeParticle::Reset()
  51. {
  52. m_smokeLife=1;
  53. for(int i=0;i<m_numParticle;i++)
  54. SetSmokeParticle(i);
  55. }
  56. void CSmokeParticle::DrawParticle()
  57. {
  58.   glBindTexture(GL_TEXTURE_2D,m_texID);
  59. glEnable(GL_TEXTURE_2D);
  60. glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  61. // glBlendFunc(GL_SRC_ALPHA,GL_ONE);
  62.     glEnable(GL_BLEND);
  63. for(int i=0;i<m_numParticle;i++)
  64. {
  65.         if (particle[i].life > 0)
  66.         {
  67.             particle[i].pos.xpos += particle[i].normal.nx;
  68.             particle[i].pos.ypos += particle[i].normal.ny;
  69.             particle[i].normal.nx *= 1.02f;
  70.             particle[i].normal.ny -= m_gravity; 
  71.             particle[i].normal.ny *= 0.94f;
  72.             particle[i].size *=1.02f;
  73.             particle[i].life -= particle[i].fade;
  74.     
  75.             glColor4d(m_color[0],m_color[1],m_color[2],particle[i].life);
  76.             glBegin(GL_QUADS);
  77.               glTexCoord2d(1,1); 
  78.   glVertex3d( particle[i].pos.xpos+particle[i].size,particle[i].pos.ypos+ particle[i].size,particle[i].pos.zpos);
  79.               glTexCoord2d(1,0); 
  80.   glVertex3d( particle[i].pos.xpos+particle[i].size,particle[i].pos.ypos-particle[i].size,particle[i].pos.zpos);
  81.               glTexCoord2d(0,0); 
  82.   glVertex3d(particle[i].pos.xpos-particle[i].size,particle[i].pos.ypos-particle[i].size,particle[i].pos.zpos);
  83.               glTexCoord2d(0,1); 
  84.   glVertex3d(particle[i].pos.xpos-particle[i].size,particle[i].pos.ypos+ particle[i].size,particle[i].pos.zpos);
  85.           glEnd();
  86. }
  87. else SetSmokeParticle(i);
  88. }
  89.     glColor3f(1,1,1); 
  90.   glDisable(GL_BLEND);
  91. }
  92. void CSmokeParticle::SetSmokeParticle(int num)
  93. {
  94. if(num>(m_numParticle-1))return;
  95. m_smokeLife -= m_smokeFade;
  96. if(m_smokeLife<0)return ;
  97. particle[num].size =m_size*( (rand()%100)+200 )*0.01f;
  98.     particle[num].pos.xpos = 0;//  /75.f;
  99.     particle[num].pos.ypos = 0;
  100.     particle[num].pos.zpos = m_size * (num-m_numParticle/2)*0.1f;
  101.     particle[num].normal.nx = m_size * ((rand()%50)-25)*0.002f;
  102.     particle[num].normal.ny = m_size * ((rand()%50)+8)*0.04f;
  103. particle[num].fade =float(rand()%100)*0.0001f + 0.015f;
  104.     particle[num].life = 1;
  105. }