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

游戏引擎

开发平台:

Visual C++

  1. // Particle.cpp: implementation of the CParticle class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Particle.h"
  6. #include "Texture.h"
  7. //////////////////////////////////////////////////////////////////////
  8. // Construction/Destruction
  9. //////////////////////////////////////////////////////////////////////
  10. unsigned int *CParticle::m_pTexResource=NULL;
  11. CParticle::CParticle()
  12. {
  13.     particle=NULL;
  14. maxnum=100;
  15. Type=-1;
  16. }
  17. bool  CParticle::InitParticle(int type,VERTEX startPos,unsigned int *texResource)
  18. {
  19. if(m_pTexResource==NULL)m_pTexResource=texResource;
  20. if(Type<0)Type=type;
  21. switch(Type)
  22. {
  23. case RIFLE_P:
  24. maxnum=40;
  25. break;
  26. case EXPLOSION_P:
  27. maxnum=100;
  28. break;
  29. case ROCKET_P:
  30. maxnum=50;
  31. break;
  32. case SMOKE_P:
  33. maxnum=60;
  34. break;
  35. }
  36. if(particle==NULL)
  37. {
  38.     particle=new PARTICLE[maxnum];
  39. }
  40. switch(Type)
  41. {
  42. case RIFLE_P:
  43. InitShotParticle();
  44. break;
  45. case EXPLOSION_P:
  46. InitExpParticle();
  47. break;
  48. case ROCKET_P:
  49. InitBulletParticle( startPos);
  50. break;
  51. case SMOKE_P:
  52. InitSmokeParticle( );
  53. break;
  54. }
  55.     return true;
  56. }
  57. void CParticle::DrawParticles(VERTEX startPos)
  58. {
  59. switch(Type)
  60. {
  61. case RIFLE_P:
  62. DrawShotParticles();
  63. break;
  64. case EXPLOSION_P:
  65. DrawExpParticles( );
  66. break;
  67. case ROCKET_P:
  68. DrawBulletParticles( startPos);
  69. break;
  70. case SMOKE_P:
  71. DrawSmokeParticles();
  72. break;
  73. }
  74. }
  75. bool CParticle::InitShotParticle()
  76. {
  77.     smokenum=30;
  78. for(int i=0 ;i<maxnum;i++)
  79. {
  80.    particle[i].size =float(rand()%50)*0.001f+0.02f; 
  81.     particle[i].pos.xpos = float((rand()%40)*0.1f-2)*particle[i].size;//  /75.f;
  82.     particle[i].pos.ypos = float((rand()%40)*0.1f-2)*particle[i].size;
  83.     particle[i].pos.zpos = float((rand()%40)*0.1f-2)*particle[i].size;
  84.     particle[i].rotate = float(rand()%100-50);
  85.     if(i<smokenum)
  86. {
  87.         particle[i].normal.nx = float((rand()%50)-25)*0.001f;
  88.         particle[i].normal.ny = float((rand()%50)-0 )*0.005f+0.3f;
  89.         particle[i].normal.nz = float((rand()%50)-25)*0.001f;
  90.     particle[i].size =float((rand()%20)*0.01f+0.3f);
  91. }
  92. else
  93. {
  94.         particle[i].normal.nx = float((rand()%50)-25)*0.02f;
  95.         particle[i].normal.ny = float((rand()%50)-0 )*0.01f;
  96.         particle[i].normal.nz = float((rand()%50)-25)*0.02f;
  97. }
  98.     particle[i].life = 1;
  99.     particle[i].fade =float(rand()%100)/50000 + 0.01f;
  100. }
  101. glReadPixels(402,302,5,5,GL_RGB,GL_FLOAT,srccolor);
  102.     srccolor[26][0]=0;
  103.     srccolor[26][1]=0;
  104.     srccolor[26][2]=0;
  105.     for( i=0;i<25;i++)
  106. {
  107. srccolor[26][0]+=srccolor[i][0];
  108. srccolor[26][1]+=srccolor[i][1];
  109. srccolor[26][2]+=srccolor[i][2];
  110. }
  111. srccolor[26][0]=srccolor[26][0]*0.03f;
  112. srccolor[26][1]=srccolor[26][1]*0.03f;
  113. srccolor[26][2]=srccolor[26][2]*0.03f;
  114. return true;
  115. }
  116. void CParticle::DrawShotParticles()
  117.     glDisable(GL_CULL_FACE);
  118.   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  119. glEnable(GL_BLEND);
  120. glDisable(GL_TEXTURE_2D);
  121.  
  122. for(int i=smokenum;i<maxnum;i++)
  123. {
  124.         if (particle[i].life > 0 && particle[i].pos.ypos>0)
  125.         {
  126.             particle[i].pos.xpos += particle[i].normal.nx;
  127.             particle[i].pos.ypos += particle[i].normal.ny;
  128.             particle[i].pos.zpos += particle[i].normal.nz;
  129.             particle[i].normal.ny -= 0.02f;  // cheap simulated gravity
  130.             particle[i].normal.nx *= 0.9f;
  131.             particle[i].normal.ny *= 0.99f;
  132.             particle[i].normal.nz *= 0.9f;
  133.             particle[i].life -= particle[i].fade;
  134.     
  135. // if(particle[i].rotate>0)
  136.     particle[i].rotate +=4.0f;
  137. // else
  138. //     particle[i].rotate -=4.0f;
  139.     
  140. /*            glPushMatrix();
  141.           glTranslatef(particle[i].pos.xpos, particle[i].pos.ypos, particle[i].pos.zpos);
  142.               glRotated(particle[i].rotate,1,1,1);
  143.           glTranslatef(-particle[i].pos.xpos, -particle[i].pos.ypos, -particle[i].pos.zpos);
  144.           */
  145. //            glColor4d(srccolor[26][0],srccolor[26][1],srccolor[26][2],particle[i].life);
  146.             glColor4f(0,0,0,particle[i].life);
  147.             glBegin(GL_QUADS);
  148.               glTexCoord2d(1,1); 
  149.   glVertex3d( particle[i].pos.xpos+particle[i].size,particle[i].pos.ypos+ particle[i].size,particle[i].pos.zpos);
  150.               glTexCoord2d(1,0); 
  151.   glVertex3d( particle[i].pos.xpos+particle[i].size,particle[i].pos.ypos-particle[i].size,particle[i].pos.zpos);
  152.               glTexCoord2d(0,0); 
  153.   glVertex3d(particle[i].pos.xpos-particle[i].size,particle[i].pos.ypos-particle[i].size,particle[i].pos.zpos);
  154.               glTexCoord2d(0,1); 
  155.   glVertex3d(particle[i].pos.xpos-particle[i].size,particle[i].pos.ypos+ particle[i].size,particle[i].pos.zpos);
  156.           glEnd();
  157. //         glPopMatrix();
  158.       }
  159. }
  160.     //////////////draw smoke
  161. glEnable(GL_TEXTURE_2D);
  162.   glBindTexture(GL_TEXTURE_2D,m_pTexResource[2] );
  163.     glDisable(GL_DEPTH_TEST);
  164. //    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  165. //    glBlendFunc(GL_SRC_ALPHA,GL_ONE);
  166. for( i=0;i<smokenum;i++)
  167. {
  168. if(i>(smokenum-10))glBlendFunc(GL_SRC_ALPHA,GL_ONE);
  169. else glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  170.         if (particle[i].life > 0 && particle[i].pos.ypos>0)
  171.         {
  172.             particle[i].pos.xpos += particle[i].normal.nx;
  173.             particle[i].pos.ypos += particle[i].normal.ny;
  174.             particle[i].pos.zpos += particle[i].normal.nz;
  175.             particle[i].normal.ny -= 0.005f;  // cheap simulated gravity
  176.             particle[i].normal.nx *= 0.9f;
  177.             particle[i].normal.ny *= 0.9f;
  178.             particle[i].normal.nz *= 0.9f;
  179.             particle[i].life -= particle[i].fade;
  180.     
  181.             glColor4d(0.2f,0.13f,0.0f,particle[i].life);
  182. //            glColor4d(0.95f,0.75f,0.25f,particle[i].life);
  183. //            glColor4d(srccolor[26][0],srccolor[26][1],srccolor[26][2],particle[i].life);
  184.             glBegin(GL_QUADS);
  185.               glTexCoord2d(1,1); 
  186.   glVertex3d( particle[i].pos.xpos+particle[i].size,particle[i].pos.ypos+ particle[i].size,particle[i].pos.zpos);
  187.               glTexCoord2d(1,0); 
  188.   glVertex3d( particle[i].pos.xpos+particle[i].size,particle[i].pos.ypos-particle[i].size,particle[i].pos.zpos);
  189.               glTexCoord2d(0,0); 
  190.   glVertex3d(particle[i].pos.xpos-particle[i].size,particle[i].pos.ypos-particle[i].size,particle[i].pos.zpos);
  191.               glTexCoord2d(0,1); 
  192.   glVertex3d(particle[i].pos.xpos-particle[i].size,particle[i].pos.ypos+ particle[i].size,particle[i].pos.zpos);
  193.           glEnd();
  194.       }
  195. }
  196. glDisable(GL_ALPHA_TEST);
  197.     glColor4d(1,1,1,1); 
  198. glEnable(GL_DEPTH_TEST);
  199.   glDisable(GL_BLEND);
  200. }
  201. bool CParticle::InitExpParticle()
  202. {
  203.     smokenum=30;
  204. rocknum=70;
  205. for(int i=0 ;i<maxnum;i++)
  206. {
  207. particle[i].size =float(rand()%50)*0.001f+0.05f;
  208.     
  209.     particle[i].pos.xpos = float((rand()%4)-2)*particle[i].size;//  /75.f;
  210.     particle[i].pos.ypos = float((rand()%4)-2)*particle[i].size;
  211.     particle[i].pos.zpos = float(i-maxnum/2)*0.1f;
  212.     particle[i].rotate = float(i);
  213. if(i<smokenum)
  214. {
  215.         particle[i].normal.nx = float((rand()%50)-25)*0.015f;
  216.         if(i>20)
  217. {
  218.             particle[i].normal.nx = float((rand()%50)-25)*0.002f;
  219.             particle[i].normal.ny = float((rand()%50)-0)*0.04f+0.1f;
  220. particle[i].fade =float(rand()%100)*0.00001f + 0.01f;
  221. }
  222. else
  223. {
  224.             particle[i].normal.nx = float((rand()%50)-25)*0.01f;
  225.             particle[i].normal.ny = float((rand()%50)-0)*0.08f;
  226. particle[i].fade =float(rand()%100)*0.00001f + 0.001f;
  227. }
  228.         particle[i].normal.nz = 0;//float((rand()%50)-25)*0.006f;  
  229.     particle[i].size =float((rand()%40)*0.1f+12);
  230. }
  231. else if(i<rocknum)
  232. {
  233.         particle[i].normal.nx = float((rand()%50)-25)*0.04f;
  234.         particle[i].normal.ny = float((rand()%35)-0)*0.12f;
  235.         particle[i].normal.nz = float((rand()%50)-25)*0.04f;
  236. particle[i].fade =float(rand()%100)*0.00005f + 0.0001f;
  237. particle[i].size =float(rand()%50)*0.001f+0.1f;
  238. }
  239. else
  240. {
  241.         particle[i].normal.nx = float((rand()%50)-25)*0.07f;
  242.         particle[i].normal.ny = float((rand()%35)-0)*0.8f;
  243.         particle[i].normal.nz = float((rand()%50)-25)*0.07f;
  244. particle[i].fade =float(rand()%100)*0.00005f + 0.0001f;
  245. }
  246.     particle[i].life = 1;
  247. }
  248. /*
  249. glReadPixels(402,302,5,5,GL_RGB,GL_FLOAT,srccolor);
  250.     srccolor[26][0]=0;
  251.     srccolor[26][1]=0;
  252.     srccolor[26][2]=0;
  253.     for( i=0;i<25;i++)
  254. {
  255. srccolor[26][0]+=srccolor[i][0];
  256. srccolor[26][1]+=srccolor[i][1];
  257. srccolor[26][2]+=srccolor[i][2];
  258. }
  259. srccolor[26][0]=srccolor[26][0]*0.04f;
  260. srccolor[26][1]=srccolor[26][1]*0.04f;
  261. srccolor[26][2]=srccolor[26][2]*0.04f; */
  262. return true;
  263. }
  264. void CParticle::DrawExpParticles()
  265. {
  266.     glDisable(GL_CULL_FACE);
  267.   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  268. glEnable(GL_TEXTURE_2D);
  269.   glBindTexture(GL_TEXTURE_2D,m_pTexResource[1] );
  270. for(int i=smokenum;i<maxnum;i++)
  271. {
  272.         if (particle[i].life > 0)
  273.         {
  274. if(particle[i].life > 0.4)
  275.              glDisable(GL_BLEND);
  276. else glEnable(GL_BLEND);
  277.             particle[i].pos.xpos += particle[i].normal.nx;
  278.             particle[i].pos.ypos += particle[i].normal.ny;
  279.             particle[i].pos.zpos += particle[i].normal.nz;
  280.             particle[i].normal.ny -= 0.08f;  // cheap simulated gravity
  281.             particle[i].normal.nx *= 0.99f;
  282.             particle[i].normal.ny *= 0.99f;
  283.             particle[i].normal.nz *= 0.99f;
  284.             particle[i].life -= particle[i].fade;
  285. particle[i].rotate +=6.0f;
  286.     
  287.             glPushMatrix();
  288.           glTranslatef(particle[i].pos.xpos, particle[i].pos.ypos, particle[i].pos.zpos);
  289.               glRotated(particle[i].rotate,1.f,0,0);
  290.           glTranslatef(-particle[i].pos.xpos, -particle[i].pos.ypos, -particle[i].pos.zpos);
  291.           
  292.               glColor4d(1,1,1,1);//particle[i].life);
  293.               //glColor4d(srccolor[26][0],srccolor[26][1],srccolor[26][2],particle[i].life);
  294.   /////////// rock
  295.               if(i<rocknum)
  296.   {
  297.               glBegin(GL_TRIANGLE_STRIP);
  298.                   glTexCoord2d(0,0); 
  299.                   glVertex3d(particle[i].pos.xpos-particle[i].size*2,  particle[i].pos.ypos-particle[i].size*2, particle[i].pos.zpos-particle[i].size*2);
  300.                   glTexCoord2d(0,1); 
  301.                   glVertex3d(particle[i].pos.xpos-particle[i].size*2,  particle[i].pos.ypos+particle[i].size*2, particle[i].pos.zpos-particle[i].size*2);
  302.                   glTexCoord2d(1,0); 
  303.                   glVertex3d(particle[i].pos.xpos+particle[i].size*2,  particle[i].pos.ypos-particle[i].size*1.5f, particle[i].pos.zpos-particle[i].size*2);
  304.                   glTexCoord2d(1,1); 
  305.                   glVertex3d(particle[i].pos.xpos+particle[i].size*2,  particle[i].pos.ypos+particle[i].size, particle[i].pos.zpos-particle[i].size*2);
  306.                   
  307.                   glTexCoord2d(0,0); 
  308.                   glVertex3d(particle[i].pos.xpos+particle[i].size*1.5f,  particle[i].pos.ypos-particle[i].size*1.2f, particle[i].pos.zpos+particle[i].size*1.3f);
  309.                   glTexCoord2d(0,1); 
  310.                   glVertex3d(particle[i].pos.xpos+particle[i].size*1.5f,  particle[i].pos.ypos+particle[i].size*0.3f, particle[i].pos.zpos+particle[i].size*1);
  311.                   glTexCoord2d(1,0); 
  312.                   glVertex3d(particle[i].pos.xpos,  particle[i].pos.ypos-particle[i].size*1.9f, particle[i].pos.zpos+particle[i].size*2.0f);
  313.                   glTexCoord2d(1,1); 
  314.                   glVertex3d(particle[i].pos.xpos,  particle[i].pos.ypos+particle[i].size*1.3f, particle[i].pos.zpos+particle[i].size*2.2f);
  315.                   glTexCoord2d(0,0); 
  316.                   glVertex3d(particle[i].pos.xpos-particle[i].size*2,  particle[i].pos.ypos-particle[i].size*2, particle[i].pos.zpos-particle[i].size*2);
  317.                   glTexCoord2d(0,1); 
  318.                   glVertex3d(particle[i].pos.xpos-particle[i].size*2,  particle[i].pos.ypos+particle[i].size*2, particle[i].pos.zpos-particle[i].size*2);
  319.               glEnd();
  320.               glBegin(GL_TRIANGLE_FAN);
  321.                   glTexCoord2d(0,0); 
  322.                   glVertex3d(particle[i].pos.xpos-particle[i].size*2,  particle[i].pos.ypos-particle[i].size*2, particle[i].pos.zpos-particle[i].size*2);
  323.                   glTexCoord2d(0,1); 
  324.                   glVertex3d(particle[i].pos.xpos+particle[i].size*2,  particle[i].pos.ypos-particle[i].size*1.5f, particle[i].pos.zpos-particle[i].size*2);
  325.                   
  326.                   glTexCoord2d(1,1); 
  327.                   glVertex3d(particle[i].pos.xpos+particle[i].size*1.5f,  particle[i].pos.ypos-particle[i].size*1.2f, particle[i].pos.zpos+particle[i].size*1.3f);
  328.                   glTexCoord2d(1,0); 
  329.                   glVertex3d(particle[i].pos.xpos,  particle[i].pos.ypos-particle[i].size*1.9f, particle[i].pos.zpos+particle[i].size*2.0f);
  330.               glEnd();
  331.               glBegin(GL_TRIANGLE_FAN);
  332.                   glTexCoord2d(0,0); 
  333.                   glVertex3d(particle[i].pos.xpos-particle[i].size*2,  particle[i].pos.ypos+particle[i].size*2, particle[i].pos.zpos-particle[i].size*2);
  334.                   glTexCoord2d(1,0); 
  335.                   glVertex3d(particle[i].pos.xpos+particle[i].size*2,  particle[i].pos.ypos+particle[i].size, particle[i].pos.zpos-particle[i].size*2);
  336.                   glTexCoord2d(1,1); 
  337.                   glVertex3d(particle[i].pos.xpos+particle[i].size*1.5f,  particle[i].pos.ypos+particle[i].size*0.3f, particle[i].pos.zpos+particle[i].size*1);
  338.                   glTexCoord2d(0,1); 
  339.                   glVertex3d(particle[i].pos.xpos,  particle[i].pos.ypos+particle[i].size*1.3f, particle[i].pos.zpos+particle[i].size*2.2f);
  340.               glEnd();
  341.   }else
  342.   {
  343.   
  344.               glBegin(GL_TRIANGLE_FAN);
  345.                   glTexCoord2d(1,0); 
  346.                   glVertex3d(particle[i].pos.xpos,                  particle[i].pos.ypos, particle[i].pos.zpos);
  347.                   glTexCoord2d(1,1); 
  348.   glVertex3d(particle[i].pos.xpos+particle[i].size, particle[i].pos.ypos, particle[i].pos.zpos);
  349.                   glTexCoord2d(0,1); 
  350.   glVertex3d(particle[i].pos.xpos+particle[i].size*1.2f, particle[i].pos.ypos+particle[i].size, particle[i].pos.zpos);
  351.                   glTexCoord2d(1,1); 
  352.   glVertex3d(particle[i].pos.xpos+particle[i].size*0.4f, particle[i].pos.ypos+particle[i].size*2, particle[i].pos.zpos);
  353.               glEnd();
  354.   }
  355.         glPopMatrix();
  356.       }
  357. }
  358.     //////////////draw smoke
  359. glEnable(GL_TEXTURE_2D);
  360.   glBindTexture(GL_TEXTURE_2D,m_pTexResource[2] );
  361. //    glDisable(GL_DEPTH_TEST);
  362.     glEnable(GL_BLEND);
  363. //    
  364. for( i=0;i<smokenum;i++)
  365. {
  366. if(i>20)glBlendFunc(GL_SRC_ALPHA,GL_ONE);
  367. else glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  368.         if (particle[i].life > 0 && particle[i].pos.ypos>0)
  369.         {
  370.             particle[i].pos.xpos += particle[i].normal.nx;
  371.             particle[i].pos.ypos += particle[i].normal.ny;
  372.             particle[i].pos.zpos += particle[i].normal.nz;
  373.             particle[i].normal.ny -= 0.03f;  // cheap simulated gravity
  374.             particle[i].normal.nx *= 0.9f;
  375.             particle[i].normal.ny *= 0.9f;
  376.             particle[i].normal.nz *= 0.9f;
  377.             particle[i].life -= particle[i].fade;
  378.     
  379. //           glColor4d(0.28f,0.27f,0.25f,particle[i].life);
  380.             glColor4d(0.6f,0.45f,0.2f,particle[i].life);
  381. //            glColor4d(srccolor[26][0],srccolor[26][1],srccolor[26][2],particle[i].life);
  382.             glBegin(GL_QUADS);
  383.               glTexCoord2d(1,1); 
  384.   glVertex3d( particle[i].pos.xpos+particle[i].size,particle[i].pos.ypos+ particle[i].size,particle[i].pos.zpos);
  385.               glTexCoord2d(1,0); 
  386.   glVertex3d( particle[i].pos.xpos+particle[i].size,particle[i].pos.ypos-particle[i].size,particle[i].pos.zpos);
  387.               glTexCoord2d(0,0); 
  388.   glVertex3d(particle[i].pos.xpos-particle[i].size,particle[i].pos.ypos-particle[i].size,particle[i].pos.zpos);
  389.               glTexCoord2d(0,1); 
  390.   glVertex3d(particle[i].pos.xpos-particle[i].size,particle[i].pos.ypos+ particle[i].size,particle[i].pos.zpos);
  391.           glEnd();
  392.       }
  393. }
  394.     glColor4d(1,1,1,1); 
  395. glEnable(GL_DEPTH_TEST);
  396.   glDisable(GL_BLEND);
  397. }
  398. /////////////////////////////////////////////////////////
  399. bool CParticle::InitBulletParticle(VERTEX startPos)
  400. {
  401.     smokenum=maxnum;
  402. for(int i=0 ;i<maxnum;i++)
  403. {
  404. particle[i].size =float(rand()%80)*0.01f+0.1f;
  405.     particle[i].pos.xpos = startPos.xpos+float((rand()%4)-2)*particle[i].size;//  /75.f;
  406.     particle[i].pos.ypos = startPos.ypos+float((rand()%4)-2)*particle[i].size;
  407.     particle[i].pos.zpos = startPos.zpos+i*0.1f;
  408.     particle[i].rotate = float(i);
  409. if(i<smokenum)
  410. {
  411.         particle[i].normal.nx = float((rand()%50)-25)*0.0f;
  412.         particle[i].normal.ny = float((rand()%25)-0)*0.0f;//+0.1f;
  413.         particle[i].normal.nz = float((rand()%50)-25)*0.0f;  
  414. particle[i].fade =float(rand()%100)*0.01f + 0.05f;
  415.     particle[i].size =float(rand()%100)*0.0005f+0.001f;
  416. }
  417. else
  418. {
  419.         particle[i].normal.nx =  float((rand()%50)-25)*0.05f;
  420.         particle[i].normal.ny =  float((rand()%50)-25)*0.05f;
  421.         particle[i].normal.nz =  float((rand()%50)-25)*0.05f;
  422. particle[i].fade =float(rand()%100)*0.003f + 0.01f;
  423. }
  424.     particle[i].life = 1;
  425. }
  426. return true;
  427. }
  428. void CParticle::DrawBulletParticles(VERTEX startPos)
  429. {
  430.     glDisable(GL_DEPTH_TEST);
  431.     glDisable(GL_CULL_FACE);
  432.   glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  433. glEnable(GL_BLEND);
  434. glEnable(GL_TEXTURE_2D);
  435.   glBindTexture(GL_TEXTURE_2D,m_pTexResource[1] );
  436. for(int i=smokenum;i<maxnum;i++)
  437. {
  438.         if (particle[i].life > 0)
  439.         {
  440.             particle[i].pos.xpos += particle[i].normal.nx;
  441.             particle[i].pos.ypos += particle[i].normal.ny;
  442.             particle[i].pos.zpos += particle[i].normal.nz;
  443.             particle[i].normal.ny -= 0.001f;  // cheap simulated gravity
  444.             particle[i].normal.nx *= 0.9f;
  445.             particle[i].normal.ny *= 0.9f;
  446.             particle[i].normal.nz *= 0.9f;
  447.             particle[i].life -= particle[i].fade;
  448. particle[i].rotate +=6.0f;
  449. //            glColor4d(particle[i].color.r,particle[i].color.g,particle[i].color.b,particle[i].life);
  450.             glColor4d(srccolor[26][0],srccolor[26][1],srccolor[26][2],particle[i].life);
  451.             glBegin(GL_QUADS);
  452.               glTexCoord2d(1,1); 
  453.   glVertex3d( particle[i].pos.xpos+particle[i].size,particle[i].pos.ypos+ particle[i].size,particle[i].pos.zpos);
  454.               glTexCoord2d(1,0); 
  455.   glVertex3d( particle[i].pos.xpos+particle[i].size,particle[i].pos.ypos-particle[i].size,particle[i].pos.zpos);
  456.               glTexCoord2d(0,0); 
  457.   glVertex3d(particle[i].pos.xpos-particle[i].size,particle[i].pos.ypos-particle[i].size,particle[i].pos.zpos);
  458.               glTexCoord2d(0,1); 
  459.   glVertex3d(particle[i].pos.xpos-particle[i].size,particle[i].pos.ypos+ particle[i].size,particle[i].pos.zpos);
  460.           glEnd();
  461. }
  462.     else ResetParticle(i,startPos);
  463. }
  464.     //////////////draw smoke
  465. glEnable(GL_TEXTURE_2D);
  466.   glBindTexture(GL_TEXTURE_2D,m_pTexResource[2] );
  467. glBlendFunc(GL_SRC_ALPHA,GL_ONE);
  468. // glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  469. glEnable(GL_BLEND);
  470. for( i=0;i<smokenum;i++)
  471. {
  472.         if (particle[i].life > 0)
  473.         {
  474.             particle[i].pos.xpos += particle[i].normal.nx;
  475.             particle[i].pos.ypos += particle[i].normal.ny;
  476.             particle[i].pos.zpos += particle[i].normal.nz;
  477.             particle[i].normal.ny += 0.006f;  // cheap simulated gravity
  478.             particle[i].normal.nx *= 0.9f;
  479.             particle[i].normal.ny *= 0.9f;
  480.             particle[i].normal.nz *= 0.9f;
  481.             particle[i].life -= particle[i].fade;
  482.     
  483. //          glColor4d(0.1f,0.1f,0.1f,particle[i].life);
  484.             glColor4d(0.2f,0.5f,0.6f,particle[i].life);          
  485. //   glColor4d(0.3f,0.23f,0.0f,particle[i].life);
  486.             glBegin(GL_QUADS);
  487.               glTexCoord2d(1,1); 
  488.   glVertex3d( particle[i].pos.xpos+particle[i].size,particle[i].pos.ypos+ particle[i].size,particle[i].pos.zpos);
  489.               glTexCoord2d(1,0); 
  490.   glVertex3d( particle[i].pos.xpos+particle[i].size,particle[i].pos.ypos-particle[i].size,particle[i].pos.zpos);
  491.               glTexCoord2d(0,0); 
  492.   glVertex3d(particle[i].pos.xpos-particle[i].size,particle[i].pos.ypos-particle[i].size,particle[i].pos.zpos);
  493.               glTexCoord2d(0,1); 
  494.   glVertex3d(particle[i].pos.xpos-particle[i].size,particle[i].pos.ypos+ particle[i].size,particle[i].pos.zpos);
  495.           glEnd();
  496. }
  497. else ResetParticle(i,startPos);
  498. }
  499.     glColor4d(1,1,1,1); 
  500. glEnable(GL_DEPTH_TEST);
  501.   glDisable(GL_BLEND);
  502. }
  503. void CParticle::ResetParticle(int num,VERTEX startPos)
  504. {
  505. if(num>(maxnum-1))return;
  506. particle[num].size =float(rand()%80)*0.01f+0.1f;
  507.     
  508.     particle[num].pos.xpos = startPos.xpos+float((rand()%4)-2)*particle[num].size;//  /75.f;
  509.     particle[num].pos.ypos = startPos.ypos+float((rand()%4)-2)*particle[num].size;
  510.     particle[num].pos.zpos = startPos.zpos+float((rand()%4)-2)*particle[num].size;
  511.     particle[num].rotate = float(num);
  512. if(num<smokenum)
  513. {
  514.         particle[num].normal.nx = float((rand()%50)-25)*0.04f;
  515.         particle[num].normal.ny = float((rand()%25)-0)*0.04f;//+0.1f;
  516.         particle[num].normal.nz = float((rand()%50)-25)*0.04f;  
  517. particle[num].fade =float(rand()%100)*0.004f + 0.02f;
  518.     particle[num].size =float(rand()%100)*0.01f+0.3f;
  519. }
  520. else
  521. {
  522.         particle[num].normal.nx =  float((rand()%50)-25)*0.05f;
  523.         particle[num].normal.ny =  float((rand()%50)-25)*0.05f;
  524.         particle[num].normal.nz =  float((rand()%50)-25)*0.05f;
  525. particle[num].fade =float(rand()%100)*0.008f + 0.015f;
  526. }
  527.     particle[num].life = 1;
  528. }
  529. /////////////////////////////////////////////////////////
  530. bool CParticle::InitSmokeParticle()
  531. {
  532. for(int i=0 ;i<maxnum;i++)
  533. {
  534.     particle[i].size =float(rand()%80)*0.01f+1;
  535.         particle[i].pos.xpos = 0;//  /75.f;
  536.         particle[i].pos.ypos = 0;
  537.         particle[i].pos.zpos = float(i-maxnum/2)*0.1f;
  538.         particle[i].normal.nx = float((rand()%50)-25)*0.004f;
  539.         particle[i].normal.ny = float((rand()%50)-0)*0.04f+0.1f;
  540. particle[i].fade =float(rand()%100)*0.003f + 0.01f;
  541.         particle[i].life = 1;
  542. }
  543. return true;
  544. }
  545. void CParticle::DrawSmokeParticles()
  546. {
  547.     //////////////draw smoke
  548. glEnable(GL_TEXTURE_2D);
  549.   glBindTexture(GL_TEXTURE_2D,m_pTexResource[3] );
  550.     glEnable(GL_BLEND);
  551. for(int i=0;i<maxnum;i++)
  552. {
  553. // if(i>20)glBlendFunc(GL_SRC_ALPHA,GL_ONE);
  554. // else 
  555. glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  556.         if (particle[i].life > 0)
  557.         {
  558.             particle[i].pos.xpos += particle[i].normal.nx;
  559.             particle[i].pos.ypos += particle[i].normal.ny;
  560. //          particle[i].pos.zpos += particle[i].normal.nz;
  561. //            particle[i].normal.ny += 0.003f;  // cheap simulated gravity
  562.             particle[i].normal.nx *= 1.01f;
  563.             particle[i].normal.ny *= 0.96f;
  564. //           particle[i].normal.nz *= 0.9f;
  565.             particle[i].size *=1.02f;
  566.             particle[i].life -= particle[i].fade;
  567.     
  568.             glColor4d(0.5f,0.5f,0.5f,particle[i].life);
  569.             glBegin(GL_QUADS);
  570.               glTexCoord2d(1,1); 
  571.   glVertex3d( particle[i].pos.xpos+particle[i].size,particle[i].pos.ypos+ particle[i].size,particle[i].pos.zpos);
  572.               glTexCoord2d(1,0); 
  573.   glVertex3d( particle[i].pos.xpos+particle[i].size,particle[i].pos.ypos-particle[i].size,particle[i].pos.zpos);
  574.               glTexCoord2d(0,0); 
  575.   glVertex3d(particle[i].pos.xpos-particle[i].size,particle[i].pos.ypos-particle[i].size,particle[i].pos.zpos);
  576.               glTexCoord2d(0,1); 
  577.   glVertex3d(particle[i].pos.xpos-particle[i].size,particle[i].pos.ypos+ particle[i].size,particle[i].pos.zpos);
  578.           glEnd();
  579. }
  580. else ResetSmokeParticle(i);
  581. }
  582.     glColor4d(1,1,1,1); 
  583.   glDisable(GL_BLEND);
  584. }
  585. void CParticle::ResetSmokeParticle(int num)
  586. {
  587. if(num>(maxnum-1))return;
  588.     particle[num].size =float(rand()%100)*0.01f+2;
  589.         particle[num].pos.xpos = 0;//  /75.f;
  590.         particle[num].pos.ypos = 0;
  591.         particle[num].pos.zpos = float(num-maxnum/2)*0.1f;
  592.         particle[num].normal.nx =-float((rand()%50)-0)*0.004f;
  593.         particle[num].normal.ny = float((rand()%50)-0)*0.04f+0.2f;
  594. particle[num].fade =float(rand()%100)*0.0001f + 0.01f;
  595.         particle[num].life = 1;
  596. }
  597. CParticle::~CParticle()
  598. {
  599. if(particle!=NULL)
  600.     delete [] particle;
  601. }