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 | |
---|
27 | // STL Containers |
---|
28 | #include <vector> |
---|
29 | #include <deque> |
---|
30 | |
---|
31 | |
---|
32 | |
---|
33 | |
---|
34 | BspManager::BspManager() |
---|
35 | { |
---|
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]; |
---|
42 | } |
---|
43 | |
---|
44 | void BspManager::draw() |
---|
45 | { |
---|
46 | |
---|
47 | |
---|
48 | |
---|
49 | |
---|
50 | |
---|
51 | // Draw Debug Terrain |
---|
52 | /* |
---|
53 | this->bspFile->Materials[0]->select(); |
---|
54 | for(int i = 0; i < this->bspFile->numPatches ; i++) |
---|
55 | { |
---|
56 | this->bspFile->VertexArrayModels[i]->draw(); |
---|
57 | |
---|
58 | } |
---|
59 | */ |
---|
60 | |
---|
61 | |
---|
62 | |
---|
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(); |
---|
68 | // Find all visible faces... |
---|
69 | |
---|
70 | this->cam = State::getCamera()->getAbsCoor() ; |
---|
71 | this->ship = State::getCameraTargetNode()->getAbsCoor(); |
---|
72 | |
---|
73 | |
---|
74 | |
---|
75 | |
---|
76 | |
---|
77 | this->viewDir= State::getCameraTarget()->getAbsCoor() - State::getCamera()->getAbsCoor() ; |
---|
78 | float d = (cam.x*viewDir.x + cam.y*viewDir.y + cam.z * viewDir.z); |
---|
79 | |
---|
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) |
---|
83 | |
---|
84 | |
---|
85 | |
---|
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); |
---|
95 | |
---|
96 | if (true || (((int *)(this->bspFile->header))[35] == 0 ) || viscluster < 0) //!< if (sizeof(Visdata) == 0) |
---|
97 | { |
---|
98 | |
---|
99 | |
---|
100 | |
---|
101 | // Iterate through all Leafspublic final double readLEDouble() |
---|
102 | for(int i = 0; i < this->bspFile->numLeafs ; i++ ) |
---|
103 | { |
---|
104 | // cluster = (this->bspFile->leaves)[i].cluster; |
---|
105 | leaf& curLeaf = (this->bspFile->leaves)[i]; |
---|
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 | |
---|
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; |
---|
133 | |
---|
134 | addFace(f); // "visibleFaces.append(f)" |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | |
---|
140 | |
---|
141 | } //for |
---|
142 | } else { |
---|
143 | int cluster; |
---|
144 | int seven = 7; |
---|
145 | unsigned char one = 1; |
---|
146 | unsigned int v; |
---|
147 | unsigned char visSet; |
---|
148 | |
---|
149 | // Iterate through all Leafs |
---|
150 | |
---|
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; |
---|
155 | |
---|
156 | if(cluster < 0) continue; |
---|
157 | v = ((viscluster * ( ((int *)this->bspFile->visData)[1]) ) + (cluster / 8)); |
---|
158 | visSet =((unsigned char*) (this->bspFile->visData))[v + 8]; |
---|
159 | |
---|
160 | |
---|
161 | if( !((visSet) & ((unsigned char)1 <<(unsigned char) (cluster & 7))) ) { |
---|
162 | // Iterate through all faces |
---|
163 | for (int j = 0; j < curLeaf.n_leaffaces ; ++j) { |
---|
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 | |
---|
174 | }// if |
---|
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 | |
---|
190 | }//draw |
---|
191 | |
---|
192 | |
---|
193 | |
---|
194 | void BspManager::draw_face(int curface) |
---|
195 | { |
---|
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 | |
---|
201 | // PRINTF(0)("BSP Manager: "); |
---|
202 | // PRINTF(0)("BSP Manager: type: %i \n", curFace.texture); |
---|
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; |
---|
210 | // if(this->bspFile->Materials[curFace.texture] != NULL) |
---|
211 | |
---|
212 | if(this->lastTex != curFace.texture) { |
---|
213 | this->bspFile->Materials[curFace.texture].mat->select(); |
---|
214 | this->lastTex = curFace.texture; |
---|
215 | } |
---|
216 | |
---|
217 | if(curFace.lm_index < 0) { |
---|
218 | glActiveTextureARB(GL_TEXTURE1_ARB); |
---|
219 | glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap ); |
---|
220 | glEnable(GL_TEXTURE_2D); |
---|
221 | } else { |
---|
222 | glActiveTextureARB(GL_TEXTURE1_ARB); |
---|
223 | glBindTexture(GL_TEXTURE_2D, this->bspFile->glLightMapTextures[curFace.lm_index]); |
---|
224 | glEnable(GL_TEXTURE_2D); |
---|
225 | } |
---|
226 | |
---|
227 | glColor4f(3.0,3.0,3.0,1.0); |
---|
228 | glEnableClientState(GL_VERTEX_ARRAY ); |
---|
229 | glEnableClientState(GL_TEXTURE_COORD_ARRAY ); |
---|
230 | glEnableClientState(GL_NORMAL_ARRAY ); |
---|
231 | // glEnableClientState(GL_COLOR_ARRAY); |
---|
232 | |
---|
233 | |
---|
234 | glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0])); |
---|
235 | |
---|
236 | glClientActiveTextureARB(GL_TEXTURE0_ARB); |
---|
237 | glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0])); |
---|
238 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
239 | |
---|
240 | glClientActiveTextureARB(GL_TEXTURE1_ARB); |
---|
241 | glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1])); |
---|
242 | //glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
243 | |
---|
244 | |
---|
245 | glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0])); |
---|
246 | // glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0])); |
---|
247 | glDrawElements(GL_TRIANGLES, curFace.n_meshverts, |
---|
248 | GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert])); |
---|
249 | |
---|
250 | glDisableClientState(GL_TEXTURE0_ARB); |
---|
251 | glDisableClientState(GL_TEXTURE1_ARB); |
---|
252 | glDisableClientState(GL_VERTEX_ARRAY ); |
---|
253 | //glDisableClientState(GL_TEXTURE_COORD_ARRAY ); |
---|
254 | glDisableClientState(GL_NORMAL_ARRAY ); |
---|
255 | // glDisableClientState(GL_COLOR_ARRAY); |
---|
256 | |
---|
257 | } |
---|
258 | |
---|
259 | |
---|
260 | void BspManager::draw_debug_face(int curface) |
---|
261 | { |
---|
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 | |
---|
267 | // PRINTF(0)("BSP Manager: "); |
---|
268 | // PRINTF(0)("BSP Manager: type: %i \n", curFace.texture); |
---|
269 | |
---|
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) |
---|
277 | |
---|
278 | this->bspFile->Materials[2].mat->select(); |
---|
279 | this->lastTex = 2; |
---|
280 | |
---|
281 | glEnableClientState(GL_VERTEX_ARRAY ); |
---|
282 | glEnableClientState(GL_TEXTURE_COORD_ARRAY ); |
---|
283 | glEnableClientState(GL_NORMAL_ARRAY ); |
---|
284 | //glEnableClientState(GL_COLOR_ARRAY); |
---|
285 | // glEnableClientState(GL_VERTEX_ARRAY ); |
---|
286 | glClientActiveTextureARB(GL_TEXTURE0_ARB); |
---|
287 | glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0])); |
---|
288 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
289 | // glClientActiveTextureARB(GL_TEXTURE0_ARB); |
---|
290 | glClientActiveTextureARB(GL_TEXTURE1_ARB); |
---|
291 | glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0])); |
---|
292 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
293 | // glClientActiveTextureARB(GL_TEXTURE1_ARB); |
---|
294 | // glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1])); |
---|
295 | //glEnableClientState(GL_NORMAL_ARRAY ); |
---|
296 | |
---|
297 | glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0])); |
---|
298 | // glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0])); |
---|
299 | glDrawElements(GL_TRIANGLES, curFace.n_meshverts, |
---|
300 | GL_UNSIGNED_INT, &(((meshvert *)this->bspFile->meshverts) [curFace.meshvert])); |
---|
301 | |
---|
302 | } |
---|
303 | |
---|
304 | void BspManager::draw_patch(face* Face) |
---|
305 | { |
---|
306 | if(this->lastTex != Face->texture) { |
---|
307 | this->bspFile->Materials[Face->texture].mat->select(); |
---|
308 | this->lastTex = Face->texture; |
---|
309 | } |
---|
310 | |
---|
311 | |
---|
312 | |
---|
313 | if(Face->lm_index < 0) { |
---|
314 | glActiveTextureARB(GL_TEXTURE1_ARB); |
---|
315 | glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap); |
---|
316 | glEnable(GL_TEXTURE_2D); |
---|
317 | } else { |
---|
318 | glActiveTextureARB(GL_TEXTURE1_ARB); |
---|
319 | glBindTexture(GL_TEXTURE_2D, this->bspFile->glLightMapTextures[Face->lm_index]); |
---|
320 | glEnable(GL_TEXTURE_2D); |
---|
321 | } |
---|
322 | glColor4f(3.0,3.0,3.0,1.0); |
---|
323 | |
---|
324 | //glEnable( GL_AUTO_NORMAL); |
---|
325 | glEnableClientState(GL_VERTEX_ARRAY ); |
---|
326 | glEnableClientState(GL_TEXTURE_COORD_ARRAY ); |
---|
327 | for(int i = 0; i < Face->n_meshverts ; i++) { |
---|
328 | glFrontFace(GL_CW); |
---|
329 | //PRINTF(0)("BSP Manager: Face->size[0]: %i . \n", Face->size[0]); |
---|
330 | |
---|
331 | |
---|
332 | glEnableClientState(GL_NORMAL_ARRAY ); |
---|
333 | |
---|
334 | glVertexPointer(3, GL_FLOAT,44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).position[0])); |
---|
335 | |
---|
336 | |
---|
337 | glClientActiveTextureARB(GL_TEXTURE0_ARB); |
---|
338 | glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[0][0])); |
---|
339 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
340 | |
---|
341 | |
---|
342 | glClientActiveTextureARB(GL_TEXTURE1_ARB); |
---|
343 | glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[1][0])); |
---|
344 | //glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
345 | |
---|
346 | |
---|
347 | glNormalPointer( GL_FLOAT, 44,&((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).normal[0]) ); |
---|
348 | |
---|
349 | |
---|
350 | |
---|
351 | |
---|
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 | } |
---|
356 | |
---|
357 | glFrontFace(GL_CCW); |
---|
358 | } |
---|
359 | glDisableClientState(GL_TEXTURE0_ARB); |
---|
360 | glDisableClientState(GL_TEXTURE1_ARB); |
---|
361 | //glDisable(GL_AUTO_NORMAL); |
---|
362 | glDisableClientState(GL_VERTEX_ARRAY ); |
---|
363 | glDisableClientState(GL_TEXTURE_COORD_ARRAY ); |
---|
364 | |
---|
365 | glBegin(GL_QUADS); |
---|
366 | |
---|
367 | glEnd(); |
---|
368 | } |
---|
369 | |
---|
370 | bool BspManager::isAlreadyVisible(int Face) |
---|
371 | { |
---|
372 | return this->alreadyVisible[Face]; |
---|
373 | } |
---|
374 | |
---|
375 | |
---|
376 | BspTreeNode* BspManager::getLeaf(BspTreeNode* node, Vector* cam) |
---|
377 | { |
---|
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; |
---|
389 | } |
---|
390 | |
---|
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 | } |
---|
556 | void BspManager::checkCollision(BspTreeNode* node, Vector* cam) |
---|
557 | { |
---|
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 | |
---|
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 { |
---|
581 | |
---|
582 | leaf& camLeaf = ((leaf *)(this->bspFile->leaves))[(node->leafIndex ) ]; |
---|
583 | |
---|
584 | if (camLeaf.cluster < 0) {this->drawDebugCube(&this->cam); this->drawDebugCube(&next);} |
---|
585 | |
---|
586 | |
---|
587 | /* |
---|
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 | } */ |
---|
607 | |
---|
608 | } |
---|
609 | return; |
---|
610 | } |
---|
611 | |
---|
612 | void BspManager::drawDebugCube(Vector* cam) |
---|
613 | { |
---|
614 | glBegin(GL_QUADS); |
---|
615 | |
---|
616 | // Bottom Face. Red, 75% opaque, magnified texture |
---|
617 | |
---|
618 | glNormal3f( 0.0f, -1.0f, 0.0f); // Needed for lighting |
---|
619 | glColor4f(0.9,0.2,0.2,.75); // Basic polygon color |
---|
620 | |
---|
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); |
---|
625 | |
---|
626 | |
---|
627 | // Top face; offset. White, 50% opaque. |
---|
628 | |
---|
629 | glNormal3f( 0.0f, 1.0f, 0.0f); glColor4f(0.5,0.5,0.5,.5); |
---|
630 | |
---|
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); |
---|
635 | |
---|
636 | |
---|
637 | // Far face. Green, 50% opaque, non-uniform texture cooridinates. |
---|
638 | |
---|
639 | glNormal3f( 0.0f, 0.0f,-1.0f); glColor4f(0.2,0.9,0.2,.5); |
---|
640 | |
---|
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); |
---|
645 | |
---|
646 | |
---|
647 | // Right face. Blue; 25% opaque |
---|
648 | |
---|
649 | glNormal3f( 1.0f, 0.0f, 0.0f); glColor4f(0.2,0.2,0.9,.25); |
---|
650 | |
---|
651 | glTexCoord2f(0.995f, 0.005f); glVertex3f(cam->x+ 1.0f, cam->y -1.0f, cam->z -1.0f); |
---|
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); |
---|
655 | |
---|
656 | |
---|
657 | // Front face; offset. Multi-colored, 50% opaque. |
---|
658 | |
---|
659 | glNormal3f( 0.0f, 0.0f, 1.0f); |
---|
660 | |
---|
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(); |
---|
685 | } |
---|
686 | |
---|
687 | void BspManager::addFace(int f) |
---|
688 | { |
---|
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 | } |
---|