| 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 | #include "stdafx.h" |
| 20 | #include "editor.h" |
| 21 | #include "LightingStatus.h" |
| 22 | #include "radiosity.h" |
| 23 | #include "pserror.h" |
| 24 | #include "findintersection.h" |
| 25 | #include "hemicube.h" |
| 26 | #include "descent.h" |
| 27 | #include "rad_cast.h" |
| 28 | #include "ddio.h" |
| 29 | #include "vecmat.h" |
| 30 | #include <stdlib.h> |
| 31 | #include "mem.h" |
| 32 | |
| 33 | // Some radiosity globals |
| 34 | int Shoot_method = SM_HEMICUBE; |
| 35 | int Hemicube_resolution = 1024; |
| 36 | |
| 37 | int Ignore_terrain = 0; |
| 38 | int Ignore_satellites = 0; |
| 39 | |
| 40 | float rad_TotalFlux = 0.0f; |
| 41 | float rad_Convergence = 1.0f; |
| 42 | |
| 43 | int rad_NumSurfaces; |
| 44 | int rad_NumElements; |
| 45 | |
| 46 | float *rad_FormFactors; |
| 47 | |
| 48 | int rad_StepCount = 0; |
| 49 | int rad_MaxStep = 1; |
| 50 | int rad_DoneCalculating = 0; |
| 51 | |
| 52 | float rad_TotalUnsent = 0.0f; |
| 53 | |
| 54 | rad_surface *rad_MaxSurface = NULL; |
| 55 | rad_surface *rad_Surfaces; |
| 56 | |
| 57 | int UseVolumeLights = 0; // User selectable to do volumelights |
| 58 | int Calculate_specular_lighting = 0; |
| 59 | |
| 60 | // Specular variable |
| 61 | float *Room_strongest_value[MAX_ROOMS][4]; |
| 62 | |
| 63 | // Tells radiosity renderer to do volume lighting |
| 64 | int Do_volume_lighting = 0; |
| 65 | |
| 66 | volume_element *Volume_elements[MAX_VOLUME_ELEMENTS]; |
| 67 | |
| 68 | // Shoot_from_patch tells us whether or not we're shooting from the center of |
| 69 | // a surface or if we must shoot from the center of each of its individual elements |
| 70 | // Shoot_from_patch=1 is much faster |
| 71 | int Shoot_from_patch = 1; |
| 72 | |
| 73 | int DoRadiosityRun(int method, rad_surface *light_surfaces, int count) { |
| 74 | float start_time; |
| 75 | |
| 76 | mprintf(0, "Calculating radiosity on %d faces.\n", count); |
| 77 | |
| 78 | rad_Surfaces = light_surfaces; |
| 79 | rad_NumSurfaces = count; |
| 80 | |
| 81 | Shoot_method = method; |
| 82 | |
| 83 | start_time = timer_GetTime(); |
| 84 | |
| 85 | InitRadiosityRun(); |
| 86 | |
| 87 | // Setup our window |
| 88 | CLightingStatus dlg; |
| 89 | |
| 90 | dlg.Create(IDD_LIGHTINGSTATUS); |
| 91 | |
| 92 | CalculateRadiosity(); |
| 93 | |
| 94 | dlg.DestroyWindow(); |
| 95 | CloseRadiosityRun(); |
| 96 | |
| 97 | // Print time taken |
| 98 | mprintf(0, "\nLighting took %.4f seconds.\n", timer_GetTime() - start_time); |
| 99 | |
| 100 | return 1; |
| 101 | } |
| 102 | |
| 103 | // Sets up our radiosity run |
| 104 | void InitRadiosityRun() { |
| 105 | rad_TotalFlux = 0.0f; |
| 106 | rad_StepCount = 0; |
| 107 | rad_DoneCalculating = 0; |
| 108 | |
| 109 | // Clear key buffer |
| 110 | // ddio_KeyFrame(); |
| 111 | ddio_KeyFlush(); |
| 112 | |
| 113 | CountElements(); |
| 114 | CalculateArea(); |
| 115 | InitExitance(); |
| 116 | |
| 117 | if (Shoot_method == SM_HEMICUBE) { |
| 118 | SetupFormFactors(); |
| 119 | InitHemicube(Hemicube_resolution); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // Initalizes memory for form factors |
| 124 | void SetupFormFactors() { |
| 125 | ASSERT(rad_NumElements > 0); |
| 126 | |
| 127 | rad_FormFactors = (float *)mem_malloc(rad_NumElements * sizeof(float)); |
| 128 | ASSERT(rad_FormFactors != NULL); |
| 129 | } |
| 130 | |
| 131 | void CalculateAreaForSurface(rad_surface *sp) { |
| 132 | int i; |
| 133 | |
| 134 | vector normal; |
| 135 | |
| 136 | vm_GetPerp(&normal, &sp->verts[0], &sp->verts[1], &sp->verts[2]); |
| 137 | sp->area = (vm_GetMagnitude(&normal) / 2); |
| 138 | |
| 139 | for (i = 2; i < sp->num_verts - 1; i++) { |
| 140 | vm_GetPerp(&normal, &sp->verts[0], &sp->verts[i], &sp->verts[i + 1]); |
| 141 | sp->area += (vm_GetMagnitude(&normal) / 2); |
| 142 | } |
| 143 | |
| 144 | sp->surface_area = sp->area; |
| 145 | sp->element_area = sp->area / (sp->xresolution * sp->yresolution); |
| 146 | } |
| 147 | |
| 148 | void CalculateAreaForElement(rad_element *ep) { |
| 149 | int i; |
| 150 | |
| 151 | vector normal; |
| 152 | |
| 153 | if (ep->flags & EF_IGNORE) { |
| 154 | ep->area = .0000001f; |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | vm_GetPerp(&normal, &ep->verts[0], &ep->verts[1], &ep->verts[2]); |
| 159 | ep->area = (vm_GetMagnitude(&normal) / 2); |
| 160 | |
| 161 | for (i = 2; i < ep->num_verts - 1; i++) { |
| 162 | vm_GetPerp(&normal, &ep->verts[0], &ep->verts[i], &ep->verts[i + 1]); |
| 163 | ep->area += (vm_GetMagnitude(&normal) / 2); |
| 164 | } |
| 165 | |
| 166 | if (ep->area < .05) |
| 167 | ep->flags |= EF_SMALL; |
| 168 | if (ep->area == 0) { |
| 169 | ep->flags |= EF_IGNORE; |
| 170 | ep->area = .00000001f; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // Calculates the area of the surfaces and elements in our environment |
| 175 | void CalculateArea() { |
| 176 | rad_surface *surf; |
| 177 | int i, t; |
| 178 | |
| 179 | for (i = 0; i < rad_NumSurfaces; i++) { |
| 180 | surf = &rad_Surfaces[i]; |
| 181 | |
| 182 | CalculateAreaForSurface(surf); |
| 183 | |
| 184 | for (t = 0; t < surf->xresolution * surf->yresolution; t++) { |
| 185 | rad_element *ep = &surf->elements[t]; |
| 186 | CalculateAreaForElement(ep); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // Counts the total number of elements we have to work with |
| 192 | void CountElements() { |
| 193 | rad_surface *surf; |
| 194 | int i; |
| 195 | |
| 196 | rad_NumElements = 0; |
| 197 | |
| 198 | for (i = 0; i < rad_NumSurfaces; i++) { |
| 199 | surf = &rad_Surfaces[i]; |
| 200 | |
| 201 | rad_NumElements += (surf->xresolution * surf->yresolution); |
| 202 | } |
| 203 | mprintf(0, "Number of elements=%d\n", rad_NumElements); |
| 204 | } |
| 205 | |
| 206 | // Initializes the exitances for all surfaces |
| 207 | void InitExitance() { |
| 208 | int i; |
| 209 | |
| 210 | for (i = 0; i < rad_NumSurfaces; i++) { |
| 211 | SetExitanceForSurface(&rad_Surfaces[i]); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // Gets the spectral emittance for a surface |
| 216 | void GetEmittance(rad_surface *surf, spectra *dest) { *dest = surf->emittance; } |
| 217 | |
| 218 | // Sets all the elements of a surface to their initial unshot exitance values |
| 219 | void SetExitanceForSurface(rad_surface *surf) { |
| 220 | int i; |
| 221 | |
| 222 | surf->exitance = surf->emittance; |
| 223 | |
| 224 | for (i = 0; i < surf->xresolution * surf->yresolution; i++) { |
| 225 | if (Shoot_from_patch) { |
| 226 | surf->elements[i].exitance.r = 0; |
| 227 | surf->elements[i].exitance.g = 0; |
| 228 | surf->elements[i].exitance.b = 0; |
| 229 | } else |
| 230 | surf->elements[i].exitance = surf->emittance; |
| 231 | } |
| 232 | |
| 233 | rad_TotalFlux += GetUnsentFlux(surf); |
| 234 | } |
| 235 | |
| 236 | // Find the surface we want to shoot from |
| 237 | void UpdateUnsentValues() { |
| 238 | float cur_unsent; |
| 239 | float max_unsent = 0.0f; |
| 240 | float sat_max_unsent = 0.0f; |
| 241 | int use_sat = 0; |
| 242 | int i; |
| 243 | rad_surface *sat_surface; |
| 244 | |
| 245 | static int last_report_time = -10; |
| 246 | |
| 247 | rad_TotalUnsent = 0.0f; |
| 248 | rad_MaxSurface = NULL; |
| 249 | |
| 250 | // Go through all the surfaces searching for the surface with the greatest |
| 251 | // exitance yet to be shot |
| 252 | |
| 253 | for (i = 0; i < rad_NumSurfaces; i++) { |
| 254 | rad_surface *surf = &rad_Surfaces[i]; |
| 255 | cur_unsent = GetUnsentFlux(surf); |
| 256 | rad_TotalUnsent += cur_unsent; |
| 257 | |
| 258 | if (cur_unsent > max_unsent) { |
| 259 | max_unsent = cur_unsent; |
| 260 | rad_MaxSurface = surf; |
| 261 | } |
| 262 | |
| 263 | // Always give satellites priority |
| 264 | if (surf->surface_type == ST_SATELLITE && cur_unsent > 0) { |
| 265 | if (cur_unsent > sat_max_unsent) { |
| 266 | use_sat = 1; |
| 267 | sat_max_unsent = cur_unsent; |
| 268 | sat_surface = surf; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | // Update convergence |
| 274 | if (rad_TotalFlux > .0001) |
| 275 | rad_Convergence = fabs(rad_TotalUnsent) / rad_TotalFlux; |
| 276 | else |
| 277 | rad_Convergence = 0.0; |
| 278 | |
| 279 | mprintf_at(2, 3, 0, "Left=%f ", rad_Convergence); |
| 280 | |
| 281 | if (timer_GetTime() - last_report_time > 10.0) { |
| 282 | mprintf(0, "Percentage left=%f\n", rad_Convergence); |
| 283 | last_report_time = timer_GetTime(); |
| 284 | } |
| 285 | |
| 286 | if (use_sat) |
| 287 | rad_MaxSurface = sat_surface; |
| 288 | |
| 289 | if (!use_sat && Shoot_method == SM_SWITCH_AFTER_SATELLITES) { |
| 290 | SetupFormFactors(); |
| 291 | InitHemicube(Hemicube_resolution); |
| 292 | Shoot_method = SM_HEMICUBE; |
| 293 | } |
| 294 | |
| 295 | // No energy left to shoot? |
| 296 | if (rad_MaxSurface == NULL || rad_TotalUnsent == 0) |
| 297 | rad_DoneCalculating = 1; |
| 298 | } |
| 299 | |
| 300 | // Finds the world coordinate center of a surface |
| 301 | void GetCenterOfSurface(rad_surface *sp, vector *dest) { vm_GetCentroid(dest, sp->verts, sp->num_verts); } |
| 302 | |
| 303 | // Finds the world coordinate center of a surface |
| 304 | void GetCenterOfElement(rad_element *ep, vector *dest) { vm_GetCentroid(dest, ep->verts, ep->num_verts); } |
| 305 | |
| 306 | void CalculateRadiosity() { |
| 307 | int key; |
| 308 | |
| 309 | while (!rad_DoneCalculating) { |
| 310 | if (rad_StepCount >= rad_MaxStep) { |
| 311 | rad_DoneCalculating = 1; |
| 312 | break; |
| 313 | } |
| 314 | mprintf_at(2, 2, 0, "Lightcount=%d ", rad_StepCount); |
| 315 | |
| 316 | DoRadiosityIteration(); |
| 317 | |
| 318 | rad_StepCount++; |
| 319 | |
| 320 | Descent->defer(); |
| 321 | // ddio_KeyFrame(); |
| 322 | while ((key = ddio_KeyInKey()) != 0) { |
| 323 | if (key == KEY_LAPOSTRO) { |
| 324 | rad_DoneCalculating = 1; |
| 325 | break; |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | // Clear key buffer |
| 331 | ddio_KeyFlush(); |
| 332 | } |
| 333 | |
| 334 | // returns the amount of unsent flux from a surface |
| 335 | float GetUnsentFlux(rad_surface *surface) { |
| 336 | float flux; |
| 337 | |
| 338 | flux = surface->exitance.r + surface->exitance.g + surface->exitance.b; |
| 339 | |
| 340 | if (surface->surface_type != ST_SATELLITE) |
| 341 | flux *= surface->area; |
| 342 | |
| 343 | return flux; |
| 344 | } |
| 345 | |
| 346 | float GetMaxColor(spectra *sp) { |
| 347 | float m; |
| 348 | |
| 349 | m = std::max<float>(sp->r, sp->g); |
| 350 | m = std::max<float>(sp->b, m); |
| 351 | |
| 352 | return m; |
| 353 | } |
| 354 | |
| 355 | int FixEdges = 0; |
| 356 | extern void AddSpectra(spectra *dest, spectra *a, spectra *b); |
| 357 | |
| 358 | void NormalizeExitance() { |
| 359 | int i, t; |
| 360 | float rmax = 0.0f; |
| 361 | |
| 362 | for (i = 0; i < rad_NumSurfaces; i++) { |
| 363 | rad_surface *surf = &rad_Surfaces[i]; |
| 364 | spectra *emit = &surf->emittance; |
| 365 | |
| 366 | for (t = 0; t < surf->xresolution * surf->yresolution; t++) { |
| 367 | rad_element *ep = &surf->elements[t]; |
| 368 | |
| 369 | if (ep->flags & EF_IGNORE) |
| 370 | continue; |
| 371 | |
| 372 | if (Shoot_from_patch) { |
| 373 | ep->exitance.r += emit->r; |
| 374 | ep->exitance.g += emit->g; |
| 375 | ep->exitance.b += emit->b; |
| 376 | } |
| 377 | |
| 378 | /* if (ep->exitance.r>1) |
| 379 | ep->exitance.r=1; |
| 380 | if (ep->exitance.g>1) |
| 381 | ep->exitance.g=1; |
| 382 | if (ep->exitance.b>1) |
| 383 | ep->exitance.b=1;*/ |
| 384 | |
| 385 | rmax = GetMaxColor(&ep->exitance); |
| 386 | |
| 387 | if (rmax > 1.0 && rmax > 0.0) { |
| 388 | ep->exitance.r /= rmax; |
| 389 | ep->exitance.g /= rmax; |
| 390 | ep->exitance.b /= rmax; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | // Shuts down the radiosity stuff, freeing memory, etc |
| 397 | void CloseRadiosityRun() { |
| 398 | |
| 399 | NormalizeExitance(); |
| 400 | if (Shoot_method == SM_HEMICUBE) { |
| 401 | mem_free(rad_FormFactors); |
| 402 | CloseHemicube(); |
| 403 | } |
| 404 | } |
| 405 | void Calculate() { |
| 406 | |
| 407 | if (Shoot_method == SM_HEMICUBE) |
| 408 | CalculateFormFactorsHemiCube(); |
| 409 | else |
| 410 | CalculateFormFactorsRaycast(); |
| 411 | |
| 412 | // Set unshot exitance for MaxSurface to zero |
| 413 | rad_MaxSurface->exitance.r = 0; |
| 414 | rad_MaxSurface->exitance.g = 0; |
| 415 | rad_MaxSurface->exitance.b = 0; |
| 416 | rad_MaxSurface->flags &= ~SF_LIGHTSOURCE; |
| 417 | } |
| 418 | |
| 419 | // Does one iteration of ray-casting radiosity |
| 420 | int DoRadiosityIteration() { |
| 421 | UpdateUnsentValues(); |
| 422 | |
| 423 | if (!rad_DoneCalculating) |
| 424 | Calculate(); |
| 425 | |
| 426 | return 1; |
| 427 | } |