| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/TerrainSearch.cpp $ |
| 21 | * $Revision: 56 $ |
| 22 | * $Date: 12/27/99 7:48a $ |
| 23 | * $Author: Gwar $ |
| 24 | * |
| 25 | * |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/TerrainSearch.cpp $ |
| 28 | * |
| 29 | * 56 12/27/99 7:48a Gwar |
| 30 | * added NEWEDITOR #ifdefs |
| 31 | * |
| 32 | * 55 10/12/99 10:19a Gwar |
| 33 | * adding terrain support to NEWEDITOR |
| 34 | * |
| 35 | * 54 5/13/99 3:42p Ardussi |
| 36 | * changes for compiling on the Mac |
| 37 | * |
| 38 | * 53 4/27/99 3:38p Jason |
| 39 | * fixed occlusion bug |
| 40 | * |
| 41 | * 52 4/16/99 12:37a Matt |
| 42 | * Made Windows & Linux versions use the same qsort calls. |
| 43 | * |
| 44 | * 51 4/15/99 1:43a Jeff |
| 45 | * changes for linux compile |
| 46 | * |
| 47 | * 50 4/14/99 3:57a Jeff |
| 48 | * fixed case mismatch in #includes |
| 49 | * |
| 50 | * 49 4/12/99 3:04p Jason |
| 51 | * changes for low memory |
| 52 | * |
| 53 | * 48 3/04/99 6:12p Jason |
| 54 | * fixed bug with terrain lod |
| 55 | * |
| 56 | * 47 3/04/99 4:53p Jason |
| 57 | * added LODOffs |
| 58 | * |
| 59 | * 46 2/06/99 10:00p Jason |
| 60 | * fixed bug with last rev |
| 61 | * |
| 62 | * 45 2/06/99 7:55p Jason |
| 63 | * Misc speedups |
| 64 | * |
| 65 | * 44 1/20/99 10:50a Jason |
| 66 | * added new terrain |
| 67 | * |
| 68 | * 43 9/30/98 8:05p Jason |
| 69 | * Don't do occlusion test if inside |
| 70 | * |
| 71 | * 42 9/29/98 10:49a Jason |
| 72 | * fixed invisible terrain problelm |
| 73 | * |
| 74 | * 41 9/25/98 9:25p Jason |
| 75 | * did some graphics optimizations |
| 76 | * |
| 77 | * 40 8/19/98 2:19p Jeff |
| 78 | * moved detail globals to a struct |
| 79 | * |
| 80 | * 39 6/15/98 12:10p Jason |
| 81 | * made SearchQuadTree intelligently bail if we run out of cell buffers |
| 82 | * |
| 83 | * 38 6/04/98 6:45p Jason |
| 84 | * made objects use the terrain occlusion system |
| 85 | * |
| 86 | * 37 6/04/98 6:17p Jason |
| 87 | * added terrain occlusion system |
| 88 | * |
| 89 | * 36 5/14/98 12:56p Jason |
| 90 | * changes to help lower memory usage |
| 91 | * |
| 92 | * 35 4/30/98 6:45p Jason |
| 93 | * more changes for weather |
| 94 | * |
| 95 | * 34 4/27/98 1:15p Jason |
| 96 | * made lod engine use current fov |
| 97 | * |
| 98 | * 33 4/15/98 8:39p Jason |
| 99 | * inlined some functions |
| 100 | * |
| 101 | * 32 3/17/98 5:02p Jason |
| 102 | * sped up rendering the terrain from the mine by a large amount |
| 103 | * |
| 104 | * 31 2/04/98 6:09p Matt |
| 105 | * Changed object room number to indicate a terrain cell via a flag. Got |
| 106 | * rid of the object flag which used to indicate terrain. |
| 107 | * |
| 108 | * 30 1/30/98 12:12p Jason |
| 109 | * fixed LOD bug. This gives us twice the framerate in hilly areas!!! |
| 110 | * |
| 111 | * 29 1/12/98 6:01p Jason |
| 112 | * fixed memory corruption bug |
| 113 | * |
| 114 | * 28 1/12/98 3:34p Jason |
| 115 | * sped up indoor rendering by clipping faces against portals |
| 116 | * |
| 117 | * 27 1/02/98 7:06p Jason |
| 118 | * fixed mprintf |
| 119 | * |
| 120 | * 26 10/10/97 6:31p Jason |
| 121 | * added extra level of detail level |
| 122 | * |
| 123 | * 25 10/03/97 1:17p Jason |
| 124 | * tweaked to get more speed |
| 125 | * |
| 126 | * 24 10/02/97 6:45p Jason |
| 127 | * optimized terrain rotation |
| 128 | * |
| 129 | * 23 9/19/97 1:02p Chris |
| 130 | * Better big object support |
| 131 | * |
| 132 | * 22 9/17/97 6:29p Jason |
| 133 | * took out qsorting |
| 134 | * |
| 135 | * 21 9/15/97 9:09p Chris |
| 136 | * Make invalid locations for GetColTerrainSegFromPos link to zero and not |
| 137 | * error with a -1 |
| 138 | * |
| 139 | * 20 9/15/97 5:35p Jason |
| 140 | * took out tracing drops |
| 141 | * |
| 142 | * 19 9/12/97 6:36p Chris |
| 143 | * Added collision terrain segment support |
| 144 | * Added some LARGE OBJECT collision support |
| 145 | * |
| 146 | * 18 9/02/97 10:47a Jason |
| 147 | * use separate variables for game and editor LOD engine status |
| 148 | * |
| 149 | * 17 8/21/97 5:15p Samir |
| 150 | * Put some ifdef EDITOR stuff around Drop_seg and Tracing_drop |
| 151 | * references. |
| 152 | * |
| 153 | * 16 8/20/97 6:18p Jason |
| 154 | * fixed memory corruption bug |
| 155 | * |
| 156 | * 15 8/07/97 3:19p Jason |
| 157 | * replaced old terrain tmap system with new one |
| 158 | * |
| 159 | * 14 8/06/97 11:43a Jason |
| 160 | * made lightmaps work correctly in the terrain |
| 161 | * |
| 162 | * 13 8/03/97 3:32p Jason |
| 163 | * sped up LodSortingFunction after VTune said this was a HotSpot |
| 164 | * |
| 165 | * 12 8/03/97 2:26p Jason |
| 166 | * tried to eke out a few more cycles per frame from the terrain engine |
| 167 | * |
| 168 | * 11 7/29/97 5:33p Jason |
| 169 | * cleaned up code |
| 170 | * |
| 171 | * 10 7/28/97 5:23p Jason |
| 172 | * added features to lod engine - namely dynamic texture generation |
| 173 | * |
| 174 | * 9 7/25/97 5:25p Jason |
| 175 | * fixed multiple bugs in terrain LOD engine |
| 176 | * |
| 177 | * 8 7/23/97 6:29p Jason |
| 178 | * took out annoying mprintf |
| 179 | * |
| 180 | * 7 7/23/97 6:27p Jason |
| 181 | * added code to support terrain simplification |
| 182 | * |
| 183 | * 6 7/18/97 7:58p Jason |
| 184 | * fixed small bug with dropped cells |
| 185 | * |
| 186 | * 5 7/16/97 4:07p Jason |
| 187 | * fixed all the remaining drop-out bugs |
| 188 | * |
| 189 | * 4 7/16/97 12:46p Jason |
| 190 | * fixed some bugs with terrain searching |
| 191 | * and freed up lots of memory |
| 192 | * |
| 193 | * 3 7/16/97 11:26a Samir |
| 194 | * |
| 195 | * 2 7/15/97 8:09p Jason |
| 196 | * made it faster! |
| 197 | * |
| 198 | * 23 6/25/97 1:55p Jason |
| 199 | * added Flat_terrain variaable for viewing terrain as a flat plane |
| 200 | * |
| 201 | * 22 6/24/97 4:47p Mark |
| 202 | * FROM JASON:Took out non-sorting |
| 203 | * |
| 204 | * 21 6/24/97 4:24p Jason |
| 205 | * changes for y only terrain position |
| 206 | * |
| 207 | * 20 6/24/97 12:49p Jason |
| 208 | * |
| 209 | * 19 5/21/97 12:29p Jason |
| 210 | * added the ability to draw terrain dots in mine view |
| 211 | * |
| 212 | * 18 5/11/97 1:31p Matt |
| 213 | * Added code to only render terrain that might be visible when terrain |
| 214 | * called from the mine renderer. |
| 215 | * |
| 216 | * $NoKeywords: $ |
| 217 | */ |
| 218 | |
| 219 | #include "terrain.h" |
| 220 | #include "3d.h" |
| 221 | #include "mono.h" |
| 222 | #include "vecmat.h" |
| 223 | #include "pserror.h" |
| 224 | #include "pstypes.h" |
| 225 | #include "descent.h" |
| 226 | #include "game.h" |
| 227 | #include "gameloop.h" |
| 228 | #include <memory.h> |
| 229 | #include <stdlib.h> |
| 230 | #include "config.h" |
| 231 | #include "dedicated_server.h" |
| 232 | |
| 233 | |
| 234 | static int EvaluateBlock(int x, int z, int lod); |
| 235 | |
| 236 | uint16_t TS_FrameCount = 0xFFFF; |
| 237 | |
| 238 | static int SearchQuadTree(int x1, int y1, int x2, int y2, int dir, int *ccount); |
| 239 | int GlobalTransCount = 0; |
| 240 | int TotalDepth; |
| 241 | |
| 242 | // LOD shutoff stuff |
| 243 | #define MAX_LODOFFS 100 |
| 244 | static lodoff LODOffs[MAX_LODOFFS]; |
| 245 | static int Num_lodoffs = 0; |
| 246 | |
| 247 | // Render the terrain as flat? |
| 248 | uint8_t Flat_terrain = 0; |
| 249 | |
| 250 | // Variables for LOD engine |
| 251 | static float ViewDistance1 = .68f, ViewDistance2 = 1.0f; |
| 252 | static float TS_Lambda, TS_SimplifyCondition; |
| 253 | |
| 254 | static matrix *TS_View_matrix; |
| 255 | static vector *TS_View_position; |
| 256 | |
| 257 | static vector TS_PreRows[TERRAIN_DEPTH]; |
| 258 | static vector TS_FVectorAdd, TS_RVectorAdd; |
| 259 | |
| 260 | static uint8_t Terrain_y_flags[256]; |
| 261 | static vector Terrain_y_cache[256]; |
| 262 | |
| 263 | static uint16_t LOD_sort_bucket[MAX_TERRAIN_LOD][MAX_CELLS_TO_RENDER]; |
| 264 | static uint16_t LOD_sort_num[MAX_TERRAIN_LOD]; |
| 265 | |
| 266 | // Variable to determine if we're in editor or game |
| 267 | extern function_mode View_mode; |
| 268 | |
| 269 | static vector TJoint_VectorAddZ; |
| 270 | static vector TJoint_VectorAddX; |
| 271 | |
| 272 | // Since our terrain points increment in finite steps we can rotate 2 points (x,z) |
| 273 | // just add them to a base point to get the final rotated point |
| 274 | void PreRotateTerrain() { |
| 275 | vector rvec = {1 * TERRAIN_SIZE, 0, 0}; |
| 276 | vector fvec = {0, 0, 1 * TERRAIN_SIZE}; |
| 277 | |
| 278 | vector tj_rvec = {4, 0, 0}; |
| 279 | vector tj_fvec = {0, 0, 4}; |
| 280 | |
| 281 | TS_FVectorAdd = fvec * *TS_View_matrix; |
| 282 | TS_RVectorAdd = rvec * *TS_View_matrix; |
| 283 | |
| 284 | vector start_vec = {0.0f, 0.0f, 0.0f}; |
| 285 | vector temp_vec = start_vec - *TS_View_position; |
| 286 | start_vec = temp_vec * *TS_View_matrix; |
| 287 | |
| 288 | TS_PreRows[0] = start_vec; |
| 289 | |
| 290 | int i; |
| 291 | for (i = 1; i < TERRAIN_DEPTH; i++) { |
| 292 | TS_PreRows[i] = TS_PreRows[i - 1] + TS_FVectorAdd; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | static inline vector *GetDYVector(int h) { |
| 297 | vector *dyp; |
| 298 | |
| 299 | dyp = &Terrain_y_cache[h]; |
| 300 | |
| 301 | if (!Terrain_y_flags[h]) { |
| 302 | vector up_vector = {0, h * TERRAIN_HEIGHT_INCREMENT, 0}; |
| 303 | |
| 304 | *dyp = up_vector * *TS_View_matrix; |
| 305 | |
| 306 | Terrain_y_flags[h] = 1; |
| 307 | } |
| 308 | |
| 309 | return dyp; |
| 310 | } |
| 311 | |
| 312 | static inline void GetPreRotatedPointFast(vector *dest, int x, int z, int yvalue) { |
| 313 | ASSERT(yvalue >= 0 && yvalue <= 255); |
| 314 | |
| 315 | // If the terrain is supposed to be flat, bash this to zero |
| 316 | #ifdef EDITOR |
| 317 | if (Flat_terrain) { |
| 318 | yvalue = 0; |
| 319 | } |
| 320 | #endif |
| 321 | |
| 322 | *dest = TS_PreRows[z]; |
| 323 | *dest += TS_RVectorAdd * x; |
| 324 | *dest += *GetDYVector(yvalue); |
| 325 | } |
| 326 | |
| 327 | void GetPreRotatedPoint(g3Point *dest, int x, int z, int yvalue) { |
| 328 | ASSERT(yvalue >= 0 && yvalue <= 255); |
| 329 | |
| 330 | // If the terrain is supposed to be flat, bash this to zero |
| 331 | #ifdef EDITOR |
| 332 | if (Flat_terrain) { |
| 333 | yvalue = 0; |
| 334 | } |
| 335 | #endif |
| 336 | |
| 337 | dest->p3_vec = TS_PreRows[z]; |
| 338 | dest->p3_vec += TS_RVectorAdd * x; |
| 339 | dest->p3_vec += *GetDYVector(yvalue); |
| 340 | |
| 341 | // also store the unrotated point |
| 342 | dest->p3_flags |= PF_ORIGPOINT; |
| 343 | dest->p3_vecPreRot.x = TERRAIN_SIZE * x; |
| 344 | dest->p3_vecPreRot.y = TERRAIN_HEIGHT_INCREMENT * yvalue; |
| 345 | dest->p3_vecPreRot.z = TERRAIN_SIZE * z; |
| 346 | } |
| 347 | |
| 348 | // Gets a pre-rotated point that does not fall exactly on one of our 255 height values |
| 349 | void GetSpecialRotatedPoint(g3Point *dest, int x, int z, float yvalue) { |
| 350 | if (yvalue < 0) |
| 351 | yvalue = 0; |
| 352 | if (yvalue > MAX_TERRAIN_HEIGHT) |
| 353 | yvalue = MAX_TERRAIN_HEIGHT; |
| 354 | |
| 355 | vector up_vector = {0, yvalue, 0}; |
| 356 | vector dyp = up_vector * *TS_View_matrix; |
| 357 | |
| 358 | dest->p3_vec = TS_PreRows[z]; |
| 359 | dest->p3_vec += TS_RVectorAdd * x; |
| 360 | dest->p3_vec += dyp; |
| 361 | |
| 362 | // also store the unrotated point |
| 363 | dest->p3_flags |= PF_ORIGPOINT; |
| 364 | dest->p3_vecPreRot.x = TERRAIN_SIZE * x; |
| 365 | dest->p3_vecPreRot.y = yvalue; |
| 366 | dest->p3_vecPreRot.z = TERRAIN_SIZE * z; |
| 367 | } |
| 368 | |
| 369 | void Terrain_start_frame(vector *eye, matrix *view_orient) { |
| 370 | TS_View_position = eye; |
| 371 | TS_View_matrix = view_orient; |
| 372 | |
| 373 | // Set up variables for LOD switching |
| 374 | int width, height; |
| 375 | rend_GetProjectionParameters(&width, &height); |
| 376 | TS_Lambda = (float)height / (2 * ViewDistance1 * Render_zoom); |
| 377 | |
| 378 | #ifndef NEWEDITOR |
| 379 | TS_SimplifyCondition = Detail_settings.Pixel_error / (TS_Lambda * ViewDistance2); |
| 380 | #else |
| 381 | TS_SimplifyCondition = 10.0f / (TS_Lambda * ViewDistance2); |
| 382 | #endif |
| 383 | TS_SimplifyCondition *= TS_SimplifyCondition; |
| 384 | |
| 385 | // Increment TS_framecount before searching... |
| 386 | TS_FrameCount++; |
| 387 | |
| 388 | if (TS_FrameCount == 0) { |
| 389 | TS_FrameCount = 1; |
| 390 | } |
| 391 | |
| 392 | memset(Terrain_y_flags, 0, 256); |
| 393 | memset(LOD_sort_num, 0, MAX_TERRAIN_LOD * sizeof(uint16_t)); |
| 394 | |
| 395 | PreRotateTerrain(); |
| 396 | } |
| 397 | |
| 398 | // Sorts our visible terrain blocks by lod level. The lower resolution blocks |
| 399 | // go first - this prevents cracking |
| 400 | |
| 401 | int LodSortingFunction(const terrain_render_info *a, terrain_render_info *b) { return a->lod - b->lod; } |
| 402 | |
| 403 | void SortLodLevels(int count) { |
| 404 | qsort(Terrain_list, count, sizeof(terrain_render_info), (int (*)(const void *, const void *))LodSortingFunction); |
| 405 | } |
| 406 | // returns number of cells in visible terrain |
| 407 | int GetVisibleTerrain(vector *eye, matrix *view_orient) { |
| 408 | int cellcount = 0; |
| 409 | int i, t, count; |
| 410 | |
| 411 | GlobalTransCount = 0; |
| 412 | TotalDepth = 0; |
| 413 | |
| 414 | Terrain_start_frame(eye, view_orient); |
| 415 | |
| 416 | SearchQuadTree(0, 0, TERRAIN_WIDTH, TERRAIN_DEPTH, 0, &cellcount); |
| 417 | |
| 418 | // Make sure lower levels of detail come first |
| 419 | for (count = 0, i = 0; i < MAX_TERRAIN_LOD; i++) { |
| 420 | for (t = 0; t < LOD_sort_num[i]; t++, count++) { |
| 421 | Terrain_list[count].lod = i; |
| 422 | Terrain_list[count].segment = LOD_sort_bucket[i][t]; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | // Increment TS_framecount after searching because we share Terrain_rotate_list |
| 427 | // with the rendering code |
| 428 | TS_FrameCount++; |
| 429 | |
| 430 | if (TS_FrameCount == 0) |
| 431 | TS_FrameCount = 1; |
| 432 | |
| 433 | return cellcount; |
| 434 | } |
| 435 | |
| 436 | __inline void CheckCellOccupancy(int x, int y, int *ccount, uint8_t lod) { |
| 437 | int n, simplemul, i; |
| 438 | |
| 439 | if (*ccount >= MAX_CELLS_TO_RENDER) { |
| 440 | mprintf(0, "Trying to render too many cells! Cell limit=%d\n", MAX_CELLS_TO_RENDER); |
| 441 | #ifndef NEWEDITOR |
| 442 | Detail_settings.Terrain_render_distance = 80.0 * TERRAIN_SIZE; |
| 443 | #endif |
| 444 | return; |
| 445 | } |
| 446 | |
| 447 | n = y * TERRAIN_WIDTH + x; |
| 448 | |
| 449 | if (Show_invisible_terrain == 0 && lod == MAX_TERRAIN_LOD - 1 && Terrain_seg[n].flags & TF_INVISIBLE) |
| 450 | return; |
| 451 | |
| 452 | (*ccount)++; |
| 453 | |
| 454 | // Fill in our presorter |
| 455 | LOD_sort_bucket[lod][LOD_sort_num[lod]] = n; |
| 456 | LOD_sort_num[lod]++; |
| 457 | |
| 458 | // Fill in the join map so we know what edges touch what edges |
| 459 | if (lod != MAX_TERRAIN_LOD - 1) { |
| 460 | simplemul = 1 << ((MAX_TERRAIN_LOD - 1) - lod); |
| 461 | |
| 462 | for (i = 0; i < simplemul; i++) { |
| 463 | // Fill bottom edge |
| 464 | TerrainJoinMap[y * TERRAIN_WIDTH + x + i] = lod; |
| 465 | |
| 466 | // Fill top edge |
| 467 | TerrainJoinMap[(y + simplemul - 1) * TERRAIN_WIDTH + x + i] = lod; |
| 468 | |
| 469 | // Fill left edge |
| 470 | TerrainJoinMap[(y + i) * TERRAIN_WIDTH + x] = lod; |
| 471 | |
| 472 | // Fill right edge |
| 473 | TerrainJoinMap[(y + i) * TERRAIN_WIDTH + x + (simplemul - 1)] = lod; |
| 474 | } |
| 475 | } else |
| 476 | TerrainJoinMap[y * TERRAIN_WIDTH + x] = lod; |
| 477 | } |
| 478 | |
| 479 | #ifdef EDITOR |
| 480 | extern int Tracing_drops, Drop_seg; |
| 481 | #endif |
| 482 | |
| 483 | #define PUSH_STACK_TREE(a1, b1, a2, b2, l) \ |
| 484 | { \ |
| 485 | stack_x1[si] = a1; \ |
| 486 | stack_y1[si] = b1; \ |
| 487 | stack_y2[si] = b2; \ |
| 488 | stack_x2[si] = a2; \ |
| 489 | stack_level[si] = l; \ |
| 490 | si++; \ |
| 491 | } |
| 492 | #define POP_STACK_TREE() \ |
| 493 | { \ |
| 494 | si--; \ |
| 495 | x1 = stack_x1[si]; \ |
| 496 | x2 = stack_x2[si]; \ |
| 497 | y1 = stack_y1[si]; \ |
| 498 | y2 = stack_y2[si]; \ |
| 499 | level = stack_level[si]; \ |
| 500 | } |
| 501 | |
| 502 | int SearchQuadTree(int x1, int y1, int x2, int y2, int dir, int *ccount) { |
| 503 | int n[4], x[4], z[4]; |
| 504 | int i, first; |
| 505 | int anded; |
| 506 | int answer; |
| 507 | int si = 0, level; |
| 508 | int stack_x1[200]; |
| 509 | int stack_y1[200]; |
| 510 | int stack_x2[200]; |
| 511 | int stack_y2[200]; |
| 512 | int stack_level[200]; |
| 513 | float testdist = VisibleTerrainZ; |
| 514 | uint8_t ymin_int[4], ymax_int[4]; |
| 515 | int close; |
| 516 | uint8_t same_side; |
| 517 | uint8_t check_portal = 0; |
| 518 | g3Point *pnt; |
| 519 | int use_occlusion = 0; |
| 520 | int src_occlusion_index; |
| 521 | |
| 522 | if ((Terrain_checksum + 1) == Terrain_occlusion_checksum && !Terrain_from_mine) { |
| 523 | use_occlusion = 1; |
| 524 | int oz = (Viewer_object->pos.z / TERRAIN_SIZE) / OCCLUSION_SIZE; |
| 525 | int ox = (Viewer_object->pos.x / TERRAIN_SIZE) / OCCLUSION_SIZE; |
| 526 | |
| 527 | if (oz < 0 || oz >= OCCLUSION_SIZE || ox < 0 || ox >= OCCLUSION_SIZE) |
| 528 | use_occlusion = 0; |
| 529 | |
| 530 | src_occlusion_index = oz * OCCLUSION_SIZE + ox; |
| 531 | } |
| 532 | |
| 533 | PUSH_STACK_TREE(x1, y1, x2, y2, 0); |
| 534 | |
| 535 | while (si > 0) { |
| 536 | TotalDepth++; |
| 537 | |
| 538 | POP_STACK_TREE(); |
| 539 | ASSERT((x2 - x1) == (y2 - y1)); |
| 540 | |
| 541 | if ((x2 - x1) == 16) { |
| 542 | if (use_occlusion) { |
| 543 | int dest_occlusion_index = ((y1 / OCCLUSION_SIZE) * OCCLUSION_SIZE); |
| 544 | dest_occlusion_index += x1 / OCCLUSION_SIZE; |
| 545 | |
| 546 | int occ_byte = dest_occlusion_index / 8; |
| 547 | int occ_bit = dest_occlusion_index % 8; |
| 548 | |
| 549 | if (!(Terrain_occlusion_map[src_occlusion_index][occ_byte] & (1 << occ_bit))) |
| 550 | continue; |
| 551 | } |
| 552 | check_portal = 1; |
| 553 | } |
| 554 | |
| 555 | else if ((x2 - x1) == 8) { |
| 556 | answer = EvaluateBlock(x1, y1, MAX_TERRAIN_LOD - 4); |
| 557 | if (answer == -1) |
| 558 | continue; |
| 559 | else if (answer == 1) { |
| 560 | CheckCellOccupancy(x1, y1, ccount, MAX_TERRAIN_LOD - 4); |
| 561 | continue; |
| 562 | } |
| 563 | check_portal = 1; |
| 564 | } |
| 565 | |
| 566 | else if ((x2 - x1) == 4) { |
| 567 | answer = EvaluateBlock(x1, y1, MAX_TERRAIN_LOD - 3); |
| 568 | if (answer == -1) |
| 569 | continue; |
| 570 | else if (answer == 1) { |
| 571 | CheckCellOccupancy(x1, y1, ccount, MAX_TERRAIN_LOD - 3); |
| 572 | continue; |
| 573 | } |
| 574 | check_portal = 0; |
| 575 | } |
| 576 | |
| 577 | else if ((x2 - x1) == 2) { |
| 578 | answer = EvaluateBlock(x1, y1, MAX_TERRAIN_LOD - 2); |
| 579 | if (answer == -1) |
| 580 | continue; |
| 581 | else if (answer == 1) { |
| 582 | CheckCellOccupancy(x1, y1, ccount, MAX_TERRAIN_LOD - 2); |
| 583 | continue; |
| 584 | } |
| 585 | check_portal = 0; |
| 586 | } |
| 587 | |
| 588 | if ((x2 - x1) <= 2) { |
| 589 | CheckCellOccupancy(x1, y1, ccount, MAX_TERRAIN_LOD - 1); |
| 590 | CheckCellOccupancy(x1, y1 + 1, ccount, MAX_TERRAIN_LOD - 1); |
| 591 | CheckCellOccupancy(x1 + 1, y1 + 1, ccount, MAX_TERRAIN_LOD - 1); |
| 592 | CheckCellOccupancy(x1 + 1, y1, ccount, MAX_TERRAIN_LOD - 1); |
| 593 | |
| 594 | #if (defined(EDITOR) || defined(NEWEDITOR)) |
| 595 | if (*ccount >= MAX_CELLS_TO_RENDER) |
| 596 | return 0; |
| 597 | #endif |
| 598 | |
| 599 | check_portal = 0; |
| 600 | |
| 601 | continue; |
| 602 | } |
| 603 | |
| 604 | // Starts at lower left, goes clockwise |
| 605 | n[0] = y1 * TERRAIN_WIDTH + x1; |
| 606 | n[1] = y2 * TERRAIN_WIDTH + x1; |
| 607 | n[2] = y2 * TERRAIN_WIDTH + x2; |
| 608 | n[3] = y1 * TERRAIN_WIDTH + x2; |
| 609 | |
| 610 | x[0] = x[1] = x1; |
| 611 | x[2] = x[3] = x2; |
| 612 | z[0] = z[3] = y1; |
| 613 | z[1] = z[2] = y2; |
| 614 | |
| 615 | if (x2 == TERRAIN_WIDTH) { |
| 616 | x[2]--; |
| 617 | x[3]--; |
| 618 | |
| 619 | n[2]--; |
| 620 | n[3]--; |
| 621 | } |
| 622 | |
| 623 | if (y2 == TERRAIN_DEPTH) { |
| 624 | z[1]--; |
| 625 | z[2]--; |
| 626 | |
| 627 | n[1] -= TERRAIN_WIDTH; |
| 628 | n[2] -= TERRAIN_WIDTH; |
| 629 | } |
| 630 | |
| 631 | close = 0; |
| 632 | first = 1; |
| 633 | |
| 634 | anded = 0xff; |
| 635 | same_side = 0xff; |
| 636 | uint8_t portal_and = 0xff; |
| 637 | |
| 638 | for (i = 0; i < 4; i++) { |
| 639 | if (first) { |
| 640 | int lx = x[i] >> (8 - level); |
| 641 | int lz = z[i] >> (8 - level); |
| 642 | |
| 643 | int cell = (lz << level) + lx; |
| 644 | ASSERT(level <= 6); |
| 645 | |
| 646 | ymin_int[i] = *(Terrain_min_height_int[level] + cell); |
| 647 | ymax_int[i] = *(Terrain_max_height_int[level] + cell); |
| 648 | first = 0; |
| 649 | } |
| 650 | |
| 651 | pnt = &World_point_buffer[n[i]]; |
| 652 | |
| 653 | // Do min height for region |
| 654 | GetPreRotatedPointFast(&pnt->p3_vec, x[i], z[i], ymin_int[0]); |
| 655 | |
| 656 | if (pnt->p3_vec.z >= 0) |
| 657 | same_side &= 1; |
| 658 | else |
| 659 | same_side &= 2; |
| 660 | |
| 661 | if ((fabs(pnt->p3_vec.z) <= testdist)) |
| 662 | close = 1; |
| 663 | |
| 664 | anded &= g3_CodePoint(pnt); |
| 665 | if (Check_terrain_portal && check_portal) { |
| 666 | pnt->p3_flags &= ~PF_PROJECTED; |
| 667 | // Automatically flag the ones behind us as visible |
| 668 | if (pnt->p3_codes & CC_BEHIND) |
| 669 | check_portal = 0; |
| 670 | else { |
| 671 | g3_ProjectPoint(pnt); |
| 672 | portal_and &= CodeTerrainPoint(pnt); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | // Do max height for region |
| 677 | GetPreRotatedPointFast(&pnt->p3_vec, x[i], z[i], ymax_int[0]); |
| 678 | |
| 679 | if (pnt->p3_vec.z >= 0) |
| 680 | same_side &= 1; |
| 681 | else |
| 682 | same_side &= 2; |
| 683 | |
| 684 | if ((fabs(pnt->p3_vec.z) <= testdist)) |
| 685 | close = 1; |
| 686 | |
| 687 | anded &= g3_CodePoint(pnt); |
| 688 | |
| 689 | if (Check_terrain_portal && check_portal) { |
| 690 | pnt->p3_flags &= ~PF_PROJECTED; |
| 691 | // Automatically flag the ones behind us as visible |
| 692 | if (pnt->p3_codes & CC_BEHIND) |
| 693 | check_portal = 0; |
| 694 | else { |
| 695 | g3_ProjectPoint(pnt); |
| 696 | portal_and &= CodeTerrainPoint(pnt); |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | if ((!anded && close) || (!anded && !close && !same_side)) { |
| 702 | // Recurse into this quadtree |
| 703 | |
| 704 | // Check to see if this is in our portal window |
| 705 | if (Check_terrain_portal && check_portal && portal_and) |
| 706 | continue; |
| 707 | |
| 708 | int midx = ((x2 - x1) / 2) + x1; |
| 709 | int midy = ((y2 - y1) / 2) + y1; |
| 710 | |
| 711 | PUSH_STACK_TREE(x1, midy, midx, y2, level + 1); |
| 712 | PUSH_STACK_TREE(midx, midy, x2, y2, level + 1); |
| 713 | PUSH_STACK_TREE(midx, y1, x2, midy, level + 1); |
| 714 | PUSH_STACK_TREE(x1, y1, midx, midy, level + 1); |
| 715 | } else { |
| 716 | /*#ifdef EDITOR |
| 717 | if (Tracing_drops) |
| 718 | { |
| 719 | int dx=Drop_seg%TERRAIN_WIDTH; |
| 720 | int dz=Drop_seg/TERRAIN_WIDTH; |
| 721 | |
| 722 | if (x1<=dx && dx<=x2 && y1<=dz && dz<=y2) |
| 723 | { |
| 724 | Tracing_drops=0; |
| 725 | Int3(); |
| 726 | } |
| 727 | |
| 728 | } |
| 729 | #endif*/ |
| 730 | } |
| 731 | } |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | // Given a position, returns the terrain segment that that position is in/over |
| 736 | // returns -1 if not over terrain |
| 737 | int GetTerrainCellFromPos(vector *pos) { |
| 738 | int x = pos->x / TERRAIN_SIZE; |
| 739 | int z = pos->z / TERRAIN_SIZE; |
| 740 | |
| 741 | if (x < 0 || x >= TERRAIN_WIDTH || z < 0 || z >= TERRAIN_DEPTH) |
| 742 | return -1; |
| 743 | |
| 744 | return (z * TERRAIN_WIDTH + x); |
| 745 | } |
| 746 | |
| 747 | int GetTerrainRoomFromPos(vector *pos) { return MAKE_ROOMNUM(GetTerrainCellFromPos(pos)); } |
| 748 | |
| 749 | // Computes the center of the segment in x,z and also sets y touching the ground |
| 750 | void ComputeTerrainSegmentCenter(vector *pos, int segnum) { |
| 751 | int segx = segnum % TERRAIN_WIDTH; |
| 752 | int segz = segnum / TERRAIN_WIDTH; |
| 753 | |
| 754 | pos->x = (segx * TERRAIN_SIZE) + (TERRAIN_SIZE / 2); |
| 755 | pos->z = (segz * TERRAIN_SIZE) + (TERRAIN_SIZE / 2); |
| 756 | |
| 757 | pos->y = GetTerrainGroundPoint(pos); |
| 758 | } |
| 759 | |
| 760 | // Given an position, returns the terrain Y coord at that location |
| 761 | float GetTerrainGroundPoint(vector *pos, vector *normal) { |
| 762 | float y; |
| 763 | vector pnt, norm; |
| 764 | int t; |
| 765 | int x, z; |
| 766 | |
| 767 | x = pos->x / TERRAIN_SIZE; |
| 768 | z = pos->z / TERRAIN_SIZE; |
| 769 | |
| 770 | if (x < 0 || x >= TERRAIN_WIDTH || z < 0 || z >= TERRAIN_DEPTH) |
| 771 | return (0); |
| 772 | |
| 773 | t = z * TERRAIN_WIDTH + x; |
| 774 | |
| 775 | pnt = *pos; |
| 776 | pnt.x -= (x * TERRAIN_SIZE); |
| 777 | pnt.z -= (z * TERRAIN_SIZE); |
| 778 | |
| 779 | if (pnt.x > pnt.z) |
| 780 | norm = TerrainNormals[MAX_TERRAIN_LOD - 1][t].normal2; |
| 781 | else |
| 782 | norm = TerrainNormals[MAX_TERRAIN_LOD - 1][t].normal1; |
| 783 | |
| 784 | // Do plane equation on x,z and then interpolate y |
| 785 | y = ((pnt.x * norm.x) + (pnt.z * norm.z)) / norm.y; |
| 786 | y = -y; |
| 787 | y += Terrain_seg[t].y; |
| 788 | |
| 789 | if (normal != NULL) |
| 790 | *normal = norm; |
| 791 | |
| 792 | return (y); |
| 793 | } |
| 794 | |
| 795 | int SimplifyVertexSlow(int x, int z, float delta) { |
| 796 | vector *eye = TS_View_position; |
| 797 | g3Point p1, p2; |
| 798 | |
| 799 | p1.p3_codes = 0; |
| 800 | GetPreRotatedPoint(&p1, x, z, Terrain_seg[z * TERRAIN_WIDTH + x].ypos); |
| 801 | |
| 802 | p2.p3_codes = 0; |
| 803 | GetSpecialRotatedPoint(&p2, x, z, Terrain_seg[z * TERRAIN_WIDTH + x].y + delta); |
| 804 | |
| 805 | g3_ProjectPoint(&p1); |
| 806 | g3_ProjectPoint(&p2); |
| 807 | |
| 808 | float len = ((p1.p3_sx - p2.p3_sx) * (p1.p3_sx - p2.p3_sx)) + ((p1.p3_sy - p2.p3_sy) * (p1.p3_sy - p2.p3_sy)); |
| 809 | |
| 810 | #ifndef NEWEDITOR |
| 811 | if (len < (Detail_settings.Pixel_error * Detail_settings.Pixel_error)) |
| 812 | #else |
| 813 | if (len < (10.0f * 10.0f)) |
| 814 | #endif |
| 815 | return 1; |
| 816 | |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | int SimplifyVertex(int x, int z, float delta) { |
| 821 | vector *eye = TS_View_position; |
| 822 | vector vec; |
| 823 | float inner; |
| 824 | |
| 825 | vec.x = x * TERRAIN_SIZE; |
| 826 | vec.z = z * TERRAIN_SIZE; |
| 827 | vec.y = Terrain_seg[z * TERRAIN_WIDTH + x].y; |
| 828 | |
| 829 | float delta_squared = delta * delta; |
| 830 | float first_part, second_part; |
| 831 | |
| 832 | float ex_minus_vx_squared = (eye->x - vec.x) * (eye->x - vec.x); |
| 833 | float ez_minus_vz_squared = (eye->z - vec.z) * (eye->z - vec.z); |
| 834 | float ey_minus_vy_squared = (eye->y - vec.y) * (eye->y - vec.y); |
| 835 | |
| 836 | first_part = delta_squared * (ex_minus_vx_squared + ez_minus_vz_squared); |
| 837 | |
| 838 | inner = ex_minus_vx_squared + ez_minus_vz_squared + ey_minus_vy_squared; |
| 839 | |
| 840 | second_part = (TS_SimplifyCondition) * (inner * inner); |
| 841 | |
| 842 | if (first_part <= second_part) |
| 843 | return 1; |
| 844 | |
| 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | // Returns 1 if the given block (specified by the upper left corder x,z) lod can |
| 849 | // be simplified |
| 850 | // Returns -1 if the block is invisible |
| 851 | // Returns 0 if not |
| 852 | int EvaluateBlock(int x, int z, int lod) { |
| 853 | float delta; |
| 854 | int simplemul = 1 << ((MAX_TERRAIN_LOD - 1) - lod); |
| 855 | |
| 856 | #if (defined(EDITOR) || defined(NEWEDITOR)) |
| 857 | if (View_mode == EDITOR_MODE && Editor_LOD_engine_off) |
| 858 | return 0; |
| 859 | #endif |
| 860 | |
| 861 | if (View_mode != EDITOR_MODE && Terrain_LOD_engine_off) |
| 862 | return 0; |
| 863 | |
| 864 | delta = TerrainDeltaBlocks[lod][((z / simplemul) * (TERRAIN_WIDTH / simplemul)) + (x / simplemul)]; |
| 865 | |
| 866 | if (delta == SHUTOFF_LOD_INVISIBLE) |
| 867 | return -1; // This block is completely invisible |
| 868 | |
| 869 | // if (SimplifyVertexSlow (x+(simplemul/2),z+(simplemul/2),delta)) |
| 870 | // return 1; |
| 871 | |
| 872 | if (SimplifyVertex(x + (simplemul / 2), z + (simplemul / 2), delta)) |
| 873 | return 1; |
| 874 | |
| 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | // Shuts off LOD for a given cell |
| 879 | void TurnOffLODForCell(int cellnum) { |
| 880 | if (Dedicated_server) |
| 881 | return; |
| 882 | |
| 883 | ASSERT(cellnum >= 0 && cellnum < (TERRAIN_WIDTH * TERRAIN_DEPTH)); |
| 884 | int x = cellnum % TERRAIN_WIDTH; |
| 885 | int z = cellnum / TERRAIN_WIDTH; |
| 886 | |
| 887 | ASSERT(Num_lodoffs < MAX_LODOFFS); |
| 888 | LODOffs[Num_lodoffs].cellnum = cellnum; |
| 889 | |
| 890 | for (int i = 0; i < MAX_TERRAIN_LOD - 1; i++) { |
| 891 | float delta; |
| 892 | int simplemul = 1 << ((MAX_TERRAIN_LOD - 1) - i); |
| 893 | |
| 894 | delta = TerrainDeltaBlocks[i][((z / simplemul) * (TERRAIN_WIDTH / simplemul)) + (x / simplemul)]; |
| 895 | LODOffs[Num_lodoffs].save_delta[i] = delta; |
| 896 | TerrainDeltaBlocks[i][((z / simplemul) * (TERRAIN_WIDTH / simplemul)) + (x / simplemul)] = SHUTOFF_LOD_DELTA; |
| 897 | } |
| 898 | |
| 899 | Num_lodoffs++; |
| 900 | } |
| 901 | |
| 902 | // Restores the terrain deltas to their original state |
| 903 | void ClearLODOffs() { |
| 904 | for (int t = Num_lodoffs - 1; t >= 0; t--) { |
| 905 | int cellnum = LODOffs[t].cellnum; |
| 906 | |
| 907 | int x = cellnum % TERRAIN_WIDTH; |
| 908 | int z = cellnum / TERRAIN_WIDTH; |
| 909 | |
| 910 | for (int i = 0; i < MAX_TERRAIN_LOD - 1; i++) { |
| 911 | int simplemul = 1 << ((MAX_TERRAIN_LOD - 1) - i); |
| 912 | TerrainDeltaBlocks[i][((z / simplemul) * (TERRAIN_WIDTH / simplemul)) + (x / simplemul)] = |
| 913 | LODOffs[t].save_delta[i]; |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | Num_lodoffs = 0; |
| 918 | } |