Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model/src/lib/graphics/importer/bsp_manager.cc @ 7801

Last change on this file since 7801 was 7801, checked in by bottac, 18 years ago

Support for animated textures in bsp maps started.

File size: 24.3 KB
RevLine 
[7353]1/*
2   orxonox - the future of 3D-vertical-scrollers
[7511]3 
[7353]4   Copyright (C) 2006 orx
[7511]5 
[7353]6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
[7511]10 
[7353]11   ### File Specific:
12   main-programmer: bottac@ee.ethz.ch
13*/
14
15
16#include "vector.h"
17#include "bsp_file.h"
18#include "bsp_manager.h"
[7385]19#include "bsp_tree_leaf.h"
[7353]20#include "p_node.h"
21#include "state.h"
22#include "debug.h"
23#include "material.h"
24#include "camera.h"
25#include "vertex_array_model.h"
[7579]26#include "world_entities/player.h"
27#include "world_entities/playable.h"
[7596]28#include "util/loading/resource_manager.h"
[7385]29// STL Containers
30#include <vector>
31#include <deque>
[7801]32#include "movie_player.h"
[7353]33
34
35
36
37BspManager::BspManager()
38{
[7395]39  // open a BSP file
40  this->bspFile = new BspFile();
[7596]41  this->bspFile->read(ResourceManager::getFullName("test.bsp").c_str());
[7395]42  this->bspFile->build_tree();
43  this->root  = this->bspFile->get_root();
44  this->alreadyVisible = new bool [this->bspFile->numFaces];
[7353]45}
46
47void BspManager::draw()
48{
49
50
51
52
53
[7395]54  // Draw Debug Terrain
55  /*
[7511]56  this->bspFile->Materials[0]->select();
[7395]57  for(int i = 0; i <  this->bspFile->numPatches ; i++)
[7511]58        {
59                this->bspFile->VertexArrayModels[i]->draw();
60   
61        }
[7395]62  */
[7353]63
64
65
[7395]66  // erase alreadyVisible
67  for(int i = 0; i < this->bspFile->numFaces; i++) this->alreadyVisible[i] = false;
68  float tmp = 0;
[7579]69  //this->opal.clear();
70  //this->trasparent.clear();
[7353]71  // Find all visible faces...
72
[7395]73  this->cam = State::getCamera()->getAbsCoor() ;
[7579]74  //this->ship = State::getCameraTargetNode()->getAbsCoor();
[7353]75
76
77
78
[7563]79 
[7579]80 // this->viewDir=    State::getCameraTarget()->getAbsCoor() -  State::getCamera()->getAbsCoor() ;
[7395]81  float d = (cam.x*viewDir.x + cam.y*viewDir.y + cam.z * viewDir.z);
[7353]82
[7395]83  BspTreeNode*  ActLeaf = this->getLeaf(this->bspFile->root, &ship);
84  int viscluster = -1;
85  viscluster =((leaf*)(this->bspFile->leaves))[ ActLeaf->leafIndex].cluster; // get the players cluster (viscluster)
[7353]86
87
88
[7563]89  this->cam = State::getCameraTargetNode()->getAbsCoor();
[7801]90 // this->checkCollision(this->root, &this->cam);   //!< Test Collision Detection
[7563]91  this->outputStartsOut = true;
92  this->outputAllSolid = false;
93  this->outputFraction = 1.0f;
94 
[7801]95  this->checkCollisionRay(this->root,0.0f,1.0f, &(State::getCameraTargetNode()->getLastAbsCoor()), &this->cam);
[7563]96 
97  if(this->outputFraction != 1.0f || this->outputAllSolid ) this->drawDebugCube(&this->cam);
[7395]98
[7801]99  if ( viscluster < 0  || ((int *)(this->bspFile->header))[35] == 0 )  //!< if (sizeof(Visdata) == 0)
[7395]100  {
[7563]101   
102   
103   
[7465]104    // Iterate through all Leafspublic final double readLEDouble()
[7395]105    for(int i = 0; i <  this->bspFile->numLeafs   ; i++ )
[7353]106    {
[7395]107      // cluster =  (this->bspFile->leaves)[i].cluster;
108      leaf& curLeaf = (this->bspFile->leaves)[i];
[7563]109      if(curLeaf.cluster<0) continue;
110     
111     
112      /** Do Frustum culling and draw 'em all **/
113      bool inFrustum = true;
[7801]114
[7563]115      Vector dir;
116      dir.x = this->cam.x - State::getCameraTargetNode()->getLastAbsCoor().x;
117      dir.y = this->cam.y - State::getCameraTargetNode()->getLastAbsCoor().y;
118      dir.z = this->cam.z - State::getCameraTargetNode()->getLastAbsCoor().z;
119      float dist =  dir.x*this->cam.x +dir.y*this->cam.y +dir.z*this->cam.z;
120      //if(dist < 0) dist = -dist;
[7801]121      const float dMins = dir.x*(float)curLeaf.mins[0] +dir.y*(float)curLeaf.mins[1] +dir.z*(float)curLeaf.mins[2] - dist ;
122      const float dMaxs = dir.x*(float)curLeaf.maxs[0] +dir.y*(float)curLeaf.maxs[1] +dir.z*(float)curLeaf.maxs[2] - dist ;
[7563]123     
[7801]124      if(dMins < -30.0 && dMaxs < -30.0)
[7563]125      {   
[7801]126       //continue;
[7563]127      }
[7801]128      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)
129      {
130        //continue;
131      }
[7563]132   
133       
[7395]134      // Iterate through all faces
135      for (int j = 0; j < curLeaf.n_leaffaces ; ++j) {
[7596]136        const int g = (j +  curLeaf.leafface);
137        const int f = ((int *)this->bspFile->leafFaces)[g];
[7395]138        if (f >=0 && !this->isAlreadyVisible(f)) {
139          this->alreadyVisible[f] = true;
[7563]140               
[7395]141          addFace(f); // "visibleFaces.append(f)"
142        }
143      }
[7353]144
145
146
147
[7395]148    } //for
[7596]149  } 
150  else {
[7579]151
152 
[7395]153    unsigned int v;
154    unsigned char  visSet;
[7353]155
[7395]156    // Iterate through all Leafs
[7385]157
[7395]158    for(int i = 0; i <  this->bspFile->numLeafs   ; ++i ) {
159      leaf& camLeaf =  (this->bspFile->leaves)[ActLeaf->leafIndex] ;
160      leaf& curLeaf =  (this->bspFile->leaves)[i] ;
[7579]161      int& cluster =  curLeaf.cluster;
[7563]162
163      if(cluster < 0) continue;
[7395]164      v = ((viscluster *  ( ((int *)this->bspFile->visData)[1]) ) + (cluster / 8));
[7579]165      visSet =((char*) (this->bspFile->visData))[v + 8];
[7563]166
[7579]167      // gets bit of visSet
168      if( ((visSet) & (1 << (cluster &  7))) != 0 ) {
[7592]169       
170        // Frustum culling
171   
172        Vector dir;
173        dir.x = this->cam.x - State::getCameraTargetNode()->getLastAbsCoor().x;
174        dir.y = this->cam.y - State::getCameraTargetNode()->getLastAbsCoor().y;
175        dir.z = this->cam.z - State::getCameraTargetNode()->getLastAbsCoor().z;
176        const float dist =  dir.x*this->cam.x +dir.y*this->cam.y +dir.z*this->cam.z;
177      //if(dist < 0) dist = -dist;
178        const float dMins = dir.x*(float)curLeaf.mins[0] +dir.y*(float)curLeaf.mins[1] +dir.z*(float)curLeaf.mins[2] - dist;
179        const float dMaxs = dir.x*(float)curLeaf.maxs[0] +dir.y*(float)curLeaf.maxs[1] +dir.z*(float)curLeaf.maxs[2] - dist;
180     
[7801]181        if(dMins < -150.0 && dMaxs < -150.0)
[7592]182        {   
183        continue; 
184        }
185       
186       
[7395]187        // Iterate through all faces
[7563]188        for (int j = 0; j < curLeaf.n_leaffaces ; ++j) {
[7579]189          const int g = (j +  curLeaf.leafface);
190          const int f = ((int *)this->bspFile->leafFaces)[g];
[7395]191
192          if (!this->isAlreadyVisible(f) && f>=0) {
193            this->addFace(f);
194            this->alreadyVisible[f] = true;
195          }
196
197        }
198
[7563]199      }// if
[7395]200
201    }//for
202
203  }//else
204
205  while(!this->opal.empty()) {
206    this->draw_face(this->opal.front());
207    this->opal.pop_front();
208  }
209  while(!this->trasparent.empty()) {
210    this->draw_face(this->trasparent.back());
211    this->trasparent.pop_back();
212  }
[7801]213  glActiveTextureARB(GL_TEXTURE1_ARB);
214  glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap);
215  glEnable(GL_TEXTURE_2D);
[7395]216
[7353]217}//draw
218
219
[7563]220
[7353]221void BspManager::draw_face(int curface)
222{
[7395]223  face& curFace =  (this->bspFile->faces)[curface];
224  const BspVertex* curVertex = (BspVertex *) this->bspFile->vertice;
225  int stride = sizeof(BspVertex);  // sizeof(Vertex)
226  int offset    = curFace.vertex;
[7801]227  if (curFace.effect != -1) return;
[7353]228  // PRINTF(0)("BSP Manager: ");
229  // PRINTF(0)("BSP Manager: type: %i  \n", curFace.texture);
[7395]230
231  //  if(  curFace.texture < 0 ) return;
232  if(curFace.type == 2) {
233    this->draw_patch( &curFace);
234    return;
235  }
[7801]236  if(curFace.type != 1) return;
237  if((char*)(this->bspFile->textures)[curFace.texture*72]== 0) return;
[7563]238
[7544]239  if(this->lastTex != curFace.texture) {
[7801]240    if(this->bspFile->Materials[curFace.texture].animated)
241    {
242      glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_COLOR);
243      glEnable(GL_BLEND);
244      if(this->bspFile->Materials[curFace.texture].aviMat->getStatus() == 2) this->bspFile->Materials[curFace.texture].aviMat->start(0);
245      this->bspFile->Materials[curFace.texture].aviMat->tick(0.005);
246      int n =  this->bspFile->Materials[curFace.texture].aviMat->getTexture();
247      glActiveTextureARB(GL_TEXTURE0_ARB);
248      glBindTexture(GL_TEXTURE_2D, n );
249      glDisable(GL_BLEND);
250    }
251    else
252    {
[7510]253    this->bspFile->Materials[curFace.texture].mat->select();
[7563]254    this->lastTex = curFace.texture;
[7801]255    }
[7544]256  }
[7563]257
258  if(curFace.lm_index < 0) {
[7465]259    glActiveTextureARB(GL_TEXTURE1_ARB);
[7563]260    glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap );
261    glEnable(GL_TEXTURE_2D);
262  } else {
263    glActiveTextureARB(GL_TEXTURE1_ARB);
[7465]264    glBindTexture(GL_TEXTURE_2D, this->bspFile->glLightMapTextures[curFace.lm_index]);
265    glEnable(GL_TEXTURE_2D);
[7563]266  }
267
[7801]268  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
269 
[7579]270 // glColor4f(3.0,3.0,3.0,1.0);
[7544]271  glEnableClientState(GL_VERTEX_ARRAY );
272  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
[7353]273  glEnableClientState(GL_NORMAL_ARRAY );
[7395]274  //  glEnableClientState(GL_COLOR_ARRAY);
275
[7563]276
[7353]277  glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0]));
[7563]278
[7465]279  glClientActiveTextureARB(GL_TEXTURE0_ARB);
[7353]280  glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0]));
[7801]281  //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
[7563]282
[7465]283  glClientActiveTextureARB(GL_TEXTURE1_ARB);
284  glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1]));
[7544]285  //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
[7353]286
[7563]287
[7353]288  glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0]));
[7395]289  // glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0]));
[7353]290  glDrawElements(GL_TRIANGLES, curFace.n_meshverts,
[7395]291                 GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert]));
[7353]292
[7465]293  glDisableClientState(GL_TEXTURE0_ARB);
294  glDisableClientState(GL_TEXTURE1_ARB);
[7353]295  glDisableClientState(GL_VERTEX_ARRAY );
[7801]296  glDisableClientState(GL_TEXTURE_COORD_ARRAY );
[7353]297  glDisableClientState(GL_NORMAL_ARRAY );
[7395]298  // glDisableClientState(GL_COLOR_ARRAY);
299
[7353]300}
301
302
303void BspManager::draw_debug_face(int curface)
304{
[7395]305  face& curFace =  (this->bspFile->faces)[curface];
306  const BspVertex* curVertex = (BspVertex *) this->bspFile->vertice;
307  int stride = 44;  // sizeof(Vertex)
308  int offset    = curFace.vertex;
309
[7353]310  // PRINTF(0)("BSP Manager: ");
311  // PRINTF(0)("BSP Manager: type: %i  \n", curFace.texture);
312
[7395]313  //  if(  curFace.texture < 0 ) return;
314  if(curFace.type == 2) {
315    this->draw_patch( &curFace);
316    return;
317  }
318  if(curFace.type == 3) return;
319  // if(this->bspFile->Materials[curFace.texture] != NULL)
[7353]320
[7395]321  this->bspFile->Materials[2].mat->select();
322  this->lastTex = 2;
323
[7353]324  glEnableClientState(GL_VERTEX_ARRAY );
325  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
326  glEnableClientState(GL_NORMAL_ARRAY );
[7395]327  //glEnableClientState(GL_COLOR_ARRAY);
[7353]328  // glEnableClientState(GL_VERTEX_ARRAY );
[7465]329  glClientActiveTextureARB(GL_TEXTURE0_ARB);
[7353]330  glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0]));
[7465]331  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
[7395]332  // glClientActiveTextureARB(GL_TEXTURE0_ARB);
[7465]333  glClientActiveTextureARB(GL_TEXTURE1_ARB);
[7353]334  glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0]));
[7465]335  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
[7395]336  // glClientActiveTextureARB(GL_TEXTURE1_ARB);
337  // glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1]));
338  //glEnableClientState(GL_NORMAL_ARRAY );
[7353]339
340  glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0]));
[7395]341  //  glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0]));
[7353]342  glDrawElements(GL_TRIANGLES, curFace.n_meshverts,
[7395]343                 GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert]));
344
[7353]345}
346
347void BspManager::draw_patch(face* Face)
348{
[7544]349  if(this->lastTex != Face->texture) {
[7510]350    this->bspFile->Materials[Face->texture].mat->select();
[7544]351    this->lastTex = Face->texture;
352  }
[7801]353  if (Face->effect != -1) return;
[7563]354
355
356  if(Face->lm_index < 0) {
[7465]357    glActiveTextureARB(GL_TEXTURE1_ARB);
[7511]358    glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap);
[7465]359    glEnable(GL_TEXTURE_2D);
[7563]360  } else {
[7465]361    glActiveTextureARB(GL_TEXTURE1_ARB);
362    glBindTexture(GL_TEXTURE_2D, this->bspFile->glLightMapTextures[Face->lm_index]);
363    glEnable(GL_TEXTURE_2D);
[7563]364  }
[7579]365  //glColor4f(3.0,3.0,3.0,1.0);
[7563]366
[7801]367  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
368  glEnable( GL_AUTO_NORMAL);
[7465]369  glEnableClientState(GL_VERTEX_ARRAY );
370  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
[7801]371  for(int i = Face->n_meshverts -1; i >=0   ; i--) {
372   //glFrontFace(GL_CW);
[7395]373    //PRINTF(0)("BSP Manager: Face->size[0]: %i . \n", Face->size[0]);
[7563]374
375
[7801]376    //glEnableClientState(GL_NORMAL_ARRAY );
[7353]377
[7395]378    glVertexPointer(3, GL_FLOAT,44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).position[0]));
[7563]379
380
[7465]381    glClientActiveTextureARB(GL_TEXTURE0_ARB);
[7801]382    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
[7395]383    glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[0][0]));
[7801]384 
[7465]385
[7563]386
[7465]387    glClientActiveTextureARB(GL_TEXTURE1_ARB);
388    glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[1][0]));
[7544]389    //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
[7465]390
[7563]391
[7801]392  //  glNormalPointer( GL_FLOAT, 44,&((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).normal[0]) );
[7353]393
394
395
[7563]396
[7801]397    for(int row=6; row>=0; --row) {
[7395]398      glDrawElements(GL_TRIANGLE_STRIP, 2*(8), GL_UNSIGNED_INT,
399                     & (     (((GLuint*)  (this->bspFile->patchIndexes))[7*8*2*(Face->meshvert+i)+ row*2*8]  ))  );
400    }
[7563]401
[7801]402    //glFrontFace(GL_CCW);
[7395]403  }
[7465]404  glDisableClientState(GL_TEXTURE0_ARB);
405  glDisableClientState(GL_TEXTURE1_ARB);
[7801]406  glDisable(GL_AUTO_NORMAL);
[7465]407  glDisableClientState(GL_VERTEX_ARRAY );
408  glDisableClientState(GL_TEXTURE_COORD_ARRAY );
[7563]409
410
[7353]411}
412
413bool BspManager::isAlreadyVisible(int Face)
414{
[7395]415  return this->alreadyVisible[Face];
[7353]416}
417
418
419BspTreeNode*  BspManager::getLeaf(BspTreeNode* node, Vector* cam)
420{
[7395]421  float dist = 0;
422  while(!(node->isLeaf)) {
423    dist = (node->plane.x * this->cam.x + node->plane.y*this->cam.y + node->plane.z*this->cam.z) - node->d;
424    if(dist >= 0.0f) {
425      node = node->left;
426    } else {
427      node = node->right;
428    }
429  }
430  return  node;
[7353]431}
432
[7563]433void BspManager::checkBrushRay(brush* curBrush)
434{
435  float EPSILON = 0.000001;
436  float startDistance;
437  float endDistance;
438 
439  float startFraction = -1.0f;
440  float endFraction = 1.0f;
441  bool startsOut = false;
442  bool endsOut = false;
443 
444  Vector inputStart = State::getCameraTargetNode()->getLastAbsCoor();
445  Vector inputEnd   = State::getCameraTargetNode()->getAbsCoor();
446
447  for (int i = 0; i < curBrush->n_brushsides; i++)
448  {
449    brushside& curBrushSide =   this->bspFile->brushSides[curBrush->brushside + i]   ;
450    plane& curPlane  =   this->bspFile->planes[curBrushSide.plane] ;
451
452       startDistance = inputStart.x * curPlane.x + inputStart.y * curPlane.y+ inputStart.z * curPlane.z - curPlane.d;
453     endDistance = inputEnd.x * curPlane.x +inputEnd.y * curPlane.y +inputEnd.z * curPlane.z -curPlane.d;
454
455    if (startDistance > 0)
456      startsOut = true;
457    if (endDistance > 0)
458      endsOut = true;
459
460        // make sure the trace isn't completely on one side of the brush
461    if (startDistance > 0 && endDistance > 0)
462    {   // both are in front of the plane, its outside of this brush
463      return;
464    }
465    if (startDistance <= 0 && endDistance <= 0)
466    {   // both are behind this plane, it will get clipped by another one
467      continue;
468    }
469
470        // MMM... BEEFY
471    if (startDistance > endDistance)
472    {   // line is entering into the brush
473      float fraction = (startDistance - EPSILON) / (startDistance - endDistance);  // *
474      if (fraction > startFraction)
475        startFraction = fraction;
476    }
477    else
478    {   // line is leaving the brush
479      float fraction = (startDistance + EPSILON) / (startDistance - endDistance);  // *
480      if (fraction < endFraction)
481        endFraction = fraction;
482    }
483   
484  }
485   if (startsOut == false)
486    {
487      this->outputStartsOut = false;
488      if (endsOut == false)
489        this->outputAllSolid = true;
490      return;
491    }
492
493  if (startFraction < endFraction)
494  {
495    if (startFraction > -1.0f && startFraction < outputFraction)
496    {
497      if (startFraction < 0)
498        startFraction = 0;
499      this->outputFraction = startFraction;
500    }
501  }
502 
503}
504
505void BspManager::checkCollisionRay(BspTreeNode* node, float startFraction, float endFraction, Vector* start, Vector* end)
506{
507
508
509  float EPSILON = 0.000001;
510  float  endDistance = (end)->x * (node->plane.x) +(end)->y * (node->plane.y) +(end)->z * (node->plane.z)  - node->d;
511  float  startDistance = (start)->x * (node->plane.x)+ (start)->y * (node->plane.y)+ (start)->z * (node->plane.z)- node->d;
512
513
514  if(node->isLeaf) {
515    leaf& curLeaf = this->bspFile->leaves[node->leafIndex];
516    for (int i = 0; i <  curLeaf.n_leafbrushes ; i++) {
517      brush& curBrush = this->bspFile->brushes[((int*)(this->bspFile->leafBrushes))[curLeaf.leafbrush_first+i]];
518      //object *brush = &BSP.brushes[BSP.leafBrushes[leaf->firstLeafBrush + i]];
519      if (curBrush.n_brushsides > 0   &&
520        ((((BspTexture*)(this->bspFile->textures))[curBrush.texture]).contents & 1)) 
521      // CheckBrush( brush );
522      this->checkBrushRay(&curBrush);
523      if(curBrush.n_brushsides <=0) this->outputAllSolid = true;
524    }
525    return;
526  }
527
528
529  if (startDistance >= 0 && endDistance >= 0)     // A
530  {   // both points are in front of the plane
531    // so check the front child
532    this->checkCollisionRay(node->left,0,0,start,end);
533  } else if (startDistance < 0 && endDistance < 0)  // B
534  {   // both points are behind the plane
535    // so check the back child
536    this->checkCollisionRay(node->right,0,0,start,end);
537  } else                                            // C
538  {   // the line spans the splitting plane
539    int side;
540    float fraction1, fraction2, middleFraction;
541    Vector middle;
542
543    // STEP 1: split the segment into two
544    if (startDistance < endDistance) {
545      side = 1; // back
546      float inverseDistance = 1.0f / (startDistance - endDistance);
547      fraction1 = (startDistance + EPSILON) * inverseDistance;
548      fraction2 = (startDistance + EPSILON) * inverseDistance;
549    } else if (endDistance < startDistance) {
550      side = 0; // front(start)->x * (node->plane.x)+
551      float inverseDistance = 1.0f / (startDistance - endDistance);
552      fraction1 = (startDistance + EPSILON) * inverseDistance;
553      fraction2 = (startDistance - EPSILON) * inverseDistance;
554    } else {
555      side = 0; // front
556      fraction1 = 1.0f;
557      fraction2 = 0.0f;
558    }
559
560    // STEP 2: make sure the numbers are valid
561    if (fraction1 < 0.0f) fraction1 = 0.0f;
562    else if (fraction1 > 1.0f) fraction1 = 1.0f;
563    if (fraction2 < 0.0f) fraction2 = 0.0f;
564    else if (fraction2 > 1.0f) fraction2 = 1.0f;
565
566    // STEP 3: calculate the middle point for the first side
567    middleFraction = startFraction +
568                     (endFraction - startFraction) * fraction1;
569
570    middle.x = start->x + fraction1 * (end->x - start->x);
571    middle.y = start->y + fraction1 * (end->y - start->y);
572    middle.z = start->z + fraction1 * (end->z - start->z);
573
574    // STEP 4: check the first side
575    //CheckNode( node->children[side], startFraction, middleFraction, start, middle );
576    if(side == 0) this->checkCollisionRay(node->left,startFraction, middleFraction, start, &middle );
577
578    else this->checkCollisionRay(node->right,startFraction, middleFraction,
579                                   start, &middle );
580
581    // STEP 5: calculate the middle point for the second side
582    middleFraction = startFraction +
583                     (endFraction - startFraction) * fraction2;
584
585    middle.x = start->x + fraction2 * (end->x - start->x);
586    middle.y = start->y + fraction2 * (end->y - start->y);
587    middle.z = start->z + fraction2 * (end->z - start->z);
588
589    // STEP 6: check the second side
590    if(side == 1)this->checkCollisionRay(node->left,middleFraction, endFraction, &middle, end);
591
592    else this->checkCollisionRay(node->right,middleFraction, endFraction,&middle, end );
593
594
595  }
596
597}
[7353]598void  BspManager::checkCollision(BspTreeNode* node, Vector* cam)
599{
[7563]600  Vector next = this->cam;
601  next.x =   (State::getCameraTargetNode()->getLastAbsCoor()).x ;
602  next.y =   (State::getCameraTargetNode()->getLastAbsCoor()).y ;
603  next.z =   (State::getCameraTargetNode()->getLastAbsCoor()).z ;
604
[7395]605  float dist = 0;
606  if(!(node->isLeaf)) {
607    dist = (node->plane.x * this->cam.x + node->plane.y*this->cam.y + node->plane.z*this->cam.z) - node->d;
608    if(dist > 4.0f) {
609      checkCollision(node->left,cam);
610      return;
611    }
612    if(dist < -4.0f) {
613      checkCollision(node->right,cam);
614      return;
615    }
616    if(dist<=4.0f && dist >= -4.0f) {
617      checkCollision(node->left,cam);
618      checkCollision(node->right,cam);
619      return;
620    }
621    return;
622  } else {
[7353]623
[7395]624    leaf& camLeaf =  ((leaf *)(this->bspFile->leaves))[(node->leafIndex ) ];
[7353]625
[7579]626    if (camLeaf.cluster < 0) {
627                              this->drawDebugCube(&this->cam); 
628                              this->drawDebugCube(&next);
629                              State::getPlayer()->getPlayable()->setRelCoor(-100,-100,-100);
630                              State::getPlayer()->getPlayable()->collidesWith(NULL, State::getCameraTargetNode()->getLastAbsCoor());
631                              }
[7395]632
633
634    /*
[7511]635        for(int i = 0; i < camLeaf.n_leafbrushes && i < 10; i++ )
636        {
637                brush& curBrush = ((brush*)(this->bspFile->brushes))[(camLeaf.leafbrush_first +i)%this->bspFile->numLeafBrushes];
638                if(curBrush.n_brushsides < 0) return;
639                for(int j = 0; j < curBrush.n_brushsides; j++)
640                {
641                float dist = -0.1;
642                brushside& curBrushSide = ((brushside*)(this->bspFile->brushSides))[(curBrush.brushside +j)%this->bspFile->numBrushSides];
643                plane&      testPlane = ((plane*)(this->bspFile->planes))[curBrushSide.plane % this->bspFile->numPlanes];
644                dist = testPlane.x * this->cam.x +  testPlane.y * this->cam.y  +  testPlane.z * this->cam.z   -testPlane.d ;
645               
646                if(dist < -0.01f) dist = -1.0f *dist;
647                if(dist < 1.0f){
648                                this->drawDebugCube(&this->cam);
649                                return;
650                              }
651                }
652                       
653        } */
[7395]654
655  }
656  return;
[7353]657}
658
659void BspManager::drawDebugCube(Vector* cam)
660{
[7395]661  glBegin(GL_QUADS);
[7353]662
[7395]663  // Bottom Face.  Red, 75% opaque, magnified texture
[7353]664
[7395]665  glNormal3f( 0.0f, -1.0f, 0.0f); // Needed for lighting
666  glColor4f(0.9,0.2,0.2,.75); // Basic polygon color
[7353]667
[7395]668  glTexCoord2f(0.800f, 0.800f); glVertex3f(cam->x-1.0f, cam->y-1.0f,cam->z -1.0f);
669  glTexCoord2f(0.200f, 0.800f); glVertex3f(cam->x+1.0f, cam->y-1.0f,cam->z -1.0f);
670  glTexCoord2f(0.200f, 0.200f); glVertex3f(cam->x+ 1.0f,cam->y -1.0f,cam->z +  1.0f);
671  glTexCoord2f(0.800f, 0.200f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z + 1.0f);
[7353]672
673
[7395]674  // Top face; offset.  White, 50% opaque.
[7353]675
[7395]676  glNormal3f( 0.0f, 1.0f, 0.0f);  glColor4f(0.5,0.5,0.5,.5);
[7353]677
[7395]678  glTexCoord2f(0.005f, 1.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.0f);
679  glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f,  cam->z +1.0f);
680  glTexCoord2f(1.995f, 0.005f); glVertex3f(cam->x+ 1.0f,  cam->y+1.0f,  cam->z +1.0f);
681  glTexCoord2f(1.995f, 1.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z -1.0f);
[7353]682
683
[7395]684  // Far face.  Green, 50% opaque, non-uniform texture cooridinates.
[7353]685
[7395]686  glNormal3f( 0.0f, 0.0f,-1.0f);  glColor4f(0.2,0.9,0.2,.5);
[7353]687
[7395]688  glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z -1.3f);
689  glTexCoord2f(2.995f, 2.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.3f);
690  glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x+ 1.0f,cam->y+  1.0f, cam->z -1.3f);
691  glTexCoord2f(0.005f, 0.005f); glVertex3f( cam->x+1.0f,cam->y -1.0f, cam->z -1.3f);
[7353]692
693
[7395]694  // Right face.  Blue; 25% opaque
[7353]695
[7395]696  glNormal3f( 1.0f, 0.0f, 0.0f);  glColor4f(0.2,0.2,0.9,.25);
[7353]697
[7465]698  glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y -1.0f, cam->z -1.0f);
[7395]699  glTexCoord2f(0.995f, 0.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z -1.0f);
700  glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z + 1.0f);
701  glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y-1.0f,  cam->z +1.0f);
[7353]702
703
[7395]704  // Front face; offset.  Multi-colored, 50% opaque.
[7353]705
[7395]706  glNormal3f( 0.0f, 0.0f, 1.0f);
[7353]707
[7395]708  glColor4f( 0.9f, 0.2f, 0.2f, 0.5f);
709  glTexCoord2f( 0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f,  cam->z +1.0f);
710  glColor4f( 0.2f, 0.9f, 0.2f, 0.5f);
711  glTexCoord2f( 0.995f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y-1.0f,  cam->z +1.0f);
712  glColor4f( 0.2f, 0.2f, 0.9f, 0.5f);
713  glTexCoord2f( 0.995f, 0.995f); glVertex3f( cam->x+1.0f,  cam->y+1.0f,  cam->z +1.0f);
714  glColor4f( 0.1f, 0.1f, 0.1f, 0.5f);
715  glTexCoord2f( 0.005f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f,  cam->z +1.0f);
716
717
718  // Left Face; offset.  Yellow, varying levels of opaque.
719
720  glNormal3f(-1.0f, 0.0f, 0.0f);
721
722  glColor4f(0.9,0.9,0.2,0.0);
723  glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z -1.0f);
724  glColor4f(0.9,0.9,0.2,0.66);
725  glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x-1.0f,cam->y -1.0f,  cam->z +1.0f);
726  glColor4f(0.9,0.9,0.2,1.0);
727  glTexCoord2f(0.995f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f,  cam->z +1.0f);
728  glColor4f(0.9,0.9,0.2,0.33);
729  glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.0f);
730
731  glEnd();
[7385]732}
[7353]733
[7385]734void BspManager::addFace(int f)
735{
[7395]736  face& curFace =  ((face *)(this->bspFile->faces))[f];
737  if(this->bspFile->Materials[curFace.texture].alpha) this->trasparent.push_back(f);
738  else this->opal.push_back(f);
739}
Note: See TracBrowser for help on using the repository browser.