explosion.cpp
Upload User: xuczgm
Upload Date: 2022-04-25
Package Size: 8601k
Code Size: 5k
Category:

Special Effects

Development Platform:

Visual C++

  1. /****************************************************************************
  2.  Particles.h  粒子,爆炸
  3. /********************************* Includes *********************************/
  4. #include "stdafx.h"
  5. #include "explosion.h"
  6. //*****************************************************************************/
  7. CExplosion::CExplosion(int numParticles, CVector origin, float spread, GLuint texture)
  8.   : m_texture(texture), m_spread(spread), CParticleSystem(numParticles, origin)
  9. { srand(timeGetTime());
  10.   CParticleSystem::InitializeSystem();
  11.   Emit(numParticles);
  12. } // end CExplosion::Constructor
  13. //*****************************************************************************/
  14. void CExplosion::InitializeParticle(int index)//爆炸初始化
  15. { // start the particle at the sky at a random location in the emission zone
  16.   m_particleList[index].m_pos.x = m_origin.x + FRAND * m_spread;
  17.   m_particleList[index].m_pos.y = m_origin.y + FRAND * m_spread;
  18.   m_particleList[index].m_pos.z = m_origin.z + FRAND * m_spread;
  19.   // set the size of the particle
  20.   m_particleList[index].m_size = PARTICLE_SIZE + FRAND * SIZE_VARIATION;
  21.   // give the particle a random velocity
  22.   m_particleList[index].m_velocity.x = PARTICLE_VELOCITY.x + FRAND * VELOCITY_VARIATION.x;
  23.   m_particleList[index].m_velocity.y = PARTICLE_VELOCITY.y + FRAND * VELOCITY_VARIATION.y;
  24.   m_particleList[index].m_velocity.z = PARTICLE_VELOCITY.z + FRAND * VELOCITY_VARIATION.z;
  25.   m_particleList[index].m_acceleration = PARTICLE_ACCELERATION;
  26.   m_particleList[index].m_color[0] = 1.0;
  27.   m_particleList[index].m_color[1] = 0.5f + FRAND * 0.5f;///////////
  28.   m_particleList[index].m_color[2] = 0.01f;
  29.   m_particleList[index].m_color[3] = 1.0;
  30.   m_particleList[index].m_energy = 1.5f + FRAND/2.0f;///////////
  31.   m_particleList[index].m_colorDelta[0] = 0.0;
  32.   m_particleList[index].m_colorDelta[1] = -(m_particleList[index].m_color[1]/2.0f)/m_particleList[index].m_energy;
  33.   m_particleList[index].m_colorDelta[2] = 0.0;
  34.   m_particleList[index].m_colorDelta[3] = -1.0f/m_particleList[index].m_energy;
  35.   m_particleList[index].m_sizeDelta = -m_particleList[index].m_size/m_particleList[index].m_energy;
  36. } // end CExplosion::InitializeParticle
  37. /*****************************************************************************
  38.  CExplosion::Update
  39.  Update the existing particles, killing them and creating new ones as needed
  40. *****************************************************************************/
  41. void CExplosion::Update(float elapsedTime)//清除爆炸动画
  42. { for (int i = 0; i < m_numParticles; )
  43.   { // update the particle's position based on the elapsed time and velocity
  44.     m_particleList[i].m_pos = m_particleList[i].m_pos + m_particleList[i].m_velocity * elapsedTime;
  45.     m_particleList[i].m_velocity = m_particleList[i].m_velocity + m_particleList[i].m_acceleration * elapsedTime;
  46.     m_particleList[i].m_energy -= elapsedTime;
  47.     m_particleList[i].m_size += m_particleList[i].m_sizeDelta * elapsedTime;
  48.     m_particleList[i].m_color[3] += m_particleList[i].m_colorDelta[3] * elapsedTime;
  49.     m_particleList[i].m_color[1] += m_particleList[i].m_colorDelta[1] * elapsedTime;
  50.     // if the particle has hit the ground plane, kill it
  51.     if (m_particleList[i].m_energy <= 0.0)
  52.     { // move the last particle to the current positon, and decrease the count
  53.       m_particleList[i] = m_particleList[--m_numParticles];
  54.     }
  55.     else
  56.     { ++i;
  57.     }
  58.   }
  59. } // end CExplosion::Update()
  60. /*****************************************************************************
  61.  CExplosion::Render()
  62.  Draw the snowflake particles as textured quads
  63. *****************************************************************************/
  64. void CExplosion::Render()//爆炸动画
  65. { glPushAttrib(GL_CURRENT_BIT);//保存现有颜色属性 glPopAttrib();//恢复前一属性
  66.   float viewMatrix[16];
  67.   glGetFloatv(GL_MODELVIEW_MATRIX, viewMatrix);
  68.   CVector right(viewMatrix[0], viewMatrix[4], viewMatrix[8]);
  69.   CVector up(viewMatrix[1], viewMatrix[5], viewMatrix[9]);
  70.   glDepthMask(GL_FALSE);
  71.   glEnable(GL_BLEND);
  72.   glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);
  73.   glEnable(GL_TEXTURE_2D);
  74.   glBindTexture(GL_TEXTURE_2D, m_texture);
  75.   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  76.   glBegin(GL_QUADS);
  77.   for (int i = 0; i < m_numParticles; ++i)
  78.   {GLfloat size = m_particleList[i].m_size/3;
  79.    CVector pos =  m_particleList[i].m_pos;
  80.     glColor4fv(m_particleList[i].m_color);
  81.     glTexCoord2f(0.0, 0.0); glVertex3fv((pos + (right + up) * -size).v);
  82.     glTexCoord2f(1.0, 0.0); glVertex3fv((pos + (right - up) * size).v);
  83.     glTexCoord2f(1.0, 1.0); glVertex3fv((pos + (right + up) * size).v);
  84.     glTexCoord2f(0.0, 1.0); glVertex3fv((pos + (up - right) * size).v);
  85.   }
  86.   glEnd();
  87.   glDisable(GL_TEXTURE_2D);
  88.   glDisable(GL_BLEND);
  89.   glDepthMask(GL_TRUE);
  90.   glPopAttrib();//恢复前一属性
  91. } // end CExplosion::Update