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