Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Added source files to branch bsp_model.

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