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
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
15
16#include "vector.h"
17#include "bsp_file.h"
18#include "bsp_manager.h"
19#include "bsp_tree_leaf.h"
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"
26#include "world_entities/player.h"
27#include "world_entities/playable.h"
28#include "util/loading/resource_manager.h"
29// STL Containers
30#include <vector>
31#include <deque>
32#include "movie_player.h"
33
34
35
36
37BspManager::BspManager()
38{
39  // open a BSP file
40  this->bspFile = new BspFile();
41  this->bspFile->read(ResourceManager::getFullName("test.bsp").c_str());
42  this->bspFile->build_tree();
43  this->root  = this->bspFile->get_root();
44  this->alreadyVisible = new bool [this->bspFile->numFaces];
45}
46
47void BspManager::draw()
48{
49
50
51
52
53
54  // Draw Debug Terrain
55  /*
56  this->bspFile->Materials[0]->select();
57  for(int i = 0; i <  this->bspFile->numPatches ; i++)
58        {
59                this->bspFile->VertexArrayModels[i]->draw();
60   
61        }
62  */
63
64
65
66  // erase alreadyVisible
67  for(int i = 0; i < this->bspFile->numFaces; i++) this->alreadyVisible[i] = false;
68  float tmp = 0;
69  //this->opal.clear();
70  //this->trasparent.clear();
71  // Find all visible faces...
72
73  this->cam = State::getCamera()->getAbsCoor() ;
74  //this->ship = State::getCameraTargetNode()->getAbsCoor();
75
76
77
78
79 
80 // this->viewDir=    State::getCameraTarget()->getAbsCoor() -  State::getCamera()->getAbsCoor() ;
81  float d = (cam.x*viewDir.x + cam.y*viewDir.y + cam.z * viewDir.z);
82
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)
86
87
88
89  this->cam = State::getCameraTargetNode()->getAbsCoor();
90 // this->checkCollision(this->root, &this->cam);   //!< Test Collision Detection
91  this->outputStartsOut = true;
92  this->outputAllSolid = false;
93  this->outputFraction = 1.0f;
94 
95  this->checkCollisionRay(this->root,0.0f,1.0f, &(State::getCameraTargetNode()->getLastAbsCoor()), &this->cam);
96 
97  if(this->outputFraction != 1.0f || this->outputAllSolid ) this->drawDebugCube(&this->cam);
98
99  if ( viscluster < 0  || ((int *)(this->bspFile->header))[35] == 0 )  //!< if (sizeof(Visdata) == 0)
100  {
101   
102   
103   
104    // Iterate through all Leafspublic final double readLEDouble()
105    for(int i = 0; i <  this->bspFile->numLeafs   ; i++ )
106    {
107      // cluster =  (this->bspFile->leaves)[i].cluster;
108      leaf& curLeaf = (this->bspFile->leaves)[i];
109      if(curLeaf.cluster<0) continue;
110     
111     
112      /** Do Frustum culling and draw 'em all **/
113      bool inFrustum = true;
114
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;
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 ;
123     
124      if(dMins < -30.0 && dMaxs < -30.0)
125      {   
126       //continue;
127      }
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      }
132   
133       
134      // Iterate through all faces
135      for (int j = 0; j < curLeaf.n_leaffaces ; ++j) {
136        const int g = (j +  curLeaf.leafface);
137        const int f = ((int *)this->bspFile->leafFaces)[g];
138        if (f >=0 && !this->isAlreadyVisible(f)) {
139          this->alreadyVisible[f] = true;
140               
141          addFace(f); // "visibleFaces.append(f)"
142        }
143      }
144
145
146
147
148    } //for
149  } 
150  else {
151
152 
153    unsigned int v;
154    unsigned char  visSet;
155
156    // Iterate through all Leafs
157
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] ;
161      int& cluster =  curLeaf.cluster;
162
163      if(cluster < 0) continue;
164      v = ((viscluster *  ( ((int *)this->bspFile->visData)[1]) ) + (cluster / 8));
165      visSet =((char*) (this->bspFile->visData))[v + 8];
166
167      // gets bit of visSet
168      if( ((visSet) & (1 << (cluster &  7))) != 0 ) {
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     
181        if(dMins < -150.0 && dMaxs < -150.0)
182        {   
183        continue; 
184        }
185       
186       
187        // Iterate through all faces
188        for (int j = 0; j < curLeaf.n_leaffaces ; ++j) {
189          const int g = (j +  curLeaf.leafface);
190          const int f = ((int *)this->bspFile->leafFaces)[g];
191
192          if (!this->isAlreadyVisible(f) && f>=0) {
193            this->addFace(f);
194            this->alreadyVisible[f] = true;
195          }
196
197        }
198
199      }// if
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  }
213  glActiveTextureARB(GL_TEXTURE1_ARB);
214  glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap);
215  glEnable(GL_TEXTURE_2D);
216
217}//draw
218
219
220
221void BspManager::draw_face(int curface)
222{
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;
227  if (curFace.effect != -1) return;
228  // PRINTF(0)("BSP Manager: ");
229  // PRINTF(0)("BSP Manager: type: %i  \n", curFace.texture);
230
231  //  if(  curFace.texture < 0 ) return;
232  if(curFace.type == 2) {
233    this->draw_patch( &curFace);
234    return;
235  }
236  if(curFace.type != 1) return;
237  if((char*)(this->bspFile->textures)[curFace.texture*72]== 0) return;
238
239  if(this->lastTex != curFace.texture) {
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    {
253    this->bspFile->Materials[curFace.texture].mat->select();
254    this->lastTex = curFace.texture;
255    }
256  }
257
258  if(curFace.lm_index < 0) {
259    glActiveTextureARB(GL_TEXTURE1_ARB);
260    glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap );
261    glEnable(GL_TEXTURE_2D);
262  } else {
263    glActiveTextureARB(GL_TEXTURE1_ARB);
264    glBindTexture(GL_TEXTURE_2D, this->bspFile->glLightMapTextures[curFace.lm_index]);
265    glEnable(GL_TEXTURE_2D);
266  }
267
268  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
269 
270 // glColor4f(3.0,3.0,3.0,1.0);
271  glEnableClientState(GL_VERTEX_ARRAY );
272  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
273  glEnableClientState(GL_NORMAL_ARRAY );
274  //  glEnableClientState(GL_COLOR_ARRAY);
275
276
277  glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0]));
278
279  glClientActiveTextureARB(GL_TEXTURE0_ARB);
280  glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0]));
281  //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
282
283  glClientActiveTextureARB(GL_TEXTURE1_ARB);
284  glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1]));
285  //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
286
287
288  glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0]));
289  // glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0]));
290  glDrawElements(GL_TRIANGLES, curFace.n_meshverts,
291                 GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert]));
292
293  glDisableClientState(GL_TEXTURE0_ARB);
294  glDisableClientState(GL_TEXTURE1_ARB);
295  glDisableClientState(GL_VERTEX_ARRAY );
296  glDisableClientState(GL_TEXTURE_COORD_ARRAY );
297  glDisableClientState(GL_NORMAL_ARRAY );
298  // glDisableClientState(GL_COLOR_ARRAY);
299
300}
301
302
303void BspManager::draw_debug_face(int curface)
304{
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
310  // PRINTF(0)("BSP Manager: ");
311  // PRINTF(0)("BSP Manager: type: %i  \n", curFace.texture);
312
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)
320
321  this->bspFile->Materials[2].mat->select();
322  this->lastTex = 2;
323
324  glEnableClientState(GL_VERTEX_ARRAY );
325  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
326  glEnableClientState(GL_NORMAL_ARRAY );
327  //glEnableClientState(GL_COLOR_ARRAY);
328  // glEnableClientState(GL_VERTEX_ARRAY );
329  glClientActiveTextureARB(GL_TEXTURE0_ARB);
330  glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0]));
331  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
332  // glClientActiveTextureARB(GL_TEXTURE0_ARB);
333  glClientActiveTextureARB(GL_TEXTURE1_ARB);
334  glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0]));
335  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
336  // glClientActiveTextureARB(GL_TEXTURE1_ARB);
337  // glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1]));
338  //glEnableClientState(GL_NORMAL_ARRAY );
339
340  glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0]));
341  //  glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0]));
342  glDrawElements(GL_TRIANGLES, curFace.n_meshverts,
343                 GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert]));
344
345}
346
347void BspManager::draw_patch(face* Face)
348{
349  if(this->lastTex != Face->texture) {
350    this->bspFile->Materials[Face->texture].mat->select();
351    this->lastTex = Face->texture;
352  }
353  if (Face->effect != -1) return;
354
355
356  if(Face->lm_index < 0) {
357    glActiveTextureARB(GL_TEXTURE1_ARB);
358    glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap);
359    glEnable(GL_TEXTURE_2D);
360  } else {
361    glActiveTextureARB(GL_TEXTURE1_ARB);
362    glBindTexture(GL_TEXTURE_2D, this->bspFile->glLightMapTextures[Face->lm_index]);
363    glEnable(GL_TEXTURE_2D);
364  }
365  //glColor4f(3.0,3.0,3.0,1.0);
366
367  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
368  glEnable( GL_AUTO_NORMAL);
369  glEnableClientState(GL_VERTEX_ARRAY );
370  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
371  for(int i = Face->n_meshverts -1; i >=0   ; i--) {
372   //glFrontFace(GL_CW);
373    //PRINTF(0)("BSP Manager: Face->size[0]: %i . \n", Face->size[0]);
374
375
376    //glEnableClientState(GL_NORMAL_ARRAY );
377
378    glVertexPointer(3, GL_FLOAT,44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).position[0]));
379
380
381    glClientActiveTextureARB(GL_TEXTURE0_ARB);
382    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
383    glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[0][0]));
384 
385
386
387    glClientActiveTextureARB(GL_TEXTURE1_ARB);
388    glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[1][0]));
389    //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
390
391
392  //  glNormalPointer( GL_FLOAT, 44,&((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).normal[0]) );
393
394
395
396
397    for(int row=6; row>=0; --row) {
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    }
401
402    //glFrontFace(GL_CCW);
403  }
404  glDisableClientState(GL_TEXTURE0_ARB);
405  glDisableClientState(GL_TEXTURE1_ARB);
406  glDisable(GL_AUTO_NORMAL);
407  glDisableClientState(GL_VERTEX_ARRAY );
408  glDisableClientState(GL_TEXTURE_COORD_ARRAY );
409
410
411}
412
413bool BspManager::isAlreadyVisible(int Face)
414{
415  return this->alreadyVisible[Face];
416}
417
418
419BspTreeNode*  BspManager::getLeaf(BspTreeNode* node, Vector* cam)
420{
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;
431}
432
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}
598void  BspManager::checkCollision(BspTreeNode* node, Vector* cam)
599{
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
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 {
623
624    leaf& camLeaf =  ((leaf *)(this->bspFile->leaves))[(node->leafIndex ) ];
625
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                              }
632
633
634    /*
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        } */
654
655  }
656  return;
657}
658
659void BspManager::drawDebugCube(Vector* cam)
660{
661  glBegin(GL_QUADS);
662
663  // Bottom Face.  Red, 75% opaque, magnified texture
664
665  glNormal3f( 0.0f, -1.0f, 0.0f); // Needed for lighting
666  glColor4f(0.9,0.2,0.2,.75); // Basic polygon color
667
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);
672
673
674  // Top face; offset.  White, 50% opaque.
675
676  glNormal3f( 0.0f, 1.0f, 0.0f);  glColor4f(0.5,0.5,0.5,.5);
677
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);
682
683
684  // Far face.  Green, 50% opaque, non-uniform texture cooridinates.
685
686  glNormal3f( 0.0f, 0.0f,-1.0f);  glColor4f(0.2,0.9,0.2,.5);
687
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);
692
693
694  // Right face.  Blue; 25% opaque
695
696  glNormal3f( 1.0f, 0.0f, 0.0f);  glColor4f(0.2,0.2,0.9,.25);
697
698  glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y -1.0f, cam->z -1.0f);
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);
702
703
704  // Front face; offset.  Multi-colored, 50% opaque.
705
706  glNormal3f( 0.0f, 0.0f, 1.0f);
707
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();
732}
733
734void BspManager::addFace(int f)
735{
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.