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