[7353] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
[7511] | 3 | |
---|
[7353] | 4 | Copyright (C) 2006 orx |
---|
[7511] | 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. |
---|
[7511] | 10 | |
---|
[7353] | 11 | ### File Specific: |
---|
| 12 | main-programmer: bottac@ee.ethz.ch |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | #include "vector.h" |
---|
| 17 | #include "bsp_file.h" |
---|
| 18 | #include "bsp_manager.h" |
---|
[7385] | 19 | #include "bsp_tree_leaf.h" |
---|
[7353] | 20 | #include "p_node.h" |
---|
| 21 | #include "state.h" |
---|
| 22 | #include "debug.h" |
---|
| 23 | #include "material.h" |
---|
| 24 | #include "camera.h" |
---|
| 25 | #include "vertex_array_model.h" |
---|
| 26 | |
---|
[7385] | 27 | // STL Containers |
---|
| 28 | #include <vector> |
---|
| 29 | #include <deque> |
---|
[7353] | 30 | |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | BspManager::BspManager() |
---|
| 35 | { |
---|
[7395] | 36 | // open a BSP file |
---|
| 37 | this->bspFile = new BspFile(); |
---|
| 38 | this->bspFile->read("/root/data/Kanti175.bsp"); |
---|
| 39 | this->bspFile->build_tree(); |
---|
| 40 | this->root = this->bspFile->get_root(); |
---|
| 41 | this->alreadyVisible = new bool [this->bspFile->numFaces]; |
---|
[7353] | 42 | } |
---|
| 43 | |
---|
| 44 | void BspManager::draw() |
---|
| 45 | { |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | |
---|
[7395] | 51 | // Draw Debug Terrain |
---|
| 52 | /* |
---|
[7511] | 53 | this->bspFile->Materials[0]->select(); |
---|
[7395] | 54 | for(int i = 0; i < this->bspFile->numPatches ; i++) |
---|
[7511] | 55 | { |
---|
| 56 | this->bspFile->VertexArrayModels[i]->draw(); |
---|
| 57 | |
---|
| 58 | } |
---|
[7395] | 59 | */ |
---|
[7353] | 60 | |
---|
| 61 | |
---|
| 62 | |
---|
[7395] | 63 | // erase alreadyVisible |
---|
| 64 | for(int i = 0; i < this->bspFile->numFaces; i++) this->alreadyVisible[i] = false; |
---|
| 65 | float tmp = 0; |
---|
| 66 | this->opal.clear(); |
---|
| 67 | this->trasparent.clear(); |
---|
[7353] | 68 | // Find all visible faces... |
---|
| 69 | |
---|
[7395] | 70 | this->cam = State::getCamera()->getAbsCoor() ; |
---|
| 71 | this->ship = State::getCameraTargetNode()->getAbsCoor(); |
---|
[7353] | 72 | |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | |
---|
[7563] | 76 | |
---|
[7395] | 77 | this->viewDir= State::getCameraTarget()->getAbsCoor() - State::getCamera()->getAbsCoor() ; |
---|
| 78 | float d = (cam.x*viewDir.x + cam.y*viewDir.y + cam.z * viewDir.z); |
---|
[7353] | 79 | |
---|
[7395] | 80 | BspTreeNode* ActLeaf = this->getLeaf(this->bspFile->root, &ship); |
---|
| 81 | int viscluster = -1; |
---|
| 82 | viscluster =((leaf*)(this->bspFile->leaves))[ ActLeaf->leafIndex].cluster; // get the players cluster (viscluster) |
---|
[7353] | 83 | |
---|
| 84 | |
---|
| 85 | |
---|
[7563] | 86 | this->cam = State::getCameraTargetNode()->getAbsCoor(); |
---|
| 87 | //this->checkCollision(this->root, &this->cam); //!< Test Collision Detection |
---|
| 88 | this->outputStartsOut = true; |
---|
| 89 | this->outputAllSolid = false; |
---|
| 90 | this->outputFraction = 1.0f; |
---|
| 91 | |
---|
| 92 | this->checkCollisionRay(this->root,0.0f,1.0f, &(State::getCameraTargetNode()->getLastAbsCoor()), &this->cam); |
---|
| 93 | |
---|
| 94 | if(this->outputFraction != 1.0f || this->outputAllSolid ) this->drawDebugCube(&this->cam); |
---|
[7395] | 95 | |
---|
[7563] | 96 | if (true || (((int *)(this->bspFile->header))[35] == 0 ) || viscluster < 0) //!< if (sizeof(Visdata) == 0) |
---|
[7395] | 97 | { |
---|
[7563] | 98 | |
---|
| 99 | |
---|
| 100 | |
---|
[7465] | 101 | // Iterate through all Leafspublic final double readLEDouble() |
---|
[7395] | 102 | for(int i = 0; i < this->bspFile->numLeafs ; i++ ) |
---|
[7353] | 103 | { |
---|
[7395] | 104 | // cluster = (this->bspFile->leaves)[i].cluster; |
---|
| 105 | leaf& curLeaf = (this->bspFile->leaves)[i]; |
---|
[7563] | 106 | if(curLeaf.cluster<0) continue; |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | /** Do Frustum culling and draw 'em all **/ |
---|
| 110 | bool inFrustum = true; |
---|
| 111 | float dMins; |
---|
| 112 | float dMaxs; |
---|
| 113 | Vector dir; |
---|
| 114 | dir.x = this->cam.x - State::getCameraTargetNode()->getLastAbsCoor().x; |
---|
| 115 | dir.y = this->cam.y - State::getCameraTargetNode()->getLastAbsCoor().y; |
---|
| 116 | dir.z = this->cam.z - State::getCameraTargetNode()->getLastAbsCoor().z; |
---|
| 117 | float dist = dir.x*this->cam.x +dir.y*this->cam.y +dir.z*this->cam.z; |
---|
| 118 | //if(dist < 0) dist = -dist; |
---|
| 119 | dMins = dir.x*(float)curLeaf.mins[0] +dir.y*(float)curLeaf.mins[1] +dir.z*(float)curLeaf.mins[2] ; |
---|
| 120 | dMaxs = dir.x*(float)curLeaf.maxs[0] +dir.y*(float)curLeaf.maxs[1] +dir.z*(float)curLeaf.maxs[2] ; |
---|
| 121 | |
---|
| 122 | if(dMins < 0.0 && dMaxs < 0.0) |
---|
| 123 | { |
---|
| 124 | //continue; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | |
---|
[7395] | 128 | // Iterate through all faces |
---|
| 129 | for (int j = 0; j < curLeaf.n_leaffaces ; ++j) { |
---|
| 130 | const int f = ( j + ((leaf *) (this->bspFile->leaves))[i].leafface) % this->bspFile->numFaces; |
---|
| 131 | if (f >=0 && !this->isAlreadyVisible(f)) { |
---|
| 132 | this->alreadyVisible[f] = true; |
---|
[7563] | 133 | |
---|
[7395] | 134 | addFace(f); // "visibleFaces.append(f)" |
---|
| 135 | } |
---|
| 136 | } |
---|
[7353] | 137 | |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | |
---|
[7395] | 141 | } //for |
---|
| 142 | } else { |
---|
| 143 | int cluster; |
---|
| 144 | int seven = 7; |
---|
| 145 | unsigned char one = 1; |
---|
| 146 | unsigned int v; |
---|
| 147 | unsigned char visSet; |
---|
[7353] | 148 | |
---|
[7395] | 149 | // Iterate through all Leafs |
---|
[7385] | 150 | |
---|
[7395] | 151 | for(int i = 0; i < this->bspFile->numLeafs ; ++i ) { |
---|
| 152 | leaf& camLeaf = (this->bspFile->leaves)[ActLeaf->leafIndex] ; |
---|
| 153 | leaf& curLeaf = (this->bspFile->leaves)[i] ; |
---|
| 154 | cluster = curLeaf.cluster; |
---|
[7563] | 155 | |
---|
| 156 | if(cluster < 0) continue; |
---|
[7395] | 157 | v = ((viscluster * ( ((int *)this->bspFile->visData)[1]) ) + (cluster / 8)); |
---|
[7563] | 158 | visSet =((unsigned char*) (this->bspFile->visData))[v + 8]; |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | if( !((visSet) & ((unsigned char)1 <<(unsigned char) (cluster & 7))) ) { |
---|
[7395] | 162 | // Iterate through all faces |
---|
[7563] | 163 | for (int j = 0; j < curLeaf.n_leaffaces ; ++j) { |
---|
[7395] | 164 | const int f = (j + curLeaf.leafface) % this->bspFile->numFaces; |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | if (!this->isAlreadyVisible(f) && f>=0) { |
---|
| 168 | this->addFace(f); |
---|
| 169 | this->alreadyVisible[f] = true; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | } |
---|
| 173 | |
---|
[7563] | 174 | }// if |
---|
[7395] | 175 | |
---|
| 176 | }//for |
---|
| 177 | |
---|
| 178 | }//else |
---|
| 179 | |
---|
| 180 | while(!this->opal.empty()) { |
---|
| 181 | this->draw_face(this->opal.front()); |
---|
| 182 | this->opal.pop_front(); |
---|
| 183 | } |
---|
| 184 | while(!this->trasparent.empty()) { |
---|
| 185 | this->draw_face(this->trasparent.back()); |
---|
| 186 | this->trasparent.pop_back(); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | |
---|
[7353] | 190 | }//draw |
---|
| 191 | |
---|
| 192 | |
---|
[7563] | 193 | |
---|
[7353] | 194 | void BspManager::draw_face(int curface) |
---|
| 195 | { |
---|
[7395] | 196 | face& curFace = (this->bspFile->faces)[curface]; |
---|
| 197 | const BspVertex* curVertex = (BspVertex *) this->bspFile->vertice; |
---|
| 198 | int stride = sizeof(BspVertex); // sizeof(Vertex) |
---|
| 199 | int offset = curFace.vertex; |
---|
| 200 | |
---|
[7353] | 201 | // PRINTF(0)("BSP Manager: "); |
---|
| 202 | // PRINTF(0)("BSP Manager: type: %i \n", curFace.texture); |
---|
[7395] | 203 | |
---|
| 204 | // if( curFace.texture < 0 ) return; |
---|
| 205 | if(curFace.type == 2) { |
---|
| 206 | this->draw_patch( &curFace); |
---|
| 207 | return; |
---|
| 208 | } |
---|
| 209 | if(curFace.type == 3) return; |
---|
[7563] | 210 | // if(this->bspFile->Materials[curFace.texture] != NULL) |
---|
| 211 | |
---|
[7544] | 212 | if(this->lastTex != curFace.texture) { |
---|
[7510] | 213 | this->bspFile->Materials[curFace.texture].mat->select(); |
---|
[7563] | 214 | this->lastTex = curFace.texture; |
---|
[7544] | 215 | } |
---|
[7563] | 216 | |
---|
| 217 | if(curFace.lm_index < 0) { |
---|
[7465] | 218 | glActiveTextureARB(GL_TEXTURE1_ARB); |
---|
[7563] | 219 | glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap ); |
---|
| 220 | glEnable(GL_TEXTURE_2D); |
---|
| 221 | } else { |
---|
| 222 | glActiveTextureARB(GL_TEXTURE1_ARB); |
---|
[7465] | 223 | glBindTexture(GL_TEXTURE_2D, this->bspFile->glLightMapTextures[curFace.lm_index]); |
---|
| 224 | glEnable(GL_TEXTURE_2D); |
---|
[7563] | 225 | } |
---|
| 226 | |
---|
| 227 | glColor4f(3.0,3.0,3.0,1.0); |
---|
[7544] | 228 | glEnableClientState(GL_VERTEX_ARRAY ); |
---|
| 229 | glEnableClientState(GL_TEXTURE_COORD_ARRAY ); |
---|
[7353] | 230 | glEnableClientState(GL_NORMAL_ARRAY ); |
---|
[7395] | 231 | // glEnableClientState(GL_COLOR_ARRAY); |
---|
| 232 | |
---|
[7563] | 233 | |
---|
[7353] | 234 | glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0])); |
---|
[7563] | 235 | |
---|
[7465] | 236 | glClientActiveTextureARB(GL_TEXTURE0_ARB); |
---|
[7353] | 237 | glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0])); |
---|
[7465] | 238 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
[7563] | 239 | |
---|
[7465] | 240 | glClientActiveTextureARB(GL_TEXTURE1_ARB); |
---|
| 241 | glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1])); |
---|
[7544] | 242 | //glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
[7353] | 243 | |
---|
[7563] | 244 | |
---|
[7353] | 245 | glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0])); |
---|
[7395] | 246 | // glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0])); |
---|
[7353] | 247 | glDrawElements(GL_TRIANGLES, curFace.n_meshverts, |
---|
[7395] | 248 | GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert])); |
---|
[7353] | 249 | |
---|
[7465] | 250 | glDisableClientState(GL_TEXTURE0_ARB); |
---|
| 251 | glDisableClientState(GL_TEXTURE1_ARB); |
---|
[7353] | 252 | glDisableClientState(GL_VERTEX_ARRAY ); |
---|
[7465] | 253 | //glDisableClientState(GL_TEXTURE_COORD_ARRAY ); |
---|
[7353] | 254 | glDisableClientState(GL_NORMAL_ARRAY ); |
---|
[7395] | 255 | // glDisableClientState(GL_COLOR_ARRAY); |
---|
| 256 | |
---|
[7353] | 257 | } |
---|
| 258 | |
---|
| 259 | |
---|
| 260 | void BspManager::draw_debug_face(int curface) |
---|
| 261 | { |
---|
[7395] | 262 | face& curFace = (this->bspFile->faces)[curface]; |
---|
| 263 | const BspVertex* curVertex = (BspVertex *) this->bspFile->vertice; |
---|
| 264 | int stride = 44; // sizeof(Vertex) |
---|
| 265 | int offset = curFace.vertex; |
---|
| 266 | |
---|
[7353] | 267 | // PRINTF(0)("BSP Manager: "); |
---|
| 268 | // PRINTF(0)("BSP Manager: type: %i \n", curFace.texture); |
---|
| 269 | |
---|
[7395] | 270 | // if( curFace.texture < 0 ) return; |
---|
| 271 | if(curFace.type == 2) { |
---|
| 272 | this->draw_patch( &curFace); |
---|
| 273 | return; |
---|
| 274 | } |
---|
| 275 | if(curFace.type == 3) return; |
---|
| 276 | // if(this->bspFile->Materials[curFace.texture] != NULL) |
---|
[7353] | 277 | |
---|
[7395] | 278 | this->bspFile->Materials[2].mat->select(); |
---|
| 279 | this->lastTex = 2; |
---|
| 280 | |
---|
[7353] | 281 | glEnableClientState(GL_VERTEX_ARRAY ); |
---|
| 282 | glEnableClientState(GL_TEXTURE_COORD_ARRAY ); |
---|
| 283 | glEnableClientState(GL_NORMAL_ARRAY ); |
---|
[7395] | 284 | //glEnableClientState(GL_COLOR_ARRAY); |
---|
[7353] | 285 | // glEnableClientState(GL_VERTEX_ARRAY ); |
---|
[7465] | 286 | glClientActiveTextureARB(GL_TEXTURE0_ARB); |
---|
[7353] | 287 | glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0])); |
---|
[7465] | 288 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
[7395] | 289 | // glClientActiveTextureARB(GL_TEXTURE0_ARB); |
---|
[7465] | 290 | glClientActiveTextureARB(GL_TEXTURE1_ARB); |
---|
[7353] | 291 | glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0])); |
---|
[7465] | 292 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
[7395] | 293 | // glClientActiveTextureARB(GL_TEXTURE1_ARB); |
---|
| 294 | // glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1])); |
---|
| 295 | //glEnableClientState(GL_NORMAL_ARRAY ); |
---|
[7353] | 296 | |
---|
| 297 | glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0])); |
---|
[7395] | 298 | // glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0])); |
---|
[7353] | 299 | glDrawElements(GL_TRIANGLES, curFace.n_meshverts, |
---|
[7395] | 300 | GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert])); |
---|
| 301 | |
---|
[7353] | 302 | } |
---|
| 303 | |
---|
| 304 | void BspManager::draw_patch(face* Face) |
---|
| 305 | { |
---|
[7544] | 306 | if(this->lastTex != Face->texture) { |
---|
[7510] | 307 | this->bspFile->Materials[Face->texture].mat->select(); |
---|
[7544] | 308 | this->lastTex = Face->texture; |
---|
| 309 | } |
---|
[7563] | 310 | |
---|
| 311 | |
---|
| 312 | |
---|
| 313 | if(Face->lm_index < 0) { |
---|
[7465] | 314 | glActiveTextureARB(GL_TEXTURE1_ARB); |
---|
[7511] | 315 | glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap); |
---|
[7465] | 316 | glEnable(GL_TEXTURE_2D); |
---|
[7563] | 317 | } else { |
---|
[7465] | 318 | glActiveTextureARB(GL_TEXTURE1_ARB); |
---|
| 319 | glBindTexture(GL_TEXTURE_2D, this->bspFile->glLightMapTextures[Face->lm_index]); |
---|
| 320 | glEnable(GL_TEXTURE_2D); |
---|
[7563] | 321 | } |
---|
| 322 | glColor4f(3.0,3.0,3.0,1.0); |
---|
| 323 | |
---|
| 324 | //glEnable( GL_AUTO_NORMAL); |
---|
[7465] | 325 | glEnableClientState(GL_VERTEX_ARRAY ); |
---|
| 326 | glEnableClientState(GL_TEXTURE_COORD_ARRAY ); |
---|
[7395] | 327 | for(int i = 0; i < Face->n_meshverts ; i++) { |
---|
[7465] | 328 | glFrontFace(GL_CW); |
---|
[7395] | 329 | //PRINTF(0)("BSP Manager: Face->size[0]: %i . \n", Face->size[0]); |
---|
[7563] | 330 | |
---|
| 331 | |
---|
[7395] | 332 | glEnableClientState(GL_NORMAL_ARRAY ); |
---|
[7353] | 333 | |
---|
[7395] | 334 | glVertexPointer(3, GL_FLOAT,44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).position[0])); |
---|
[7563] | 335 | |
---|
| 336 | |
---|
[7465] | 337 | glClientActiveTextureARB(GL_TEXTURE0_ARB); |
---|
[7395] | 338 | glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[0][0])); |
---|
[7465] | 339 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
| 340 | |
---|
[7563] | 341 | |
---|
[7465] | 342 | glClientActiveTextureARB(GL_TEXTURE1_ARB); |
---|
| 343 | glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[1][0])); |
---|
[7544] | 344 | //glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
[7465] | 345 | |
---|
[7563] | 346 | |
---|
[7395] | 347 | glNormalPointer( GL_FLOAT, 44,&((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).normal[0]) ); |
---|
[7353] | 348 | |
---|
| 349 | |
---|
| 350 | |
---|
[7563] | 351 | |
---|
[7395] | 352 | for(int row=0; row<7; ++row) { |
---|
| 353 | glDrawElements(GL_TRIANGLE_STRIP, 2*(8), GL_UNSIGNED_INT, |
---|
| 354 | & ( (((GLuint*) (this->bspFile->patchIndexes))[7*8*2*(Face->meshvert+i)+ row*2*8] )) ); |
---|
| 355 | } |
---|
[7563] | 356 | |
---|
[7465] | 357 | glFrontFace(GL_CCW); |
---|
[7395] | 358 | } |
---|
[7465] | 359 | glDisableClientState(GL_TEXTURE0_ARB); |
---|
| 360 | glDisableClientState(GL_TEXTURE1_ARB); |
---|
[7563] | 361 | //glDisable(GL_AUTO_NORMAL); |
---|
[7465] | 362 | glDisableClientState(GL_VERTEX_ARRAY ); |
---|
| 363 | glDisableClientState(GL_TEXTURE_COORD_ARRAY ); |
---|
[7563] | 364 | |
---|
[7465] | 365 | glBegin(GL_QUADS); |
---|
[7563] | 366 | |
---|
[7465] | 367 | glEnd(); |
---|
[7353] | 368 | } |
---|
| 369 | |
---|
| 370 | bool BspManager::isAlreadyVisible(int Face) |
---|
| 371 | { |
---|
[7395] | 372 | return this->alreadyVisible[Face]; |
---|
[7353] | 373 | } |
---|
| 374 | |
---|
| 375 | |
---|
| 376 | BspTreeNode* BspManager::getLeaf(BspTreeNode* node, Vector* cam) |
---|
| 377 | { |
---|
[7395] | 378 | float dist = 0; |
---|
| 379 | while(!(node->isLeaf)) { |
---|
| 380 | dist = (node->plane.x * this->cam.x + node->plane.y*this->cam.y + node->plane.z*this->cam.z) - node->d; |
---|
| 381 | if(dist >= 0.0f) { |
---|
| 382 | node = node->left; |
---|
| 383 | } else { |
---|
| 384 | node = node->right; |
---|
| 385 | |
---|
| 386 | } |
---|
| 387 | } |
---|
| 388 | return node; |
---|
[7353] | 389 | } |
---|
| 390 | |
---|
[7563] | 391 | void BspManager::checkBrushRay(brush* curBrush) |
---|
| 392 | { |
---|
| 393 | float EPSILON = 0.000001; |
---|
| 394 | float startDistance; |
---|
| 395 | float endDistance; |
---|
| 396 | |
---|
| 397 | float startFraction = -1.0f; |
---|
| 398 | float endFraction = 1.0f; |
---|
| 399 | bool startsOut = false; |
---|
| 400 | bool endsOut = false; |
---|
| 401 | |
---|
| 402 | Vector inputStart = State::getCameraTargetNode()->getLastAbsCoor(); |
---|
| 403 | Vector inputEnd = State::getCameraTargetNode()->getAbsCoor(); |
---|
| 404 | |
---|
| 405 | for (int i = 0; i < curBrush->n_brushsides; i++) |
---|
| 406 | { |
---|
| 407 | brushside& curBrushSide = this->bspFile->brushSides[curBrush->brushside + i] ; |
---|
| 408 | plane& curPlane = this->bspFile->planes[curBrushSide.plane] ; |
---|
| 409 | |
---|
| 410 | startDistance = inputStart.x * curPlane.x + inputStart.y * curPlane.y+ inputStart.z * curPlane.z - curPlane.d; |
---|
| 411 | endDistance = inputEnd.x * curPlane.x +inputEnd.y * curPlane.y +inputEnd.z * curPlane.z -curPlane.d; |
---|
| 412 | |
---|
| 413 | if (startDistance > 0) |
---|
| 414 | startsOut = true; |
---|
| 415 | if (endDistance > 0) |
---|
| 416 | endsOut = true; |
---|
| 417 | |
---|
| 418 | // make sure the trace isn't completely on one side of the brush |
---|
| 419 | if (startDistance > 0 && endDistance > 0) |
---|
| 420 | { // both are in front of the plane, its outside of this brush |
---|
| 421 | return; |
---|
| 422 | } |
---|
| 423 | if (startDistance <= 0 && endDistance <= 0) |
---|
| 424 | { // both are behind this plane, it will get clipped by another one |
---|
| 425 | continue; |
---|
| 426 | } |
---|
| 427 | |
---|
| 428 | // MMM... BEEFY |
---|
| 429 | if (startDistance > endDistance) |
---|
| 430 | { // line is entering into the brush |
---|
| 431 | float fraction = (startDistance - EPSILON) / (startDistance - endDistance); // * |
---|
| 432 | if (fraction > startFraction) |
---|
| 433 | startFraction = fraction; |
---|
| 434 | } |
---|
| 435 | else |
---|
| 436 | { // line is leaving the brush |
---|
| 437 | float fraction = (startDistance + EPSILON) / (startDistance - endDistance); // * |
---|
| 438 | if (fraction < endFraction) |
---|
| 439 | endFraction = fraction; |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | } |
---|
| 443 | if (startsOut == false) |
---|
| 444 | { |
---|
| 445 | this->outputStartsOut = false; |
---|
| 446 | if (endsOut == false) |
---|
| 447 | this->outputAllSolid = true; |
---|
| 448 | return; |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | if (startFraction < endFraction) |
---|
| 452 | { |
---|
| 453 | if (startFraction > -1.0f && startFraction < outputFraction) |
---|
| 454 | { |
---|
| 455 | if (startFraction < 0) |
---|
| 456 | startFraction = 0; |
---|
| 457 | this->outputFraction = startFraction; |
---|
| 458 | } |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | } |
---|
| 462 | |
---|
| 463 | void BspManager::checkCollisionRay(BspTreeNode* node, float startFraction, float endFraction, Vector* start, Vector* end) |
---|
| 464 | { |
---|
| 465 | |
---|
| 466 | |
---|
| 467 | float EPSILON = 0.000001; |
---|
| 468 | float endDistance = (end)->x * (node->plane.x) +(end)->y * (node->plane.y) +(end)->z * (node->plane.z) - node->d; |
---|
| 469 | float startDistance = (start)->x * (node->plane.x)+ (start)->y * (node->plane.y)+ (start)->z * (node->plane.z)- node->d; |
---|
| 470 | |
---|
| 471 | |
---|
| 472 | if(node->isLeaf) { |
---|
| 473 | leaf& curLeaf = this->bspFile->leaves[node->leafIndex]; |
---|
| 474 | for (int i = 0; i < curLeaf.n_leafbrushes ; i++) { |
---|
| 475 | brush& curBrush = this->bspFile->brushes[((int*)(this->bspFile->leafBrushes))[curLeaf.leafbrush_first+i]]; |
---|
| 476 | //object *brush = &BSP.brushes[BSP.leafBrushes[leaf->firstLeafBrush + i]]; |
---|
| 477 | if (curBrush.n_brushsides > 0 && |
---|
| 478 | ((((BspTexture*)(this->bspFile->textures))[curBrush.texture]).contents & 1)) |
---|
| 479 | // CheckBrush( brush ); |
---|
| 480 | this->checkBrushRay(&curBrush); |
---|
| 481 | if(curBrush.n_brushsides <=0) this->outputAllSolid = true; |
---|
| 482 | } |
---|
| 483 | return; |
---|
| 484 | } |
---|
| 485 | |
---|
| 486 | |
---|
| 487 | if (startDistance >= 0 && endDistance >= 0) // A |
---|
| 488 | { // both points are in front of the plane |
---|
| 489 | // so check the front child |
---|
| 490 | this->checkCollisionRay(node->left,0,0,start,end); |
---|
| 491 | } else if (startDistance < 0 && endDistance < 0) // B |
---|
| 492 | { // both points are behind the plane |
---|
| 493 | // so check the back child |
---|
| 494 | this->checkCollisionRay(node->right,0,0,start,end); |
---|
| 495 | } else // C |
---|
| 496 | { // the line spans the splitting plane |
---|
| 497 | int side; |
---|
| 498 | float fraction1, fraction2, middleFraction; |
---|
| 499 | Vector middle; |
---|
| 500 | |
---|
| 501 | // STEP 1: split the segment into two |
---|
| 502 | if (startDistance < endDistance) { |
---|
| 503 | side = 1; // back |
---|
| 504 | float inverseDistance = 1.0f / (startDistance - endDistance); |
---|
| 505 | fraction1 = (startDistance + EPSILON) * inverseDistance; |
---|
| 506 | fraction2 = (startDistance + EPSILON) * inverseDistance; |
---|
| 507 | } else if (endDistance < startDistance) { |
---|
| 508 | side = 0; // front(start)->x * (node->plane.x)+ |
---|
| 509 | float inverseDistance = 1.0f / (startDistance - endDistance); |
---|
| 510 | fraction1 = (startDistance + EPSILON) * inverseDistance; |
---|
| 511 | fraction2 = (startDistance - EPSILON) * inverseDistance; |
---|
| 512 | } else { |
---|
| 513 | side = 0; // front |
---|
| 514 | fraction1 = 1.0f; |
---|
| 515 | fraction2 = 0.0f; |
---|
| 516 | } |
---|
| 517 | |
---|
| 518 | // STEP 2: make sure the numbers are valid |
---|
| 519 | if (fraction1 < 0.0f) fraction1 = 0.0f; |
---|
| 520 | else if (fraction1 > 1.0f) fraction1 = 1.0f; |
---|
| 521 | if (fraction2 < 0.0f) fraction2 = 0.0f; |
---|
| 522 | else if (fraction2 > 1.0f) fraction2 = 1.0f; |
---|
| 523 | |
---|
| 524 | // STEP 3: calculate the middle point for the first side |
---|
| 525 | middleFraction = startFraction + |
---|
| 526 | (endFraction - startFraction) * fraction1; |
---|
| 527 | |
---|
| 528 | middle.x = start->x + fraction1 * (end->x - start->x); |
---|
| 529 | middle.y = start->y + fraction1 * (end->y - start->y); |
---|
| 530 | middle.z = start->z + fraction1 * (end->z - start->z); |
---|
| 531 | |
---|
| 532 | // STEP 4: check the first side |
---|
| 533 | //CheckNode( node->children[side], startFraction, middleFraction, start, middle ); |
---|
| 534 | if(side == 0) this->checkCollisionRay(node->left,startFraction, middleFraction, start, &middle ); |
---|
| 535 | |
---|
| 536 | else this->checkCollisionRay(node->right,startFraction, middleFraction, |
---|
| 537 | start, &middle ); |
---|
| 538 | |
---|
| 539 | // STEP 5: calculate the middle point for the second side |
---|
| 540 | middleFraction = startFraction + |
---|
| 541 | (endFraction - startFraction) * fraction2; |
---|
| 542 | |
---|
| 543 | middle.x = start->x + fraction2 * (end->x - start->x); |
---|
| 544 | middle.y = start->y + fraction2 * (end->y - start->y); |
---|
| 545 | middle.z = start->z + fraction2 * (end->z - start->z); |
---|
| 546 | |
---|
| 547 | // STEP 6: check the second side |
---|
| 548 | if(side == 1)this->checkCollisionRay(node->left,middleFraction, endFraction, &middle, end); |
---|
| 549 | |
---|
| 550 | else this->checkCollisionRay(node->right,middleFraction, endFraction,&middle, end ); |
---|
| 551 | |
---|
| 552 | |
---|
| 553 | } |
---|
| 554 | |
---|
| 555 | } |
---|
[7353] | 556 | void BspManager::checkCollision(BspTreeNode* node, Vector* cam) |
---|
| 557 | { |
---|
[7563] | 558 | Vector next = this->cam; |
---|
| 559 | next.x = (State::getCameraTargetNode()->getLastAbsCoor()).x ; |
---|
| 560 | next.y = (State::getCameraTargetNode()->getLastAbsCoor()).y ; |
---|
| 561 | next.z = (State::getCameraTargetNode()->getLastAbsCoor()).z ; |
---|
| 562 | |
---|
[7395] | 563 | float dist = 0; |
---|
| 564 | if(!(node->isLeaf)) { |
---|
| 565 | dist = (node->plane.x * this->cam.x + node->plane.y*this->cam.y + node->plane.z*this->cam.z) - node->d; |
---|
| 566 | if(dist > 4.0f) { |
---|
| 567 | checkCollision(node->left,cam); |
---|
| 568 | return; |
---|
| 569 | } |
---|
| 570 | if(dist < -4.0f) { |
---|
| 571 | checkCollision(node->right,cam); |
---|
| 572 | return; |
---|
| 573 | } |
---|
| 574 | if(dist<=4.0f && dist >= -4.0f) { |
---|
| 575 | checkCollision(node->left,cam); |
---|
| 576 | checkCollision(node->right,cam); |
---|
| 577 | return; |
---|
| 578 | } |
---|
| 579 | return; |
---|
| 580 | } else { |
---|
[7353] | 581 | |
---|
[7395] | 582 | leaf& camLeaf = ((leaf *)(this->bspFile->leaves))[(node->leafIndex ) ]; |
---|
[7353] | 583 | |
---|
[7563] | 584 | if (camLeaf.cluster < 0) {this->drawDebugCube(&this->cam); this->drawDebugCube(&next);} |
---|
[7395] | 585 | |
---|
| 586 | |
---|
| 587 | /* |
---|
[7511] | 588 | for(int i = 0; i < camLeaf.n_leafbrushes && i < 10; i++ ) |
---|
| 589 | { |
---|
| 590 | brush& curBrush = ((brush*)(this->bspFile->brushes))[(camLeaf.leafbrush_first +i)%this->bspFile->numLeafBrushes]; |
---|
| 591 | if(curBrush.n_brushsides < 0) return; |
---|
| 592 | for(int j = 0; j < curBrush.n_brushsides; j++) |
---|
| 593 | { |
---|
| 594 | float dist = -0.1; |
---|
| 595 | brushside& curBrushSide = ((brushside*)(this->bspFile->brushSides))[(curBrush.brushside +j)%this->bspFile->numBrushSides]; |
---|
| 596 | plane& testPlane = ((plane*)(this->bspFile->planes))[curBrushSide.plane % this->bspFile->numPlanes]; |
---|
| 597 | dist = testPlane.x * this->cam.x + testPlane.y * this->cam.y + testPlane.z * this->cam.z -testPlane.d ; |
---|
| 598 | |
---|
| 599 | if(dist < -0.01f) dist = -1.0f *dist; |
---|
| 600 | if(dist < 1.0f){ |
---|
| 601 | this->drawDebugCube(&this->cam); |
---|
| 602 | return; |
---|
| 603 | } |
---|
| 604 | } |
---|
| 605 | |
---|
| 606 | } */ |
---|
[7395] | 607 | |
---|
| 608 | } |
---|
| 609 | return; |
---|
[7353] | 610 | } |
---|
| 611 | |
---|
| 612 | void BspManager::drawDebugCube(Vector* cam) |
---|
| 613 | { |
---|
[7395] | 614 | glBegin(GL_QUADS); |
---|
[7353] | 615 | |
---|
[7395] | 616 | // Bottom Face. Red, 75% opaque, magnified texture |
---|
[7353] | 617 | |
---|
[7395] | 618 | glNormal3f( 0.0f, -1.0f, 0.0f); // Needed for lighting |
---|
| 619 | glColor4f(0.9,0.2,0.2,.75); // Basic polygon color |
---|
[7353] | 620 | |
---|
[7395] | 621 | glTexCoord2f(0.800f, 0.800f); glVertex3f(cam->x-1.0f, cam->y-1.0f,cam->z -1.0f); |
---|
| 622 | glTexCoord2f(0.200f, 0.800f); glVertex3f(cam->x+1.0f, cam->y-1.0f,cam->z -1.0f); |
---|
| 623 | glTexCoord2f(0.200f, 0.200f); glVertex3f(cam->x+ 1.0f,cam->y -1.0f,cam->z + 1.0f); |
---|
| 624 | glTexCoord2f(0.800f, 0.200f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z + 1.0f); |
---|
[7353] | 625 | |
---|
| 626 | |
---|
[7395] | 627 | // Top face; offset. White, 50% opaque. |
---|
[7353] | 628 | |
---|
[7395] | 629 | glNormal3f( 0.0f, 1.0f, 0.0f); glColor4f(0.5,0.5,0.5,.5); |
---|
[7353] | 630 | |
---|
[7395] | 631 | glTexCoord2f(0.005f, 1.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.0f); |
---|
| 632 | glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z +1.0f); |
---|
| 633 | glTexCoord2f(1.995f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y+1.0f, cam->z +1.0f); |
---|
| 634 | glTexCoord2f(1.995f, 1.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z -1.0f); |
---|
[7353] | 635 | |
---|
| 636 | |
---|
[7395] | 637 | // Far face. Green, 50% opaque, non-uniform texture cooridinates. |
---|
[7353] | 638 | |
---|
[7395] | 639 | glNormal3f( 0.0f, 0.0f,-1.0f); glColor4f(0.2,0.9,0.2,.5); |
---|
[7353] | 640 | |
---|
[7395] | 641 | glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z -1.3f); |
---|
| 642 | glTexCoord2f(2.995f, 2.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.3f); |
---|
| 643 | glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x+ 1.0f,cam->y+ 1.0f, cam->z -1.3f); |
---|
| 644 | glTexCoord2f(0.005f, 0.005f); glVertex3f( cam->x+1.0f,cam->y -1.0f, cam->z -1.3f); |
---|
[7353] | 645 | |
---|
| 646 | |
---|
[7395] | 647 | // Right face. Blue; 25% opaque |
---|
[7353] | 648 | |
---|
[7395] | 649 | glNormal3f( 1.0f, 0.0f, 0.0f); glColor4f(0.2,0.2,0.9,.25); |
---|
[7353] | 650 | |
---|
[7465] | 651 | glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y -1.0f, cam->z -1.0f); |
---|
[7395] | 652 | glTexCoord2f(0.995f, 0.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z -1.0f); |
---|
| 653 | glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x+ 1.0f, cam->y+ 1.0f, cam->z + 1.0f); |
---|
| 654 | glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y-1.0f, cam->z +1.0f); |
---|
[7353] | 655 | |
---|
| 656 | |
---|
[7395] | 657 | // Front face; offset. Multi-colored, 50% opaque. |
---|
[7353] | 658 | |
---|
[7395] | 659 | glNormal3f( 0.0f, 0.0f, 1.0f); |
---|
[7353] | 660 | |
---|
[7395] | 661 | glColor4f( 0.9f, 0.2f, 0.2f, 0.5f); |
---|
| 662 | glTexCoord2f( 0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z +1.0f); |
---|
| 663 | glColor4f( 0.2f, 0.9f, 0.2f, 0.5f); |
---|
| 664 | glTexCoord2f( 0.995f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y-1.0f, cam->z +1.0f); |
---|
| 665 | glColor4f( 0.2f, 0.2f, 0.9f, 0.5f); |
---|
| 666 | glTexCoord2f( 0.995f, 0.995f); glVertex3f( cam->x+1.0f, cam->y+1.0f, cam->z +1.0f); |
---|
| 667 | glColor4f( 0.1f, 0.1f, 0.1f, 0.5f); |
---|
| 668 | glTexCoord2f( 0.005f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z +1.0f); |
---|
| 669 | |
---|
| 670 | |
---|
| 671 | // Left Face; offset. Yellow, varying levels of opaque. |
---|
| 672 | |
---|
| 673 | glNormal3f(-1.0f, 0.0f, 0.0f); |
---|
| 674 | |
---|
| 675 | glColor4f(0.9,0.9,0.2,0.0); |
---|
| 676 | glTexCoord2f(0.005f, 0.005f); glVertex3f(cam->x-1.0f, cam->y-1.0f, cam->z -1.0f); |
---|
| 677 | glColor4f(0.9,0.9,0.2,0.66); |
---|
| 678 | glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x-1.0f,cam->y -1.0f, cam->z +1.0f); |
---|
| 679 | glColor4f(0.9,0.9,0.2,1.0); |
---|
| 680 | glTexCoord2f(0.995f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z +1.0f); |
---|
| 681 | glColor4f(0.9,0.9,0.2,0.33); |
---|
| 682 | glTexCoord2f(0.005f, 0.995f); glVertex3f(cam->x-1.0f, cam->y+ 1.0f, cam->z -1.0f); |
---|
| 683 | |
---|
| 684 | glEnd(); |
---|
[7385] | 685 | } |
---|
[7353] | 686 | |
---|
[7385] | 687 | void BspManager::addFace(int f) |
---|
| 688 | { |
---|
[7395] | 689 | face& curFace = ((face *)(this->bspFile->faces))[f]; |
---|
| 690 | if(this->bspFile->Materials[curFace.texture].alpha) this->trasparent.push_back(f); |
---|
| 691 | else this->opal.push_back(f); |
---|
| 692 | } |
---|