Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/lib/graphics/importer/bsp_manager.cc @ 9089

Last change on this file since 9089 was 9089, checked in by patrick, 18 years ago

cr z axis half check

File size: 46.4 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2006 orx
5
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.
10
11   ### File Specific:
12   main-programmer: bottac@ee.ethz.ch
13
14   Inspired by:
15   Rendering Q3 Maps by Morgan McGuire                  http://graphics.cs.brown.edu/games/quake/quake3.html
16   Unofficial Quake 3 Map Specs by Kekoa Proudfoot      http://graphics.stanford.edu/~kekoa/q3/
17
18   Collision detection adapted from:
19   Quake 3 Collision Detection by Nathan Ostgard        http://www.devmaster.net/articles/quake3collision/
20*/
21
22
23#include "vector.h"
24#include "bsp_file.h"
25#include "bsp_manager.h"
26#include "bsp_tree_leaf.h"
27#include "p_node.h"
28#include "state.h"
29#include "debug.h"
30#include "material.h"
31#include "camera.h"
32#include "vertex_array_model.h"
33#include "world_entities/player.h"
34#include "world_entities/playable.h"
35#include "util/loading/resource_manager.h"
36// STL Containers
37#include <vector>
38#include <deque>
39#include "movie_player.h"
40
41#include "world_entity.h"
42
43#include "util/loading/load_param.h"
44#include "util/loading/factory.h"
45
46#include "aabb.h"
47#include "cr_defs.h"
48
49
50//CREATE_FACTORY( BspManager, CL_BSP_MODEL);
51
52BspManager::BspManager(WorldEntity* parent)
53{
54
55  this->parent = parent;
56  /*// open a BSP file
57  this->bspFile = new BspFile();
58  this->bspFile->scale = 0.4f;
59  this->bspFile->read(ResourceManager::getFullName("test.bsp").c_str());
60  this->bspFile->build_tree();
61  this->root  = this->bspFile->get_root();
62  this->alreadyVisible = new bool [this->bspFile->numFaces];
63  */
64
65}
66
67
68/*
69BspManager::BspManager(const TiXmlElement* root)
70{
71
72
73  if( root != NULL)
74    this->loadParams(root);
75
76  CDEngine::getInstance()->setBSPModel(this);
77} */
78
79BspManager::~BspManager()
80{
81  if(this->bspFile)
82    delete this->bspFile;
83}
84
85int BspManager::load(const char* fileName, float scale)
86{
87  // open a BSP file
88
89
90  this->bspFile = new BspFile();
91  this->bspFile->scale =  scale;
92  if(this->bspFile->read(ResourceManager::getFullName(fileName).c_str()) == -1)
93    return -1;
94
95  this->bspFile->build_tree();
96  this->root  = this->bspFile->get_root();
97  this->alreadyVisible = new bool [this->bspFile->numFaces];
98
99  this->outputFraction = 1.0f;
100
101  return 0;
102}
103/*
104BspManager::BspManager(const char* fileName, float scale)
105{
106  // open a BSP file
107  this->bspFile = new BspFile();
108  this->bspFile->scale =  scale;
109  this->bspFile->read(fileName);
110  this->bspFile->build_tree();
111  this->root  = this->bspFile->get_root();
112  this->alreadyVisible = new bool [this->bspFile->numFaces];
113
114  CDEngine::getInstance()->setBSPModel(this);
115}
116*/
117
118const void BspManager::tick(float time)
119{
120
121  if(!this->bspFile->MovieMaterials.empty()) {
122    ::std::vector<MoviePlayer *>::iterator it = this->bspFile->MovieMaterials.begin() ;
123    while(it != this->bspFile->MovieMaterials.end()) {
124      (*it)->tick(time);
125      it++;
126    }
127    //this->bspFile->MovieMaterials.front()->tick(time );
128
129
130  }
131
132}
133const void BspManager::draw()
134{
135
136  /*
137  this->drawDebugCube(&this->out);
138  this->out1 = this->out;
139  this->out2 = this->out;
140  if(this->collPlane != NULL) {
141    this->out1.x += this->collPlane->x*5.0;
142    this->out1.y += this->collPlane->y*5.0;
143    this->out1.z += this->collPlane->z*5.0;
144
145    this->out2.x += this->collPlane->x*10.0;
146    this->out2.y += this->collPlane->y*10.0;
147    this->out2.z += this->collPlane->z*10.0;
148  }
149  this->drawDebugCube(&this->out1);
150  this->drawDebugCube(&this->out2);
151
152  */
153
154
155  // Draw Debug Terrain
156  /*
157  this->bspFile->Materials[0]->select();
158  for(int i = 0; i <  this->bspFile->numPatches ; i++)
159        {
160                this->bspFile->VertexArrayModels[i]->draw();
161
162        }
163  */
164
165
166
167  // erase alreadyVisible
168  for(int i = 0; i < this->bspFile->numFaces; i++) this->alreadyVisible[i] = false;
169  float tmp = 0;
170  //this->opal.clear();
171  //this->trasparent.clear();
172  // Find all visible faces...
173
174  this->cam = State::getCamera()->getAbsCoor() ;
175  //this->ship = State::getCameraTargetNode()->getAbsCoor();
176
177
178
179
180
181  this->viewDir=    State::getCamera()->getAbsDirX();
182  float d = (cam.x*viewDir.x + cam.y*viewDir.y + cam.z * viewDir.z);
183
184  BspTreeNode*  ActLeaf = this->getLeaf(this->bspFile->root, &ship);
185  int viscluster = -1;
186  viscluster =((leaf*)(this->bspFile->leaves))[ ActLeaf->leafIndex].cluster; // get the players cluster (viscluster)
187
188
189
190
191  // this->checkCollision(this->root, &this->cam);   //!< Test Collision Detection
192
193
194  this->outputStartsOut = true;
195  this->outputAllSolid = false;
196  this->outputFraction = 1.0f;
197
198  if ( viscluster < 0  || ((int *)(this->bspFile->header))[35] == 0 )  //!< if (sizeof(Visdata) == 0)
199  {
200
201
202
203    // Iterate through all Leafs
204    for(int i = 0; i <  this->bspFile->numLeafs   ; i++ )
205    {
206      // cluster =  (this->bspFile->leaves)[i].cluster;
207      leaf& curLeaf = (this->bspFile->leaves)[i];
208      if(curLeaf.cluster<0) continue;
209
210      /** Do Frustum culling and draw 'em all **/
211
212      Vector dir = State::getCameraNode()->getAbsDirX();
213
214      float dist =  dir.x*this->cam.x +dir.y*this->cam.y +dir.z*this->cam.z;
215      //if(dist < 0) dist = -dist;
216      const float dMins = dir.x*(float)curLeaf.mins[0] +dir.y*(float)curLeaf.mins[1] +dir.z*(float)curLeaf.mins[2] - dist ;
217      const float dMaxs = dir.x*(float)curLeaf.maxs[0] +dir.y*(float)curLeaf.maxs[1] +dir.z*(float)curLeaf.maxs[2] - dist ;
218
219      if(dMins < -300.0 && dMaxs < -300.0) {
220        continue;
221      }
222      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) {
223        continue;
224      }
225
226
227      // Iterate through all faces
228      for (int j = 0; j < curLeaf.n_leaffaces ; ++j) {
229        const int g = (j +  curLeaf.leafface);
230        const int f = ((int *)this->bspFile->leafFaces)[g];
231        if (f >=0 && !this->isAlreadyVisible(f)) {
232          this->alreadyVisible[f] = true;
233          addFace(f); // "visibleFaces.append(f)"
234        }
235      }
236
237
238
239
240    } //for
241  } else {
242
243
244    unsigned int v;
245    unsigned char  visSet;
246
247    // Iterate through all Leafs
248
249    for(int i = 0; i <  this->bspFile->numLeafs   ; ++i ) {
250      leaf& camLeaf =  (this->bspFile->leaves)[ActLeaf->leafIndex] ;
251      leaf& curLeaf =  (this->bspFile->leaves)[i] ;
252      int& cluster =  curLeaf.cluster;
253
254      if(cluster < 0) continue;
255      v = ((viscluster *  ( ((int *)this->bspFile->visData)[1]) ) + (cluster / 8));
256      visSet =((char*) (this->bspFile->visData))[v + 8];
257
258      // gets bit of visSet
259      if( ((visSet) & (1 << (cluster &  7))) != 0 ) {
260
261        // Frustum culling
262
263        Vector dir;
264        dir.x = State::getCameraNode()->getAbsDirX().x;
265        dir.y =  State::getCameraNode()->getAbsDirX().y;
266        dir.z =  State::getCameraNode()->getAbsDirX().z;
267        const float dist =  dir.x*this->cam.x +dir.y*this->cam.y +dir.z*this->cam.z;
268        //if(dist < 0) dist = -dist;
269        const float dMins = dir.x*(float)curLeaf.mins[0] +dir.y*(float)curLeaf.mins[1] +dir.z*(float)curLeaf.mins[2] - dist;
270        const float dMaxs = dir.x*(float)curLeaf.maxs[0] +dir.y*(float)curLeaf.maxs[1] +dir.z*(float)curLeaf.maxs[2] - dist;
271
272        if(dMins < -50.0 && dMaxs < -  50.0) {
273          continue;
274        }
275
276
277        // Iterate through all faces
278        for (int j = 0; j < curLeaf.n_leaffaces ; ++j) {
279          const int g = (j +  curLeaf.leafface);
280          const int f = ((int *)this->bspFile->leafFaces)[g];
281
282          if (!this->isAlreadyVisible(f) && f>=0) {
283            this->addFace(f);
284            this->alreadyVisible[f] = true;
285          }
286
287        }
288
289      }// if
290
291    }//for
292
293  }//else
294
295  while(!this->opal.empty()) {
296    this->draw_face(this->opal.front());
297    this->opal.pop_front();
298  }
299  while(!this->trasparent.empty()) {
300    this->draw_face(this->trasparent.back());
301    this->trasparent.pop_back();
302  }
303  //glEnable(GL_TEXTURE_2D);
304  glActiveTextureARB(GL_TEXTURE1_ARB);
305  glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap);
306
307
308
309}//draw
310
311
312
313void BspManager::draw_face(int curface)
314{
315  face& curFace =  (this->bspFile->faces)[curface];
316  const BspVertex* curVertex = (BspVertex *) this->bspFile->vertice;
317  int stride = sizeof(BspVertex);  // sizeof(Vertex)
318  int offset    = curFace.vertex;
319  if (curFace.effect != -1) return;
320  // PRINTF(0)("BSP Manager: ");
321  // PRINTF(0)("BSP Manager: type: %i  \n", curFace.texture);
322
323  //  if(  curFace.texture < 0 ) return;
324  if(curFace.type == 2) {
325    this->draw_patch( &curFace);
326    return;
327  }
328  // if(curFace.type != 1) return;
329  if((char*)(this->bspFile->textures)[curFace.texture*72]== 0) return;
330
331  if(this->lastTex != curFace.texture) {
332    if(this->bspFile->Materials[curFace.texture].animated) {
333      // glBlendFunc(GL_ZERO,GL_ONE);
334
335
336
337      if(this->bspFile->Materials[curFace.texture].aviMat->getStatus() == 2) this->bspFile->Materials[curFace.texture].aviMat->start(0);
338      //this->bspFile->Materials[curFace.texture].aviMat->tick(0.005);
339      int n =  this->bspFile->Materials[curFace.texture].aviMat->getTexture();
340      glActiveTextureARB(GL_TEXTURE0_ARB);
341      glBindTexture(GL_TEXTURE_2D, n );
342      this->lastTex = curFace.texture;
343
344    } else {
345      this->bspFile->Materials[curFace.texture].mat->select();
346      this->lastTex = curFace.texture;
347    }
348  }
349
350  if(curFace.lm_index < 0) {
351    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
352    glActiveTextureARB(GL_TEXTURE1_ARB);
353    glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap );
354    glEnable(GL_TEXTURE_2D);
355  } else {
356    // glEnable(GL_BLEND);
357    //glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
358    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
359    glActiveTextureARB(GL_TEXTURE1_ARB);
360    glBindTexture(GL_TEXTURE_2D, this->bspFile->glLightMapTextures[curFace.lm_index]);
361    glEnable(GL_TEXTURE_2D);
362    //  glDisable(GL_BLEND);
363  }
364
365  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
366
367  // glColor4f(3.0,3.0,3.0,1.0);
368  glEnableClientState(GL_VERTEX_ARRAY );
369  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
370  glEnableClientState(GL_NORMAL_ARRAY );
371  //  glEnableClientState(GL_COLOR_ARRAY);
372
373
374  glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0]));
375
376  glClientActiveTextureARB(GL_TEXTURE0_ARB);
377  glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0]));
378  //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
379
380  glClientActiveTextureARB(GL_TEXTURE1_ARB);
381  glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1]));
382  //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
383
384
385  glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0]));
386  // glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0]));
387  glDrawElements(GL_TRIANGLES, curFace.n_meshverts,
388                 GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert]));
389
390  glDisableClientState(GL_TEXTURE0_ARB);
391  glDisableClientState(GL_TEXTURE1_ARB);
392  glDisableClientState(GL_VERTEX_ARRAY );
393  glDisableClientState(GL_TEXTURE_COORD_ARRAY );
394  glDisableClientState(GL_NORMAL_ARRAY );
395  // glDisableClientState(GL_COLOR_ARRAY);
396
397}
398
399
400void BspManager::draw_debug_face(int curface)
401{
402  face& curFace =  (this->bspFile->faces)[curface];
403  const BspVertex* curVertex = (BspVertex *) this->bspFile->vertice;
404  int stride = 44;  // sizeof(Vertex)
405  int offset    = curFace.vertex;
406
407  // PRINTF(0)("BSP Manager: ");
408  // PRINTF(0)("BSP Manager: type: %i  \n", curFace.texture);
409
410  //  if(  curFace.texture < 0 ) return;
411  if(curFace.type == 2) {
412    this->draw_patch( &curFace);
413    return;
414  }
415  if(curFace.type == 3) return;
416  // if(this->bspFile->Materials[curFace.texture] != NULL)
417
418  this->bspFile->Materials[2].mat->select();
419  this->lastTex = 2;
420
421  glEnableClientState(GL_VERTEX_ARRAY );
422  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
423  glEnableClientState(GL_NORMAL_ARRAY );
424  //glEnableClientState(GL_COLOR_ARRAY);
425  // glEnableClientState(GL_VERTEX_ARRAY );
426  glClientActiveTextureARB(GL_TEXTURE0_ARB);
427  glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0]));
428  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
429  // glClientActiveTextureARB(GL_TEXTURE0_ARB);
430  glClientActiveTextureARB(GL_TEXTURE1_ARB);
431  glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0]));
432  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
433  // glClientActiveTextureARB(GL_TEXTURE1_ARB);
434  // glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1]));
435  //glEnableClientState(GL_NORMAL_ARRAY );
436
437  glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0]));
438  //  glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0]));
439  glDrawElements(GL_TRIANGLES, curFace.n_meshverts,
440                 GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert]));
441
442}
443
444void BspManager::draw_patch(face* Face)
445{
446  if(this->lastTex != Face->texture) {
447    this->bspFile->Materials[Face->texture].mat->select();
448    this->lastTex = Face->texture;
449  }
450  if (Face->effect != -1) return;
451
452
453  if(Face->lm_index < 0) {
454    glActiveTextureARB(GL_TEXTURE1_ARB);
455    glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap);
456    glEnable(GL_TEXTURE_2D);
457  } else {
458    glActiveTextureARB(GL_TEXTURE1_ARB);
459    glBindTexture(GL_TEXTURE_2D, this->bspFile->glLightMapTextures[Face->lm_index]);
460    glEnable(GL_TEXTURE_2D);
461  }
462  //glColor4f(3.0,3.0,3.0,1.0);
463
464  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
465  glEnable( GL_AUTO_NORMAL);
466  glEnableClientState(GL_VERTEX_ARRAY );
467  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
468  for(int i = Face->n_meshverts -1; i >=0   ; i--) {
469    //glFrontFace(GL_CW);
470    //PRINTF(0)("BSP Manager: Face->size[0]: %i . \n", Face->size[0]);
471
472
473    //glEnableClientState(GL_NORMAL_ARRAY );
474
475    glVertexPointer(3, GL_FLOAT,44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).position[0]));
476
477
478    glClientActiveTextureARB(GL_TEXTURE0_ARB);
479    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
480    glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[0][0]));
481
482
483
484    glClientActiveTextureARB(GL_TEXTURE1_ARB);
485    glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[1][0]));
486    //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
487
488
489    //  glNormalPointer( GL_FLOAT, 44,&((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).normal[0]) );
490
491
492
493
494    for(int row=6; row>=0; --row) {
495      glDrawElements(GL_TRIANGLE_STRIP, 2*(8), GL_UNSIGNED_INT,
496                     & (     (((GLuint*)  (this->bspFile->patchIndexes))[7*8*2*(Face->meshvert+i)+ row*2*8]  ))  );
497    }
498
499    //glFrontFace(GL_CCW);
500  }
501  glDisableClientState(GL_TEXTURE0_ARB);
502  glDisableClientState(GL_TEXTURE1_ARB);
503  glDisable(GL_AUTO_NORMAL);
504  glDisableClientState(GL_VERTEX_ARRAY );
505  glDisableClientState(GL_TEXTURE_COORD_ARRAY );
506
507
508}
509
510bool BspManager::isAlreadyVisible(int Face)
511{
512  return this->alreadyVisible[Face];
513}
514
515
516BspTreeNode*  BspManager::getLeaf(BspTreeNode* node, Vector* cam)
517{
518  float dist = 0;
519  while(!(node->isLeaf)) {
520    dist = (node->plane.x * this->cam.x + node->plane.y*this->cam.y + node->plane.z*this->cam.z) - node->d;
521    if(dist >= 0.0f) {
522      node = node->left;
523    } else {
524      node = node->right;
525    }
526  }
527  return  node;
528}
529
530void BspManager::checkBrushRay(brush* curBrush)
531{
532  float EPSILON = 0.000001;
533  float startDistance;
534  float endDistance;
535
536  float startFraction = -1.0f;
537  float endFraction = 1.0f;
538  bool startsOut = false;
539  bool endsOut = false;
540
541  Vector inputStart = State::getCameraTargetNode()->getLastAbsCoor();
542  Vector inputEnd   = State::getCameraTargetNode()->getAbsCoor();
543
544  for (int i = 0; i < curBrush->n_brushsides; i++) {
545    brushside& curBrushSide =   this->bspFile->brushSides[curBrush->brushside + i]   ;
546    plane& curPlane  =   this->bspFile->planes[curBrushSide.plane] ;
547
548    startDistance = inputStart.x * curPlane.x + inputStart.y * curPlane.y+ inputStart.z * curPlane.z - curPlane.d;
549    endDistance = inputEnd.x * curPlane.x +inputEnd.y * curPlane.y +inputEnd.z * curPlane.z -curPlane.d;
550
551    if (startDistance > 0)
552      startsOut = true;
553    if (endDistance > 0)
554      endsOut = true;
555
556    // make sure the trace isn't completely on one side of the brush
557    if (startDistance > 0 && endDistance > 0) {   // both are in front of the plane, its outside of this brush
558      return;
559    }
560    if (startDistance <= 0 && endDistance <= 0) {   // both are behind this plane, it will get clipped by another one
561      continue;
562    }
563
564    // MMM... BEEFY
565    if (startDistance > endDistance) {   // line is entering into the brush
566      float fraction = (startDistance - EPSILON) / (startDistance - endDistance);  // *
567      if (fraction > startFraction)
568        startFraction = fraction;
569      // don't store plane
570      // this->collPlane = &curPlane;
571
572    } else {   // line is leaving the brush
573      float fraction = (startDistance + EPSILON) / (startDistance - endDistance);  // *
574      if (fraction < endFraction)
575        endFraction = fraction;
576      // don't store plane
577      //this->collPlane = & curPlane;
578
579    }
580
581  }
582  if (startsOut == false) {
583    this->outputStartsOut = false;
584    if (endsOut == false)
585      this->outputAllSolid = true;
586    return;
587  }
588
589  if (startFraction < endFraction) {
590    if (startFraction > -1.0f && startFraction < outputFraction) {
591      if (startFraction < 0)
592        startFraction = 0;
593      this->outputFraction = startFraction;
594    }
595  }
596
597}
598
599void BspManager::checkBrushRayN(brush* curBrush)
600{
601  float EPSILON = 0.000001;
602  float startDistance;
603  float endDistance;
604
605  float startFraction = -1.0f;
606  float endFraction = 1.0f;
607  bool  startsOut = false;
608  bool  endsOut = false;
609
610  // Vector inputStart = State::getCameraTargetNode()->getLastAbsCoor();
611  // Vector inputEnd   = State::getCameraTargetNode()->getAbsCoor();
612
613  for (int i = 0; i < curBrush->n_brushsides; i++) {
614    brushside& curBrushSide =   this->bspFile->brushSides[curBrush->brushside + i]   ;
615    plane& curPlane  =   this->bspFile->planes[curBrushSide.plane] ;
616
617    startDistance = inputStart.x * curPlane.x + inputStart.y * curPlane.y+ inputStart.z * curPlane.z - curPlane.d;
618    endDistance = inputEnd.x * curPlane.x +inputEnd.y * curPlane.y +inputEnd.z * curPlane.z -curPlane.d;
619
620    if (startDistance > 0)
621      startsOut = true;
622    if (endDistance > 0)
623      endsOut = true;
624
625    // make sure the trace isn't completely on one side of the brush
626    if (startDistance > 0 && endDistance > 0) {   // both are in front of the plane, its outside of this brush
627      return;
628    }
629    if (startDistance <= 0 && endDistance <= 0) {   // both are behind this plane, it will get clipped by another one
630      continue;
631    }
632
633    // MMM... BEEFY
634    if (startDistance > endDistance) {   // line is entering into the brush
635      float fraction = (startDistance - EPSILON) / (startDistance - endDistance);  // *
636      if (fraction > startFraction)
637        startFraction = fraction;
638      // store plane
639      this->collPlane = &curPlane;
640
641    } else {   // line is leaving the brush
642      float fraction = (startDistance + EPSILON) / (startDistance - endDistance);  // *
643      if (fraction < endFraction)
644        endFraction = fraction;
645      // store plane
646      this->collPlane = & curPlane;
647
648    }
649
650  }
651  if (startsOut == false) {
652    this->outputStartsOut = false;
653    if (endsOut == false)
654      this->outputAllSolid = true;
655    return;
656  }
657
658  if (startFraction < endFraction) {
659    if (startFraction > -1.0f && startFraction < outputFraction) {
660      if (startFraction < 0)
661        startFraction = 0;
662      this->outputFraction = startFraction;
663    }
664  }
665
666}
667
668void BspManager::checkBrushRayN(brush* curBrush, Vector& inputStart, Vector& inputEnd)
669{
670  float EPSILON = 0.000001;
671  float startDistance;
672  float endDistance;
673
674  float startFraction = -1.0f;
675  float endFraction = 1.0f;
676  bool  startsOut = false;
677  bool  endsOut = false;
678
679  //Vector inputStart = State::getCameraTargetNode()->getLastAbsCoor();
680  //Vector inputEnd   = State::getCameraTargetNode()->getAbsCoor();
681
682  for (int i = 0; i < curBrush->n_brushsides; i++) {
683    brushside& curBrushSide =   this->bspFile->brushSides[curBrush->brushside + i]   ;
684    plane& curPlane  =   this->bspFile->planes[curBrushSide.plane] ;
685
686    startDistance = inputStart.x * curPlane.x + inputStart.y * curPlane.y+ inputStart.z * curPlane.z - curPlane.d;
687    endDistance = inputEnd.x * curPlane.x +inputEnd.y * curPlane.y +inputEnd.z * curPlane.z -curPlane.d;
688
689    if (startDistance > 0)
690      startsOut = true;
691    if (endDistance > 0)
692      endsOut = true;
693
694    // make sure the trace isn't completely on one side of the brush
695    if (startDistance > 0 && endDistance > 0) {   // both are in front of the plane, its outside of this brush
696      return;
697    }
698    if (startDistance <= 0 && endDistance <= 0) {   // both are behind this plane, it will get clipped by another one
699      continue;
700    }
701
702    // MMM... BEEFY
703    if (startDistance > endDistance) {   // line is entering into the brush
704      float fraction = (startDistance - EPSILON) / (startDistance - endDistance);  // *
705      if (fraction > startFraction)
706        startFraction = fraction;
707      // store plane
708      this->collPlane = &curPlane;
709
710    } else {   // line is leaving the brush
711      float fraction = (startDistance + EPSILON) / (startDistance - endDistance);  // *
712      if (fraction < endFraction)
713        endFraction = fraction;
714      // store plane
715      this->collPlane = & curPlane;
716
717    }
718
719  }
720  if (startsOut == false) {
721    this->outputStartsOut = false;
722    if (endsOut == false)
723      this->outputAllSolid = true;
724    return;
725  }
726
727  if (startFraction < endFraction) {
728    if (startFraction > -1.0f && startFraction < outputFraction) {
729      if (startFraction < 0)
730        startFraction = 0;
731      this->outputFraction = startFraction;
732    }
733  }
734
735}
736
737
738void BspManager::checkCollisionRay(BspTreeNode* node, float startFraction, float endFraction, Vector* start, Vector* end)
739{
740
741
742  float EPSILON = 0.000001;
743  float  endDistance = (end)->x * (node->plane.x) +(end)->y * (node->plane.y) +(end)->z * (node->plane.z)  - node->d;
744  float  startDistance = (start)->x * (node->plane.x)+ (start)->y * (node->plane.y)+ (start)->z * (node->plane.z)- node->d;
745
746
747  if(node->isLeaf) {
748    leaf& curLeaf = this->bspFile->leaves[node->leafIndex];
749    for (int i = 0; i <  curLeaf.n_leafbrushes ; i++) {
750      brush& curBrush = this->bspFile->brushes[((int*)(this->bspFile->leafBrushes))[curLeaf.leafbrush_first+i]];
751      //object *brush = &BSP.brushes[BSP.leafBrushes[leaf->firstLeafBrush + i]];
752      if (curBrush.n_brushsides > 0   &&
753          ((((BspTexture*)(this->bspFile->textures))[curBrush.texture]).contents & 1))
754        // CheckBrush( brush );
755        this->checkBrushRay(&curBrush);
756      if(curBrush.n_brushsides <=0) this->outputAllSolid = true;
757    }
758    return;
759  }
760
761
762  if (startDistance >= 0 && endDistance >= 0)     // A
763  {   // both points are in front of the plane
764    // so check the front child
765    this->checkCollisionRay(node->left,0,0,start,end);
766  } else if (startDistance < 0 && endDistance < 0)  // B
767  {   // both points are behind the plane
768    // so check the back child
769    this->checkCollisionRay(node->right,0,0,start,end);
770  } else                                            // C
771  {   // the line spans the splitting plane
772    int side;
773    float fraction1, fraction2, middleFraction;
774    Vector middle;
775
776    // STEP 1: split the segment into two
777    if (startDistance < endDistance) {
778      side = 1; // back
779      float inverseDistance = 1.0f / (startDistance - endDistance);
780      fraction1 = (startDistance + EPSILON) * inverseDistance;
781      fraction2 = (startDistance + EPSILON) * inverseDistance;
782    } else if (endDistance < startDistance) {
783      side = 0; // front(start)->x * (node->plane.x)+
784      float inverseDistance = 1.0f / (startDistance - endDistance);
785      fraction1 = (startDistance + EPSILON) * inverseDistance;
786      fraction2 = (startDistance - EPSILON) * inverseDistance;
787    } else {
788      side = 0; // front
789      fraction1 = 1.0f;
790      fraction2 = 0.0f;
791    }
792
793    // STEP 2: make sure the numbers are valid
794    if (fraction1 < 0.0f) fraction1 = 0.0f;
795    else if (fraction1 > 1.0f) fraction1 = 1.0f;
796    if (fraction2 < 0.0f) fraction2 = 0.0f;
797    else if (fraction2 > 1.0f) fraction2 = 1.0f;
798
799    // STEP 3: calculate the middle point for the first side
800    middleFraction = startFraction +
801                     (endFraction - startFraction) * fraction1;
802
803    middle.x = start->x + fraction1 * (end->x - start->x);
804    middle.y = start->y + fraction1 * (end->y - start->y);
805    middle.z = start->z + fraction1 * (end->z - start->z);
806
807    // STEP 4: check the first side
808    //CheckNode( node->children[side], startFraction, middleFraction, start, middle );
809    if(side == 0) this->checkCollisionRay(node->left,startFraction, middleFraction, start, &middle );
810
811    else this->checkCollisionRay(node->right,startFraction, middleFraction,
812                                   start, &middle );
813
814    // STEP 5: calculate the middle point for the second side
815    middleFraction = startFraction +
816                     (endFraction - startFraction) * fraction2;
817
818    middle.x = start->x + fraction2 * (end->x - start->x);
819    middle.y = start->y + fraction2 * (end->y - start->y);
820    middle.z = start->z + fraction2 * (end->z - start->z);
821
822    // STEP 6: check the second side
823    if(side == 1)this->checkCollisionRay(node->left,middleFraction, endFraction, &middle, end);
824
825    else this->checkCollisionRay(node->right,middleFraction, endFraction,&middle, end );
826
827
828  }
829
830}
831
832
833
834void BspManager::checkCollisionRayN(BspTreeNode* node, float startFraction, float endFraction, Vector* start, Vector* end)
835{
836
837
838  float EPSILON = 0.000001;
839
840  float endDistance = end->dot(node->plane) - node->d;
841  float startDistance = start->dot(node->plane) - node->d;
842
843
844  if( node->isLeaf) {
845    leaf& curLeaf = this->bspFile->leaves[node->leafIndex];
846    for (int i = 0; i <  curLeaf.n_leafbrushes ; i++) {
847      brush& curBrush = this->bspFile->brushes[((int*)(this->bspFile->leafBrushes))[curLeaf.leafbrush_first+i]];
848      //object *brush = &BSP.brushes[BSP.leafBrushes[leaf->firstLeafBrush + i]];
849      if (curBrush.n_brushsides > 0   &&
850          ((((BspTexture*)(this->bspFile->textures))[curBrush.texture]).contents & 1))
851        // CheckBrush( brush );
852        this->checkBrushRayN(&curBrush);
853      if(curBrush.n_brushsides <=0) this->outputAllSolid = true;
854    }
855
856    return;
857  }
858
859
860  if (startDistance >= 0 && endDistance >= 0)     // A
861  {   // both points are in front of the plane
862    // so check the front child
863    this->checkCollisionRayN(node->left,0,0,start,end);
864  } else if (startDistance < 0 && endDistance < 0)  // B
865  {   // both points are behind the plane
866    // so check the back child
867    this->checkCollisionRayN(node->right,0,0,start,end);
868  } else                                            // C
869  {   // the line spans the splitting plane
870    int side;
871    float fraction1, fraction2, middleFraction;
872    Vector middle;
873
874    // STEP 1: split the segment into two
875    if (startDistance < endDistance) {
876      side = 1; // back
877      float inverseDistance = 1.0f / (startDistance - endDistance);
878      fraction1 = (startDistance + EPSILON) * inverseDistance;
879      fraction2 = (startDistance + EPSILON) * inverseDistance;
880    } else if (endDistance < startDistance) {
881      side = 0; // front(start)->x * (node->plane.x)+
882      float inverseDistance = 1.0f / (startDistance - endDistance);
883      fraction1 = (startDistance + EPSILON) * inverseDistance;
884      fraction2 = (startDistance - EPSILON) * inverseDistance;
885    } else {
886      side = 0; // front
887      fraction1 = 1.0f;
888      fraction2 = 0.0f;
889    }
890
891    // STEP 2: make sure the numbers are valid
892    if (fraction1 < 0.0f) fraction1 = 0.0f;
893    else if (fraction1 > 1.0f) fraction1 = 1.0f;
894    if (fraction2 < 0.0f) fraction2 = 0.0f;
895    else if (fraction2 > 1.0f) fraction2 = 1.0f;
896
897    // STEP 3: calculate the middle point for the first side
898    middleFraction = startFraction + (endFraction - startFraction) * fraction1;
899    middle = (*start) + ((*end) - (*start)) * fraction1;
900
901
902    // STEP 4: check the first side
903    //CheckNode( node->children[side], startFraction, middleFraction, start, middle );
904    if(side == 0) this->checkCollisionRayN(node->left,startFraction, middleFraction, start, &middle );
905
906    else this->checkCollisionRayN(node->right,startFraction, middleFraction,
907                                    start, &middle );
908
909    // STEP 5: calculate the middle point for the second side
910    middleFraction = startFraction + (endFraction - startFraction) * fraction2;
911    middle = (*start) + ((*end) - (*start)) * fraction2;
912
913    // STEP 6: check the second side
914    if(side == 1)this->checkCollisionRayN(node->left,middleFraction, endFraction, &middle, end);
915
916    else this->checkCollisionRayN(node->right,middleFraction, endFraction,&middle, end );
917
918
919  }
920
921}
922
923float BspManager::checkPatchAltitude(BspTreeNode* node)
924{
925  leaf& curLeaf = this->bspFile->leaves[node->leafIndex];
926  for(int i = 0; i < curLeaf.n_leaffaces ; i++) {}
927  return 10.0f;
928}
929
930void BspManager::checkCollisionBox(void)
931{}
932
933
934void BspManager::TraceBox( Vector& inputStart, Vector& inputEnd,
935                           Vector& inputMins, Vector& inputMaxs )
936{
937  if (inputMins.x == 0 && inputMins.y == 0 && inputMins.z == 0 &&
938      inputMaxs.x == 0 && inputMaxs.y == 0 && inputMaxs.z == 0) {   // the user called TraceBox, but this is actually a ray
939    //!> FIXME TraceRay( inputStart, inputEnd );
940  } else {   // setup for a box
941    //traceType = TT_BOX;
942    this->traceMins = inputMins;
943    this->traceMaxs = inputMaxs;
944    this->traceExtents.x = -traceMins.x > traceMaxs.x ?
945                           -traceMins.x : traceMaxs.x;
946    this->traceExtents.y = -traceMins.y > traceMaxs.y ?
947                           -traceMins.y : traceMaxs.y;
948    this->traceExtents.z = -traceMins.z > traceMaxs.z ?
949                           -traceMins.z : traceMaxs.z;
950    //!> FIXME Trace( inputStart, inputEnd );
951  }
952}
953
954void BspManager::checkCollision(WorldEntity* worldEntity)
955{
956
957  // Init  Collision Detection
958  this->outputStartsOut = true;
959  this->outputAllSolid = false;
960  this->outputFraction = 1.0f;
961
962
963  // Retrieve Bounding box
964  AABB* box = worldEntity->getModelAABB();
965
966
967  Vector forwardDir = Vector(0.0,0.0,1.0);
968  Vector upDir = Vector(0.0,1.0,0.0);
969  Vector position = worldEntity->getAbsCoor();
970
971  bool SolidFlag = false;
972  bool collision = false;
973  Vector position1 = position;
974  Vector position2 = position + Vector(0.0,1.0,0.0);
975  Vector position3 = position;
976  Vector position4 = position + Vector(0.0,1.0,0.0);
977  Vector dest = worldEntity->getAbsCoor() - upDir*40.0f; //
978  Vector dest1 = position + forwardDir*4.0f;
979  Vector dest2 = position2 + forwardDir*4.0;
980  Vector dest3 = position + forwardDir*4.0f;
981  Vector dest4 = position2 + forwardDir*4.0;
982  dest = position - Vector(0.0, 40.0,0.0);
983  Vector out = dest;
984  Vector out1;
985  Vector out2;
986
987
988  plane* testPlane;
989
990  bool xCollision = false;
991  bool zCollision = false;
992
993
994  float height = 40;
995
996
997  if( box != NULL) {
998    position = worldEntity->getAbsCoor() +  box->center; // + Vector(0.0, 1.0, 0.0) * box->halfLength[1] * 1.0f;
999    dest     = worldEntity->getAbsCoor() +  box->center - Vector(0.0, 1.0, 0.0) * (box->halfLength[1] + BSP_Y_OFFSET) *   100;
1000
1001    Vector dirX =  worldEntity->getAbsDirX(); dirX.y = 0.0f; dirX.normalize();
1002
1003    //position1 = worldEntity->getAbsCoor() +  box->center - dirX * (box->halfLength[0]  + BSP_X_OFFSET);
1004    dest1     = worldEntity->getAbsCoor() +  box->center + dirX * (box->halfLength[0]  + BSP_X_OFFSET);
1005    dest2     = worldEntity->getAbsCoor() -  box->center + dirX * (box->halfLength[0]  + BSP_X_OFFSET);
1006
1007    Vector dirZ =  worldEntity->getAbsDirZ(); dirX.y = 0.0f; dirZ.normalize();
1008    //position2 = worldEntity->getAbsCoor() +  box->center - dirZ * (box->halfLength[2]  + BSP_Z_OFFSET);
1009    dest3     = worldEntity->getAbsCoor() +  box->center + dirZ * (box->halfLength[2]  + BSP_Z_OFFSET);
1010    dest4     = worldEntity->getAbsCoor() -  box->center + dirZ * (box->halfLength[2]  + BSP_Z_OFFSET);
1011  } else {
1012    // Init positions and destinations to anything useful!
1013
1014  }
1015
1016
1017
1018  // 1st Ray: Y RAY
1019  this->inputStart =  position;
1020  this->inputEnd =   dest;
1021  this->checkCollisionRayN(this->root,0.0f,1.0f, &position, &dest );
1022
1023
1024  //
1025  if(!this->outputStartsOut ) {
1026    this->collPlane = new plane;
1027    this->collPlane->x = 0.0f;
1028    this->collPlane->y = 0.0f;
1029    this->collPlane->z = 0.0f;
1030    collision = true;
1031  } else {
1032
1033    if( this->outputFraction == 1.0f) {
1034      if(this->outputAllSolid ) {
1035        this->collPlane = new plane;
1036        this->collPlane->x = 0.0f;
1037        this->collPlane->y = 0.0f;
1038        this->collPlane->z = 0.0f;
1039        collision = true;
1040        SolidFlag = true;
1041      } else
1042        collision = false;
1043
1044
1045      out = dest;
1046
1047    } else {
1048      collision = true;
1049      out.x = position.x + (dest.x -position.x) * this->outputFraction;
1050      out.y = position.y + (dest.y -position.y) * this->outputFraction;
1051      out.z = position.z + (dest.z -position.z) * this->outputFraction;
1052
1053      Vector out3 = out + Vector(height*this->collPlane->x,height*this->collPlane->y,height*this->collPlane->z);
1054      this->out = out;
1055    }
1056  }
1057    testPlane = this->collPlane;
1058
1059
1060  bool xCollisionNeg = false;
1061  bool zCollisionNeg = false;
1062
1063
1064
1065    // 2nd Collision Detection X-RAY
1066    this->outputStartsOut = true;
1067    this->outputAllSolid = false;
1068    this->outputFraction = 1.0f;
1069    this->inputStart =  position1;
1070    this->inputEnd =   dest1;
1071    this->checkCollisionRayN(this->root,0.0f,1.0f, &position1, &dest1 );
1072
1073    if(this->outputFraction < 1.0f) {
1074      out.x = dest1.x + (dest1.x -position1.x) * this->outputFraction;
1075      dest1 = position1 + (dest1 -position1) * this->outputFraction;
1076      xCollision = true;
1077      testPlane = this->collPlane;
1078    }
1079    if(this->outputAllSolid ) {
1080
1081      this->collPlane = new plane;
1082      this->collPlane->x = 0.0f;
1083      this->collPlane->y = 0.0f;
1084      this->collPlane->z = 0.0f;
1085      testPlane = this->collPlane;
1086      SolidFlag = true;
1087      xCollision = true;
1088    }
1089    //out.z = this->outputFraction;
1090
1091
1092
1093      // 3rd Collision Detection Z-RAY
1094      this->outputStartsOut = true;
1095      this->outputAllSolid = false;
1096      this->outputFraction = 1.0f;
1097      this->inputStart =  position2;
1098      this->inputEnd =   dest2;
1099
1100      this->checkCollisionRayN(this->root,0.0f,1.0f, &position2, &dest2 );
1101      //out.x = this->outputFraction;
1102
1103      if(this->outputFraction < 1.0f ) {
1104        out.z = out.z = dest2.z + (dest2.z -position2.z) * this->outputFraction;
1105        dest2 = position2 + (dest2 -position2) * this->outputFraction;
1106        zCollision = true;
1107        testPlane = this->collPlane;
1108
1109      }
1110      if(this->outputAllSolid ) {
1111        this->collPlane = new plane;
1112        this->collPlane->x = 0.0f;
1113        this->collPlane->y = 0.0f;
1114        this->collPlane->z = 0.0f;
1115        testPlane = this->collPlane;
1116
1117        SolidFlag = true;
1118        zCollision = true;
1119      }
1120
1121
1122  // Return the normal here: Normal's stored in this->collPlane;
1123  if( collision) {
1124    worldEntity->registerCollision(COLLISION_TYPE_AXIS_Y , this->parent, worldEntity, Vector(testPlane->x, testPlane->y, testPlane->z), out, SolidFlag);
1125}
1126  if(xCollision) {
1127    worldEntity->registerCollision(COLLISION_TYPE_AXIS_X , this->parent, worldEntity, Vector(testPlane->x, testPlane->y, testPlane->z),dest1 , SolidFlag);
1128  }
1129
1130  if(zCollision) {
1131    worldEntity->registerCollision(COLLISION_TYPE_AXIS_Z , this->parent, worldEntity, Vector(testPlane->x, testPlane->y, testPlane->z), dest2 , SolidFlag);
1132  }
1133
1134
1135}
1136
1137
1138
1139/**
1140 * check the collision in the x direction (forward, backward)
1141 */
1142void BspManager::checkCollisionX(WorldEntity* entity)
1143{
1144  // Retrieve Bounding box
1145  AABB* box = entity->getModelAABB();
1146
1147
1148  plane*            testPlane          = NULL;  //!< the collision test plane
1149
1150  Vector            forward;                    //!< left collision ray
1151  Vector            backward;                   //!< right collision ray
1152  Vector            collPos;                    //!< the collision position
1153
1154  bool              xCollisionForward  = false; //!< flag true if right collision
1155  bool              xCollisionBackward = false; //!< flag true if left collision
1156  bool              SolidFlag          = false; //!< flag set true if solid
1157
1158  Vector            position;                   //!< current position of the entity
1159  Vector            dirX;                       //!< direction x
1160
1161  position = entity->getAbsCoor();
1162  dirX =  entity->getAbsDirX(); dirX.y = 0.0f; dirX.normalize();
1163
1164  // calculate the rays
1165  if( box != NULL)
1166  {
1167    forward  = entity->getAbsCoor() +  box->center + dirX * (box->halfLength[0]  + BSP_X_OFFSET);
1168    backward = entity->getAbsCoor() -  box->center + dirX * (box->halfLength[0]  + BSP_X_OFFSET);
1169  }
1170  else
1171  {
1172    forward  = position + dirX * 4.0f;
1173    backward = position + Vector(0.0, 1.0, 0.0) + dirX * 4.0;
1174  }
1175
1176
1177  /*   X Ray forward  */
1178  // init some member variables before collision check
1179  this->outputStartsOut = true;
1180  this->outputAllSolid = false;
1181  this->outputFraction = 1.0f;
1182  this->inputStart =  position;
1183  this->inputEnd =   forward;
1184  this->checkCollisionRayN(this->root, 0.0f, 1.0f, &position, &forward );
1185
1186  // collision occured
1187  if( this->outputFraction < 1.0f)
1188  {
1189    collPos = position + (forward - position) * this->outputFraction;
1190    xCollisionForward = true;
1191    testPlane = this->collPlane;
1192  }
1193  if(this->outputAllSolid )
1194  {
1195    this->collPlane = new plane;
1196    this->collPlane->x = 0.0f;
1197    this->collPlane->y = 0.0f;
1198    this->collPlane->z = 0.0f;
1199    testPlane = this->collPlane;
1200    SolidFlag = true;
1201    xCollisionForward = true;
1202  }
1203
1204  // collision registration
1205  if( xCollisionForward) {
1206    entity->registerCollision(COLLISION_TYPE_AXIS_X ,
1207                              this->parent, entity,
1208                              Vector(testPlane->x, testPlane->y, testPlane->z),
1209                              collPos,
1210                              SolidFlag);
1211  }
1212
1213
1214
1215  /*   X Ray backward  */
1216  // init some member variables before collision check
1217  this->outputStartsOut = true;
1218  this->outputAllSolid = false;
1219  this->outputFraction = 1.0f;
1220  this->inputStart =  position;
1221  this->inputEnd =   backward;
1222  this->checkCollisionRayN(this->root, 0.0f, 1.0f, &position, &backward );
1223
1224  // collision occured
1225  if( this->outputFraction < 1.0f)
1226  {
1227    collPos = position + (backward - position) * this->outputFraction;
1228    xCollisionBackward = true;
1229    testPlane = this->collPlane;
1230  }
1231  if(this->outputAllSolid )
1232  {
1233    this->collPlane = new plane;
1234    this->collPlane->x = 0.0f;
1235    this->collPlane->y = 0.0f;
1236    this->collPlane->z = 0.0f;
1237    testPlane = this->collPlane;
1238    SolidFlag = true;
1239    xCollisionBackward = true;
1240  }
1241
1242  // collision registration
1243  if( xCollisionBackward) {
1244    entity->registerCollision(COLLISION_TYPE_AXIS_X_NEG ,
1245                              this->parent, entity,
1246                              Vector(testPlane->x, testPlane->y, testPlane->z),
1247                              collPos,
1248                              SolidFlag);
1249  }
1250
1251}
1252
1253/**
1254 * check the collision in the y direction (up, down)
1255 */
1256void BspManager::checkCollisionY(WorldEntity* entity)
1257{
1258  // Retrieve Bounding box
1259  AABB* box = entity->getModelAABB();
1260
1261
1262  plane*            testPlane          = NULL;  //!< the collision test plane
1263
1264  Vector            right;                      //!< right collision ray
1265  Vector            left;                       //!< left collision ray
1266  Vector            collPos;                    //!< the collision position
1267
1268  bool              zCollisionRight    = false; //!< flag true if right collision
1269  bool              zCollisionLeft     = false; //!< flag true if left collision
1270  bool              SolidFlag          = false; //!< flag set true if solid
1271
1272  Vector            position;                   //!< current position of the entity
1273  Vector            dirZ;                       //!< direction x
1274
1275  position = entity->getAbsCoor();
1276  dirZ =  entity->getAbsDirZ(); dirZ.y = 0.0f; dirZ.normalize();
1277
1278  // calculate the rays
1279  if( box != NULL)
1280  {
1281    right = entity->getAbsCoor() +  box->center + dirZ * (box->halfLength[2]  + BSP_Z_OFFSET);
1282    left  = entity->getAbsCoor() -  box->center + dirZ * (box->halfLength[2]  + BSP_Z_OFFSET);
1283  }
1284  else
1285  {
1286    right = position + dirZ * 4.0f;
1287    left  = position + Vector(0.0, 1.0, 0.0) + dirZ * 4.0;
1288  }
1289
1290
1291  /*   Z Ray right */
1292  // init some member variables before collision check
1293  this->outputStartsOut = true;
1294  this->outputAllSolid = false;
1295  this->outputFraction = 1.0f;
1296  this->inputStart =  position;
1297  this->inputEnd =   right;
1298  this->checkCollisionRayN(this->root, 0.0f, 1.0f, &position, &right );
1299
1300
1301  // collision occured
1302  if( this->outputFraction < 1.0f )
1303  {
1304    collPos = position + (right - position) * this->outputFraction;
1305    zCollisionRight = true;
1306    testPlane = this->collPlane;
1307  }
1308  if(this->outputAllSolid )
1309  {
1310    this->collPlane = new plane;
1311    this->collPlane->x = 0.0f;
1312    this->collPlane->y = 0.0f;
1313    this->collPlane->z = 0.0f;
1314    testPlane = this->collPlane;
1315
1316    SolidFlag = true;
1317    zCollisionRight = true;
1318  }
1319
1320
1321  if( zCollisionRight) {
1322    entity->registerCollision(COLLISION_TYPE_AXIS_Z , this->parent,
1323                                   entity,
1324                                   Vector(testPlane->x, testPlane->y, testPlane->z),
1325                                   collPos , SolidFlag);
1326  }
1327
1328
1329}
1330
1331
1332/**
1333 * check the collision in the z direction (left, right)
1334 */
1335void BspManager::checkCollisionZ(WorldEntity* entity)
1336{
1337
1338}
1339
1340
1341
1342
1343
1344void  BspManager::checkCollision(BspTreeNode* node, Vector* cam)
1345{
1346  Vector next = this->cam;
1347  next.x =   (State::getCameraTargetNode()->getLastAbsCoor()).x ;
1348  next.y =   (State::getCameraTargetNode()->getLastAbsCoor()).y ;
1349  next.z =   (State::getCameraTargetNode()->getLastAbsCoor()).z ;
1350
1351  float dist = 0;
1352  if(!(node->isLeaf)) {
1353    dist = (node->plane.x * this->cam.x + node->plane.y*this->cam.y + node->plane.z*this->cam.z) - node->d;
1354    if(dist > 4.0f) {
1355      checkCollision(node->left,cam);
1356      return;
1357    }
1358    if(dist < -4.0f) {
1359      checkCollision(node->right,cam);
1360      return;
1361    }
1362    if(dist<=4.0f && dist >= -4.0f) {
1363      checkCollision(node->left,cam);
1364      checkCollision(node->right,cam);
1365      return;
1366    }
1367    return;
1368  } else {
1369
1370    leaf& camLeaf =  ((leaf *)(this->bspFile->leaves))[(node->leafIndex ) ];
1371
1372    if (camLeaf.cluster < 0) {
1373      this->drawDebugCube(&this->cam);
1374      this->drawDebugCube(&next);
1375      // State::getPlayer()->getPlayable()->setRelCoor(-100,-100,-100);
1376      //State::getPlayer()->getPlayable()->collidesWith(NULL, State::getCameraTargetNode()->getLastAbsCoor());
1377    }
1378
1379
1380    /*
1381        for(int i = 0; i < camLeaf.n_leafbrushes && i < 10; i++ )
1382        {
1383                brush& curBrush = ((brush*)(this->bspFile->brushes))[(camLeaf.leafbrush_first +i)%this->bspFile->numLeafBrushes];
1384                if(curBrush.n_brushsides < 0) return;
1385                for(int j = 0; j < curBrush.n_brushsides; j++)
1386                {
1387                float dist = -0.1;
1388                brushside& curBrushSide = ((brushside*)(this->bspFile->brushSides))[(curBrush.brushside +j)%this->bspFile->numBrushSides];
1389                plane&      testPlane = ((plane*)(this->bspFile->planes))[curBrushSide.plane % this->bspFile->numPlanes];
1390                dist = testPlane.x * this->cam.x +  testPlane.y * this->cam.y  +  testPlane.z * this->cam.z   -testPlane.d ;
1391
1392                if(dist < -0.01f) dist = -1.0f *dist;
1393                if(dist < 1.0f){
1394                                this->drawDebugCube(&this->cam);
1395                                return;
1396                              }
1397                }
1398
1399        } */
1400
1401  }
1402  return;
1403}
1404
1405void BspManager::drawDebugCube(Vector* cam)
1406{
1407  glBegin(GL_QUADS);
1408
1409  // Bottom Face.  Red, 75% opaque, magnified texture
1410
1411  glNormal3f( 0.0f, -1.0f, 0.0f); // Needed for lighting
1412  glColor4f(0.9,0.2,0.2,.75); // Basic polygon color
1413
1414  glTexCoord2f(0.800f, 0.800f); glVertex3f(cam->x-1.0f, cam->y-1.0f,cam->z -1.0f);
1415  glTexCoord2f(0.200f, 0.800f); glVertex3f(cam->x+1.0f, cam->y-1.0f,cam->z -1.0f);
1416  glTexCoord2f(0.200f, 0.200f); glVertex3f(cam->x+ 1.0f,cam->y -1.0f,cam->z +  1.0f);
1417  glTexCoord2f(0.800f, 0.200f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z + 1.0f);
1418
1419
1420  // Top face; offset.  White, 50% opaque.
1421
1422  glNormal3f( 0.0f, 1.0f, 0.0f);  glColor4f(0.5,0.5,0.5,.5);
1423
1424  glTexCoord2f(0.005f, 1.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.0f);
1425  glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f,  cam->z +1.0f);
1426  glTexCoord2f(1.995f, 0.005f); glVertex3f(cam->x+ 1.0f,  cam->y+1.0f,  cam->z +1.0f);
1427  glTexCoord2f(1.995f, 1.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z -1.0f);
1428
1429
1430  // Far face.  Green, 50% opaque, non-uniform texture cooridinates.
1431
1432  glNormal3f( 0.0f, 0.0f,-1.0f);  glColor4f(0.2,0.9,0.2,.5);
1433
1434  glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z -1.3f);
1435  glTexCoord2f(2.995f, 2.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.3f);
1436  glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x+ 1.0f,cam->y+  1.0f, cam->z -1.3f);
1437  glTexCoord2f(0.005f, 0.005f); glVertex3f( cam->x+1.0f,cam->y -1.0f, cam->z -1.3f);
1438
1439
1440  // Right face.  Blue; 25% opaque
1441
1442  glNormal3f( 1.0f, 0.0f, 0.0f);  glColor4f(0.2,0.2,0.9,.25);
1443
1444  glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y -1.0f, cam->z -1.0f);
1445  glTexCoord2f(0.995f, 0.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z -1.0f);
1446  glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z + 1.0f);
1447  glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y-1.0f,  cam->z +1.0f);
1448
1449
1450  // Front face; offset.  Multi-colored, 50% opaque.
1451
1452  glNormal3f( 0.0f, 0.0f, 1.0f);
1453
1454  glColor4f( 0.9f, 0.2f, 0.2f, 0.5f);
1455  glTexCoord2f( 0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f,  cam->z +1.0f);
1456  glColor4f( 0.2f, 0.9f, 0.2f, 0.5f);
1457  glTexCoord2f( 0.995f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y-1.0f,  cam->z +1.0f);
1458  glColor4f( 0.2f, 0.2f, 0.9f, 0.5f);
1459  glTexCoord2f( 0.995f, 0.995f); glVertex3f( cam->x+1.0f,  cam->y+1.0f,  cam->z +1.0f);
1460  glColor4f( 0.1f, 0.1f, 0.1f, 0.5f);
1461  glTexCoord2f( 0.005f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f,  cam->z +1.0f);
1462
1463
1464  // Left Face; offset.  Yellow, varying levels of opaque.
1465
1466  glNormal3f(-1.0f, 0.0f, 0.0f);
1467
1468  glColor4f(0.9,0.9,0.2,0.0);
1469  glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z -1.0f);
1470  glColor4f(0.9,0.9,0.2,0.66);
1471  glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x-1.0f,cam->y -1.0f,  cam->z +1.0f);
1472  glColor4f(0.9,0.9,0.2,1.0);
1473  glTexCoord2f(0.995f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f,  cam->z +1.0f);
1474  glColor4f(0.9,0.9,0.2,0.33);
1475  glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.0f);
1476
1477  glEnd();
1478}
1479
1480void BspManager::addFace(int f)
1481{
1482  face& curFace =  ((face *)(this->bspFile->faces))[f];
1483  if(this->bspFile->Materials[curFace.texture].alpha) this->trasparent.push_back(f);
1484  else this->opal.push_back(f);
1485}
Note: See TracBrowser for help on using the repository browser.