H3D API  2.4.1
X3DParticleEmitterNode.h
Go to the documentation of this file.
1 // Copyright 2004-2019, SenseGraphics AB
3 //
4 // This file is part of H3D API.
5 //
6 // H3D API is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // H3D API is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with H3D API; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // A commercial license is also available. Please contact us at
21 // www.sensegraphics.com for more information.
22 //
23 //
27 //
29 #ifndef __X3DPARTICLEEMITTERNODE_H__
30 #define __X3DPARTICLEEMITTERNODE_H__
31 
32 #include <H3D/X3DNode.h>
33 #include <H3D/SFFloat.h>
34 #include <GL/glew.h>
35 
36 #include <H3D/X3DColorNode.h>
37 #include <H3D/X3DGeometryNode.h>
38 #include <H3D/TextureCoordinate.h>
39 
40 // stl includes
41 #include <list>
42 
43 namespace H3D {
44  // forward declaration
45  class ParticleSystem;
46 
76  class H3DAPI_API X3DParticleEmitterNode : public X3DNode {
77  public:
78 
80  X3DParticleEmitterNode( Inst< SFNode > _metadata = 0,
81  Inst< SFFloat > _speed = 0,
82  Inst< SFFloat > _variation = 0,
83  Inst< SFFloat > _mass = 0,
84  Inst< SFFloat > _surfaceArea = 0,
85  Inst< SFFloat > _angularSpeed = 0 );
86 
87 
88  class H3DAPI_API Particle {
89  public:
90  typedef enum {
91  LINE,
92  POINT,
93  QUAD,
94  SPRITE,
95  TRIANGLE,
96  GEOMETRY
97  } ParticleType;
98 
100  typedef RefCountedClass UserData;
101 
103  Particle( const Vec3f &_position = Vec3f( 0, 0, 0 ),
104  const Vec3f &_velocity = Vec3f( 0, 0, 0 ),
105  const Vec2f &_size = Vec2f( 0, 0 ),
106  H3DFloat _mass = 0,
107  H3DFloat _surface_area = 0,
108  const Vec3f &_rotation_axis = Vec3f( 0, 0, 1 ),
109  H3DFloat _rotation_angle = 0,
110  H3DFloat _angular_speed = 0 ):
111  mass( _mass ),
112  surface_area( _surface_area ),
113  position( _position ),
114  velocity( _velocity ),
115  rotation_axis( _rotation_axis ),
116  rotation_angle( _rotation_angle ),
117  angular_speed( _angular_speed ),
118  new_particle( true ),
119  time_lived( 0 ),
120  total_time_to_live(0),
121  size( _size ),
122  type( LINE ),
123  geometry( NULL ),
124  distance_from_viewer( 0 ),
125  user_data ( NULL ) {}
126 
127  inline void updateParticle( const Matrix4f &_global_to_local,
128  Vec3f vp_pos_local,
129  Vec3f /*vp_up_local*/,
130  Vec3f vp_look_at_local,
131  H3DTime dt ) {
132  global_to_local = _global_to_local;
133  position += velocity * dt;
134  rotation_angle += angular_speed * H3DFloat(dt);
135  //if ( rotation_angle > 2*Constants::pi ) rotation_angle -= 2*Constants::pi;
136  time_lived += dt;
137  distance_from_viewer = vp_look_at_local * ( position - vp_pos_local );
138  //distance_from_viewer = ( position - vp_pos_local ).lengthSqr();
139  }
140 
144  int lookupKey( H3DFloat f, H3DFloat &w, const vector< H3DFloat > &keys ) {
145  w = 0;
146  if ( f <= keys[0] ) {
147  w = 0;
148  return 0;
149  }
150  if ( f >= keys[keys.size()-1] ) {
151  w = 1;
152  return (int)keys.size() - 1;
153  }
154 
155  // we know here that f is larger than keys[0] so we know that we will
156  // go into the if-statement at some point.
157  for( size_t i = keys.size()-1; true; --i) {
158  if ( f > keys[i] ) {
159  w = (f-keys[i])/(keys[i+1]-keys[i]);
160  if( w < 0 ) w = 0;
161  if( w > 1 ) w = 1;
162  return (int) i;
163  }
164  }
165  return -1; // something must have gone wrong to get here
166  }
167 
168  void render( ParticleSystem *ps );
169 
170  inline bool isDead() {
171  return time_lived > total_time_to_live;
172  }
173 
174  bool operator<( const Particle &p ) const {
175  return distance_from_viewer > p.distance_from_viewer;
176  }
177 
183  void setUserData ( UserData* _data ) {
184  user_data.reset ( _data );
185  }
186 
187  UserData* getUserData () const {
188  return user_data.get();
189  }
190 
191  H3DFloat mass;
192  H3DFloat surface_area;
193  Vec3f position;
194  Vec3f velocity;
195  Vec3f rotation_axis;
196  H3DFloat rotation_angle;
197  H3DFloat angular_speed;
198 
199 
200  bool new_particle;
201 
202  H3DTime time_lived;
203  // the total lifetime of the particle
204  H3DTime total_time_to_live;
205  Vec2f size;
206  ParticleType type;
208  Matrix4f global_to_local;
209  H3DFloat distance_from_viewer;
210  protected:
211  void renderTexCoord( const Vec3f &tc );
212  void renderTexCoord( unsigned int i, X3DTextureCoordinateNode *tc );
213 
214  AutoRef < UserData > user_data;
215  };
216 
217  virtual void generateParticles( ParticleSystem *ps,
218  H3DTime last_time,
219  H3DTime now,
220  std::list< Particle > &particles ) = 0;
221 
224  virtual string defaultXMLContainerField() {
225  return "emitter";
226  }
227 
235  H3DUniquePtr< SFFloat > speed;
236 
244  H3DUniquePtr< SFFloat > variation;
245 
252  H3DUniquePtr< SFFloat > mass;
253 
263  H3DUniquePtr< SFFloat > surfaceArea;
264 
275  H3DUniquePtr< SFFloat > angularSpeed;
276 
279 
280  protected:
281  Particle newParticle( ParticleSystem *ps, const Vec3f &pos, const Vec3f &dir );
282  };
283 }
284 
285 #endif
Contains the SFFloat field class.
Header file for TextureCoordinate, X3D scene-graph node.
Header file for X3DColorNode, X3D scene-graph node.
Header file for X3DGeometryNode, X3D scene-graph node.
Header file for X3DNode, X3D scene-graph node.
The ParticleSystem node specifies a complete particle system.
Definition: ParticleSystem.h:184
This abstract node type is the base type for all nodes in the X3D system.
Definition: X3DNode.h:65
The X3DParticleEmitterNode abstract type represents any node that is an emitter of particles.
Definition: X3DParticleEmitterNode.h:76
H3DUniquePtr< SFFloat > surfaceArea
The surfaceArea field specifies the surface area of the particle in metres-squared.
Definition: X3DParticleEmitterNode.h:263
H3DUniquePtr< SFFloat > variation
The variation field specifies a multiplier for the randomness that is used to control the range of po...
Definition: X3DParticleEmitterNode.h:244
H3DUniquePtr< SFFloat > mass
The mass field specifies the basic mass of each particle in kilograms.
Definition: X3DParticleEmitterNode.h:252
virtual string defaultXMLContainerField()
Returns the default xml containerField attribute value.
Definition: X3DParticleEmitterNode.h:224
H3DUniquePtr< SFFloat > angularSpeed
The angularSpeed field specifies an initial rotational speed that will be imparted to all particles.
Definition: X3DParticleEmitterNode.h:275
static H3DNodeDatabase database
The H3DNodeDatabase for this node.
Definition: X3DParticleEmitterNode.h:278
H3DUniquePtr< SFFloat > speed
The speed field specifies an initial linear speed that will be imparted to all particles.
Definition: X3DParticleEmitterNode.h:235
This abstract node type is the base type for all node types which specify texture coordinates.
Definition: X3DTextureCoordinateNode.h:55
Vec2f()
float H3DFloat
double H3DTime
H3D API namespace.
Definition: Anchor.h:38
The H3DNodeDatabase contains a mapping between a name of a Node and the constructor for the Node with...
Definition: H3DNodeDatabase.h:194