home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 March / ENTER.ISO / files / fwp-0.0.6-win32-installer.exe / LightEffectsParticleClusters.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-12-06  |  3.8 KB  |  140 lines

  1. #include "LightEffectsParticleClusters.h"
  2.  
  3. #include "Game.h"
  4. #include "Mesh.h"
  5. #include "RendererInfo.h"
  6. #include "Network.h"
  7. #include "intersection.h"
  8. #include "GameObject.h"
  9.  
  10.  
  11. DynamicLightParticleCluster::DynamicLightParticleCluster(vec3_t pos, vec3_t col, float radius, unsigned long lifetime):ParticleCluster(lifetime){
  12.  
  13.     this->gameObject = NULL;
  14.     this->colAnim = NULL;
  15.     this->radius = radius;
  16.  
  17.     vectorCopy3d(pos, this->pos);
  18.     vectorCopy3d(col, this->col);
  19.  
  20.     nextUpdateMillis = 0;
  21. //    numMeshes = 0;
  22. //    meshes = NULL;
  23. //    colors = NULL;
  24.  
  25.  
  26.     createAndAssignColors();
  27. }
  28.  
  29. DynamicLightParticleCluster::DynamicLightParticleCluster(GameObject* obj, vec3_t col, float radius, unsigned long lifetime):ParticleCluster(lifetime){
  30.  
  31.     this->gameObject=obj;
  32.     obj->attachDynamicLightParticleCluster(this);
  33.     this->colAnim = NULL;
  34.     this->radius=radius;
  35.  
  36.     vectorCopy3d(obj->pos, this->pos);
  37.     vectorCopy3d(col, this->col);
  38.  
  39.     nextUpdateMillis = 0;
  40. //    meshes=NULL;
  41. //    colors=NULL;
  42.  
  43.     createAndAssignColors();
  44. }
  45.  
  46. DynamicLightParticleCluster::~DynamicLightParticleCluster(){
  47.     freeAndRevertColors();
  48.  
  49.     if( gameObject != NULL ){
  50.         gameObject->detachDynamicLightParticleCluster(this);
  51.     }
  52. //    if(meshes!=NULL)
  53. //        delete[] meshes;
  54.  
  55. }
  56.  
  57.  
  58. void DynamicLightParticleCluster::createAndAssignColors(){
  59.     unsigned int i,j;
  60.  
  61.     freeAndRevertColors();
  62. //    if(meshes != NULL)
  63. //        delete[] meshes;
  64.     meshes.clear();
  65.     
  66.     vec3_t min, max;
  67.     vectorInit3d(pos[0]-radius*2.0f, pos[1]-radius*2.0f, pos[2]-radius*2.0f, min);
  68.     vectorInit3d(pos[0]+radius*2.0f, pos[1]+radius*2.0f, pos[2]+radius*2.0f, max);
  69.  
  70.     vector<SpacePartitioningTreeNode*> nodes = Game::arena->sptree->getLeafNodesIntersectingAABB(min, max);
  71.  
  72.     // light arena
  73.     for(i=0; i<nodes.size(); i++){
  74.         // generate and assign colors
  75.         Mesh* mesh = nodes[i]->mesh;
  76.         if( mesh == NULL )
  77.             continue;
  78.  
  79.         meshes.push_back( mesh );
  80.         GLfloat* colArray = new GLfloat[mesh->numSecondaryColors*3];
  81.         meshColors.push_back( colArray );
  82.         for(j=0;j<(unsigned int)mesh->numSecondaryColors;j++){
  83.             float f = vectorPointDistance3d(pos, &mesh->vertices[j*3])/radius;
  84.             f = f > 1.0f ? 1.0f : f;
  85.             vectorScale3d(1.0f - f, col, &colArray[j*3]);
  86.             vectorAdd3d(&mesh->secondaryColors[j*3], &colArray[j*3], &mesh->secondaryColors[j*3]);
  87.         }
  88.         if(mesh->secondaryColorVBOName != 0)
  89.             mesh->updateSecondaryColorVBO();
  90.     }
  91.  
  92.     // light vehicles
  93.     vectorInit3d(pos[0]-radius, pos[1]-radius, pos[2]-radius, min);
  94.     vectorInit3d(pos[0]+radius, pos[1]+radius, pos[2]+radius, max);
  95.     for(i=0;i<GAME_MAX_VEHICLES;i++){
  96.         if( Game::vehicles[i] == NULL )
  97.             continue;
  98.  
  99.         Vehicle* v = Game::vehicles[i];
  100.         vec3_t v_min, v_max;
  101.         vectorAdd3d(v->pos, v->moveAABB.min, v_min);
  102.         vectorAdd3d(v->pos, v->moveAABB.max, v_max);
  103.         if( AABBIntersectsAABB(min, max, v_min, v_max) ){
  104.             vehicles.push_back(v);
  105.             v->addDynamicLightParticleCluster(this);
  106.         }
  107.     }
  108. }
  109.  
  110. void DynamicLightParticleCluster::freeAndRevertColors(){
  111.     unsigned int i, j;
  112.     
  113.     for(i=0;i<meshes.size();i++){
  114.         for(j=0;j<(unsigned int)meshes[i]->numSecondaryColors;j++){
  115.             vectorSub3d(&meshes[i]->secondaryColors[j*3], &meshColors[i][j*3], &meshes[i]->secondaryColors[j*3]);
  116.         }
  117.         if(meshes[i]->secondaryColorVBOName != 0)
  118.             meshes[i]->updateSecondaryColorVBO();
  119.         delete[] meshColors[i];
  120.     }
  121.     meshColors.clear();
  122.  
  123.     while( !vehicles.empty() ){
  124.         list<Vehicle*>::iterator iter = vehicles.begin();
  125.         (*iter)->removeDynamicLightParticleCluster(this);
  126.         vehicles.pop_front();
  127.     }
  128. }
  129.  
  130. void DynamicLightParticleCluster::move(){
  131.     unsigned long currentMillis = SDL_GetTicks();
  132.     if(gameObject!=NULL && Renderer::info.var.useDynamicLighting && currentMillis > nextUpdateMillis ){
  133.         vectorCopy3d(gameObject->pos, pos);    // update pos
  134.         createAndAssignColors();
  135.         nextUpdateMillis = currentMillis + 30;
  136.     }
  137. }
  138. void DynamicLightParticleCluster::render(){
  139. }
  140.