Page principale   Liste des namespaces   Hiérarchie des classes   Liste par ordre alphabétique   Liste des composants   Liste des fichiers   Composants   Déclarations  

ExCSystemeParticule.cpp

Aller à la documentation de ce fichier.
00001 /*
00002  * ExNihilo 3D Engine
00003  * 
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  *
00018  * Please read AUTHORS file !!!
00019  * 
00020  * $Id: ExCSystemeParticule.cpp,v 1.30 2002/07/29 11:05:02 binny Exp $
00021  *
00022  */
00023 
00024 #include "ExCSystemeParticule.h"
00025 
00026 ExCSystemeParticule::ExCSystemeParticule(void)
00027 {
00028 Guard(ExCSystemeParticule::ExCSystemeParticule(void))
00029         m_MaxLifeSpan   = 1000;
00030         m_MinLifeSpan   = 100;
00031         m_MaxSize               = 0.3f;
00032         m_MinSize               = 0.00f;        
00033         m_MaxWeight             = 10;
00034         m_MinWeight             = 1;
00035         m_MaxColorR             = 255;
00036         m_MinColorR             = 0;
00037         m_MaxColorG             = 255;
00038         m_MinColorG             = 0;
00039         m_MaxColorB             = 255;
00040         m_MinColorB             = 0;
00041         m_MaxBlending   = 255;
00042         m_MinBlending   = 200;  
00043         m_MaxVelocity.SetValue(0.0f,1.0f,0.0f);
00044         m_MinVelocity.SetValue(0.0f,1.0f,0.0f); 
00045         m_MaxAcceleration.SetValue(0.0f,0.0f,0.0f);
00046         m_MinAcceleration.SetValue(0.0f,0.0f,0.0f);
00047         m_MaxGravity.SetValue(0.0f,0.0f,0.0f);
00048         m_MinGravity.SetValue(0.0f,0.0f,0.0f);
00049     m_Type                              = PARTICULE_POINT;
00050         m_TimeLastParticule=0;
00051 
00052         m_MaxParticule  =10000;
00053         m_EmissionRate  =10;//in particule by seconde
00054         m_Pause=false;
00055         m_State=true;
00056         m_angle=5;
00057         m_rayon=3.0f;
00058         m_ParticuleSystemeName = new char[20];
00059         m_TypeEmmeteur=EMMETEUR_DISQUE;
00060 
00061         m_P1.SetValue(0.0f,0.0f,0.0f);
00062         m_P2.SetValue(10.0f,0.0f,0.0f);
00063 
00064 
00065 UnGuard
00066 }
00067 
00068 ExCSystemeParticule::~ExCSystemeParticule(void)
00069 {
00070 Guard(ExCSystemeParticule::~ExCSystemeParticule(void))
00071 UnGuard
00072 }
00073 
00074 void ExCSystemeParticule::SetParticuleSystemeName(char * name)
00075 {
00076 Guard(void ExCParticule::SetParticuleSystemeName(char * name))
00077         m_ParticuleSystemeName=new char[strlen(name)];
00078         strcpy(m_ParticuleSystemeName,name);
00079 UnGuard
00080 }
00081 
00082 void ExCSystemeParticule::SetParticuleType(int type)
00083 {
00084 Guard(void ExCParticule::SetParticuleType(int type))
00085         m_Type=type;
00086         if(m_Type>3)m_Type=1;
00087 UnGuard
00088 }
00089 
00090 void ExCSystemeParticule::SetParticuleTexture(char *texture)
00091 {
00092 Guard(void ExCParticule::SetParticuleTexture(char *texture))
00093         LoadTexture(texture);
00094 UnGuard
00095 }
00096 
00097 void ExCSystemeParticule::Draw(void)
00098 {
00099 Guard(void ExCSystemeParticule::Draw(void))
00100         
00101         ExCRandomNumber  Randomnum;
00102         double                   a,c;
00103         int                             i;
00104 
00105         glPushMatrix();
00106                 
00107 
00108         /*      Position.SetValue(a,0.0f,c);
00109                 Position.SetValue(0.0f,0.0f,10.0f);
00110                 Position.SetValue(Randomnum.getFloat(0.0f,100.0f),Randomnum.getFloat(0.0f,100.0f),0.0f);
00111 */
00112                 ExCVec3D Position;
00113 
00114                 
00115                 switch(m_TypeEmmeteur)
00116                 {
00117                 case EMMETEUR_POINT     :
00118                         Position=m_Position+m_P1;
00119                         break;
00120 
00121                 case EMMETEUR_LIGNE :
00122                 case EMMETEUR_RECTANGLE :
00123                 case EMMETEUR_CUBE :
00124                         Position.SetValue(Randomnum.getFloat(m_P1.m_Vector[0],m_P2.m_Vector[0]),
00125                                                                 Randomnum.getFloat(m_P1.m_Vector[1],m_P2.m_Vector[1]),
00126                                                                 Randomnum.getFloat(m_P1.m_Vector[2],m_P2.m_Vector[2]));
00127                         Position=m_Position+Position;
00128                         break;
00129 
00130                 case EMMETEUR_CERCLE :
00131                         a=Cos[m_angle]*m_rayon;
00132                         c=Sin[m_angle]*m_rayon;
00133                         m_angle=Randomnum.getInt(0,360);
00134                         Position.SetValue(a,0.0f,c);
00135                         Position=m_Position+Position;
00136                         break;
00137 
00138                 case EMMETEUR_DISQUE :
00139                         a=Cos[m_angle]*(Randomnum.getFloat(0.0f,m_rayon));
00140                         c=Sin[m_angle]*(Randomnum.getFloat(0.0f,m_rayon));
00141                         m_angle++;
00142                         if(m_angle>360)m_angle=0;
00143                         Position.SetValue(a,0.0f,c);
00144                         Position=m_Position+Position;
00145                         break;
00146 
00147                 case EMMETEUR_SPIRAL :
00148                         a=Cos[m_angle]*m_rayon;
00149                         c=Sin[m_angle]*m_rayon;
00150                         m_angle+=m_vitesseangle;
00151                         if(m_angle>360)m_angle=0;
00152                         Position.SetValue(a,0.0f,c);
00153                         Position=m_Position+Position;
00154                         break;
00155                 }
00156 
00157 
00158 
00159 
00160                 time_t ltime;
00161                 time( &ltime );
00162                 
00163                 // create new particule
00164                 if(!m_Pause)
00165                 {
00166                         if(m_TimeLastParticule<ltime)
00167                         {
00168                                 ExCParticule Particule;
00169                                 ExCVec3D Velocity,Acceleration,Gravity;
00170                                 Particule.SetParticuleTexture(m_Texture);
00171                                 Particule.SetParticuleType(m_Type);
00172                                 Particule.SetParticulePosition(Position);
00173                                 Particule.SetParticuleLife(Randomnum.getInt(m_MinLifeSpan,m_MaxLifeSpan));
00174                                 Particule.SetParticuleWeight(Randomnum.getInt(m_MinWeight,m_MaxWeight));
00175                                 Particule.SetParticuleColorB(Randomnum.getInt(m_MinColorB,m_MaxColorB));
00176                                 Particule.SetParticuleColorG(Randomnum.getInt(m_MinColorG,m_MaxColorG));
00177                                 Particule.SetParticuleColorR(Randomnum.getInt(m_MinColorR,m_MaxColorR));
00178                                 Particule.SetParticuleBlending(Randomnum.getInt(m_MinBlending,m_MaxBlending));
00179                                 Particule.SetParticuleMass(1.0f);
00180                                 //Particule.SetParticuleSize(Randomnum.getFloat (m_MinSize,m_MaxSize));
00181                                 Velocity.m_Vector[0]=Randomnum.getFloat(m_MaxVelocity.m_Vector[0],m_MinVelocity.m_Vector[0]);
00182                                 Velocity.m_Vector[1]=Randomnum.getFloat(m_MaxVelocity.m_Vector[1],m_MinVelocity.m_Vector[1]);
00183                                 Velocity.m_Vector[2]=Randomnum.getFloat(m_MaxVelocity.m_Vector[2],m_MinVelocity.m_Vector[2]);
00184                                 Particule.SetParticuleVelocity(Velocity);
00185                                 Acceleration.m_Vector[0]=Randomnum.getFloat(m_MaxAcceleration.m_Vector[0],m_MinAcceleration.m_Vector[0]);
00186                                 Acceleration.m_Vector[1]=Randomnum.getFloat(m_MaxAcceleration.m_Vector[1],m_MinAcceleration.m_Vector[1]);
00187                                 Acceleration.m_Vector[2]=Randomnum.getFloat(m_MaxAcceleration.m_Vector[2],m_MinAcceleration.m_Vector[2]);
00188                                 Particule.SetParticuleAcceleration(Acceleration);
00189                                 Gravity.m_Vector[0]=Randomnum.getFloat(m_MaxGravity.m_Vector[0],m_MinGravity.m_Vector[0]);
00190                                 Gravity.m_Vector[1]=Randomnum.getFloat(m_MaxGravity.m_Vector[1],m_MinGravity.m_Vector[1]);
00191                                 Gravity.m_Vector[2]=Randomnum.getFloat(m_MaxGravity.m_Vector[2],m_MinGravity.m_Vector[2]);
00192                                 Particule.SetParticuleGravity(Gravity);
00193                                 Particule.PrecalculeParticuleTrajectory(0,Particule.GetParticuleLife());
00194                                 m_VecParticule.push_back(Particule);
00195                                 //m_TimeLastParticule=ltime + m_EmissionRate;
00196                                 //std::cout<<"Create new particule"<<std::endl;                 
00197                         }
00198                 }
00199         glPopMatrix();
00200         
00201         //draw all particule and destrou particule 
00202         //std::cout<<"Nombre particule :"<<m_VecParticule.size()<<std::endl;            
00203         if(m_VecParticule.size()>0)
00204         {
00205                 
00206                 for(i=0,m_ItVecParticule=m_VecParticule.begin();(unsigned)i<m_VecParticule.size();m_ItVecParticule++,i++)
00207                 {
00208                         //std::cout<<"particule :"<<i<<std::endl;               
00209                         m_ItVecParticule->Draw();
00210                         if(!m_Pause)m_ItVecParticule->DecreaseLifeSpan();
00211                         if(!m_State)m_ItVecParticule->DecreaseLifeSpan();
00212                         if(m_ItVecParticule->GetParticuleLife()<0)
00213                         {
00214                                 m_VecParticule.erase(m_ItVecParticule);
00215                         }
00216                 }
00217         }
00218         //std::cout<<"add draw particule"<<std::endl;                   
00219 UnGuard
00220 }
00221 
00222 bool ExCSystemeParticule::LoadTexture(const char * name)
00223 {
00224 Guard(ExCSystemeParticule::LoadTexture(const char * name))
00225         char * filename;
00226         char buffer[255];
00227         
00228         filename = new char[strlen(name)+80];
00229 #ifdef UNIX_SRC
00230         sprintf(filename, PREFIX "/ExNihilo/Data/Textures/%s.bmp",name);   //change path here if textures path change
00231 #else
00232         sprintf(filename,"../Data/Textures/%s.bmp",name);   //change path here if textures path change
00233 #endif
00234 
00235         
00236                 //---------------------------------------------
00237                 Image *image = new Image();
00238                 FILE *file;
00239                 unsigned long size;                 // size of the image in bytes.
00240                 unsigned long i;                    // standard counter.
00241                 unsigned short int planes;          // number of planes in image (must be 1) 
00242                 unsigned short int bpp;             // number of bits per pixel (must be 24)
00243                 char temp;                          // temporary color storage for bgr-rgb conversion.
00244 
00245                 // make sure the file is there.
00246                 if ((file = fopen(filename, "rb"))==NULL)
00247                 {
00248                         sprintf(buffer,"File Not Found : %s",filename);
00249                         WriteToConsol(buffer);
00250                         return false;
00251                 }
00252 #ifdef UNIX_SRC
00253                 unsigned short int is_bmp;
00254                 if (! fread (&is_bmp, sizeof (short int), 1, file)) {
00255                         sprintf(buffer,"cannot read %s.\n", filename);
00256                         WriteToConsol(buffer);
00257                         return false;
00258                 }
00259                 
00260                 /* check if file is a bitmap */
00261                 if (is_bmp != 19778) {
00262                         sprintf(buffer,"%s is not a valid bmp file.\n", filename);
00263                         WriteToConsol(buffer);
00264                         return false;
00265                 }
00266                 
00267                 fseek (file, 8, SEEK_CUR);
00268                 /* get the position of the actual bitmap data */
00269                 long int bfOffBits;
00270                 if (! fread (&bfOffBits, sizeof (long int), 1, file)) {
00271                         sprintf(buffer,"Error reading %s.\n", filename);
00272                         WriteToConsol(buffer);
00273                         return false;
00274                 }
00275                 sprintf(buffer,"Data at offset: %ld .\n", bfOffBits);
00276                 WriteToConsol(buffer);
00277                 
00278                 // seek through the bmp header, up to the width/height:
00279                 fseek(file, 4, SEEK_CUR);
00280 #else
00281                 fseek(file, 18, SEEK_SET);
00282 #endif
00283                 // read the width
00284                 if ((i = fread(&image->sizeX, 4, 1, file)) != 1) 
00285                 {
00286                         sprintf(buffer,"Error reading width from %s.\n", filename);
00287                         WriteToConsol(buffer);
00288                         return false;
00289                 }
00290                             
00291                 // read the height 
00292                 if ((i = fread(&image->sizeY, 4, 1, file)) != 1) 
00293                 {
00294                         sprintf(buffer,"Error reading height from %s.\n", filename);
00295                         WriteToConsol(buffer);
00296                         return false;
00297                 }
00298                 
00299             
00300                 // calculate the size (assuming 24 bits or 3 bytes per pixel).
00301                 size = image->sizeX * image->sizeY * 3;
00302 
00303                 // read the planes
00304                 if ((fread(&planes, 2, 1, file)) != 1)
00305                 {
00306                         sprintf(buffer,"Error reading planes from %s.\n", filename);
00307                         WriteToConsol(buffer);
00308                         return false;
00309                 }
00310 
00311                 if (planes != 1) 
00312                 {
00313                         sprintf(buffer,"Planes from %s is not 1: %u \n", filename, planes);
00314                         WriteToConsol(buffer);
00315                         return false;
00316                 }
00317 
00318                 // read the bpp
00319                 if ((i = fread(&bpp, 2, 1, file)) != 1) 
00320                 {
00321                         sprintf(buffer,"Error reading bpp from %s.\n", filename);
00322                         WriteToConsol(buffer);
00323                         return false;
00324                 }
00325                 if (bpp != 24) 
00326                 {
00327                         sprintf(buffer,"Bpp from %s is not 24: %u\n", filename, bpp);
00328                         WriteToConsol(buffer);
00329                         return false;
00330                 }
00331                 
00332                 // seek past the rest of the bitmap header.
00333                 fseek(file, 24, SEEK_CUR);
00334 
00335                 // read the data. 
00336                 image->data = (char *) malloc(size);
00337                 if (image->data == NULL)
00338                 {
00339                         sprintf(buffer,"Error allocating memory for color-corrected image data");
00340                         WriteToConsol(buffer);
00341                         return false;   
00342                 }
00343 
00344                 if ((i = fread(image->data, size, 1, file)) != 1) 
00345                 {
00346                         sprintf(buffer,"Error reading image data from %s.\n", filename);
00347                         WriteToConsol(buffer);
00348                         return false;
00349                 }
00350 
00351                 for (i=0;i<size;i+=3) 
00352                 { 
00353                         // reverse all of the colors. (bgr -> rgb)
00354                         temp = image->data[i];
00355                         image->data[i] = image->data[i+2];
00356                         image->data[i+2] = temp;
00357                 }
00358                 //---------------------------------------------
00359                                                 
00360         
00361                 glGenTextures(1,&m_Texture);
00362 
00363                 glBindTexture(GL_TEXTURE_2D,m_Texture);
00364 
00365                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
00366                 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
00367                 gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image->sizeX, image->sizeY, GL_RGB, GL_UNSIGNED_BYTE, image->data);
00368                 
00369                 sprintf(buffer,"Particule syteme texture ok");
00370                 WriteToConsol(buffer);
00371         return true;
00372 UnGuard
00373 }
00374 
00375 void ExCSystemeParticule::Start(void)
00376 {
00377 Guard(void ExCSystemeParticule::Start(void))
00378         m_State=true;
00379         m_Pause=false;
00380 UnGuard
00381 }
00382 
00383 void ExCSystemeParticule::Pause(void)
00384 {
00385 Guard(void ExCSystemeParticule::Pause(void))
00386         m_Pause=true;
00387 UnGuard
00388 }
00389 
00390 void ExCSystemeParticule::Stop(void)
00391 {
00392 Guard(void ExCSystemeParticule::Stop(void))
00393         m_Pause=true;
00394         m_State=false;
00395 UnGuard
00396 }
00397 
00398 bool ExCSystemeParticule::LoadParticuleSysteme(const char * FileName)
00399 {
00400 Guard(bool ExCParticule::LoadParticuleSysteme(const char * FileName))
00401         SetParticuleTexture("star");
00402         m_Type=PARTICULE_TEXTURE;
00403 
00404         int i=0;
00405         char                            ch;
00406         int                                     RetVal;
00407         int                                     FileSize;
00408         FILE                            *MyFile;
00409         std::string                     StBuffer;
00410         char                            buffer[255],bufferbis[255];
00411         std::vector<float>      vecvalue;
00412 
00413         
00414         sprintf(buffer,"../Data/ParticuleSystem/%s",FileName);
00415         m_ParticuleSystemeName=new char[strlen(FileName)];
00416         sprintf(m_ParticuleSystemeName,"%s",FileName);
00417     
00418         MyFile=fopen (buffer,"r");
00419         if(!MyFile)
00420         {
00421                 sprintf(bufferbis,"Particule syteme File not found:%s",buffer);
00422                 WriteToConsol(bufferbis);
00423                 return false;
00424         }else
00425         {
00426                 sprintf(buffer,"Loading Particule systeme file :%s",FileName);
00427                 WriteToConsol(buffer);
00428         
00429                 fseek(MyFile,0,SEEK_END);
00430                 FileSize=ftell(MyFile);
00431                 sprintf(bufferbis,"Size:%d",FileSize);
00432                 WriteToConsol(bufferbis);
00433                 //Start of file
00434                 fseek(MyFile,0,SEEK_SET);
00435                 do
00436                 {
00437                         RetVal=fread(&ch,sizeof(char),1,MyFile);
00438                 }while(ch!='#');
00439                 //reading file type;
00440                 do
00441                 {
00442                         RetVal=fread(&ch,sizeof(char),1,MyFile);
00443                         StBuffer=StBuffer+ch;   
00444                 }while(ch!='#');
00445                 if(strcmp(StBuffer.data(),"particulesystem#")==0)
00446                 {
00447                         for(i=0;i<44;i++)
00448                         {
00449                                 StBuffer.erase(StBuffer.begin(),StBuffer.end());
00450                                 do
00451                                 {
00452                                                 RetVal=fread(&ch,sizeof(char),1,MyFile);
00453                                                 StBuffer=StBuffer+ch;
00454                                 }while(ch!='#');
00455                                 StBuffer.erase(StBuffer.begin(),StBuffer.end());
00456                                 do
00457                                 {
00458                                                 RetVal=fread(&ch,sizeof(char),1,MyFile);
00459                                                 StBuffer=StBuffer+ch;
00460                                 }while(ch!='#');
00461                                 
00462                                 StBuffer.erase(StBuffer.end()-1,StBuffer.end());
00463                                 
00464                                 try
00465                                 {
00466                                         vecvalue.push_back(atof(StBuffer.data()));
00467                                 }catch(...)
00468                                 {
00469                                         vecvalue.push_back(0);
00470                                 }
00471                         }
00472                         /*
00473                         for(i=0;i<nombrealire;i++)
00474                         {
00475                                 StBuffer.erase(StBuffer.begin(),StBuffer.end());
00476                                 do
00477                                 {
00478                                         RetVal=fread(&ch,sizeof(char),1,MyFile);
00479                                         StBuffer=StBuffer+ch;
00480                                 }while(ch!='#');
00481                                 StBuffer.erase(StBuffer.begin(),StBuffer.end());
00482                                 vecvalue.push_back(StBuffer);
00483                         }*/
00484 
00485                         //read texture for particule
00486                         StBuffer.erase(StBuffer.begin(),StBuffer.end());
00487                         do
00488                         {
00489                                 RetVal=fread(&ch,sizeof(char),1,MyFile);
00490                                 StBuffer=StBuffer+ch;
00491                         }while(ch!='#');
00492 
00493                         StBuffer.erase(StBuffer.begin(),StBuffer.end());
00494                         do
00495                         {
00496                                 RetVal=fread(&ch,sizeof(char),1,MyFile);
00497                                 StBuffer=StBuffer+ch;
00498                         }while(ch!='#');
00499                         StBuffer.erase(StBuffer.end()-1,StBuffer.end());
00500                         
00501                         SetParticuleTexture((char *)StBuffer.data());
00502                         //-----------------------------------
00503                         m_EmissionRate  = vecvalue.at(0);
00504                         m_MaxParticule  = vecvalue.at(1);
00505                         m_TypeEmmeteur  = vecvalue.at(2);
00506                         m_P1.SetValue(vecvalue.at(3),vecvalue.at(4),vecvalue.at(5));
00507                         m_P2.SetValue(vecvalue.at(6),vecvalue.at(7),vecvalue.at(8));
00508                         m_rayon         = vecvalue.at(9);
00509                         m_vitesseangle  = vecvalue.at(10);
00510                         m_MaxLifeSpan   = vecvalue.at(11);
00511                         m_MinLifeSpan   = vecvalue.at(12);
00512                         m_MaxSize               = vecvalue.at(13);
00513                         m_MinSize               = vecvalue.at(14);      
00514                         m_MaxWeight             = vecvalue.at(15);
00515                         m_MinWeight             = vecvalue.at(16);
00516                         m_MaxColorR             = vecvalue.at(17);
00517                         m_MinColorR             = vecvalue.at(18);
00518                         m_MaxColorG             = vecvalue.at(19);
00519                         m_MinColorG             = vecvalue.at(20);
00520                         m_MaxColorB             = vecvalue.at(21);
00521                         m_MinColorB             = vecvalue.at(22);
00522                         m_MaxBlending   = vecvalue.at(23);
00523                         m_MinBlending   = vecvalue.at(24);      
00524                         m_MaxVelocity.SetValue(vecvalue.at(25),vecvalue.at(26),vecvalue.at(27));
00525                         m_MinVelocity.SetValue(vecvalue.at(28),vecvalue.at(29),vecvalue.at(30));        
00526                         m_MaxAcceleration.SetValue(vecvalue.at(31),vecvalue.at(32),vecvalue.at(33));
00527                         m_MinAcceleration.SetValue(vecvalue.at(34),vecvalue.at(35),vecvalue.at(36));
00528                         m_MaxGravity.SetValue(vecvalue.at(37),vecvalue.at(38),vecvalue.at(39));
00529                         m_MinGravity.SetValue(vecvalue.at(40),vecvalue.at(41),vecvalue.at(42));
00530                         
00531                         m_Type                          = vecvalue.at(43);
00532                         
00533                         return true;
00534                 }else
00535                 {
00536                         sprintf(buffer,"%s is not a valide particule syteme file",FileName);
00537                         WriteToConsol(buffer);
00538                         return false;
00539                 }
00540                 fclose(MyFile);
00541         }
00542 
00543 UnGuard
00544 }
00545 

Généré le Tue Aug 6 20:25:26 2002 pour ExNihilo par doxygen1.2.17