Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Minor improvements (transparancy). Code formatting revised.

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