| 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 | // rad_scan |
| 20 | |
| 21 | #include "vecmat.h" |
| 22 | #include "3d.h" |
| 23 | #include "radiosity.h" |
| 24 | #include "hemicube.h" |
| 25 | #include "gr.h" |
| 26 | #include "d3edit.h" |
| 27 | #include "ddio.h" |
| 28 | #include "mem.h" |
| 29 | |
| 30 | #include <stdlib.h> |
| 31 | #include <algorithm> |
| 32 | |
| 33 | #define TOP_FACE 0 |
| 34 | #define LEFT_FACE 1 |
| 35 | #define RIGHT_FACE 2 |
| 36 | #define FRONT_FACE 3 |
| 37 | #define BACK_FACE 4 |
| 38 | |
| 39 | float Hemicube_view_zoom = 1.0; |
| 40 | |
| 41 | int rad_Drawing = 0; |
| 42 | |
| 43 | g3Point Element_points[100]; |
| 44 | rad_element *rad_MaxElement; |
| 45 | |
| 46 | rad_hemicube rad_Hemicube; |
| 47 | |
| 48 | extern void ShowRadView(); |
| 49 | int Show_rad_progress = 0; |
| 50 | int Cracks_this_frame, Cracks_this_side; |
| 51 | |
| 52 | float Highest_top_delta, Highest_side_delta; |
| 53 | |
| 54 | #define PI 3.141592654 |
| 55 | |
| 56 | // Calculates delta form factors |
| 57 | void CalculateDeltaFormFactors() { |
| 58 | int i, j; // Loop indices |
| 59 | float da; // Cell area |
| 60 | float dx, dy, dz; // Cell dimensions |
| 61 | float r, x, y, z; // Cell co-ordinates |
| 62 | float val; |
| 63 | |
| 64 | // Initialize cell dimensions and area |
| 65 | Highest_top_delta = -1.0f; |
| 66 | Highest_side_delta = -1.0f; |
| 67 | dx = dy = dz = 2.0 / (float)rad_Hemicube.ff_res; |
| 68 | da = 4.0 / ((float)rad_Hemicube.ff_res * (float)rad_Hemicube.ff_res); |
| 69 | |
| 70 | // Calculate top face delta form factors |
| 71 | x = dx / 2.0; |
| 72 | for (i = 0; i < rad_Hemicube.grid_dim; i++) { |
| 73 | y = dy / 2.0; |
| 74 | for (j = 0; j < rad_Hemicube.grid_dim; j++) { |
| 75 | r = x * x + y * y + 1.0; |
| 76 | val = (float)(da / (PI * r * r)); |
| 77 | rad_Hemicube.top_array[j * rad_Hemicube.grid_dim + i] = val; |
| 78 | |
| 79 | if (val > Highest_top_delta) |
| 80 | Highest_top_delta = val; |
| 81 | |
| 82 | y += dy; |
| 83 | } |
| 84 | x += dx; |
| 85 | } |
| 86 | |
| 87 | // Calculate side face delta form factors |
| 88 | x = dx / 2.0; |
| 89 | for (i = 0; i < rad_Hemicube.grid_dim; i++) { |
| 90 | z = dz / 2.0; |
| 91 | for (j = 0; j < rad_Hemicube.grid_dim; j++) { |
| 92 | r = x * x + z * z + 1.0; |
| 93 | val = (float)(z * da / (PI * r * r)); |
| 94 | rad_Hemicube.side_array[j * rad_Hemicube.grid_dim + i] = val; |
| 95 | |
| 96 | if (val > Highest_side_delta) |
| 97 | Highest_side_delta = val; |
| 98 | |
| 99 | z += dz; |
| 100 | } |
| 101 | x += dx; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Calculates the form factors for a hemicube |
| 106 | void CalculateFormFactorsHemiCube() { |
| 107 | g3Point *pointlist[100]; |
| 108 | int i, t, k, j, en_limit, en; |
| 109 | int ff_index = 0; |
| 110 | int self; |
| 111 | rad_element *dest_element; |
| 112 | |
| 113 | for (i = 0; i < rad_NumElements; i++) |
| 114 | rad_FormFactors[i] = 0.0f; |
| 115 | |
| 116 | // Shoot from patch (faster) or element? |
| 117 | if (Shoot_from_patch) |
| 118 | en_limit = 1; |
| 119 | else |
| 120 | en_limit = rad_MaxSurface->xresolution * rad_MaxSurface->yresolution; |
| 121 | for (en = 0; en < en_limit; en++) { |
| 122 | if (Shoot_from_patch) { |
| 123 | SetSurfaceView(rad_MaxSurface); |
| 124 | } else { |
| 125 | rad_MaxElement = &rad_MaxSurface->elements[en]; |
| 126 | if (rad_MaxElement->flags & EF_IGNORE) |
| 127 | continue; |
| 128 | |
| 129 | SetElementView(rad_MaxElement); |
| 130 | } |
| 131 | |
| 132 | Cracks_this_frame = 0; |
| 133 | |
| 134 | for (i = 0; i < 5; i++) { |
| 135 | ClearHemicubeGrid(); |
| 136 | UpdateView(i); |
| 137 | StartHemicubeDrawing(); |
| 138 | |
| 139 | ff_index = 0; |
| 140 | |
| 141 | for (t = 0; t < rad_NumSurfaces; t++) { |
| 142 | int ignore = 0; |
| 143 | rad_surface *surf = &rad_Surfaces[t]; |
| 144 | |
| 145 | if (surf == rad_MaxSurface) |
| 146 | ignore = 1; |
| 147 | if (surf->surface_type == ST_PORTAL) |
| 148 | ignore = 1; |
| 149 | |
| 150 | for (j = 0; j < surf->xresolution * surf->yresolution; j++, ff_index++) { |
| 151 | if (ignore) |
| 152 | continue; |
| 153 | |
| 154 | rad_element *ep = &surf->elements[j]; |
| 155 | |
| 156 | if (ep->flags & EF_IGNORE) |
| 157 | continue; |
| 158 | |
| 159 | for (k = 0; k < ep->num_verts; k++) { |
| 160 | vector vec = ep->verts[k]; |
| 161 | g3_RotatePoint(&Element_points[k], &vec); |
| 162 | Element_points[k].p3_flags = 0; |
| 163 | } |
| 164 | |
| 165 | if (g3_CheckNormalFacing(&ep->verts[0], &surf->normal)) { |
| 166 | for (k = 0; k < ep->num_verts; k++) { |
| 167 | g3Point *p = &Element_points[k]; |
| 168 | pointlist[k] = p; |
| 169 | } |
| 170 | |
| 171 | DrawRadiosityPoly(ep->num_verts, pointlist, ff_index); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | SumDeltas(rad_FormFactors, i); |
| 177 | EndHemicubeDrawing(i); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // Now extract the results |
| 182 | for (ff_index = 0, i = 0; i < rad_NumSurfaces; i++) { |
| 183 | rad_surface *surf = &rad_Surfaces[i]; |
| 184 | |
| 185 | // Check for self surface |
| 186 | if (rad_MaxSurface == surf) |
| 187 | self = 1; |
| 188 | else |
| 189 | self = 0; |
| 190 | |
| 191 | for (t = 0; t < surf->xresolution * surf->yresolution; t++, ff_index++) { |
| 192 | if (self) |
| 193 | continue; |
| 194 | |
| 195 | dest_element = &surf->elements[t]; |
| 196 | |
| 197 | if (rad_FormFactors[ff_index] > 0.0) { |
| 198 | spectra delta; |
| 199 | float reflect_factor = surf->reflectivity; |
| 200 | |
| 201 | // Compute reciprocal form factor |
| 202 | float rff; |
| 203 | |
| 204 | if (rad_MaxSurface->surface_type == ST_SATELLITE) |
| 205 | rff = std::min(rad_FormFactors[ff_index], 1.0f); |
| 206 | else |
| 207 | rff = (float)std::min(rad_FormFactors[ff_index] * rad_MaxSurface->area / dest_element->area, 1.0f); |
| 208 | |
| 209 | // Get shooting patch unsent exitance |
| 210 | spectra shoot = rad_MaxSurface->exitance; |
| 211 | |
| 212 | // Calculate delta exitance |
| 213 | delta.r = shoot.r * reflect_factor * rff; |
| 214 | delta.g = shoot.g * reflect_factor * rff; |
| 215 | delta.b = shoot.b * reflect_factor * rff; |
| 216 | |
| 217 | // Update element exitance |
| 218 | dest_element->exitance.r += delta.r; |
| 219 | dest_element->exitance.g += delta.g; |
| 220 | dest_element->exitance.b += delta.b; |
| 221 | |
| 222 | // Update patch unsent exitance |
| 223 | |
| 224 | surf->exitance.r += ((dest_element->area / surf->area) * delta.r); |
| 225 | surf->exitance.g += ((dest_element->area / surf->area) * delta.g); |
| 226 | surf->exitance.b += ((dest_element->area / surf->area) * delta.b); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | void InitHemicube(int resolution) { |
| 233 | // Make sure resolution is even |
| 234 | ASSERT(resolution % 2 == 0); |
| 235 | |
| 236 | rad_Drawing = 1; |
| 237 | |
| 238 | rad_Hemicube.ff_res = resolution; |
| 239 | rad_Hemicube.grid_dim = resolution / 2; |
| 240 | |
| 241 | rad_Hemicube.id_grid = (int *)mem_malloc(rad_Hemicube.ff_res * rad_Hemicube.ff_res * sizeof(int)); |
| 242 | ASSERT(rad_Hemicube.id_grid != NULL); |
| 243 | rad_Hemicube.depth_grid = (float *)mem_malloc(rad_Hemicube.ff_res * rad_Hemicube.ff_res * sizeof(float)); |
| 244 | ASSERT(rad_Hemicube.depth_grid != NULL); |
| 245 | |
| 246 | rad_Hemicube.top_array = (float *)mem_malloc(rad_Hemicube.grid_dim * rad_Hemicube.grid_dim * sizeof(float)); |
| 247 | ASSERT(rad_Hemicube.top_array != NULL); |
| 248 | rad_Hemicube.side_array = (float *)mem_malloc(rad_Hemicube.grid_dim * rad_Hemicube.grid_dim * sizeof(float)); |
| 249 | ASSERT(rad_Hemicube.side_array != NULL); |
| 250 | |
| 251 | CalculateDeltaFormFactors(); |
| 252 | |
| 253 | // Get a surface to draw to |
| 254 | rad_Hemicube.drawing_surface.create(rad_Hemicube.ff_res, rad_Hemicube.ff_res, BPP_16); |
| 255 | rad_Hemicube.vport = new grViewport(&rad_Hemicube.drawing_surface); |
| 256 | } |
| 257 | |
| 258 | void CloseHemicube() { |
| 259 | delete rad_Hemicube.vport; |
| 260 | rad_Hemicube.drawing_surface.free(); |
| 261 | |
| 262 | mem_free(rad_Hemicube.depth_grid); |
| 263 | mem_free(rad_Hemicube.id_grid); |
| 264 | mem_free(rad_Hemicube.side_array); |
| 265 | mem_free(rad_Hemicube.top_array); |
| 266 | |
| 267 | rad_Drawing = 0; |
| 268 | } |
| 269 | |
| 270 | void ClearHemicubeGrid() { |
| 271 | int i, t; |
| 272 | |
| 273 | for (i = 0; i < rad_Hemicube.ff_res; i++) { |
| 274 | for (t = 0; t < rad_Hemicube.ff_res; t++) { |
| 275 | rad_Hemicube.depth_grid[i * rad_Hemicube.ff_res + t] = 999999.0f; |
| 276 | rad_Hemicube.id_grid[i * rad_Hemicube.ff_res + t] = -1; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | void SetElementView(rad_element *ep) { |
| 282 | vector rv; // Random vector |
| 283 | vector u, v, n; |
| 284 | |
| 285 | // Select random vector for hemicube orientation |
| 286 | rv.x = ((rand() / RAND_MAX) * 2.0 - 1.0); |
| 287 | rv.y = ((rand() / RAND_MAX) * 2.0 - 1.0); |
| 288 | rv.z = ((rand() / RAND_MAX) * 2.0 - 1.0); |
| 289 | |
| 290 | n = rad_MaxSurface->normal; // Get patch normal |
| 291 | |
| 292 | do // Get valid u-axis vector |
| 293 | { |
| 294 | vm_CrossProduct(&u, &n, &rv); |
| 295 | } while (vm_GetMagnitude(&u) < .0001); |
| 296 | |
| 297 | vm_NormalizeVector(&u); |
| 298 | vm_CrossProduct(&v, &u, &n); // Determine v-axis |
| 299 | |
| 300 | rad_Hemicube.head_matrix.rvec = u; |
| 301 | rad_Hemicube.head_matrix.uvec = v; |
| 302 | rad_Hemicube.head_matrix.fvec = n; |
| 303 | |
| 304 | vm_VectorToMatrix(&rad_Hemicube.head_matrix, &n, NULL, NULL); |
| 305 | |
| 306 | rad_Hemicube.shooting_element = ep; |
| 307 | } |
| 308 | |
| 309 | void SetSurfaceView(rad_surface *surf) { |
| 310 | vector rv; // Random vector |
| 311 | vector u, v, n; |
| 312 | |
| 313 | // Select random vector for hemicube orientation |
| 314 | rv.x = ((rand() / RAND_MAX) * 2.0 - 1.0); |
| 315 | rv.y = ((rand() / RAND_MAX) * 2.0 - 1.0); |
| 316 | rv.z = ((rand() / RAND_MAX) * 2.0 - 1.0); |
| 317 | |
| 318 | n = rad_MaxSurface->normal; // Get patch normal |
| 319 | |
| 320 | do // Get valid u-axis vector |
| 321 | { |
| 322 | vm_CrossProduct(&u, &n, &rv); |
| 323 | } while (vm_GetMagnitude(&u) < .0001); |
| 324 | |
| 325 | vm_NormalizeVector(&u); |
| 326 | vm_CrossProduct(&v, &u, &n); // Determine v-axis |
| 327 | |
| 328 | rad_Hemicube.head_matrix.rvec = u; |
| 329 | rad_Hemicube.head_matrix.uvec = v; |
| 330 | rad_Hemicube.head_matrix.fvec = n; |
| 331 | |
| 332 | vm_VectorToMatrix(&rad_Hemicube.head_matrix, &n, NULL, NULL); |
| 333 | |
| 334 | rad_Hemicube.shooting_surface = surf; |
| 335 | } |
| 336 | |
| 337 | // Build transformation matrix for our hemicube |
| 338 | void BuildTransform(vector *nu, vector *nv, vector *nn) { |
| 339 | matrix *vm = &rad_Hemicube.view_matrix; // view matrix |
| 340 | |
| 341 | if (Shoot_from_patch) |
| 342 | GetCenterOfSurface(rad_Hemicube.shooting_surface, &rad_Hemicube.view_position); |
| 343 | else |
| 344 | GetCenterOfElement(rad_Hemicube.shooting_element, &rad_Hemicube.view_position); |
| 345 | |
| 346 | rad_Hemicube.view_position += (rad_MaxSurface->normal / 16.0); |
| 347 | |
| 348 | vm->fvec = *nn; |
| 349 | vm->uvec = *nv; |
| 350 | vm->rvec = *nu; |
| 351 | } |
| 352 | |
| 353 | void StartHemicubeDrawing() { |
| 354 | StartEditorFrame(rad_Hemicube.vport, &rad_Hemicube.view_position, &rad_Hemicube.view_matrix, Hemicube_view_zoom); |
| 355 | } |
| 356 | |
| 357 | int surface_colors[9000]; |
| 358 | void EndHemicubeDrawing(int face) { |
| 359 | static int first = 1; |
| 360 | |
| 361 | EndEditorFrame(); |
| 362 | |
| 363 | mprintf_at(2, 4, 0, "CTF=%d ", Cracks_this_frame); |
| 364 | mprintf_at(2, 5, 0, "CTS=%d ", Cracks_this_side); |
| 365 | |
| 366 | if (Show_rad_progress) { |
| 367 | int i, t; |
| 368 | int key; |
| 369 | |
| 370 | if (first) { |
| 371 | for (i = 0; i < 9000; i++) { |
| 372 | int r = (rand() % 127) + 128; |
| 373 | int g = (rand() % 127) + 128; |
| 374 | int b = (rand() % 127) + 128; |
| 375 | |
| 376 | surface_colors[i] = GR_RGB(r, g, b); |
| 377 | } |
| 378 | |
| 379 | first = 0; |
| 380 | } |
| 381 | |
| 382 | while ((key = ddio_KeyInKey()) != 0) |
| 383 | ; |
| 384 | |
| 385 | uint16_t surfval[90000]; |
| 386 | int ff_index = 0; |
| 387 | |
| 388 | for (i = 0; i < rad_NumSurfaces; i++) { |
| 389 | rad_surface *surf = &rad_Surfaces[i]; |
| 390 | |
| 391 | for (t = 0; t < surf->yresolution * surf->xresolution; t++, ff_index++) { |
| 392 | surfval[ff_index] = i; |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | for (i = 0; i < rad_Hemicube.ff_res; i++) { |
| 397 | for (t = 0; t < rad_Hemicube.ff_res; t++) { |
| 398 | int element_num = rad_Hemicube.id_grid[i * rad_Hemicube.ff_res + t]; |
| 399 | float factor; |
| 400 | |
| 401 | if (face == TOP_FACE) |
| 402 | factor = GetTopFactor(i, t); |
| 403 | else |
| 404 | factor = GetSideFactor(i, t); |
| 405 | |
| 406 | if (element_num == -1) |
| 407 | rad_Hemicube.id_grid[i * rad_Hemicube.ff_res + t] = -1; |
| 408 | else { |
| 409 | int surfnum = surfval[element_num]; |
| 410 | int color = surface_colors[surfnum]; |
| 411 | |
| 412 | float norm; |
| 413 | |
| 414 | if (face == TOP_FACE) |
| 415 | norm = factor / Highest_top_delta; |
| 416 | else |
| 417 | norm = factor / Highest_side_delta; |
| 418 | |
| 419 | int r = GR_COLOR_RED(color) * norm; |
| 420 | int g = GR_COLOR_GREEN(color) * norm; |
| 421 | int b = GR_COLOR_BLUE(color) * norm; |
| 422 | |
| 423 | rad_Hemicube.id_grid[i * rad_Hemicube.ff_res + t] = GR_RGB(r, g, b); |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | ShowRadView(); |
| 429 | |
| 430 | while ((key = ddio_KeyInKey()) == 0) { |
| 431 | ; |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | // Update hemicube view transformation matrix |
| 437 | void UpdateView(int face_id) { |
| 438 | vector nu, nv, nn; // View space co-ordinates |
| 439 | |
| 440 | switch (face_id) // Exchange co-ordinates |
| 441 | { |
| 442 | case TOP_FACE: |
| 443 | nu = rad_Hemicube.head_matrix.rvec; |
| 444 | nv = rad_Hemicube.head_matrix.uvec; |
| 445 | nn = rad_Hemicube.head_matrix.fvec; |
| 446 | break; |
| 447 | case FRONT_FACE: |
| 448 | nu = rad_Hemicube.head_matrix.rvec; |
| 449 | nv = rad_Hemicube.head_matrix.fvec; |
| 450 | nn = rad_Hemicube.head_matrix.uvec * -1; |
| 451 | break; |
| 452 | case RIGHT_FACE: |
| 453 | nu = rad_Hemicube.head_matrix.uvec; |
| 454 | nv = rad_Hemicube.head_matrix.fvec; |
| 455 | nn = rad_Hemicube.head_matrix.rvec; |
| 456 | break; |
| 457 | case BACK_FACE: |
| 458 | nu = rad_Hemicube.head_matrix.rvec * -1; |
| 459 | nv = rad_Hemicube.head_matrix.fvec; |
| 460 | nn = (rad_Hemicube.head_matrix.uvec); |
| 461 | break; |
| 462 | case LEFT_FACE: |
| 463 | nu = (rad_Hemicube.head_matrix.uvec * -1); |
| 464 | nv = rad_Hemicube.head_matrix.fvec; |
| 465 | nn = (rad_Hemicube.head_matrix.rvec * -1); |
| 466 | break; |
| 467 | default: |
| 468 | Int3(); |
| 469 | break; |
| 470 | } |
| 471 | |
| 472 | // Build new view transformation matrix |
| 473 | BuildTransform(&nu, &nv, &nn); |
| 474 | } |
| 475 | |
| 476 | void DrawRadiosityPoly(int nv, g3Point **pointlist, int id) { |
| 477 | int i; |
| 478 | g3Codes cc; |
| 479 | bool was_clipped = 0; |
| 480 | int triangulate = 1; |
| 481 | |
| 482 | ASSERT(id >= 0 && id <= rad_NumElements); |
| 483 | |
| 484 | if (triangulate) { |
| 485 | if (nv > 3) { |
| 486 | g3Point *tripoints[3]; |
| 487 | |
| 488 | tripoints[0] = pointlist[0]; |
| 489 | tripoints[2] = pointlist[1]; |
| 490 | |
| 491 | for (i = 0; i < nv - 2; i++) { |
| 492 | tripoints[1] = tripoints[2]; |
| 493 | tripoints[2] = pointlist[2 + i]; |
| 494 | DrawRadiosityPoly(3, tripoints, id); |
| 495 | } |
| 496 | |
| 497 | return; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | // Initialize |
| 502 | cc.cc_or = 0; |
| 503 | cc.cc_and = 0xff; |
| 504 | |
| 505 | // Get codes for this polygon, and copy uvls into points |
| 506 | for (i = 0; i < nv; i++) { |
| 507 | uint8_t c; |
| 508 | |
| 509 | c = pointlist[i]->p3_codes; |
| 510 | |
| 511 | cc.cc_and &= c; |
| 512 | cc.cc_or |= c; |
| 513 | } |
| 514 | |
| 515 | // All points off grid? |
| 516 | if (cc.cc_and) |
| 517 | return; |
| 518 | |
| 519 | // One or more point off screen, so clip |
| 520 | if (cc.cc_or) { |
| 521 | |
| 522 | // Clip the polygon, getting pointer to new buffer |
| 523 | pointlist = g3_ClipPolygon(pointlist, &nv, &cc); |
| 524 | |
| 525 | // Flag as clipped so temp points will be freed |
| 526 | was_clipped = 1; |
| 527 | |
| 528 | // Check for polygon clipped away, or clip otherwise failed |
| 529 | if ((nv == 0) || (cc.cc_or & CC_BEHIND) || cc.cc_and) |
| 530 | goto free_points; |
| 531 | } |
| 532 | |
| 533 | // Make list of 2d coords |
| 534 | for (i = 0; i < nv; i++) { |
| 535 | g3Point *p = pointlist[i]; |
| 536 | g3_ProjectPoint(p); |
| 537 | } |
| 538 | |
| 539 | // Draw! |
| 540 | ScanRadiosityPoly(pointlist, nv, id); |
| 541 | |
| 542 | free_points:; |
| 543 | |
| 544 | // If was clipped, free temp points |
| 545 | if (was_clipped) |
| 546 | g3_FreeTempPoints(pointlist, nv); |
| 547 | } |
| 548 | |
| 549 | /*void GetVertexOrdering (hemicube_point *t, int nv, int *vlt, int *vlb, int *vrt, int *vrb,int *bottom_y_ind) |
| 550 | { |
| 551 | int i; |
| 552 | float min_y,max_y; |
| 553 | int min_y_ind; |
| 554 | int original_vrt; |
| 555 | float min_x; |
| 556 | |
| 557 | // Scan all vertices, set min_y_ind to vertex with smallest y coordinate. |
| 558 | min_y = t[0].sy; |
| 559 | max_y = min_y; |
| 560 | min_y_ind = 0; |
| 561 | min_x = t[0].sx; |
| 562 | *bottom_y_ind = 0; |
| 563 | |
| 564 | for (i=1; i<nv; i++) |
| 565 | { |
| 566 | if (t[i].sy < min_y) |
| 567 | { |
| 568 | min_y = t[i].sy; |
| 569 | min_y_ind = i; |
| 570 | min_x = t[i].sx; |
| 571 | } |
| 572 | else if (t[i].sy == min_y) |
| 573 | { |
| 574 | if (t[i].sx < min_x) |
| 575 | { |
| 576 | min_y_ind = i; |
| 577 | min_x = t[i].sx; |
| 578 | } |
| 579 | } |
| 580 | if (t[i].sy > max_y) |
| 581 | { |
| 582 | max_y = t[i].sy; |
| 583 | *bottom_y_ind = i; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | |
| 588 | // Set "vertex left top", etc. based on vertex with topmost y coordinate |
| 589 | *vlt = min_y_ind; |
| 590 | *vrt = *vlt; |
| 591 | *vlb = PrevIndex(*vlt,nv); |
| 592 | *vrb = NextIndex(*vrt,nv); |
| 593 | |
| 594 | // If right edge is horizontal, then advance along polygon bound until it no longer is or until all |
| 595 | // vertices have been examined. |
| 596 | // (Left edge cannot be horizontal, because *vlt is set to leftmost point with highest y coordinate.) |
| 597 | |
| 598 | original_vrt = *vrt; |
| 599 | |
| 600 | while (t[*vrt].sy == t[*vrb].sy) |
| 601 | { |
| 602 | if (NextIndex(*vrt,nv) == original_vrt) |
| 603 | break; |
| 604 | |
| 605 | *vrt = NextIndex(*vrt,nv); |
| 606 | *vrb = NextIndex(*vrt,nv); |
| 607 | } |
| 608 | }*/ |
| 609 | |
| 610 | void GetVertexOrdering(hemicube_point *t, int nv, int *vlt, int *vlb, int *vrt, int *vrb, float *top_y, float *bottom_y, |
| 611 | int *left_edge_dir) { |
| 612 | int i; |
| 613 | float min_y, max_y; |
| 614 | int min_index_l, min_index_r, max_index; |
| 615 | |
| 616 | // Scan all vertices, set min_y_ind to vertex with smallest y coordinate. |
| 617 | |
| 618 | *bottom_y = 0; |
| 619 | *top_y = 0; |
| 620 | |
| 621 | min_index_l = max_index = 0; |
| 622 | max_y = min_y = t[0].sy; |
| 623 | for (i = 1; i < nv; i++) { |
| 624 | if (t[i].sy < min_y) |
| 625 | min_y = t[min_index_l = i].sy; |
| 626 | else if (t[i].sy > max_y) |
| 627 | max_y = t[max_index = i].sy; |
| 628 | } |
| 629 | if (min_y == max_y) |
| 630 | return; |
| 631 | |
| 632 | // Scan in ascending order to find the last top-edge point */ |
| 633 | min_index_r = min_index_l; |
| 634 | while (t[min_index_r].sy == min_y) |
| 635 | min_index_r = NextIndex(min_index_r, nv); |
| 636 | min_index_r = PrevIndex(min_index_r, nv); |
| 637 | |
| 638 | // Now scan in descending order to find the first top-edge point |
| 639 | while (t[min_index_l].sy == min_y) |
| 640 | min_index_l = PrevIndex(min_index_l, nv); |
| 641 | min_index_l = NextIndex(min_index_l, nv); |
| 642 | |
| 643 | *left_edge_dir = -1; |
| 644 | if (t[min_index_l].sx != t[min_index_r].sx) { |
| 645 | // If the top is flat, just see which of the ends is leftmost |
| 646 | if (t[min_index_l].sx > t[min_index_r].sx) { |
| 647 | *left_edge_dir = 1; |
| 648 | |
| 649 | int temp = min_index_l; |
| 650 | min_index_l = min_index_r; |
| 651 | min_index_r = temp; |
| 652 | } |
| 653 | } else { |
| 654 | // Point to the downward end of the first line of each of the |
| 655 | // two edges down from the top |
| 656 | int next_index = min_index_r; |
| 657 | next_index = NextIndex(next_index, nv); |
| 658 | int prev_index = min_index_r; |
| 659 | prev_index = PrevIndex(prev_index, nv); |
| 660 | |
| 661 | /* Calculate X and Y lengths from the top vertex to the end of |
| 662 | the first line down each edge; use those to compare slopes |
| 663 | and see which line is leftmost */ |
| 664 | float deltaXN = t[next_index].sx - t[min_index_l].sx; |
| 665 | float deltaYN = t[next_index].sy - t[min_index_l].sy; |
| 666 | float deltaXP = t[prev_index].sx - t[min_index_l].sx; |
| 667 | float deltaYP = t[prev_index].sy - t[min_index_l].sy; |
| 668 | if (((deltaXN * deltaYP) - (deltaYN * deltaXP)) < 0) { |
| 669 | *left_edge_dir = 1; |
| 670 | |
| 671 | int temp = min_index_l; |
| 672 | min_index_l = min_index_r; |
| 673 | min_index_r = temp; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | *bottom_y = max_y; |
| 678 | *top_y = min_y; |
| 679 | |
| 680 | *vlt = min_index_l; |
| 681 | *vrt = min_index_r; |
| 682 | |
| 683 | if (*left_edge_dir == -1) { |
| 684 | *vlb = PrevIndex(min_index_l, nv); |
| 685 | *vrb = NextIndex(min_index_r, nv); |
| 686 | } else { |
| 687 | *vrb = PrevIndex(min_index_r, nv); |
| 688 | *vlb = NextIndex(min_index_l, nv); |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | // Returns number preceding val modulo modulus. |
| 693 | // prevmod(3,4) = 2 |
| 694 | // prevmod(0,4) = 3 |
| 695 | int PrevIndex(int val, int modulus) { |
| 696 | if (val > 0) |
| 697 | return val - 1; |
| 698 | else |
| 699 | return modulus - 1; |
| 700 | } |
| 701 | |
| 702 | // Returns number succeeding val modulo modulus. |
| 703 | // succmod(3,4) = 0 |
| 704 | // succmod(0,4) = 1 |
| 705 | int NextIndex(int val, int modulus) { |
| 706 | if (val < modulus - 1) |
| 707 | return val + 1; |
| 708 | else |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | void ScanRadiosityPoly(g3Point **pl, int nv, int element_id) { |
| 713 | int i, left_edge_dir; |
| 714 | |
| 715 | float Delta_right_x, Right_x, Delta_left_x, Left_x; |
| 716 | float Left_z, Right_z, Delta_left_z, Delta_right_z; |
| 717 | |
| 718 | float height, left_height, right_height; |
| 719 | int vlt, vlb, vrt, vrb, desty; |
| 720 | float top_y, bottom_y, next_break_left, next_break_right, y; |
| 721 | |
| 722 | hemicube_point cp[100]; |
| 723 | |
| 724 | int destptr; |
| 725 | |
| 726 | ASSERT(element_id >= 0 && element_id <= rad_NumElements); |
| 727 | |
| 728 | for (i = 0; i < nv; i++) { |
| 729 | g3Point p; |
| 730 | p = *pl[i]; |
| 731 | |
| 732 | cp[i].sx = p.p3_sx; |
| 733 | cp[i].sy = p.p3_sy; |
| 734 | cp[i].z = 1.0 / (float)p.p3_vec.z; |
| 735 | } |
| 736 | |
| 737 | // Determine top and bottom y coords. |
| 738 | GetVertexOrdering(cp, nv, &vlt, &vlb, &vrt, &vrb, &top_y, &bottom_y, &left_edge_dir); |
| 739 | |
| 740 | height = bottom_y - top_y; |
| 741 | if (height < 1.0) |
| 742 | return; |
| 743 | |
| 744 | #include "radscan_leftedge.h" |
| 745 | #include "radscan_rightedge.h" |
| 746 | |
| 747 | // Set the destptr to equal the first row to draw to |
| 748 | destptr = ((int)top_y * rad_Hemicube.ff_res); |
| 749 | desty = (int)top_y; |
| 750 | |
| 751 | for (y = top_y; y < bottom_y; y++) { |
| 752 | if (y >= next_break_left) { |
| 753 | do { |
| 754 | vlt = vlb; |
| 755 | |
| 756 | if (left_edge_dir == -1) |
| 757 | vlb = PrevIndex(vlb, nv); |
| 758 | else |
| 759 | vlb = NextIndex(vlb, nv); |
| 760 | } while (y == cp[vlb].sy); |
| 761 | |
| 762 | #include "radscan_leftedge.h" |
| 763 | } |
| 764 | |
| 765 | if (y >= next_break_right) { |
| 766 | do { |
| 767 | vrt = vrb; |
| 768 | if (left_edge_dir == -1) |
| 769 | vrb = NextIndex(vrb, nv); |
| 770 | else |
| 771 | vrb = PrevIndex(vrb, nv); |
| 772 | |
| 773 | } while (y == cp[vrb].sy); |
| 774 | #include "radscan_rightedge.h" |
| 775 | } |
| 776 | |
| 777 | // Draw a scanline |
| 778 | |
| 779 | float sl, sr; |
| 780 | float lz, rz; |
| 781 | |
| 782 | sl = Left_x; |
| 783 | sr = Right_x; |
| 784 | |
| 785 | lz = Left_z; |
| 786 | rz = Right_z; |
| 787 | |
| 788 | int x1 = sl; |
| 789 | int width = (sr - x1) + 1; |
| 790 | |
| 791 | if (desty < 0 || desty >= rad_Hemicube.ff_res) |
| 792 | goto UpdateExtents; |
| 793 | |
| 794 | if (x1 < 0 || x1 >= rad_Hemicube.ff_res) |
| 795 | goto UpdateExtents; |
| 796 | if ((x1 + width) > rad_Hemicube.ff_res) { |
| 797 | width = (rad_Hemicube.ff_res - x1); |
| 798 | } |
| 799 | |
| 800 | if (width > 0) { |
| 801 | float z = lz; |
| 802 | float dz = (rz - z) / width; |
| 803 | |
| 804 | // Enter scan line |
| 805 | for (int x = x1; x < x1 + width; x++) { |
| 806 | float realz = 1.0 / z; |
| 807 | // Check element visibility |
| 808 | if (realz <= rad_Hemicube.depth_grid[destptr + x]) { |
| 809 | // Update Z-buffer |
| 810 | rad_Hemicube.depth_grid[destptr + x] = realz; |
| 811 | |
| 812 | // Set polygon identifier |
| 813 | rad_Hemicube.id_grid[destptr + x] = element_id; |
| 814 | } |
| 815 | // Update element pseudodepth |
| 816 | z += dz; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | UpdateExtents: |
| 821 | |
| 822 | destptr += rad_Hemicube.ff_res; |
| 823 | desty++; |
| 824 | |
| 825 | Left_x += Delta_left_x; |
| 826 | Right_x += Delta_right_x; |
| 827 | Right_z += Delta_right_z; |
| 828 | Left_z += Delta_left_z; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | float GetTopFactor(int row, int col) { |
| 833 | if (row >= rad_Hemicube.grid_dim) |
| 834 | row -= rad_Hemicube.grid_dim; |
| 835 | else |
| 836 | row = rad_Hemicube.grid_dim - row - 1; |
| 837 | |
| 838 | if (col >= rad_Hemicube.grid_dim) |
| 839 | col -= rad_Hemicube.grid_dim; |
| 840 | else |
| 841 | col = rad_Hemicube.grid_dim - col - 1; |
| 842 | |
| 843 | return rad_Hemicube.top_array[row * rad_Hemicube.grid_dim + col]; |
| 844 | } |
| 845 | |
| 846 | // Get side face cell form factor |
| 847 | float GetSideFactor(int row, int col) { |
| 848 | if (col >= rad_Hemicube.grid_dim) |
| 849 | col -= rad_Hemicube.grid_dim; |
| 850 | else |
| 851 | col = rad_Hemicube.grid_dim - col - 1; |
| 852 | |
| 853 | if (row >= rad_Hemicube.grid_dim) |
| 854 | return 0.0f; |
| 855 | else |
| 856 | row = rad_Hemicube.grid_dim - row - 1; |
| 857 | |
| 858 | return rad_Hemicube.side_array[(row * rad_Hemicube.grid_dim) + col]; |
| 859 | } |
| 860 | |
| 861 | // Sums the delta form factors |
| 862 | void SumDeltas(float *ff_array, int face_id) { |
| 863 | int poly_id; // Polygon identifier |
| 864 | int row, col; // Face cell indices |
| 865 | |
| 866 | Cracks_this_side = 0; |
| 867 | |
| 868 | if (face_id == TOP_FACE) { |
| 869 | // Scan entire face buffer |
| 870 | for (row = 0; row < rad_Hemicube.ff_res; row++) { |
| 871 | for (col = 0; col < rad_Hemicube.ff_res; col++) { |
| 872 | poly_id = rad_Hemicube.id_grid[row * rad_Hemicube.ff_res + col]; |
| 873 | if (poly_id != -1) { |
| 874 | if (Shoot_from_patch) |
| 875 | ff_array[poly_id] += (GetTopFactor(row, col)); |
| 876 | else |
| 877 | ff_array[poly_id] += (GetTopFactor(row, col) * (rad_MaxElement->area / rad_MaxSurface->area)); |
| 878 | } else { |
| 879 | Cracks_this_frame++; |
| 880 | Cracks_this_side++; |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | } else { |
| 885 | // Scan upper half of face buffer only |
| 886 | for (row = 0; row < rad_Hemicube.grid_dim; row++) { |
| 887 | for (col = 0; col < rad_Hemicube.ff_res; col++) { |
| 888 | poly_id = rad_Hemicube.id_grid[row * rad_Hemicube.ff_res + col]; |
| 889 | if (poly_id != -1) { |
| 890 | if (Shoot_from_patch) |
| 891 | ff_array[poly_id] += GetSideFactor(row, col); |
| 892 | else |
| 893 | ff_array[poly_id] += (GetSideFactor(row, col) * (rad_MaxElement->area / rad_MaxSurface->area)); |
| 894 | } else { |
| 895 | Cracks_this_frame++; |
| 896 | Cracks_this_side++; |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | } |