Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

further reformating

File size: 13.8 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
27// STL Containers
28#include <vector>
29#include <deque>
30
31
32
33
34BspManager::BspManager()
35{
36  // open a BSP file
37  this->bspFile = new BspFile();
38  this->bspFile->read("/root/data/Kanti175.bsp");
39  this->bspFile->build_tree();
40  this->root  = this->bspFile->get_root();
41  this->alreadyVisible = new bool [this->bspFile->numFaces];
42}
43
44void BspManager::draw()
45{
46
47
48
49
50
51  // Draw Debug Terrain
52  /*
53  this->bspFile->Materials[0]->select();
54  for(int i = 0; i <  this->bspFile->numPatches ; i++)
55        {
56                this->bspFile->VertexArrayModels[i]->draw();
57   
58        }
59  */
60
61
62
63  // erase alreadyVisible
64  for(int i = 0; i < this->bspFile->numFaces; i++) this->alreadyVisible[i] = false;
65  float tmp = 0;
66  this->opal.clear();
67  this->trasparent.clear();
68  // Find all visible faces...
69
70  this->cam = State::getCamera()->getAbsCoor() ;
71  this->ship = State::getCameraTargetNode()->getAbsCoor();
72
73
74
75
76  this->cam = State::getCameraTargetNode()->getAbsCoor();
77  this->viewDir=    State::getCameraTarget()->getAbsCoor() -  State::getCamera()->getAbsCoor() ;
78  float d = (cam.x*viewDir.x + cam.y*viewDir.y + cam.z * viewDir.z);
79
80  BspTreeNode*  ActLeaf = this->getLeaf(this->bspFile->root, &ship);
81  int viscluster = -1;
82  viscluster =((leaf*)(this->bspFile->leaves))[ ActLeaf->leafIndex].cluster; // get the players cluster (viscluster)
83
84
85
86
87  this->checkCollision(this->root, &this->cam);   //!< Test Collision Detection
88
89  if ((((int *)(this->bspFile->header))[35] == 0)  || viscluster < 0)  //!< if (sizeof(Visdata) == 0)
90  {
91    /** Do Frustum culling and draw 'em all **/
92
93    // Iterate through all Leafs
94    for(int i = 0; i <  this->bspFile->numLeafs   ; i++ )
95    {
96      // cluster =  (this->bspFile->leaves)[i].cluster;
97      leaf& curLeaf = (this->bspFile->leaves)[i];
98
99
100      // Iterate through all faces
101      for (int j = 0; j < curLeaf.n_leaffaces ; ++j) {
102        const int f = ( j +  ((leaf *) (this->bspFile->leaves))[i].leafface) % this->bspFile->numFaces;
103        if (f >=0 && !this->isAlreadyVisible(f)) {
104          this->alreadyVisible[f] = true;
105          /*    if(ActLeaf->leafIndex == i)
106                {
107                        this->alreadyVisible[i] = false;
108                        this->draw_debug_face(f);
109                        this->alreadyVisible[i] = true;
110                } 
111                else */
112          addFace(f); // "visibleFaces.append(f)"
113        }
114      }
115
116
117
118
119    } //for
120  } else {
121    int cluster;
122    int seven = 7;
123    unsigned char  one = 1;
124    unsigned int v;
125    unsigned char  visSet;
126
127    // Iterate through all Leafs
128
129    for(int i = 0; i <  this->bspFile->numLeafs   ; ++i ) {
130      leaf& camLeaf =  (this->bspFile->leaves)[ActLeaf->leafIndex] ;
131      leaf& curLeaf =  (this->bspFile->leaves)[i] ;
132      cluster =  curLeaf.cluster;
133     
134      if(cluster <0) continue;
135      v = ((viscluster *  ( ((int *)this->bspFile->visData)[1]) ) + (cluster / 8));
136      visSet =((unsigned char*) (this->bspFile->visData))[v+8];
137   
138     
139      if( ((visSet) & ((unsigned char)1 <<(unsigned char) (cluster &  7))) == 0 )
140      {
141        // Iterate through all faces
142        for (int j = 0; j < curLeaf.n_leaffaces ; ++j)
143        {
144          const int f = (j +  curLeaf.leafface) % this->bspFile->numFaces;
145
146
147          if (!this->isAlreadyVisible(f) && f>=0) {
148            this->addFace(f);
149            this->alreadyVisible[f] = true;
150          }
151
152        }
153
154    }// if
155
156    }//for
157
158  }//else
159
160  while(!this->opal.empty()) {
161    this->draw_face(this->opal.front());
162    this->opal.pop_front();
163  }
164  while(!this->trasparent.empty()) {
165    this->draw_face(this->trasparent.back());
166    this->trasparent.pop_back();
167  }
168
169
170}//draw
171
172void BspManager::draw_leaf()
173{}
174
175void BspManager::draw_face(int curface)
176{
177  face& curFace =  (this->bspFile->faces)[curface];
178  const BspVertex* curVertex = (BspVertex *) this->bspFile->vertice;
179  int stride = sizeof(BspVertex);  // sizeof(Vertex)
180  int offset    = curFace.vertex;
181
182  // PRINTF(0)("BSP Manager: ");
183  // PRINTF(0)("BSP Manager: type: %i  \n", curFace.texture);
184
185  //  if(  curFace.texture < 0 ) return;
186  if(curFace.type == 2) {
187    this->draw_patch( &curFace);
188    return;
189  }
190  if(curFace.type == 3) return;
191  // if(this->bspFile->Materials[curFace.texture] != NULL)
192  if(this->lastTex != curFace.texture) {
193    this->bspFile->Materials[curFace.texture].mat->select();
194    this->lastTex = curFace.texture;
195  }
196  glEnableClientState(GL_VERTEX_ARRAY );
197  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
198  glEnableClientState(GL_NORMAL_ARRAY );
199  //  glEnableClientState(GL_COLOR_ARRAY);
200 
201
202  glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0]));
203  // glClientActiveTextureARB(GL_TEXTURE0_ARB);
204  glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0]));
205  // glClientActiveTextureARB(GL_TEXTURE1_ARB);
206  // glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1]));
207
208
209  glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0]));
210  // glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0]));
211  glDrawElements(GL_TRIANGLES, curFace.n_meshverts,
212                 GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert]));
213
214
215  glDisableClientState(GL_VERTEX_ARRAY );
216  glDisableClientState(GL_TEXTURE_COORD_ARRAY );
217  glDisableClientState(GL_NORMAL_ARRAY );
218  // glDisableClientState(GL_COLOR_ARRAY);
219
220}
221
222
223void BspManager::draw_debug_face(int curface)
224{
225  face& curFace =  (this->bspFile->faces)[curface];
226  const BspVertex* curVertex = (BspVertex *) this->bspFile->vertice;
227  int stride = 44;  // sizeof(Vertex)
228  int offset    = curFace.vertex;
229
230  // PRINTF(0)("BSP Manager: ");
231  // PRINTF(0)("BSP Manager: type: %i  \n", curFace.texture);
232
233  //  if(  curFace.texture < 0 ) return;
234  if(curFace.type == 2) {
235    this->draw_patch( &curFace);
236    return;
237  }
238  if(curFace.type == 3) return;
239  // if(this->bspFile->Materials[curFace.texture] != NULL)
240
241  this->bspFile->Materials[2].mat->select();
242  this->lastTex = 2;
243
244  glEnableClientState(GL_VERTEX_ARRAY );
245  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
246  glEnableClientState(GL_NORMAL_ARRAY );
247  //glEnableClientState(GL_COLOR_ARRAY);
248  // glEnableClientState(GL_VERTEX_ARRAY );
249
250  glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0]));
251  // glClientActiveTextureARB(GL_TEXTURE0_ARB);
252  glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0]));
253  // glClientActiveTextureARB(GL_TEXTURE1_ARB);
254  // glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1]));
255  //glEnableClientState(GL_NORMAL_ARRAY );
256
257  glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0]));
258  //  glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0]));
259  glDrawElements(GL_TRIANGLES, curFace.n_meshverts,
260                 GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert]));
261
262}
263
264void BspManager::draw_patch(face* Face)
265{
266  if(this->lastTex != Face->texture) {
267    this->bspFile->Materials[Face->texture].mat->select();
268    this->lastTex = Face->texture;
269  }
270
271  for(int i = 0; i < Face->n_meshverts  ; i++) {
272    //glFrontFace(GL_CW);
273    //PRINTF(0)("BSP Manager: Face->size[0]: %i . \n", Face->size[0]);
274    glEnableClientState(GL_VERTEX_ARRAY );
275    glEnableClientState(GL_TEXTURE_COORD_ARRAY );
276    glEnableClientState(GL_NORMAL_ARRAY );
277
278    glVertexPointer(3, GL_FLOAT,44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).position[0]));
279    glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[0][0]));
280    glNormalPointer( GL_FLOAT, 44,&((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).normal[0]) );
281
282    // We'll need multitexture suport for lightning
283    //glClientActiveTextureARB(GL_TEXTURE0_ARB);
284    //glTexCoordPointer(2, GL_FLOAT,0, &vertex[0].textureCoord);
285    //glClientActiveTextureARB(GL_TEXTURE1_ARB);
286    //glTexCoordPointer(2, GL_FLOAT, sizeof(BSPVertex), &vertex[0].lightmapCoord);
287
288
289    for(int row=0; row<7; ++row) {
290      glDrawElements(GL_TRIANGLE_STRIP, 2*(8), GL_UNSIGNED_INT,
291                     & (     (((GLuint*)  (this->bspFile->patchIndexes))[7*8*2*(Face->meshvert+i)+ row*2*8]  ))  );
292    }
293
294    //glFrontFace(GL_CCW);
295  }
296
297}
298
299bool BspManager::isAlreadyVisible(int Face)
300{
301  return this->alreadyVisible[Face];
302}
303
304
305BspTreeNode*  BspManager::getLeaf(BspTreeNode* node, Vector* cam)
306{
307  float dist = 0;
308  while(!(node->isLeaf)) {
309    dist = (node->plane.x * this->cam.x + node->plane.y*this->cam.y + node->plane.z*this->cam.z) - node->d;
310    if(dist >= 0.0f) {
311      node = node->left;
312    } else {
313      node = node->right;
314
315    }
316  }
317  return  node;
318}
319
320void  BspManager::checkCollision(BspTreeNode* node, Vector* cam)
321{
322  float dist = 0;
323  if(!(node->isLeaf)) {
324    dist = (node->plane.x * this->cam.x + node->plane.y*this->cam.y + node->plane.z*this->cam.z) - node->d;
325    if(dist > 4.0f) {
326      checkCollision(node->left,cam);
327      return;
328    }
329    if(dist < -4.0f) {
330      checkCollision(node->right,cam);
331      return;
332    }
333    if(dist<=4.0f && dist >= -4.0f) {
334      checkCollision(node->left,cam);
335      checkCollision(node->right,cam);
336      return;
337    }
338    return;
339  } else {
340
341    leaf& camLeaf =  ((leaf *)(this->bspFile->leaves))[(node->leafIndex ) ];
342
343    if (camLeaf.cluster < 0) {this->drawDebugCube(&this->cam);}
344
345
346    /*
347        for(int i = 0; i < camLeaf.n_leafbrushes && i < 10; i++ )
348        {
349                brush& curBrush = ((brush*)(this->bspFile->brushes))[(camLeaf.leafbrush_first +i)%this->bspFile->numLeafBrushes];
350                if(curBrush.n_brushsides < 0) return;
351                for(int j = 0; j < curBrush.n_brushsides; j++)
352                {
353                float dist = -0.1;
354                brushside& curBrushSide = ((brushside*)(this->bspFile->brushSides))[(curBrush.brushside +j)%this->bspFile->numBrushSides];
355                plane&      testPlane = ((plane*)(this->bspFile->planes))[curBrushSide.plane % this->bspFile->numPlanes];
356                dist = testPlane.x * this->cam.x +  testPlane.y * this->cam.y  +  testPlane.z * this->cam.z   -testPlane.d ;
357               
358                if(dist < -0.01f) dist = -1.0f *dist;
359                if(dist < 1.0f){
360                                this->drawDebugCube(&this->cam);
361                                return;
362                              }
363                }
364                       
365        } */
366
367  }
368  return;
369}
370
371void BspManager::drawDebugCube(Vector* cam)
372{
373  glBegin(GL_QUADS);
374
375  // Bottom Face.  Red, 75% opaque, magnified texture
376
377  glNormal3f( 0.0f, -1.0f, 0.0f); // Needed for lighting
378  glColor4f(0.9,0.2,0.2,.75); // Basic polygon color
379
380  glTexCoord2f(0.800f, 0.800f); glVertex3f(cam->x-1.0f, cam->y-1.0f,cam->z -1.0f);
381  glTexCoord2f(0.200f, 0.800f); glVertex3f(cam->x+1.0f, cam->y-1.0f,cam->z -1.0f);
382  glTexCoord2f(0.200f, 0.200f); glVertex3f(cam->x+ 1.0f,cam->y -1.0f,cam->z +  1.0f);
383  glTexCoord2f(0.800f, 0.200f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z + 1.0f);
384
385
386  // Top face; offset.  White, 50% opaque.
387
388  glNormal3f( 0.0f, 1.0f, 0.0f);  glColor4f(0.5,0.5,0.5,.5);
389
390  glTexCoord2f(0.005f, 1.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.0f);
391  glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f,  cam->z +1.0f);
392  glTexCoord2f(1.995f, 0.005f); glVertex3f(cam->x+ 1.0f,  cam->y+1.0f,  cam->z +1.0f);
393  glTexCoord2f(1.995f, 1.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z -1.0f);
394
395
396  // Far face.  Green, 50% opaque, non-uniform texture cooridinates.
397
398  glNormal3f( 0.0f, 0.0f,-1.0f);  glColor4f(0.2,0.9,0.2,.5);
399
400  glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z -1.3f);
401  glTexCoord2f(2.995f, 2.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.3f);
402  glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x+ 1.0f,cam->y+  1.0f, cam->z -1.3f);
403  glTexCoord2f(0.005f, 0.005f); glVertex3f( cam->x+1.0f,cam->y -1.0f, cam->z -1.3f);
404
405
406  // Right face.  Blue; 25% opaque
407
408  glNormal3f( 1.0f, 0.0f, 0.0f);  glColor4f(0.2,0.2,0.9,.25);
409
410  glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x+ 1.0f,cam->y -1.0f, cam->z -1.0f);
411  glTexCoord2f(0.995f, 0.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z -1.0f);
412  glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z + 1.0f);
413  glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y-1.0f,  cam->z +1.0f);
414
415
416  // Front face; offset.  Multi-colored, 50% opaque.
417
418  glNormal3f( 0.0f, 0.0f, 1.0f);
419
420  glColor4f( 0.9f, 0.2f, 0.2f, 0.5f);
421  glTexCoord2f( 0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f,  cam->z +1.0f);
422  glColor4f( 0.2f, 0.9f, 0.2f, 0.5f);
423  glTexCoord2f( 0.995f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y-1.0f,  cam->z +1.0f);
424  glColor4f( 0.2f, 0.2f, 0.9f, 0.5f);
425  glTexCoord2f( 0.995f, 0.995f); glVertex3f( cam->x+1.0f,  cam->y+1.0f,  cam->z +1.0f);
426  glColor4f( 0.1f, 0.1f, 0.1f, 0.5f);
427  glTexCoord2f( 0.005f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f,  cam->z +1.0f);
428
429
430  // Left Face; offset.  Yellow, varying levels of opaque.
431
432  glNormal3f(-1.0f, 0.0f, 0.0f);
433
434  glColor4f(0.9,0.9,0.2,0.0);
435  glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z -1.0f);
436  glColor4f(0.9,0.9,0.2,0.66);
437  glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x-1.0f,cam->y -1.0f,  cam->z +1.0f);
438  glColor4f(0.9,0.9,0.2,1.0);
439  glTexCoord2f(0.995f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f,  cam->z +1.0f);
440  glColor4f(0.9,0.9,0.2,0.33);
441  glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.0f);
442
443  glEnd();
444}
445
446void BspManager::addFace(int f)
447{
448  face& curFace =  ((face *)(this->bspFile->faces))[f];
449  if(this->bspFile->Materials[curFace.texture].alpha) this->trasparent.push_back(f);
450  else this->opal.push_back(f);
451}
Note: See TracBrowser for help on using the repository browser.