Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 27, 2006, 3:35:01 PM (18 years ago)
Author:
bottac
Message:

further reformating

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/bsp_model/src/lib/graphics/importer/bsp_manager.cc

    r7385 r7395  
    11/*
    22   orxonox - the future of 3D-vertical-scrollers
    3 
     3 
    44   Copyright (C) 2006 orx
    5 
     5 
    66   This program is free software; you can redistribute it and/or modify
    77   it under the terms of the GNU General Public License as published by
    88   the Free Software Foundation; either version 2, or (at your option)
    99   any later version.
    10 
     10 
    1111   ### File Specific:
    1212   main-programmer: bottac@ee.ethz.ch
     
    3434BspManager::BspManager()
    3535{
    36         // open a BSP file
    37         this->bspFile = new BspFile();
    38         this->bspFile->read("/root/data/Kanti175.bsp");
    39         this->bspFile->build_tree();
    40         this->root  = this->bspFile->get_root();
    41         this->alreadyVisible = new bool [this->bspFile->numFaces];
     36  // open a BSP file
     37  this->bspFile = new BspFile();
     38  this->bspFile->read("/root/data/Kanti175.bsp");
     39  this->bspFile->build_tree();
     40  this->root  = this->bspFile->get_root();
     41  this->alreadyVisible = new bool [this->bspFile->numFaces];
    4242}
    4343
    4444void BspManager::draw()
    4545{
    46        
    47 
    48 
    49 
    50 
    51  // Draw Debug Terrain
    52 /*
    53 this->bspFile->Materials[0]->select();
    54 for(int i = 0; i <  this->bspFile->numPatches ; i++)
    55         {
    56                 this->bspFile->VertexArrayModels[i]->draw();
    57 
    58         }
    59 */
    60 
    61 
    62 
    63    // erase alreadyVisible
    64    for(int i = 0; i < this->bspFile->numFaces; i++) this->alreadyVisible[i] = false;
    65         float tmp = 0;
    66    this->opal.clear();
    67    this->trasparent.clear();   
     46
     47
     48
     49
     50
     51  // Draw Debug Terrain
     52  /*
     53  this->bspFile->Materials[0]->select();
     54  for(int i = 0; i <  this->bspFile->numPatches ; i++)
     55        {
     56                this->bspFile->VertexArrayModels[i]->draw();
     57   
     58        }
     59  */
     60
     61
     62
     63  // erase alreadyVisible
     64  for(int i = 0; i < this->bspFile->numFaces; i++) this->alreadyVisible[i] = false;
     65  float tmp = 0;
     66  this->opal.clear();
     67  this->trasparent.clear();
    6868  // Find all visible faces...
    6969
    70   this->cam = State::getCamera()->getAbsCoor() ;
    71  this->ship = State::getCameraTargetNode()->getAbsCoor();
    72 
    73 
    74 
    75 
    76    this->cam = State::getCameraTargetNode()->getAbsCoor();
    77    this->viewDir=    State::getCameraTarget()->getAbsCoor() -  State::getCamera()->getAbsCoor() ;
    78    float d = (cam.x*viewDir.x + cam.y*viewDir.y + cam.z * viewDir.z);
    79 
    80    BspTreeNode*  ActLeaf = this->getLeaf(this->bspFile->root, &ship);
    81    int viscluster = -1;
    82         viscluster =((leaf*)(this->bspFile->leaves))[ ActLeaf->leafIndex].cluster; // get the players cluster (viscluster)
    83 
    84 
    85  
    86    // Test Collision Detection
    87         this->checkCollision(this->root, &this->cam);
    88 
    89     if (true)//(((int *)(this->bspFile->header))[35] == 0)  || viscluster < 0)  // if (sizeof(Visdata) == 0)
     70  this->cam = State::getCamera()->getAbsCoor() ;
     71  this->ship = State::getCameraTargetNode()->getAbsCoor();
     72
     73
     74
     75
     76  this->cam = State::getCameraTargetNode()->getAbsCoor();
     77  this->viewDir=    State::getCameraTarget()->getAbsCoor() -  State::getCamera()->getAbsCoor() ;
     78  float d = (cam.x*viewDir.x + cam.y*viewDir.y + cam.z * viewDir.z);
     79
     80  BspTreeNode*  ActLeaf = this->getLeaf(this->bspFile->root, &ship);
     81  int viscluster = -1;
     82  viscluster =((leaf*)(this->bspFile->leaves))[ ActLeaf->leafIndex].cluster; // get the players cluster (viscluster)
     83
     84
     85
     86
     87  this->checkCollision(this->root, &this->cam);   //!< Test Collision Detection
     88
     89  if ((((int *)(this->bspFile->header))[35] == 0)  || viscluster < 0)  //!< if (sizeof(Visdata) == 0)
     90  {
     91    /** Do Frustum culling and draw 'em all **/
     92
     93    // Iterate through all Leafs
     94    for(int i = 0; i <  this->bspFile->numLeafs   ; i++ )
    9095    {
    91         // Do Frustum culling and draw 'em all
    92        
    93         // Iterate through all Leafs
    94         for(int i = 0; i <  this->bspFile->numLeafs   ; i++ )   
    95         {
    96                 // cluster =  ((leaf *)(this->bspFile->leaves))[i].cluster;
    97                 leaf& curLeaf = ((leaf *)(this->bspFile->leaves))[i];
    98        
    99                
    100                 // Iterate through all faces
    101                 for (int j = 0; j < curLeaf.n_leaffaces ; ++j)
    102                 {
    103                          const int f = ( j +  ((leaf *) (this->bspFile->leaves))[i].leafface) % this->bspFile->numFaces;
    104                         if (f >=0 && !this->isAlreadyVisible(f))
    105                         {
    106                                 this->alreadyVisible[f] = true;
    107                         /*      if(ActLeaf->leafIndex == i)
    108                                 {
    109                                         this->alreadyVisible[i] = false;
    110                                         this->draw_debug_face(f);
    111                                         this->alreadyVisible[i] = true;
    112                                 }
    113                                 else */addFace(f); // "visibleFaces.append(f)"
    114                         }
    115                 }
    116                                  
    117        
    118 
    119          
    120         } //for
    121     }
    122     else
    123    {
    124          int cluster;
    125         int seven = 7;
    126         unsigned char  one = 1;
    127         unsigned int v;
    128         unsigned char  visSet;
    129        
    130         // Iterate through all Leafs
    131        
    132         for(int i = 0; i <  this->bspFile->numLeafs   ; ++i )   
    133         {
    134                 leaf& camLeaf =  ((leaf *)(this->bspFile->leaves))[ActLeaf->leafIndex] ;
    135                 leaf& curLeaf = ((leaf *)(this->bspFile->leaves))[i] ;
    136                 cluster =  curLeaf.cluster;
    137                 if(cluster <0) continue;
    138                 v = ((viscluster *  ( ((int *)this->bspFile->visData)[1]) ) + (cluster / 8));
    139                 visSet =((unsigned char*) (this->bspFile->visData))[v+8];
    140                 //   PRINTF(0)("BSP Manager: visibility ");             
    141 
    142 
    143         if( ((visSet) & ((unsigned char)1 <<(unsigned char) (cluster &  7))) == 0 ) // < 20 || ((visSet) & ((unsigned char)1 <<(unsigned char) (cluster &  7)))  >= 128)
    144         {
    145                 // Iterate through all faces
    146                 for (int j = 0; j < curLeaf.n_leaffaces ; ++j)
    147                 {
    148                          const int f = (j +  curLeaf.leafface) % this->bspFile->numFaces;
    149                
    150 
    151                         if (!this->isAlreadyVisible(f) && f>=0)
    152                         {
    153                                         this->addFace(f);
    154                                         this->alreadyVisible[f] = true;
    155                         }
    156                        
    157                 }
    158        
    159         }// if
    160        
    161         }//for
    162 
    163     }//else
    164 
    165 while(!this->opal.empty())
    166 {
    167  this->draw_face(this->opal.front());
    168   this->opal.pop_front();
    169 }
    170 while(!this->trasparent.empty())
    171 {
    172  this->draw_face(this->trasparent.back());
    173  this->trasparent.pop_back();
    174 }
     96      // cluster =  (this->bspFile->leaves)[i].cluster;
     97      leaf& curLeaf = (this->bspFile->leaves)[i];
     98
     99
     100      // Iterate through all faces
     101      for (int j = 0; j < curLeaf.n_leaffaces ; ++j) {
     102        const int f = ( j +  ((leaf *) (this->bspFile->leaves))[i].leafface) % this->bspFile->numFaces;
     103        if (f >=0 && !this->isAlreadyVisible(f)) {
     104          this->alreadyVisible[f] = true;
     105          /*    if(ActLeaf->leafIndex == i)
     106                {
     107                        this->alreadyVisible[i] = false;
     108                        this->draw_debug_face(f);
     109                        this->alreadyVisible[i] = true;
     110                } 
     111                else */
     112          addFace(f); // "visibleFaces.append(f)"
     113        }
     114      }
     115
     116
     117
     118
     119    } //for
     120  } else {
     121    int cluster;
     122    int seven = 7;
     123    unsigned char  one = 1;
     124    unsigned int v;
     125    unsigned char  visSet;
     126
     127    // Iterate through all Leafs
     128
     129    for(int i = 0; i <  this->bspFile->numLeafs   ; ++i ) {
     130      leaf& camLeaf =  (this->bspFile->leaves)[ActLeaf->leafIndex] ;
     131      leaf& curLeaf =  (this->bspFile->leaves)[i] ;
     132      cluster =  curLeaf.cluster;
     133     
     134      if(cluster <0) continue;
     135      v = ((viscluster *  ( ((int *)this->bspFile->visData)[1]) ) + (cluster / 8));
     136      visSet =((unsigned char*) (this->bspFile->visData))[v+8];
     137   
     138     
     139      if( ((visSet) & ((unsigned char)1 <<(unsigned char) (cluster &  7))) == 0 )
     140      {
     141        // Iterate through all faces
     142        for (int j = 0; j < curLeaf.n_leaffaces ; ++j)
     143        {
     144          const int f = (j +  curLeaf.leafface) % this->bspFile->numFaces;
     145
     146
     147          if (!this->isAlreadyVisible(f) && f>=0) {
     148            this->addFace(f);
     149            this->alreadyVisible[f] = true;
     150          }
     151
     152        }
     153
     154    }// if
     155
     156    }//for
     157
     158  }//else
     159
     160  while(!this->opal.empty()) {
     161    this->draw_face(this->opal.front());
     162    this->opal.pop_front();
     163  }
     164  while(!this->trasparent.empty()) {
     165    this->draw_face(this->trasparent.back());
     166    this->trasparent.pop_back();
     167  }
    175168
    176169
     
    178171
    179172void BspManager::draw_leaf()
    180 {
    181 
    182 }
     173{}
    183174
    184175void BspManager::draw_face(int curface)
    185176{
    186  face& curFace =  ((face *)(this->bspFile->faces))[curface];
    187  const BspVertex* curVertex = (BspVertex *) this->bspFile->vertice;
    188  int stride = 44;  // sizeof(Vertex)
    189  int offset    = curFace.vertex;
    190        
     177  face& curFace =  (this->bspFile->faces)[curface];
     178  const BspVertex* curVertex = (BspVertex *) this->bspFile->vertice;
     179  int stride = sizeof(BspVertex);  // sizeof(Vertex)
     180  int offset    = curFace.vertex;
     181
    191182  // PRINTF(0)("BSP Manager: ");
    192183  // PRINTF(0)("BSP Manager: type: %i  \n", curFace.texture);
    193    
    194  //  if(  curFace.texture < 0 ) return;
    195    if(curFace.type == 2)
    196         {
    197         this->draw_patch( &curFace);
    198         return;
    199         }
    200  if(curFace.type == 3) return;
    201    // if(this->bspFile->Materials[curFace.texture] != NULL)     
    202         if(this->lastTex != curFace.texture)
    203         {
    204         this->bspFile->Materials[curFace.texture].mat->select();
    205         this->lastTex = curFace.texture;
    206         }
     184
     185  //  if(  curFace.texture < 0 ) return;
     186  if(curFace.type == 2) {
     187    this->draw_patch( &curFace);
     188    return;
     189  }
     190  if(curFace.type == 3) return;
     191  // if(this->bspFile->Materials[curFace.texture] != NULL)
     192  if(this->lastTex != curFace.texture) {
     193    this->bspFile->Materials[curFace.texture].mat->select();
     194    this->lastTex = curFace.texture;
     195  }
    207196  glEnableClientState(GL_VERTEX_ARRAY );
    208197  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
    209198  glEnableClientState(GL_NORMAL_ARRAY );
    210 //  glEnableClientState(GL_COLOR_ARRAY);       
    211   // glEnableClientState(GL_VERTEX_ARRAY );
    212        
     199  //  glEnableClientState(GL_COLOR_ARRAY);
     200 
     201
    213202  glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0]));
    214    // glClientActiveTextureARB(GL_TEXTURE0_ARB);
     203  // glClientActiveTextureARB(GL_TEXTURE0_ARB);
    215204  glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0]));
    216    // glClientActiveTextureARB(GL_TEXTURE1_ARB);
    217    // glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1]));
    218    //glEnableClientState(GL_NORMAL_ARRAY );
     205  // glClientActiveTextureARB(GL_TEXTURE1_ARB);
     206  // glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1]));
     207
    219208
    220209  glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0]));
    221     // glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0]));
     210  // glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0]));
    222211  glDrawElements(GL_TRIANGLES, curFace.n_meshverts,
    223                              GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert]));
     212                 GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert]));
    224213
    225214
     
    227216  glDisableClientState(GL_TEXTURE_COORD_ARRAY );
    228217  glDisableClientState(GL_NORMAL_ARRAY );
    229 // glDisableClientState(GL_COLOR_ARRAY);
    230        
     218  // glDisableClientState(GL_COLOR_ARRAY);
     219
    231220}
    232221
     
    234223void BspManager::draw_debug_face(int curface)
    235224{
    236  face& curFace =  ((face *)(this->bspFile->faces))[curface];
    237  const BspVertex* curVertex = (BspVertex *) this->bspFile->vertice;
    238  int stride = 44;  // sizeof(Vertex)
    239  int offset    = curFace.vertex;
    240        
     225  face& curFace =  (this->bspFile->faces)[curface];
     226  const BspVertex* curVertex = (BspVertex *) this->bspFile->vertice;
     227  int stride = 44;  // sizeof(Vertex)
     228  int offset    = curFace.vertex;
     229
    241230  // PRINTF(0)("BSP Manager: ");
    242231  // PRINTF(0)("BSP Manager: type: %i  \n", curFace.texture);
    243    
    244  //  if(  curFace.texture < 0 ) return;
    245    if(curFace.type == 2)
    246         {
    247         this->draw_patch( &curFace);
    248         return;
    249         }
    250  if(curFace.type == 3) return;
    251    // if(this->bspFile->Materials[curFace.texture] != NULL)     
    252 
    253         this->bspFile->Materials[2].mat->select();
    254         this->lastTex = 2;
     232
     233  //  if(  curFace.texture < 0 ) return;
     234  if(curFace.type == 2) {
     235    this->draw_patch( &curFace);
     236    return;
     237  }
     238  if(curFace.type == 3) return;
     239  // if(this->bspFile->Materials[curFace.texture] != NULL)
     240
     241  this->bspFile->Materials[2].mat->select();
     242  this->lastTex = 2;
    255243
    256244  glEnableClientState(GL_VERTEX_ARRAY );
    257245  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
    258246  glEnableClientState(GL_NORMAL_ARRAY );
    259   //glEnableClientState(GL_COLOR_ARRAY);       
     247  //glEnableClientState(GL_COLOR_ARRAY);
    260248  // glEnableClientState(GL_VERTEX_ARRAY );
    261        
     249
    262250  glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0]));
    263    // glClientActiveTextureARB(GL_TEXTURE0_ARB);
     251  // glClientActiveTextureARB(GL_TEXTURE0_ARB);
    264252  glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0]));
    265    // glClientActiveTextureARB(GL_TEXTURE1_ARB);
    266    // glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1]));
    267    //glEnableClientState(GL_NORMAL_ARRAY );
     253  // glClientActiveTextureARB(GL_TEXTURE1_ARB);
     254  // glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1]));
     255  //glEnableClientState(GL_NORMAL_ARRAY );
    268256
    269257  glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0]));
    270    //  glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0]));
     258  //  glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0]));
    271259  glDrawElements(GL_TRIANGLES, curFace.n_meshverts,
    272                              GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert]));
    273        
     260                 GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert]));
     261
    274262}
    275263
    276264void BspManager::draw_patch(face* Face)
    277265{
    278                 if(this->lastTex != Face->texture)
    279                 {
    280                 this->bspFile->Materials[Face->texture].mat->select();
    281                 this->lastTex = Face->texture;
    282                 }
    283 
    284         for(int i = 0; i < Face->n_meshverts  ; i++)
    285         {
    286            //glFrontFace(GL_CW);       
    287            //PRINTF(0)("BSP Manager: Face->size[0]: %i . \n", Face->size[0]);   
    288            glEnableClientState(GL_VERTEX_ARRAY );
    289            glEnableClientState(GL_TEXTURE_COORD_ARRAY );
    290            glEnableClientState(GL_NORMAL_ARRAY );
    291 
    292            glVertexPointer(3, GL_FLOAT,44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).position[0]));
    293            glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[0][0]));
    294            glNormalPointer( GL_FLOAT, 44,&((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).normal[0]) );
    295 
    296            // We'll need multitexture suport for lightning
    297            //glClientActiveTextureARB(GL_TEXTURE0_ARB);
    298            //glTexCoordPointer(2, GL_FLOAT,0, &vertex[0].textureCoord);
    299           //glClientActiveTextureARB(GL_TEXTURE1_ARB);
    300           //glTexCoordPointer(2, GL_FLOAT, sizeof(BSPVertex), &vertex[0].lightmapCoord);
    301 
    302 
    303                 for(int row=0; row<7; ++row)
    304                 {
    305                         glDrawElements(GL_TRIANGLE_STRIP, 2*(8), GL_UNSIGNED_INT,
    306                                                         & (     (((GLuint*)  (this->bspFile->patchIndexes))[7*8*2*(Face->meshvert+i)+ row*2*8]  ))  );
    307                 }
    308 
    309                 //glFrontFace(GL_CCW);
    310         }
    311                
     266  if(this->lastTex != Face->texture) {
     267    this->bspFile->Materials[Face->texture].mat->select();
     268    this->lastTex = Face->texture;
     269  }
     270
     271  for(int i = 0; i < Face->n_meshverts  ; i++) {
     272    //glFrontFace(GL_CW);
     273    //PRINTF(0)("BSP Manager: Face->size[0]: %i . \n", Face->size[0]);
     274    glEnableClientState(GL_VERTEX_ARRAY );
     275    glEnableClientState(GL_TEXTURE_COORD_ARRAY );
     276    glEnableClientState(GL_NORMAL_ARRAY );
     277
     278    glVertexPointer(3, GL_FLOAT,44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).position[0]));
     279    glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[0][0]));
     280    glNormalPointer( GL_FLOAT, 44,&((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).normal[0]) );
     281
     282    // We'll need multitexture suport for lightning
     283    //glClientActiveTextureARB(GL_TEXTURE0_ARB);
     284    //glTexCoordPointer(2, GL_FLOAT,0, &vertex[0].textureCoord);
     285    //glClientActiveTextureARB(GL_TEXTURE1_ARB);
     286    //glTexCoordPointer(2, GL_FLOAT, sizeof(BSPVertex), &vertex[0].lightmapCoord);
     287
     288
     289    for(int row=0; row<7; ++row) {
     290      glDrawElements(GL_TRIANGLE_STRIP, 2*(8), GL_UNSIGNED_INT,
     291                     & (     (((GLuint*)  (this->bspFile->patchIndexes))[7*8*2*(Face->meshvert+i)+ row*2*8]  ))  );
     292    }
     293
     294    //glFrontFace(GL_CCW);
     295  }
     296
    312297}
    313298
    314299bool BspManager::isAlreadyVisible(int Face)
    315300{
    316         return this->alreadyVisible[Face];
     301  return this->alreadyVisible[Face];
    317302}
    318303
     
    320305BspTreeNode*  BspManager::getLeaf(BspTreeNode* node, Vector* cam)
    321306{
    322         float dist = 0;
    323         while(!(node->isLeaf))
    324         {
    325                 dist = (node->plane.x * this->cam.x + node->plane.y*this->cam.y + node->plane.z*this->cam.z) - node->d;
    326                 if(dist >= 0.0f)
    327                 {
    328                  node = node->left;
    329                 }
    330                 else
    331                 {
    332                 node = node->right;
    333                
    334                 }
    335         }
    336         return  node;
     307  float dist = 0;
     308  while(!(node->isLeaf)) {
     309    dist = (node->plane.x * this->cam.x + node->plane.y*this->cam.y + node->plane.z*this->cam.z) - node->d;
     310    if(dist >= 0.0f) {
     311      node = node->left;
     312    } else {
     313      node = node->right;
     314
     315    }
     316  }
     317  return  node;
    337318}
    338319
    339320void  BspManager::checkCollision(BspTreeNode* node, Vector* cam)
    340321{
    341         float dist = 0;
    342         if(!(node->isLeaf))
    343         {
    344                 dist = (node->plane.x * this->cam.x + node->plane.y*this->cam.y + node->plane.z*this->cam.z) - node->d;
    345                 if(dist > 4.0f)
    346                 {
    347                  checkCollision(node->left,cam);
    348                  return;
    349                 }
    350                 if(dist < -4.0f)
    351                 {
    352                  checkCollision(node->right,cam);
    353                  return;
    354                 }
    355                 if(dist<=4.0f && dist >= -4.0f)
    356                         {
    357                         checkCollision(node->left,cam);
    358                         checkCollision(node->right,cam);
    359                         return;
    360                         }
    361                 return;
    362         }
    363         else
    364         {
    365        
    366         leaf& camLeaf =  ((leaf *)(this->bspFile->leaves))[(node->leafIndex ) ];
    367 
    368         if (camLeaf.cluster < 0) {this->drawDebugCube(&this->cam);}
    369 
    370        
    371 /*
    372         for(int i = 0; i < camLeaf.n_leafbrushes && i < 10; i++ )
    373         {
    374                 brush& curBrush = ((brush*)(this->bspFile->brushes))[(camLeaf.leafbrush_first +i)%this->bspFile->numLeafBrushes];
    375                 if(curBrush.n_brushsides < 0) return;
    376                 for(int j = 0; j < curBrush.n_brushsides; j++)
    377                 {
    378                 float dist = -0.1;
    379                 brushside& curBrushSide = ((brushside*)(this->bspFile->brushSides))[(curBrush.brushside +j)%this->bspFile->numBrushSides];
    380                 plane&      testPlane = ((plane*)(this->bspFile->planes))[curBrushSide.plane % this->bspFile->numPlanes];
    381                 dist = testPlane.x * this->cam.x +  testPlane.y * this->cam.y  +  testPlane.z * this->cam.z   -testPlane.d ;
    382                
    383                 if(dist < -0.01f) dist = -1.0f *dist;
    384                 if(dist < 1.0f){
    385                                 this->drawDebugCube(&this->cam);
    386                                 return;
    387                               }
    388                 }
    389                        
    390         } */
    391        
    392         }
    393         return;
     322  float dist = 0;
     323  if(!(node->isLeaf)) {
     324    dist = (node->plane.x * this->cam.x + node->plane.y*this->cam.y + node->plane.z*this->cam.z) - node->d;
     325    if(dist > 4.0f) {
     326      checkCollision(node->left,cam);
     327      return;
     328    }
     329    if(dist < -4.0f) {
     330      checkCollision(node->right,cam);
     331      return;
     332    }
     333    if(dist<=4.0f && dist >= -4.0f) {
     334      checkCollision(node->left,cam);
     335      checkCollision(node->right,cam);
     336      return;
     337    }
     338    return;
     339  } else {
     340
     341    leaf& camLeaf =  ((leaf *)(this->bspFile->leaves))[(node->leafIndex ) ];
     342
     343    if (camLeaf.cluster < 0) {this->drawDebugCube(&this->cam);}
     344
     345
     346    /*
     347        for(int i = 0; i < camLeaf.n_leafbrushes && i < 10; i++ )
     348        {
     349                brush& curBrush = ((brush*)(this->bspFile->brushes))[(camLeaf.leafbrush_first +i)%this->bspFile->numLeafBrushes];
     350                if(curBrush.n_brushsides < 0) return;
     351                for(int j = 0; j < curBrush.n_brushsides; j++)
     352                {
     353                float dist = -0.1;
     354                brushside& curBrushSide = ((brushside*)(this->bspFile->brushSides))[(curBrush.brushside +j)%this->bspFile->numBrushSides];
     355                plane&      testPlane = ((plane*)(this->bspFile->planes))[curBrushSide.plane % this->bspFile->numPlanes];
     356                dist = testPlane.x * this->cam.x +  testPlane.y * this->cam.y  +  testPlane.z * this->cam.z   -testPlane.d ;
     357               
     358                if(dist < -0.01f) dist = -1.0f *dist;
     359                if(dist < 1.0f){
     360                                this->drawDebugCube(&this->cam);
     361                                return;
     362                              }
     363                }
     364                       
     365        } */
     366
     367  }
     368  return;
    394369}
    395370
    396371void BspManager::drawDebugCube(Vector* cam)
    397372{
    398         glBegin(GL_QUADS);
    399 
    400    // Bottom Face.  Red, 75% opaque, magnified texture
    401  
    402    glNormal3f( 0.0f, -1.0f, 0.0f); // Needed for lighting
    403    glColor4f(0.9,0.2,0.2,.75); // Basic polygon color
    404 
    405    glTexCoord2f(0.800f, 0.800f); glVertex3f(cam->x-1.0f, cam->y-1.0f,cam->z -1.0f);
    406    glTexCoord2f(0.200f, 0.800f); glVertex3f(cam->x+1.0f, cam->y-1.0f,cam->z -1.0f);
    407    glTexCoord2f(0.200f, 0.200f); glVertex3f(cam->x+ 1.0f,cam->y -1.0f,cam->z +  1.0f);
    408    glTexCoord2f(0.800f, 0.200f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z + 1.0f);
    409 
    410 
    411    // Top face; offset.  White, 50% opaque.
    412  
    413    glNormal3f( 0.0f, 1.0f, 0.0f);  glColor4f(0.5,0.5,0.5,.5);
    414 
    415    glTexCoord2f(0.005f, 1.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.0f);
    416    glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f,  cam->z +1.0f);
    417    glTexCoord2f(1.995f, 0.005f); glVertex3f(cam->x+ 1.0f,  cam->y+1.0f,  cam->z +1.0f);
    418    glTexCoord2f(1.995f, 1.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z -1.0f);
    419 
    420 
    421    // Far face.  Green, 50% opaque, non-uniform texture cooridinates.
    422 
    423    glNormal3f( 0.0f, 0.0f,-1.0f);  glColor4f(0.2,0.9,0.2,.5);
    424 
    425    glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z -1.3f);
    426    glTexCoord2f(2.995f, 2.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.3f);
    427    glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x+ 1.0f,cam->y+  1.0f, cam->z -1.3f);
    428    glTexCoord2f(0.005f, 0.005f); glVertex3f( cam->x+1.0f,cam->y -1.0f, cam->z -1.3f);
    429 
    430 
    431    // Right face.  Blue; 25% opaque
    432    
    433    glNormal3f( 1.0f, 0.0f, 0.0f);  glColor4f(0.2,0.2,0.9,.25);
    434 
    435    glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x+ 1.0f,cam->y -1.0f, cam->z -1.0f);
    436    glTexCoord2f(0.995f, 0.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z -1.0f);
    437    glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z + 1.0f);
    438    glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y-1.0f,  cam->z +1.0f);
    439 
    440 
    441    // Front face; offset.  Multi-colored, 50% opaque.
    442 
    443    glNormal3f( 0.0f, 0.0f, 1.0f);
    444 
    445    glColor4f( 0.9f, 0.2f, 0.2f, 0.5f);
    446    glTexCoord2f( 0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f,  cam->z +1.0f);
    447    glColor4f( 0.2f, 0.9f, 0.2f, 0.5f);
    448    glTexCoord2f( 0.995f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y-1.0f,  cam->z +1.0f);
    449    glColor4f( 0.2f, 0.2f, 0.9f, 0.5f);
    450    glTexCoord2f( 0.995f, 0.995f); glVertex3f( cam->x+1.0f,  cam->y+1.0f,  cam->z +1.0f);
    451    glColor4f( 0.1f, 0.1f, 0.1f, 0.5f);
    452    glTexCoord2f( 0.005f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f,  cam->z +1.0f);
    453 
    454 
    455    // Left Face; offset.  Yellow, varying levels of opaque.
    456    
    457    glNormal3f(-1.0f, 0.0f, 0.0f); 
    458    
    459    glColor4f(0.9,0.9,0.2,0.0);
    460    glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z -1.0f);
    461    glColor4f(0.9,0.9,0.2,0.66);
    462    glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x-1.0f,cam->y -1.0f,  cam->z +1.0f);
    463    glColor4f(0.9,0.9,0.2,1.0);
    464    glTexCoord2f(0.995f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f,  cam->z +1.0f);
    465    glColor4f(0.9,0.9,0.2,0.33);
    466    glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.0f);
    467 
    468         glEnd();
     373  glBegin(GL_QUADS);
     374
     375  // Bottom Face.  Red, 75% opaque, magnified texture
     376
     377  glNormal3f( 0.0f, -1.0f, 0.0f); // Needed for lighting
     378  glColor4f(0.9,0.2,0.2,.75); // Basic polygon color
     379
     380  glTexCoord2f(0.800f, 0.800f); glVertex3f(cam->x-1.0f, cam->y-1.0f,cam->z -1.0f);
     381  glTexCoord2f(0.200f, 0.800f); glVertex3f(cam->x+1.0f, cam->y-1.0f,cam->z -1.0f);
     382  glTexCoord2f(0.200f, 0.200f); glVertex3f(cam->x+ 1.0f,cam->y -1.0f,cam->z +  1.0f);
     383  glTexCoord2f(0.800f, 0.200f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z + 1.0f);
     384
     385
     386  // Top face; offset.  White, 50% opaque.
     387
     388  glNormal3f( 0.0f, 1.0f, 0.0f);  glColor4f(0.5,0.5,0.5,.5);
     389
     390  glTexCoord2f(0.005f, 1.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.0f);
     391  glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f,  cam->z +1.0f);
     392  glTexCoord2f(1.995f, 0.005f); glVertex3f(cam->x+ 1.0f,  cam->y+1.0f,  cam->z +1.0f);
     393  glTexCoord2f(1.995f, 1.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z -1.0f);
     394
     395
     396  // Far face.  Green, 50% opaque, non-uniform texture cooridinates.
     397
     398  glNormal3f( 0.0f, 0.0f,-1.0f);  glColor4f(0.2,0.9,0.2,.5);
     399
     400  glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z -1.3f);
     401  glTexCoord2f(2.995f, 2.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.3f);
     402  glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x+ 1.0f,cam->y+  1.0f, cam->z -1.3f);
     403  glTexCoord2f(0.005f, 0.005f); glVertex3f( cam->x+1.0f,cam->y -1.0f, cam->z -1.3f);
     404
     405
     406  // Right face.  Blue; 25% opaque
     407
     408  glNormal3f( 1.0f, 0.0f, 0.0f);  glColor4f(0.2,0.2,0.9,.25);
     409
     410  glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x+ 1.0f,cam->y -1.0f, cam->z -1.0f);
     411  glTexCoord2f(0.995f, 0.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z -1.0f);
     412  glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z + 1.0f);
     413  glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y-1.0f,  cam->z +1.0f);
     414
     415
     416  // Front face; offset.  Multi-colored, 50% opaque.
     417
     418  glNormal3f( 0.0f, 0.0f, 1.0f);
     419
     420  glColor4f( 0.9f, 0.2f, 0.2f, 0.5f);
     421  glTexCoord2f( 0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f,  cam->z +1.0f);
     422  glColor4f( 0.2f, 0.9f, 0.2f, 0.5f);
     423  glTexCoord2f( 0.995f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y-1.0f,  cam->z +1.0f);
     424  glColor4f( 0.2f, 0.2f, 0.9f, 0.5f);
     425  glTexCoord2f( 0.995f, 0.995f); glVertex3f( cam->x+1.0f,  cam->y+1.0f,  cam->z +1.0f);
     426  glColor4f( 0.1f, 0.1f, 0.1f, 0.5f);
     427  glTexCoord2f( 0.005f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f,  cam->z +1.0f);
     428
     429
     430  // Left Face; offset.  Yellow, varying levels of opaque.
     431
     432  glNormal3f(-1.0f, 0.0f, 0.0f);
     433
     434  glColor4f(0.9,0.9,0.2,0.0);
     435  glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z -1.0f);
     436  glColor4f(0.9,0.9,0.2,0.66);
     437  glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x-1.0f,cam->y -1.0f,  cam->z +1.0f);
     438  glColor4f(0.9,0.9,0.2,1.0);
     439  glTexCoord2f(0.995f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f,  cam->z +1.0f);
     440  glColor4f(0.9,0.9,0.2,0.33);
     441  glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.0f);
     442
     443  glEnd();
    469444}
    470445
    471446void BspManager::addFace(int f)
    472447{
    473  face& curFace =  ((face *)(this->bspFile->faces))[f];
    474  if(this->bspFile->Materials[curFace.texture].alpha) this->trasparent.push_back(f);
    475  else this->opal.push_back(f);
    476 }
     448  face& curFace =  ((face *)(this->bspFile->faces))[f];
     449  if(this->bspFile->Materials[curFace.texture].alpha) this->trasparent.push_back(f);
     450  else this->opal.push_back(f);
     451}
Note: See TracChangeset for help on using the changeset viewer.