| 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 | |
| 19 | // Lightmap info |
| 20 | #include "pstypes.h" |
| 21 | |
| 22 | #include "lightmap_info.h" |
| 23 | #include "lightmap.h" |
| 24 | #include "mono.h" |
| 25 | #include "Ddgr.h" |
| 26 | #include "grdefs.h" |
| 27 | #include "renderer.h" |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include "mem.h" |
| 31 | #include "dedicated_server.h" |
| 32 | |
| 33 | int Num_of_lightmap_info = 0; |
| 34 | lightmap_info *LightmapInfo = NULL; |
| 35 | |
| 36 | static uint16_t *Free_lmi_list = NULL; |
| 37 | |
| 38 | void CloseLightmapInfos() { |
| 39 | bool final_lightmap = true; |
| 40 | |
| 41 | if (LightmapInfo) |
| 42 | mem_free(LightmapInfo); |
| 43 | |
| 44 | if (Free_lmi_list) |
| 45 | mem_free(Free_lmi_list); |
| 46 | |
| 47 | LightmapInfo = NULL; |
| 48 | Free_lmi_list = NULL; |
| 49 | } |
| 50 | |
| 51 | // Sets all the lightmaps to unused |
| 52 | void InitLightmapInfo(int nummaps) { |
| 53 | int i; |
| 54 | |
| 55 | if (Dedicated_server) |
| 56 | return; |
| 57 | |
| 58 | if (nummaps == 0) { |
| 59 | LightmapInfo = (lightmap_info *)mem_malloc(MAX_LIGHTMAP_INFOS * sizeof(lightmap_info)); |
| 60 | ASSERT(LightmapInfo); |
| 61 | Free_lmi_list = (uint16_t *)mem_malloc(MAX_LIGHTMAP_INFOS * sizeof(uint16_t)); |
| 62 | ASSERT(Free_lmi_list); |
| 63 | |
| 64 | for (i = 0; i < MAX_LIGHTMAP_INFOS; i++) { |
| 65 | LightmapInfo[i].used = 0; |
| 66 | Free_lmi_list[i] = i; |
| 67 | } |
| 68 | } |
| 69 | /* //DAJ this did not work! the lm_FreeLightmap() is refcount-- not a free() |
| 70 | else { |
| 71 | LightmapInfo=(lightmap_info *)mem_malloc (nummaps*sizeof(lightmap_info)); |
| 72 | ASSERT (LightmapInfo); |
| 73 | Free_lmi_list=(uint16_t *)mem_malloc (nummaps*sizeof(uint16_t)); |
| 74 | ASSERT (Free_lmi_list); |
| 75 | |
| 76 | |
| 77 | for (i=0;i<nummaps;i++) |
| 78 | { |
| 79 | LightmapInfo[i].used=0; |
| 80 | Free_lmi_list[i]=i; |
| 81 | } |
| 82 | } |
| 83 | */ |
| 84 | Num_of_lightmap_info = 0; |
| 85 | atexit(CloseLightmapInfos); |
| 86 | } |
| 87 | // Allocs a lightmap of w x h size |
| 88 | // Returns lightmap handle if successful, -1 if otherwise |
| 89 | |
| 90 | int AllocLightmapInfo(int w, int h, int type, bool alloc_lightmap) { |
| 91 | int n, i; |
| 92 | |
| 93 | if (Dedicated_server) |
| 94 | Int3(); // Shouldn't be here! |
| 95 | |
| 96 | if (Num_of_lightmap_info > MAX_LIGHTMAP_INFOS) |
| 97 | Int3(); // Get Jason, ran out of lightmaps! |
| 98 | |
| 99 | n = Free_lmi_list[Num_of_lightmap_info++]; |
| 100 | ASSERT(LightmapInfo[n].used == 0); |
| 101 | |
| 102 | ASSERT(n >= 0 && n < MAX_LIGHTMAP_INFOS); |
| 103 | |
| 104 | /*for (i=Highest_lmi_index;i<MAX_LIGHTMAP_INFOS;i++) |
| 105 | { |
| 106 | if (LightmapInfo[i].used==0) |
| 107 | { |
| 108 | n=i; |
| 109 | break; |
| 110 | } |
| 111 | }*/ |
| 112 | |
| 113 | // If we can't find a free slot in which to alloc, bail out |
| 114 | /*if (i==MAX_LIGHTMAP_INFOS) |
| 115 | { |
| 116 | Int3(); |
| 117 | mprintf(0,"ERROR! Couldn't find a free lightmap info to alloc!\n"); |
| 118 | return -1; |
| 119 | }*/ |
| 120 | |
| 121 | memset(&LightmapInfo[n], 0, sizeof(lightmap_info)); |
| 122 | |
| 123 | ASSERT(w >= 2 && h >= 2); |
| 124 | |
| 125 | if (alloc_lightmap) { |
| 126 | LightmapInfo[n].lm_handle = lm_AllocLightmap(w, h); |
| 127 | ASSERT(LightmapInfo[n].lm_handle != BAD_LM_INDEX); // Make sure we have a valid lightmap |
| 128 | |
| 129 | uint16_t *dest_data = lm_data(LightmapInfo[n].lm_handle); |
| 130 | |
| 131 | // Set the lightmap to be transparent |
| 132 | for (i = 0; i < w * h; i++) |
| 133 | dest_data[i] = NEW_TRANSPARENT_COLOR; |
| 134 | } |
| 135 | |
| 136 | LightmapInfo[n].used = 1; |
| 137 | LightmapInfo[n].type = type; |
| 138 | LightmapInfo[n].dynamic = BAD_LM_INDEX; |
| 139 | LightmapInfo[n].spec_map = -1; |
| 140 | LightmapInfo[n].width = w; |
| 141 | LightmapInfo[n].height = h; |
| 142 | LightmapInfo[n].x1 = 0; |
| 143 | LightmapInfo[n].y1 = 0; |
| 144 | |
| 145 | return n; |
| 146 | } |
| 147 | |
| 148 | // Given a handle, frees the lightmap memory and flags this lightmap as unused |
| 149 | void FreeLightmapInfo(int handle) { |
| 150 | ASSERT(handle >= 0 && handle <= MAX_LIGHTMAP_INFOS); |
| 151 | |
| 152 | if (Dedicated_server) |
| 153 | return; |
| 154 | |
| 155 | // when we free up our rooms, which frees up lightmaps, the lightmap array |
| 156 | // may already be freed |
| 157 | if (!LightmapInfo) |
| 158 | return; |
| 159 | |
| 160 | if (LightmapInfo[handle].used < 1) |
| 161 | return; |
| 162 | |
| 163 | LightmapInfo[handle].used--; |
| 164 | |
| 165 | if (LightmapInfo[handle].used == 0) { |
| 166 | if (!Dedicated_server) |
| 167 | rend_FreePreUploadedTexture(LightmapInfo[handle].lm_handle, MAP_TYPE_LIGHTMAP); |
| 168 | lm_FreeLightmap(LightmapInfo[handle].lm_handle); |
| 169 | |
| 170 | Free_lmi_list[--Num_of_lightmap_info] = handle; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // Gets the width of this lightmap_info handle |
| 175 | int lmi_w(int handle) { |
| 176 | if (!LightmapInfo[handle].used) { |
| 177 | Int3(); // Get Jason |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | return (LightmapInfo[handle].width); |
| 182 | } |
| 183 | |
| 184 | // Gets the height of this lightmap_info handle |
| 185 | int lmi_h(int handle) { |
| 186 | if (!LightmapInfo[handle].used) { |
| 187 | Int3(); // Get Jason |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | return (LightmapInfo[handle].height); |
| 192 | } |
| 193 | |
| 194 | // TODO: MTS: unused? |
| 195 | // Softens the edges of lightmaps so there are fewer artifaces |
| 196 | void ShadeLightmapInfoEdges(int type) { |
| 197 | int i; |
| 198 | |
| 199 | for (i = 0; i < MAX_LIGHTMAP_INFOS; i++) { |
| 200 | if (LightmapInfo[i].used && LightmapInfo[i].type == type) { |
| 201 | uint16_t *src_data; |
| 202 | uint16_t *dest_data = lm_data(LightmapInfo[i].lm_handle); |
| 203 | int w = lmi_w(i); |
| 204 | int h = lmi_h(i); |
| 205 | int x, y; |
| 206 | |
| 207 | src_data = (uint16_t *)mem_malloc(w * h * 2); |
| 208 | ASSERT(src_data); |
| 209 | |
| 210 | memcpy(src_data, dest_data, w * h * 2); |
| 211 | |
| 212 | for (y = 0; y < h; y++) { |
| 213 | for (x = 0; x < w; x++) { |
| 214 | if (!(src_data[y * w + x] & OPAQUE_FLAG)) { |
| 215 | int r = 0, g = 0, b = 0; |
| 216 | int num = 0; |
| 217 | ddgr_color color; |
| 218 | uint16_t color16; |
| 219 | |
| 220 | // Left edge |
| 221 | if (x != 0) { |
| 222 | |
| 223 | color16 = src_data[y * w + (x - 1)]; |
| 224 | if (color16 & OPAQUE_FLAG) { |
| 225 | color = GR_16_TO_COLOR(color16); |
| 226 | r += GR_COLOR_RED(color); |
| 227 | g += GR_COLOR_GREEN(color); |
| 228 | b += GR_COLOR_BLUE(color); |
| 229 | num++; |
| 230 | } |
| 231 | |
| 232 | if (y != 0) { |
| 233 | color16 = src_data[(y - 1) * w + (x - 1)]; |
| 234 | if (color16 & OPAQUE_FLAG) { |
| 235 | color = GR_16_TO_COLOR(color16); |
| 236 | r += GR_COLOR_RED(color); |
| 237 | g += GR_COLOR_GREEN(color); |
| 238 | b += GR_COLOR_BLUE(color); |
| 239 | num++; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if (y != h - 1) { |
| 244 | color16 = src_data[(y + 1) * w + (x - 1)]; |
| 245 | if (color16 & OPAQUE_FLAG) { |
| 246 | color = GR_16_TO_COLOR(color16); |
| 247 | r += GR_COLOR_RED(color); |
| 248 | g += GR_COLOR_GREEN(color); |
| 249 | b += GR_COLOR_BLUE(color); |
| 250 | num++; |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | // Right edge |
| 256 | if (x != (w - 1)) { |
| 257 | |
| 258 | color16 = src_data[y * w + (x + 1)]; |
| 259 | if (color16 & OPAQUE_FLAG) { |
| 260 | color = GR_16_TO_COLOR(color16); |
| 261 | r += GR_COLOR_RED(color); |
| 262 | g += GR_COLOR_GREEN(color); |
| 263 | b += GR_COLOR_BLUE(color); |
| 264 | num++; |
| 265 | } |
| 266 | |
| 267 | if (y != 0) { |
| 268 | color16 = src_data[(y - 1) * w + (x + 1)]; |
| 269 | if (color16 & OPAQUE_FLAG) { |
| 270 | color = GR_16_TO_COLOR(color16); |
| 271 | r += GR_COLOR_RED(color); |
| 272 | g += GR_COLOR_GREEN(color); |
| 273 | b += GR_COLOR_BLUE(color); |
| 274 | num++; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if (y != h - 1) { |
| 279 | color16 = src_data[(y + 1) * w + (x + 1)]; |
| 280 | if (color16 & OPAQUE_FLAG) { |
| 281 | color = GR_16_TO_COLOR(color16); |
| 282 | r += GR_COLOR_RED(color); |
| 283 | g += GR_COLOR_GREEN(color); |
| 284 | b += GR_COLOR_BLUE(color); |
| 285 | num++; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | if (y != 0) { |
| 291 | color16 = src_data[(y - 1) * w + (x)]; |
| 292 | if (color16 & OPAQUE_FLAG) { |
| 293 | color = GR_16_TO_COLOR(color16); |
| 294 | r += GR_COLOR_RED(color); |
| 295 | g += GR_COLOR_GREEN(color); |
| 296 | b += GR_COLOR_BLUE(color); |
| 297 | num++; |
| 298 | } |
| 299 | } |
| 300 | if (y != h - 1) { |
| 301 | color16 = src_data[(y + 1) * w + (x)]; |
| 302 | if (color16 & OPAQUE_FLAG) { |
| 303 | color = GR_16_TO_COLOR(color16); |
| 304 | r += GR_COLOR_RED(color); |
| 305 | g += GR_COLOR_GREEN(color); |
| 306 | b += GR_COLOR_BLUE(color); |
| 307 | num++; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | if (num > 0) { |
| 312 | r /= num; |
| 313 | g /= num; |
| 314 | b /= num; |
| 315 | color16 = GR_RGB16(r, g, b); |
| 316 | dest_data[y * w + x] = OPAQUE_FLAG | color16; |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | mem_free(src_data); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | // TODO: MTS: unused? |
| 327 | // Blurs the lightmaps so a dithering pattern is less noticeable |
| 328 | void BlurLightmapInfos(int type) { |
| 329 | int i; |
| 330 | |
| 331 | for (i = 0; i < MAX_LIGHTMAP_INFOS; i++) { |
| 332 | if (LightmapInfo[i].used && LightmapInfo[i].type == type) { |
| 333 | uint16_t *src_data; |
| 334 | uint16_t *dest_data = lm_data(LightmapInfo[i].lm_handle); |
| 335 | int w = lmi_w(i); |
| 336 | int h = lmi_h(i); |
| 337 | int x, y; |
| 338 | |
| 339 | src_data = (uint16_t *)mem_malloc(w * h * 2); |
| 340 | ASSERT(src_data); |
| 341 | |
| 342 | memcpy(src_data, dest_data, w * h * 2); |
| 343 | |
| 344 | for (y = 0; y < h; y++) { |
| 345 | for (x = 0; x < w; x++) { |
| 346 | if (src_data[y * w + x] & OPAQUE_FLAG) { |
| 347 | int r = 0, g = 0, b = 0; |
| 348 | int num = 0; |
| 349 | ddgr_color color; |
| 350 | uint16_t color16; |
| 351 | |
| 352 | color16 = src_data[y * w + (x)]; |
| 353 | color = GR_16_TO_COLOR(color16); |
| 354 | r += GR_COLOR_RED(color); |
| 355 | g += GR_COLOR_GREEN(color); |
| 356 | b += GR_COLOR_BLUE(color); |
| 357 | num++; |
| 358 | |
| 359 | // Left edge |
| 360 | if (x != 0) { |
| 361 | |
| 362 | color16 = src_data[y * w + (x - 1)]; |
| 363 | if (color16 & OPAQUE_FLAG) { |
| 364 | color = GR_16_TO_COLOR(color16); |
| 365 | r += GR_COLOR_RED(color); |
| 366 | g += GR_COLOR_GREEN(color); |
| 367 | b += GR_COLOR_BLUE(color); |
| 368 | num++; |
| 369 | } |
| 370 | |
| 371 | if (y != 0) { |
| 372 | color16 = src_data[(y - 1) * w + (x - 1)]; |
| 373 | if (color16 & OPAQUE_FLAG) { |
| 374 | color = GR_16_TO_COLOR(color16); |
| 375 | r += GR_COLOR_RED(color); |
| 376 | g += GR_COLOR_GREEN(color); |
| 377 | b += GR_COLOR_BLUE(color); |
| 378 | num++; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | if (y != h - 1) { |
| 383 | color16 = src_data[(y + 1) * w + (x - 1)]; |
| 384 | if (color16 & OPAQUE_FLAG) { |
| 385 | color = GR_16_TO_COLOR(color16); |
| 386 | r += GR_COLOR_RED(color); |
| 387 | g += GR_COLOR_GREEN(color); |
| 388 | b += GR_COLOR_BLUE(color); |
| 389 | num++; |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // Right edge |
| 395 | if (x != (w - 1)) { |
| 396 | |
| 397 | color16 = src_data[y * w + (x + 1)]; |
| 398 | if (color16 & OPAQUE_FLAG) { |
| 399 | color = GR_16_TO_COLOR(color16); |
| 400 | r += GR_COLOR_RED(color); |
| 401 | g += GR_COLOR_GREEN(color); |
| 402 | b += GR_COLOR_BLUE(color); |
| 403 | num++; |
| 404 | } |
| 405 | |
| 406 | if (y != 0) { |
| 407 | color16 = src_data[(y - 1) * w + (x + 1)]; |
| 408 | if (color16 & OPAQUE_FLAG) { |
| 409 | color = GR_16_TO_COLOR(color16); |
| 410 | r += GR_COLOR_RED(color); |
| 411 | g += GR_COLOR_GREEN(color); |
| 412 | b += GR_COLOR_BLUE(color); |
| 413 | num++; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | if (y != h - 1) { |
| 418 | color16 = src_data[(y + 1) * w + (x + 1)]; |
| 419 | if (color16 & OPAQUE_FLAG) { |
| 420 | color = GR_16_TO_COLOR(color16); |
| 421 | r += GR_COLOR_RED(color); |
| 422 | g += GR_COLOR_GREEN(color); |
| 423 | b += GR_COLOR_BLUE(color); |
| 424 | num++; |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | if (y != 0) { |
| 430 | color16 = src_data[(y - 1) * w + (x)]; |
| 431 | if (color16 & OPAQUE_FLAG) { |
| 432 | color = GR_16_TO_COLOR(color16); |
| 433 | r += GR_COLOR_RED(color); |
| 434 | g += GR_COLOR_GREEN(color); |
| 435 | b += GR_COLOR_BLUE(color); |
| 436 | num++; |
| 437 | } |
| 438 | } |
| 439 | if (y != h - 1) { |
| 440 | color16 = src_data[(y + 1) * w + (x)]; |
| 441 | if (color16 & OPAQUE_FLAG) { |
| 442 | color = GR_16_TO_COLOR(color16); |
| 443 | r += GR_COLOR_RED(color); |
| 444 | g += GR_COLOR_GREEN(color); |
| 445 | b += GR_COLOR_BLUE(color); |
| 446 | num++; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | if (num > 0) { |
| 451 | r /= num; |
| 452 | g /= num; |
| 453 | b /= num; |
| 454 | color16 = GR_RGB16(r, g, b); |
| 455 | dest_data[y * w + x] = OPAQUE_FLAG | color16; |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | mem_free(src_data); |
| 461 | } |
| 462 | } |
| 463 | } |