| 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 |    Inspired by: | 
|---|
| 15 |    Rendering Q3 Maps by Morgan McGuire                  http://graphics.cs.brown.edu/games/quake/quake3.html | 
|---|
| 16 |    Unofficial Quake 3 Map Specs by Kekoa Proudfoot      http://graphics.stanford.edu/~kekoa/q3/ | 
|---|
| 17 |  | 
|---|
| 18 |    Collision detection adapted from: | 
|---|
| 19 |    Quake 3 Collision Detection by Nathan Ostgard        http://www.devmaster.net/articles/quake3collision/ | 
|---|
| 20 | */ | 
|---|
| 21 |  | 
|---|
| 22 |  | 
|---|
| 23 | #include "vector.h" | 
|---|
| 24 | #include "bsp_file.h" | 
|---|
| 25 | #include "bsp_manager.h" | 
|---|
| 26 | #include "bsp_tree_leaf.h" | 
|---|
| 27 | #include "p_node.h" | 
|---|
| 28 | #include "state.h" | 
|---|
| 29 | #include "debug.h" | 
|---|
| 30 | #include "material.h" | 
|---|
| 31 | #include "camera.h" | 
|---|
| 32 | #include "vertex_array_model.h" | 
|---|
| 33 | #include "world_entities/player.h" | 
|---|
| 34 | #include "world_entities/playable.h" | 
|---|
| 35 | #include "util/loading/resource_manager.h" | 
|---|
| 36 | // STL Containers | 
|---|
| 37 | #include <vector> | 
|---|
| 38 | #include <deque> | 
|---|
| 39 | #include "movie_player.h" | 
|---|
| 40 |  | 
|---|
| 41 | #include "world_entity.h" | 
|---|
| 42 |  | 
|---|
| 43 | #include "util/loading/load_param.h" | 
|---|
| 44 | #include "util/loading/factory.h" | 
|---|
| 45 |  | 
|---|
| 46 | #include "aabb.h" | 
|---|
| 47 | #include "cr_defs.h" | 
|---|
| 48 |  | 
|---|
| 49 |  | 
|---|
| 50 | //CREATE_FACTORY( BspManager, CL_BSP_MODEL); | 
|---|
| 51 |  | 
|---|
| 52 | BspManager::BspManager(WorldEntity* parent) | 
|---|
| 53 | { | 
|---|
| 54 |  | 
|---|
| 55 |   this->parent = parent; | 
|---|
| 56 |   /*// open a BSP file | 
|---|
| 57 |   this->bspFile = new BspFile(); | 
|---|
| 58 |   this->bspFile->scale = 0.4f; | 
|---|
| 59 |   this->bspFile->read(ResourceManager::getFullName("test.bsp").c_str()); | 
|---|
| 60 |   this->bspFile->build_tree(); | 
|---|
| 61 |   this->root  = this->bspFile->get_root(); | 
|---|
| 62 |   this->alreadyVisible = new bool [this->bspFile->numFaces]; | 
|---|
| 63 |   */ | 
|---|
| 64 |  | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 |  | 
|---|
| 68 | /* | 
|---|
| 69 | BspManager::BspManager(const TiXmlElement* root) | 
|---|
| 70 | { | 
|---|
| 71 |  | 
|---|
| 72 |  | 
|---|
| 73 |   if( root != NULL) | 
|---|
| 74 |     this->loadParams(root); | 
|---|
| 75 |  | 
|---|
| 76 |   CDEngine::getInstance()->setBSPModel(this); | 
|---|
| 77 | } */ | 
|---|
| 78 |  | 
|---|
| 79 | BspManager::~BspManager() | 
|---|
| 80 | { | 
|---|
| 81 |   if(this->bspFile) | 
|---|
| 82 |     delete this->bspFile; | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | int BspManager::load(const char* fileName, float scale) | 
|---|
| 86 | { | 
|---|
| 87 |   // open a BSP file | 
|---|
| 88 |  | 
|---|
| 89 |  | 
|---|
| 90 |   this->bspFile = new BspFile(); | 
|---|
| 91 |   this->bspFile->scale =  scale; | 
|---|
| 92 |   if(this->bspFile->read(ResourceManager::getFullName(fileName).c_str()) == -1) | 
|---|
| 93 |     return -1; | 
|---|
| 94 |  | 
|---|
| 95 |   this->bspFile->build_tree(); | 
|---|
| 96 |   this->root  = this->bspFile->get_root(); | 
|---|
| 97 |   this->alreadyVisible = new bool [this->bspFile->numFaces]; | 
|---|
| 98 |  | 
|---|
| 99 |   this->outputFraction = 1.0f; | 
|---|
| 100 |  | 
|---|
| 101 |   return 0; | 
|---|
| 102 | } | 
|---|
| 103 | /* | 
|---|
| 104 | BspManager::BspManager(const char* fileName, float scale) | 
|---|
| 105 | { | 
|---|
| 106 |   // open a BSP file | 
|---|
| 107 |   this->bspFile = new BspFile(); | 
|---|
| 108 |   this->bspFile->scale =  scale; | 
|---|
| 109 |   this->bspFile->read(fileName); | 
|---|
| 110 |   this->bspFile->build_tree(); | 
|---|
| 111 |   this->root  = this->bspFile->get_root(); | 
|---|
| 112 |   this->alreadyVisible = new bool [this->bspFile->numFaces]; | 
|---|
| 113 |  | 
|---|
| 114 |   CDEngine::getInstance()->setBSPModel(this); | 
|---|
| 115 | } | 
|---|
| 116 | */ | 
|---|
| 117 |  | 
|---|
| 118 | const void BspManager::tick(float time) | 
|---|
| 119 | { | 
|---|
| 120 |  | 
|---|
| 121 |   if(!this->bspFile->MovieMaterials.empty()) { | 
|---|
| 122 |     ::std::vector<MoviePlayer *>::iterator it = this->bspFile->MovieMaterials.begin() ; | 
|---|
| 123 |     while(it != this->bspFile->MovieMaterials.end()) { | 
|---|
| 124 |       (*it)->tick(time); | 
|---|
| 125 |       it++; | 
|---|
| 126 |     } | 
|---|
| 127 |     //this->bspFile->MovieMaterials.front()->tick(time ); | 
|---|
| 128 |  | 
|---|
| 129 |  | 
|---|
| 130 |   } | 
|---|
| 131 |  | 
|---|
| 132 | } | 
|---|
| 133 | const void BspManager::draw() | 
|---|
| 134 | { | 
|---|
| 135 |  | 
|---|
| 136 |   /* | 
|---|
| 137 |   this->drawDebugCube(&this->out); | 
|---|
| 138 |   this->out1 = this->out; | 
|---|
| 139 |   this->out2 = this->out; | 
|---|
| 140 |   if(this->collPlane != NULL) { | 
|---|
| 141 |     this->out1.x += this->collPlane->x*5.0; | 
|---|
| 142 |     this->out1.y += this->collPlane->y*5.0; | 
|---|
| 143 |     this->out1.z += this->collPlane->z*5.0; | 
|---|
| 144 |  | 
|---|
| 145 |     this->out2.x += this->collPlane->x*10.0; | 
|---|
| 146 |     this->out2.y += this->collPlane->y*10.0; | 
|---|
| 147 |     this->out2.z += this->collPlane->z*10.0; | 
|---|
| 148 |   } | 
|---|
| 149 |   this->drawDebugCube(&this->out1); | 
|---|
| 150 |   this->drawDebugCube(&this->out2); | 
|---|
| 151 |  | 
|---|
| 152 |   */ | 
|---|
| 153 |  | 
|---|
| 154 |  | 
|---|
| 155 |   // Draw Debug Terrain | 
|---|
| 156 |   /* | 
|---|
| 157 |   this->bspFile->Materials[0]->select(); | 
|---|
| 158 |   for(int i = 0; i <  this->bspFile->numPatches ; i++) | 
|---|
| 159 |         { | 
|---|
| 160 |                 this->bspFile->VertexArrayModels[i]->draw(); | 
|---|
| 161 |  | 
|---|
| 162 |         } | 
|---|
| 163 |   */ | 
|---|
| 164 |  | 
|---|
| 165 |  | 
|---|
| 166 |  | 
|---|
| 167 |   // erase alreadyVisible | 
|---|
| 168 |   for(int i = 0; i < this->bspFile->numFaces; i++) this->alreadyVisible[i] = false; | 
|---|
| 169 |   float tmp = 0; | 
|---|
| 170 |   //this->opal.clear(); | 
|---|
| 171 |   //this->trasparent.clear(); | 
|---|
| 172 |   // Find all visible faces... | 
|---|
| 173 |  | 
|---|
| 174 |   this->cam = State::getCamera()->getAbsCoor() ; | 
|---|
| 175 |   //this->ship = State::getCameraTargetNode()->getAbsCoor(); | 
|---|
| 176 |  | 
|---|
| 177 |  | 
|---|
| 178 |  | 
|---|
| 179 |  | 
|---|
| 180 |  | 
|---|
| 181 |   this->viewDir=    State::getCamera()->getAbsDirX(); | 
|---|
| 182 |   float d = (cam.x*viewDir.x + cam.y*viewDir.y + cam.z * viewDir.z); | 
|---|
| 183 |  | 
|---|
| 184 |   BspTreeNode*  ActLeaf = this->getLeaf(this->bspFile->root, &ship); | 
|---|
| 185 |   int viscluster = -1; | 
|---|
| 186 |   viscluster =((leaf*)(this->bspFile->leaves))[ ActLeaf->leafIndex].cluster; // get the players cluster (viscluster) | 
|---|
| 187 |  | 
|---|
| 188 |  | 
|---|
| 189 |  | 
|---|
| 190 |  | 
|---|
| 191 |   // this->checkCollision(this->root, &this->cam);   //!< Test Collision Detection | 
|---|
| 192 |  | 
|---|
| 193 |  | 
|---|
| 194 |   this->outputStartsOut = true; | 
|---|
| 195 |   this->outputAllSolid = false; | 
|---|
| 196 |   this->outputFraction = 1.0f; | 
|---|
| 197 |  | 
|---|
| 198 |   if ( viscluster < 0  || ((int *)(this->bspFile->header))[35] == 0 )  //!< if (sizeof(Visdata) == 0) | 
|---|
| 199 |   { | 
|---|
| 200 |  | 
|---|
| 201 |  | 
|---|
| 202 |  | 
|---|
| 203 |     // Iterate through all Leafs | 
|---|
| 204 |     for(int i = 0; i <  this->bspFile->numLeafs   ; i++ ) | 
|---|
| 205 |     { | 
|---|
| 206 |       // cluster =  (this->bspFile->leaves)[i].cluster; | 
|---|
| 207 |       leaf& curLeaf = (this->bspFile->leaves)[i]; | 
|---|
| 208 |       if(curLeaf.cluster<0) continue; | 
|---|
| 209 |  | 
|---|
| 210 |       /** Do Frustum culling and draw 'em all **/ | 
|---|
| 211 |  | 
|---|
| 212 |       Vector dir = State::getCameraNode()->getAbsDirX(); | 
|---|
| 213 |  | 
|---|
| 214 |       float dist =  dir.x*this->cam.x +dir.y*this->cam.y +dir.z*this->cam.z; | 
|---|
| 215 |       //if(dist < 0) dist = -dist; | 
|---|
| 216 |       const float dMins = dir.x*(float)curLeaf.mins[0] +dir.y*(float)curLeaf.mins[1] +dir.z*(float)curLeaf.mins[2] - dist ; | 
|---|
| 217 |       const float dMaxs = dir.x*(float)curLeaf.maxs[0] +dir.y*(float)curLeaf.maxs[1] +dir.z*(float)curLeaf.maxs[2] - dist ; | 
|---|
| 218 |  | 
|---|
| 219 |       if(dMins < -300.0 && dMaxs < -300.0) { | 
|---|
| 220 |         continue; | 
|---|
| 221 |       } | 
|---|
| 222 |       if( (this->cam - Vector(curLeaf.mins[0],curLeaf.mins[1], curLeaf.mins[2])).len() > 2000  && (this->cam - Vector(curLeaf.maxs[0],curLeaf.maxs[1], curLeaf.maxs[2])).len() > 2000) { | 
|---|
| 223 |         continue; | 
|---|
| 224 |       } | 
|---|
| 225 |  | 
|---|
| 226 |  | 
|---|
| 227 |       // Iterate through all faces | 
|---|
| 228 |       for (int j = 0; j < curLeaf.n_leaffaces ; ++j) { | 
|---|
| 229 |         const int g = (j +  curLeaf.leafface); | 
|---|
| 230 |         const int f = ((int *)this->bspFile->leafFaces)[g]; | 
|---|
| 231 |         if (f >=0 && !this->isAlreadyVisible(f)) { | 
|---|
| 232 |           this->alreadyVisible[f] = true; | 
|---|
| 233 |           addFace(f); // "visibleFaces.append(f)" | 
|---|
| 234 |         } | 
|---|
| 235 |       } | 
|---|
| 236 |  | 
|---|
| 237 |  | 
|---|
| 238 |  | 
|---|
| 239 |  | 
|---|
| 240 |     } //for | 
|---|
| 241 |   } else { | 
|---|
| 242 |  | 
|---|
| 243 |  | 
|---|
| 244 |     unsigned int v; | 
|---|
| 245 |     unsigned char  visSet; | 
|---|
| 246 |  | 
|---|
| 247 |     // Iterate through all Leafs | 
|---|
| 248 |  | 
|---|
| 249 |     for(int i = 0; i <  this->bspFile->numLeafs   ; ++i ) { | 
|---|
| 250 |       leaf& camLeaf =  (this->bspFile->leaves)[ActLeaf->leafIndex] ; | 
|---|
| 251 |       leaf& curLeaf =  (this->bspFile->leaves)[i] ; | 
|---|
| 252 |       int& cluster =  curLeaf.cluster; | 
|---|
| 253 |  | 
|---|
| 254 |       if(cluster < 0) continue; | 
|---|
| 255 |       v = ((viscluster *  ( ((int *)this->bspFile->visData)[1]) ) + (cluster / 8)); | 
|---|
| 256 |       visSet =((char*) (this->bspFile->visData))[v + 8]; | 
|---|
| 257 |  | 
|---|
| 258 |       // gets bit of visSet | 
|---|
| 259 |       if( ((visSet) & (1 << (cluster &  7))) != 0 ) { | 
|---|
| 260 |  | 
|---|
| 261 |         // Frustum culling | 
|---|
| 262 |  | 
|---|
| 263 |         Vector dir; | 
|---|
| 264 |         dir.x = State::getCameraNode()->getAbsDirX().x; | 
|---|
| 265 |         dir.y =  State::getCameraNode()->getAbsDirX().y; | 
|---|
| 266 |         dir.z =  State::getCameraNode()->getAbsDirX().z; | 
|---|
| 267 |         const float dist =  dir.x*this->cam.x +dir.y*this->cam.y +dir.z*this->cam.z; | 
|---|
| 268 |         //if(dist < 0) dist = -dist; | 
|---|
| 269 |         const float dMins = dir.x*(float)curLeaf.mins[0] +dir.y*(float)curLeaf.mins[1] +dir.z*(float)curLeaf.mins[2] - dist; | 
|---|
| 270 |         const float dMaxs = dir.x*(float)curLeaf.maxs[0] +dir.y*(float)curLeaf.maxs[1] +dir.z*(float)curLeaf.maxs[2] - dist; | 
|---|
| 271 |  | 
|---|
| 272 |         if(dMins < -50.0 && dMaxs < -  50.0) { | 
|---|
| 273 |           continue; | 
|---|
| 274 |         } | 
|---|
| 275 |  | 
|---|
| 276 |  | 
|---|
| 277 |         // Iterate through all faces | 
|---|
| 278 |         for (int j = 0; j < curLeaf.n_leaffaces ; ++j) { | 
|---|
| 279 |           const int g = (j +  curLeaf.leafface); | 
|---|
| 280 |           const int f = ((int *)this->bspFile->leafFaces)[g]; | 
|---|
| 281 |  | 
|---|
| 282 |           if (!this->isAlreadyVisible(f) && f>=0) { | 
|---|
| 283 |             this->addFace(f); | 
|---|
| 284 |             this->alreadyVisible[f] = true; | 
|---|
| 285 |           } | 
|---|
| 286 |  | 
|---|
| 287 |         } | 
|---|
| 288 |  | 
|---|
| 289 |       }// if | 
|---|
| 290 |  | 
|---|
| 291 |     }//for | 
|---|
| 292 |  | 
|---|
| 293 |   }//else | 
|---|
| 294 |  | 
|---|
| 295 |   while(!this->opal.empty()) { | 
|---|
| 296 |     this->draw_face(this->opal.front()); | 
|---|
| 297 |     this->opal.pop_front(); | 
|---|
| 298 |   } | 
|---|
| 299 |   while(!this->trasparent.empty()) { | 
|---|
| 300 |     this->draw_face(this->trasparent.back()); | 
|---|
| 301 |     this->trasparent.pop_back(); | 
|---|
| 302 |   } | 
|---|
| 303 |   //glEnable(GL_TEXTURE_2D); | 
|---|
| 304 |   glActiveTextureARB(GL_TEXTURE1_ARB); | 
|---|
| 305 |   glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap); | 
|---|
| 306 |  | 
|---|
| 307 |  | 
|---|
| 308 |  | 
|---|
| 309 | }//draw | 
|---|
| 310 |  | 
|---|
| 311 |  | 
|---|
| 312 |  | 
|---|
| 313 | void BspManager::draw_face(int curface) | 
|---|
| 314 | { | 
|---|
| 315 |   face& curFace =  (this->bspFile->faces)[curface]; | 
|---|
| 316 |   const BspVertex* curVertex = (BspVertex *) this->bspFile->vertice; | 
|---|
| 317 |   int stride = sizeof(BspVertex);  // sizeof(Vertex) | 
|---|
| 318 |   int offset    = curFace.vertex; | 
|---|
| 319 |   if (curFace.effect != -1) return; | 
|---|
| 320 |   // PRINTF(0)("BSP Manager: "); | 
|---|
| 321 |   // PRINTF(0)("BSP Manager: type: %i  \n", curFace.texture); | 
|---|
| 322 |  | 
|---|
| 323 |   //  if(  curFace.texture < 0 ) return; | 
|---|
| 324 |   if(curFace.type == 2) { | 
|---|
| 325 |     this->draw_patch( &curFace); | 
|---|
| 326 |     return; | 
|---|
| 327 |   } | 
|---|
| 328 |   // if(curFace.type != 1) return; | 
|---|
| 329 |   if((char*)(this->bspFile->textures)[curFace.texture*72]== 0) return; | 
|---|
| 330 |  | 
|---|
| 331 |   if(this->lastTex != curFace.texture) { | 
|---|
| 332 |     if(this->bspFile->Materials[curFace.texture].animated) { | 
|---|
| 333 |       // glBlendFunc(GL_ZERO,GL_ONE); | 
|---|
| 334 |  | 
|---|
| 335 |  | 
|---|
| 336 |  | 
|---|
| 337 |       if(this->bspFile->Materials[curFace.texture].aviMat->getStatus() == 2) this->bspFile->Materials[curFace.texture].aviMat->start(0); | 
|---|
| 338 |       //this->bspFile->Materials[curFace.texture].aviMat->tick(0.005); | 
|---|
| 339 |       int n =  this->bspFile->Materials[curFace.texture].aviMat->getTexture(); | 
|---|
| 340 |       glActiveTextureARB(GL_TEXTURE0_ARB); | 
|---|
| 341 |       glBindTexture(GL_TEXTURE_2D, n ); | 
|---|
| 342 |       this->lastTex = curFace.texture; | 
|---|
| 343 |  | 
|---|
| 344 |     } else { | 
|---|
| 345 |       this->bspFile->Materials[curFace.texture].mat->select(); | 
|---|
| 346 |       this->lastTex = curFace.texture; | 
|---|
| 347 |     } | 
|---|
| 348 |   } | 
|---|
| 349 |  | 
|---|
| 350 |   if(curFace.lm_index < 0) { | 
|---|
| 351 |     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 
|---|
| 352 |     glActiveTextureARB(GL_TEXTURE1_ARB); | 
|---|
| 353 |     glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap ); | 
|---|
| 354 |     glEnable(GL_TEXTURE_2D); | 
|---|
| 355 |   } else { | 
|---|
| 356 |     // glEnable(GL_BLEND); | 
|---|
| 357 |     //glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); | 
|---|
| 358 |     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 
|---|
| 359 |     glActiveTextureARB(GL_TEXTURE1_ARB); | 
|---|
| 360 |     glBindTexture(GL_TEXTURE_2D, this->bspFile->glLightMapTextures[curFace.lm_index]); | 
|---|
| 361 |     glEnable(GL_TEXTURE_2D); | 
|---|
| 362 |     //  glDisable(GL_BLEND); | 
|---|
| 363 |   } | 
|---|
| 364 |  | 
|---|
| 365 |   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 
|---|
| 366 |  | 
|---|
| 367 |   // glColor4f(3.0,3.0,3.0,1.0); | 
|---|
| 368 |   glEnableClientState(GL_VERTEX_ARRAY ); | 
|---|
| 369 |   glEnableClientState(GL_TEXTURE_COORD_ARRAY ); | 
|---|
| 370 |   glEnableClientState(GL_NORMAL_ARRAY ); | 
|---|
| 371 |   //  glEnableClientState(GL_COLOR_ARRAY); | 
|---|
| 372 |  | 
|---|
| 373 |  | 
|---|
| 374 |   glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0])); | 
|---|
| 375 |  | 
|---|
| 376 |   glClientActiveTextureARB(GL_TEXTURE0_ARB); | 
|---|
| 377 |   glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0])); | 
|---|
| 378 |   //glEnableClientState(GL_TEXTURE_COORD_ARRAY); | 
|---|
| 379 |  | 
|---|
| 380 |   glClientActiveTextureARB(GL_TEXTURE1_ARB); | 
|---|
| 381 |   glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1])); | 
|---|
| 382 |   //glEnableClientState(GL_TEXTURE_COORD_ARRAY); | 
|---|
| 383 |  | 
|---|
| 384 |  | 
|---|
| 385 |   glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0])); | 
|---|
| 386 |   // glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0])); | 
|---|
| 387 |   glDrawElements(GL_TRIANGLES, curFace.n_meshverts, | 
|---|
| 388 |                  GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert])); | 
|---|
| 389 |  | 
|---|
| 390 |   glDisableClientState(GL_TEXTURE0_ARB); | 
|---|
| 391 |   glDisableClientState(GL_TEXTURE1_ARB); | 
|---|
| 392 |   glDisableClientState(GL_VERTEX_ARRAY ); | 
|---|
| 393 |   glDisableClientState(GL_TEXTURE_COORD_ARRAY ); | 
|---|
| 394 |   glDisableClientState(GL_NORMAL_ARRAY ); | 
|---|
| 395 |   // glDisableClientState(GL_COLOR_ARRAY); | 
|---|
| 396 |  | 
|---|
| 397 | } | 
|---|
| 398 |  | 
|---|
| 399 |  | 
|---|
| 400 | void BspManager::draw_debug_face(int curface) | 
|---|
| 401 | { | 
|---|
| 402 |   face& curFace =  (this->bspFile->faces)[curface]; | 
|---|
| 403 |   const BspVertex* curVertex = (BspVertex *) this->bspFile->vertice; | 
|---|
| 404 |   int stride = 44;  // sizeof(Vertex) | 
|---|
| 405 |   int offset    = curFace.vertex; | 
|---|
| 406 |  | 
|---|
| 407 |   // PRINTF(0)("BSP Manager: "); | 
|---|
| 408 |   // PRINTF(0)("BSP Manager: type: %i  \n", curFace.texture); | 
|---|
| 409 |  | 
|---|
| 410 |   //  if(  curFace.texture < 0 ) return; | 
|---|
| 411 |   if(curFace.type == 2) { | 
|---|
| 412 |     this->draw_patch( &curFace); | 
|---|
| 413 |     return; | 
|---|
| 414 |   } | 
|---|
| 415 |   if(curFace.type == 3) return; | 
|---|
| 416 |   // if(this->bspFile->Materials[curFace.texture] != NULL) | 
|---|
| 417 |  | 
|---|
| 418 |   this->bspFile->Materials[2].mat->select(); | 
|---|
| 419 |   this->lastTex = 2; | 
|---|
| 420 |  | 
|---|
| 421 |   glEnableClientState(GL_VERTEX_ARRAY ); | 
|---|
| 422 |   glEnableClientState(GL_TEXTURE_COORD_ARRAY ); | 
|---|
| 423 |   glEnableClientState(GL_NORMAL_ARRAY ); | 
|---|
| 424 |   //glEnableClientState(GL_COLOR_ARRAY); | 
|---|
| 425 |   // glEnableClientState(GL_VERTEX_ARRAY ); | 
|---|
| 426 |   glClientActiveTextureARB(GL_TEXTURE0_ARB); | 
|---|
| 427 |   glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0])); | 
|---|
| 428 |   glEnableClientState(GL_TEXTURE_COORD_ARRAY); | 
|---|
| 429 |   // glClientActiveTextureARB(GL_TEXTURE0_ARB); | 
|---|
| 430 |   glClientActiveTextureARB(GL_TEXTURE1_ARB); | 
|---|
| 431 |   glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0])); | 
|---|
| 432 |   glEnableClientState(GL_TEXTURE_COORD_ARRAY); | 
|---|
| 433 |   // glClientActiveTextureARB(GL_TEXTURE1_ARB); | 
|---|
| 434 |   // glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1])); | 
|---|
| 435 |   //glEnableClientState(GL_NORMAL_ARRAY ); | 
|---|
| 436 |  | 
|---|
| 437 |   glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0])); | 
|---|
| 438 |   //  glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0])); | 
|---|
| 439 |   glDrawElements(GL_TRIANGLES, curFace.n_meshverts, | 
|---|
| 440 |                  GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert])); | 
|---|
| 441 |  | 
|---|
| 442 | } | 
|---|
| 443 |  | 
|---|
| 444 | void BspManager::draw_patch(face* Face) | 
|---|
| 445 | { | 
|---|
| 446 |   if(this->lastTex != Face->texture) { | 
|---|
| 447 |     this->bspFile->Materials[Face->texture].mat->select(); | 
|---|
| 448 |     this->lastTex = Face->texture; | 
|---|
| 449 |   } | 
|---|
| 450 |   if (Face->effect != -1) return; | 
|---|
| 451 |  | 
|---|
| 452 |  | 
|---|
| 453 |   if(Face->lm_index < 0) { | 
|---|
| 454 |     glActiveTextureARB(GL_TEXTURE1_ARB); | 
|---|
| 455 |     glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap); | 
|---|
| 456 |     glEnable(GL_TEXTURE_2D); | 
|---|
| 457 |   } else { | 
|---|
| 458 |     glActiveTextureARB(GL_TEXTURE1_ARB); | 
|---|
| 459 |     glBindTexture(GL_TEXTURE_2D, this->bspFile->glLightMapTextures[Face->lm_index]); | 
|---|
| 460 |     glEnable(GL_TEXTURE_2D); | 
|---|
| 461 |   } | 
|---|
| 462 |   //glColor4f(3.0,3.0,3.0,1.0); | 
|---|
| 463 |  | 
|---|
| 464 |   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 
|---|
| 465 |   glEnable( GL_AUTO_NORMAL); | 
|---|
| 466 |   glEnableClientState(GL_VERTEX_ARRAY ); | 
|---|
| 467 |   glEnableClientState(GL_TEXTURE_COORD_ARRAY ); | 
|---|
| 468 |   for(int i = Face->n_meshverts -1; i >=0   ; i--) { | 
|---|
| 469 |     //glFrontFace(GL_CW); | 
|---|
| 470 |     //PRINTF(0)("BSP Manager: Face->size[0]: %i . \n", Face->size[0]); | 
|---|
| 471 |  | 
|---|
| 472 |  | 
|---|
| 473 |     //glEnableClientState(GL_NORMAL_ARRAY ); | 
|---|
| 474 |  | 
|---|
| 475 |     glVertexPointer(3, GL_FLOAT,44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).position[0])); | 
|---|
| 476 |  | 
|---|
| 477 |  | 
|---|
| 478 |     glClientActiveTextureARB(GL_TEXTURE0_ARB); | 
|---|
| 479 |     glEnableClientState(GL_TEXTURE_COORD_ARRAY); | 
|---|
| 480 |     glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[0][0])); | 
|---|
| 481 |  | 
|---|
| 482 |  | 
|---|
| 483 |  | 
|---|
| 484 |     glClientActiveTextureARB(GL_TEXTURE1_ARB); | 
|---|
| 485 |     glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[1][0])); | 
|---|
| 486 |     //glEnableClientState(GL_TEXTURE_COORD_ARRAY); | 
|---|
| 487 |  | 
|---|
| 488 |  | 
|---|
| 489 |     //  glNormalPointer( GL_FLOAT, 44,&((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).normal[0]) ); | 
|---|
| 490 |  | 
|---|
| 491 |  | 
|---|
| 492 |  | 
|---|
| 493 |  | 
|---|
| 494 |     for(int row=6; row>=0; --row) { | 
|---|
| 495 |       glDrawElements(GL_TRIANGLE_STRIP, 2*(8), GL_UNSIGNED_INT, | 
|---|
| 496 |                      & (     (((GLuint*)  (this->bspFile->patchIndexes))[7*8*2*(Face->meshvert+i)+ row*2*8]  ))  ); | 
|---|
| 497 |     } | 
|---|
| 498 |  | 
|---|
| 499 |     //glFrontFace(GL_CCW); | 
|---|
| 500 |   } | 
|---|
| 501 |   glDisableClientState(GL_TEXTURE0_ARB); | 
|---|
| 502 |   glDisableClientState(GL_TEXTURE1_ARB); | 
|---|
| 503 |   glDisable(GL_AUTO_NORMAL); | 
|---|
| 504 |   glDisableClientState(GL_VERTEX_ARRAY ); | 
|---|
| 505 |   glDisableClientState(GL_TEXTURE_COORD_ARRAY ); | 
|---|
| 506 |  | 
|---|
| 507 |  | 
|---|
| 508 | } | 
|---|
| 509 |  | 
|---|
| 510 | bool BspManager::isAlreadyVisible(int Face) | 
|---|
| 511 | { | 
|---|
| 512 |   return this->alreadyVisible[Face]; | 
|---|
| 513 | } | 
|---|
| 514 |  | 
|---|
| 515 |  | 
|---|
| 516 | BspTreeNode*  BspManager::getLeaf(BspTreeNode* node, Vector* cam) | 
|---|
| 517 | { | 
|---|
| 518 |   float dist = 0; | 
|---|
| 519 |   while(!(node->isLeaf)) { | 
|---|
| 520 |     dist = (node->plane.x * this->cam.x + node->plane.y*this->cam.y + node->plane.z*this->cam.z) - node->d; | 
|---|
| 521 |     if(dist >= 0.0f) { | 
|---|
| 522 |       node = node->left; | 
|---|
| 523 |     } else { | 
|---|
| 524 |       node = node->right; | 
|---|
| 525 |     } | 
|---|
| 526 |   } | 
|---|
| 527 |   return  node; | 
|---|
| 528 | } | 
|---|
| 529 |  | 
|---|
| 530 | void BspManager::checkBrushRay(brush* curBrush) | 
|---|
| 531 | { | 
|---|
| 532 |   float EPSILON = 0.000001; | 
|---|
| 533 |   float startDistance; | 
|---|
| 534 |   float endDistance; | 
|---|
| 535 |  | 
|---|
| 536 |   float startFraction = -1.0f; | 
|---|
| 537 |   float endFraction = 1.0f; | 
|---|
| 538 |   bool startsOut = false; | 
|---|
| 539 |   bool endsOut = false; | 
|---|
| 540 |  | 
|---|
| 541 |   Vector inputStart = State::getCameraTargetNode()->getLastAbsCoor(); | 
|---|
| 542 |   Vector inputEnd   = State::getCameraTargetNode()->getAbsCoor(); | 
|---|
| 543 |  | 
|---|
| 544 |   for (int i = 0; i < curBrush->n_brushsides; i++) { | 
|---|
| 545 |     brushside& curBrushSide =   this->bspFile->brushSides[curBrush->brushside + i]   ; | 
|---|
| 546 |     plane& curPlane  =   this->bspFile->planes[curBrushSide.plane] ; | 
|---|
| 547 |  | 
|---|
| 548 |     startDistance = inputStart.x * curPlane.x + inputStart.y * curPlane.y+ inputStart.z * curPlane.z - curPlane.d; | 
|---|
| 549 |     endDistance = inputEnd.x * curPlane.x +inputEnd.y * curPlane.y +inputEnd.z * curPlane.z -curPlane.d; | 
|---|
| 550 |  | 
|---|
| 551 |     if (startDistance > 0) | 
|---|
| 552 |       startsOut = true; | 
|---|
| 553 |     if (endDistance > 0) | 
|---|
| 554 |       endsOut = true; | 
|---|
| 555 |  | 
|---|
| 556 |     // make sure the trace isn't completely on one side of the brush | 
|---|
| 557 |     if (startDistance > 0 && endDistance > 0) {   // both are in front of the plane, its outside of this brush | 
|---|
| 558 |       return; | 
|---|
| 559 |     } | 
|---|
| 560 |     if (startDistance <= 0 && endDistance <= 0) {   // both are behind this plane, it will get clipped by another one | 
|---|
| 561 |       continue; | 
|---|
| 562 |     } | 
|---|
| 563 |  | 
|---|
| 564 |     // MMM... BEEFY | 
|---|
| 565 |     if (startDistance > endDistance) {   // line is entering into the brush | 
|---|
| 566 |       float fraction = (startDistance - EPSILON) / (startDistance - endDistance);  // * | 
|---|
| 567 |       if (fraction > startFraction) | 
|---|
| 568 |         startFraction = fraction; | 
|---|
| 569 |       // don't store plane | 
|---|
| 570 |       // this->collPlane = &curPlane; | 
|---|
| 571 |  | 
|---|
| 572 |     } else {   // line is leaving the brush | 
|---|
| 573 |       float fraction = (startDistance + EPSILON) / (startDistance - endDistance);  // * | 
|---|
| 574 |       if (fraction < endFraction) | 
|---|
| 575 |         endFraction = fraction; | 
|---|
| 576 |       // don't store plane | 
|---|
| 577 |       //this->collPlane = & curPlane; | 
|---|
| 578 |  | 
|---|
| 579 |     } | 
|---|
| 580 |  | 
|---|
| 581 |   } | 
|---|
| 582 |   if (startsOut == false) { | 
|---|
| 583 |     this->outputStartsOut = false; | 
|---|
| 584 |     if (endsOut == false) | 
|---|
| 585 |       this->outputAllSolid = true; | 
|---|
| 586 |     return; | 
|---|
| 587 |   } | 
|---|
| 588 |  | 
|---|
| 589 |   if (startFraction < endFraction) { | 
|---|
| 590 |     if (startFraction > -1.0f && startFraction < outputFraction) { | 
|---|
| 591 |       if (startFraction < 0) | 
|---|
| 592 |         startFraction = 0; | 
|---|
| 593 |       this->outputFraction = startFraction; | 
|---|
| 594 |     } | 
|---|
| 595 |   } | 
|---|
| 596 |  | 
|---|
| 597 | } | 
|---|
| 598 |  | 
|---|
| 599 | void BspManager::checkBrushRayN(brush* curBrush) | 
|---|
| 600 | { | 
|---|
| 601 |   float EPSILON = 0.000001; | 
|---|
| 602 |   float startDistance; | 
|---|
| 603 |   float endDistance; | 
|---|
| 604 |  | 
|---|
| 605 |   float startFraction = -1.0f; | 
|---|
| 606 |   float endFraction = 1.0f; | 
|---|
| 607 |   bool  startsOut = false; | 
|---|
| 608 |   bool  endsOut = false; | 
|---|
| 609 |  | 
|---|
| 610 |   // Vector inputStart = State::getCameraTargetNode()->getLastAbsCoor(); | 
|---|
| 611 |   // Vector inputEnd   = State::getCameraTargetNode()->getAbsCoor(); | 
|---|
| 612 |  | 
|---|
| 613 |   for (int i = 0; i < curBrush->n_brushsides; i++) { | 
|---|
| 614 |     brushside& curBrushSide =   this->bspFile->brushSides[curBrush->brushside + i]   ; | 
|---|
| 615 |     plane& curPlane  =   this->bspFile->planes[curBrushSide.plane] ; | 
|---|
| 616 |  | 
|---|
| 617 |     startDistance = inputStart.x * curPlane.x + inputStart.y * curPlane.y+ inputStart.z * curPlane.z - curPlane.d; | 
|---|
| 618 |     endDistance = inputEnd.x * curPlane.x +inputEnd.y * curPlane.y +inputEnd.z * curPlane.z -curPlane.d; | 
|---|
| 619 |  | 
|---|
| 620 |     if (startDistance > 0) | 
|---|
| 621 |       startsOut = true; | 
|---|
| 622 |     if (endDistance > 0) | 
|---|
| 623 |       endsOut = true; | 
|---|
| 624 |  | 
|---|
| 625 |     // make sure the trace isn't completely on one side of the brush | 
|---|
| 626 |     if (startDistance > 0 && endDistance > 0) {   // both are in front of the plane, its outside of this brush | 
|---|
| 627 |       return; | 
|---|
| 628 |     } | 
|---|
| 629 |     if (startDistance <= 0 && endDistance <= 0) {   // both are behind this plane, it will get clipped by another one | 
|---|
| 630 |       continue; | 
|---|
| 631 |     } | 
|---|
| 632 |  | 
|---|
| 633 |     // MMM... BEEFY | 
|---|
| 634 |     if (startDistance > endDistance) {   // line is entering into the brush | 
|---|
| 635 |       float fraction = (startDistance - EPSILON) / (startDistance - endDistance);  // * | 
|---|
| 636 |       if (fraction > startFraction) | 
|---|
| 637 |         startFraction = fraction; | 
|---|
| 638 |       // store plane | 
|---|
| 639 |       this->collPlane = &curPlane; | 
|---|
| 640 |  | 
|---|
| 641 |     } else {   // line is leaving the brush | 
|---|
| 642 |       float fraction = (startDistance + EPSILON) / (startDistance - endDistance);  // * | 
|---|
| 643 |       if (fraction < endFraction) | 
|---|
| 644 |         endFraction = fraction; | 
|---|
| 645 |       // store plane | 
|---|
| 646 |       this->collPlane = & curPlane; | 
|---|
| 647 |  | 
|---|
| 648 |     } | 
|---|
| 649 |  | 
|---|
| 650 |   } | 
|---|
| 651 |   if (startsOut == false) { | 
|---|
| 652 |     this->outputStartsOut = false; | 
|---|
| 653 |     if (endsOut == false) | 
|---|
| 654 |       this->outputAllSolid = true; | 
|---|
| 655 |     return; | 
|---|
| 656 |   } | 
|---|
| 657 |  | 
|---|
| 658 |   if (startFraction < endFraction) { | 
|---|
| 659 |     if (startFraction > -1.0f && startFraction < outputFraction) { | 
|---|
| 660 |       if (startFraction < 0) | 
|---|
| 661 |         startFraction = 0; | 
|---|
| 662 |       this->outputFraction = startFraction; | 
|---|
| 663 |     } | 
|---|
| 664 |   } | 
|---|
| 665 |  | 
|---|
| 666 | } | 
|---|
| 667 |  | 
|---|
| 668 | void BspManager::checkBrushRayN(brush* curBrush, Vector& inputStart, Vector& inputEnd) | 
|---|
| 669 | { | 
|---|
| 670 |   float EPSILON = 0.000001; | 
|---|
| 671 |   float startDistance; | 
|---|
| 672 |   float endDistance; | 
|---|
| 673 |  | 
|---|
| 674 |   float startFraction = -1.0f; | 
|---|
| 675 |   float endFraction = 1.0f; | 
|---|
| 676 |   bool  startsOut = false; | 
|---|
| 677 |   bool  endsOut = false; | 
|---|
| 678 |  | 
|---|
| 679 |   //Vector inputStart = State::getCameraTargetNode()->getLastAbsCoor(); | 
|---|
| 680 |   //Vector inputEnd   = State::getCameraTargetNode()->getAbsCoor(); | 
|---|
| 681 |  | 
|---|
| 682 |   for (int i = 0; i < curBrush->n_brushsides; i++) { | 
|---|
| 683 |     brushside& curBrushSide =   this->bspFile->brushSides[curBrush->brushside + i]   ; | 
|---|
| 684 |     plane& curPlane  =   this->bspFile->planes[curBrushSide.plane] ; | 
|---|
| 685 |  | 
|---|
| 686 |     startDistance = inputStart.x * curPlane.x + inputStart.y * curPlane.y+ inputStart.z * curPlane.z - curPlane.d; | 
|---|
| 687 |     endDistance = inputEnd.x * curPlane.x +inputEnd.y * curPlane.y +inputEnd.z * curPlane.z -curPlane.d; | 
|---|
| 688 |  | 
|---|
| 689 |     if (startDistance > 0) | 
|---|
| 690 |       startsOut = true; | 
|---|
| 691 |     if (endDistance > 0) | 
|---|
| 692 |       endsOut = true; | 
|---|
| 693 |  | 
|---|
| 694 |     // make sure the trace isn't completely on one side of the brush | 
|---|
| 695 |     if (startDistance > 0 && endDistance > 0) {   // both are in front of the plane, its outside of this brush | 
|---|
| 696 |       return; | 
|---|
| 697 |     } | 
|---|
| 698 |     if (startDistance <= 0 && endDistance <= 0) {   // both are behind this plane, it will get clipped by another one | 
|---|
| 699 |       continue; | 
|---|
| 700 |     } | 
|---|
| 701 |  | 
|---|
| 702 |     // MMM... BEEFY | 
|---|
| 703 |     if (startDistance > endDistance) {   // line is entering into the brush | 
|---|
| 704 |       float fraction = (startDistance - EPSILON) / (startDistance - endDistance);  // * | 
|---|
| 705 |       if (fraction > startFraction) | 
|---|
| 706 |         startFraction = fraction; | 
|---|
| 707 |       // store plane | 
|---|
| 708 |       this->collPlane = &curPlane; | 
|---|
| 709 |  | 
|---|
| 710 |     } else {   // line is leaving the brush | 
|---|
| 711 |       float fraction = (startDistance + EPSILON) / (startDistance - endDistance);  // * | 
|---|
| 712 |       if (fraction < endFraction) | 
|---|
| 713 |         endFraction = fraction; | 
|---|
| 714 |       // store plane | 
|---|
| 715 |       this->collPlane = & curPlane; | 
|---|
| 716 |  | 
|---|
| 717 |     } | 
|---|
| 718 |  | 
|---|
| 719 |   } | 
|---|
| 720 |   if (startsOut == false) { | 
|---|
| 721 |     this->outputStartsOut = false; | 
|---|
| 722 |     if (endsOut == false) | 
|---|
| 723 |       this->outputAllSolid = true; | 
|---|
| 724 |     return; | 
|---|
| 725 |   } | 
|---|
| 726 |  | 
|---|
| 727 |   if (startFraction < endFraction) { | 
|---|
| 728 |     if (startFraction > -1.0f && startFraction < outputFraction) { | 
|---|
| 729 |       if (startFraction < 0) | 
|---|
| 730 |         startFraction = 0; | 
|---|
| 731 |       this->outputFraction = startFraction; | 
|---|
| 732 |     } | 
|---|
| 733 |   } | 
|---|
| 734 |  | 
|---|
| 735 | } | 
|---|
| 736 |  | 
|---|
| 737 |  | 
|---|
| 738 | void BspManager::checkCollisionRay(BspTreeNode* node, float startFraction, float endFraction, Vector* start, Vector* end) | 
|---|
| 739 | { | 
|---|
| 740 |  | 
|---|
| 741 |  | 
|---|
| 742 |   float EPSILON = 0.000001; | 
|---|
| 743 |   float  endDistance = (end)->x * (node->plane.x) +(end)->y * (node->plane.y) +(end)->z * (node->plane.z)  - node->d; | 
|---|
| 744 |   float  startDistance = (start)->x * (node->plane.x)+ (start)->y * (node->plane.y)+ (start)->z * (node->plane.z)- node->d; | 
|---|
| 745 |  | 
|---|
| 746 |  | 
|---|
| 747 |   if(node->isLeaf) { | 
|---|
| 748 |     leaf& curLeaf = this->bspFile->leaves[node->leafIndex]; | 
|---|
| 749 |     for (int i = 0; i <  curLeaf.n_leafbrushes ; i++) { | 
|---|
| 750 |       brush& curBrush = this->bspFile->brushes[((int*)(this->bspFile->leafBrushes))[curLeaf.leafbrush_first+i]]; | 
|---|
| 751 |       //object *brush = &BSP.brushes[BSP.leafBrushes[leaf->firstLeafBrush + i]]; | 
|---|
| 752 |       if (curBrush.n_brushsides > 0   && | 
|---|
| 753 |           ((((BspTexture*)(this->bspFile->textures))[curBrush.texture]).contents & 1)) | 
|---|
| 754 |         // CheckBrush( brush ); | 
|---|
| 755 |         this->checkBrushRay(&curBrush); | 
|---|
| 756 |       if(curBrush.n_brushsides <=0) this->outputAllSolid = true; | 
|---|
| 757 |     } | 
|---|
| 758 |     return; | 
|---|
| 759 |   } | 
|---|
| 760 |  | 
|---|
| 761 |  | 
|---|
| 762 |   if (startDistance >= 0 && endDistance >= 0)     // A | 
|---|
| 763 |   {   // both points are in front of the plane | 
|---|
| 764 |     // so check the front child | 
|---|
| 765 |     this->checkCollisionRay(node->left,0,0,start,end); | 
|---|
| 766 |   } else if (startDistance < 0 && endDistance < 0)  // B | 
|---|
| 767 |   {   // both points are behind the plane | 
|---|
| 768 |     // so check the back child | 
|---|
| 769 |     this->checkCollisionRay(node->right,0,0,start,end); | 
|---|
| 770 |   } else                                            // C | 
|---|
| 771 |   {   // the line spans the splitting plane | 
|---|
| 772 |     int side; | 
|---|
| 773 |     float fraction1, fraction2, middleFraction; | 
|---|
| 774 |     Vector middle; | 
|---|
| 775 |  | 
|---|
| 776 |     // STEP 1: split the segment into two | 
|---|
| 777 |     if (startDistance < endDistance) { | 
|---|
| 778 |       side = 1; // back | 
|---|
| 779 |       float inverseDistance = 1.0f / (startDistance - endDistance); | 
|---|
| 780 |       fraction1 = (startDistance + EPSILON) * inverseDistance; | 
|---|
| 781 |       fraction2 = (startDistance + EPSILON) * inverseDistance; | 
|---|
| 782 |     } else if (endDistance < startDistance) { | 
|---|
| 783 |       side = 0; // front(start)->x * (node->plane.x)+ | 
|---|
| 784 |       float inverseDistance = 1.0f / (startDistance - endDistance); | 
|---|
| 785 |       fraction1 = (startDistance + EPSILON) * inverseDistance; | 
|---|
| 786 |       fraction2 = (startDistance - EPSILON) * inverseDistance; | 
|---|
| 787 |     } else { | 
|---|
| 788 |       side = 0; // front | 
|---|
| 789 |       fraction1 = 1.0f; | 
|---|
| 790 |       fraction2 = 0.0f; | 
|---|
| 791 |     } | 
|---|
| 792 |  | 
|---|
| 793 |     // STEP 2: make sure the numbers are valid | 
|---|
| 794 |     if (fraction1 < 0.0f) fraction1 = 0.0f; | 
|---|
| 795 |     else if (fraction1 > 1.0f) fraction1 = 1.0f; | 
|---|
| 796 |     if (fraction2 < 0.0f) fraction2 = 0.0f; | 
|---|
| 797 |     else if (fraction2 > 1.0f) fraction2 = 1.0f; | 
|---|
| 798 |  | 
|---|
| 799 |     // STEP 3: calculate the middle point for the first side | 
|---|
| 800 |     middleFraction = startFraction + | 
|---|
| 801 |                      (endFraction - startFraction) * fraction1; | 
|---|
| 802 |  | 
|---|
| 803 |     middle.x = start->x + fraction1 * (end->x - start->x); | 
|---|
| 804 |     middle.y = start->y + fraction1 * (end->y - start->y); | 
|---|
| 805 |     middle.z = start->z + fraction1 * (end->z - start->z); | 
|---|
| 806 |  | 
|---|
| 807 |     // STEP 4: check the first side | 
|---|
| 808 |     //CheckNode( node->children[side], startFraction, middleFraction, start, middle ); | 
|---|
| 809 |     if(side == 0) this->checkCollisionRay(node->left,startFraction, middleFraction, start, &middle ); | 
|---|
| 810 |  | 
|---|
| 811 |     else this->checkCollisionRay(node->right,startFraction, middleFraction, | 
|---|
| 812 |                                    start, &middle ); | 
|---|
| 813 |  | 
|---|
| 814 |     // STEP 5: calculate the middle point for the second side | 
|---|
| 815 |     middleFraction = startFraction + | 
|---|
| 816 |                      (endFraction - startFraction) * fraction2; | 
|---|
| 817 |  | 
|---|
| 818 |     middle.x = start->x + fraction2 * (end->x - start->x); | 
|---|
| 819 |     middle.y = start->y + fraction2 * (end->y - start->y); | 
|---|
| 820 |     middle.z = start->z + fraction2 * (end->z - start->z); | 
|---|
| 821 |  | 
|---|
| 822 |     // STEP 6: check the second side | 
|---|
| 823 |     if(side == 1)this->checkCollisionRay(node->left,middleFraction, endFraction, &middle, end); | 
|---|
| 824 |  | 
|---|
| 825 |     else this->checkCollisionRay(node->right,middleFraction, endFraction,&middle, end ); | 
|---|
| 826 |  | 
|---|
| 827 |  | 
|---|
| 828 |   } | 
|---|
| 829 |  | 
|---|
| 830 | } | 
|---|
| 831 |  | 
|---|
| 832 |  | 
|---|
| 833 |  | 
|---|
| 834 | void BspManager::checkCollisionRayN(BspTreeNode* node, float startFraction, float endFraction, Vector* start, Vector* end) | 
|---|
| 835 | { | 
|---|
| 836 |  | 
|---|
| 837 |  | 
|---|
| 838 |   float EPSILON = 0.000001; | 
|---|
| 839 |  | 
|---|
| 840 |   float endDistance = end->dot(node->plane) - node->d; | 
|---|
| 841 |   float startDistance = start->dot(node->plane) - node->d; | 
|---|
| 842 |  | 
|---|
| 843 |  | 
|---|
| 844 |   if( node->isLeaf) { | 
|---|
| 845 |     leaf& curLeaf = this->bspFile->leaves[node->leafIndex]; | 
|---|
| 846 |     for (int i = 0; i <  curLeaf.n_leafbrushes ; i++) { | 
|---|
| 847 |       brush& curBrush = this->bspFile->brushes[((int*)(this->bspFile->leafBrushes))[curLeaf.leafbrush_first+i]]; | 
|---|
| 848 |       //object *brush = &BSP.brushes[BSP.leafBrushes[leaf->firstLeafBrush + i]]; | 
|---|
| 849 |       if (curBrush.n_brushsides > 0   && | 
|---|
| 850 |           ((((BspTexture*)(this->bspFile->textures))[curBrush.texture]).contents & 1)) | 
|---|
| 851 |         // CheckBrush( brush ); | 
|---|
| 852 |         this->checkBrushRayN(&curBrush); | 
|---|
| 853 |       if(curBrush.n_brushsides <=0) this->outputAllSolid = true; | 
|---|
| 854 |     } | 
|---|
| 855 |  | 
|---|
| 856 |     return; | 
|---|
| 857 |   } | 
|---|
| 858 |  | 
|---|
| 859 |  | 
|---|
| 860 |   if (startDistance >= 0 && endDistance >= 0)     // A | 
|---|
| 861 |   {   // both points are in front of the plane | 
|---|
| 862 |     // so check the front child | 
|---|
| 863 |     this->checkCollisionRayN(node->left,0,0,start,end); | 
|---|
| 864 |   } else if (startDistance < 0 && endDistance < 0)  // B | 
|---|
| 865 |   {   // both points are behind the plane | 
|---|
| 866 |     // so check the back child | 
|---|
| 867 |     this->checkCollisionRayN(node->right,0,0,start,end); | 
|---|
| 868 |   } else                                            // C | 
|---|
| 869 |   {   // the line spans the splitting plane | 
|---|
| 870 |     int side; | 
|---|
| 871 |     float fraction1, fraction2, middleFraction; | 
|---|
| 872 |     Vector middle; | 
|---|
| 873 |  | 
|---|
| 874 |     // STEP 1: split the segment into two | 
|---|
| 875 |     if (startDistance < endDistance) { | 
|---|
| 876 |       side = 1; // back | 
|---|
| 877 |       float inverseDistance = 1.0f / (startDistance - endDistance); | 
|---|
| 878 |       fraction1 = (startDistance + EPSILON) * inverseDistance; | 
|---|
| 879 |       fraction2 = (startDistance + EPSILON) * inverseDistance; | 
|---|
| 880 |     } else if (endDistance < startDistance) { | 
|---|
| 881 |       side = 0; // front(start)->x * (node->plane.x)+ | 
|---|
| 882 |       float inverseDistance = 1.0f / (startDistance - endDistance); | 
|---|
| 883 |       fraction1 = (startDistance + EPSILON) * inverseDistance; | 
|---|
| 884 |       fraction2 = (startDistance - EPSILON) * inverseDistance; | 
|---|
| 885 |     } else { | 
|---|
| 886 |       side = 0; // front | 
|---|
| 887 |       fraction1 = 1.0f; | 
|---|
| 888 |       fraction2 = 0.0f; | 
|---|
| 889 |     } | 
|---|
| 890 |  | 
|---|
| 891 |     // STEP 2: make sure the numbers are valid | 
|---|
| 892 |     if (fraction1 < 0.0f) fraction1 = 0.0f; | 
|---|
| 893 |     else if (fraction1 > 1.0f) fraction1 = 1.0f; | 
|---|
| 894 |     if (fraction2 < 0.0f) fraction2 = 0.0f; | 
|---|
| 895 |     else if (fraction2 > 1.0f) fraction2 = 1.0f; | 
|---|
| 896 |  | 
|---|
| 897 |     // STEP 3: calculate the middle point for the first side | 
|---|
| 898 |     middleFraction = startFraction + (endFraction - startFraction) * fraction1; | 
|---|
| 899 |     middle = (*start) + ((*end) - (*start)) * fraction1; | 
|---|
| 900 |  | 
|---|
| 901 |  | 
|---|
| 902 |     // STEP 4: check the first side | 
|---|
| 903 |     //CheckNode( node->children[side], startFraction, middleFraction, start, middle ); | 
|---|
| 904 |     if(side == 0) this->checkCollisionRayN(node->left,startFraction, middleFraction, start, &middle ); | 
|---|
| 905 |  | 
|---|
| 906 |     else this->checkCollisionRayN(node->right,startFraction, middleFraction, | 
|---|
| 907 |                                     start, &middle ); | 
|---|
| 908 |  | 
|---|
| 909 |     // STEP 5: calculate the middle point for the second side | 
|---|
| 910 |     middleFraction = startFraction + (endFraction - startFraction) * fraction2; | 
|---|
| 911 |     middle = (*start) + ((*end) - (*start)) * fraction2; | 
|---|
| 912 |  | 
|---|
| 913 |     // STEP 6: check the second side | 
|---|
| 914 |     if(side == 1)this->checkCollisionRayN(node->left,middleFraction, endFraction, &middle, end); | 
|---|
| 915 |  | 
|---|
| 916 |     else this->checkCollisionRayN(node->right,middleFraction, endFraction,&middle, end ); | 
|---|
| 917 |  | 
|---|
| 918 |  | 
|---|
| 919 |   } | 
|---|
| 920 |  | 
|---|
| 921 | } | 
|---|
| 922 |  | 
|---|
| 923 | float BspManager::checkPatchAltitude(BspTreeNode* node) | 
|---|
| 924 | { | 
|---|
| 925 |   leaf& curLeaf = this->bspFile->leaves[node->leafIndex]; | 
|---|
| 926 |   for(int i = 0; i < curLeaf.n_leaffaces ; i++) {} | 
|---|
| 927 |   return 10.0f; | 
|---|
| 928 | } | 
|---|
| 929 |  | 
|---|
| 930 | void BspManager::checkCollisionBox(void) | 
|---|
| 931 | {} | 
|---|
| 932 |  | 
|---|
| 933 |  | 
|---|
| 934 | void BspManager::TraceBox( Vector& inputStart, Vector& inputEnd, | 
|---|
| 935 |                            Vector& inputMins, Vector& inputMaxs ) | 
|---|
| 936 | { | 
|---|
| 937 |   if (inputMins.x == 0 && inputMins.y == 0 && inputMins.z == 0 && | 
|---|
| 938 |       inputMaxs.x == 0 && inputMaxs.y == 0 && inputMaxs.z == 0) {   // the user called TraceBox, but this is actually a ray | 
|---|
| 939 |     //!> FIXME TraceRay( inputStart, inputEnd ); | 
|---|
| 940 |   } else {   // setup for a box | 
|---|
| 941 |     //traceType = TT_BOX; | 
|---|
| 942 |     this->traceMins = inputMins; | 
|---|
| 943 |     this->traceMaxs = inputMaxs; | 
|---|
| 944 |     this->traceExtents.x = -traceMins.x > traceMaxs.x ? | 
|---|
| 945 |                            -traceMins.x : traceMaxs.x; | 
|---|
| 946 |     this->traceExtents.y = -traceMins.y > traceMaxs.y ? | 
|---|
| 947 |                            -traceMins.y : traceMaxs.y; | 
|---|
| 948 |     this->traceExtents.z = -traceMins.z > traceMaxs.z ? | 
|---|
| 949 |                            -traceMins.z : traceMaxs.z; | 
|---|
| 950 |     //!> FIXME Trace( inputStart, inputEnd ); | 
|---|
| 951 |   } | 
|---|
| 952 | } | 
|---|
| 953 |  | 
|---|
| 954 | void BspManager::checkCollision(WorldEntity* worldEntity) | 
|---|
| 955 | { | 
|---|
| 956 |  | 
|---|
| 957 |   // Init  Collision Detection | 
|---|
| 958 |   this->outputStartsOut = true; | 
|---|
| 959 |   this->outputAllSolid = false; | 
|---|
| 960 |   this->outputFraction = 1.0f; | 
|---|
| 961 |  | 
|---|
| 962 |   // Retrieve Bounding box | 
|---|
| 963 |   AABB* box = worldEntity->getModelAABB(); | 
|---|
| 964 |  | 
|---|
| 965 |  | 
|---|
| 966 |   Vector forwardDir = Vector(0.0,0.0,1.0); | 
|---|
| 967 |   Vector upDir = Vector(0.0,1.0,0.0); | 
|---|
| 968 |   Vector position = worldEntity->getAbsCoor(); | 
|---|
| 969 |  | 
|---|
| 970 |   bool SolidFlag = false; | 
|---|
| 971 |   bool collision = false; | 
|---|
| 972 |   Vector position1 = position; | 
|---|
| 973 |   Vector position2 = position + Vector(0.0,1.0,0.0); | 
|---|
| 974 |   Vector position3 = position; | 
|---|
| 975 |   Vector position4 = position + Vector(0.0,1.0,0.0); | 
|---|
| 976 |   Vector dest = worldEntity->getAbsCoor() - upDir*40.0f; // | 
|---|
| 977 |   Vector dest1 = position + forwardDir*4.0f; | 
|---|
| 978 |   Vector dest2 = position2 + forwardDir*4.0; | 
|---|
| 979 |   Vector dest3 = position + forwardDir*4.0f; | 
|---|
| 980 |   Vector dest4 = position2 + forwardDir*4.0; | 
|---|
| 981 |   dest = position - Vector(0.0, 40.0,0.0); | 
|---|
| 982 |   Vector out = dest; | 
|---|
| 983 |   Vector out1; | 
|---|
| 984 |   Vector out2; | 
|---|
| 985 |  | 
|---|
| 986 |  | 
|---|
| 987 |  | 
|---|
| 988 |   float height = 40; | 
|---|
| 989 |  | 
|---|
| 990 |  | 
|---|
| 991 |   if( box != NULL) { | 
|---|
| 992 |     position = worldEntity->getAbsCoor() +  box->center; // + Vector(0.0, 1.0, 0.0) * box->halfLength[1] * 1.0f; | 
|---|
| 993 |     dest     = worldEntity->getAbsCoor() +  box->center - Vector(0.0, 1.0, 0.0) * (box->halfLength[1] + BSP_Y_OFFSET) *   100; | 
|---|
| 994 |  | 
|---|
| 995 |     Vector dirX =  worldEntity->getAbsDirX(); dirX.y = 0.0f; dirX.normalize(); | 
|---|
| 996 |  | 
|---|
| 997 |     //position1 = worldEntity->getAbsCoor() +  box->center - dirX * (box->halfLength[0]  + BSP_X_OFFSET); | 
|---|
| 998 |     dest1     = worldEntity->getAbsCoor() +  box->center + dirX * (box->halfLength[0]  + BSP_X_OFFSET); | 
|---|
| 999 |     dest2     = worldEntity->getAbsCoor() -  box->center + dirX * (box->halfLength[0]  + BSP_X_OFFSET); | 
|---|
| 1000 |      | 
|---|
| 1001 |     Vector dirZ =  worldEntity->getAbsDirZ(); dirX.y = 0.0f; dirZ.normalize(); | 
|---|
| 1002 |     //position2 = worldEntity->getAbsCoor() +  box->center - dirZ * (box->halfLength[2]  + BSP_Z_OFFSET); | 
|---|
| 1003 |     dest3     = worldEntity->getAbsCoor() +  box->center + dirZ * (box->halfLength[2]  + BSP_Z_OFFSET); | 
|---|
| 1004 |     dest4     = worldEntity->getAbsCoor() -  box->center + dirZ * (box->halfLength[2]  + BSP_Z_OFFSET); | 
|---|
| 1005 |   } else { | 
|---|
| 1006 |     // Init positions and destinations to anything useful! | 
|---|
| 1007 |  | 
|---|
| 1008 |   } | 
|---|
| 1009 |  | 
|---|
| 1010 |  | 
|---|
| 1011 | //   PRINTF(0)("x and v\n"); | 
|---|
| 1012 | //   worldEntity->getAbsDirX().debug(); | 
|---|
| 1013 | //   worldEntity->getAbsDirV().debug(); | 
|---|
| 1014 |  | 
|---|
| 1015 |  | 
|---|
| 1016 |   // 1st Ray | 
|---|
| 1017 |   this->inputStart =  position; | 
|---|
| 1018 |   this->inputEnd =   dest; | 
|---|
| 1019 |   this->checkCollisionRayN(this->root,0.0f,1.0f, &position, &dest ); | 
|---|
| 1020 |  | 
|---|
| 1021 |  | 
|---|
| 1022 |   // | 
|---|
| 1023 |   if(!this->outputStartsOut ) { | 
|---|
| 1024 |     this->collPlane = new plane; | 
|---|
| 1025 |     this->collPlane->x = 0.0f; | 
|---|
| 1026 |     this->collPlane->y = 0.0f; | 
|---|
| 1027 |     this->collPlane->z = 0.0f; | 
|---|
| 1028 |     collision = true; | 
|---|
| 1029 |   } else { | 
|---|
| 1030 |  | 
|---|
| 1031 |     if( this->outputFraction == 1.0f) { | 
|---|
| 1032 |       if(this->outputAllSolid ) { | 
|---|
| 1033 |         this->collPlane = new plane; | 
|---|
| 1034 |         this->collPlane->x = 0.0f; | 
|---|
| 1035 |         this->collPlane->y = 0.0f; | 
|---|
| 1036 |         this->collPlane->z = 0.0f; | 
|---|
| 1037 |         collision = true; | 
|---|
| 1038 |         SolidFlag = true; | 
|---|
| 1039 |       } else | 
|---|
| 1040 |         collision = false; | 
|---|
| 1041 |  | 
|---|
| 1042 |  | 
|---|
| 1043 |       out = dest; | 
|---|
| 1044 |  | 
|---|
| 1045 |     } else { | 
|---|
| 1046 |       collision = true; | 
|---|
| 1047 |       out.x = position.x + (dest.x -position.x) * this->outputFraction; | 
|---|
| 1048 |       out.y = position.y + (dest.y -position.y) * this->outputFraction; | 
|---|
| 1049 |       out.z = position.z + (dest.z -position.z) * this->outputFraction; | 
|---|
| 1050 |  | 
|---|
| 1051 |       Vector out3 = out + Vector(height*this->collPlane->x,height*this->collPlane->y,height*this->collPlane->z); | 
|---|
| 1052 |       this->out = out; | 
|---|
| 1053 |     } | 
|---|
| 1054 |  | 
|---|
| 1055 |  | 
|---|
| 1056 |   } | 
|---|
| 1057 |  | 
|---|
| 1058 |   plane* testPlane = this->collPlane; | 
|---|
| 1059 |  | 
|---|
| 1060 |   bool xCollision = false; | 
|---|
| 1061 |   bool zCollision = false; | 
|---|
| 1062 |   bool xCollisionNeg = false; | 
|---|
| 1063 |   bool zCollisionNeg = false; | 
|---|
| 1064 |    | 
|---|
| 1065 |  | 
|---|
| 1066 |  | 
|---|
| 1067 |     // 2nd Collision Detection | 
|---|
| 1068 |     this->outputStartsOut = true; | 
|---|
| 1069 |     this->outputAllSolid = false; | 
|---|
| 1070 |     this->outputFraction = 1.0f; | 
|---|
| 1071 |     this->inputStart =  position1; | 
|---|
| 1072 |     this->inputEnd =   dest1; | 
|---|
| 1073 |     this->checkCollisionRayN(this->root,0.0f,1.0f, &position1, &dest1 ); | 
|---|
| 1074 |  | 
|---|
| 1075 |     if(this->outputFraction < 1.0f) { | 
|---|
| 1076 |       out.x = dest1.x + (dest1.x -position1.x) * this->outputFraction; | 
|---|
| 1077 |       dest1 = position1 + (dest1 -position1) * this->outputFraction; | 
|---|
| 1078 |       xCollision = true; | 
|---|
| 1079 |       testPlane = this->collPlane; | 
|---|
| 1080 |     } | 
|---|
| 1081 |     if(this->outputAllSolid ) { | 
|---|
| 1082 |  | 
|---|
| 1083 |       this->collPlane = new plane; | 
|---|
| 1084 |       this->collPlane->x = 0.0f; | 
|---|
| 1085 |       this->collPlane->y = 0.0f; | 
|---|
| 1086 |       this->collPlane->z = 0.0f; | 
|---|
| 1087 |       testPlane = this->collPlane; | 
|---|
| 1088 |       SolidFlag = true; | 
|---|
| 1089 |       xCollision = true; | 
|---|
| 1090 |     } | 
|---|
| 1091 |     //out.z = this->outputFraction; | 
|---|
| 1092 |  | 
|---|
| 1093 |  | 
|---|
| 1094 |  | 
|---|
| 1095 |       // 3rd Collision Detection | 
|---|
| 1096 |       this->outputStartsOut = true; | 
|---|
| 1097 |       this->outputAllSolid = false; | 
|---|
| 1098 |       this->outputFraction = 1.0f; | 
|---|
| 1099 |       this->inputStart =  position2; | 
|---|
| 1100 |       this->inputEnd =   dest2; | 
|---|
| 1101 |  | 
|---|
| 1102 |       this->checkCollisionRayN(this->root,0.0f,1.0f, &position2, &dest2 ); | 
|---|
| 1103 |       //out.x = this->outputFraction; | 
|---|
| 1104 |  | 
|---|
| 1105 |       if(this->outputFraction < 1.0f ) { | 
|---|
| 1106 |         out.z = out.z = dest2.z + (dest2.z -position2.z) * this->outputFraction; | 
|---|
| 1107 |         dest2 = position2 + (dest2 -position2) * this->outputFraction; | 
|---|
| 1108 |         zCollision = true; | 
|---|
| 1109 |         testPlane = this->collPlane; | 
|---|
| 1110 |  | 
|---|
| 1111 |       } | 
|---|
| 1112 |       if(this->outputAllSolid ) { | 
|---|
| 1113 |         this->collPlane = new plane; | 
|---|
| 1114 |         this->collPlane->x = 0.0f; | 
|---|
| 1115 |         this->collPlane->y = 0.0f; | 
|---|
| 1116 |         this->collPlane->z = 0.0f; | 
|---|
| 1117 |         testPlane = this->collPlane; | 
|---|
| 1118 |  | 
|---|
| 1119 |         SolidFlag = true; | 
|---|
| 1120 |         zCollision = true; | 
|---|
| 1121 |       } | 
|---|
| 1122 |  | 
|---|
| 1123 |   //end if | 
|---|
| 1124 |   /* | 
|---|
| 1125 |   This is how you would calculate the Coordinates where worldEntity Collided with the BSP world. | 
|---|
| 1126 |   out.x = position1.x + (dest.x -position1.x) * this->outputFraction; | 
|---|
| 1127 |   out.z = position1.z + (dest.z -position1.z) * this->outputFraction; | 
|---|
| 1128 |   */ | 
|---|
| 1129 |  | 
|---|
| 1130 |  | 
|---|
| 1131 |   // Return the normal here: Normal's stored in this->collPlane; | 
|---|
| 1132 |   if( collision) { | 
|---|
| 1133 |     worldEntity->registerCollision(COLLISION_TYPE_AXIS_Y_NEG , this->parent, worldEntity, Vector(testPlane->x, testPlane->y, testPlane->z), out, SolidFlag); | 
|---|
| 1134 | } | 
|---|
| 1135 |   if(xCollision) { | 
|---|
| 1136 |     worldEntity->registerCollision(COLLISION_TYPE_AXIS_X , this->parent, worldEntity, Vector(testPlane->x, testPlane->y, testPlane->z),dest1 , SolidFlag); | 
|---|
| 1137 |   } | 
|---|
| 1138 |    | 
|---|
| 1139 |   if(zCollision) { | 
|---|
| 1140 |     worldEntity->registerCollision(COLLISION_TYPE_AXIS_Z , this->parent, worldEntity, Vector(testPlane->x, testPlane->y, testPlane->z), dest2 , SolidFlag); | 
|---|
| 1141 |   } | 
|---|
| 1142 |  | 
|---|
| 1143 |  | 
|---|
| 1144 |   //else  worldEntity->registerCollision(COLLISION_TYPE_AXIS_Y, this->parent, worldEntity, Vector(0.0, 2.0, 0.0), dest, false); | 
|---|
| 1145 |  | 
|---|
| 1146 | } | 
|---|
| 1147 |  | 
|---|
| 1148 |  | 
|---|
| 1149 | void  BspManager::checkCollision(BspTreeNode* node, Vector* cam) | 
|---|
| 1150 | { | 
|---|
| 1151 |   Vector next = this->cam; | 
|---|
| 1152 |   next.x =   (State::getCameraTargetNode()->getLastAbsCoor()).x ; | 
|---|
| 1153 |   next.y =   (State::getCameraTargetNode()->getLastAbsCoor()).y ; | 
|---|
| 1154 |   next.z =   (State::getCameraTargetNode()->getLastAbsCoor()).z ; | 
|---|
| 1155 |  | 
|---|
| 1156 |   float dist = 0; | 
|---|
| 1157 |   if(!(node->isLeaf)) { | 
|---|
| 1158 |     dist = (node->plane.x * this->cam.x + node->plane.y*this->cam.y + node->plane.z*this->cam.z) - node->d; | 
|---|
| 1159 |     if(dist > 4.0f) { | 
|---|
| 1160 |       checkCollision(node->left,cam); | 
|---|
| 1161 |       return; | 
|---|
| 1162 |     } | 
|---|
| 1163 |     if(dist < -4.0f) { | 
|---|
| 1164 |       checkCollision(node->right,cam); | 
|---|
| 1165 |       return; | 
|---|
| 1166 |     } | 
|---|
| 1167 |     if(dist<=4.0f && dist >= -4.0f) { | 
|---|
| 1168 |       checkCollision(node->left,cam); | 
|---|
| 1169 |       checkCollision(node->right,cam); | 
|---|
| 1170 |       return; | 
|---|
| 1171 |     } | 
|---|
| 1172 |     return; | 
|---|
| 1173 |   } else { | 
|---|
| 1174 |  | 
|---|
| 1175 |     leaf& camLeaf =  ((leaf *)(this->bspFile->leaves))[(node->leafIndex ) ]; | 
|---|
| 1176 |  | 
|---|
| 1177 |     if (camLeaf.cluster < 0) { | 
|---|
| 1178 |       this->drawDebugCube(&this->cam); | 
|---|
| 1179 |       this->drawDebugCube(&next); | 
|---|
| 1180 |       // State::getPlayer()->getPlayable()->setRelCoor(-100,-100,-100); | 
|---|
| 1181 |       //State::getPlayer()->getPlayable()->collidesWith(NULL, State::getCameraTargetNode()->getLastAbsCoor()); | 
|---|
| 1182 |     } | 
|---|
| 1183 |  | 
|---|
| 1184 |  | 
|---|
| 1185 |     /* | 
|---|
| 1186 |         for(int i = 0; i < camLeaf.n_leafbrushes && i < 10; i++ ) | 
|---|
| 1187 |         { | 
|---|
| 1188 |                 brush& curBrush = ((brush*)(this->bspFile->brushes))[(camLeaf.leafbrush_first +i)%this->bspFile->numLeafBrushes]; | 
|---|
| 1189 |                 if(curBrush.n_brushsides < 0) return; | 
|---|
| 1190 |                 for(int j = 0; j < curBrush.n_brushsides; j++) | 
|---|
| 1191 |                 { | 
|---|
| 1192 |                 float dist = -0.1; | 
|---|
| 1193 |                 brushside& curBrushSide = ((brushside*)(this->bspFile->brushSides))[(curBrush.brushside +j)%this->bspFile->numBrushSides]; | 
|---|
| 1194 |                 plane&      testPlane = ((plane*)(this->bspFile->planes))[curBrushSide.plane % this->bspFile->numPlanes]; | 
|---|
| 1195 |                 dist = testPlane.x * this->cam.x +  testPlane.y * this->cam.y  +  testPlane.z * this->cam.z   -testPlane.d ; | 
|---|
| 1196 |  | 
|---|
| 1197 |                 if(dist < -0.01f) dist = -1.0f *dist; | 
|---|
| 1198 |                 if(dist < 1.0f){ | 
|---|
| 1199 |                                 this->drawDebugCube(&this->cam); | 
|---|
| 1200 |                                 return; | 
|---|
| 1201 |                               } | 
|---|
| 1202 |                 } | 
|---|
| 1203 |  | 
|---|
| 1204 |         } */ | 
|---|
| 1205 |  | 
|---|
| 1206 |   } | 
|---|
| 1207 |   return; | 
|---|
| 1208 | } | 
|---|
| 1209 |  | 
|---|
| 1210 | void BspManager::drawDebugCube(Vector* cam) | 
|---|
| 1211 | { | 
|---|
| 1212 |   glBegin(GL_QUADS); | 
|---|
| 1213 |  | 
|---|
| 1214 |   // Bottom Face.  Red, 75% opaque, magnified texture | 
|---|
| 1215 |  | 
|---|
| 1216 |   glNormal3f( 0.0f, -1.0f, 0.0f); // Needed for lighting | 
|---|
| 1217 |   glColor4f(0.9,0.2,0.2,.75); // Basic polygon color | 
|---|
| 1218 |  | 
|---|
| 1219 |   glTexCoord2f(0.800f, 0.800f); glVertex3f(cam->x-1.0f, cam->y-1.0f,cam->z -1.0f); | 
|---|
| 1220 |   glTexCoord2f(0.200f, 0.800f); glVertex3f(cam->x+1.0f, cam->y-1.0f,cam->z -1.0f); | 
|---|
| 1221 |   glTexCoord2f(0.200f, 0.200f); glVertex3f(cam->x+ 1.0f,cam->y -1.0f,cam->z +  1.0f); | 
|---|
| 1222 |   glTexCoord2f(0.800f, 0.200f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z + 1.0f); | 
|---|
| 1223 |  | 
|---|
| 1224 |  | 
|---|
| 1225 |   // Top face; offset.  White, 50% opaque. | 
|---|
| 1226 |  | 
|---|
| 1227 |   glNormal3f( 0.0f, 1.0f, 0.0f);  glColor4f(0.5,0.5,0.5,.5); | 
|---|
| 1228 |  | 
|---|
| 1229 |   glTexCoord2f(0.005f, 1.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.0f); | 
|---|
| 1230 |   glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f,  cam->z +1.0f); | 
|---|
| 1231 |   glTexCoord2f(1.995f, 0.005f); glVertex3f(cam->x+ 1.0f,  cam->y+1.0f,  cam->z +1.0f); | 
|---|
| 1232 |   glTexCoord2f(1.995f, 1.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z -1.0f); | 
|---|
| 1233 |  | 
|---|
| 1234 |  | 
|---|
| 1235 |   // Far face.  Green, 50% opaque, non-uniform texture cooridinates. | 
|---|
| 1236 |  | 
|---|
| 1237 |   glNormal3f( 0.0f, 0.0f,-1.0f);  glColor4f(0.2,0.9,0.2,.5); | 
|---|
| 1238 |  | 
|---|
| 1239 |   glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z -1.3f); | 
|---|
| 1240 |   glTexCoord2f(2.995f, 2.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.3f); | 
|---|
| 1241 |   glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x+ 1.0f,cam->y+  1.0f, cam->z -1.3f); | 
|---|
| 1242 |   glTexCoord2f(0.005f, 0.005f); glVertex3f( cam->x+1.0f,cam->y -1.0f, cam->z -1.3f); | 
|---|
| 1243 |  | 
|---|
| 1244 |  | 
|---|
| 1245 |   // Right face.  Blue; 25% opaque | 
|---|
| 1246 |  | 
|---|
| 1247 |   glNormal3f( 1.0f, 0.0f, 0.0f);  glColor4f(0.2,0.2,0.9,.25); | 
|---|
| 1248 |  | 
|---|
| 1249 |   glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y -1.0f, cam->z -1.0f); | 
|---|
| 1250 |   glTexCoord2f(0.995f, 0.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z -1.0f); | 
|---|
| 1251 |   glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z + 1.0f); | 
|---|
| 1252 |   glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y-1.0f,  cam->z +1.0f); | 
|---|
| 1253 |  | 
|---|
| 1254 |  | 
|---|
| 1255 |   // Front face; offset.  Multi-colored, 50% opaque. | 
|---|
| 1256 |  | 
|---|
| 1257 |   glNormal3f( 0.0f, 0.0f, 1.0f); | 
|---|
| 1258 |  | 
|---|
| 1259 |   glColor4f( 0.9f, 0.2f, 0.2f, 0.5f); | 
|---|
| 1260 |   glTexCoord2f( 0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f,  cam->z +1.0f); | 
|---|
| 1261 |   glColor4f( 0.2f, 0.9f, 0.2f, 0.5f); | 
|---|
| 1262 |   glTexCoord2f( 0.995f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y-1.0f,  cam->z +1.0f); | 
|---|
| 1263 |   glColor4f( 0.2f, 0.2f, 0.9f, 0.5f); | 
|---|
| 1264 |   glTexCoord2f( 0.995f, 0.995f); glVertex3f( cam->x+1.0f,  cam->y+1.0f,  cam->z +1.0f); | 
|---|
| 1265 |   glColor4f( 0.1f, 0.1f, 0.1f, 0.5f); | 
|---|
| 1266 |   glTexCoord2f( 0.005f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f,  cam->z +1.0f); | 
|---|
| 1267 |  | 
|---|
| 1268 |  | 
|---|
| 1269 |   // Left Face; offset.  Yellow, varying levels of opaque. | 
|---|
| 1270 |  | 
|---|
| 1271 |   glNormal3f(-1.0f, 0.0f, 0.0f); | 
|---|
| 1272 |  | 
|---|
| 1273 |   glColor4f(0.9,0.9,0.2,0.0); | 
|---|
| 1274 |   glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z -1.0f); | 
|---|
| 1275 |   glColor4f(0.9,0.9,0.2,0.66); | 
|---|
| 1276 |   glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x-1.0f,cam->y -1.0f,  cam->z +1.0f); | 
|---|
| 1277 |   glColor4f(0.9,0.9,0.2,1.0); | 
|---|
| 1278 |   glTexCoord2f(0.995f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f,  cam->z +1.0f); | 
|---|
| 1279 |   glColor4f(0.9,0.9,0.2,0.33); | 
|---|
| 1280 |   glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.0f); | 
|---|
| 1281 |  | 
|---|
| 1282 |   glEnd(); | 
|---|
| 1283 | } | 
|---|
| 1284 |  | 
|---|
| 1285 | void BspManager::addFace(int f) | 
|---|
| 1286 | { | 
|---|
| 1287 |   face& curFace =  ((face *)(this->bspFile->faces))[f]; | 
|---|
| 1288 |   if(this->bspFile->Materials[curFace.texture].alpha) this->trasparent.push_back(f); | 
|---|
| 1289 |   else this->opal.push_back(f); | 
|---|
| 1290 | } | 
|---|