Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Improved again.

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