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