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