particles.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. #include "particles.h"
  3. #include "Renderer.h"
  4. #include "Util.h"
  5. using namespace std;
  6. #define MIN 0
  7. #define MAX 1
  8. #define VEL0 0
  9. #define VEL1 1
  10. =============================================================================================================================================================
  11. render @ particle_t =
  12. =============================================================================================================================================================
  13. void particle_t::render()
  14. {
  15. if( !life ) return;
  16. //glPointSize( 4.0 );
  17. --life;
  18. // calc new pos
  19. float dt = ( float(App::timerTick)/1000 );
  20. Vec3 s_vel; // sigma velocity
  21. s_vel = Vec3( 0.0, 0.0, 0.0 );
  22. for( int i=0; i<PARTICLE_VELS_NUM; i++ )
  23. {
  24. velocity[i] = (acceleration[i] * dt) + velocity[i];
  25. s_vel += velocity[i];
  26. }
  27. translationLspace = s_vel * dt + translationLspace;
  28. Quat q;
  29. q.setFrom2Vec3( Vec3(1.0, 0.0, 0.0), s_vel );
  30. rotationLspace = Mat3( q );
  31. updateWorldTransform();
  32. // draw the point
  33. glColor3f( 1.0, 0.0, 0.0 );
  34. glBegin( GL_POINTS );
  35. glVertex3fv( &translationWspace[0] );
  36. glEnd();
  37. // draw axis
  38. if( 1 )
  39. {
  40. glPushMatrix();
  41. R::multMatrix( transformationWspace );
  42. glBegin( GL_LINES );
  43. // x-axis
  44. glColor3f( 1.0, 0.0, 0.0 );
  45. glVertex3f( 0.0, 0.0, 0.0 );
  46. glVertex3f( 1.0, 0.0, 0.0 );
  47. // y-axis
  48. glColor3f( 0.0, 1.0, 0.0 );
  49. glVertex3f( 0.0, 0.0, 0.0 );
  50. glVertex3f( 0.0, 1.0, 0.0 );
  51. // z-axis
  52. glColor3f( 0.0, 0.0, 1.0 );
  53. glVertex3f( 0.0, 0.0, 0.0 );
  54. glVertex3f( 0.0, 0.0, 1.0 );
  55. glEnd();
  56. glPopMatrix();
  57. }
  58. // draw the velocities
  59. if( 0 )
  60. {
  61. // draw the velocities
  62. for( int i=0; i<PARTICLE_VELS_NUM; i++ )
  63. {
  64. glColor3f( 0.0, 0.0, 1.0 );
  65. glBegin( GL_LINES );
  66. glVertex3fv( &translationWspace[0] );
  67. glVertex3fv( &(velocity[i]+translationWspace)[0] );
  68. glEnd();
  69. }
  70. // draw the Sv
  71. glColor3f( 1.0, 1.0, 1.0 );
  72. glBegin( GL_LINES );
  73. glVertex3fv( &translationWspace[0] );
  74. glVertex3fv( &(s_vel+translationWspace)[0] );
  75. glEnd();
  76. }
  77. }
  78. =============================================================================================================================================================
  79. init @ particle_emitter_t =
  80. =============================================================================================================================================================
  81. void particle_emitter_t::init()
  82. {
  83. particles.resize(200);
  84. particle_life[0] = 200;
  85. particle_life[1] = 400;
  86. particles_per_frame = 1;
  87. velocities[VEL0].angs[MIN] = Euler( 0.0, toRad(-30.0), toRad(10.0) );
  88. velocities[VEL0].angs[MAX] = Euler( 0.0, toRad(30.0), toRad(45.0) );
  89. velocities[VEL0].magnitude = 5.0f;
  90. velocities[VEL0].acceleration_magnitude = -0.1f;
  91. velocities[VEL0].rotatable = true;
  92. velocities[VEL1].angs[MIN] = Euler( 0.0, 0.0, toRad(270.0) );
  93. velocities[VEL1].angs[MAX] = Euler( 0.0, 0.0, toRad(270.0) );
  94. velocities[VEL1].magnitude = 0.0f;
  95. velocities[VEL1].acceleration_magnitude = 1.0f;
  96. velocities[VEL1].rotatable = false;
  97. particles_translation_lspace[0] = Vec3( 0.0, 0.0, 0.0 );
  98. particles_translation_lspace[1] = Vec3( 0.0, 0.0, 0.0 );
  99. }
  100. ==============================================================================================================================================================
  101. ReInitParticle @ particle_emitter_t =
  102. ==============================================================================================================================================================
  103. void particle_emitter_t::ReInitParticle( particle_t& particle )
  104. {
  105. // life
  106. particle.life = Util::randRange( particle_life[MIN], particle_life[MAX] );
  107. // pos
  108. particle.translationLspace = Vec3(
  109. Util::randRange( particles_translation_lspace[MIN].x, particles_translation_lspace[MAX].x ),
  110. Util::randRange( particles_translation_lspace[MIN].y, particles_translation_lspace[MAX].y ),
  111. Util::randRange( particles_translation_lspace[MIN].z, particles_translation_lspace[MAX].z )
  112. );
  113. particle.rotationLspace = Mat3::getIdentity();
  114. particle.scaleLspace = 1.0;
  115. particle.parent = this;
  116. particle.updateWorldTransform();
  117. particle.translationLspace = particle.translationWspace;
  118. particle.rotationLspace = particle.rotationWspace;
  119. particle.scaleLspace = particle.scaleWspace;
  120. particle.parent = NULL;
  121. // initial velocities
  122. for( int i=0; i<PARTICLE_VELS_NUM; i++ )
  123. {
  124. Euler tmp_angs = Euler(
  125. Util::randRange( velocities[i].angs[MIN].x, velocities[i].angs[MAX].x ),
  126. Util::randRange( velocities[i].angs[MIN].y, velocities[i].angs[MAX].y ),
  127. Util::randRange( velocities[i].angs[MIN].z, velocities[i].angs[MAX].z )
  128. );
  129. Mat3 m3;
  130. m3 = Mat3(tmp_angs);
  131. if( velocities[i].rotatable ) m3 = rotationWspace * m3;
  132. particle.velocity[i] = particle.acceleration[i] = m3 * Vec3( 1.0, 0.0, 0.0 );
  133. particle.velocity[i] *= velocities[i].magnitude;
  134. particle.acceleration[i] *= velocities[i].acceleration_magnitude;
  135. }
  136. }
  137. =============================================================================================================================================================
  138. render @ particle_emitter_t =
  139. =============================================================================================================================================================
  140. void particle_emitter_t::render()
  141. {
  142. updateWorldTransform();
  143. // emitt new particles
  144. int remain = particles_per_frame;
  145. for( uint i=0; i<particles.size(); i++ )
  146. {
  147. if( particles[i].life != 0 ) continue;
  148. ReInitParticle( particles[i] );
  149. --remain;
  150. if( remain == 0 ) break;
  151. }
  152. // render all particles
  153. for( uint i=0; i<particles.size(); i++ )
  154. particles[i].render();
  155. // render the debug cube
  156. if( 1 )
  157. {
  158. glEnable( GL_DEPTH_TEST );
  159. glDisable( GL_TEXTURE_2D );
  160. glDisable( GL_BLEND );
  161. //glLineWidth( 2.0 );
  162. glPolygonMode( GL_FRONT, GL_LINE );
  163. glPushMatrix();
  164. updateWorldTransform();
  165. R::multMatrix( transformationWspace );
  166. glColor3f( 0.0, 1.0, 0.0 );
  167. R::dbg::renderCube();
  168. glPolygonMode( GL_FRONT, GL_FILL );
  169. glPopMatrix();
  170. }
  171. }
  172. */