Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

bsp: better collision ground reaction: now it jumps back. but there are still bugs and freezes

File size: 34.5 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
47
48//CREATE_FACTORY( BspManager, CL_BSP_MODEL);
49
50BspManager::BspManager(WorldEntity* parent)
51{
52  this->parent = parent;
53  /*// open a BSP file
54  this->bspFile = new BspFile();
55  this->bspFile->scale = 0.4f;
56  this->bspFile->read(ResourceManager::getFullName("test.bsp").c_str());
57  this->bspFile->build_tree();
58  this->root  = this->bspFile->get_root();
59  this->alreadyVisible = new bool [this->bspFile->numFaces];
60  */
61
62}
63
64
65/*
66BspManager::BspManager(const TiXmlElement* root)
67{
68
69
70  if( root != NULL)
71    this->loadParams(root);
72
73  CDEngine::getInstance()->setBSPModel(this);
74} */
75
76void BspManager::load(const char* fileName, float scale)
77{
78  // open a BSP file
79  this->bspFile = new BspFile();
80  this->bspFile->scale =  scale;
81  this->bspFile->read(ResourceManager::getFullName(fileName).c_str());
82  this->bspFile->build_tree();
83  this->root  = this->bspFile->get_root();
84  this->alreadyVisible = new bool [this->bspFile->numFaces];
85
86  this->outputFraction = 1.0f;
87}
88/*
89BspManager::BspManager(const char* fileName, float scale)
90{
91  // open a BSP file
92  this->bspFile = new BspFile();
93  this->bspFile->scale =  scale;
94  this->bspFile->read(fileName);
95  this->bspFile->build_tree();
96  this->root  = this->bspFile->get_root();
97  this->alreadyVisible = new bool [this->bspFile->numFaces];
98
99  CDEngine::getInstance()->setBSPModel(this);
100}
101*/
102
103const void BspManager::tick(float time)
104{
105
106  if(!this->bspFile->MovieMaterials.empty())
107  {
108      ::std::vector<MoviePlayer *>::iterator it = this->bspFile->MovieMaterials.begin() ;
109      while(it != this->bspFile->MovieMaterials.end())
110      {
111         (*it)->tick(time);
112        it++;
113      }
114 //this->bspFile->MovieMaterials.front()->tick(time );
115
116
117  }
118
119}
120const void BspManager::draw()
121{
122
123  /*
124  this->drawDebugCube(&this->out);
125  this->out1 = this->out;
126  this->out2 = this->out;
127  if(this->collPlane != NULL) {
128    this->out1.x += this->collPlane->x*5.0;
129    this->out1.y += this->collPlane->y*5.0;
130    this->out1.z += this->collPlane->z*5.0;
131
132    this->out2.x += this->collPlane->x*10.0;
133    this->out2.y += this->collPlane->y*10.0;
134    this->out2.z += this->collPlane->z*10.0;
135  }
136  this->drawDebugCube(&this->out1);
137  this->drawDebugCube(&this->out2);
138
139  */
140
141
142  // Draw Debug Terrain
143  /*
144  this->bspFile->Materials[0]->select();
145  for(int i = 0; i <  this->bspFile->numPatches ; i++)
146        {
147                this->bspFile->VertexArrayModels[i]->draw();
148
149        }
150  */
151
152
153
154  // erase alreadyVisible
155  for(int i = 0; i < this->bspFile->numFaces; i++) this->alreadyVisible[i] = false;
156  float tmp = 0;
157  //this->opal.clear();
158  //this->trasparent.clear();
159  // Find all visible faces...
160
161  this->cam = State::getCamera()->getAbsCoor() ;
162  //this->ship = State::getCameraTargetNode()->getAbsCoor();
163
164
165
166
167
168  this->viewDir=    State::getCamera()->getAbsDirX();
169  float d = (cam.x*viewDir.x + cam.y*viewDir.y + cam.z * viewDir.z);
170
171  BspTreeNode*  ActLeaf = this->getLeaf(this->bspFile->root, &ship);
172  int viscluster = -1;
173  viscluster =((leaf*)(this->bspFile->leaves))[ ActLeaf->leafIndex].cluster; // get the players cluster (viscluster)
174
175
176
177
178 // this->checkCollision(this->root, &this->cam);   //!< Test Collision Detection
179
180
181  this->outputStartsOut = true;
182  this->outputAllSolid = false;
183  this->outputFraction = 1.0f;
184
185  if ( viscluster < 0  || ((int *)(this->bspFile->header))[35] == 0 )  //!< if (sizeof(Visdata) == 0)
186  {
187
188
189
190    // Iterate through all Leafs
191    for(int i = 0; i <  this->bspFile->numLeafs   ; i++ )
192    {
193      // cluster =  (this->bspFile->leaves)[i].cluster;
194      leaf& curLeaf = (this->bspFile->leaves)[i];
195      if(curLeaf.cluster<0) continue;
196
197      /** Do Frustum culling and draw 'em all **/
198
199      Vector dir = State::getCameraNode()->getAbsDirX();
200
201      float dist =  dir.x*this->cam.x +dir.y*this->cam.y +dir.z*this->cam.z;
202      //if(dist < 0) dist = -dist;
203      const float dMins = dir.x*(float)curLeaf.mins[0] +dir.y*(float)curLeaf.mins[1] +dir.z*(float)curLeaf.mins[2] - dist ;
204      const float dMaxs = dir.x*(float)curLeaf.maxs[0] +dir.y*(float)curLeaf.maxs[1] +dir.z*(float)curLeaf.maxs[2] - dist ;
205
206      if(dMins < -300.0 && dMaxs < -300.0) {
207        continue;
208      }
209      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) {
210        continue;
211      }
212
213
214      // Iterate through all faces
215      for (int j = 0; j < curLeaf.n_leaffaces ; ++j) {
216        const int g = (j +  curLeaf.leafface);
217        const int f = ((int *)this->bspFile->leafFaces)[g];
218        if (f >=0 && !this->isAlreadyVisible(f)) {
219          this->alreadyVisible[f] = true;
220          addFace(f); // "visibleFaces.append(f)"
221        }
222      }
223
224
225
226
227    } //for
228  } else {
229
230
231    unsigned int v;
232    unsigned char  visSet;
233
234    // Iterate through all Leafs
235
236    for(int i = 0; i <  this->bspFile->numLeafs   ; ++i ) {
237      leaf& camLeaf =  (this->bspFile->leaves)[ActLeaf->leafIndex] ;
238      leaf& curLeaf =  (this->bspFile->leaves)[i] ;
239      int& cluster =  curLeaf.cluster;
240
241      if(cluster < 0) continue;
242      v = ((viscluster *  ( ((int *)this->bspFile->visData)[1]) ) + (cluster / 8));
243      visSet =((char*) (this->bspFile->visData))[v + 8];
244
245      // gets bit of visSet
246      if( ((visSet) & (1 << (cluster &  7))) != 0 ) {
247
248        // Frustum culling
249
250        Vector dir;
251        dir.x = State::getCameraNode()->getAbsDirX().x;
252        dir.y =  State::getCameraNode()->getAbsDirX().y;
253        dir.z =  State::getCameraNode()->getAbsDirX().z;
254        const float dist =  dir.x*this->cam.x +dir.y*this->cam.y +dir.z*this->cam.z;
255        //if(dist < 0) dist = -dist;
256        const float dMins = dir.x*(float)curLeaf.mins[0] +dir.y*(float)curLeaf.mins[1] +dir.z*(float)curLeaf.mins[2] - dist;
257        const float dMaxs = dir.x*(float)curLeaf.maxs[0] +dir.y*(float)curLeaf.maxs[1] +dir.z*(float)curLeaf.maxs[2] - dist;
258
259        if(dMins < -50.0 && dMaxs < -  50.0) {
260          continue;
261        }
262
263
264        // Iterate through all faces
265        for (int j = 0; j < curLeaf.n_leaffaces ; ++j) {
266          const int g = (j +  curLeaf.leafface);
267          const int f = ((int *)this->bspFile->leafFaces)[g];
268
269          if (!this->isAlreadyVisible(f) && f>=0) {
270            this->addFace(f);
271            this->alreadyVisible[f] = true;
272          }
273
274        }
275
276      }// if
277
278    }//for
279
280  }//else
281
282  while(!this->opal.empty()) {
283    this->draw_face(this->opal.front());
284    this->opal.pop_front();
285  }
286  while(!this->trasparent.empty()) {
287    this->draw_face(this->trasparent.back());
288    this->trasparent.pop_back();
289  }
290  //glEnable(GL_TEXTURE_2D);
291  glActiveTextureARB(GL_TEXTURE1_ARB);
292  glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap);
293
294
295
296}//draw
297
298
299
300void BspManager::draw_face(int curface)
301{
302  face& curFace =  (this->bspFile->faces)[curface];
303  const BspVertex* curVertex = (BspVertex *) this->bspFile->vertice;
304  int stride = sizeof(BspVertex);  // sizeof(Vertex)
305  int offset    = curFace.vertex;
306  if (curFace.effect != -1) return;
307  // PRINTF(0)("BSP Manager: ");
308  // PRINTF(0)("BSP Manager: type: %i  \n", curFace.texture);
309
310  //  if(  curFace.texture < 0 ) return;
311  if(curFace.type == 2) {
312    this->draw_patch( &curFace);
313    return;
314  }
315 // if(curFace.type != 1) return;
316  if((char*)(this->bspFile->textures)[curFace.texture*72]== 0) return;
317
318  if(this->lastTex != curFace.texture) {
319    if(this->bspFile->Materials[curFace.texture].animated) {
320     // glBlendFunc(GL_ZERO,GL_ONE);
321
322
323
324      if(this->bspFile->Materials[curFace.texture].aviMat->getStatus() == 2) this->bspFile->Materials[curFace.texture].aviMat->start(0);
325      //this->bspFile->Materials[curFace.texture].aviMat->tick(0.005);
326      int n =  this->bspFile->Materials[curFace.texture].aviMat->getTexture();
327      glActiveTextureARB(GL_TEXTURE0_ARB);
328      glBindTexture(GL_TEXTURE_2D, n );
329      this->lastTex = curFace.texture;
330
331    } else {
332      this->bspFile->Materials[curFace.texture].mat->select();
333      this->lastTex = curFace.texture;
334    }
335  }
336
337  if(curFace.lm_index < 0) {
338    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
339    glActiveTextureARB(GL_TEXTURE1_ARB);
340    glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap );
341    glEnable(GL_TEXTURE_2D);
342  } else {
343   // glEnable(GL_BLEND);
344    //glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
345    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
346    glActiveTextureARB(GL_TEXTURE1_ARB);
347    glBindTexture(GL_TEXTURE_2D, this->bspFile->glLightMapTextures[curFace.lm_index]);
348    glEnable(GL_TEXTURE_2D);
349  //  glDisable(GL_BLEND);
350  }
351
352  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
353
354  // glColor4f(3.0,3.0,3.0,1.0);
355  glEnableClientState(GL_VERTEX_ARRAY );
356  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
357  glEnableClientState(GL_NORMAL_ARRAY );
358  //  glEnableClientState(GL_COLOR_ARRAY);
359
360
361  glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0]));
362
363  glClientActiveTextureARB(GL_TEXTURE0_ARB);
364  glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0]));
365  //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
366
367  glClientActiveTextureARB(GL_TEXTURE1_ARB);
368  glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1]));
369  //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
370
371
372  glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0]));
373  // glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0]));
374  glDrawElements(GL_TRIANGLES, curFace.n_meshverts,
375                 GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert]));
376
377  glDisableClientState(GL_TEXTURE0_ARB);
378  glDisableClientState(GL_TEXTURE1_ARB);
379  glDisableClientState(GL_VERTEX_ARRAY );
380  glDisableClientState(GL_TEXTURE_COORD_ARRAY );
381  glDisableClientState(GL_NORMAL_ARRAY );
382  // glDisableClientState(GL_COLOR_ARRAY);
383
384}
385
386
387void BspManager::draw_debug_face(int curface)
388{
389  face& curFace =  (this->bspFile->faces)[curface];
390  const BspVertex* curVertex = (BspVertex *) this->bspFile->vertice;
391  int stride = 44;  // sizeof(Vertex)
392  int offset    = curFace.vertex;
393
394  // PRINTF(0)("BSP Manager: ");
395  // PRINTF(0)("BSP Manager: type: %i  \n", curFace.texture);
396
397  //  if(  curFace.texture < 0 ) return;
398  if(curFace.type == 2) {
399    this->draw_patch( &curFace);
400    return;
401  }
402  if(curFace.type == 3) return;
403  // if(this->bspFile->Materials[curFace.texture] != NULL)
404
405  this->bspFile->Materials[2].mat->select();
406  this->lastTex = 2;
407
408  glEnableClientState(GL_VERTEX_ARRAY );
409  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
410  glEnableClientState(GL_NORMAL_ARRAY );
411  //glEnableClientState(GL_COLOR_ARRAY);
412  // glEnableClientState(GL_VERTEX_ARRAY );
413  glClientActiveTextureARB(GL_TEXTURE0_ARB);
414  glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0]));
415  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
416  // glClientActiveTextureARB(GL_TEXTURE0_ARB);
417  glClientActiveTextureARB(GL_TEXTURE1_ARB);
418  glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0]));
419  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
420  // glClientActiveTextureARB(GL_TEXTURE1_ARB);
421  // glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1]));
422  //glEnableClientState(GL_NORMAL_ARRAY );
423
424  glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0]));
425  //  glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0]));
426  glDrawElements(GL_TRIANGLES, curFace.n_meshverts,
427                 GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert]));
428
429}
430
431void BspManager::draw_patch(face* Face)
432{
433  if(this->lastTex != Face->texture) {
434    this->bspFile->Materials[Face->texture].mat->select();
435    this->lastTex = Face->texture;
436  }
437  if (Face->effect != -1) return;
438
439
440  if(Face->lm_index < 0) {
441    glActiveTextureARB(GL_TEXTURE1_ARB);
442    glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap);
443    glEnable(GL_TEXTURE_2D);
444  } else {
445    glActiveTextureARB(GL_TEXTURE1_ARB);
446    glBindTexture(GL_TEXTURE_2D, this->bspFile->glLightMapTextures[Face->lm_index]);
447    glEnable(GL_TEXTURE_2D);
448  }
449  //glColor4f(3.0,3.0,3.0,1.0);
450
451  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
452  glEnable( GL_AUTO_NORMAL);
453  glEnableClientState(GL_VERTEX_ARRAY );
454  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
455  for(int i = Face->n_meshverts -1; i >=0   ; i--) {
456    //glFrontFace(GL_CW);
457    //PRINTF(0)("BSP Manager: Face->size[0]: %i . \n", Face->size[0]);
458
459
460    //glEnableClientState(GL_NORMAL_ARRAY );
461
462    glVertexPointer(3, GL_FLOAT,44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).position[0]));
463
464
465    glClientActiveTextureARB(GL_TEXTURE0_ARB);
466    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
467    glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[0][0]));
468
469
470
471    glClientActiveTextureARB(GL_TEXTURE1_ARB);
472    glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[1][0]));
473    //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
474
475
476    //  glNormalPointer( GL_FLOAT, 44,&((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).normal[0]) );
477
478
479
480
481    for(int row=6; row>=0; --row) {
482      glDrawElements(GL_TRIANGLE_STRIP, 2*(8), GL_UNSIGNED_INT,
483                     & (     (((GLuint*)  (this->bspFile->patchIndexes))[7*8*2*(Face->meshvert+i)+ row*2*8]  ))  );
484    }
485
486    //glFrontFace(GL_CCW);
487  }
488  glDisableClientState(GL_TEXTURE0_ARB);
489  glDisableClientState(GL_TEXTURE1_ARB);
490  glDisable(GL_AUTO_NORMAL);
491  glDisableClientState(GL_VERTEX_ARRAY );
492  glDisableClientState(GL_TEXTURE_COORD_ARRAY );
493
494
495}
496
497bool BspManager::isAlreadyVisible(int Face)
498{
499  return this->alreadyVisible[Face];
500}
501
502
503BspTreeNode*  BspManager::getLeaf(BspTreeNode* node, Vector* cam)
504{
505  float dist = 0;
506  while(!(node->isLeaf)) {
507    dist = (node->plane.x * this->cam.x + node->plane.y*this->cam.y + node->plane.z*this->cam.z) - node->d;
508    if(dist >= 0.0f) {
509      node = node->left;
510    } else {
511      node = node->right;
512    }
513  }
514  return  node;
515}
516
517void BspManager::checkBrushRay(brush* curBrush)
518{
519  float EPSILON = 0.000001;
520  float startDistance;
521  float endDistance;
522
523  float startFraction = -1.0f;
524  float endFraction = 1.0f;
525  bool startsOut = false;
526  bool endsOut = false;
527
528  Vector inputStart = State::getCameraTargetNode()->getLastAbsCoor();
529  Vector inputEnd   = State::getCameraTargetNode()->getAbsCoor();
530
531  for (int i = 0; i < curBrush->n_brushsides; i++) {
532    brushside& curBrushSide =   this->bspFile->brushSides[curBrush->brushside + i]   ;
533    plane& curPlane  =   this->bspFile->planes[curBrushSide.plane] ;
534
535    startDistance = inputStart.x * curPlane.x + inputStart.y * curPlane.y+ inputStart.z * curPlane.z - curPlane.d;
536    endDistance = inputEnd.x * curPlane.x +inputEnd.y * curPlane.y +inputEnd.z * curPlane.z -curPlane.d;
537
538    if (startDistance > 0)
539      startsOut = true;
540    if (endDistance > 0)
541      endsOut = true;
542
543    // make sure the trace isn't completely on one side of the brush
544    if (startDistance > 0 && endDistance > 0) {   // both are in front of the plane, its outside of this brush
545      return;
546    }
547    if (startDistance <= 0 && endDistance <= 0) {   // both are behind this plane, it will get clipped by another one
548      continue;
549    }
550
551    // MMM... BEEFY
552    if (startDistance > endDistance) {   // line is entering into the brush
553      float fraction = (startDistance - EPSILON) / (startDistance - endDistance);  // *
554      if (fraction > startFraction)
555        startFraction = fraction;
556      // don't store plane
557      // this->collPlane = &curPlane;
558
559    } else {   // line is leaving the brush
560      float fraction = (startDistance + EPSILON) / (startDistance - endDistance);  // *
561      if (fraction < endFraction)
562        endFraction = fraction;
563      // don't store plane
564      //this->collPlane = & curPlane;
565
566    }
567
568  }
569  if (startsOut == false) {
570    this->outputStartsOut = false;
571    if (endsOut == false)
572      this->outputAllSolid = true;
573    return;
574  }
575
576  if (startFraction < endFraction) {
577    if (startFraction > -1.0f && startFraction < outputFraction) {
578      if (startFraction < 0)
579        startFraction = 0;
580      this->outputFraction = startFraction;
581    }
582  }
583
584}
585
586void BspManager::checkBrushRayN(brush* curBrush)
587{
588  float EPSILON = 0.000001;
589  float startDistance;
590  float endDistance;
591
592  float startFraction = -1.0f;
593  float endFraction = 1.0f;
594  bool startsOut = false;
595  bool endsOut = false;
596
597  Vector inputStart = State::getCameraTargetNode()->getLastAbsCoor();
598  Vector inputEnd   = State::getCameraTargetNode()->getAbsCoor();
599
600  for (int i = 0; i < curBrush->n_brushsides; i++) {
601    brushside& curBrushSide =   this->bspFile->brushSides[curBrush->brushside + i]   ;
602    plane& curPlane  =   this->bspFile->planes[curBrushSide.plane] ;
603
604    startDistance = inputStart.x * curPlane.x + inputStart.y * curPlane.y+ inputStart.z * curPlane.z - curPlane.d;
605    endDistance = inputEnd.x * curPlane.x +inputEnd.y * curPlane.y +inputEnd.z * curPlane.z -curPlane.d;
606
607    if (startDistance > 0)
608      startsOut = true;
609    if (endDistance > 0)
610      endsOut = true;
611
612    // make sure the trace isn't completely on one side of the brush
613    if (startDistance > 0 && endDistance > 0) {   // both are in front of the plane, its outside of this brush
614      return;
615    }
616    if (startDistance <= 0 && endDistance <= 0) {   // both are behind this plane, it will get clipped by another one
617      continue;
618    }
619
620    // MMM... BEEFY
621    if (startDistance > endDistance) {   // line is entering into the brush
622      float fraction = (startDistance - EPSILON) / (startDistance - endDistance);  // *
623      if (fraction > startFraction)
624        startFraction = fraction;
625      // store plane
626      this->collPlane = &curPlane;
627
628    } else {   // line is leaving the brush
629      float fraction = (startDistance + EPSILON) / (startDistance - endDistance);  // *
630      if (fraction < endFraction)
631        endFraction = fraction;
632      // store plane
633      this->collPlane = & curPlane;
634
635    }
636
637  }
638  if (startsOut == false) {
639    this->outputStartsOut = false;
640    if (endsOut == false)
641      this->outputAllSolid = true;
642    return;
643  }
644
645  if (startFraction < endFraction) {
646    if (startFraction > -1.0f && startFraction < outputFraction) {
647      if (startFraction < 0)
648        startFraction = 0;
649      this->outputFraction = startFraction;
650    }
651  }
652
653}
654
655
656void BspManager::checkCollisionRay(BspTreeNode* node, float startFraction, float endFraction, Vector* start, Vector* end)
657{
658
659
660  float EPSILON = 0.000001;
661  float  endDistance = (end)->x * (node->plane.x) +(end)->y * (node->plane.y) +(end)->z * (node->plane.z)  - node->d;
662  float  startDistance = (start)->x * (node->plane.x)+ (start)->y * (node->plane.y)+ (start)->z * (node->plane.z)- node->d;
663
664
665  if(node->isLeaf) {
666    leaf& curLeaf = this->bspFile->leaves[node->leafIndex];
667    for (int i = 0; i <  curLeaf.n_leafbrushes ; i++) {
668      brush& curBrush = this->bspFile->brushes[((int*)(this->bspFile->leafBrushes))[curLeaf.leafbrush_first+i]];
669      //object *brush = &BSP.brushes[BSP.leafBrushes[leaf->firstLeafBrush + i]];
670      if (curBrush.n_brushsides > 0   &&
671          ((((BspTexture*)(this->bspFile->textures))[curBrush.texture]).contents & 1))
672        // CheckBrush( brush );
673        this->checkBrushRay(&curBrush);
674      if(curBrush.n_brushsides <=0) this->outputAllSolid = true;
675    }
676    return;
677  }
678
679
680  if (startDistance >= 0 && endDistance >= 0)     // A
681  {   // both points are in front of the plane
682    // so check the front child
683    this->checkCollisionRay(node->left,0,0,start,end);
684  } else if (startDistance < 0 && endDistance < 0)  // B
685  {   // both points are behind the plane
686    // so check the back child
687    this->checkCollisionRay(node->right,0,0,start,end);
688  } else                                            // C
689  {   // the line spans the splitting plane
690    int side;
691    float fraction1, fraction2, middleFraction;
692    Vector middle;
693
694    // STEP 1: split the segment into two
695    if (startDistance < endDistance) {
696      side = 1; // back
697      float inverseDistance = 1.0f / (startDistance - endDistance);
698      fraction1 = (startDistance + EPSILON) * inverseDistance;
699      fraction2 = (startDistance + EPSILON) * inverseDistance;
700    } else if (endDistance < startDistance) {
701      side = 0; // front(start)->x * (node->plane.x)+
702      float inverseDistance = 1.0f / (startDistance - endDistance);
703      fraction1 = (startDistance + EPSILON) * inverseDistance;
704      fraction2 = (startDistance - EPSILON) * inverseDistance;
705    } else {
706      side = 0; // front
707      fraction1 = 1.0f;
708      fraction2 = 0.0f;
709    }
710
711    // STEP 2: make sure the numbers are valid
712    if (fraction1 < 0.0f) fraction1 = 0.0f;
713    else if (fraction1 > 1.0f) fraction1 = 1.0f;
714    if (fraction2 < 0.0f) fraction2 = 0.0f;
715    else if (fraction2 > 1.0f) fraction2 = 1.0f;
716
717    // STEP 3: calculate the middle point for the first side
718    middleFraction = startFraction +
719                     (endFraction - startFraction) * fraction1;
720
721    middle.x = start->x + fraction1 * (end->x - start->x);
722    middle.y = start->y + fraction1 * (end->y - start->y);
723    middle.z = start->z + fraction1 * (end->z - start->z);
724
725    // STEP 4: check the first side
726    //CheckNode( node->children[side], startFraction, middleFraction, start, middle );
727    if(side == 0) this->checkCollisionRay(node->left,startFraction, middleFraction, start, &middle );
728
729    else this->checkCollisionRay(node->right,startFraction, middleFraction,
730                                   start, &middle );
731
732    // STEP 5: calculate the middle point for the second side
733    middleFraction = startFraction +
734                     (endFraction - startFraction) * fraction2;
735
736    middle.x = start->x + fraction2 * (end->x - start->x);
737    middle.y = start->y + fraction2 * (end->y - start->y);
738    middle.z = start->z + fraction2 * (end->z - start->z);
739
740    // STEP 6: check the second side
741    if(side == 1)this->checkCollisionRay(node->left,middleFraction, endFraction, &middle, end);
742
743    else this->checkCollisionRay(node->right,middleFraction, endFraction,&middle, end );
744
745
746  }
747
748}
749
750
751
752void BspManager::checkCollisionRayN(BspTreeNode* node, float startFraction, float endFraction, Vector* start, Vector* end)
753{
754
755
756  float EPSILON = 0.000001;
757
758  float endDistance = end->dot(node->plane) - node->d;
759  float startDistance = start->dot(node->plane) - node->d;
760
761
762  if( node->isLeaf) {
763    leaf& curLeaf = this->bspFile->leaves[node->leafIndex];
764    for (int i = 0; i <  curLeaf.n_leafbrushes ; i++) {
765      brush& curBrush = this->bspFile->brushes[((int*)(this->bspFile->leafBrushes))[curLeaf.leafbrush_first+i]];
766      //object *brush = &BSP.brushes[BSP.leafBrushes[leaf->firstLeafBrush + i]];
767      if (curBrush.n_brushsides > 0   &&
768          ((((BspTexture*)(this->bspFile->textures))[curBrush.texture]).contents & 1))
769        // CheckBrush( brush );
770        this->checkBrushRayN(&curBrush);
771      if(curBrush.n_brushsides <=0) this->outputAllSolid = true;
772    }
773    return;
774  }
775
776
777  if (startDistance >= 0 && endDistance >= 0)     // A
778  {   // both points are in front of the plane
779    // so check the front child
780    this->checkCollisionRayN(node->left,0,0,start,end);
781  } else if (startDistance < 0 && endDistance < 0)  // B
782  {   // both points are behind the plane
783    // so check the back child
784    this->checkCollisionRayN(node->right,0,0,start,end);
785  } else                                            // C
786  {   // the line spans the splitting plane
787    int side;
788    float fraction1, fraction2, middleFraction;
789    Vector middle;
790
791    // STEP 1: split the segment into two
792    if (startDistance < endDistance) {
793      side = 1; // back
794      float inverseDistance = 1.0f / (startDistance - endDistance);
795      fraction1 = (startDistance + EPSILON) * inverseDistance;
796      fraction2 = (startDistance + EPSILON) * inverseDistance;
797    } else if (endDistance < startDistance) {
798      side = 0; // front(start)->x * (node->plane.x)+
799      float inverseDistance = 1.0f / (startDistance - endDistance);
800      fraction1 = (startDistance + EPSILON) * inverseDistance;
801      fraction2 = (startDistance - EPSILON) * inverseDistance;
802    } else {
803      side = 0; // front
804      fraction1 = 1.0f;
805      fraction2 = 0.0f;
806    }
807
808    // STEP 2: make sure the numbers are valid
809    if (fraction1 < 0.0f) fraction1 = 0.0f;
810    else if (fraction1 > 1.0f) fraction1 = 1.0f;
811    if (fraction2 < 0.0f) fraction2 = 0.0f;
812    else if (fraction2 > 1.0f) fraction2 = 1.0f;
813
814    // STEP 3: calculate the middle point for the first side
815    middleFraction = startFraction + (endFraction - startFraction) * fraction1;
816    middle = (*start) + ((*end) - (*start)) * fraction1;
817
818
819    // STEP 4: check the first side
820    //CheckNode( node->children[side], startFraction, middleFraction, start, middle );
821    if(side == 0) this->checkCollisionRayN(node->left,startFraction, middleFraction, start, &middle );
822
823    else this->checkCollisionRayN(node->right,startFraction, middleFraction,
824                                    start, &middle );
825
826    // STEP 5: calculate the middle point for the second side
827    middleFraction = startFraction + (endFraction - startFraction) * fraction2;
828    middle = (*start) + ((*end) - (*start)) * fraction2;
829
830    // STEP 6: check the second side
831    if(side == 1)this->checkCollisionRayN(node->left,middleFraction, endFraction, &middle, end);
832
833    else this->checkCollisionRayN(node->right,middleFraction, endFraction,&middle, end );
834
835
836  }
837
838}
839void BspManager::checkCollisionBox(void)
840{
841
842};
843
844void BspManager::TraceBox( Vector& inputStart, Vector& inputEnd,
845               Vector& inputMins, Vector& inputMaxs )
846{
847  if (inputMins.x == 0 && inputMins.y == 0 && inputMins.z == 0 &&
848      inputMaxs.x == 0 && inputMaxs.y == 0 && inputMaxs.z == 0)
849  {   // the user called TraceBox, but this is actually a ray
850   //!> FIXME TraceRay( inputStart, inputEnd );
851  }
852  else
853  {   // setup for a box
854    //traceType = TT_BOX;
855    this->traceMins = inputMins;
856    this->traceMaxs = inputMaxs;
857    this->traceExtents.x = -traceMins.x > traceMaxs.x ?
858        -traceMins.x : traceMaxs.x;
859    this->traceExtents.y = -traceMins.y > traceMaxs.y ?
860        -traceMins.y : traceMaxs.y;
861    this->traceExtents.z = -traceMins.z > traceMaxs.z ?
862        -traceMins.z : traceMaxs.z;
863   //!> FIXME Trace( inputStart, inputEnd );
864  }
865}
866
867void BspManager::checkCollision(WorldEntity* worldEntity)
868{
869  Vector position = worldEntity->getLastAbsCoor();
870
871
872  Vector forwardDir = worldEntity->getAbsDirX();
873  forwardDir = forwardDir * 8.0f;
874
875  Vector upDir = worldEntity->getAbsDirY();
876  Vector dest = position;
877  /*
878  dest.x  += forwardDir.x;
879  dest.y  += forwardDir.y;
880  dest.z  += forwardDir.z;
881  */
882
883  dest = worldEntity->getAbsCoor();
884  Vector out = dest;
885
886
887  if(/*!worldEntity->isA(CL_PLAYABLE)*/ false) {
888
889
890
891  }
892  else {
893
894    bool collision = false;
895    Vector position1 = position + Vector(0.0,0.1,0.0);
896    Vector position2 = position + Vector(0.0,0.2,0.0);
897    Vector dest1 = position1 + forwardDir;
898    Vector dest2 = position2 + forwardDir;
899    dest = position - Vector(0.0, 40.0,0.0);
900    Vector out1;
901    Vector out2;
902
903
904    float height = 40;
905    this->outputAllSolid = false;
906
907    this->checkCollisionRayN(this->root,0.0f,1.0f, &position, &dest );
908
909    if( this->outputFraction == 1.0f)
910    {
911      if(this->outputAllSolid)
912      {
913        this->collPlane = new plane;
914        this->collPlane->x = 1.0f;
915        this->collPlane->y = 0.0f;
916        this->collPlane->z = 0.0f;
917        collision = true;
918      }
919      else
920        collision = false;
921
922
923      out = dest;
924    }
925    else {
926
927      collision = true;
928      out.x = position.x + (dest.x -position.x) * this->outputFraction;
929      out.y = position.y + (dest.y -position.y) * this->outputFraction;
930      out.z = position.z + (dest.z -position.z) * this->outputFraction;
931
932      Vector out3 = out + Vector(height*this->collPlane->x,height*this->collPlane->y,height*this->collPlane->z);
933      this->out = out;
934      //this->out1 = out1;
935      //this->out2 = out2;
936      //this->drawDebugCube(&out1);
937      //this->drawDebugCube(&out2);
938      //this->drawDebugCube(&out3);
939    }
940    // Return the normal here: Normal's stored in this->collPlane;
941    if(collision ) {
942      PRINTF(0)("We got a collision!! Are you sure: outputFraction = %f\n", this->outputFraction);
943      worldEntity->registerCollision(this->parent, worldEntity, Vector(this->collPlane->x, this->collPlane->y, this->collPlane->z), out);
944    }
945  }
946
947}
948
949
950void  BspManager::checkCollision(BspTreeNode* node, Vector* cam)
951{
952  Vector next = this->cam;
953  next.x =   (State::getCameraTargetNode()->getLastAbsCoor()).x ;
954  next.y =   (State::getCameraTargetNode()->getLastAbsCoor()).y ;
955  next.z =   (State::getCameraTargetNode()->getLastAbsCoor()).z ;
956
957  float dist = 0;
958  if(!(node->isLeaf)) {
959    dist = (node->plane.x * this->cam.x + node->plane.y*this->cam.y + node->plane.z*this->cam.z) - node->d;
960    if(dist > 4.0f) {
961      checkCollision(node->left,cam);
962      return;
963    }
964    if(dist < -4.0f) {
965      checkCollision(node->right,cam);
966      return;
967    }
968    if(dist<=4.0f && dist >= -4.0f) {
969      checkCollision(node->left,cam);
970      checkCollision(node->right,cam);
971      return;
972    }
973    return;
974  } else {
975
976    leaf& camLeaf =  ((leaf *)(this->bspFile->leaves))[(node->leafIndex ) ];
977
978    if (camLeaf.cluster < 0) {
979      this->drawDebugCube(&this->cam);
980      this->drawDebugCube(&next);
981     // State::getPlayer()->getPlayable()->setRelCoor(-100,-100,-100);
982      //State::getPlayer()->getPlayable()->collidesWith(NULL, State::getCameraTargetNode()->getLastAbsCoor());
983    }
984
985
986    /*
987        for(int i = 0; i < camLeaf.n_leafbrushes && i < 10; i++ )
988        {
989                brush& curBrush = ((brush*)(this->bspFile->brushes))[(camLeaf.leafbrush_first +i)%this->bspFile->numLeafBrushes];
990                if(curBrush.n_brushsides < 0) return;
991                for(int j = 0; j < curBrush.n_brushsides; j++)
992                {
993                float dist = -0.1;
994                brushside& curBrushSide = ((brushside*)(this->bspFile->brushSides))[(curBrush.brushside +j)%this->bspFile->numBrushSides];
995                plane&      testPlane = ((plane*)(this->bspFile->planes))[curBrushSide.plane % this->bspFile->numPlanes];
996                dist = testPlane.x * this->cam.x +  testPlane.y * this->cam.y  +  testPlane.z * this->cam.z   -testPlane.d ;
997
998                if(dist < -0.01f) dist = -1.0f *dist;
999                if(dist < 1.0f){
1000                                this->drawDebugCube(&this->cam);
1001                                return;
1002                              }
1003                }
1004
1005        } */
1006
1007  }
1008  return;
1009}
1010
1011void BspManager::drawDebugCube(Vector* cam)
1012{
1013  glBegin(GL_QUADS);
1014
1015  // Bottom Face.  Red, 75% opaque, magnified texture
1016
1017  glNormal3f( 0.0f, -1.0f, 0.0f); // Needed for lighting
1018  glColor4f(0.9,0.2,0.2,.75); // Basic polygon color
1019
1020  glTexCoord2f(0.800f, 0.800f); glVertex3f(cam->x-1.0f, cam->y-1.0f,cam->z -1.0f);
1021  glTexCoord2f(0.200f, 0.800f); glVertex3f(cam->x+1.0f, cam->y-1.0f,cam->z -1.0f);
1022  glTexCoord2f(0.200f, 0.200f); glVertex3f(cam->x+ 1.0f,cam->y -1.0f,cam->z +  1.0f);
1023  glTexCoord2f(0.800f, 0.200f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z + 1.0f);
1024
1025
1026  // Top face; offset.  White, 50% opaque.
1027
1028  glNormal3f( 0.0f, 1.0f, 0.0f);  glColor4f(0.5,0.5,0.5,.5);
1029
1030  glTexCoord2f(0.005f, 1.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.0f);
1031  glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f,  cam->z +1.0f);
1032  glTexCoord2f(1.995f, 0.005f); glVertex3f(cam->x+ 1.0f,  cam->y+1.0f,  cam->z +1.0f);
1033  glTexCoord2f(1.995f, 1.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z -1.0f);
1034
1035
1036  // Far face.  Green, 50% opaque, non-uniform texture cooridinates.
1037
1038  glNormal3f( 0.0f, 0.0f,-1.0f);  glColor4f(0.2,0.9,0.2,.5);
1039
1040  glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z -1.3f);
1041  glTexCoord2f(2.995f, 2.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.3f);
1042  glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x+ 1.0f,cam->y+  1.0f, cam->z -1.3f);
1043  glTexCoord2f(0.005f, 0.005f); glVertex3f( cam->x+1.0f,cam->y -1.0f, cam->z -1.3f);
1044
1045
1046  // Right face.  Blue; 25% opaque
1047
1048  glNormal3f( 1.0f, 0.0f, 0.0f);  glColor4f(0.2,0.2,0.9,.25);
1049
1050  glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y -1.0f, cam->z -1.0f);
1051  glTexCoord2f(0.995f, 0.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z -1.0f);
1052  glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z + 1.0f);
1053  glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y-1.0f,  cam->z +1.0f);
1054
1055
1056  // Front face; offset.  Multi-colored, 50% opaque.
1057
1058  glNormal3f( 0.0f, 0.0f, 1.0f);
1059
1060  glColor4f( 0.9f, 0.2f, 0.2f, 0.5f);
1061  glTexCoord2f( 0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f,  cam->z +1.0f);
1062  glColor4f( 0.2f, 0.9f, 0.2f, 0.5f);
1063  glTexCoord2f( 0.995f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y-1.0f,  cam->z +1.0f);
1064  glColor4f( 0.2f, 0.2f, 0.9f, 0.5f);
1065  glTexCoord2f( 0.995f, 0.995f); glVertex3f( cam->x+1.0f,  cam->y+1.0f,  cam->z +1.0f);
1066  glColor4f( 0.1f, 0.1f, 0.1f, 0.5f);
1067  glTexCoord2f( 0.005f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f,  cam->z +1.0f);
1068
1069
1070  // Left Face; offset.  Yellow, varying levels of opaque.
1071
1072  glNormal3f(-1.0f, 0.0f, 0.0f);
1073
1074  glColor4f(0.9,0.9,0.2,0.0);
1075  glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z -1.0f);
1076  glColor4f(0.9,0.9,0.2,0.66);
1077  glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x-1.0f,cam->y -1.0f,  cam->z +1.0f);
1078  glColor4f(0.9,0.9,0.2,1.0);
1079  glTexCoord2f(0.995f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f,  cam->z +1.0f);
1080  glColor4f(0.9,0.9,0.2,0.33);
1081  glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.0f);
1082
1083  glEnd();
1084}
1085
1086void BspManager::addFace(int f)
1087{
1088  face& curFace =  ((face *)(this->bspFile->faces))[f];
1089  if(this->bspFile->Materials[curFace.texture].alpha) this->trasparent.push_back(f);
1090  else this->opal.push_back(f);
1091}
Note: See TracBrowser for help on using the repository browser.