| 1 |
/* Emacs style mode select -*- C -*- |
| 2 |
*----------------------------------------------------------------------------- |
| 3 |
* |
| 4 |
* |
| 5 |
* PrBoom: a Doom port merged with LxDoom and LSDLDoom |
| 6 |
* based on BOOM, a modified and improved DOOM engine |
| 7 |
* Copyright (C) 1999 by |
| 8 |
* id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman |
| 9 |
* Copyright (C) 1999-2000 by |
| 10 |
* Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze |
| 11 |
* Copyright 2005, 2006 by |
| 12 |
* Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko |
| 13 |
* |
| 14 |
* This program is free software; you can redistribute it and/or |
| 15 |
* modify it under the terms of the GNU General Public License |
| 16 |
* as published by the Free Software Foundation; either version 2 |
| 17 |
* of the License, or (at your option) any later version. |
| 18 |
* |
| 19 |
* This program is distributed in the hope that it will be useful, |
| 20 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
* GNU General Public License for more details. |
| 23 |
* |
| 24 |
* You should have received a copy of the GNU General Public License |
| 25 |
* along with this program; if not, write to the Free Software |
| 26 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 27 |
* 02111-1307, USA. |
| 28 |
* |
| 29 |
* DESCRIPTION: |
| 30 |
* |
| 31 |
*--------------------------------------------------------------------- |
| 32 |
*/ |
| 33 |
|
| 34 |
/* |
| 35 |
* |
| 36 |
** gl_clipper.cpp |
| 37 |
** |
| 38 |
** Handles visibility checks. |
| 39 |
** Loosely based on the JDoom clipper. |
| 40 |
** |
| 41 |
**--------------------------------------------------------------------------- |
| 42 |
** Copyright 2003 Tim Stump |
| 43 |
** All rights reserved. |
| 44 |
** |
| 45 |
** Redistribution and use in source and binary forms, with or without |
| 46 |
** modification, are permitted provided that the following conditions |
| 47 |
** are met: |
| 48 |
** |
| 49 |
** 1. Redistributions of source code must retain the above copyright |
| 50 |
** notice, this list of conditions and the following disclaimer. |
| 51 |
** 2. Redistributions in binary form must reproduce the above copyright |
| 52 |
** notice, this list of conditions and the following disclaimer in the |
| 53 |
** documentation and/or other materials provided with the distribution. |
| 54 |
** 3. The name of the author may not be used to endorse or promote products |
| 55 |
** derived from this software without specific prior written permission. |
| 56 |
** |
| 57 |
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 58 |
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 59 |
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 60 |
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 61 |
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 62 |
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 63 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 64 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 65 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 66 |
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 67 |
**--------------------------------------------------------------------------- |
| 68 |
** |
| 69 |
*/ |
| 70 |
|
| 71 |
#include <SDL_opengl.h> |
| 72 |
#include <math.h> |
| 73 |
#include "v_video.h" |
| 74 |
#include "gl_intern.h" |
| 75 |
#include "r_main.h" |
| 76 |
#include "e6y.h" |
| 77 |
|
| 78 |
float frustum[6][4]; |
| 79 |
|
| 80 |
typedef struct clipnode_s |
| 81 |
{ |
| 82 |
struct clipnode_s *prev, *next; |
| 83 |
angle_t start, end; |
| 84 |
} clipnode_t; |
| 85 |
|
| 86 |
clipnode_t *freelist; |
| 87 |
clipnode_t *clipnodes; |
| 88 |
clipnode_t *cliphead; |
| 89 |
|
| 90 |
static clipnode_t * gld_clipnode_GetNew(void); |
| 91 |
static clipnode_t * gld_clipnode_NewRange(angle_t start, angle_t end); |
| 92 |
static dboolean gld_clipper_IsRangeVisible(angle_t startAngle, angle_t endAngle); |
| 93 |
static void gld_clipper_AddClipRange(angle_t start, angle_t end); |
| 94 |
static void gld_clipper_RemoveRange(clipnode_t * range); |
| 95 |
static void gld_clipnode_Free(clipnode_t *node); |
| 96 |
|
| 97 |
static clipnode_t * gld_clipnode_GetNew(void) |
| 98 |
{ |
| 99 |
if (freelist) |
| 100 |
{ |
| 101 |
clipnode_t * p = freelist; |
| 102 |
freelist = p->next; |
| 103 |
return p; |
| 104 |
} |
| 105 |
else |
| 106 |
{ |
| 107 |
return Z_Malloc(sizeof(clipnode_t)); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
static clipnode_t * gld_clipnode_NewRange(angle_t start, angle_t end) |
| 112 |
{ |
| 113 |
clipnode_t * c = gld_clipnode_GetNew(); |
| 114 |
c->start = start; |
| 115 |
c->end = end; |
| 116 |
c->next = c->prev=NULL; |
| 117 |
return c; |
| 118 |
} |
| 119 |
|
| 120 |
dboolean gld_clipper_SafeCheckRange(angle_t startAngle, angle_t endAngle) |
| 121 |
{ |
| 122 |
if(startAngle > endAngle) |
| 123 |
{ |
| 124 |
return (gld_clipper_IsRangeVisible(startAngle, ANGLE_MAX) || gld_clipper_IsRangeVisible(0, endAngle)); |
| 125 |
} |
| 126 |
|
| 127 |
return gld_clipper_IsRangeVisible(startAngle, endAngle); |
| 128 |
} |
| 129 |
|
| 130 |
static dboolean gld_clipper_IsRangeVisible(angle_t startAngle, angle_t endAngle) |
| 131 |
{ |
| 132 |
clipnode_t *ci; |
| 133 |
ci = cliphead; |
| 134 |
|
| 135 |
if (endAngle == 0 && ci && ci->start == 0) |
| 136 |
return false; |
| 137 |
|
| 138 |
while (ci != NULL && ci->start < endAngle) |
| 139 |
{ |
| 140 |
if (startAngle >= ci->start && endAngle <= ci->end) |
| 141 |
{ |
| 142 |
return false; |
| 143 |
} |
| 144 |
ci = ci->next; |
| 145 |
} |
| 146 |
|
| 147 |
return true; |
| 148 |
} |
| 149 |
|
| 150 |
static void gld_clipnode_Free(clipnode_t *node) |
| 151 |
{ |
| 152 |
node->next = freelist; |
| 153 |
freelist = node; |
| 154 |
} |
| 155 |
|
| 156 |
static void gld_clipper_RemoveRange(clipnode_t *range) |
| 157 |
{ |
| 158 |
if (range == cliphead) |
| 159 |
{ |
| 160 |
cliphead = cliphead->next; |
| 161 |
} |
| 162 |
else |
| 163 |
{ |
| 164 |
if (range->prev) |
| 165 |
{ |
| 166 |
range->prev->next = range->next; |
| 167 |
} |
| 168 |
if (range->next) |
| 169 |
{ |
| 170 |
range->next->prev = range->prev; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
gld_clipnode_Free(range); |
| 175 |
} |
| 176 |
|
| 177 |
void gld_clipper_SafeAddClipRange(angle_t startangle, angle_t endangle) |
| 178 |
{ |
| 179 |
if(startangle > endangle) |
| 180 |
{ |
| 181 |
// The range has to added in two parts. |
| 182 |
gld_clipper_AddClipRange(startangle, ANGLE_MAX); |
| 183 |
gld_clipper_AddClipRange(0, endangle); |
| 184 |
} |
| 185 |
else |
| 186 |
{ |
| 187 |
// Add the range as usual. |
| 188 |
gld_clipper_AddClipRange(startangle, endangle); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
angle_t gld_clipper_AngleToPseudo(angle_t ang) |
| 193 |
{ |
| 194 |
double vecx = cos(ang * M_PI / ANG180); |
| 195 |
double vecy = sin(ang * M_PI / ANG180); |
| 196 |
|
| 197 |
double result = vecy / (fabs(vecx) + fabs(vecy)); |
| 198 |
if (vecx < 0) |
| 199 |
{ |
| 200 |
result = 2.f - result; |
| 201 |
} |
| 202 |
return (angle_t)xs_CRoundToUInt(result * (1<<30)); |
| 203 |
} |
| 204 |
|
| 205 |
static void gld_clipper_SafeAddClipRangeRealAngles(angle_t startangle, angle_t endangle) |
| 206 |
{ |
| 207 |
gld_clipper_SafeAddClipRange( |
| 208 |
gld_clipper_AngleToPseudo(startangle), |
| 209 |
gld_clipper_AngleToPseudo(endangle)); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
static void gld_clipper_AddClipRange(angle_t start, angle_t end) |
| 214 |
{ |
| 215 |
clipnode_t *node, *temp, *prevNode, *node2, *delnode; |
| 216 |
|
| 217 |
if (cliphead) |
| 218 |
{ |
| 219 |
//check to see if range contains any old ranges |
| 220 |
node = cliphead; |
| 221 |
while (node != NULL && node->start < end) |
| 222 |
{ |
| 223 |
if (node->start >= start && node->end <= end) |
| 224 |
{ |
| 225 |
temp = node; |
| 226 |
node = node->next; |
| 227 |
gld_clipper_RemoveRange(temp); |
| 228 |
} |
| 229 |
else |
| 230 |
{ |
| 231 |
if (node->start <= start && node->end >= end) |
| 232 |
{ |
| 233 |
return; |
| 234 |
} |
| 235 |
else |
| 236 |
{ |
| 237 |
node = node->next; |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
//check to see if range overlaps a range (or possibly 2) |
| 243 |
node = cliphead; |
| 244 |
while (node != NULL && node->start <= end) |
| 245 |
{ |
| 246 |
if (node->end >= start) |
| 247 |
{ |
| 248 |
// we found the first overlapping node |
| 249 |
if (node->start > start) |
| 250 |
{ |
| 251 |
// the new range overlaps with this node's start point |
| 252 |
node->start = start; |
| 253 |
} |
| 254 |
if (node->end < end) |
| 255 |
{ |
| 256 |
node->end = end; |
| 257 |
} |
| 258 |
|
| 259 |
node2 = node->next; |
| 260 |
while (node2 && node2->start <= node->end) |
| 261 |
{ |
| 262 |
if (node2->end > node->end) |
| 263 |
{ |
| 264 |
node->end = node2->end; |
| 265 |
} |
| 266 |
|
| 267 |
delnode = node2; |
| 268 |
node2 = node2->next; |
| 269 |
gld_clipper_RemoveRange(delnode); |
| 270 |
} |
| 271 |
return; |
| 272 |
} |
| 273 |
node = node->next; |
| 274 |
} |
| 275 |
|
| 276 |
//just add range |
| 277 |
node = cliphead; |
| 278 |
prevNode = NULL; |
| 279 |
temp = gld_clipnode_NewRange(start, end); |
| 280 |
while (node != NULL && node->start < end) |
| 281 |
{ |
| 282 |
prevNode = node; |
| 283 |
node = node->next; |
| 284 |
} |
| 285 |
temp->next = node; |
| 286 |
if (node == NULL) |
| 287 |
{ |
| 288 |
temp->prev = prevNode; |
| 289 |
if (prevNode) |
| 290 |
{ |
| 291 |
prevNode->next = temp; |
| 292 |
} |
| 293 |
if (!cliphead) |
| 294 |
{ |
| 295 |
cliphead = temp; |
| 296 |
} |
| 297 |
} |
| 298 |
else |
| 299 |
{ |
| 300 |
if (node == cliphead) |
| 301 |
{ |
| 302 |
cliphead->prev = temp; |
| 303 |
cliphead = temp; |
| 304 |
} |
| 305 |
else |
| 306 |
{ |
| 307 |
temp->prev = prevNode; |
| 308 |
prevNode->next = temp; |
| 309 |
node->prev = temp; |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
else |
| 314 |
{ |
| 315 |
temp = gld_clipnode_NewRange(start, end); |
| 316 |
cliphead = temp; |
| 317 |
return; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
static void gld_clipper_Clear(void) |
| 322 |
{ |
| 323 |
clipnode_t *node = cliphead; |
| 324 |
clipnode_t *temp; |
| 325 |
|
| 326 |
while (node != NULL) |
| 327 |
{ |
| 328 |
temp = node; |
| 329 |
node = node->next; |
| 330 |
gld_clipnode_Free(temp); |
| 331 |
} |
| 332 |
|
| 333 |
cliphead = NULL; |
| 334 |
} |
| 335 |
|
| 336 |
static angle_t gld_FrustumAngle(void) |
| 337 |
{ |
| 338 |
double fov; |
| 339 |
double floatangle; |
| 340 |
angle_t a1; |
| 341 |
|
| 342 |
float tilt = (float)fabs(((double)(int)viewpitch) / ANG1); |
| 343 |
if (tilt > 90.0f) |
| 344 |
{ |
| 345 |
tilt = 90.0f; |
| 346 |
} |
| 347 |
|
| 348 |
// If the pitch is larger than this you can look all around at a FOV of 90 |
| 349 |
if (D_abs((int) viewpitch) > 46 * ANG1) |
| 350 |
return 0xffffffff; |
| 351 |
|
| 352 |
// ok, this is a gross hack that barely works... |
| 353 |
// but at least it doesn't overestimate too much... |
| 354 |
fov = atan(1 / projMatrix[0]) * 360 / M_PI; |
| 355 |
floatangle = 2.0f + (45.0f + (tilt / 1.9f)) * fov / 90.0f; |
| 356 |
if (floatangle >= 180.0) |
| 357 |
return 0xffffffff; |
| 358 |
a1 = (angle_t)xs_CRoundToInt(ANG1 * floatangle); |
| 359 |
return a1; |
| 360 |
} |
| 361 |
|
| 362 |
// |
| 363 |
// gld_FrustumSetup |
| 364 |
// |
| 365 |
|
| 366 |
#define CALCMATRIX(a, b, c, d, e, f, g, h)\ |
| 367 |
(float)(modelMatrix[a] * projMatrix[b] + \ |
| 368 |
modelMatrix[c] * projMatrix[d] + \ |
| 369 |
modelMatrix[e] * projMatrix[f] + \ |
| 370 |
modelMatrix[g] * projMatrix[h]) |
| 371 |
|
| 372 |
#define NORMALIZE_PLANE(i)\ |
| 373 |
t = (float)sqrt(\ |
| 374 |
frustum[i][0] * frustum[i][0] + \ |
| 375 |
frustum[i][1] * frustum[i][1] + \ |
| 376 |
frustum[i][2] * frustum[i][2]); \ |
| 377 |
frustum[i][0] /= t; \ |
| 378 |
frustum[i][1] /= t; \ |
| 379 |
frustum[i][2] /= t; \ |
| 380 |
frustum[i][3] /= t |
| 381 |
|
| 382 |
void gld_FrustumSetup(void) |
| 383 |
{ |
| 384 |
float t; |
| 385 |
float clip[16]; |
| 386 |
angle_t a1 = gld_FrustumAngle(); |
| 387 |
|
| 388 |
gld_clipper_Clear(); |
| 389 |
gld_clipper_SafeAddClipRangeRealAngles(viewangle + a1, viewangle - a1); |
| 390 |
|
| 391 |
clip[0] = CALCMATRIX(0, 0, 1, 4, 2, 8, 3, 12); |
| 392 |
clip[1] = CALCMATRIX(0, 1, 1, 5, 2, 9, 3, 13); |
| 393 |
clip[2] = CALCMATRIX(0, 2, 1, 6, 2, 10, 3, 14); |
| 394 |
clip[3] = CALCMATRIX(0, 3, 1, 7, 2, 11, 3, 15); |
| 395 |
|
| 396 |
clip[4] = CALCMATRIX(4, 0, 5, 4, 6, 8, 7, 12); |
| 397 |
clip[5] = CALCMATRIX(4, 1, 5, 5, 6, 9, 7, 13); |
| 398 |
clip[6] = CALCMATRIX(4, 2, 5, 6, 6, 10, 7, 14); |
| 399 |
clip[7] = CALCMATRIX(4, 3, 5, 7, 6, 11, 7, 15); |
| 400 |
|
| 401 |
clip[8] = CALCMATRIX(8, 0, 9, 4, 10, 8, 11, 12); |
| 402 |
clip[9] = CALCMATRIX(8, 1, 9, 5, 10, 9, 11, 13); |
| 403 |
clip[10] = CALCMATRIX(8, 2, 9, 6, 10, 10, 11, 14); |
| 404 |
clip[11] = CALCMATRIX(8, 3, 9, 7, 10, 11, 11, 15); |
| 405 |
|
| 406 |
clip[12] = CALCMATRIX(12, 0, 13, 4, 14, 8, 15, 12); |
| 407 |
clip[13] = CALCMATRIX(12, 1, 13, 5, 14, 9, 15, 13); |
| 408 |
clip[14] = CALCMATRIX(12, 2, 13, 6, 14, 10, 15, 14); |
| 409 |
clip[15] = CALCMATRIX(12, 3, 13, 7, 14, 11, 15, 15); |
| 410 |
|
| 411 |
// Right plane |
| 412 |
frustum[0][0] = clip[ 3] - clip[ 0]; |
| 413 |
frustum[0][1] = clip[ 7] - clip[ 4]; |
| 414 |
frustum[0][2] = clip[11] - clip[ 8]; |
| 415 |
frustum[0][3] = clip[15] - clip[12]; |
| 416 |
NORMALIZE_PLANE(0); |
| 417 |
|
| 418 |
// Left plane |
| 419 |
frustum[1][0] = clip[ 3] + clip[ 0]; |
| 420 |
frustum[1][1] = clip[ 7] + clip[ 4]; |
| 421 |
frustum[1][2] = clip[11] + clip[ 8]; |
| 422 |
frustum[1][3] = clip[15] + clip[12]; |
| 423 |
NORMALIZE_PLANE(1); |
| 424 |
|
| 425 |
// Bottom plane |
| 426 |
frustum[2][0] = clip[ 3] + clip[ 1]; |
| 427 |
frustum[2][1] = clip[ 7] + clip[ 5]; |
| 428 |
frustum[2][2] = clip[11] + clip[ 9]; |
| 429 |
frustum[2][3] = clip[15] + clip[13]; |
| 430 |
NORMALIZE_PLANE(2); |
| 431 |
|
| 432 |
// Top plane |
| 433 |
frustum[3][0] = clip[ 3] - clip[ 1]; |
| 434 |
frustum[3][1] = clip[ 7] - clip[ 5]; |
| 435 |
frustum[3][2] = clip[11] - clip[ 9]; |
| 436 |
frustum[3][3] = clip[15] - clip[13]; |
| 437 |
NORMALIZE_PLANE(3); |
| 438 |
|
| 439 |
// Far plane |
| 440 |
frustum[4][0] = clip[ 3] - clip[ 2]; |
| 441 |
frustum[4][1] = clip[ 7] - clip[ 6]; |
| 442 |
frustum[4][2] = clip[11] - clip[10]; |
| 443 |
frustum[4][3] = clip[15] - clip[14]; |
| 444 |
NORMALIZE_PLANE(4); |
| 445 |
|
| 446 |
// Near plane |
| 447 |
frustum[5][0] = clip[ 3] + clip[ 2]; |
| 448 |
frustum[5][1] = clip[ 7] + clip[ 6]; |
| 449 |
frustum[5][2] = clip[11] + clip[10]; |
| 450 |
frustum[5][3] = clip[15] + clip[14]; |
| 451 |
NORMALIZE_PLANE(5); |
| 452 |
} |
| 453 |
|
| 454 |
dboolean gld_SphereInFrustum(float x, float y, float z, float radius) |
| 455 |
{ |
| 456 |
int p; |
| 457 |
|
| 458 |
for (p = 0; p < 4; p++) |
| 459 |
{ |
| 460 |
if (frustum[p][0] * x + |
| 461 |
frustum[p][1] * y + |
| 462 |
frustum[p][2] * z + |
| 463 |
frustum[p][3] <= -radius) |
| 464 |
{ |
| 465 |
return false; |
| 466 |
} |
| 467 |
} |
| 468 |
return true; |
| 469 |
} |