[7353] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
[9003] | 3 | |
---|
[7353] | 4 | Copyright (C) 2006 orx |
---|
[9003] | 5 | |
---|
[7353] | 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. |
---|
[9003] | 10 | |
---|
[7353] | 11 | ### File Specific: |
---|
| 12 | main-programmer: bottac@ee.ethz.ch |
---|
[9003] | 13 | |
---|
[8081] | 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/ |
---|
[9003] | 17 | |
---|
[8081] | 18 | Collision detection adapted from: |
---|
| 19 | Quake 3 Collision Detection by Nathan Ostgard http://www.devmaster.net/articles/quake3collision/ |
---|
[7353] | 20 | */ |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | #include "vector.h" |
---|
| 24 | #include "bsp_file.h" |
---|
| 25 | #include "bsp_manager.h" |
---|
[7385] | 26 | #include "bsp_tree_leaf.h" |
---|
[7353] | 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" |
---|
[7579] | 33 | #include "world_entities/player.h" |
---|
| 34 | #include "world_entities/playable.h" |
---|
[7596] | 35 | #include "util/loading/resource_manager.h" |
---|
[7385] | 36 | // STL Containers |
---|
| 37 | #include <vector> |
---|
| 38 | #include <deque> |
---|
[7801] | 39 | #include "movie_player.h" |
---|
[7353] | 40 | |
---|
[7833] | 41 | #include "world_entity.h" |
---|
[7353] | 42 | |
---|
[8081] | 43 | #include "util/loading/load_param.h" |
---|
| 44 | #include "util/loading/factory.h" |
---|
| 45 | |
---|
[8796] | 46 | #include "aabb.h" |
---|
[8894] | 47 | #include "cr_defs.h" |
---|
[8081] | 48 | |
---|
[8490] | 49 | |
---|
[8081] | 50 | //CREATE_FACTORY( BspManager, CL_BSP_MODEL); |
---|
| 51 | |
---|
[8490] | 52 | BspManager::BspManager(WorldEntity* parent) |
---|
[7353] | 53 | { |
---|
[8724] | 54 | |
---|
[8490] | 55 | this->parent = parent; |
---|
[8081] | 56 | /*// open a BSP file |
---|
[7395] | 57 | this->bspFile = new BspFile(); |
---|
[8081] | 58 | this->bspFile->scale = 0.4f; |
---|
[7596] | 59 | this->bspFile->read(ResourceManager::getFullName("test.bsp").c_str()); |
---|
[7395] | 60 | this->bspFile->build_tree(); |
---|
| 61 | this->root = this->bspFile->get_root(); |
---|
| 62 | this->alreadyVisible = new bool [this->bspFile->numFaces]; |
---|
[8081] | 63 | */ |
---|
[8490] | 64 | |
---|
[8081] | 65 | } |
---|
[7833] | 66 | |
---|
[8081] | 67 | |
---|
| 68 | /* |
---|
| 69 | BspManager::BspManager(const TiXmlElement* root) |
---|
| 70 | { |
---|
[9003] | 71 | |
---|
| 72 | |
---|
[8081] | 73 | if( root != NULL) |
---|
| 74 | this->loadParams(root); |
---|
[9003] | 75 | |
---|
[7833] | 76 | CDEngine::getInstance()->setBSPModel(this); |
---|
[8081] | 77 | } */ |
---|
| 78 | |
---|
[9003] | 79 | BspManager::~BspManager() |
---|
[8081] | 80 | { |
---|
[9003] | 81 | if(this->bspFile) |
---|
| 82 | delete this->bspFile; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | int BspManager::load(const char* fileName, float scale) |
---|
| 86 | { |
---|
[8490] | 87 | // open a BSP file |
---|
[9003] | 88 | |
---|
| 89 | |
---|
[8081] | 90 | this->bspFile = new BspFile(); |
---|
| 91 | this->bspFile->scale = scale; |
---|
[9003] | 92 | if(this->bspFile->read(ResourceManager::getFullName(fileName).c_str()) == -1) |
---|
| 93 | return -1; |
---|
| 94 | |
---|
[8081] | 95 | this->bspFile->build_tree(); |
---|
| 96 | this->root = this->bspFile->get_root(); |
---|
| 97 | this->alreadyVisible = new bool [this->bspFile->numFaces]; |
---|
| 98 | |
---|
[8490] | 99 | this->outputFraction = 1.0f; |
---|
[9003] | 100 | |
---|
| 101 | return 0; |
---|
[7353] | 102 | } |
---|
[8081] | 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]; |
---|
[9003] | 113 | |
---|
[8081] | 114 | CDEngine::getInstance()->setBSPModel(this); |
---|
| 115 | } |
---|
| 116 | */ |
---|
| 117 | |
---|
[8490] | 118 | const void BspManager::tick(float time) |
---|
| 119 | { |
---|
| 120 | |
---|
[8796] | 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 ); |
---|
[8490] | 128 | |
---|
| 129 | |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | } |
---|
[8081] | 133 | const void BspManager::draw() |
---|
[7353] | 134 | { |
---|
| 135 | |
---|
[8081] | 136 | /* |
---|
[8030] | 137 | this->drawDebugCube(&this->out); |
---|
| 138 | this->out1 = this->out; |
---|
| 139 | this->out2 = this->out; |
---|
[8490] | 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; |
---|
[8087] | 144 | |
---|
[8490] | 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; |
---|
[8030] | 148 | } |
---|
| 149 | this->drawDebugCube(&this->out1); |
---|
| 150 | this->drawDebugCube(&this->out2); |
---|
[8490] | 151 | |
---|
[8087] | 152 | */ |
---|
[8490] | 153 | |
---|
| 154 | |
---|
[7395] | 155 | // Draw Debug Terrain |
---|
| 156 | /* |
---|
[8087] | 157 | this->bspFile->Materials[0]->select(); |
---|
[7395] | 158 | for(int i = 0; i < this->bspFile->numPatches ; i++) |
---|
[8087] | 159 | { |
---|
| 160 | this->bspFile->VertexArrayModels[i]->draw(); |
---|
| 161 | |
---|
| 162 | } |
---|
[7395] | 163 | */ |
---|
[7353] | 164 | |
---|
| 165 | |
---|
| 166 | |
---|
[7395] | 167 | // erase alreadyVisible |
---|
| 168 | for(int i = 0; i < this->bspFile->numFaces; i++) this->alreadyVisible[i] = false; |
---|
| 169 | float tmp = 0; |
---|
[7579] | 170 | //this->opal.clear(); |
---|
| 171 | //this->trasparent.clear(); |
---|
[7353] | 172 | // Find all visible faces... |
---|
| 173 | |
---|
[7395] | 174 | this->cam = State::getCamera()->getAbsCoor() ; |
---|
[7579] | 175 | //this->ship = State::getCameraTargetNode()->getAbsCoor(); |
---|
[7353] | 176 | |
---|
| 177 | |
---|
| 178 | |
---|
| 179 | |
---|
[7833] | 180 | |
---|
[8490] | 181 | this->viewDir= State::getCamera()->getAbsDirX(); |
---|
[7395] | 182 | float d = (cam.x*viewDir.x + cam.y*viewDir.y + cam.z * viewDir.z); |
---|
[7353] | 183 | |
---|
[7395] | 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) |
---|
[7353] | 187 | |
---|
| 188 | |
---|
| 189 | |
---|
[8490] | 190 | |
---|
[8796] | 191 | // this->checkCollision(this->root, &this->cam); //!< Test Collision Detection |
---|
[8490] | 192 | |
---|
| 193 | |
---|
[7563] | 194 | this->outputStartsOut = true; |
---|
| 195 | this->outputAllSolid = false; |
---|
| 196 | this->outputFraction = 1.0f; |
---|
[7833] | 197 | |
---|
[7801] | 198 | if ( viscluster < 0 || ((int *)(this->bspFile->header))[35] == 0 ) //!< if (sizeof(Visdata) == 0) |
---|
[7395] | 199 | { |
---|
[7833] | 200 | |
---|
| 201 | |
---|
| 202 | |
---|
[8490] | 203 | // Iterate through all Leafs |
---|
[7395] | 204 | for(int i = 0; i < this->bspFile->numLeafs ; i++ ) |
---|
[7353] | 205 | { |
---|
[7395] | 206 | // cluster = (this->bspFile->leaves)[i].cluster; |
---|
| 207 | leaf& curLeaf = (this->bspFile->leaves)[i]; |
---|
[7563] | 208 | if(curLeaf.cluster<0) continue; |
---|
[7833] | 209 | |
---|
[7563] | 210 | /** Do Frustum culling and draw 'em all **/ |
---|
[7801] | 211 | |
---|
[8490] | 212 | Vector dir = State::getCameraNode()->getAbsDirX(); |
---|
| 213 | |
---|
[7563] | 214 | float dist = dir.x*this->cam.x +dir.y*this->cam.y +dir.z*this->cam.z; |
---|
| 215 | //if(dist < 0) dist = -dist; |
---|
[7801] | 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 ; |
---|
[7833] | 218 | |
---|
[8490] | 219 | if(dMins < -300.0 && dMaxs < -300.0) { |
---|
[8030] | 220 | continue; |
---|
[7563] | 221 | } |
---|
[8490] | 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) { |
---|
[8030] | 223 | continue; |
---|
[7801] | 224 | } |
---|
[7833] | 225 | |
---|
| 226 | |
---|
[7395] | 227 | // Iterate through all faces |
---|
| 228 | for (int j = 0; j < curLeaf.n_leaffaces ; ++j) { |
---|
[7596] | 229 | const int g = (j + curLeaf.leafface); |
---|
| 230 | const int f = ((int *)this->bspFile->leafFaces)[g]; |
---|
[7395] | 231 | if (f >=0 && !this->isAlreadyVisible(f)) { |
---|
| 232 | this->alreadyVisible[f] = true; |
---|
| 233 | addFace(f); // "visibleFaces.append(f)" |
---|
| 234 | } |
---|
| 235 | } |
---|
[7353] | 236 | |
---|
| 237 | |
---|
| 238 | |
---|
| 239 | |
---|
[7395] | 240 | } //for |
---|
[7833] | 241 | } else { |
---|
[7579] | 242 | |
---|
[7833] | 243 | |
---|
[7395] | 244 | unsigned int v; |
---|
| 245 | unsigned char visSet; |
---|
[7353] | 246 | |
---|
[7395] | 247 | // Iterate through all Leafs |
---|
[7385] | 248 | |
---|
[7395] | 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] ; |
---|
[7579] | 252 | int& cluster = curLeaf.cluster; |
---|
[7563] | 253 | |
---|
| 254 | if(cluster < 0) continue; |
---|
[7395] | 255 | v = ((viscluster * ( ((int *)this->bspFile->visData)[1]) ) + (cluster / 8)); |
---|
[7579] | 256 | visSet =((char*) (this->bspFile->visData))[v + 8]; |
---|
[7563] | 257 | |
---|
[7833] | 258 | // gets bit of visSet |
---|
[7579] | 259 | if( ((visSet) & (1 << (cluster & 7))) != 0 ) { |
---|
[7833] | 260 | |
---|
[7592] | 261 | // Frustum culling |
---|
[7833] | 262 | |
---|
[7592] | 263 | Vector dir; |
---|
[7833] | 264 | dir.x = State::getCameraNode()->getAbsDirX().x; |
---|
| 265 | dir.y = State::getCameraNode()->getAbsDirX().y; |
---|
| 266 | dir.z = State::getCameraNode()->getAbsDirX().z; |
---|
[7592] | 267 | const float dist = dir.x*this->cam.x +dir.y*this->cam.y +dir.z*this->cam.z; |
---|
[7833] | 268 | //if(dist < 0) dist = -dist; |
---|
[7592] | 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; |
---|
[7833] | 271 | |
---|
[8081] | 272 | if(dMins < -50.0 && dMaxs < - 50.0) { |
---|
[7833] | 273 | continue; |
---|
[7592] | 274 | } |
---|
[7833] | 275 | |
---|
| 276 | |
---|
[7395] | 277 | // Iterate through all faces |
---|
[7563] | 278 | for (int j = 0; j < curLeaf.n_leaffaces ; ++j) { |
---|
[7579] | 279 | const int g = (j + curLeaf.leafface); |
---|
| 280 | const int f = ((int *)this->bspFile->leafFaces)[g]; |
---|
[7395] | 281 | |
---|
| 282 | if (!this->isAlreadyVisible(f) && f>=0) { |
---|
| 283 | this->addFace(f); |
---|
| 284 | this->alreadyVisible[f] = true; |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | } |
---|
| 288 | |
---|
[7563] | 289 | }// if |
---|
[7395] | 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 | } |
---|
[8081] | 303 | //glEnable(GL_TEXTURE_2D); |
---|
[7801] | 304 | glActiveTextureARB(GL_TEXTURE1_ARB); |
---|
| 305 | glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap); |
---|
[7395] | 306 | |
---|
[8087] | 307 | |
---|
| 308 | |
---|
[7353] | 309 | }//draw |
---|
| 310 | |
---|
| 311 | |
---|
[7563] | 312 | |
---|
[7353] | 313 | void BspManager::draw_face(int curface) |
---|
| 314 | { |
---|
[7395] | 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; |
---|
[7801] | 319 | if (curFace.effect != -1) return; |
---|
[7353] | 320 | // PRINTF(0)("BSP Manager: "); |
---|
| 321 | // PRINTF(0)("BSP Manager: type: %i \n", curFace.texture); |
---|
[7395] | 322 | |
---|
| 323 | // if( curFace.texture < 0 ) return; |
---|
| 324 | if(curFace.type == 2) { |
---|
| 325 | this->draw_patch( &curFace); |
---|
| 326 | return; |
---|
| 327 | } |
---|
[8796] | 328 | // if(curFace.type != 1) return; |
---|
[7801] | 329 | if((char*)(this->bspFile->textures)[curFace.texture*72]== 0) return; |
---|
[7563] | 330 | |
---|
[7544] | 331 | if(this->lastTex != curFace.texture) { |
---|
[7833] | 332 | if(this->bspFile->Materials[curFace.texture].animated) { |
---|
[8796] | 333 | // glBlendFunc(GL_ZERO,GL_ONE); |
---|
[8490] | 334 | |
---|
| 335 | |
---|
| 336 | |
---|
[7801] | 337 | if(this->bspFile->Materials[curFace.texture].aviMat->getStatus() == 2) this->bspFile->Materials[curFace.texture].aviMat->start(0); |
---|
[8490] | 338 | //this->bspFile->Materials[curFace.texture].aviMat->tick(0.005); |
---|
[7801] | 339 | int n = this->bspFile->Materials[curFace.texture].aviMat->getTexture(); |
---|
| 340 | glActiveTextureARB(GL_TEXTURE0_ARB); |
---|
| 341 | glBindTexture(GL_TEXTURE_2D, n ); |
---|
[8490] | 342 | this->lastTex = curFace.texture; |
---|
| 343 | |
---|
[7833] | 344 | } else { |
---|
| 345 | this->bspFile->Materials[curFace.texture].mat->select(); |
---|
| 346 | this->lastTex = curFace.texture; |
---|
[7801] | 347 | } |
---|
[7544] | 348 | } |
---|
[7563] | 349 | |
---|
| 350 | if(curFace.lm_index < 0) { |
---|
[8490] | 351 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
---|
[7465] | 352 | glActiveTextureARB(GL_TEXTURE1_ARB); |
---|
[7563] | 353 | glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap ); |
---|
| 354 | glEnable(GL_TEXTURE_2D); |
---|
| 355 | } else { |
---|
[8796] | 356 | // glEnable(GL_BLEND); |
---|
[8490] | 357 | //glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
---|
| 358 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
---|
[7563] | 359 | glActiveTextureARB(GL_TEXTURE1_ARB); |
---|
[7465] | 360 | glBindTexture(GL_TEXTURE_2D, this->bspFile->glLightMapTextures[curFace.lm_index]); |
---|
| 361 | glEnable(GL_TEXTURE_2D); |
---|
[8796] | 362 | // glDisable(GL_BLEND); |
---|
[7563] | 363 | } |
---|
| 364 | |
---|
[7801] | 365 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
---|
[7833] | 366 | |
---|
| 367 | // glColor4f(3.0,3.0,3.0,1.0); |
---|
[7544] | 368 | glEnableClientState(GL_VERTEX_ARRAY ); |
---|
| 369 | glEnableClientState(GL_TEXTURE_COORD_ARRAY ); |
---|
[7353] | 370 | glEnableClientState(GL_NORMAL_ARRAY ); |
---|
[7395] | 371 | // glEnableClientState(GL_COLOR_ARRAY); |
---|
| 372 | |
---|
[7563] | 373 | |
---|
[7353] | 374 | glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0])); |
---|
[7563] | 375 | |
---|
[7465] | 376 | glClientActiveTextureARB(GL_TEXTURE0_ARB); |
---|
[7353] | 377 | glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0])); |
---|
[7801] | 378 | //glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
[7563] | 379 | |
---|
[7465] | 380 | glClientActiveTextureARB(GL_TEXTURE1_ARB); |
---|
| 381 | glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1])); |
---|
[7544] | 382 | //glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
[7353] | 383 | |
---|
[7563] | 384 | |
---|
[7353] | 385 | glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0])); |
---|
[7395] | 386 | // glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0])); |
---|
[7353] | 387 | glDrawElements(GL_TRIANGLES, curFace.n_meshverts, |
---|
[7395] | 388 | GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert])); |
---|
[7353] | 389 | |
---|
[7465] | 390 | glDisableClientState(GL_TEXTURE0_ARB); |
---|
| 391 | glDisableClientState(GL_TEXTURE1_ARB); |
---|
[7353] | 392 | glDisableClientState(GL_VERTEX_ARRAY ); |
---|
[7801] | 393 | glDisableClientState(GL_TEXTURE_COORD_ARRAY ); |
---|
[7353] | 394 | glDisableClientState(GL_NORMAL_ARRAY ); |
---|
[7395] | 395 | // glDisableClientState(GL_COLOR_ARRAY); |
---|
| 396 | |
---|
[7353] | 397 | } |
---|
| 398 | |
---|
| 399 | |
---|
| 400 | void BspManager::draw_debug_face(int curface) |
---|
| 401 | { |
---|
[7395] | 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 | |
---|
[7353] | 407 | // PRINTF(0)("BSP Manager: "); |
---|
| 408 | // PRINTF(0)("BSP Manager: type: %i \n", curFace.texture); |
---|
| 409 | |
---|
[7395] | 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) |
---|
[7353] | 417 | |
---|
[7395] | 418 | this->bspFile->Materials[2].mat->select(); |
---|
| 419 | this->lastTex = 2; |
---|
| 420 | |
---|
[7353] | 421 | glEnableClientState(GL_VERTEX_ARRAY ); |
---|
| 422 | glEnableClientState(GL_TEXTURE_COORD_ARRAY ); |
---|
| 423 | glEnableClientState(GL_NORMAL_ARRAY ); |
---|
[7395] | 424 | //glEnableClientState(GL_COLOR_ARRAY); |
---|
[7353] | 425 | // glEnableClientState(GL_VERTEX_ARRAY ); |
---|
[7465] | 426 | glClientActiveTextureARB(GL_TEXTURE0_ARB); |
---|
[7353] | 427 | glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0])); |
---|
[7465] | 428 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
[7395] | 429 | // glClientActiveTextureARB(GL_TEXTURE0_ARB); |
---|
[7465] | 430 | glClientActiveTextureARB(GL_TEXTURE1_ARB); |
---|
[7353] | 431 | glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0])); |
---|
[7465] | 432 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
[7395] | 433 | // glClientActiveTextureARB(GL_TEXTURE1_ARB); |
---|
| 434 | // glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1])); |
---|
| 435 | //glEnableClientState(GL_NORMAL_ARRAY ); |
---|
[7353] | 436 | |
---|
| 437 | glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0])); |
---|
[7395] | 438 | // glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0])); |
---|
[7353] | 439 | glDrawElements(GL_TRIANGLES, curFace.n_meshverts, |
---|
[7395] | 440 | GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert])); |
---|
| 441 | |
---|
[7353] | 442 | } |
---|
| 443 | |
---|
| 444 | void BspManager::draw_patch(face* Face) |
---|
| 445 | { |
---|
[7544] | 446 | if(this->lastTex != Face->texture) { |
---|
[7510] | 447 | this->bspFile->Materials[Face->texture].mat->select(); |
---|
[7544] | 448 | this->lastTex = Face->texture; |
---|
| 449 | } |
---|
[7801] | 450 | if (Face->effect != -1) return; |
---|
[7563] | 451 | |
---|
| 452 | |
---|
| 453 | if(Face->lm_index < 0) { |
---|
[7465] | 454 | glActiveTextureARB(GL_TEXTURE1_ARB); |
---|
[7511] | 455 | glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap); |
---|
[7465] | 456 | glEnable(GL_TEXTURE_2D); |
---|
[7563] | 457 | } else { |
---|
[7465] | 458 | glActiveTextureARB(GL_TEXTURE1_ARB); |
---|
| 459 | glBindTexture(GL_TEXTURE_2D, this->bspFile->glLightMapTextures[Face->lm_index]); |
---|
| 460 | glEnable(GL_TEXTURE_2D); |
---|
[7563] | 461 | } |
---|
[7579] | 462 | //glColor4f(3.0,3.0,3.0,1.0); |
---|
[7563] | 463 | |
---|
[7801] | 464 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
---|
| 465 | glEnable( GL_AUTO_NORMAL); |
---|
[7465] | 466 | glEnableClientState(GL_VERTEX_ARRAY ); |
---|
| 467 | glEnableClientState(GL_TEXTURE_COORD_ARRAY ); |
---|
[7801] | 468 | for(int i = Face->n_meshverts -1; i >=0 ; i--) { |
---|
[7833] | 469 | //glFrontFace(GL_CW); |
---|
[7395] | 470 | //PRINTF(0)("BSP Manager: Face->size[0]: %i . \n", Face->size[0]); |
---|
[7563] | 471 | |
---|
| 472 | |
---|
[7801] | 473 | //glEnableClientState(GL_NORMAL_ARRAY ); |
---|
[7353] | 474 | |
---|
[7395] | 475 | glVertexPointer(3, GL_FLOAT,44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).position[0])); |
---|
[7563] | 476 | |
---|
| 477 | |
---|
[7465] | 478 | glClientActiveTextureARB(GL_TEXTURE0_ARB); |
---|
[7801] | 479 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
[7395] | 480 | glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[0][0])); |
---|
[7465] | 481 | |
---|
[7563] | 482 | |
---|
[7833] | 483 | |
---|
[7465] | 484 | glClientActiveTextureARB(GL_TEXTURE1_ARB); |
---|
| 485 | glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[1][0])); |
---|
[7544] | 486 | //glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
[7465] | 487 | |
---|
[7563] | 488 | |
---|
[7833] | 489 | // glNormalPointer( GL_FLOAT, 44,&((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).normal[0]) ); |
---|
[7353] | 490 | |
---|
| 491 | |
---|
| 492 | |
---|
[7563] | 493 | |
---|
[7801] | 494 | for(int row=6; row>=0; --row) { |
---|
[7395] | 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 | } |
---|
[7563] | 498 | |
---|
[7801] | 499 | //glFrontFace(GL_CCW); |
---|
[7395] | 500 | } |
---|
[7465] | 501 | glDisableClientState(GL_TEXTURE0_ARB); |
---|
| 502 | glDisableClientState(GL_TEXTURE1_ARB); |
---|
[7801] | 503 | glDisable(GL_AUTO_NORMAL); |
---|
[7465] | 504 | glDisableClientState(GL_VERTEX_ARRAY ); |
---|
| 505 | glDisableClientState(GL_TEXTURE_COORD_ARRAY ); |
---|
[7563] | 506 | |
---|
| 507 | |
---|
[7353] | 508 | } |
---|
| 509 | |
---|
| 510 | bool BspManager::isAlreadyVisible(int Face) |
---|
| 511 | { |
---|
[7395] | 512 | return this->alreadyVisible[Face]; |
---|
[7353] | 513 | } |
---|
| 514 | |
---|
| 515 | |
---|
| 516 | BspTreeNode* BspManager::getLeaf(BspTreeNode* node, Vector* cam) |
---|
| 517 | { |
---|
[7395] | 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; |
---|
[7353] | 528 | } |
---|
| 529 | |
---|
[7563] | 530 | void BspManager::checkBrushRay(brush* curBrush) |
---|
| 531 | { |
---|
| 532 | float EPSILON = 0.000001; |
---|
| 533 | float startDistance; |
---|
| 534 | float endDistance; |
---|
[7833] | 535 | |
---|
[7563] | 536 | float startFraction = -1.0f; |
---|
| 537 | float endFraction = 1.0f; |
---|
| 538 | bool startsOut = false; |
---|
| 539 | bool endsOut = false; |
---|
[7833] | 540 | |
---|
[7563] | 541 | Vector inputStart = State::getCameraTargetNode()->getLastAbsCoor(); |
---|
| 542 | Vector inputEnd = State::getCameraTargetNode()->getAbsCoor(); |
---|
| 543 | |
---|
[7833] | 544 | for (int i = 0; i < curBrush->n_brushsides; i++) { |
---|
[7563] | 545 | brushside& curBrushSide = this->bspFile->brushSides[curBrush->brushside + i] ; |
---|
| 546 | plane& curPlane = this->bspFile->planes[curBrushSide.plane] ; |
---|
| 547 | |
---|
[7833] | 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; |
---|
[7563] | 550 | |
---|
| 551 | if (startDistance > 0) |
---|
| 552 | startsOut = true; |
---|
| 553 | if (endDistance > 0) |
---|
| 554 | endsOut = true; |
---|
| 555 | |
---|
[7833] | 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 |
---|
[7563] | 558 | return; |
---|
| 559 | } |
---|
[7833] | 560 | if (startDistance <= 0 && endDistance <= 0) { // both are behind this plane, it will get clipped by another one |
---|
[7563] | 561 | continue; |
---|
| 562 | } |
---|
| 563 | |
---|
[7833] | 564 | // MMM... BEEFY |
---|
| 565 | if (startDistance > endDistance) { // line is entering into the brush |
---|
[7563] | 566 | float fraction = (startDistance - EPSILON) / (startDistance - endDistance); // * |
---|
| 567 | if (fraction > startFraction) |
---|
| 568 | startFraction = fraction; |
---|
[8081] | 569 | // don't store plane |
---|
| 570 | // this->collPlane = &curPlane; |
---|
[8087] | 571 | |
---|
[8081] | 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; |
---|
[8087] | 578 | |
---|
[8081] | 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; |
---|
[8087] | 593 | this->outputFraction = startFraction; |
---|
[8081] | 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; |
---|
[8490] | 607 | bool startsOut = false; |
---|
| 608 | bool endsOut = false; |
---|
[8081] | 609 | |
---|
[8796] | 610 | // Vector inputStart = State::getCameraTargetNode()->getLastAbsCoor(); |
---|
| 611 | // Vector inputEnd = State::getCameraTargetNode()->getAbsCoor(); |
---|
[8081] | 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; |
---|
[8030] | 638 | // store plane |
---|
| 639 | this->collPlane = &curPlane; |
---|
[8087] | 640 | |
---|
[7833] | 641 | } else { // line is leaving the brush |
---|
[7563] | 642 | float fraction = (startDistance + EPSILON) / (startDistance - endDistance); // * |
---|
| 643 | if (fraction < endFraction) |
---|
| 644 | endFraction = fraction; |
---|
[8030] | 645 | // store plane |
---|
| 646 | this->collPlane = & curPlane; |
---|
[8087] | 647 | |
---|
[7563] | 648 | } |
---|
[7833] | 649 | |
---|
[7563] | 650 | } |
---|
[7833] | 651 | if (startsOut == false) { |
---|
| 652 | this->outputStartsOut = false; |
---|
| 653 | if (endsOut == false) |
---|
| 654 | this->outputAllSolid = true; |
---|
| 655 | return; |
---|
| 656 | } |
---|
[7563] | 657 | |
---|
[7833] | 658 | if (startFraction < endFraction) { |
---|
| 659 | if (startFraction > -1.0f && startFraction < outputFraction) { |
---|
[7563] | 660 | if (startFraction < 0) |
---|
| 661 | startFraction = 0; |
---|
[8087] | 662 | this->outputFraction = startFraction; |
---|
[7563] | 663 | } |
---|
| 664 | } |
---|
[7833] | 665 | |
---|
[7563] | 666 | } |
---|
| 667 | |
---|
[8490] | 668 | void BspManager::checkBrushRayN(brush* curBrush, Vector& inputStart, Vector& inputEnd) |
---|
| 669 | { |
---|
| 670 | float EPSILON = 0.000001; |
---|
| 671 | float startDistance; |
---|
| 672 | float endDistance; |
---|
[8081] | 673 | |
---|
[8490] | 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 | |
---|
[7563] | 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 && |
---|
[7833] | 753 | ((((BspTexture*)(this->bspFile->textures))[curBrush.texture]).contents & 1)) |
---|
| 754 | // CheckBrush( brush ); |
---|
| 755 | this->checkBrushRay(&curBrush); |
---|
[7563] | 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) { |
---|
[7833] | 783 | side = 0; // front(start)->x * (node->plane.x)+ |
---|
[7563] | 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 | } |
---|
[7833] | 831 | |
---|
| 832 | |
---|
[8081] | 833 | |
---|
| 834 | void BspManager::checkCollisionRayN(BspTreeNode* node, float startFraction, float endFraction, Vector* start, Vector* end) |
---|
| 835 | { |
---|
| 836 | |
---|
| 837 | |
---|
| 838 | float EPSILON = 0.000001; |
---|
| 839 | |
---|
[8490] | 840 | float endDistance = end->dot(node->plane) - node->d; |
---|
| 841 | float startDistance = start->dot(node->plane) - node->d; |
---|
[8081] | 842 | |
---|
[8490] | 843 | |
---|
| 844 | if( node->isLeaf) { |
---|
[8081] | 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; |
---|
[8796] | 854 | } |
---|
| 855 | |
---|
[8081] | 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 |
---|
[8490] | 898 | middleFraction = startFraction + (endFraction - startFraction) * fraction1; |
---|
| 899 | middle = (*start) + ((*end) - (*start)) * fraction1; |
---|
[8081] | 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, |
---|
[8490] | 907 | start, &middle ); |
---|
[8081] | 908 | |
---|
| 909 | // STEP 5: calculate the middle point for the second side |
---|
[8490] | 910 | middleFraction = startFraction + (endFraction - startFraction) * fraction2; |
---|
| 911 | middle = (*start) + ((*end) - (*start)) * fraction2; |
---|
[8081] | 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 | } |
---|
[8724] | 922 | |
---|
| 923 | float BspManager::checkPatchAltitude(BspTreeNode* node) |
---|
| 924 | { |
---|
| 925 | leaf& curLeaf = this->bspFile->leaves[node->leafIndex]; |
---|
[8894] | 926 | for(int i = 0; i < curLeaf.n_leaffaces ; i++) {} |
---|
[8724] | 927 | return 10.0f; |
---|
| 928 | } |
---|
| 929 | |
---|
[8490] | 930 | void BspManager::checkCollisionBox(void) |
---|
[8894] | 931 | {} |
---|
[8081] | 932 | |
---|
[8894] | 933 | |
---|
[8490] | 934 | void BspManager::TraceBox( Vector& inputStart, Vector& inputEnd, |
---|
[8796] | 935 | Vector& inputMins, Vector& inputMaxs ) |
---|
[8490] | 936 | { |
---|
| 937 | if (inputMins.x == 0 && inputMins.y == 0 && inputMins.z == 0 && |
---|
[8796] | 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 |
---|
[8490] | 941 | //traceType = TT_BOX; |
---|
| 942 | this->traceMins = inputMins; |
---|
| 943 | this->traceMaxs = inputMaxs; |
---|
| 944 | this->traceExtents.x = -traceMins.x > traceMaxs.x ? |
---|
[8796] | 945 | -traceMins.x : traceMaxs.x; |
---|
[8490] | 946 | this->traceExtents.y = -traceMins.y > traceMaxs.y ? |
---|
[8796] | 947 | -traceMins.y : traceMaxs.y; |
---|
[8490] | 948 | this->traceExtents.z = -traceMins.z > traceMaxs.z ? |
---|
[8796] | 949 | -traceMins.z : traceMaxs.z; |
---|
| 950 | //!> FIXME Trace( inputStart, inputEnd ); |
---|
[8490] | 951 | } |
---|
| 952 | } |
---|
| 953 | |
---|
[7833] | 954 | void BspManager::checkCollision(WorldEntity* worldEntity) |
---|
| 955 | { |
---|
[8724] | 956 | |
---|
[8796] | 957 | // Init Collision Detection |
---|
[8490] | 958 | this->outputStartsOut = true; |
---|
| 959 | this->outputAllSolid = false; |
---|
| 960 | this->outputFraction = 1.0f; |
---|
[7833] | 961 | |
---|
[9080] | 962 | |
---|
[8796] | 963 | // Retrieve Bounding box |
---|
| 964 | AABB* box = worldEntity->getModelAABB(); |
---|
[7833] | 965 | |
---|
[8724] | 966 | |
---|
[8894] | 967 | Vector forwardDir = Vector(0.0,0.0,1.0); |
---|
| 968 | Vector upDir = Vector(0.0,1.0,0.0); |
---|
| 969 | Vector position = worldEntity->getAbsCoor(); |
---|
[8087] | 970 | |
---|
[8894] | 971 | bool SolidFlag = false; |
---|
[8490] | 972 | bool collision = false; |
---|
[8796] | 973 | Vector position1 = position; |
---|
[8724] | 974 | Vector position2 = position + Vector(0.0,1.0,0.0); |
---|
[9061] | 975 | Vector position3 = position; |
---|
| 976 | Vector position4 = position + Vector(0.0,1.0,0.0); |
---|
[8894] | 977 | Vector dest = worldEntity->getAbsCoor() - upDir*40.0f; // |
---|
[8724] | 978 | Vector dest1 = position + forwardDir*4.0f; |
---|
[9003] | 979 | Vector dest2 = position2 + forwardDir*4.0; |
---|
[9061] | 980 | Vector dest3 = position + forwardDir*4.0f; |
---|
| 981 | Vector dest4 = position2 + forwardDir*4.0; |
---|
[8490] | 982 | dest = position - Vector(0.0, 40.0,0.0); |
---|
[8894] | 983 | Vector out = dest; |
---|
[8490] | 984 | Vector out1; |
---|
| 985 | Vector out2; |
---|
[7833] | 986 | |
---|
| 987 | |
---|
[9065] | 988 | plane* testPlane; |
---|
[8894] | 989 | |
---|
[9065] | 990 | bool xCollision = false; |
---|
| 991 | bool zCollision = false; |
---|
| 992 | |
---|
| 993 | |
---|
[8490] | 994 | float height = 40; |
---|
[8894] | 995 | |
---|
| 996 | |
---|
| 997 | if( box != NULL) { |
---|
[9061] | 998 | position = worldEntity->getAbsCoor() + box->center; // + Vector(0.0, 1.0, 0.0) * box->halfLength[1] * 1.0f; |
---|
| 999 | dest = worldEntity->getAbsCoor() + box->center - Vector(0.0, 1.0, 0.0) * (box->halfLength[1] + BSP_Y_OFFSET) * 100; |
---|
[8894] | 1000 | |
---|
[9061] | 1001 | Vector dirX = worldEntity->getAbsDirX(); dirX.y = 0.0f; dirX.normalize(); |
---|
[8894] | 1002 | |
---|
[9061] | 1003 | //position1 = worldEntity->getAbsCoor() + box->center - dirX * (box->halfLength[0] + BSP_X_OFFSET); |
---|
| 1004 | dest1 = worldEntity->getAbsCoor() + box->center + dirX * (box->halfLength[0] + BSP_X_OFFSET); |
---|
| 1005 | dest2 = worldEntity->getAbsCoor() - box->center + dirX * (box->halfLength[0] + BSP_X_OFFSET); |
---|
[9065] | 1006 | |
---|
[9061] | 1007 | Vector dirZ = worldEntity->getAbsDirZ(); dirX.y = 0.0f; dirZ.normalize(); |
---|
| 1008 | //position2 = worldEntity->getAbsCoor() + box->center - dirZ * (box->halfLength[2] + BSP_Z_OFFSET); |
---|
| 1009 | dest3 = worldEntity->getAbsCoor() + box->center + dirZ * (box->halfLength[2] + BSP_Z_OFFSET); |
---|
| 1010 | dest4 = worldEntity->getAbsCoor() - box->center + dirZ * (box->halfLength[2] + BSP_Z_OFFSET); |
---|
[8894] | 1011 | } else { |
---|
| 1012 | // Init positions and destinations to anything useful! |
---|
| 1013 | |
---|
[8796] | 1014 | } |
---|
[8724] | 1015 | |
---|
[8894] | 1016 | |
---|
| 1017 | |
---|
[9065] | 1018 | // 1st Ray: Y RAY |
---|
[8490] | 1019 | this->inputStart = position; |
---|
| 1020 | this->inputEnd = dest; |
---|
| 1021 | this->checkCollisionRayN(this->root,0.0f,1.0f, &position, &dest ); |
---|
| 1022 | |
---|
[8724] | 1023 | |
---|
[8796] | 1024 | // |
---|
| 1025 | if(!this->outputStartsOut ) { |
---|
[8724] | 1026 | this->collPlane = new plane; |
---|
| 1027 | this->collPlane->x = 0.0f; |
---|
| 1028 | this->collPlane->y = 0.0f; |
---|
| 1029 | this->collPlane->z = 0.0f; |
---|
| 1030 | collision = true; |
---|
[8796] | 1031 | } else { |
---|
[8724] | 1032 | |
---|
[8796] | 1033 | if( this->outputFraction == 1.0f) { |
---|
| 1034 | if(this->outputAllSolid ) { |
---|
| 1035 | this->collPlane = new plane; |
---|
| 1036 | this->collPlane->x = 0.0f; |
---|
| 1037 | this->collPlane->y = 0.0f; |
---|
| 1038 | this->collPlane->z = 0.0f; |
---|
| 1039 | collision = true; |
---|
[8894] | 1040 | SolidFlag = true; |
---|
| 1041 | } else |
---|
[8796] | 1042 | collision = false; |
---|
[8894] | 1043 | |
---|
| 1044 | |
---|
| 1045 | out = dest; |
---|
| 1046 | |
---|
[8796] | 1047 | } else { |
---|
| 1048 | collision = true; |
---|
| 1049 | out.x = position.x + (dest.x -position.x) * this->outputFraction; |
---|
| 1050 | out.y = position.y + (dest.y -position.y) * this->outputFraction; |
---|
| 1051 | out.z = position.z + (dest.z -position.z) * this->outputFraction; |
---|
[8724] | 1052 | |
---|
[8796] | 1053 | Vector out3 = out + Vector(height*this->collPlane->x,height*this->collPlane->y,height*this->collPlane->z); |
---|
| 1054 | this->out = out; |
---|
[7833] | 1055 | } |
---|
[8490] | 1056 | } |
---|
[9065] | 1057 | testPlane = this->collPlane; |
---|
[8490] | 1058 | |
---|
[8087] | 1059 | |
---|
[9061] | 1060 | bool xCollisionNeg = false; |
---|
| 1061 | bool zCollisionNeg = false; |
---|
[8894] | 1062 | |
---|
[9003] | 1063 | |
---|
[9065] | 1064 | |
---|
| 1065 | // 2nd Collision Detection X-RAY |
---|
[8894] | 1066 | this->outputStartsOut = true; |
---|
| 1067 | this->outputAllSolid = false; |
---|
| 1068 | this->outputFraction = 1.0f; |
---|
| 1069 | this->inputStart = position1; |
---|
| 1070 | this->inputEnd = dest1; |
---|
| 1071 | this->checkCollisionRayN(this->root,0.0f,1.0f, &position1, &dest1 ); |
---|
| 1072 | |
---|
[9003] | 1073 | if(this->outputFraction < 1.0f) { |
---|
[8894] | 1074 | out.x = dest1.x + (dest1.x -position1.x) * this->outputFraction; |
---|
[9003] | 1075 | dest1 = position1 + (dest1 -position1) * this->outputFraction; |
---|
[8894] | 1076 | xCollision = true; |
---|
| 1077 | testPlane = this->collPlane; |
---|
| 1078 | } |
---|
[9003] | 1079 | if(this->outputAllSolid ) { |
---|
| 1080 | |
---|
| 1081 | this->collPlane = new plane; |
---|
| 1082 | this->collPlane->x = 0.0f; |
---|
| 1083 | this->collPlane->y = 0.0f; |
---|
| 1084 | this->collPlane->z = 0.0f; |
---|
| 1085 | testPlane = this->collPlane; |
---|
[8894] | 1086 | SolidFlag = true; |
---|
[9003] | 1087 | xCollision = true; |
---|
[8894] | 1088 | } |
---|
| 1089 | //out.z = this->outputFraction; |
---|
| 1090 | |
---|
| 1091 | |
---|
[9003] | 1092 | |
---|
[9065] | 1093 | // 3rd Collision Detection Z-RAY |
---|
[8894] | 1094 | this->outputStartsOut = true; |
---|
| 1095 | this->outputAllSolid = false; |
---|
| 1096 | this->outputFraction = 1.0f; |
---|
| 1097 | this->inputStart = position2; |
---|
| 1098 | this->inputEnd = dest2; |
---|
[9003] | 1099 | |
---|
[8894] | 1100 | this->checkCollisionRayN(this->root,0.0f,1.0f, &position2, &dest2 ); |
---|
| 1101 | //out.x = this->outputFraction; |
---|
| 1102 | |
---|
[9003] | 1103 | if(this->outputFraction < 1.0f ) { |
---|
[8894] | 1104 | out.z = out.z = dest2.z + (dest2.z -position2.z) * this->outputFraction; |
---|
[9003] | 1105 | dest2 = position2 + (dest2 -position2) * this->outputFraction; |
---|
[8894] | 1106 | zCollision = true; |
---|
| 1107 | testPlane = this->collPlane; |
---|
| 1108 | |
---|
| 1109 | } |
---|
[9003] | 1110 | if(this->outputAllSolid ) { |
---|
| 1111 | this->collPlane = new plane; |
---|
| 1112 | this->collPlane->x = 0.0f; |
---|
| 1113 | this->collPlane->y = 0.0f; |
---|
| 1114 | this->collPlane->z = 0.0f; |
---|
| 1115 | testPlane = this->collPlane; |
---|
| 1116 | |
---|
| 1117 | SolidFlag = true; |
---|
| 1118 | zCollision = true; |
---|
[8894] | 1119 | } |
---|
[9003] | 1120 | |
---|
[8796] | 1121 | |
---|
[8490] | 1122 | // Return the normal here: Normal's stored in this->collPlane; |
---|
[8894] | 1123 | if( collision) { |
---|
[9065] | 1124 | worldEntity->registerCollision(COLLISION_TYPE_AXIS_Y , this->parent, worldEntity, Vector(testPlane->x, testPlane->y, testPlane->z), out, SolidFlag); |
---|
[9003] | 1125 | } |
---|
| 1126 | if(xCollision) { |
---|
| 1127 | worldEntity->registerCollision(COLLISION_TYPE_AXIS_X , this->parent, worldEntity, Vector(testPlane->x, testPlane->y, testPlane->z),dest1 , SolidFlag); |
---|
| 1128 | } |
---|
[9065] | 1129 | |
---|
[9003] | 1130 | if(zCollision) { |
---|
| 1131 | worldEntity->registerCollision(COLLISION_TYPE_AXIS_Z , this->parent, worldEntity, Vector(testPlane->x, testPlane->y, testPlane->z), dest2 , SolidFlag); |
---|
| 1132 | } |
---|
[7833] | 1133 | |
---|
[8894] | 1134 | |
---|
[7833] | 1135 | } |
---|
| 1136 | |
---|
[8490] | 1137 | |
---|
[9080] | 1138 | |
---|
| 1139 | /** |
---|
| 1140 | * check the collision in the x direction (forward, backward) |
---|
| 1141 | */ |
---|
| 1142 | void BspManager::checkCollisionX(WorldEntity* entity) |
---|
| 1143 | { |
---|
| 1144 | // Retrieve Bounding box |
---|
[9081] | 1145 | AABB* box = entity->getModelAABB(); |
---|
[9080] | 1146 | |
---|
| 1147 | |
---|
[9088] | 1148 | plane* testPlane = NULL; //!< the collision test plane |
---|
[9080] | 1149 | |
---|
[9088] | 1150 | Vector forward; //!< left collision ray |
---|
| 1151 | Vector backward; //!< right collision ray |
---|
[9084] | 1152 | Vector collPos; //!< the collision position |
---|
[9080] | 1153 | |
---|
[9088] | 1154 | bool xCollisionForward = false; //!< flag true if right collision |
---|
| 1155 | bool xCollisionBackward = false; //!< flag true if left collision |
---|
| 1156 | bool SolidFlag = false; //!< flag set true if solid |
---|
[9081] | 1157 | |
---|
[9088] | 1158 | Vector position; //!< current position of the entity |
---|
| 1159 | Vector dirX; //!< direction x |
---|
[9080] | 1160 | |
---|
[9088] | 1161 | position = entity->getAbsCoor(); |
---|
| 1162 | dirX = entity->getAbsDirX(); dirX.y = 0.0f; dirX.normalize(); |
---|
[9084] | 1163 | |
---|
[9086] | 1164 | // calculate the rays |
---|
[9080] | 1165 | if( box != NULL) |
---|
| 1166 | { |
---|
[9088] | 1167 | forward = entity->getAbsCoor() + box->center + dirX * (box->halfLength[0] + BSP_X_OFFSET); |
---|
| 1168 | backward = entity->getAbsCoor() - box->center + dirX * (box->halfLength[0] + BSP_X_OFFSET); |
---|
[9080] | 1169 | } |
---|
| 1170 | else |
---|
| 1171 | { |
---|
[9088] | 1172 | forward = position + dirX * 4.0f; |
---|
| 1173 | backward = position + Vector(0.0, 1.0, 0.0) + dirX * 4.0; |
---|
[9080] | 1174 | } |
---|
| 1175 | |
---|
| 1176 | |
---|
[9088] | 1177 | /* X Ray forward */ |
---|
[9084] | 1178 | // init some member variables before collision check |
---|
[9080] | 1179 | this->outputStartsOut = true; |
---|
| 1180 | this->outputAllSolid = false; |
---|
| 1181 | this->outputFraction = 1.0f; |
---|
[9084] | 1182 | this->inputStart = position; |
---|
[9088] | 1183 | this->inputEnd = forward; |
---|
[9089] | 1184 | this->checkCollisionRayN(this->root, 0.0f, 1.0f, &position, &forward ); |
---|
[9080] | 1185 | |
---|
[9084] | 1186 | // collision occured |
---|
| 1187 | if( this->outputFraction < 1.0f) |
---|
| 1188 | { |
---|
[9088] | 1189 | collPos = position + (forward - position) * this->outputFraction; |
---|
| 1190 | xCollisionForward = true; |
---|
[9080] | 1191 | testPlane = this->collPlane; |
---|
| 1192 | } |
---|
[9084] | 1193 | if(this->outputAllSolid ) |
---|
| 1194 | { |
---|
[9080] | 1195 | this->collPlane = new plane; |
---|
| 1196 | this->collPlane->x = 0.0f; |
---|
| 1197 | this->collPlane->y = 0.0f; |
---|
| 1198 | this->collPlane->z = 0.0f; |
---|
| 1199 | testPlane = this->collPlane; |
---|
| 1200 | SolidFlag = true; |
---|
[9088] | 1201 | xCollisionForward = true; |
---|
[9080] | 1202 | } |
---|
| 1203 | |
---|
[9084] | 1204 | // collision registration |
---|
[9088] | 1205 | if( xCollisionForward) { |
---|
[9084] | 1206 | entity->registerCollision(COLLISION_TYPE_AXIS_X , |
---|
| 1207 | this->parent, entity, |
---|
| 1208 | Vector(testPlane->x, testPlane->y, testPlane->z), |
---|
| 1209 | collPos, |
---|
| 1210 | SolidFlag); |
---|
[9080] | 1211 | } |
---|
| 1212 | |
---|
[9088] | 1213 | |
---|
| 1214 | |
---|
| 1215 | /* X Ray backward */ |
---|
| 1216 | // init some member variables before collision check |
---|
| 1217 | this->outputStartsOut = true; |
---|
| 1218 | this->outputAllSolid = false; |
---|
| 1219 | this->outputFraction = 1.0f; |
---|
| 1220 | this->inputStart = position; |
---|
| 1221 | this->inputEnd = backward; |
---|
[9089] | 1222 | this->checkCollisionRayN(this->root, 0.0f, 1.0f, &position, &backward ); |
---|
[9088] | 1223 | |
---|
| 1224 | // collision occured |
---|
| 1225 | if( this->outputFraction < 1.0f) |
---|
| 1226 | { |
---|
| 1227 | collPos = position + (backward - position) * this->outputFraction; |
---|
| 1228 | xCollisionBackward = true; |
---|
| 1229 | testPlane = this->collPlane; |
---|
| 1230 | } |
---|
| 1231 | if(this->outputAllSolid ) |
---|
| 1232 | { |
---|
| 1233 | this->collPlane = new plane; |
---|
| 1234 | this->collPlane->x = 0.0f; |
---|
| 1235 | this->collPlane->y = 0.0f; |
---|
| 1236 | this->collPlane->z = 0.0f; |
---|
| 1237 | testPlane = this->collPlane; |
---|
| 1238 | SolidFlag = true; |
---|
| 1239 | xCollisionBackward = true; |
---|
| 1240 | } |
---|
| 1241 | |
---|
| 1242 | // collision registration |
---|
| 1243 | if( xCollisionBackward) { |
---|
| 1244 | entity->registerCollision(COLLISION_TYPE_AXIS_X_NEG , |
---|
| 1245 | this->parent, entity, |
---|
| 1246 | Vector(testPlane->x, testPlane->y, testPlane->z), |
---|
| 1247 | collPos, |
---|
| 1248 | SolidFlag); |
---|
| 1249 | } |
---|
| 1250 | |
---|
[9080] | 1251 | } |
---|
| 1252 | |
---|
| 1253 | /** |
---|
| 1254 | * check the collision in the y direction (up, down) |
---|
| 1255 | */ |
---|
| 1256 | void BspManager::checkCollisionY(WorldEntity* entity) |
---|
| 1257 | { |
---|
[9089] | 1258 | // Retrieve Bounding box |
---|
| 1259 | AABB* box = entity->getModelAABB(); |
---|
[9080] | 1260 | |
---|
[9089] | 1261 | |
---|
| 1262 | plane* testPlane = NULL; //!< the collision test plane |
---|
| 1263 | |
---|
| 1264 | Vector right; //!< right collision ray |
---|
| 1265 | Vector left; //!< left collision ray |
---|
| 1266 | Vector collPos; //!< the collision position |
---|
| 1267 | |
---|
| 1268 | bool zCollisionRight = false; //!< flag true if right collision |
---|
| 1269 | bool zCollisionLeft = false; //!< flag true if left collision |
---|
| 1270 | bool SolidFlag = false; //!< flag set true if solid |
---|
| 1271 | |
---|
| 1272 | Vector position; //!< current position of the entity |
---|
| 1273 | Vector dirZ; //!< direction x |
---|
| 1274 | |
---|
| 1275 | position = entity->getAbsCoor(); |
---|
| 1276 | dirZ = entity->getAbsDirZ(); dirZ.y = 0.0f; dirZ.normalize(); |
---|
| 1277 | |
---|
| 1278 | // calculate the rays |
---|
| 1279 | if( box != NULL) |
---|
| 1280 | { |
---|
| 1281 | right = entity->getAbsCoor() + box->center + dirZ * (box->halfLength[2] + BSP_Z_OFFSET); |
---|
| 1282 | left = entity->getAbsCoor() - box->center + dirZ * (box->halfLength[2] + BSP_Z_OFFSET); |
---|
| 1283 | } |
---|
| 1284 | else |
---|
| 1285 | { |
---|
| 1286 | right = position + dirZ * 4.0f; |
---|
| 1287 | left = position + Vector(0.0, 1.0, 0.0) + dirZ * 4.0; |
---|
| 1288 | } |
---|
| 1289 | |
---|
| 1290 | |
---|
| 1291 | /* Z Ray right */ |
---|
| 1292 | // init some member variables before collision check |
---|
| 1293 | this->outputStartsOut = true; |
---|
| 1294 | this->outputAllSolid = false; |
---|
| 1295 | this->outputFraction = 1.0f; |
---|
| 1296 | this->inputStart = position; |
---|
| 1297 | this->inputEnd = right; |
---|
| 1298 | this->checkCollisionRayN(this->root, 0.0f, 1.0f, &position, &right ); |
---|
| 1299 | |
---|
| 1300 | |
---|
| 1301 | // collision occured |
---|
| 1302 | if( this->outputFraction < 1.0f ) |
---|
| 1303 | { |
---|
| 1304 | collPos = position + (right - position) * this->outputFraction; |
---|
| 1305 | zCollisionRight = true; |
---|
| 1306 | testPlane = this->collPlane; |
---|
| 1307 | } |
---|
| 1308 | if(this->outputAllSolid ) |
---|
| 1309 | { |
---|
| 1310 | this->collPlane = new plane; |
---|
| 1311 | this->collPlane->x = 0.0f; |
---|
| 1312 | this->collPlane->y = 0.0f; |
---|
| 1313 | this->collPlane->z = 0.0f; |
---|
| 1314 | testPlane = this->collPlane; |
---|
| 1315 | |
---|
| 1316 | SolidFlag = true; |
---|
| 1317 | zCollisionRight = true; |
---|
| 1318 | } |
---|
| 1319 | |
---|
| 1320 | |
---|
| 1321 | if( zCollisionRight) { |
---|
| 1322 | entity->registerCollision(COLLISION_TYPE_AXIS_Z , this->parent, |
---|
| 1323 | entity, |
---|
| 1324 | Vector(testPlane->x, testPlane->y, testPlane->z), |
---|
| 1325 | collPos , SolidFlag); |
---|
| 1326 | } |
---|
| 1327 | |
---|
| 1328 | |
---|
[9080] | 1329 | } |
---|
| 1330 | |
---|
| 1331 | |
---|
| 1332 | /** |
---|
| 1333 | * check the collision in the z direction (left, right) |
---|
| 1334 | */ |
---|
| 1335 | void BspManager::checkCollisionZ(WorldEntity* entity) |
---|
| 1336 | { |
---|
| 1337 | |
---|
| 1338 | } |
---|
| 1339 | |
---|
| 1340 | |
---|
| 1341 | |
---|
| 1342 | |
---|
| 1343 | |
---|
[7353] | 1344 | void BspManager::checkCollision(BspTreeNode* node, Vector* cam) |
---|
| 1345 | { |
---|
[7563] | 1346 | Vector next = this->cam; |
---|
| 1347 | next.x = (State::getCameraTargetNode()->getLastAbsCoor()).x ; |
---|
| 1348 | next.y = (State::getCameraTargetNode()->getLastAbsCoor()).y ; |
---|
| 1349 | next.z = (State::getCameraTargetNode()->getLastAbsCoor()).z ; |
---|
| 1350 | |
---|
[7395] | 1351 | float dist = 0; |
---|
| 1352 | if(!(node->isLeaf)) { |
---|
| 1353 | dist = (node->plane.x * this->cam.x + node->plane.y*this->cam.y + node->plane.z*this->cam.z) - node->d; |
---|
| 1354 | if(dist > 4.0f) { |
---|
| 1355 | checkCollision(node->left,cam); |
---|
| 1356 | return; |
---|
| 1357 | } |
---|
| 1358 | if(dist < -4.0f) { |
---|
| 1359 | checkCollision(node->right,cam); |
---|
| 1360 | return; |
---|
| 1361 | } |
---|
| 1362 | if(dist<=4.0f && dist >= -4.0f) { |
---|
| 1363 | checkCollision(node->left,cam); |
---|
| 1364 | checkCollision(node->right,cam); |
---|
| 1365 | return; |
---|
| 1366 | } |
---|
| 1367 | return; |
---|
| 1368 | } else { |
---|
[7353] | 1369 | |
---|
[7395] | 1370 | leaf& camLeaf = ((leaf *)(this->bspFile->leaves))[(node->leafIndex ) ]; |
---|
[7353] | 1371 | |
---|
[7579] | 1372 | if (camLeaf.cluster < 0) { |
---|
[7833] | 1373 | this->drawDebugCube(&this->cam); |
---|
| 1374 | this->drawDebugCube(&next); |
---|
[8796] | 1375 | // State::getPlayer()->getPlayable()->setRelCoor(-100,-100,-100); |
---|
[8490] | 1376 | //State::getPlayer()->getPlayable()->collidesWith(NULL, State::getCameraTargetNode()->getLastAbsCoor()); |
---|
[7833] | 1377 | } |
---|
[7395] | 1378 | |
---|
| 1379 | |
---|
| 1380 | /* |
---|
[8087] | 1381 | for(int i = 0; i < camLeaf.n_leafbrushes && i < 10; i++ ) |
---|
| 1382 | { |
---|
| 1383 | brush& curBrush = ((brush*)(this->bspFile->brushes))[(camLeaf.leafbrush_first +i)%this->bspFile->numLeafBrushes]; |
---|
| 1384 | if(curBrush.n_brushsides < 0) return; |
---|
| 1385 | for(int j = 0; j < curBrush.n_brushsides; j++) |
---|
| 1386 | { |
---|
| 1387 | float dist = -0.1; |
---|
| 1388 | brushside& curBrushSide = ((brushside*)(this->bspFile->brushSides))[(curBrush.brushside +j)%this->bspFile->numBrushSides]; |
---|
| 1389 | plane& testPlane = ((plane*)(this->bspFile->planes))[curBrushSide.plane % this->bspFile->numPlanes]; |
---|
| 1390 | dist = testPlane.x * this->cam.x + testPlane.y * this->cam.y + testPlane.z * this->cam.z -testPlane.d ; |
---|
[7395] | 1391 | |
---|
[8087] | 1392 | if(dist < -0.01f) dist = -1.0f *dist; |
---|
| 1393 | if(dist < 1.0f){ |
---|
| 1394 | this->drawDebugCube(&this->cam); |
---|
| 1395 | return; |
---|
| 1396 | } |
---|
| 1397 | } |
---|
| 1398 | |
---|
| 1399 | } */ |
---|
| 1400 | |
---|
[7395] | 1401 | } |
---|
| 1402 | return; |
---|
[7353] | 1403 | } |
---|
| 1404 | |
---|
| 1405 | void BspManager::drawDebugCube(Vector* cam) |
---|
| 1406 | { |
---|
[7395] | 1407 | glBegin(GL_QUADS); |
---|
[7353] | 1408 | |
---|
[7395] | 1409 | // Bottom Face. Red, 75% opaque, magnified texture |
---|
[7353] | 1410 | |
---|
[7395] | 1411 | glNormal3f( 0.0f, -1.0f, 0.0f); // Needed for lighting |
---|
| 1412 | glColor4f(0.9,0.2,0.2,.75); // Basic polygon color |
---|
[7353] | 1413 | |
---|
[7395] | 1414 | glTexCoord2f(0.800f, 0.800f); glVertex3f(cam->x-1.0f, cam->y-1.0f,cam->z -1.0f); |
---|
| 1415 | glTexCoord2f(0.200f, 0.800f); glVertex3f(cam->x+1.0f, cam->y-1.0f,cam->z -1.0f); |
---|
| 1416 | glTexCoord2f(0.200f, 0.200f); glVertex3f(cam->x+ 1.0f,cam->y -1.0f,cam->z + 1.0f); |
---|
| 1417 | glTexCoord2f(0.800f, 0.200f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z + 1.0f); |
---|
[7353] | 1418 | |
---|
| 1419 | |
---|
[7395] | 1420 | // Top face; offset. White, 50% opaque. |
---|
[7353] | 1421 | |
---|
[7395] | 1422 | glNormal3f( 0.0f, 1.0f, 0.0f); glColor4f(0.5,0.5,0.5,.5); |
---|
[7353] | 1423 | |
---|
[7395] | 1424 | glTexCoord2f(0.005f, 1.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.0f); |
---|
| 1425 | glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z +1.0f); |
---|
| 1426 | glTexCoord2f(1.995f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y+1.0f, cam->z +1.0f); |
---|
| 1427 | glTexCoord2f(1.995f, 1.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z -1.0f); |
---|
[7353] | 1428 | |
---|
| 1429 | |
---|
[7395] | 1430 | // Far face. Green, 50% opaque, non-uniform texture cooridinates. |
---|
[7353] | 1431 | |
---|
[7395] | 1432 | glNormal3f( 0.0f, 0.0f,-1.0f); glColor4f(0.2,0.9,0.2,.5); |
---|
[7353] | 1433 | |
---|
[7395] | 1434 | glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z -1.3f); |
---|
| 1435 | glTexCoord2f(2.995f, 2.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.3f); |
---|
| 1436 | glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x+ 1.0f,cam->y+ 1.0f, cam->z -1.3f); |
---|
| 1437 | glTexCoord2f(0.005f, 0.005f); glVertex3f( cam->x+1.0f,cam->y -1.0f, cam->z -1.3f); |
---|
[7353] | 1438 | |
---|
| 1439 | |
---|
[7395] | 1440 | // Right face. Blue; 25% opaque |
---|
[7353] | 1441 | |
---|
[7395] | 1442 | glNormal3f( 1.0f, 0.0f, 0.0f); glColor4f(0.2,0.2,0.9,.25); |
---|
[7353] | 1443 | |
---|
[7465] | 1444 | glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y -1.0f, cam->z -1.0f); |
---|
[7395] | 1445 | glTexCoord2f(0.995f, 0.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z -1.0f); |
---|
| 1446 | glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z + 1.0f); |
---|
| 1447 | glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y-1.0f, cam->z +1.0f); |
---|
[7353] | 1448 | |
---|
| 1449 | |
---|
[7395] | 1450 | // Front face; offset. Multi-colored, 50% opaque. |
---|
[7353] | 1451 | |
---|
[7395] | 1452 | glNormal3f( 0.0f, 0.0f, 1.0f); |
---|
[7353] | 1453 | |
---|
[7395] | 1454 | glColor4f( 0.9f, 0.2f, 0.2f, 0.5f); |
---|
| 1455 | glTexCoord2f( 0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z +1.0f); |
---|
| 1456 | glColor4f( 0.2f, 0.9f, 0.2f, 0.5f); |
---|
| 1457 | glTexCoord2f( 0.995f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y-1.0f, cam->z +1.0f); |
---|
| 1458 | glColor4f( 0.2f, 0.2f, 0.9f, 0.5f); |
---|
| 1459 | glTexCoord2f( 0.995f, 0.995f); glVertex3f( cam->x+1.0f, cam->y+1.0f, cam->z +1.0f); |
---|
| 1460 | glColor4f( 0.1f, 0.1f, 0.1f, 0.5f); |
---|
| 1461 | glTexCoord2f( 0.005f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z +1.0f); |
---|
| 1462 | |
---|
| 1463 | |
---|
| 1464 | // Left Face; offset. Yellow, varying levels of opaque. |
---|
| 1465 | |
---|
| 1466 | glNormal3f(-1.0f, 0.0f, 0.0f); |
---|
| 1467 | |
---|
| 1468 | glColor4f(0.9,0.9,0.2,0.0); |
---|
| 1469 | glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z -1.0f); |
---|
| 1470 | glColor4f(0.9,0.9,0.2,0.66); |
---|
| 1471 | glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x-1.0f,cam->y -1.0f, cam->z +1.0f); |
---|
| 1472 | glColor4f(0.9,0.9,0.2,1.0); |
---|
| 1473 | glTexCoord2f(0.995f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z +1.0f); |
---|
| 1474 | glColor4f(0.9,0.9,0.2,0.33); |
---|
| 1475 | glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.0f); |
---|
| 1476 | |
---|
| 1477 | glEnd(); |
---|
[7385] | 1478 | } |
---|
[7353] | 1479 | |
---|
[7385] | 1480 | void BspManager::addFace(int f) |
---|
| 1481 | { |
---|
[7395] | 1482 | face& curFace = ((face *)(this->bspFile->faces))[f]; |
---|
| 1483 | if(this->bspFile->Materials[curFace.texture].alpha) this->trasparent.push_back(f); |
---|
| 1484 | else this->opal.push_back(f); |
---|
| 1485 | } |
---|