source: GTP/branches/IllumWPdeliver2008dec/IlluminationWP/demos/Standalone/Hierarchical Systems Demo [OpenGL]/RESOURCES/include/My3DGraphRes/ParticleEmitter.cpp @ 3255

Revision 3255, 3.8 KB checked in by szirmay, 15 years ago (diff)
Line 
1#include ".\particleemitter.h"
2#include <random.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <fstream>
6#ifdef WIN32
7#include <windows.h>
8#endif
9
10using namespace std;
11
12ParticleEmitter::ParticleEmitter(void)
13{
14        m_Position=Vector(0,0,0);
15        m_Velocity=0.7;
16        m_TimeToLive=4000;
17        m_TTLvariation=2000;
18        m_Size=0.7;
19        m_EmissionRate=5;
20        m_Sizevariation=0.2;
21        m_isDirectionSet=false;
22        m_Direction=Vector(0,1,0);
23        m_Radius=0.7;
24        m_VelocityVariation=0.5;
25        m_Type=0;
26        m_Offset=0;
27}
28
29ParticleEmitter::~ParticleEmitter(void)
30{
31}
32
33void ParticleEmitter::setPositionsFile(char* filename)
34{
35        m_Type=1;
36
37        ifstream file;
38
39        file.open(filename);
40
41        char in[50];
42        file>>in;//particlecount
43        unsigned int count;
44        file>>count;
45        m_ScriptPositionCount=count;
46        m_ScriptPositions=new Vector[count];
47        file>>in;
48        file>>in;
49
50        for(unsigned int i=0;i<count;i++)
51        {
52                double pos;
53                file>>pos;
54                m_ScriptPositions[i].x=pos;
55                file>>pos;
56                m_ScriptPositions[i].y=pos;
57                file>>pos;
58                m_ScriptPositions[i].z=pos;
59        }
60
61        file.close();
62
63}
64
65unsigned int ParticleEmitter::Emit(unsigned int Dt,unsigned int maxcount,Particle* particles,Vector CamPos)
66{
67        //Dt= ms
68
69        //how much to emit?
70        unsigned int toemit=Dt*m_EmissionRate/1000-m_EmittedInSecond;
71       
72        if(toemit>maxcount||m_EmissionRate==0)
73        {
74                toemit=maxcount;
75        }
76        //emit particles
77        m_EmittedInSecond+=toemit;
78
79        Particle* thisParticle=particles;
80        /*
81        Halton Halton1(1,11);
82        Halton Halton2(1,23);
83        Halton Halton3(1,19);
84        */     
85        for(unsigned int i=0;i<toemit;i++)
86        {
87                Particle* newParticle=new Particle;
88                //fill the properties
89
90                //add direction
91
92                //calculate random dir
93/*     
94                double angle1=RangeRandom(-M_PI,M_PI);
95                double angle2=RangeRandom(-M_PI,M_PI);
96                       
97                Vector randpoint(cos(angle1)*cos(angle2),
98                                                        sin(angle2),
99                                                        sin(angle1)*cos(angle2));
100        */
101        /*      Vector randpoint((double)Halton1.Random(),
102                                                        (double)Halton2.Random(),
103                                                        (double)Halton3.Random());
104*/
105        //      randpoint.Normalize();
106                Vector randpoint(RangeRandom(-1,1),RangeRandom(-1,1),RangeRandom(-1,1));
107        //      randpoint.Normalize();
108
109               
110
111                //set position
112                if(m_Type==0)
113                {
114                        if(m_Radius==0)
115                        {
116                                newParticle->setPosition(m_Position);
117                        }
118                        else
119                        {
120                                randpoint.Normalize();
121                                newParticle->setPosition(m_Position+randpoint*m_Radius*UnitRandom());
122                        }
123                }
124                else//get particle from script positions
125                {
126                        int randindex=(rand())%m_ScriptPositionCount;
127                        newParticle->setPosition(m_Position+m_ScriptPositions[randindex]*m_Radius+randpoint*m_Offset);
128                        randpoint.Normalize();
129                }
130       
131                if(m_VelocityVariation>1)m_VelocityVariation=1;
132                if(m_VelocityVariation<0)m_VelocityVariation=0;
133                double v=RangeRandom(-m_VelocityVariation,m_VelocityVariation);
134                double vel=m_Velocity+v*m_Velocity;
135
136                if(m_isDirectionSet)
137                {
138                        newParticle->setVelocity(m_Direction*vel);
139                }
140                else
141                {
142                       
143                        newParticle->setVelocity(randpoint*vel);
144
145                }
146
147                newParticle->m_Distance=(newParticle->getPosition()-CamPos).Length();
148
149                //add time to live
150                unsigned int ttl;
151                if(m_TimeToLive!=0)
152                {
153                ttl=(unsigned int)RangeRandom(m_TimeToLive-m_TTLvariation,m_TimeToLive+m_TTLvariation);
154                newParticle->refrcol=true;
155                }
156                else
157                {
158                ttl=0;
159                }
160                newParticle->setTimetoLive(ttl);
161
162                //set size
163                if(m_Sizevariation>1)m_Sizevariation=1;
164                if(m_Sizevariation<0)m_Sizevariation=0;
165                double s=RangeRandom(-m_Sizevariation,m_Sizevariation);
166                double siz=m_Size+s*m_Size;
167                //newParticle->setSize(siz);
168                newParticle->setSize(RangeRandom(m_Size-m_Sizevariation*m_Size,m_Size+m_Sizevariation*m_Size));
169
170                newParticle->Refresh(0);
171               
172                thisParticle->m_NextParticle=newParticle;
173                thisParticle=thisParticle->m_NextParticle;
174        }
175
176        if(Dt>1000)
177        {
178                //new second begins
179                m_EmittedInSecond=0;
180        }
181       
182        return toemit;
183}
Note: See TracBrowser for help on using the repository browser.