Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7563 in orxonox.OLD


Ignore:
Timestamp:
May 10, 2006, 12:54:38 PM (18 years ago)
Author:
bottac
Message:

worked on collision detection

Location:
branches/bsp_model/src/lib
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/bsp_model/src/lib/coord/p_node.h

    r7221 r7563  
    198198  /** tells the child that the parent's Direction has changed */
    199199  inline void parentDirChanged () { this->bRelDirChanged = true; }
     200 public:
    200201  /** @returns the last calculated coordinate */
    201202  inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; }
    202 
     203 private:
    203204  static PNode* createNullParent();
    204205  void reparent();
  • branches/bsp_model/src/lib/graphics/importer/bsp_file.h

    r7511 r7563  
    6262leaf;
    6363
    64 typedef struct
     64struct brush
    6565{
    6666  int brushside;        //!< First brushside for brush.
    6767  int n_brushsides;     //!< Number of brushsides for brush.
    6868  int texture;          //!< Texture index.
    69 }
    70 brush;
     69};
    7170
    7271typedef struct
     
    128127}
    129128lightmap;
     129
     130typedef struct
     131{
     132  char name[64];
     133  int flags;
     134  int contents;
     135}
     136BspTexture;
    130137
    131138class BspFile
     
    182189  int numLightMaps;
    183190
     191
     192
    184193  BspTreeNode* build_tree_rec( int i );
    185194  unsigned int loadLightMapToGL(lightmap&);
  • branches/bsp_model/src/lib/graphics/importer/bsp_manager.cc

    r7544 r7563  
    7474
    7575
    76   this->cam = State::getCameraTargetNode()->getAbsCoor();
     76 
    7777  this->viewDir=    State::getCameraTarget()->getAbsCoor() -  State::getCamera()->getAbsCoor() ;
    7878  float d = (cam.x*viewDir.x + cam.y*viewDir.y + cam.z * viewDir.z);
     
    8484
    8585
    86 
    87   this->checkCollision(this->root, &this->cam);   //!< Test Collision Detection
    88 
    89   if (true /*(((int *)(this->bspFile->header))[35] == 0 )  || viscluster < 0*/)  //!< if (sizeof(Visdata) == 0)
     86  this->cam = State::getCameraTargetNode()->getAbsCoor();
     87  //this->checkCollision(this->root, &this->cam);   //!< Test Collision Detection
     88  this->outputStartsOut = true;
     89  this->outputAllSolid = false;
     90  this->outputFraction = 1.0f;
     91 
     92  this->checkCollisionRay(this->root,0.0f,1.0f, &(State::getCameraTargetNode()->getLastAbsCoor()), &this->cam);
     93 
     94  if(this->outputFraction != 1.0f || this->outputAllSolid ) this->drawDebugCube(&this->cam);
     95
     96  if (true || (((int *)(this->bspFile->header))[35] == 0 )  || viscluster < 0)  //!< if (sizeof(Visdata) == 0)
    9097  {
    91     /** Do Frustum culling and draw 'em all **/
    92 
     98   
     99   
     100   
    93101    // Iterate through all Leafspublic final double readLEDouble()
    94102    for(int i = 0; i <  this->bspFile->numLeafs   ; i++ )
     
    96104      // cluster =  (this->bspFile->leaves)[i].cluster;
    97105      leaf& curLeaf = (this->bspFile->leaves)[i];
    98 
    99 
     106      if(curLeaf.cluster<0) continue;
     107     
     108     
     109      /** Do Frustum culling and draw 'em all **/
     110      bool inFrustum = true;
     111      float dMins;
     112      float dMaxs;
     113      Vector dir;
     114      dir.x = this->cam.x - State::getCameraTargetNode()->getLastAbsCoor().x;
     115      dir.y = this->cam.y - State::getCameraTargetNode()->getLastAbsCoor().y;
     116      dir.z = this->cam.z - State::getCameraTargetNode()->getLastAbsCoor().z;
     117      float dist =  dir.x*this->cam.x +dir.y*this->cam.y +dir.z*this->cam.z;
     118      //if(dist < 0) dist = -dist;
     119      dMins = dir.x*(float)curLeaf.mins[0] +dir.y*(float)curLeaf.mins[1] +dir.z*(float)curLeaf.mins[2] ;
     120      dMaxs = dir.x*(float)curLeaf.maxs[0] +dir.y*(float)curLeaf.maxs[1] +dir.z*(float)curLeaf.maxs[2] ;
     121     
     122      if(dMins < 0.0 && dMaxs < 0.0)
     123      {   
     124        //continue;
     125      }
     126   
     127       
    100128      // Iterate through all faces
    101129      for (int j = 0; j < curLeaf.n_leaffaces ; ++j) {
     
    103131        if (f >=0 && !this->isAlreadyVisible(f)) {
    104132          this->alreadyVisible[f] = true;
    105           /*    if(ActLeaf->leafIndex == i)public final double readLEDouble()
    106                 {
    107                         this->alreadyVisible[i] = false;
    108                         this->draw_debug_face(f);
    109                         this->alreadyVisible[i] = true;
    110                 } 
    111                 else */
     133               
    112134          addFace(f); // "visibleFaces.append(f)"
    113135        }
     
    131153      leaf& curLeaf =  (this->bspFile->leaves)[i] ;
    132154      cluster =  curLeaf.cluster;
    133      
    134       if(cluster <0) continue;
     155
     156      if(cluster < 0) continue;
    135157      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       {
     158      visSet =((unsigned char*) (this->bspFile->visData))[v + 8];
     159
     160
     161      if( !((visSet) & ((unsigned char)1 <<(unsigned char) (cluster &  7)))  ) {
    141162        // Iterate through all faces
    142         for (int j = 0; j < curLeaf.n_leaffaces ; ++j)
    143         {
     163        for (int j = 0; j < curLeaf.n_leaffaces ; ++j) {
    144164          const int f = (j +  curLeaf.leafface) % this->bspFile->numFaces;
    145165
     
    152172        }
    153173
    154     }// if
     174      }// if
    155175
    156176    }//for
     
    170190}//draw
    171191
    172 void BspManager::draw_leaf()
    173 {}
     192
    174193
    175194void BspManager::draw_face(int curface)
     
    189208  }
    190209  if(curFace.type == 3) return;
    191 //  if(this->bspFile->Materials[curFace.texture] != NULL)
    192    
     210  //  if(this->bspFile->Materials[curFace.texture] != NULL)
     211
    193212  if(this->lastTex != curFace.texture) {
    194213    this->bspFile->Materials[curFace.texture].mat->select();
    195      this->lastTex = curFace.texture;
    196   }
    197    
    198     if(curFace.lm_index < 0)
    199     {
    200       glActiveTextureARB(GL_TEXTURE1_ARB);
    201       glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap );
    202       glEnable(GL_TEXTURE_2D);
    203     }
    204     else
    205     {
     214    this->lastTex = curFace.texture;
     215  }
     216
     217  if(curFace.lm_index < 0) {
     218    glActiveTextureARB(GL_TEXTURE1_ARB);
     219    glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap );
     220    glEnable(GL_TEXTURE_2D);
     221  } else {
    206222    glActiveTextureARB(GL_TEXTURE1_ARB);
    207223    glBindTexture(GL_TEXTURE_2D, this->bspFile->glLightMapTextures[curFace.lm_index]);
    208224    glEnable(GL_TEXTURE_2D);
    209     }
    210    
    211     glColor4f(3.0,3.0,3.0,1.0);
     225  }
     226
     227  glColor4f(3.0,3.0,3.0,1.0);
    212228  glEnableClientState(GL_VERTEX_ARRAY );
    213229  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
    214230  glEnableClientState(GL_NORMAL_ARRAY );
    215231  //  glEnableClientState(GL_COLOR_ARRAY);
    216  
     232
    217233
    218234  glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0]));
    219  
     235
    220236  glClientActiveTextureARB(GL_TEXTURE0_ARB);
    221237  glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0]));
    222238  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    223  
     239
    224240  glClientActiveTextureARB(GL_TEXTURE1_ARB);
    225241  glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1]));
    226242  //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    227243
    228  
     244
    229245  glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0]));
    230246  // glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0]));
     
    292308    this->lastTex = Face->texture;
    293309  }
    294  
    295  
    296  
    297   if(Face->lm_index < 0)
    298   {
     310
     311
     312
     313  if(Face->lm_index < 0) {
    299314    glActiveTextureARB(GL_TEXTURE1_ARB);
    300315    glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap);
    301316    glEnable(GL_TEXTURE_2D);
    302   }
    303   else
    304   {
     317  } else {
    305318    glActiveTextureARB(GL_TEXTURE1_ARB);
    306319    glBindTexture(GL_TEXTURE_2D, this->bspFile->glLightMapTextures[Face->lm_index]);
    307320    glEnable(GL_TEXTURE_2D);
    308 }
    309     glColor4f(3.0,3.0,3.0,1.0);
    310  
    311   glEnable( GL_AUTO_NORMAL);
     321  }
     322  glColor4f(3.0,3.0,3.0,1.0);
     323
     324  //glEnable( GL_AUTO_NORMAL);
    312325  glEnableClientState(GL_VERTEX_ARRAY );
    313326  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
     
    315328    glFrontFace(GL_CW);
    316329    //PRINTF(0)("BSP Manager: Face->size[0]: %i . \n", Face->size[0]);
    317  
    318  
     330
     331
    319332    glEnableClientState(GL_NORMAL_ARRAY );
    320333
    321334    glVertexPointer(3, GL_FLOAT,44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).position[0]));
    322    
    323    
     335
     336
    324337    glClientActiveTextureARB(GL_TEXTURE0_ARB);
    325338    glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[0][0]));
    326339    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    327340
    328    
     341
    329342    glClientActiveTextureARB(GL_TEXTURE1_ARB);
    330343    glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[1][0]));
    331344    //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    332345
    333    
     346
    334347    glNormalPointer( GL_FLOAT, 44,&((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).normal[0]) );
    335348
    336  
     349
    337350
    338351
     
    341354                     & (     (((GLuint*)  (this->bspFile->patchIndexes))[7*8*2*(Face->meshvert+i)+ row*2*8]  ))  );
    342355    }
    343    
     356
    344357    glFrontFace(GL_CCW);
    345358  }
    346359  glDisableClientState(GL_TEXTURE0_ARB);
    347360  glDisableClientState(GL_TEXTURE1_ARB);
    348   glDisable(GL_AUTO_NORMAL);
     361  //glDisable(GL_AUTO_NORMAL);
    349362  glDisableClientState(GL_VERTEX_ARRAY );
    350363  glDisableClientState(GL_TEXTURE_COORD_ARRAY );
    351  
     364
    352365  glBegin(GL_QUADS);
    353  
     366
    354367  glEnd();
    355368}
     
    376389}
    377390
     391void BspManager::checkBrushRay(brush* curBrush)
     392{
     393  float EPSILON = 0.000001;
     394  float startDistance;
     395  float endDistance;
     396 
     397  float startFraction = -1.0f;
     398  float endFraction = 1.0f;
     399  bool startsOut = false;
     400  bool endsOut = false;
     401 
     402  Vector inputStart = State::getCameraTargetNode()->getLastAbsCoor();
     403  Vector inputEnd   = State::getCameraTargetNode()->getAbsCoor();
     404
     405  for (int i = 0; i < curBrush->n_brushsides; i++)
     406  {
     407    brushside& curBrushSide =   this->bspFile->brushSides[curBrush->brushside + i]   ;
     408    plane& curPlane  =   this->bspFile->planes[curBrushSide.plane] ;
     409
     410       startDistance = inputStart.x * curPlane.x + inputStart.y * curPlane.y+ inputStart.z * curPlane.z - curPlane.d;
     411     endDistance = inputEnd.x * curPlane.x +inputEnd.y * curPlane.y +inputEnd.z * curPlane.z -curPlane.d;
     412
     413    if (startDistance > 0)
     414      startsOut = true;
     415    if (endDistance > 0)
     416      endsOut = true;
     417
     418        // make sure the trace isn't completely on one side of the brush
     419    if (startDistance > 0 && endDistance > 0)
     420    {   // both are in front of the plane, its outside of this brush
     421      return;
     422    }
     423    if (startDistance <= 0 && endDistance <= 0)
     424    {   // both are behind this plane, it will get clipped by another one
     425      continue;
     426    }
     427
     428        // MMM... BEEFY
     429    if (startDistance > endDistance)
     430    {   // line is entering into the brush
     431      float fraction = (startDistance - EPSILON) / (startDistance - endDistance);  // *
     432      if (fraction > startFraction)
     433        startFraction = fraction;
     434    }
     435    else
     436    {   // line is leaving the brush
     437      float fraction = (startDistance + EPSILON) / (startDistance - endDistance);  // *
     438      if (fraction < endFraction)
     439        endFraction = fraction;
     440    }
     441   
     442  }
     443   if (startsOut == false)
     444    {
     445      this->outputStartsOut = false;
     446      if (endsOut == false)
     447        this->outputAllSolid = true;
     448      return;
     449    }
     450
     451  if (startFraction < endFraction)
     452  {
     453    if (startFraction > -1.0f && startFraction < outputFraction)
     454    {
     455      if (startFraction < 0)
     456        startFraction = 0;
     457      this->outputFraction = startFraction;
     458    }
     459  }
     460 
     461}
     462
     463void BspManager::checkCollisionRay(BspTreeNode* node, float startFraction, float endFraction, Vector* start, Vector* end)
     464{
     465
     466
     467  float EPSILON = 0.000001;
     468  float  endDistance = (end)->x * (node->plane.x) +(end)->y * (node->plane.y) +(end)->z * (node->plane.z)  - node->d;
     469  float  startDistance = (start)->x * (node->plane.x)+ (start)->y * (node->plane.y)+ (start)->z * (node->plane.z)- node->d;
     470
     471
     472  if(node->isLeaf) {
     473    leaf& curLeaf = this->bspFile->leaves[node->leafIndex];
     474    for (int i = 0; i <  curLeaf.n_leafbrushes ; i++) {
     475      brush& curBrush = this->bspFile->brushes[((int*)(this->bspFile->leafBrushes))[curLeaf.leafbrush_first+i]];
     476      //object *brush = &BSP.brushes[BSP.leafBrushes[leaf->firstLeafBrush + i]];
     477      if (curBrush.n_brushsides > 0   &&
     478        ((((BspTexture*)(this->bspFile->textures))[curBrush.texture]).contents & 1))
     479      // CheckBrush( brush );
     480      this->checkBrushRay(&curBrush);
     481      if(curBrush.n_brushsides <=0) this->outputAllSolid = true;
     482    }
     483    return;
     484  }
     485
     486
     487  if (startDistance >= 0 && endDistance >= 0)     // A
     488  {   // both points are in front of the plane
     489    // so check the front child
     490    this->checkCollisionRay(node->left,0,0,start,end);
     491  } else if (startDistance < 0 && endDistance < 0)  // B
     492  {   // both points are behind the plane
     493    // so check the back child
     494    this->checkCollisionRay(node->right,0,0,start,end);
     495  } else                                            // C
     496  {   // the line spans the splitting plane
     497    int side;
     498    float fraction1, fraction2, middleFraction;
     499    Vector middle;
     500
     501    // STEP 1: split the segment into two
     502    if (startDistance < endDistance) {
     503      side = 1; // back
     504      float inverseDistance = 1.0f / (startDistance - endDistance);
     505      fraction1 = (startDistance + EPSILON) * inverseDistance;
     506      fraction2 = (startDistance + EPSILON) * inverseDistance;
     507    } else if (endDistance < startDistance) {
     508      side = 0; // front(start)->x * (node->plane.x)+
     509      float inverseDistance = 1.0f / (startDistance - endDistance);
     510      fraction1 = (startDistance + EPSILON) * inverseDistance;
     511      fraction2 = (startDistance - EPSILON) * inverseDistance;
     512    } else {
     513      side = 0; // front
     514      fraction1 = 1.0f;
     515      fraction2 = 0.0f;
     516    }
     517
     518    // STEP 2: make sure the numbers are valid
     519    if (fraction1 < 0.0f) fraction1 = 0.0f;
     520    else if (fraction1 > 1.0f) fraction1 = 1.0f;
     521    if (fraction2 < 0.0f) fraction2 = 0.0f;
     522    else if (fraction2 > 1.0f) fraction2 = 1.0f;
     523
     524    // STEP 3: calculate the middle point for the first side
     525    middleFraction = startFraction +
     526                     (endFraction - startFraction) * fraction1;
     527
     528    middle.x = start->x + fraction1 * (end->x - start->x);
     529    middle.y = start->y + fraction1 * (end->y - start->y);
     530    middle.z = start->z + fraction1 * (end->z - start->z);
     531
     532    // STEP 4: check the first side
     533    //CheckNode( node->children[side], startFraction, middleFraction, start, middle );
     534    if(side == 0) this->checkCollisionRay(node->left,startFraction, middleFraction, start, &middle );
     535
     536    else this->checkCollisionRay(node->right,startFraction, middleFraction,
     537                                   start, &middle );
     538
     539    // STEP 5: calculate the middle point for the second side
     540    middleFraction = startFraction +
     541                     (endFraction - startFraction) * fraction2;
     542
     543    middle.x = start->x + fraction2 * (end->x - start->x);
     544    middle.y = start->y + fraction2 * (end->y - start->y);
     545    middle.z = start->z + fraction2 * (end->z - start->z);
     546
     547    // STEP 6: check the second side
     548    if(side == 1)this->checkCollisionRay(node->left,middleFraction, endFraction, &middle, end);
     549
     550    else this->checkCollisionRay(node->right,middleFraction, endFraction,&middle, end );
     551
     552
     553  }
     554
     555}
    378556void  BspManager::checkCollision(BspTreeNode* node, Vector* cam)
    379557{
     558  Vector next = this->cam;
     559  next.x =   (State::getCameraTargetNode()->getLastAbsCoor()).x ;
     560  next.y =   (State::getCameraTargetNode()->getLastAbsCoor()).y ;
     561  next.z =   (State::getCameraTargetNode()->getLastAbsCoor()).z ;
     562
    380563  float dist = 0;
    381564  if(!(node->isLeaf)) {
     
    399582    leaf& camLeaf =  ((leaf *)(this->bspFile->leaves))[(node->leafIndex ) ];
    400583
    401     if (camLeaf.cluster < 0) {this->drawDebugCube(&this->cam);}
     584    if (camLeaf.cluster < 0) {this->drawDebugCube(&this->cam); this->drawDebugCube(&next);}
    402585
    403586
  • branches/bsp_model/src/lib/graphics/importer/bsp_manager.h

    r7395 r7563  
    1717
    1818
    19 // FORWARD DECLARATION
     19// FORWARD DECLARATIONS
    2020class  BspFile;
    2121class BspTreeLeaf;
     
    2424class set;
    2525struct face;
     26struct brush;
    2627
    2728class BspManager
     
    3334  // Functions
    3435  void draw();
    35   void draw_leaf();
    36   void draw_debug_face(int Face);
     36  void draw_debug_face(int Face);
    3737  void draw_face(int Face);
    3838  void draw_patch(face* Face);
     
    4141private:
    4242  // Functions
     43  BspTreeNode* getLeaf(BspTreeNode*  node,   Vector* cam) ;  //!< Traverses the tree
     44  void  checkCollision(BspTreeNode* node, Vector* cam);
     45  void  checkCollisionRay(BspTreeNode * node,float startFraction, float endFraction, Vector* start, Vector* end);
     46  void  checkBrushRay(brush* curBrush);
     47  void drawDebugCube(Vector* cam);
     48  bool isAlreadyVisible(int Face);
     49  void addFace(int Face);
     50 
     51  // Data
    4352  BspFile*  bspFile;
    4453  BspTreeNode* root;
    45   BspTreeNode* getLeaf(BspTreeNode*  node,   Vector* cam) ;  //!< Traverses the tree
    46   void  checkCollision(BspTreeNode* node, Vector* cam);
    47   void drawDebugCube(Vector* cam);
    4854  Vector cam;
    4955  Vector ship;
    5056  Vector  viewDir;
     57  int lastTex;
     58 
     59  //obsolete
     60  bool  outputStartsOut;
     61  bool  outputAllSolid;
     62  float outputFraction;
     63 
    5164  bool * alreadyVisible;
    52   bool isAlreadyVisible(int Face);
    53   void addFace(int Face);
    54   int lastTex;
    55 
    56 
    5765  // Deques to store the visible faces
    5866  ::std::deque<int> trasparent; //!< the ones with transparancy go here
Note: See TracChangeset for help on using the changeset viewer.