Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 15, 2006, 9:34:48 PM (18 years ago)
Author:
patrick
Message:

merged the bsp branche back to trunk

File:
1 edited

Legend:

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

    r8186 r8490  
    3939#include "movie_player.h"
    4040
    41 #include "cd_engine.h"
    42 
    4341#include "world_entity.h"
    4442
     
    4745
    4846
     47
    4948//CREATE_FACTORY( BspManager, CL_BSP_MODEL);
    5049
    51 BspManager::BspManager()
    52 {
     50BspManager::BspManager(WorldEntity* parent)
     51{
     52 
     53  this->parent = parent;
    5354  /*// open a BSP file
    5455  this->bspFile = new BspFile();
     
    5960  this->alreadyVisible = new bool [this->bspFile->numFaces];
    6061  */
    61   CDEngine::getInstance()->setBSPModel(this);
     62
    6263}
    6364
     
    7677void BspManager::load(const char* fileName, float scale)
    7778{
    78   //this->setClassID(CL_BSP_MODEL, "BspManager");
    79     // open a BSP file
     79  // open a BSP file
    8080  this->bspFile = new BspFile();
    8181  this->bspFile->scale =  scale;
     
    8585  this->alreadyVisible = new bool [this->bspFile->numFaces];
    8686
    87 
     87  this->outputFraction = 1.0f;
    8888}
    8989/*
     
    102102*/
    103103
     104const void BspManager::tick(float time)
     105{
     106
     107  if(!this->bspFile->MovieMaterials.empty())
     108  {
     109      ::std::vector<MoviePlayer *>::iterator it = this->bspFile->MovieMaterials.begin() ;
     110      while(it != this->bspFile->MovieMaterials.end())
     111      {
     112         (*it)->tick(time);
     113        it++;
     114      }
     115 //this->bspFile->MovieMaterials.front()->tick(time );
     116
     117
     118  }
     119
     120}
    104121const void BspManager::draw()
    105122{
     
    109126  this->out1 = this->out;
    110127  this->out2 = this->out;
    111   if(this->collPlane != NULL)
    112   {
    113   this->out1.x += this->collPlane->x*5.0;
    114   this->out1.y += this->collPlane->y*5.0;
    115   this->out1.z += this->collPlane->z*5.0;
    116 
    117   this->out2.x += this->collPlane->x*10.0;
    118   this->out2.y += this->collPlane->y*10.0;
    119   this->out2.z += this->collPlane->z*10.0;
     128  if(this->collPlane != NULL) {
     129    this->out1.x += this->collPlane->x*5.0;
     130    this->out1.y += this->collPlane->y*5.0;
     131    this->out1.z += this->collPlane->z*5.0;
     132
     133    this->out2.x += this->collPlane->x*10.0;
     134    this->out2.y += this->collPlane->y*10.0;
     135    this->out2.z += this->collPlane->z*10.0;
    120136  }
    121137  this->drawDebugCube(&this->out1);
    122138  this->drawDebugCube(&this->out2);
     139
    123140  */
     141
     142
    124143  // Draw Debug Terrain
    125144  /*
     
    148167
    149168
    150   // this->viewDir=    State::getCameraTarget()->getAbsCoor() -  State::getCamera()->getAbsCoor() ;
     169  this->viewDir=    State::getCamera()->getAbsDirX();
    151170  float d = (cam.x*viewDir.x + cam.y*viewDir.y + cam.z * viewDir.z);
    152171
     
    157176
    158177
    159   this->cam = State::getCameraTargetNode()->getAbsCoor();
    160   // this->checkCollision(this->root, &this->cam);   //!< Test Collision Detection
     178
     179 // this->checkCollision(this->root, &this->cam);   //!< Test Collision Detection
     180
     181
    161182  this->outputStartsOut = true;
    162183  this->outputAllSolid = false;
    163184  this->outputFraction = 1.0f;
    164185
    165   this->checkCollisionRay(this->root,0.0f,1.0f, &(State::getCameraTargetNode()->getLastAbsCoor()), &this->cam);
    166 
    167   //if(this->outputFraction != 1.0f || this->outputAllSolid ) this->drawDebugCube(&this->cam);
    168 
    169186  if ( viscluster < 0  || ((int *)(this->bspFile->header))[35] == 0 )  //!< if (sizeof(Visdata) == 0)
    170187  {
     
    172189
    173190
    174     // Iterate through all Leafspublic final double readLEDouble()
     191    // Iterate through all Leafs
    175192    for(int i = 0; i <  this->bspFile->numLeafs   ; i++ )
    176193    {
     
    179196      if(curLeaf.cluster<0) continue;
    180197
    181 
    182198      /** Do Frustum culling and draw 'em all **/
    183       bool inFrustum = true;
    184 
    185       Vector dir;
    186       dir.x = State::getCameraNode()->getAbsDirX().x;
    187       dir.y =  State::getCameraNode()->getAbsDirX().y;
    188       dir.z =  State::getCameraNode()->getAbsDirX().z;
     199
     200      Vector dir = State::getCameraNode()->getAbsDirX();
     201
    189202      float dist =  dir.x*this->cam.x +dir.y*this->cam.y +dir.z*this->cam.z;
    190203      //if(dist < 0) dist = -dist;
     
    192205      const float dMaxs = dir.x*(float)curLeaf.maxs[0] +dir.y*(float)curLeaf.maxs[1] +dir.z*(float)curLeaf.maxs[2] - dist ;
    193206
    194       if(dMins < -150.0 && dMaxs < -150.0) {
     207      if(dMins < -300.0 && dMaxs < -300.0) {
    195208        continue;
    196209      }
    197       if( (this->cam - Vector(curLeaf.mins[0],curLeaf.mins[1], curLeaf.mins[2])).len() > 3000  && (this->cam - Vector(curLeaf.maxs[0],curLeaf.maxs[1], curLeaf.maxs[2])).len() > 3000) {
     210      if( (this->cam - Vector(curLeaf.mins[0],curLeaf.mins[1], curLeaf.mins[2])).len() > 2000  && (this->cam - Vector(curLeaf.maxs[0],curLeaf.maxs[1], curLeaf.maxs[2])).len() > 2000) {
    198211        continue;
    199212      }
     
    206219        if (f >=0 && !this->isAlreadyVisible(f)) {
    207220          this->alreadyVisible[f] = true;
    208 
    209221          addFace(f); // "visibleFaces.append(f)"
    210222        }
     
    302314    return;
    303315  }
    304   if(curFace.type != 1) return;
     316 // if(curFace.type != 1) return;
    305317  if((char*)(this->bspFile->textures)[curFace.texture*72]== 0) return;
    306318
    307319  if(this->lastTex != curFace.texture) {
    308320    if(this->bspFile->Materials[curFace.texture].animated) {
    309       glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_COLOR);
    310       glEnable(GL_BLEND);
     321     // glBlendFunc(GL_ZERO,GL_ONE);
     322
     323
     324
    311325      if(this->bspFile->Materials[curFace.texture].aviMat->getStatus() == 2) this->bspFile->Materials[curFace.texture].aviMat->start(0);
    312       this->bspFile->Materials[curFace.texture].aviMat->tick(0.005);
     326      //this->bspFile->Materials[curFace.texture].aviMat->tick(0.005);
    313327      int n =  this->bspFile->Materials[curFace.texture].aviMat->getTexture();
    314328      glActiveTextureARB(GL_TEXTURE0_ARB);
    315329      glBindTexture(GL_TEXTURE_2D, n );
    316       glDisable(GL_BLEND);
     330      this->lastTex = curFace.texture;
     331
    317332    } else {
    318333      this->bspFile->Materials[curFace.texture].mat->select();
     
    322337
    323338  if(curFace.lm_index < 0) {
     339    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    324340    glActiveTextureARB(GL_TEXTURE1_ARB);
    325341    glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap );
    326342    glEnable(GL_TEXTURE_2D);
    327343  } else {
     344   // glEnable(GL_BLEND);
     345    //glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
     346    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    328347    glActiveTextureARB(GL_TEXTURE1_ARB);
    329348    glBindTexture(GL_TEXTURE_2D, this->bspFile->glLightMapTextures[curFace.lm_index]);
    330349    glEnable(GL_TEXTURE_2D);
     350  //  glDisable(GL_BLEND);
    331351  }
    332352
     
    573593  float startFraction = -1.0f;
    574594  float endFraction = 1.0f;
    575   bool startsOut = false;
    576   bool endsOut = false;
    577 
    578   Vector inputStart = State::getCameraTargetNode()->getLastAbsCoor();
    579   Vector inputEnd   = State::getCameraTargetNode()->getAbsCoor();
     595  bool  startsOut = false;
     596  bool  endsOut = false;
     597
     598 // Vector inputStart = State::getCameraTargetNode()->getLastAbsCoor();
     599 // Vector inputEnd   = State::getCameraTargetNode()->getAbsCoor();
     600
     601  for (int i = 0; i < curBrush->n_brushsides; i++) {
     602    brushside& curBrushSide =   this->bspFile->brushSides[curBrush->brushside + i]   ;
     603    plane& curPlane  =   this->bspFile->planes[curBrushSide.plane] ;
     604
     605    startDistance = inputStart.x * curPlane.x + inputStart.y * curPlane.y+ inputStart.z * curPlane.z - curPlane.d;
     606    endDistance = inputEnd.x * curPlane.x +inputEnd.y * curPlane.y +inputEnd.z * curPlane.z -curPlane.d;
     607
     608    if (startDistance > 0)
     609      startsOut = true;
     610    if (endDistance > 0)
     611      endsOut = true;
     612
     613    // make sure the trace isn't completely on one side of the brush
     614    if (startDistance > 0 && endDistance > 0) {   // both are in front of the plane, its outside of this brush
     615      return;
     616    }
     617    if (startDistance <= 0 && endDistance <= 0) {   // both are behind this plane, it will get clipped by another one
     618      continue;
     619    }
     620
     621    // MMM... BEEFY
     622    if (startDistance > endDistance) {   // line is entering into the brush
     623      float fraction = (startDistance - EPSILON) / (startDistance - endDistance);  // *
     624      if (fraction > startFraction)
     625        startFraction = fraction;
     626      // store plane
     627      this->collPlane = &curPlane;
     628
     629    } else {   // line is leaving the brush
     630      float fraction = (startDistance + EPSILON) / (startDistance - endDistance);  // *
     631      if (fraction < endFraction)
     632        endFraction = fraction;
     633      // store plane
     634      this->collPlane = & curPlane;
     635
     636    }
     637
     638  }
     639  if (startsOut == false) {
     640    this->outputStartsOut = false;
     641    if (endsOut == false)
     642      this->outputAllSolid = true;
     643    return;
     644  }
     645
     646  if (startFraction < endFraction) {
     647    if (startFraction > -1.0f && startFraction < outputFraction) {
     648      if (startFraction < 0)
     649        startFraction = 0;
     650      this->outputFraction = startFraction;
     651    }
     652  }
     653
     654}
     655
     656void BspManager::checkBrushRayN(brush* curBrush, Vector& inputStart, Vector& inputEnd)
     657{
     658  float EPSILON = 0.000001;
     659  float startDistance;
     660  float endDistance;
     661
     662  float startFraction = -1.0f;
     663  float endFraction = 1.0f;
     664  bool  startsOut = false;
     665  bool  endsOut = false;
     666
     667  //Vector inputStart = State::getCameraTargetNode()->getLastAbsCoor();
     668  //Vector inputEnd   = State::getCameraTargetNode()->getAbsCoor();
    580669
    581670  for (int i = 0; i < curBrush->n_brushsides; i++) {
     
    736825
    737826  float EPSILON = 0.000001;
    738   float  endDistance = (end)->x * (node->plane.x) +(end)->y * (node->plane.y) +(end)->z * (node->plane.z)  - node->d;
    739   float  startDistance = (start)->x * (node->plane.x)+ (start)->y * (node->plane.y)+ (start)->z * (node->plane.z)- node->d;
    740 
    741 
    742   if(node->isLeaf) {
     827
     828  float endDistance = end->dot(node->plane) - node->d;
     829  float startDistance = start->dot(node->plane) - node->d;
     830
     831
     832  if( node->isLeaf) {
    743833    leaf& curLeaf = this->bspFile->leaves[node->leafIndex];
    744834    for (int i = 0; i <  curLeaf.n_leafbrushes ; i++) {
     
    793883
    794884    // STEP 3: calculate the middle point for the first side
    795     middleFraction = startFraction +
    796         (endFraction - startFraction) * fraction1;
    797 
    798     middle.x = start->x + fraction1 * (end->x - start->x);
    799     middle.y = start->y + fraction1 * (end->y - start->y);
    800     middle.z = start->z + fraction1 * (end->z - start->z);
     885    middleFraction = startFraction + (endFraction - startFraction) * fraction1;
     886    middle = (*start) + ((*end) - (*start)) * fraction1;
     887
    801888
    802889    // STEP 4: check the first side
     
    805892
    806893    else this->checkCollisionRayN(node->right,startFraction, middleFraction,
    807                                  start, &middle );
     894                                    start, &middle );
    808895
    809896    // STEP 5: calculate the middle point for the second side
    810     middleFraction = startFraction +
    811         (endFraction - startFraction) * fraction2;
    812 
    813     middle.x = start->x + fraction2 * (end->x - start->x);
    814     middle.y = start->y + fraction2 * (end->y - start->y);
    815     middle.z = start->z + fraction2 * (end->z - start->z);
     897    middleFraction = startFraction + (endFraction - startFraction) * fraction2;
     898    middle = (*start) + ((*end) - (*start)) * fraction2;
    816899
    817900    // STEP 6: check the second side
     
    824907
    825908}
     909void BspManager::checkCollisionBox(void)
     910{
     911
     912};
     913
     914void BspManager::TraceBox( Vector& inputStart, Vector& inputEnd,
     915               Vector& inputMins, Vector& inputMaxs )
     916{
     917  if (inputMins.x == 0 && inputMins.y == 0 && inputMins.z == 0 &&
     918      inputMaxs.x == 0 && inputMaxs.y == 0 && inputMaxs.z == 0)
     919  {   // the user called TraceBox, but this is actually a ray
     920   //!> FIXME TraceRay( inputStart, inputEnd );
     921  }
     922  else
     923  {   // setup for a box
     924    //traceType = TT_BOX;
     925    this->traceMins = inputMins;
     926    this->traceMaxs = inputMaxs;
     927    this->traceExtents.x = -traceMins.x > traceMaxs.x ?
     928        -traceMins.x : traceMaxs.x;
     929    this->traceExtents.y = -traceMins.y > traceMaxs.y ?
     930        -traceMins.y : traceMaxs.y;
     931    this->traceExtents.z = -traceMins.z > traceMaxs.z ?
     932        -traceMins.z : traceMaxs.z;
     933   //!> FIXME Trace( inputStart, inputEnd );
     934  }
     935}
    826936
    827937void BspManager::checkCollision(WorldEntity* worldEntity)
    828938{
    829   return;
    830 
     939 
     940  this->outputStartsOut = true;
     941  this->outputAllSolid = false;
     942  this->outputFraction = 1.0f;
     943 
    831944  Vector position = worldEntity->getAbsCoor();
    832945
    833946
    834947  Vector forwardDir = worldEntity->getAbsDirX();
    835   forwardDir.x =2.0*forwardDir.x;
    836   forwardDir.y =2.0*forwardDir.y;
    837   forwardDir.z =2.0*forwardDir.z;
     948  forwardDir = forwardDir * 8.0f;
    838949
    839950  Vector upDir = worldEntity->getAbsDirY();
    840   Vector dest = position;
     951  upDir.x = 0.0;
     952  upDir.y = 1.0;
     953  upDir.z = 0.0;
     954  Vector dest;
     955  /*
    841956  dest.x  += forwardDir.x;
    842957  dest.y  += forwardDir.y;
    843958  dest.z  += forwardDir.z;
    844   Vector out = Vector(-1875.0,-1875.0,-1875.0);
    845   if(!worldEntity->isA(CL_PLAYABLE)) {
    846 
    847 
    848 
    849 
     959  */
     960
     961  dest = worldEntity->getAbsCoor() - upDir*40.0;
     962  Vector out = dest;
     963
     964
     965
     966
     967  bool collision = false;
     968  Vector position1 = position + Vector(0.0,0.1,0.0);
     969  Vector position2 = position + Vector(0.0,0.2,0.0);
     970  Vector dest1 = position1 + forwardDir;
     971  Vector dest2 = position2 + forwardDir;
     972  dest = position - Vector(0.0, 40.0,0.0);
     973  Vector out1;
     974  Vector out2;
     975
     976
     977  float height = 40;
     978 
     979  this->inputStart =  position;
     980  this->inputEnd =   dest;
     981  this->checkCollisionRayN(this->root,0.0f,1.0f, &position, &dest );
     982
     983  PRINTF(0)(" checking collision: %s, solid = %i, fraction = %f\n", worldEntity->getClassName(), this->outputAllSolid, this->outputFraction);
     984  PRINTF(0)("checking collision!! Pos.Coords: %f , %f , %f\n", position.x , position.y, position.z);
     985  PRINTF(0)("checking collision!! Dest.Coords: %f , %f , %f\n", dest.x , dest.y, dest.z); 
     986//   position1.debug();
     987
     988  if( this->outputFraction == 1.0f)
     989  {
     990    if(this->outputAllSolid)
     991    {
     992      this->collPlane = new plane;
     993      this->collPlane->x = 1.0f;
     994      this->collPlane->y = 0.0f;
     995      this->collPlane->z = 0.0f;
     996      collision = true;
     997    }
     998    else
     999      collision = false;
     1000
     1001
     1002    out = dest;
    8501003  }
    8511004  else {
    852     bool collision = false;
    853     Vector position1 = position + Vector(0.0,0.1,0.0);
    854     Vector position2 = position + Vector(0.0,0.2,0.0);
    855     Vector dest1 = position1 + forwardDir;
    856     Vector dest2 = position2 + forwardDir;
    857     dest = position - upDir;
    858     Vector out1;
    859     Vector out2;
    860 
    861     this->checkCollisionRayN(this->root,0.0f,1.0f, &position1, &dest1 );
    862     if(this->outputFraction == 1.0f)  out1 = dest;
    863     else {
    864       collision = true;
    865       out1.x = position1.x + (dest1.x -position1.x) * this->outputFraction;
    866       out1.y = position1.y + (dest1.y -position1.y) * this->outputFraction;
    867       out1.z = position1.z + (dest1.z - position1.z) * this->outputFraction;
    868 
    869     }
    870 
    871     this->checkCollisionRayN(this->root,0.0f,1.0f, &position2, &dest2 );
    872     if(this->outputFraction == 1.0f) out2= dest;
    873     else
    874     {
    875       collision = true;
    876     out2.x = position2.x + (dest2.x -position2.x) * this->outputFraction;
    877     out2.y = position2.y + (dest2.y -position2.y) * this->outputFraction;
    878     out2.z = position2.z + (dest2.z - position2.z) * this->outputFraction;
    879 
    880     }
    881 
    882     this->checkCollisionRayN(this->root,0.0f,1.0f, &position, &dest );
    883     if(this->outputFraction == 1.0f) out = dest;
    884     else
    885     {
     1005
    8861006    collision = true;
    8871007    out.x = position.x + (dest.x -position.x) * this->outputFraction;
     
    8891009    out.z = position.z + (dest.z -position.z) * this->outputFraction;
    8901010
    891     //Vector out3 = out + Vector(3*this->collPlane->x,3*this->collPlane->y,3*this->collPlane->z);
    892     //this->out = out;
    893     //this->out1 = out1;
    894     //this->out2 = out2;
    895     //this->drawDebugCube(&out1);
    896     //this->drawDebugCube(&out2);
    897 
    898     //this->drawDebugCube(&out3);
    899 
    900     }
    901 
    902     // Return the normal here: Normal's stored in this->collPlane;
    903 //    if(collision) worldEntity->collidesWithGround(out,out1,out2);
    904 
    905   }
    906 
    907 }
     1011    Vector out3 = out + Vector(height*this->collPlane->x,height*this->collPlane->y,height*this->collPlane->z);
     1012    this->out = out;
     1013  }
     1014
     1015  // Return the normal here: Normal's stored in this->collPlane;
     1016  if( collision) {
     1017    PRINTF(0)("We got a collision!! Are you sure: outputFraction = %f\n", this->outputFraction);
     1018    worldEntity->registerCollision(this->parent, worldEntity, Vector(this->collPlane->x, this->collPlane->y, this->collPlane->z), out);
     1019  }
     1020
     1021}
     1022
    9081023
    9091024void  BspManager::checkCollision(BspTreeNode* node, Vector* cam)
     
    9381053      this->drawDebugCube(&this->cam);
    9391054      this->drawDebugCube(&next);
    940       State::getPlayer()->getPlayable()->setRelCoor(-100,-100,-100);
    941       State::getPlayer()->getPlayable()->collidesWith(NULL, State::getCameraTargetNode()->getLastAbsCoor());
     1055     // State::getPlayer()->getPlayable()->setRelCoor(-100,-100,-100);
     1056      //State::getPlayer()->getPlayable()->collidesWith(NULL, State::getCameraTargetNode()->getLastAbsCoor());
    9421057    }
    9431058
Note: See TracChangeset for help on using the changeset viewer.