| 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/editor/editor_lighting.cpp $ |
| 21 | * $Revision: 1.1.1.1 $ |
| 22 | * $Date: 2003-08-26 03:57:38 $ |
| 23 | * $Author: kevinb $ |
| 24 | * $NoKeywords: $ |
| 25 | */ |
| 26 | |
| 27 | #include <algorithm> |
| 28 | #include <windows.h> |
| 29 | #include "3d.h" |
| 30 | #include "gametexture.h" |
| 31 | #include "erooms.h" |
| 32 | #include "editor_lighting.h" |
| 33 | #include "descent.h" |
| 34 | #include "room.h" |
| 35 | #include "lightmap.h" |
| 36 | #include "polymodel.h" |
| 37 | #include <string.h> |
| 38 | #include <stdlib.h> |
| 39 | #include "terrain.h" |
| 40 | #include "radiosity.h" |
| 41 | #include "lighting.h" |
| 42 | #include "findintersection.h" |
| 43 | #include "lightmap_info.h" |
| 44 | #include "object_lighting.h" |
| 45 | #include "d3edit.h" |
| 46 | #include "ddio.h" |
| 47 | #include "bsp.h" |
| 48 | #include "Application.h" |
| 49 | #include "AppDatabase.h" |
| 50 | #include "program.h" |
| 51 | #include "loadlevel.h" |
| 52 | #include "special_face.h" |
| 53 | #include "boa.h" |
| 54 | #include "mem.h" |
| 55 | |
| 56 | struct spec_vertex { |
| 57 | float x, y; |
| 58 | }; |
| 59 | |
| 60 | int AllowCombining = 1; |
| 61 | float GlobalMultiplier = 1.0; |
| 62 | |
| 63 | rad_surface *Light_surfaces = NULL; |
| 64 | |
| 65 | vector ScratchCenters[MAX_LIGHTMAP_INFOS]; |
| 66 | vector ScratchRVecs[MAX_LIGHTMAP_INFOS]; |
| 67 | vector ScratchUVecs[MAX_LIGHTMAP_INFOS]; |
| 68 | |
| 69 | float Room_multiplier[MAX_ROOMS + MAX_PALETTE_ROOMS]; |
| 70 | float Room_ambience_r[MAX_ROOMS + MAX_PALETTE_ROOMS], Room_ambience_g[MAX_ROOMS + MAX_PALETTE_ROOMS], |
| 71 | Room_ambience_b[MAX_ROOMS + MAX_PALETTE_ROOMS]; |
| 72 | |
| 73 | uint8_t *TerrainLightSpeedup[MAX_SATELLITES]; |
| 74 | |
| 75 | int LightSpacing = LIGHTMAP_SPACING; |
| 76 | int BestFit = 0; |
| 77 | int Square_surfaces = 0; |
| 78 | int Lightmaps_for_rad = 0; |
| 79 | |
| 80 | // Ambient values for terrain |
| 81 | float Ambient_red = 0.0f, Ambient_green = 0.0f, Ambient_blue = 0.0f; |
| 82 | |
| 83 | void DoTerrainDynamicTable(); |
| 84 | |
| 85 | uint8_t *Lightmap_mask = NULL; |
| 86 | static uint8_t *Lmi_spoken_for; |
| 87 | int Squeeze_lightmap_handle = -1; |
| 88 | |
| 89 | int FindEmptyMaskSpot(int w, int h, int *dest_x, int *dest_y) { |
| 90 | int cur_x = 0, cur_y = 0; |
| 91 | int i, t; |
| 92 | |
| 93 | for (cur_y = 0; cur_y < 128; cur_y++) { |
| 94 | if (cur_y + h > 128) |
| 95 | return 0; |
| 96 | for (cur_x = 0; cur_x < 128; cur_x++) { |
| 97 | if (cur_x + w > 128) |
| 98 | continue; |
| 99 | |
| 100 | int hit_mask = 0; |
| 101 | for (i = 0; i < h && !hit_mask; i++) { |
| 102 | for (t = 0; t < w && !hit_mask; t++) { |
| 103 | if (Lightmap_mask[((cur_y + i) * 128) + cur_x + t]) |
| 104 | hit_mask = 1; |
| 105 | } |
| 106 | } |
| 107 | if (hit_mask == 0) { |
| 108 | // Hurray! We found an empty spot |
| 109 | *dest_x = cur_x; |
| 110 | *dest_y = cur_y; |
| 111 | return 1; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | void CopySqueezeBodyAndEdges(uint16_t *dest_data, uint16_t *src_data, int w, int h, int dest_x, int dest_y) { |
| 120 | int i, t; |
| 121 | |
| 122 | ASSERT(w + dest_x <= 126); |
| 123 | ASSERT(h + dest_y <= 126); |
| 124 | ASSERT(dest_x >= 0 && dest_y >= 0); |
| 125 | |
| 126 | // First copy the main body |
| 127 | for (i = 0; i < h; i++) { |
| 128 | for (t = 0; t < w; t++) { |
| 129 | dest_data[((dest_y + 1 + i) * 128) + (dest_x + 1 + t)] = src_data[(w * i) + t]; |
| 130 | Lightmap_mask[((dest_y + i + 1) * 128) + dest_x + 1 + t] = 1; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Now copy the edges |
| 135 | // Left edge |
| 136 | for (i = 0; i < h; i++) { |
| 137 | dest_data[((dest_y + 1 + i) * 128) + (dest_x + 0)] = src_data[(w * i) + 0]; |
| 138 | Lightmap_mask[((dest_y + i + 1) * 128) + dest_x + 0] = 1; |
| 139 | } |
| 140 | // Right edge |
| 141 | for (i = 0; i < h; i++) { |
| 142 | dest_data[((dest_y + 1 + i) * 128) + (dest_x + w + 1)] = src_data[(w * i) + (w - 1)]; |
| 143 | Lightmap_mask[((dest_y + i + 1) * 128) + dest_x + w + 1] = 1; |
| 144 | } |
| 145 | // Top edge |
| 146 | for (i = 0; i < w; i++) { |
| 147 | dest_data[(dest_y * 128) + (dest_x + i + 1)] = src_data[i]; |
| 148 | Lightmap_mask[(dest_y * 128) + (dest_x + i + 1)] = 1; |
| 149 | } |
| 150 | // Bottom edge |
| 151 | for (i = 0; i < w; i++) { |
| 152 | dest_data[((dest_y + 1 + h) * 128) + (dest_x + i + 1)] = src_data[(w * (h - 1)) + i]; |
| 153 | Lightmap_mask[((dest_y + 1 + h) * 128) + (dest_x + i + 1)] = 1; |
| 154 | } |
| 155 | |
| 156 | // Now copy the corners |
| 157 | // Upper left |
| 158 | dest_data[(dest_y * 128) + (dest_x)] = src_data[0]; |
| 159 | Lightmap_mask[(dest_y * 128) + (dest_x)] = 1; |
| 160 | // Upper right |
| 161 | dest_data[(dest_y * 128) + (dest_x + w + 1)] = src_data[w - 1]; |
| 162 | Lightmap_mask[(dest_y * 128) + (dest_x + w + 1)] = 1; |
| 163 | // Lower left |
| 164 | dest_data[((dest_y + h + 1) * 128) + (dest_x)] = src_data[w * (h - 1)]; |
| 165 | Lightmap_mask[((dest_y + h + 1) * 128) + (dest_x)] = 1; |
| 166 | // Lower right |
| 167 | dest_data[((dest_y + h + 1) * 128) + (dest_x + w + 1)] = src_data[(w * (h - 1)) + (w - 1)]; |
| 168 | Lightmap_mask[((dest_y + h + 1) * 128) + (dest_x + w + 1)] = 1; |
| 169 | } |
| 170 | |
| 171 | void CopySqueezeDataForRooms(int roomnum, int facenum, uint16_t *dest_data, int dest_x, int dest_y) { |
| 172 | room *rp = &Rooms[roomnum]; |
| 173 | lightmap_info *lmi_ptr = &LightmapInfo[rp->faces[facenum].lmi_handle]; |
| 174 | uint16_t *src_data = (uint16_t *)lm_data(lmi_ptr->lm_handle); |
| 175 | |
| 176 | int w = lmi_ptr->width; |
| 177 | int h = lmi_ptr->height; |
| 178 | |
| 179 | // Copy over the actual lightmap data |
| 180 | CopySqueezeBodyAndEdges(dest_data, src_data, w, h, dest_x, dest_y); |
| 181 | |
| 182 | // Now alter all face uvs that have this lightmap info |
| 183 | float dest_u = (float)(dest_x + 1) / 128.0; |
| 184 | float dest_v = (float)(dest_y + 1) / 128.0; |
| 185 | |
| 186 | float u_scalar = (float)w / 128.0; |
| 187 | float v_scalar = (float)h / 128.0; |
| 188 | |
| 189 | for (int t = roomnum; t <= Highest_room_index; t++) { |
| 190 | room *this_rp = &Rooms[t]; |
| 191 | if (!this_rp->used) |
| 192 | continue; |
| 193 | |
| 194 | for (int j = 0; j < this_rp->num_faces; j++) { |
| 195 | |
| 196 | if (!(this_rp->faces[j].flags & FF_LIGHTMAP)) |
| 197 | continue; |
| 198 | |
| 199 | if (this_rp->faces[j].lmi_handle == BAD_LMI_INDEX) |
| 200 | continue; |
| 201 | |
| 202 | if (Lmi_spoken_for[this_rp->faces[j].lmi_handle]) |
| 203 | continue; |
| 204 | if (this_rp->faces[j].lmi_handle == rp->faces[facenum].lmi_handle) { |
| 205 | lmi_ptr->x1 = dest_x + 1; |
| 206 | lmi_ptr->y1 = dest_y + 1; |
| 207 | |
| 208 | // We have to alter our uvs for this face to reflect our change |
| 209 | face *fp = &this_rp->faces[j]; |
| 210 | for (int k = 0; k < fp->num_verts; k++) { |
| 211 | fp->face_uvls[k].u2 *= u_scalar; |
| 212 | fp->face_uvls[k].u2 += dest_u; |
| 213 | |
| 214 | fp->face_uvls[k].v2 *= v_scalar; |
| 215 | fp->face_uvls[k].v2 += dest_v; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | ASSERT(rp->faces[facenum].lmi_handle != BAD_LMI_INDEX); |
| 222 | |
| 223 | Lmi_spoken_for[rp->faces[facenum].lmi_handle] = 1; |
| 224 | |
| 225 | // Free our old lightmap |
| 226 | GameLightmaps[lmi_ptr->lm_handle].used = 1; |
| 227 | lm_FreeLightmap(lmi_ptr->lm_handle); |
| 228 | |
| 229 | lmi_ptr->lm_handle = Squeeze_lightmap_handle; |
| 230 | GameLightmaps[Squeeze_lightmap_handle].used++; |
| 231 | } |
| 232 | |
| 233 | void CopySqueezeDataForObject(object *obj, int subnum, int facenum, uint16_t *dest_data, int dest_x, int dest_y) { |
| 234 | lightmap_object_face *fp = &obj->lm_object.lightmap_faces[subnum][facenum]; |
| 235 | lightmap_info *lmi_ptr = &LightmapInfo[fp->lmi_handle]; |
| 236 | uint16_t *src_data = (uint16_t *)lm_data(lmi_ptr->lm_handle); |
| 237 | int t, k; |
| 238 | |
| 239 | int w = lmi_ptr->width; |
| 240 | int h = lmi_ptr->height; |
| 241 | |
| 242 | // Copy over the actual lightmap data |
| 243 | CopySqueezeBodyAndEdges(dest_data, src_data, w, h, dest_x, dest_y); |
| 244 | |
| 245 | // Now alter all face uvs that have this lightmap info |
| 246 | float dest_u = (float)(dest_x + 1) / 128.0; |
| 247 | float dest_v = (float)(dest_y + 1) / 128.0; |
| 248 | |
| 249 | float u_scalar = (float)w / 128.0; |
| 250 | float v_scalar = (float)h / 128.0; |
| 251 | |
| 252 | for (int objnum = obj - Objects; objnum != -1; objnum = Objects[objnum].next) { |
| 253 | object *this_obj = &Objects[objnum]; |
| 254 | |
| 255 | if (this_obj->lighting_render_type != LRT_LIGHTMAPS) |
| 256 | continue; |
| 257 | if (!this_obj->lm_object.used) |
| 258 | continue; |
| 259 | |
| 260 | for (t = 0; t < this_obj->lm_object.num_models; t++) { |
| 261 | if (IsNonRenderableSubmodel(&Poly_models[this_obj->rtype.pobj_info.model_num], t)) |
| 262 | continue; |
| 263 | |
| 264 | for (k = 0; k < this_obj->lm_object.num_faces[t]; k++) { |
| 265 | lightmap_object_face *this_fp = &this_obj->lm_object.lightmap_faces[t][k]; |
| 266 | int lmi_handle = this_fp->lmi_handle; |
| 267 | if (lmi_handle == BAD_LMI_INDEX) |
| 268 | continue; |
| 269 | if (Lmi_spoken_for[lmi_handle]) |
| 270 | continue; |
| 271 | |
| 272 | if (lmi_handle == fp->lmi_handle) { |
| 273 | lmi_ptr->x1 = dest_x + 1; |
| 274 | lmi_ptr->y1 = dest_y + 1; |
| 275 | |
| 276 | // We have to alter our uvs for this face to reflect our change |
| 277 | for (int j = 0; j < this_fp->num_verts; j++) { |
| 278 | this_fp->u2[j] *= u_scalar; |
| 279 | this_fp->u2[j] += dest_u; |
| 280 | |
| 281 | this_fp->v2[j] *= v_scalar; |
| 282 | this_fp->v2[j] += dest_v; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | ASSERT(fp->lmi_handle != BAD_LMI_INDEX); |
| 290 | Lmi_spoken_for[fp->lmi_handle] = 1; |
| 291 | |
| 292 | // Free our old lightmap |
| 293 | GameLightmaps[lmi_ptr->lm_handle].used = 1; |
| 294 | lm_FreeLightmap(lmi_ptr->lm_handle); |
| 295 | |
| 296 | lmi_ptr->lm_handle = Squeeze_lightmap_handle; |
| 297 | GameLightmaps[Squeeze_lightmap_handle].used++; |
| 298 | } |
| 299 | |
| 300 | // Simply clears flags for combine portals |
| 301 | void ClearCombinePortals(int terrain) { |
| 302 | for (int i = 0; i <= Highest_room_index; i++) { |
| 303 | room *rp = &Rooms[i]; |
| 304 | |
| 305 | if (!rp->used) |
| 306 | continue; |
| 307 | |
| 308 | if (terrain && !(rp->flags & RF_EXTERNAL)) |
| 309 | continue; |
| 310 | |
| 311 | if (!terrain && (rp->flags & RF_EXTERNAL)) |
| 312 | continue; |
| 313 | |
| 314 | for (int t = 0; t < rp->num_portals; t++) { |
| 315 | portal *portal_a = &rp->portals[t]; |
| 316 | portal_a->flags &= ~PF_COMBINED; |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | // For rendering...combines all portals if they are on the same plane |
| 322 | void CheckCombinePortals(int terrain) { |
| 323 | ClearCombinePortals(terrain); |
| 324 | |
| 325 | int combine_count = 0; |
| 326 | mprintf(0, "Combining portals..."); |
| 327 | |
| 328 | for (int i = 0; i <= Highest_room_index; i++) { |
| 329 | room *rp = &Rooms[i]; |
| 330 | |
| 331 | if (!rp->used) |
| 332 | continue; |
| 333 | |
| 334 | if (terrain && !(rp->flags & RF_EXTERNAL)) |
| 335 | continue; |
| 336 | |
| 337 | if (!terrain && (rp->flags & RF_EXTERNAL)) |
| 338 | continue; |
| 339 | |
| 340 | for (int t = 0; t < rp->num_portals; t++) { |
| 341 | portal *portal_a = &rp->portals[t]; |
| 342 | face *face_a = &rp->faces[portal_a->portal_face]; |
| 343 | |
| 344 | if (portal_a->flags & PF_COMBINED) |
| 345 | continue; |
| 346 | |
| 347 | // Don't combine if face is breakable |
| 348 | if (GameTextures[face_a->tmap].flags & TF_BREAKABLE) |
| 349 | continue; |
| 350 | |
| 351 | float dist_a = vm_DotProduct(&rp->verts[face_a->face_verts[0]], &face_a->normal); |
| 352 | |
| 353 | for (int k = 0; k < rp->num_portals; k++) { |
| 354 | portal *portal_b = &rp->portals[k]; |
| 355 | face *face_b = &rp->faces[portal_b->portal_face]; |
| 356 | |
| 357 | if (portal_b->flags & PF_COMBINED) |
| 358 | continue; |
| 359 | |
| 360 | // Don't combine if one portal is render-faces and the other is not |
| 361 | if ((portal_a->flags & PF_RENDER_FACES) != (portal_b->flags & PF_RENDER_FACES)) |
| 362 | continue; |
| 363 | |
| 364 | // Don't combine if face is breakable |
| 365 | if (GameTextures[face_b->tmap].flags & TF_BREAKABLE) |
| 366 | continue; |
| 367 | |
| 368 | if (t == k) |
| 369 | continue; |
| 370 | |
| 371 | // Check to see if the portals connect to the same room |
| 372 | if (portal_a->croom != portal_b->croom) |
| 373 | continue; |
| 374 | |
| 375 | /*// Check to see if they share a normal |
| 376 | float dp=vm_DotProduct (&face_a->normal,&face_b->normal); |
| 377 | |
| 378 | if (dp < .95f) |
| 379 | continue; |
| 380 | |
| 381 | |
| 382 | // Check to see if the distances are same |
| 383 | float dist_b=vm_DotProduct (&rp->verts[face_b->face_verts[0]],&face_b->normal); |
| 384 | |
| 385 | if (fabs(dist_b-dist_a)>.5) |
| 386 | continue; |
| 387 | |
| 388 | int match=0; |
| 389 | // Test to see if any points at all can touch |
| 390 | for (int x=0; x<face_a->num_verts && !match; x++ ) |
| 391 | { |
| 392 | for (int y=0; y<face_b->num_verts && !match; y++ ) |
| 393 | { |
| 394 | if (PointsAreSame(&rp->verts[face_a->face_verts[x]],&rp->verts[face_b->face_verts[y]])) |
| 395 | match=1; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | if (match==0) |
| 400 | continue; |
| 401 | |
| 402 | //Check to see if the portals connect to the same room |
| 403 | if (portal_a->croom != portal_b->croom) |
| 404 | continue; |
| 405 | |
| 406 | // Hurray! These portals can be combined*/ |
| 407 | portal_a->flags |= PF_COMBINED; |
| 408 | portal_b->flags |= PF_COMBINED; |
| 409 | |
| 410 | portal_a->combine_master = t; |
| 411 | portal_b->combine_master = t; |
| 412 | combine_count++; |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | mprintf(0, "%d portals combined.\n", combine_count); |
| 418 | } |
| 419 | |
| 420 | // Squeezes all the lightmaps down into as few 128x128s as possible |
| 421 | void SqueezeLightmaps(int external, int target_roomnum) { |
| 422 | int i, t, k; |
| 423 | mprintf(0, "Squeezing %s lightmaps, please wait...\n", external ? "external" : "internal"); |
| 424 | |
| 425 | Lmi_spoken_for = (uint8_t *)mem_malloc(MAX_LIGHTMAP_INFOS); |
| 426 | Lightmap_mask = (uint8_t *)mem_malloc(128 * 128); |
| 427 | Squeeze_lightmap_handle = -1; |
| 428 | |
| 429 | ASSERT(Lightmap_mask); |
| 430 | ASSERT(Lmi_spoken_for); |
| 431 | memset(Lmi_spoken_for, 0, MAX_LIGHTMAP_INFOS); |
| 432 | memset(Lightmap_mask, 0, 128 * 128); |
| 433 | |
| 434 | // Go through all the rooms and sqeeze them one by one |
| 435 | for (i = 0; i <= Highest_room_index; i++) { |
| 436 | room *rp = &Rooms[i]; |
| 437 | if (!rp->used) |
| 438 | continue; |
| 439 | if (rp->flags & RF_NO_LIGHT) |
| 440 | continue; |
| 441 | |
| 442 | if (external) { |
| 443 | if (!(rp->flags & RF_EXTERNAL)) |
| 444 | continue; |
| 445 | } else { |
| 446 | if ((rp->flags & RF_EXTERNAL)) |
| 447 | continue; |
| 448 | |
| 449 | if (target_roomnum != -1 && target_roomnum != i) |
| 450 | continue; |
| 451 | } |
| 452 | |
| 453 | // Go through each face |
| 454 | for (t = 0; t < rp->num_faces; t++) { |
| 455 | face *fp = &rp->faces[t]; |
| 456 | int lmi_handle = fp->lmi_handle; |
| 457 | |
| 458 | if (!(fp->flags & FF_LIGHTMAP)) |
| 459 | continue; |
| 460 | |
| 461 | if (lmi_handle == BAD_LMI_INDEX) |
| 462 | continue; |
| 463 | if (Lmi_spoken_for[lmi_handle]) |
| 464 | continue; |
| 465 | |
| 466 | lightmap_info *lmi = &LightmapInfo[lmi_handle]; |
| 467 | |
| 468 | int dest_x, dest_y; |
| 469 | int src_w = lmi->width; |
| 470 | int src_h = lmi->height; |
| 471 | |
| 472 | if (Squeeze_lightmap_handle == -1) { |
| 473 | memset(Lightmap_mask, 0, 128 * 128); |
| 474 | Squeeze_lightmap_handle = lm_AllocLightmap(128, 128); |
| 475 | uint16_t *fill_data = (uint16_t *)lm_data(Squeeze_lightmap_handle); |
| 476 | memset(fill_data, 0, 128 * 128 * 2); |
| 477 | } |
| 478 | |
| 479 | if (FindEmptyMaskSpot(src_w + 2, src_h + 2, &dest_x, &dest_y)) { |
| 480 | // Cool, found a spot |
| 481 | CopySqueezeDataForRooms(i, t, lm_data(Squeeze_lightmap_handle), dest_x, dest_y); |
| 482 | } else { |
| 483 | // Can't find an empty slot, so search through all the other remaining faces |
| 484 | for (k = t; k < rp->num_faces; k++) { |
| 485 | face *fp = &rp->faces[k]; |
| 486 | int lmi_handle = fp->lmi_handle; |
| 487 | |
| 488 | if (!(fp->flags & FF_LIGHTMAP)) |
| 489 | continue; |
| 490 | if (lmi_handle == BAD_LMI_INDEX) |
| 491 | continue; |
| 492 | if (Lmi_spoken_for[lmi_handle]) |
| 493 | continue; |
| 494 | |
| 495 | lightmap_info *lmi = &LightmapInfo[lmi_handle]; |
| 496 | |
| 497 | int src_w = lmi->width; |
| 498 | int src_h = lmi->height; |
| 499 | |
| 500 | if (FindEmptyMaskSpot(src_w + 2, src_h + 2, &dest_x, &dest_y)) { |
| 501 | CopySqueezeDataForRooms(i, k, lm_data(Squeeze_lightmap_handle), dest_x, dest_y); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | // Now, allocate a new lightmap and start over |
| 506 | ASSERT(Squeeze_lightmap_handle != -1); |
| 507 | ASSERT(GameLightmaps[Squeeze_lightmap_handle].used != 1); |
| 508 | GameLightmaps[Squeeze_lightmap_handle].used--; |
| 509 | |
| 510 | memset(Lightmap_mask, 0, 128 * 128); |
| 511 | Squeeze_lightmap_handle = lm_AllocLightmap(128, 128); |
| 512 | uint16_t *fill_data = (uint16_t *)lm_data(Squeeze_lightmap_handle); |
| 513 | memset(fill_data, 0, 128 * 128 * 2); |
| 514 | |
| 515 | ASSERT(Lmi_spoken_for[lmi_handle] == 0); |
| 516 | |
| 517 | if (FindEmptyMaskSpot(src_w + 2, src_h + 2, &dest_x, &dest_y)) { |
| 518 | CopySqueezeDataForRooms(i, t, lm_data(Squeeze_lightmap_handle), dest_x, dest_y); |
| 519 | } else { |
| 520 | Int3(); // Get Jason, how did this happen???? |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | // Now search through all the objects in this room |
| 526 | for (k = rp->objects; k != -1; k = Objects[k].next) { |
| 527 | object *obj = &Objects[k]; |
| 528 | if (obj->lighting_render_type != LRT_LIGHTMAPS) |
| 529 | continue; |
| 530 | if (!obj->lm_object.used) |
| 531 | continue; |
| 532 | |
| 533 | for (t = 0; t < obj->lm_object.num_models; t++) { |
| 534 | if (IsNonRenderableSubmodel(&Poly_models[obj->rtype.pobj_info.model_num], t)) |
| 535 | continue; |
| 536 | |
| 537 | for (int j = 0; j < obj->lm_object.num_faces[t]; j++) { |
| 538 | lightmap_object_face *fp = &obj->lm_object.lightmap_faces[t][j]; |
| 539 | int lmi_handle = fp->lmi_handle; |
| 540 | if (lmi_handle == BAD_LMI_INDEX) |
| 541 | continue; |
| 542 | if (Lmi_spoken_for[lmi_handle]) |
| 543 | continue; |
| 544 | |
| 545 | lightmap_info *lmi = &LightmapInfo[lmi_handle]; |
| 546 | |
| 547 | int dest_x, dest_y; |
| 548 | int src_w = lmi->width; |
| 549 | int src_h = lmi->height; |
| 550 | |
| 551 | if (Squeeze_lightmap_handle == -1) { |
| 552 | memset(Lightmap_mask, 0, 128 * 128); |
| 553 | Squeeze_lightmap_handle = lm_AllocLightmap(128, 128); |
| 554 | uint16_t *fill_data = (uint16_t *)lm_data(Squeeze_lightmap_handle); |
| 555 | memset(fill_data, 0, 128 * 128 * 2); |
| 556 | } |
| 557 | |
| 558 | if (FindEmptyMaskSpot(src_w + 2, src_h + 2, &dest_x, &dest_y)) { |
| 559 | // Cool, found a spot |
| 560 | CopySqueezeDataForObject(obj, t, j, lm_data(Squeeze_lightmap_handle), dest_x, dest_y); |
| 561 | } else { |
| 562 | // Can't find an empty slot, so search through all the other remaining faces |
| 563 | for (int a = rp->objects; a != -1; a = Objects[a].next) { |
| 564 | object *obj = &Objects[a]; |
| 565 | if (obj->lighting_render_type != LRT_LIGHTMAPS) |
| 566 | continue; |
| 567 | if (!obj->lm_object.used) |
| 568 | continue; |
| 569 | |
| 570 | for (int b = 0; b < obj->lm_object.num_models; b++) { |
| 571 | if (IsNonRenderableSubmodel(&Poly_models[obj->rtype.pobj_info.model_num], b)) |
| 572 | continue; |
| 573 | |
| 574 | for (int c = 0; c < obj->lm_object.num_faces[b]; c++) { |
| 575 | lightmap_object_face *fp = &obj->lm_object.lightmap_faces[b][c]; |
| 576 | int lmi_handle = fp->lmi_handle; |
| 577 | if (lmi_handle == BAD_LMI_INDEX) |
| 578 | continue; |
| 579 | if (Lmi_spoken_for[lmi_handle]) |
| 580 | continue; |
| 581 | |
| 582 | lightmap_info *lmi = &LightmapInfo[lmi_handle]; |
| 583 | |
| 584 | int src_w = lmi->width; |
| 585 | int src_h = lmi->height; |
| 586 | |
| 587 | if (FindEmptyMaskSpot(src_w + 2, src_h + 2, &dest_x, &dest_y)) { |
| 588 | CopySqueezeDataForObject(obj, b, c, lm_data(Squeeze_lightmap_handle), dest_x, dest_y); |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | // Now, allocate a new lightmap and start over |
| 595 | ASSERT(Squeeze_lightmap_handle != -1); |
| 596 | ASSERT(GameLightmaps[Squeeze_lightmap_handle].used != 1); |
| 597 | GameLightmaps[Squeeze_lightmap_handle].used--; |
| 598 | |
| 599 | memset(Lightmap_mask, 0, 128 * 128); |
| 600 | Squeeze_lightmap_handle = lm_AllocLightmap(128, 128); |
| 601 | uint16_t *fill_data = (uint16_t *)lm_data(Squeeze_lightmap_handle); |
| 602 | memset(fill_data, 0, 128 * 128 * 2); |
| 603 | |
| 604 | ASSERT(Lmi_spoken_for[lmi_handle] == 0); |
| 605 | |
| 606 | if (FindEmptyMaskSpot(src_w + 2, src_h + 2, &dest_x, &dest_y)) { |
| 607 | CopySqueezeDataForObject(obj, t, j, lm_data(Squeeze_lightmap_handle), dest_x, dest_y); |
| 608 | } else { |
| 609 | Int3(); // Get Jason, how did this happen???? |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | if (Squeeze_lightmap_handle != -1) { |
| 618 | ASSERT(GameLightmaps[Squeeze_lightmap_handle].used != 1); |
| 619 | GameLightmaps[Squeeze_lightmap_handle].used--; |
| 620 | } |
| 621 | |
| 622 | // Squeeze all terrain object lightmaps now |
| 623 | if (external) { |
| 624 | |
| 625 | Squeeze_lightmap_handle = -1; |
| 626 | for (i = 0; i <= Highest_object_index; i++) { |
| 627 | object *obj = &Objects[i]; |
| 628 | |
| 629 | if (obj->type == OBJ_ROOM) |
| 630 | continue; |
| 631 | if (obj->lighting_render_type != LRT_LIGHTMAPS) |
| 632 | continue; |
| 633 | if (!obj->lm_object.used) |
| 634 | continue; |
| 635 | if (!OBJECT_OUTSIDE(obj)) |
| 636 | continue; |
| 637 | |
| 638 | for (t = 0; t < obj->lm_object.num_models; t++) { |
| 639 | if (IsNonRenderableSubmodel(&Poly_models[obj->rtype.pobj_info.model_num], t)) |
| 640 | continue; |
| 641 | for (k = 0; k < obj->lm_object.num_faces[t]; k++) { |
| 642 | lightmap_object_face *fp = &obj->lm_object.lightmap_faces[t][k]; |
| 643 | int lmi_handle = fp->lmi_handle; |
| 644 | if (lmi_handle == BAD_LMI_INDEX) |
| 645 | continue; |
| 646 | if (Lmi_spoken_for[lmi_handle]) |
| 647 | continue; |
| 648 | |
| 649 | lightmap_info *lmi = &LightmapInfo[lmi_handle]; |
| 650 | |
| 651 | int dest_x, dest_y; |
| 652 | int src_w = lmi->width; |
| 653 | int src_h = lmi->height; |
| 654 | |
| 655 | if (Squeeze_lightmap_handle == -1) { |
| 656 | memset(Lightmap_mask, 0, 128 * 128); |
| 657 | Squeeze_lightmap_handle = lm_AllocLightmap(128, 128); |
| 658 | uint16_t *fill_data = (uint16_t *)lm_data(Squeeze_lightmap_handle); |
| 659 | memset(fill_data, 0, 128 * 128 * 2); |
| 660 | } |
| 661 | |
| 662 | if (FindEmptyMaskSpot(src_w + 2, src_h + 2, &dest_x, &dest_y)) { |
| 663 | // Cool, found a spot |
| 664 | CopySqueezeDataForObject(obj, t, k, lm_data(Squeeze_lightmap_handle), dest_x, dest_y); |
| 665 | } else { |
| 666 | // Search through the remaining objects on the terrain |
| 667 | for (int a = i; a <= Highest_object_index; a++) { |
| 668 | object *obj = &Objects[a]; |
| 669 | |
| 670 | if (!obj->lm_object.used) |
| 671 | continue; |
| 672 | if (!OBJECT_OUTSIDE(obj)) |
| 673 | continue; |
| 674 | |
| 675 | for (int b = 0; b < obj->lm_object.num_models; b++) { |
| 676 | if (IsNonRenderableSubmodel(&Poly_models[obj->rtype.pobj_info.model_num], b)) |
| 677 | continue; |
| 678 | for (int c = 0; c < obj->lm_object.num_faces[b]; c++) { |
| 679 | lightmap_object_face *fp = &obj->lm_object.lightmap_faces[b][c]; |
| 680 | int lmi_handle = fp->lmi_handle; |
| 681 | if (lmi_handle == BAD_LMI_INDEX) |
| 682 | continue; |
| 683 | if (Lmi_spoken_for[lmi_handle]) |
| 684 | continue; |
| 685 | |
| 686 | lightmap_info *lmi = &LightmapInfo[lmi_handle]; |
| 687 | |
| 688 | int src_w = lmi->width; |
| 689 | int src_h = lmi->height; |
| 690 | |
| 691 | if (FindEmptyMaskSpot(src_w + 2, src_h + 2, &dest_x, &dest_y)) { |
| 692 | CopySqueezeDataForObject(obj, b, c, lm_data(Squeeze_lightmap_handle), dest_x, dest_y); |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | // Now, allocate a new lightmap and start over |
| 699 | ASSERT(Squeeze_lightmap_handle != -1); |
| 700 | ASSERT(GameLightmaps[Squeeze_lightmap_handle].used != 1); |
| 701 | GameLightmaps[Squeeze_lightmap_handle].used--; |
| 702 | |
| 703 | memset(Lightmap_mask, 0, 128 * 128); |
| 704 | Squeeze_lightmap_handle = lm_AllocLightmap(128, 128); |
| 705 | uint16_t *fill_data = (uint16_t *)lm_data(Squeeze_lightmap_handle); |
| 706 | memset(fill_data, 0, 128 * 128 * 2); |
| 707 | |
| 708 | ASSERT(Lmi_spoken_for[lmi_handle] == 0); |
| 709 | |
| 710 | if (FindEmptyMaskSpot(src_w + 2, src_h + 2, &dest_x, &dest_y)) { |
| 711 | CopySqueezeDataForObject(obj, t, k, lm_data(Squeeze_lightmap_handle), dest_x, dest_y); |
| 712 | } else { |
| 713 | Int3(); // Get Jason, how did this happen???? |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | if (Squeeze_lightmap_handle != -1) { |
| 721 | ASSERT(GameLightmaps[Squeeze_lightmap_handle].used != 1); |
| 722 | GameLightmaps[Squeeze_lightmap_handle].used--; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | mem_free(Lightmap_mask); |
| 727 | mem_free(Lmi_spoken_for); |
| 728 | mprintf(0, "Done squeezing lightmaps.\n"); |
| 729 | } |
| 730 | |
| 731 | void ComputeSurfaceRes(rad_surface *surf, room *rp, int facenum) { |
| 732 | int i; |
| 733 | float left = 1.1f, right = -1, top = 1.1f, bottom = -1; |
| 734 | face *fp = &rp->faces[facenum]; |
| 735 | int lw = lmi_w(fp->lmi_handle); |
| 736 | int lh = lmi_h(fp->lmi_handle); |
| 737 | |
| 738 | for (i = 0; i < fp->num_verts; i++) { |
| 739 | if (fp->face_uvls[i].u2 < left) |
| 740 | left = fp->face_uvls[i].u2; |
| 741 | if (fp->face_uvls[i].u2 > right) |
| 742 | right = fp->face_uvls[i].u2; |
| 743 | if (fp->face_uvls[i].v2 < top) |
| 744 | top = fp->face_uvls[i].v2; |
| 745 | if (fp->face_uvls[i].v2 > bottom) |
| 746 | bottom = fp->face_uvls[i].v2; |
| 747 | } |
| 748 | |
| 749 | float left_result = (left * lw) + .0001; |
| 750 | float right_result = (right * lw) + .0001; |
| 751 | float top_result = (top * lh) + .0001; |
| 752 | float bottom_result = (bottom * lh) + .0001; |
| 753 | |
| 754 | surf->x1 = floor(left_result); |
| 755 | surf->x2 = floor(right_result); |
| 756 | |
| 757 | surf->y1 = floor(top_result); |
| 758 | surf->y2 = floor(bottom_result); |
| 759 | |
| 760 | surf->xresolution = (surf->x2 - surf->x1); |
| 761 | surf->yresolution = (surf->y2 - surf->y1); |
| 762 | |
| 763 | // Adjust for a accuracy errors |
| 764 | if (((right_result) - (float)surf->x2) > .005) |
| 765 | surf->xresolution++; |
| 766 | |
| 767 | if (((bottom_result) - (float)surf->y2) > .005) |
| 768 | surf->yresolution++; |
| 769 | |
| 770 | if (((top_result) - (float)surf->y1) > .99) |
| 771 | surf->y1++; |
| 772 | |
| 773 | if (((left_result) - (float)surf->x1) > .99) |
| 774 | surf->x1++; |
| 775 | |
| 776 | ASSERT((surf->x1 + surf->xresolution) <= lw); |
| 777 | ASSERT((surf->y1 + surf->yresolution) <= lh); |
| 778 | } |
| 779 | |
| 780 | // Take the computed volume spectra of a room and save it in the room struct |
| 781 | void AssignVolumeSpectraToRoom(int roomnum) { |
| 782 | ASSERT(Rooms[roomnum].used); |
| 783 | ASSERT(!(Rooms[roomnum].flags & RF_EXTERNAL)); |
| 784 | ASSERT(!(Rooms[roomnum].flags & RF_NO_LIGHT)); |
| 785 | |
| 786 | room *rp = &Rooms[roomnum]; |
| 787 | |
| 788 | int i, t, j; |
| 789 | int w = rp->volume_width; |
| 790 | int h = rp->volume_height; |
| 791 | int d = rp->volume_depth; |
| 792 | |
| 793 | for (i = 0; i < d; i++) { |
| 794 | for (t = 0; t < h; t++) { |
| 795 | for (j = 0; j < w; j++) { |
| 796 | spectra *this_spectra = &Volume_elements[roomnum][(i * w * h) + (t * w) + j].color; |
| 797 | |
| 798 | if (this_spectra->r < 0) { |
| 799 | Int3(); // Shouldn't hit this |
| 800 | rp->volume_lights[(i * w * h) + (t * w) + j] = INVISIBLE_VOLUME_ELEMENT; |
| 801 | } else { |
| 802 | float rmax = GetMaxColor(this_spectra); |
| 803 | |
| 804 | if (rmax > 1.0 && rmax > 0.0) { |
| 805 | this_spectra->r /= rmax; |
| 806 | this_spectra->g /= rmax; |
| 807 | this_spectra->b /= rmax; |
| 808 | } |
| 809 | |
| 810 | int r = (this_spectra->r * 7); |
| 811 | r <<= 5; |
| 812 | int g = (this_spectra->g * 7); |
| 813 | g <<= 2; |
| 814 | int b = (this_spectra->b * 3); |
| 815 | |
| 816 | uint8_t volume_color = r | g | b; |
| 817 | |
| 818 | if (!UseVolumeLights) |
| 819 | rp->volume_lights[(i * w * h) + (t * w) + j] = 255; |
| 820 | else |
| 821 | rp->volume_lights[(i * w * h) + (t * w) + j] = volume_color; |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | // Returns 0 if there are bad faces in this level |
| 829 | int CheckForBadFaces(int roomnum) { |
| 830 | int i, t; |
| 831 | |
| 832 | for (i = 0; i <= Highest_room_index; i++) { |
| 833 | if (roomnum != -1 && i != roomnum) |
| 834 | continue; |
| 835 | |
| 836 | room *rp = &Rooms[i]; |
| 837 | if (!rp->used) |
| 838 | continue; |
| 839 | |
| 840 | for (t = 0; t < rp->num_faces; t++) { |
| 841 | if (rp->faces[t].num_verts < 3) |
| 842 | return 0; // Bad face detected! |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | return 1; |
| 847 | } |
| 848 | |
| 849 | // Calculates radiosity and sets lightmaps for indoor faces only |
| 850 | void DoRadiosityForRooms() { |
| 851 | int i, t, j; |
| 852 | int facecount = 0; |
| 853 | int surface_index = 0; |
| 854 | int max_index; |
| 855 | int save_after_bsp = 0; |
| 856 | |
| 857 | if (!CheckForBadFaces(-1)) { |
| 858 | OutrageMessageBox("You have bad faces in your level. Please do a Verify Level."); |
| 859 | return; |
| 860 | } |
| 861 | |
| 862 | MakeBOAVisTable(1); |
| 863 | |
| 864 | if (UseBSP) { |
| 865 | // if ((MessageBox(NULL,"Do you wish to save the level after BSP construction?","Question",MB_YESNO))==IDYES) |
| 866 | // save_after_bsp=1; |
| 867 | } |
| 868 | |
| 869 | BuildBSPTree(); |
| 870 | |
| 871 | if (save_after_bsp) { |
| 872 | char filename[_MAX_PATH]; |
| 873 | ddio_MakePath(filename, Base_directory_write, "BSPSave.D3L", NULL); |
| 874 | |
| 875 | // Save the level to |
| 876 | SaveLevel(filename); |
| 877 | } |
| 878 | |
| 879 | mprintf(0, "Setting up...\n"); |
| 880 | |
| 881 | Lightmaps_for_rad = 0; |
| 882 | |
| 883 | if (UseVolumeLights) |
| 884 | Do_volume_lighting = 1; |
| 885 | |
| 886 | ClearAllVolumeLights(); |
| 887 | ClearAllRoomLightmaps(0); |
| 888 | ClearAllObjectLightmaps(0); |
| 889 | ComputeAllRoomLightmapUVs(0); |
| 890 | |
| 891 | // Figure out memory for volume lights |
| 892 | int vw, vh, vd; |
| 893 | for (int roomnum = 0; roomnum < MAX_ROOMS; roomnum++) { |
| 894 | Volume_elements[roomnum] = NULL; |
| 895 | |
| 896 | if (Rooms[roomnum].used && !(Rooms[roomnum].flags & RF_EXTERNAL) && !(Rooms[roomnum].flags & RF_NO_LIGHT)) { |
| 897 | int num_bytes = GetVolumeSizeOfRoom(&Rooms[roomnum], &vw, &vh, &vd); |
| 898 | |
| 899 | Rooms[roomnum].volume_width = vw; |
| 900 | Rooms[roomnum].volume_height = vh; |
| 901 | Rooms[roomnum].volume_depth = vd; |
| 902 | |
| 903 | Rooms[roomnum].volume_lights = (uint8_t *)mem_malloc(vw * vh * vd); |
| 904 | ASSERT(Rooms[roomnum].volume_lights); |
| 905 | |
| 906 | Volume_elements[roomnum] = (volume_element *)mem_malloc((vw * vh * vd) * sizeof(volume_element)); |
| 907 | ASSERT(Volume_elements[roomnum]); |
| 908 | |
| 909 | // Now go through and find all the valid spectra points |
| 910 | float cur_z = Rooms[roomnum].min_xyz.z + .1; |
| 911 | |
| 912 | for (i = 0; i < vd; i++, cur_z += VOLUME_SPACING) { |
| 913 | float cur_y = Rooms[roomnum].min_xyz.y + .1; |
| 914 | for (t = 0; t < vh; t++, cur_y += VOLUME_SPACING) { |
| 915 | float cur_x = Rooms[roomnum].min_xyz.x + .1; |
| 916 | for (j = 0; j < vw; j++, cur_x += VOLUME_SPACING) { |
| 917 | vector dest_vec; |
| 918 | dest_vec.x = cur_x; |
| 919 | dest_vec.y = cur_y; |
| 920 | dest_vec.z = cur_z; |
| 921 | |
| 922 | Volume_elements[roomnum][(i * vw * vh) + (t * vw) + j].pos = dest_vec; |
| 923 | |
| 924 | if (FindPointRoom(&dest_vec) != roomnum) { |
| 925 | Volume_elements[roomnum][(i * vw * vh) + (t * vw) + j].color.r = 0; |
| 926 | Volume_elements[roomnum][(i * vw * vh) + (t * vw) + j].flags = VEF_REVERSE_SHOOT; |
| 927 | } else { |
| 928 | Volume_elements[roomnum][(i * vw * vh) + (t * vw) + j].color.r = 0; |
| 929 | Volume_elements[roomnum][(i * vw * vh) + (t * vw) + j].flags = 0; |
| 930 | } |
| 931 | } |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | // First count how many faces we'll need |
| 938 | for (i = 0; i < MAX_ROOMS; i++) { |
| 939 | if (Rooms[i].used && !(Rooms[i].flags & RF_EXTERNAL) && !(Rooms[i].flags & RF_NO_LIGHT)) { |
| 940 | facecount += Rooms[i].num_faces; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | // Setup specular lighting |
| 945 | Calculate_specular_lighting = 1; |
| 946 | SetupSpecularLighting(0); |
| 947 | |
| 948 | // Do objects |
| 949 | facecount += GetTotalObjectFaces(0); |
| 950 | // Do satellites |
| 951 | facecount += Terrain_sky.num_satellites; |
| 952 | |
| 953 | // Allocate enough memory to hold all surfaces |
| 954 | |
| 955 | Light_surfaces = (rad_surface *)mem_malloc(facecount * sizeof(rad_surface)); |
| 956 | ASSERT(Light_surfaces != NULL); |
| 957 | |
| 958 | // Set initial surface properties |
| 959 | max_index = surface_index = 0; |
| 960 | |
| 961 | for (i = 0; i < MAX_ROOMS; i++) { |
| 962 | if (Rooms[i].used) { |
| 963 | if ((Rooms[i].flags & RF_EXTERNAL) || (Rooms[i].flags & RF_NO_LIGHT)) |
| 964 | continue; |
| 965 | |
| 966 | for (t = 0; t < Rooms[i].num_faces; t++, surface_index++, max_index++) { |
| 967 | |
| 968 | ComputeSurfaceRes(&Light_surfaces[surface_index], &Rooms[i], t); |
| 969 | |
| 970 | if (Rooms[i].faces[t].num_verts) { |
| 971 | Light_surfaces[surface_index].verts = (vector *)mem_malloc(Rooms[i].faces[t].num_verts * sizeof(vector)); |
| 972 | ASSERT(Light_surfaces[surface_index].verts != NULL); |
| 973 | } else { |
| 974 | Light_surfaces[surface_index].verts = NULL; |
| 975 | mprintf(0, "Room=%d Face %d has no verts!\n", i, t); |
| 976 | } |
| 977 | |
| 978 | if (Light_surfaces[surface_index].xresolution * Light_surfaces[surface_index].yresolution) { |
| 979 | Light_surfaces[surface_index].elements = |
| 980 | (rad_element *)mem_malloc(Light_surfaces[surface_index].xresolution * |
| 981 | Light_surfaces[surface_index].yresolution * sizeof(rad_element)); |
| 982 | ASSERT(Light_surfaces[surface_index].elements != NULL); |
| 983 | } else { |
| 984 | Light_surfaces[surface_index].elements = NULL; |
| 985 | mprintf(0, "Room=%d Face %d is slivered!\n", i, t); |
| 986 | } |
| 987 | |
| 988 | Light_surfaces[surface_index].flags = 0; |
| 989 | |
| 990 | if (Rooms[i].faces[t].portal_num != -1 && |
| 991 | (((Rooms[i].portals[Rooms[i].faces[t].portal_num].flags & PF_RENDER_FACES) == 0) || |
| 992 | ((Rooms[i].portals[Rooms[i].faces[t].portal_num].flags & PF_RENDER_FACES) && |
| 993 | (GameTextures[Rooms[i].faces[t].tmap].flags & TF_TMAP2)))) |
| 994 | |
| 995 | { |
| 996 | Light_surfaces[surface_index].surface_type = ST_PORTAL; |
| 997 | Light_surfaces[surface_index].emittance.r = 0; |
| 998 | Light_surfaces[surface_index].emittance.g = 0; |
| 999 | Light_surfaces[surface_index].emittance.b = 0; |
| 1000 | } else { |
| 1001 | if (Rooms[i].faces[t].light_multiple > 200) |
| 1002 | Rooms[i].faces[t].light_multiple = 4; |
| 1003 | |
| 1004 | float mul = ((float)Rooms[i].faces[t].light_multiple) / 4.0; |
| 1005 | mul *= GlobalMultiplier * Room_multiplier[i]; |
| 1006 | |
| 1007 | Light_surfaces[surface_index].emittance.r = (float)GameTextures[Rooms[i].faces[t].tmap].r * mul; |
| 1008 | Light_surfaces[surface_index].emittance.g = (float)GameTextures[Rooms[i].faces[t].tmap].g * mul; |
| 1009 | Light_surfaces[surface_index].emittance.b = (float)GameTextures[Rooms[i].faces[t].tmap].b * mul; |
| 1010 | Light_surfaces[surface_index].surface_type = ST_ROOM; |
| 1011 | |
| 1012 | if ((GetMaxColor(&Light_surfaces[surface_index].emittance)) > .005) |
| 1013 | Light_surfaces[surface_index].flags |= SF_LIGHTSOURCE; |
| 1014 | } |
| 1015 | |
| 1016 | Light_surfaces[surface_index].normal = LightmapInfo[Rooms[i].faces[t].lmi_handle].normal; |
| 1017 | Light_surfaces[surface_index].roomnum = i; |
| 1018 | Light_surfaces[surface_index].facenum = t; |
| 1019 | |
| 1020 | if (Rooms[i].flags & RF_TOUCHES_TERRAIN) |
| 1021 | Light_surfaces[surface_index].flags |= SF_TOUCHES_TERRAIN; |
| 1022 | |
| 1023 | for (int k = 0; k < Rooms[i].num_portals; k++) { |
| 1024 | if (Rooms[i].portals[k].croom == -1 || (Rooms[Rooms[i].portals[k].croom].flags & RF_EXTERNAL)) |
| 1025 | Light_surfaces[surface_index].flags |= SF_TOUCHES_TERRAIN; |
| 1026 | } |
| 1027 | |
| 1028 | Light_surfaces[surface_index].reflectivity = GameTextures[Rooms[i].faces[t].tmap].reflectivity; |
| 1029 | |
| 1030 | // Set the vertices for each element |
| 1031 | BuildElementListForRoomFace(i, t, &Light_surfaces[surface_index]); |
| 1032 | |
| 1033 | int xres = Light_surfaces[surface_index].xresolution; |
| 1034 | int yres = Light_surfaces[surface_index].yresolution; |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | // Setup satellites |
| 1040 | for (i = 0; i < Terrain_sky.num_satellites; i++, surface_index++) { |
| 1041 | Light_surfaces[surface_index].verts = (vector *)mem_malloc(sizeof(vector) * 3); |
| 1042 | ASSERT(Light_surfaces[surface_index].verts != NULL); |
| 1043 | |
| 1044 | Light_surfaces[surface_index].elements = (rad_element *)mem_malloc(sizeof(rad_element)); |
| 1045 | ASSERT(Light_surfaces[surface_index].elements != NULL); |
| 1046 | |
| 1047 | Light_surfaces[surface_index].elements[0].verts = (vector *)mem_malloc(sizeof(vector) * 3); |
| 1048 | ASSERT(Light_surfaces[surface_index].elements[0].verts); |
| 1049 | |
| 1050 | Light_surfaces[surface_index].surface_type = ST_SATELLITE; |
| 1051 | Light_surfaces[surface_index].emittance.r = Terrain_sky.satellite_r[i]; |
| 1052 | Light_surfaces[surface_index].emittance.g = Terrain_sky.satellite_g[i]; |
| 1053 | Light_surfaces[surface_index].emittance.b = Terrain_sky.satellite_b[i]; |
| 1054 | |
| 1055 | Light_surfaces[surface_index].roomnum = i; |
| 1056 | Light_surfaces[surface_index].facenum = 0; |
| 1057 | |
| 1058 | Light_surfaces[surface_index].reflectivity = 0; |
| 1059 | Light_surfaces[surface_index].xresolution = 1; |
| 1060 | Light_surfaces[surface_index].yresolution = 1; |
| 1061 | |
| 1062 | vm_MakeZero(&Light_surfaces[surface_index].normal); |
| 1063 | |
| 1064 | Light_surfaces[surface_index].verts[0] = Terrain_sky.satellite_vectors[i]; |
| 1065 | Light_surfaces[surface_index].verts[1] = Terrain_sky.satellite_vectors[i]; |
| 1066 | Light_surfaces[surface_index].verts[2] = Terrain_sky.satellite_vectors[i]; |
| 1067 | Light_surfaces[surface_index].num_verts = 3; |
| 1068 | |
| 1069 | Light_surfaces[surface_index].elements[0].verts[0] = Terrain_sky.satellite_vectors[i]; |
| 1070 | Light_surfaces[surface_index].elements[0].verts[1] = Terrain_sky.satellite_vectors[i]; |
| 1071 | Light_surfaces[surface_index].elements[0].verts[2] = Terrain_sky.satellite_vectors[i]; |
| 1072 | Light_surfaces[surface_index].elements[0].num_verts = 3; |
| 1073 | |
| 1074 | Light_surfaces[surface_index].elements[0].flags = 0; |
| 1075 | } |
| 1076 | |
| 1077 | // Setup Objects |
| 1078 | ComputeSurfacesForObjects(surface_index, 0); |
| 1079 | |
| 1080 | mprintf(0, "This radiosity run is using %d lightmaps.\n", Lightmaps_for_rad); |
| 1081 | mprintf(0, "Solving radiosity equation (press tilde key to stop)...\n"); |
| 1082 | if (D3EditState.hemicube_radiosity) |
| 1083 | DoRadiosityRun(SM_HEMICUBE, Light_surfaces, facecount); |
| 1084 | else |
| 1085 | DoRadiosityRun(SM_RAYCAST, Light_surfaces, facecount); |
| 1086 | mprintf(0, "Done solving radiosity - cleaning up...\n"); |
| 1087 | |
| 1088 | surface_index = 0; |
| 1089 | |
| 1090 | // Assign lightap properties |
| 1091 | for (i = 0; i < MAX_ROOMS; i++) { |
| 1092 | if (Rooms[i].used) { |
| 1093 | if ((Rooms[i].flags & RF_EXTERNAL) || (Rooms[i].flags & RF_NO_LIGHT)) |
| 1094 | continue; |
| 1095 | |
| 1096 | for (t = 0; t < Rooms[i].num_faces; t++, surface_index++) { |
| 1097 | AssignRoomSurfaceToLightmap(i, t, &Light_surfaces[surface_index]); |
| 1098 | } |
| 1099 | |
| 1100 | AssignVolumeSpectraToRoom(i); |
| 1101 | mem_free(Volume_elements[i]); |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | surface_index += Terrain_sky.num_satellites; |
| 1106 | |
| 1107 | AssignLightmapsToObjectSurfaces(surface_index, 0); |
| 1108 | |
| 1109 | BlurLightmapInfos(LMI_ROOM); |
| 1110 | BlurLightmapInfos(LMI_ROOM_OBJECT); |
| 1111 | |
| 1112 | ShadeLightmapInfoEdges(LMI_ROOM); |
| 1113 | ShadeLightmapInfoEdges(LMI_ROOM_OBJECT); |
| 1114 | |
| 1115 | // Free our memory |
| 1116 | |
| 1117 | for (i = 0; i < facecount; i++) { |
| 1118 | if (Light_surfaces[i].verts) |
| 1119 | mem_free(Light_surfaces[i].verts); |
| 1120 | for (t = 0; t < Light_surfaces[i].xresolution * Light_surfaces[i].yresolution; t++) |
| 1121 | if (Light_surfaces[i].elements[t].num_verts > 0) |
| 1122 | mem_free(Light_surfaces[i].elements[t].verts); |
| 1123 | |
| 1124 | if (Light_surfaces[i].elements) |
| 1125 | mem_free(Light_surfaces[i].elements); |
| 1126 | Light_surfaces[i].elements = NULL; |
| 1127 | } |
| 1128 | mem_free(Light_surfaces); |
| 1129 | Light_surfaces = NULL; |
| 1130 | Do_volume_lighting = 0; |
| 1131 | |
| 1132 | // Free specular lighting stuff |
| 1133 | CleanupSpecularLighting(0); |
| 1134 | Calculate_specular_lighting = 0; |
| 1135 | |
| 1136 | // Figure out combined portals |
| 1137 | CheckCombinePortals(0); |
| 1138 | |
| 1139 | // Finally, squeeze the lightmaps |
| 1140 | SqueezeLightmaps(0, -1); |
| 1141 | |
| 1142 | char filename[_MAX_PATH + 1]; |
| 1143 | ddio_MakePath(filename, Base_directory_write, "LightSave.D3L", NULL); |
| 1144 | |
| 1145 | // Save the level to disk |
| 1146 | SaveLevel(filename); |
| 1147 | |
| 1148 | OutrageMessageBox("Mine radiosity complete!"); |
| 1149 | } |
| 1150 | |
| 1151 | // Calculates radiosity and sets lightmaps for indoor faces only |
| 1152 | void DoRadiosityForCurrentRoom(room *rp) { |
| 1153 | int t; |
| 1154 | int facecount = 0; |
| 1155 | int surface_index = 0; |
| 1156 | int max_index; |
| 1157 | |
| 1158 | if (!CheckForBadFaces(rp - Rooms)) { |
| 1159 | OutrageMessageBox("You have bad faces in your level. Please do a Verify Level."); |
| 1160 | return; |
| 1161 | } |
| 1162 | |
| 1163 | mprintf(0, "Setting up...\n"); |
| 1164 | |
| 1165 | ASSERT(rp != NULL); |
| 1166 | ASSERT(rp->used); |
| 1167 | |
| 1168 | if (rp->flags & RF_EXTERNAL) { |
| 1169 | OutrageMessageBox("You cannot run single room radiosity on external rooms!"); |
| 1170 | return; |
| 1171 | } |
| 1172 | |
| 1173 | if (rp->flags & RF_NO_LIGHT) { |
| 1174 | OutrageMessageBox("You cannot run single room radiosity on a room marked not to light!"); |
| 1175 | return; |
| 1176 | } |
| 1177 | |
| 1178 | // Build bsp tree |
| 1179 | BuildSingleBSPTree(rp - Rooms); |
| 1180 | |
| 1181 | ClearRoomLightmaps(rp - Rooms); |
| 1182 | for (t = 0; t <= Highest_object_index; t++) { |
| 1183 | if (Objects[t].type != OBJ_NONE && (Objects[t].roomnum == rp - Rooms)) |
| 1184 | ClearObjectLightmaps(&Objects[t]); |
| 1185 | } |
| 1186 | |
| 1187 | // Build lightmap uvs |
| 1188 | for (t = 0; t < rp->num_faces; t++) { |
| 1189 | vector verts[MAX_VERTS_PER_FACE * 5]; |
| 1190 | int room_list[2], face_list[2]; |
| 1191 | for (int k = 0; k < rp->faces[t].num_verts; k++) |
| 1192 | verts[k] = rp->verts[rp->faces[t].face_verts[k]]; |
| 1193 | |
| 1194 | room_list[0] = rp - Rooms; |
| 1195 | face_list[0] = t; |
| 1196 | |
| 1197 | BuildLightmapUVs(room_list, face_list, 1, verts, rp->faces[t].num_verts, 0); |
| 1198 | } |
| 1199 | |
| 1200 | // First count how many faces we'll need |
| 1201 | facecount += rp->num_faces; |
| 1202 | |
| 1203 | // Do objects |
| 1204 | facecount += GetTotalObjectFacesForSingleRoom(rp - Rooms); |
| 1205 | |
| 1206 | // Allocate enough memory to hold all surfaces |
| 1207 | |
| 1208 | Light_surfaces = (rad_surface *)mem_malloc(facecount * sizeof(rad_surface)); |
| 1209 | ASSERT(Light_surfaces != NULL); |
| 1210 | |
| 1211 | // Set initial surface properties |
| 1212 | max_index = surface_index = 0; |
| 1213 | |
| 1214 | for (t = 0; t < rp->num_faces; t++, surface_index++, max_index++) { |
| 1215 | ComputeSurfaceRes(&Light_surfaces[surface_index], rp, t); |
| 1216 | |
| 1217 | if (rp->faces[t].num_verts) { |
| 1218 | Light_surfaces[surface_index].verts = (vector *)mem_malloc(rp->faces[t].num_verts * sizeof(vector)); |
| 1219 | ASSERT(Light_surfaces[surface_index].verts != NULL); |
| 1220 | } else { |
| 1221 | Light_surfaces[surface_index].verts = NULL; |
| 1222 | mprintf(0, "Room=%d Face %d has no verts!\n", rp - Rooms, t); |
| 1223 | } |
| 1224 | |
| 1225 | if (Light_surfaces[surface_index].xresolution * Light_surfaces[surface_index].yresolution) { |
| 1226 | Light_surfaces[surface_index].elements = (rad_element *)mem_malloc( |
| 1227 | Light_surfaces[surface_index].xresolution * Light_surfaces[surface_index].yresolution * sizeof(rad_element)); |
| 1228 | ASSERT(Light_surfaces[surface_index].elements != NULL); |
| 1229 | } else { |
| 1230 | Light_surfaces[surface_index].elements = NULL; |
| 1231 | mprintf(0, "Room=%d Face %d is slivered!\n", rp - Rooms, t); |
| 1232 | } |
| 1233 | |
| 1234 | if (rp->faces[t].portal_num != -1 && (((rp->portals[rp->faces[t].portal_num].flags & PF_RENDER_FACES) == 0) || |
| 1235 | ((rp->portals[rp->faces[t].portal_num].flags & PF_RENDER_FACES) && |
| 1236 | (GameTextures[rp->faces[t].tmap].flags & TF_TMAP2)))) { |
| 1237 | Light_surfaces[surface_index].surface_type = ST_PORTAL; |
| 1238 | Light_surfaces[surface_index].emittance.r = 0; |
| 1239 | Light_surfaces[surface_index].emittance.g = 0; |
| 1240 | Light_surfaces[surface_index].emittance.b = 0; |
| 1241 | } else { |
| 1242 | float mul = ((float)rp->faces[t].light_multiple) / 4.0; |
| 1243 | mul *= GlobalMultiplier * Room_multiplier[rp - Rooms]; |
| 1244 | Light_surfaces[surface_index].emittance.r = (float)GameTextures[rp->faces[t].tmap].r * mul; |
| 1245 | Light_surfaces[surface_index].emittance.g = (float)GameTextures[rp->faces[t].tmap].g * mul; |
| 1246 | Light_surfaces[surface_index].emittance.b = (float)GameTextures[rp->faces[t].tmap].b * mul; |
| 1247 | Light_surfaces[surface_index].surface_type = ST_ROOM; |
| 1248 | } |
| 1249 | |
| 1250 | Light_surfaces[surface_index].normal = rp->faces[t].normal; |
| 1251 | Light_surfaces[surface_index].roomnum = ROOMNUM(rp); |
| 1252 | Light_surfaces[surface_index].facenum = t; |
| 1253 | |
| 1254 | Light_surfaces[surface_index].reflectivity = GameTextures[rp->faces[t].tmap].reflectivity; |
| 1255 | |
| 1256 | // Set the vertices for each element |
| 1257 | BuildElementListForRoomFace(rp - Rooms, t, &Light_surfaces[surface_index]); |
| 1258 | |
| 1259 | int xres = Light_surfaces[surface_index].xresolution; |
| 1260 | int yres = Light_surfaces[surface_index].yresolution; |
| 1261 | } |
| 1262 | |
| 1263 | // Setup Objects |
| 1264 | ComputeSurfacesForObjectsForSingleRoom(surface_index, rp - Rooms); |
| 1265 | |
| 1266 | mprintf(0, "Solving radiosity equation (press tilde key to stop)...\n"); |
| 1267 | if (D3EditState.hemicube_radiosity) |
| 1268 | DoRadiosityRun(SM_HEMICUBE, Light_surfaces, facecount); |
| 1269 | else |
| 1270 | DoRadiosityRun(SM_RAYCAST, Light_surfaces, facecount); |
| 1271 | mprintf(0, "Done solving radiosity - cleaning up...\n"); |
| 1272 | |
| 1273 | surface_index = 0; |
| 1274 | |
| 1275 | // Assign lightap properties |
| 1276 | for (t = 0; t < rp->num_faces; t++, surface_index++) { |
| 1277 | AssignRoomSurfaceToLightmap(rp - Rooms, t, &Light_surfaces[surface_index]); |
| 1278 | } |
| 1279 | |
| 1280 | AssignLightmapsToObjectSurfacesForSingleRoom(surface_index, rp - Rooms); |
| 1281 | |
| 1282 | // BlurLightmapInfos (LMI_ROOM); |
| 1283 | // BlurLightmapInfos (LMI_ROOM_OBJECT); |
| 1284 | |
| 1285 | // ShadeLightmapInfoEdges (LMI_ROOM); |
| 1286 | // ShadeLightmapInfoEdges (LMI_ROOM_OBJECT); |
| 1287 | |
| 1288 | // Free our memory |
| 1289 | |
| 1290 | for (int i = 0; i < facecount; i++) { |
| 1291 | mem_free(Light_surfaces[i].verts); |
| 1292 | for (t = 0; t < Light_surfaces[i].xresolution * Light_surfaces[i].yresolution; t++) |
| 1293 | if (Light_surfaces[i].elements[t].num_verts > 0) |
| 1294 | mem_free(Light_surfaces[i].elements[t].verts); |
| 1295 | |
| 1296 | mem_free(Light_surfaces[i].elements); |
| 1297 | Light_surfaces[i].elements = NULL; |
| 1298 | } |
| 1299 | mem_free(Light_surfaces); |
| 1300 | Light_surfaces = NULL; |
| 1301 | |
| 1302 | // Finally, squeeze the lightmaps |
| 1303 | SqueezeLightmaps(0, rp - Rooms); |
| 1304 | |
| 1305 | OutrageMessageBox("Room radiosity complete!"); |
| 1306 | } |
| 1307 | |
| 1308 | // Allocates and sets a lightmap based on the surface elements given |
| 1309 | void AssignRoomSurfaceToLightmap(int roomnum, int facenum, rad_surface *sp) { |
| 1310 | face *fp = &Rooms[roomnum].faces[facenum]; |
| 1311 | room *rp = &Rooms[roomnum]; |
| 1312 | |
| 1313 | int i, t, lmi_handle; |
| 1314 | int xres, yres; |
| 1315 | int lw, lh; |
| 1316 | int x1 = sp->x1; |
| 1317 | int y1 = sp->y1; |
| 1318 | int use_lightmap = 1; |
| 1319 | |
| 1320 | xres = sp->xresolution; |
| 1321 | yres = sp->yresolution; |
| 1322 | |
| 1323 | ASSERT(fp->lmi_handle != BAD_LMI_INDEX); |
| 1324 | lmi_handle = fp->lmi_handle; |
| 1325 | |
| 1326 | lw = lmi_w(lmi_handle); |
| 1327 | lh = lmi_h(lmi_handle); |
| 1328 | |
| 1329 | ASSERT((xres + x1) <= lw); |
| 1330 | ASSERT((yres + y1) <= lh); |
| 1331 | |
| 1332 | ASSERT(lw >= 2); |
| 1333 | ASSERT(lh >= 2); |
| 1334 | |
| 1335 | uint16_t *dest_data = lm_data(LightmapInfo[lmi_handle].lm_handle); |
| 1336 | |
| 1337 | // Set face pointer |
| 1338 | if (GameTextures[fp->tmap].flags & TF_ALPHA) |
| 1339 | use_lightmap = 0; |
| 1340 | if (GameTextures[fp->tmap].flags & (TF_FORCE_LIGHTMAP | TF_SATURATE_LIGHTMAP)) |
| 1341 | use_lightmap = 1; |
| 1342 | |
| 1343 | if (use_lightmap) |
| 1344 | fp->flags |= FF_LIGHTMAP; |
| 1345 | |
| 1346 | for (i = 0; i < yres; i++) { |
| 1347 | for (t = 0; t < xres; t++) { |
| 1348 | if (!(sp->elements[i * xres + t].flags & EF_IGNORE)) { |
| 1349 | ddgr_color color = GR_16_TO_COLOR(dest_data[(i + y1) * lw + (t + x1)]); |
| 1350 | int red = GR_COLOR_RED(color); |
| 1351 | int green = GR_COLOR_GREEN(color); |
| 1352 | int blue = GR_COLOR_BLUE(color); |
| 1353 | |
| 1354 | float fr, fg, fb; |
| 1355 | |
| 1356 | if (!(dest_data[(i + y1) * lw + (t + x1)] & OPAQUE_FLAG)) { |
| 1357 | red = green = blue = 0; |
| 1358 | } |
| 1359 | |
| 1360 | fr = std::min(1.0f, sp->elements[i * xres + t].exitance.r + Ambient_red + Room_ambience_r[roomnum]); |
| 1361 | fg = std::min(1.0f, sp->elements[i * xres + t].exitance.g + Ambient_green + Room_ambience_g[roomnum]); |
| 1362 | fb = std::min(1.0f, sp->elements[i * xres + t].exitance.b + Ambient_blue + Room_ambience_b[roomnum]); |
| 1363 | |
| 1364 | fr = (fr * 255) + .5; |
| 1365 | fg = (fg * 255) + .5; |
| 1366 | fb = (fb * 255) + .5; |
| 1367 | |
| 1368 | red += (int)fr; |
| 1369 | green += (int)fg; |
| 1370 | blue += (int)fb; |
| 1371 | |
| 1372 | if (dest_data[(i + y1) * lw + (t + x1)] & OPAQUE_FLAG) { |
| 1373 | |
| 1374 | red /= 2; |
| 1375 | green /= 2; |
| 1376 | blue /= 2; |
| 1377 | } |
| 1378 | |
| 1379 | red = std::min(red, 255); |
| 1380 | green = std::min(green, 255); |
| 1381 | blue = std::min(blue, 255); |
| 1382 | |
| 1383 | uint16_t texel = OPAQUE_FLAG | GR_RGB16(red, green, blue); |
| 1384 | |
| 1385 | dest_data[(i + y1) * lw + (t + x1)] = texel; |
| 1386 | } |
| 1387 | } |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | // Important - vertnum is the index into the face_verts[] array in the face structure, |
| 1392 | // not an index into the verts[] array of the room structure |
| 1393 | void BuildElementListForRoomFace(int roomnum, int facenum, rad_surface *surf) { |
| 1394 | matrix face_matrix, trans_matrix; |
| 1395 | vector fvec; |
| 1396 | vector avg_vert; |
| 1397 | vector verts[MAX_VERTS_PER_FACE * 5]; |
| 1398 | vector rot_vert; |
| 1399 | vector vert; |
| 1400 | int i, t; |
| 1401 | int xres, yres; |
| 1402 | int lmi_handle; |
| 1403 | int x1 = surf->x1; |
| 1404 | int y1 = surf->y1; |
| 1405 | |
| 1406 | xres = surf->xresolution; |
| 1407 | yres = surf->yresolution; |
| 1408 | |
| 1409 | ASSERT(Rooms[roomnum].used); |
| 1410 | ASSERT(Rooms[roomnum].faces[facenum].num_verts >= 3); |
| 1411 | ASSERT(Rooms[roomnum].faces[facenum].lmi_handle != BAD_LMI_INDEX); |
| 1412 | |
| 1413 | lmi_handle = Rooms[roomnum].faces[facenum].lmi_handle; |
| 1414 | avg_vert = ScratchCenters[lmi_handle]; |
| 1415 | |
| 1416 | // Make the orientation matrix |
| 1417 | // Reverse the normal because we're looking "at" the face, not from it |
| 1418 | fvec = -LightmapInfo[lmi_handle].normal; |
| 1419 | |
| 1420 | vm_VectorToMatrix(&face_matrix, &fvec, NULL, NULL); |
| 1421 | // Make the transformation matrix |
| 1422 | |
| 1423 | angvec avec; |
| 1424 | vm_ExtractAnglesFromMatrix(&avec, &face_matrix); |
| 1425 | vm_AnglesToMatrix(&trans_matrix, avec.p, avec.h, avec.b); |
| 1426 | |
| 1427 | // Rotate all the points |
| 1428 | for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) { |
| 1429 | vert = Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[i]]; |
| 1430 | |
| 1431 | vert -= avg_vert; |
| 1432 | vm_MatrixMulVector(&rot_vert, &vert, &trans_matrix); |
| 1433 | |
| 1434 | verts[i] = rot_vert; |
| 1435 | } |
| 1436 | |
| 1437 | // Find a base vector |
| 1438 | vector base_vector; |
| 1439 | vector xdiff, ydiff; |
| 1440 | |
| 1441 | vm_MakeZero(&xdiff); |
| 1442 | vm_MakeZero(&ydiff); |
| 1443 | |
| 1444 | // Rotate our upper left point into our 2d space |
| 1445 | vert = LightmapInfo[lmi_handle].upper_left - avg_vert; |
| 1446 | vm_MatrixMulVector(&base_vector, &vert, &trans_matrix); |
| 1447 | |
| 1448 | xdiff.x = LightmapInfo[lmi_handle].xspacing; |
| 1449 | ydiff.y = LightmapInfo[lmi_handle].yspacing; |
| 1450 | |
| 1451 | vm_TransposeMatrix(&trans_matrix); |
| 1452 | |
| 1453 | for (i = 0; i < yres; i++) { |
| 1454 | for (t = 0; t < xres; t++) { |
| 1455 | int element_index = i * xres + t; |
| 1456 | vector clip_verts[4]; |
| 1457 | |
| 1458 | rad_element *ep = &surf->elements[element_index]; |
| 1459 | |
| 1460 | // Allocate some free space |
| 1461 | clip_verts[0] = base_vector + (xdiff * (t + x1)) - (ydiff * (i + y1)); |
| 1462 | clip_verts[1] = base_vector + (xdiff * (t + x1 + 1)) - (ydiff * (i + y1)); |
| 1463 | clip_verts[2] = base_vector + (xdiff * (t + x1 + 1)) - (ydiff * (i + y1 + 1)); |
| 1464 | clip_verts[3] = base_vector + (xdiff * (t + x1)) - (ydiff * (i + y1 + 1)); |
| 1465 | ClipSurfaceElement(verts, ep, clip_verts, Rooms[roomnum].faces[facenum].num_verts); |
| 1466 | |
| 1467 | for (int k = 0; k < ep->num_verts; k++) { |
| 1468 | vm_MatrixMulVector(&rot_vert, &ep->verts[k], &trans_matrix); |
| 1469 | ep->verts[k] = rot_vert + avg_vert; |
| 1470 | } |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | if (Square_surfaces) { |
| 1475 | surf->verts[0] = base_vector; |
| 1476 | surf->verts[1] = base_vector + (xdiff * xres); |
| 1477 | surf->verts[2] = base_vector + (xdiff * xres) - (ydiff * yres); |
| 1478 | surf->verts[3] = base_vector - (ydiff * yres); |
| 1479 | |
| 1480 | for (int k = 0; k < 4; k++) { |
| 1481 | vm_MatrixMulVector(&rot_vert, &surf->verts[k], &trans_matrix); |
| 1482 | surf->verts[k] = rot_vert + avg_vert; |
| 1483 | } |
| 1484 | surf->num_verts = 4; |
| 1485 | } else { |
| 1486 | |
| 1487 | surf->num_verts = Rooms[roomnum].faces[facenum].num_verts; |
| 1488 | for (int k = 0; k < surf->num_verts; k++) { |
| 1489 | surf->verts[k] = Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[k]]; |
| 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | vector *rad_ClipTop, *rad_ClipBottom, *rad_ClipLeft, *rad_ClipRight; |
| 1495 | rad_point GlobalTempRadPoint; |
| 1496 | |
| 1497 | void SetRadClipLines(vector *tp, vector *rp, vector *bp, vector *lp) { |
| 1498 | rad_ClipTop = tp; |
| 1499 | rad_ClipRight = rp; |
| 1500 | rad_ClipBottom = bp; |
| 1501 | rad_ClipLeft = lp; |
| 1502 | } |
| 1503 | |
| 1504 | uint8_t CodeRadPoint(rad_point *rp) { |
| 1505 | uint8_t code = 0; |
| 1506 | if (rp->pos.x < rad_ClipLeft->x) |
| 1507 | code |= CC_OFF_LEFT; |
| 1508 | if (rp->pos.x > rad_ClipRight->x) |
| 1509 | code |= CC_OFF_RIGHT; |
| 1510 | if (rp->pos.y < rad_ClipBottom->y) |
| 1511 | code |= CC_OFF_BOT; |
| 1512 | if (rp->pos.y > rad_ClipTop->y) |
| 1513 | code |= CC_OFF_TOP; |
| 1514 | rp->code = code; |
| 1515 | |
| 1516 | return code; |
| 1517 | } |
| 1518 | |
| 1519 | void ClipSurfaceElement(vector *surf_verts, rad_element *ep, vector *clip_verts, int nv) { |
| 1520 | rad_point src_verts[50]; |
| 1521 | rad_point dest_verts[50]; |
| 1522 | rad_point *slist, *dlist; |
| 1523 | uint8_t and = 0xff; |
| 1524 | uint8_t or = 0; |
| 1525 | int i; |
| 1526 | |
| 1527 | ASSERT(nv < 50); |
| 1528 | |
| 1529 | ep->flags = 0; |
| 1530 | |
| 1531 | SetRadClipLines(&clip_verts[0], &clip_verts[1], &clip_verts[2], &clip_verts[3]); |
| 1532 | |
| 1533 | for (i = 0; i < nv; i++) { |
| 1534 | src_verts[i].pos = surf_verts[i]; |
| 1535 | src_verts[i].code = CodeRadPoint(&src_verts[i]); |
| 1536 | } |
| 1537 | |
| 1538 | for (i = 0; i < nv; i++) { |
| 1539 | and &= src_verts[i].code; |
| 1540 | or |= src_verts[i].code; |
| 1541 | } |
| 1542 | |
| 1543 | if (and) { |
| 1544 | // This element is not even in the face, ignore it |
| 1545 | ep->flags |= EF_IGNORE; |
| 1546 | ep->num_verts = 0; |
| 1547 | return; |
| 1548 | } |
| 1549 | |
| 1550 | int pnv = nv, nnv; |
| 1551 | slist = src_verts; |
| 1552 | dlist = dest_verts; |
| 1553 | |
| 1554 | nnv = ClipRadPointList(&slist, &dlist, &pnv, or); |
| 1555 | |
| 1556 | ep->num_verts = nnv; |
| 1557 | |
| 1558 | if (ep->num_verts == 0) |
| 1559 | ep->flags |= EF_IGNORE; |
| 1560 | else { |
| 1561 | ep->verts = (vector *)mem_malloc(sizeof(vector) * nnv); |
| 1562 | ASSERT(ep->verts); |
| 1563 | |
| 1564 | for (i = 0; i < nnv; i++) { |
| 1565 | ep->verts[i] = dlist[i].pos; |
| 1566 | } |
| 1567 | } |
| 1568 | } |
| 1569 | |
| 1570 | // Takes a points and clips all its viewpoints so they fit into the view frustum |
| 1571 | // Puts the new clipped list in dest |
| 1572 | // Returns number of points in clipped polygon |
| 1573 | |
| 1574 | int ClipRadPointList(rad_point **src, rad_point **dest, int *nv, int code) { |
| 1575 | int plane, num = 0; |
| 1576 | rad_point **save_src = src, *t; |
| 1577 | rad_point **save_dest = dest; |
| 1578 | |
| 1579 | for (plane = 1; plane < 16; plane <<= 1) { |
| 1580 | if (plane & code) // Is this point off this plane? |
| 1581 | { |
| 1582 | |
| 1583 | *nv = ClipRadToPlane(plane, *src, *dest, *nv); |
| 1584 | if (*nv == 0) |
| 1585 | return 0; |
| 1586 | ASSERT(*nv >= 3); |
| 1587 | |
| 1588 | t = *dest; |
| 1589 | *dest = *src; |
| 1590 | *src = t; |
| 1591 | num++; |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | t = *dest; |
| 1596 | *dest = *src; |
| 1597 | *src = t; |
| 1598 | |
| 1599 | return (*nv); |
| 1600 | } |
| 1601 | |
| 1602 | // Clips to a specific plane, putting resulting points in dest and returning number of points in dest |
| 1603 | int ClipRadToPlane(int plane, rad_point *src, rad_point *dest, int nv) { |
| 1604 | int i, num = 0, limit = nv - 1, ppoint, npoint; |
| 1605 | |
| 1606 | ASSERT(nv >= 3); |
| 1607 | |
| 1608 | for (i = 0; i < nv; i++) { |
| 1609 | if (i == limit) |
| 1610 | npoint = 0; |
| 1611 | else |
| 1612 | npoint = i + 1; |
| 1613 | |
| 1614 | if (i == 0) |
| 1615 | ppoint = limit; |
| 1616 | else |
| 1617 | ppoint = i - 1; |
| 1618 | |
| 1619 | // mprintf(0,"checking point %d ",i); |
| 1620 | if (src[i].code & plane) // off this plane? |
| 1621 | { |
| 1622 | |
| 1623 | if (!(src[ppoint].code & plane)) // prev point on? |
| 1624 | { |
| 1625 | // mprintf(0,"pVertex %d off %d plane.\n",i,plane); |
| 1626 | ClipRadEdge(plane, &src[ppoint], &src[i]); |
| 1627 | |
| 1628 | memcpy(&dest[num], &GlobalTempRadPoint, sizeof(rad_point)); |
| 1629 | num++; |
| 1630 | } |
| 1631 | |
| 1632 | if (!(src[npoint].code & plane)) // next point on? |
| 1633 | { |
| 1634 | |
| 1635 | // mprintf(0,"nVertex %d off %d plane.\n",i,plane); |
| 1636 | ClipRadEdge(plane, &src[npoint], &src[i]); |
| 1637 | |
| 1638 | memcpy(&dest[num], &GlobalTempRadPoint, sizeof(rad_point)); |
| 1639 | num++; |
| 1640 | } |
| 1641 | |
| 1642 | } else // This point is on |
| 1643 | { |
| 1644 | |
| 1645 | // mprintf(0,"is on\n"); |
| 1646 | memcpy(&dest[num], &src[i], sizeof(rad_point)); |
| 1647 | num++; |
| 1648 | } |
| 1649 | } |
| 1650 | return (num); |
| 1651 | } |
| 1652 | |
| 1653 | // Takes two points and a plane, and clips. |
| 1654 | void ClipRadEdge(int plane_flag, rad_point *on_pnt, rad_point *off_pnt) { |
| 1655 | if (plane_flag & CC_OFF_TOP) { |
| 1656 | float percent_on = 1.0 - ((off_pnt->pos.y - rad_ClipTop->y) / (off_pnt->pos.y - on_pnt->pos.y)); |
| 1657 | GlobalTempRadPoint.pos = on_pnt->pos + ((off_pnt->pos - on_pnt->pos) * percent_on); |
| 1658 | } |
| 1659 | |
| 1660 | if (plane_flag & CC_OFF_RIGHT) { |
| 1661 | float percent_on = 1.0 - ((off_pnt->pos.x - rad_ClipRight->x) / (off_pnt->pos.x - on_pnt->pos.x)); |
| 1662 | GlobalTempRadPoint.pos = on_pnt->pos + ((off_pnt->pos - on_pnt->pos) * percent_on); |
| 1663 | } |
| 1664 | |
| 1665 | if (plane_flag & CC_OFF_LEFT) { |
| 1666 | float percent_on = 1.0 - ((off_pnt->pos.x - rad_ClipLeft->x) / (off_pnt->pos.x - on_pnt->pos.x)); |
| 1667 | GlobalTempRadPoint.pos = on_pnt->pos + ((off_pnt->pos - on_pnt->pos) * percent_on); |
| 1668 | } |
| 1669 | |
| 1670 | if (plane_flag & CC_OFF_BOT) { |
| 1671 | float percent_on = 1.0 - ((off_pnt->pos.y - rad_ClipBottom->y) / (off_pnt->pos.y - on_pnt->pos.y)); |
| 1672 | GlobalTempRadPoint.pos = on_pnt->pos + ((off_pnt->pos - on_pnt->pos) * percent_on); |
| 1673 | } |
| 1674 | |
| 1675 | CodeRadPoint(&GlobalTempRadPoint); |
| 1676 | } |
| 1677 | |
| 1678 | // Adds color spectrums together |
| 1679 | void AddSpectra(spectra *dest, spectra *a, spectra *b) { |
| 1680 | dest->r = a->r + b->r; |
| 1681 | dest->g = a->g + b->g; |
| 1682 | dest->b = a->b + b->b; |
| 1683 | } |
| 1684 | |
| 1685 | // Returns 1 if a src vector can hit dest vector unobstructed, else 0 |
| 1686 | int ShootRayForTerrainLight(vector *src, vector *dest, int cellnum) { |
| 1687 | fvi_info hit_info; |
| 1688 | fvi_query fq; |
| 1689 | vector temp_dest = *dest; |
| 1690 | |
| 1691 | if (temp_dest.y >= MAX_TERRAIN_HEIGHT * 3) { |
| 1692 | float mag; |
| 1693 | float ydiff; |
| 1694 | vector ray = temp_dest - *src; |
| 1695 | |
| 1696 | mag = vm_GetMagnitude(&ray); |
| 1697 | ray /= mag; |
| 1698 | |
| 1699 | ydiff = ((MAX_TERRAIN_HEIGHT * 3) - src->y) / ray.y; |
| 1700 | |
| 1701 | temp_dest = *src + (ray * ydiff); |
| 1702 | } |
| 1703 | |
| 1704 | // shoot a ray from the light position to the current vertex |
| 1705 | fq.p0 = src; |
| 1706 | fq.p1 = &temp_dest; |
| 1707 | fq.startroom = MAKE_ROOMNUM(cellnum); |
| 1708 | |
| 1709 | fq.rad = 0.0f; |
| 1710 | fq.flags = FQ_CHECK_OBJS | FQ_IGNORE_NON_LIGHTMAP_OBJECTS | FQ_OBJ_BACKFACE | FQ_NO_RELINK | |
| 1711 | FQ_IGNORE_RENDER_THROUGH_PORTALS; |
| 1712 | fq.thisobjnum = -1; |
| 1713 | fq.ignore_obj_list = NULL; |
| 1714 | |
| 1715 | int fate = fvi_FindIntersection(&fq, &hit_info); |
| 1716 | |
| 1717 | if (fate == HIT_OUT_OF_TERRAIN_BOUNDS || fate == HIT_NONE) |
| 1718 | return 1; |
| 1719 | |
| 1720 | return 0; |
| 1721 | } |
| 1722 | |
| 1723 | #define AREA_X (TERRAIN_WIDTH - 1) |
| 1724 | #define AREA_Z (TERRAIN_DEPTH - 1) |
| 1725 | // #define AREA_X 128 |
| 1726 | // #define AREA_Z 128 |
| 1727 | |
| 1728 | // Puts a 3d grid of points on the terrain and sets a bit indicating if a light source |
| 1729 | // can reach that point. This is used for the dynamic lighting of objects on the terrain |
| 1730 | void DoTerrainDynamicTable() { |
| 1731 | int i, t, k, j, maxrays; |
| 1732 | int raynum = 0; |
| 1733 | int key; |
| 1734 | |
| 1735 | maxrays = AREA_X * AREA_Z * 8 * Terrain_sky.num_satellites; |
| 1736 | |
| 1737 | mprintf(0, "Calculating dynamic light table for %d points...\n", maxrays); |
| 1738 | mprintf(0, "Press tilde key to abort!\n"); |
| 1739 | |
| 1740 | memset(Terrain_dynamic_table, 0, (TERRAIN_DEPTH * TERRAIN_WIDTH)); |
| 1741 | |
| 1742 | for (i = 0; i < AREA_Z; i++) { |
| 1743 | // ddio_KeyFrame(); |
| 1744 | while ((key = ddio_KeyInKey()) != 0) { |
| 1745 | if (key == KEY_LAPOSTRO) { |
| 1746 | for (; i < AREA_Z; i++) { |
| 1747 | for (t = 0; t < AREA_X; t++) { |
| 1748 | int tseg = i * TERRAIN_WIDTH + t; |
| 1749 | Terrain_dynamic_table[tseg] = 255; |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | return; |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | for (t = 0; t < AREA_X; t++) { |
| 1758 | int tseg = i * TERRAIN_WIDTH + t; |
| 1759 | vector gp; |
| 1760 | |
| 1761 | gp.x = (t * TERRAIN_SIZE); |
| 1762 | gp.z = (i * TERRAIN_SIZE); |
| 1763 | |
| 1764 | gp.y = Terrain_seg[tseg].y; |
| 1765 | |
| 1766 | for (k = 0; k < 8; k++) { |
| 1767 | vector pos; |
| 1768 | |
| 1769 | pos.x = gp.x; |
| 1770 | pos.z = gp.z; |
| 1771 | pos.y = k * (MAX_TERRAIN_HEIGHT / 8); |
| 1772 | |
| 1773 | for (j = 0; j < Terrain_sky.num_satellites; j++) { |
| 1774 | raynum++; |
| 1775 | mprintf_at(2, 4, 0, "Ray=%d ", raynum); |
| 1776 | |
| 1777 | if (gp.y > pos.y) |
| 1778 | continue; |
| 1779 | |
| 1780 | int answer; |
| 1781 | answer = ShootRayForTerrainLight(&pos, &Terrain_sky.satellite_vectors[j], tseg); |
| 1782 | if (!answer) |
| 1783 | continue; |
| 1784 | |
| 1785 | Terrain_dynamic_table[tseg] |= (1 << k); |
| 1786 | j = Terrain_sky.num_satellites; |
| 1787 | } |
| 1788 | } |
| 1789 | } |
| 1790 | } |
| 1791 | } |
| 1792 | |
| 1793 | void ComputeTerrainSpeedTable() { |
| 1794 | int i, t, j, raynum = 0; |
| 1795 | |
| 1796 | mprintf(0, "Precomputing terrain speed table...(%d rays)\n", AREA_X * AREA_Z); |
| 1797 | for (i = 0; i < AREA_Z; i++) { |
| 1798 | for (t = 0; t < AREA_X; t++) { |
| 1799 | int tseg = i * TERRAIN_WIDTH + t; |
| 1800 | vector pos; |
| 1801 | |
| 1802 | pos.x = (t * TERRAIN_SIZE); |
| 1803 | pos.z = (i * TERRAIN_SIZE); |
| 1804 | pos.y = (Terrain_seg[tseg].y) + .001; |
| 1805 | |
| 1806 | raynum++; |
| 1807 | if ((raynum % 1000) == 0) |
| 1808 | mprintf_at(2, 4, 0, "Ray=%d ", raynum); |
| 1809 | |
| 1810 | for (j = 0; j < Terrain_sky.num_satellites; j++) |
| 1811 | TerrainLightSpeedup[j][tseg] = ShootRayForTerrainLight(&pos, &Terrain_sky.satellite_vectors[j], tseg); |
| 1812 | } |
| 1813 | } |
| 1814 | } |
| 1815 | |
| 1816 | float GetMaxColor(spectra *sp); |
| 1817 | |
| 1818 | // Calculates radiosity for the outdoor surfaces |
| 1819 | void DoRadiosityForTerrain() { |
| 1820 | int i, t, raynum = 0; |
| 1821 | int extra_surfaces = 0, total_surfaces = 0; |
| 1822 | int surf_index = 0; |
| 1823 | spectra *terrain_sums[2]; |
| 1824 | int room_surf_start, obj_surf_start; |
| 1825 | |
| 1826 | int do_dynamic = 0; |
| 1827 | int lit = 0; |
| 1828 | |
| 1829 | // First make sure there is even a light source |
| 1830 | for (i = 0; i < Terrain_sky.num_satellites; i++) { |
| 1831 | int tmap = Terrain_sky.satellite_texture[i]; |
| 1832 | if (Terrain_sky.satellite_r[i] > 0 || Terrain_sky.satellite_g[i] > 0 || Terrain_sky.satellite_b[i] > 0) |
| 1833 | lit = 1; |
| 1834 | } |
| 1835 | |
| 1836 | if (!lit) { |
| 1837 | OutrageMessageBox("No moons/suns cast light so there is no point in running terrain radiosity!"); |
| 1838 | return; |
| 1839 | } |
| 1840 | |
| 1841 | for (i = 0; i < Terrain_sky.num_satellites; i++) { |
| 1842 | TerrainLightSpeedup[i] = (uint8_t *)mem_malloc(TERRAIN_WIDTH * TERRAIN_DEPTH); |
| 1843 | ASSERT(TerrainLightSpeedup[i]); |
| 1844 | } |
| 1845 | |
| 1846 | if ((MessageBox(NULL, |
| 1847 | "Do you wish to calculate dynamic terrain lighting when radiosity is completed? (Note: Dynamic " |
| 1848 | "lighting takes a long time)", |
| 1849 | "Question", MB_YESNO)) == IDYES) |
| 1850 | do_dynamic = 1; |
| 1851 | |
| 1852 | if (!Ignore_terrain) |
| 1853 | ComputeTerrainSpeedTable(); |
| 1854 | |
| 1855 | extra_surfaces += Terrain_sky.num_satellites; |
| 1856 | extra_surfaces += GetTotalObjectFaces(1); |
| 1857 | |
| 1858 | // Get our external rooms ready |
| 1859 | ClearAllRoomLightmaps(1); |
| 1860 | ComputeAllRoomLightmapUVs(1); |
| 1861 | |
| 1862 | // count how many faces we'll need |
| 1863 | for (i = 0; i < MAX_ROOMS; i++) { |
| 1864 | if (Rooms[i].used && (Rooms[i].flags & RF_EXTERNAL)) |
| 1865 | extra_surfaces += Rooms[i].num_faces; |
| 1866 | } |
| 1867 | |
| 1868 | if (!Ignore_terrain) |
| 1869 | total_surfaces = ((AREA_X) * (AREA_Z) * 2); |
| 1870 | total_surfaces += extra_surfaces; |
| 1871 | |
| 1872 | // Get our object lightmaps ready |
| 1873 | ClearAllObjectLightmaps(1); |
| 1874 | |
| 1875 | // Allocate memory |
| 1876 | terrain_sums[0] = (spectra *)mem_malloc(TERRAIN_WIDTH * TERRAIN_DEPTH * sizeof(spectra)); |
| 1877 | terrain_sums[1] = (spectra *)mem_malloc(TERRAIN_WIDTH * TERRAIN_DEPTH * sizeof(spectra)); |
| 1878 | ASSERT(terrain_sums[0] && terrain_sums[1]); |
| 1879 | |
| 1880 | Light_surfaces = (rad_surface *)mem_malloc(total_surfaces * sizeof(rad_surface)); |
| 1881 | ASSERT(Light_surfaces != NULL); |
| 1882 | |
| 1883 | // Setup radiosity surfaces |
| 1884 | if (!Ignore_terrain) { |
| 1885 | for (i = 0; i < (AREA_X) * (AREA_Z); i++, surf_index += 2) { |
| 1886 | vector a, b, c; |
| 1887 | int x, z; |
| 1888 | |
| 1889 | x = i % AREA_X; |
| 1890 | z = i / AREA_Z; |
| 1891 | |
| 1892 | int seg = z * TERRAIN_WIDTH + x; |
| 1893 | |
| 1894 | // Do common stuff (for both triangles) |
| 1895 | for (x = 0; x < 2; x++) { |
| 1896 | Light_surfaces[i * 2 + x].surface_type = ST_TERRAIN; |
| 1897 | Light_surfaces[i * 2 + x].emittance.r = |
| 1898 | (float)GameTextures[Terrain_tex_seg[Terrain_seg[seg].texseg_index].tex_index].r; |
| 1899 | Light_surfaces[i * 2 + x].emittance.g = |
| 1900 | (float)GameTextures[Terrain_tex_seg[Terrain_seg[seg].texseg_index].tex_index].g; |
| 1901 | Light_surfaces[i * 2 + x].emittance.b = |
| 1902 | (float)GameTextures[Terrain_tex_seg[Terrain_seg[seg].texseg_index].tex_index].b; |
| 1903 | |
| 1904 | Light_surfaces[i * 2 + x].roomnum = MAKE_ROOMNUM(seg); |
| 1905 | Light_surfaces[i * 2 + x].facenum = x; |
| 1906 | |
| 1907 | Light_surfaces[i * 2 + x].reflectivity = |
| 1908 | GameTextures[Terrain_tex_seg[Terrain_seg[seg].texseg_index].tex_index].reflectivity; |
| 1909 | Light_surfaces[i * 2 + x].xresolution = 1; |
| 1910 | Light_surfaces[i * 2 + x].yresolution = 1; |
| 1911 | } |
| 1912 | |
| 1913 | // Do upper left triangle |
| 1914 | Light_surfaces[i * 2].elements = (rad_element *)mem_malloc(sizeof(rad_element)); |
| 1915 | ASSERT(Light_surfaces[i * 2].elements != NULL); |
| 1916 | |
| 1917 | Light_surfaces[i * 2].elements[0].verts = (vector *)mem_malloc(sizeof(vector) * 3); |
| 1918 | ASSERT(Light_surfaces[i * 2].elements[0].verts); |
| 1919 | |
| 1920 | Light_surfaces[i * 2].verts = (vector *)mem_malloc(sizeof(vector) * 3); |
| 1921 | ASSERT(Light_surfaces[i * 2].verts != NULL); |
| 1922 | |
| 1923 | Light_surfaces[i * 2].normal = TerrainNormals[MAX_TERRAIN_LOD - 1][seg].normal1; |
| 1924 | |
| 1925 | z = i / (AREA_X); |
| 1926 | x = i % (AREA_X); |
| 1927 | |
| 1928 | a.x = x * TERRAIN_SIZE; |
| 1929 | a.y = Terrain_seg[(z * TERRAIN_WIDTH) + (x)].y; |
| 1930 | a.z = z * TERRAIN_SIZE; |
| 1931 | |
| 1932 | b.x = x * TERRAIN_SIZE; |
| 1933 | b.y = Terrain_seg[((z + 1) * TERRAIN_WIDTH) + (x)].y; |
| 1934 | b.z = (z + 1) * TERRAIN_SIZE; |
| 1935 | |
| 1936 | c.x = (x + 1) * TERRAIN_SIZE; |
| 1937 | c.y = Terrain_seg[((z + 1) * TERRAIN_WIDTH) + (x + 1)].y; |
| 1938 | c.z = (z + 1) * TERRAIN_SIZE; |
| 1939 | |
| 1940 | Light_surfaces[i * 2].verts[0] = a; |
| 1941 | Light_surfaces[i * 2].verts[1] = b; |
| 1942 | Light_surfaces[i * 2].verts[2] = c; |
| 1943 | Light_surfaces[i * 2].num_verts = 3; |
| 1944 | |
| 1945 | Light_surfaces[i * 2].elements[0].verts[0] = a; |
| 1946 | Light_surfaces[i * 2].elements[0].verts[1] = b; |
| 1947 | Light_surfaces[i * 2].elements[0].verts[2] = c; |
| 1948 | Light_surfaces[i * 2].elements[0].num_verts = 3; |
| 1949 | |
| 1950 | Light_surfaces[i * 2].elements[0].flags = 0; |
| 1951 | |
| 1952 | // Now do lower right |
| 1953 | |
| 1954 | Light_surfaces[i * 2 + 1].elements = (rad_element *)mem_malloc(sizeof(rad_element)); |
| 1955 | ASSERT(Light_surfaces[i * 2 + 1].elements != NULL); |
| 1956 | |
| 1957 | Light_surfaces[i * 2 + 1].elements[0].verts = (vector *)mem_malloc(sizeof(vector) * 3); |
| 1958 | ASSERT(Light_surfaces[i * 2 + 1].elements[0].verts); |
| 1959 | |
| 1960 | Light_surfaces[i * 2 + 1].verts = (vector *)mem_malloc(sizeof(vector) * 3); |
| 1961 | ASSERT(Light_surfaces[i * 2 + 1].verts != NULL); |
| 1962 | |
| 1963 | Light_surfaces[i * 2 + 1].normal = TerrainNormals[MAX_TERRAIN_LOD - 1][seg].normal2; |
| 1964 | |
| 1965 | z = i / (AREA_X); |
| 1966 | x = i % (AREA_X); |
| 1967 | |
| 1968 | a.x = x * TERRAIN_SIZE; |
| 1969 | a.y = Terrain_seg[(z * TERRAIN_WIDTH) + (x)].y; |
| 1970 | a.z = z * TERRAIN_SIZE; |
| 1971 | |
| 1972 | b.x = (x + 1) * TERRAIN_SIZE; |
| 1973 | b.y = Terrain_seg[((z + 1) * TERRAIN_WIDTH) + (x + 1)].y; |
| 1974 | b.z = (z + 1) * TERRAIN_SIZE; |
| 1975 | |
| 1976 | c.x = (x + 1) * TERRAIN_SIZE; |
| 1977 | c.y = Terrain_seg[(z * TERRAIN_WIDTH) + (x + 1)].y; |
| 1978 | c.z = z * TERRAIN_SIZE; |
| 1979 | |
| 1980 | Light_surfaces[i * 2 + 1].verts[0] = a; |
| 1981 | Light_surfaces[i * 2 + 1].verts[1] = b; |
| 1982 | Light_surfaces[i * 2 + 1].verts[2] = c; |
| 1983 | Light_surfaces[i * 2 + 1].num_verts = 3; |
| 1984 | |
| 1985 | Light_surfaces[i * 2 + 1].elements[0].verts[0] = a; |
| 1986 | Light_surfaces[i * 2 + 1].elements[0].verts[1] = b; |
| 1987 | Light_surfaces[i * 2 + 1].elements[0].verts[2] = c; |
| 1988 | Light_surfaces[i * 2 + 1].elements[0].num_verts = 3; |
| 1989 | |
| 1990 | Light_surfaces[i * 2 + 1].elements[0].flags = 0; |
| 1991 | } |
| 1992 | } |
| 1993 | |
| 1994 | // Setup satellites |
| 1995 | for (i = 0; i < Terrain_sky.num_satellites; i++, surf_index++) { |
| 1996 | Light_surfaces[surf_index].verts = (vector *)mem_malloc(sizeof(vector) * 3); |
| 1997 | ASSERT(Light_surfaces[surf_index].verts != NULL); |
| 1998 | |
| 1999 | Light_surfaces[surf_index].elements = (rad_element *)mem_malloc(sizeof(rad_element)); |
| 2000 | ASSERT(Light_surfaces[surf_index].elements != NULL); |
| 2001 | |
| 2002 | Light_surfaces[surf_index].elements[0].verts = (vector *)mem_malloc(sizeof(vector) * 3); |
| 2003 | ASSERT(Light_surfaces[surf_index].elements[0].verts); |
| 2004 | |
| 2005 | Light_surfaces[surf_index].surface_type = ST_SATELLITE; |
| 2006 | Light_surfaces[surf_index].emittance.r = Terrain_sky.satellite_r[i]; |
| 2007 | Light_surfaces[surf_index].emittance.g = Terrain_sky.satellite_g[i]; |
| 2008 | Light_surfaces[surf_index].emittance.b = Terrain_sky.satellite_b[i]; |
| 2009 | |
| 2010 | Light_surfaces[surf_index].roomnum = i; |
| 2011 | Light_surfaces[surf_index].facenum = 0; |
| 2012 | |
| 2013 | Light_surfaces[surf_index].reflectivity = 0; |
| 2014 | Light_surfaces[surf_index].xresolution = 1; |
| 2015 | Light_surfaces[surf_index].yresolution = 1; |
| 2016 | |
| 2017 | vm_MakeZero(&Light_surfaces[surf_index].normal); |
| 2018 | |
| 2019 | Light_surfaces[surf_index].verts[0] = Terrain_sky.satellite_vectors[i]; |
| 2020 | Light_surfaces[surf_index].verts[1] = Terrain_sky.satellite_vectors[i]; |
| 2021 | Light_surfaces[surf_index].verts[2] = Terrain_sky.satellite_vectors[i]; |
| 2022 | Light_surfaces[surf_index].num_verts = 3; |
| 2023 | |
| 2024 | Light_surfaces[surf_index].elements[0].verts[0] = Terrain_sky.satellite_vectors[i]; |
| 2025 | Light_surfaces[surf_index].elements[0].verts[1] = Terrain_sky.satellite_vectors[i]; |
| 2026 | Light_surfaces[surf_index].elements[0].verts[2] = Terrain_sky.satellite_vectors[i]; |
| 2027 | Light_surfaces[surf_index].elements[0].num_verts = 3; |
| 2028 | |
| 2029 | Light_surfaces[surf_index].elements[0].flags = 0; |
| 2030 | } |
| 2031 | |
| 2032 | // Setup external rooms |
| 2033 | |
| 2034 | room_surf_start = surf_index; |
| 2035 | for (i = 0; i < MAX_ROOMS; i++) { |
| 2036 | if (Rooms[i].used) { |
| 2037 | if (!(Rooms[i].flags & RF_EXTERNAL)) |
| 2038 | continue; |
| 2039 | |
| 2040 | for (t = 0; t < Rooms[i].num_faces; t++, surf_index++) { |
| 2041 | |
| 2042 | ComputeSurfaceRes(&Light_surfaces[surf_index], &Rooms[i], t); |
| 2043 | |
| 2044 | Light_surfaces[surf_index].verts = (vector *)mem_malloc(Rooms[i].faces[t].num_verts * sizeof(vector)); |
| 2045 | ASSERT(Light_surfaces[surf_index].verts != NULL); |
| 2046 | |
| 2047 | Light_surfaces[surf_index].elements = (rad_element *)mem_malloc( |
| 2048 | Light_surfaces[surf_index].xresolution * Light_surfaces[surf_index].yresolution * sizeof(rad_element)); |
| 2049 | ASSERT(Light_surfaces[surf_index].elements != NULL); |
| 2050 | |
| 2051 | if (Rooms[i].faces[t].portal_num != -1 && |
| 2052 | !(Rooms[i].portals[Rooms[i].faces[t].portal_num].flags & PF_RENDER_FACES)) |
| 2053 | |
| 2054 | { |
| 2055 | Light_surfaces[surf_index].surface_type = ST_PORTAL; |
| 2056 | Light_surfaces[surf_index].emittance.r = 0; |
| 2057 | Light_surfaces[surf_index].emittance.g = 0; |
| 2058 | Light_surfaces[surf_index].emittance.b = 0; |
| 2059 | } else { |
| 2060 | float mul = ((float)Rooms[i].faces[t].light_multiple) / 4.0; |
| 2061 | mul *= GlobalMultiplier * Room_multiplier[i]; |
| 2062 | |
| 2063 | Light_surfaces[surf_index].emittance.r = (float)GameTextures[Rooms[i].faces[t].tmap].r * mul; |
| 2064 | Light_surfaces[surf_index].emittance.g = (float)GameTextures[Rooms[i].faces[t].tmap].g * mul; |
| 2065 | Light_surfaces[surf_index].emittance.b = (float)GameTextures[Rooms[i].faces[t].tmap].b * mul; |
| 2066 | Light_surfaces[surf_index].surface_type = ST_EXTERNAL_ROOM; |
| 2067 | |
| 2068 | // if ((GetMaxColor (&Light_surfaces[surf_index].emittance))>.005) |
| 2069 | // Light_surfaces[surf_index].flags|=SF_LIGHTSOURCE; |
| 2070 | } |
| 2071 | |
| 2072 | Light_surfaces[surf_index].normal = LightmapInfo[Rooms[i].faces[t].lmi_handle].normal; |
| 2073 | Light_surfaces[surf_index].roomnum = i; |
| 2074 | Light_surfaces[surf_index].facenum = t; |
| 2075 | |
| 2076 | Light_surfaces[surf_index].reflectivity = GameTextures[Rooms[i].faces[t].tmap].reflectivity; |
| 2077 | |
| 2078 | // Set the vertices for each element |
| 2079 | BuildElementListForRoomFace(i, t, &Light_surfaces[surf_index]); |
| 2080 | |
| 2081 | int xres = Light_surfaces[surf_index].xresolution; |
| 2082 | int yres = Light_surfaces[surf_index].yresolution; |
| 2083 | } |
| 2084 | } |
| 2085 | } |
| 2086 | |
| 2087 | // Setup objects |
| 2088 | obj_surf_start = surf_index; |
| 2089 | ComputeSurfacesForObjects(surf_index, 1); |
| 2090 | |
| 2091 | mprintf(0, "Solving radiosity equation (press tilde key to stop)...\n"); |
| 2092 | |
| 2093 | if (D3EditState.hemicube_radiosity) |
| 2094 | DoRadiosityRun(SM_SWITCH_AFTER_SATELLITES, Light_surfaces, total_surfaces); |
| 2095 | else |
| 2096 | DoRadiosityRun(SM_RAYCAST, Light_surfaces, total_surfaces); |
| 2097 | |
| 2098 | // Figure out lighting by averaging the two triangles per terrain cell |
| 2099 | |
| 2100 | if (!Ignore_terrain) { |
| 2101 | for (i = 0; i < AREA_X * AREA_Z; i++) { |
| 2102 | // Add in ambient terrain light |
| 2103 | terrain_sums[0][i].r = std::min(1.0f, Light_surfaces[i * 2].elements[0].exitance.r + Ambient_red); |
| 2104 | terrain_sums[0][i].g = std::min(1.0f, Light_surfaces[i * 2].elements[0].exitance.g + Ambient_green); |
| 2105 | terrain_sums[0][i].b = std::min(1.0f, Light_surfaces[i * 2].elements[0].exitance.b + Ambient_blue); |
| 2106 | |
| 2107 | terrain_sums[1][i].r = std::min(1.0f, Light_surfaces[i * 2 + 1].elements[0].exitance.r + Ambient_red); |
| 2108 | terrain_sums[1][i].g = std::min(1.0f, Light_surfaces[i * 2 + 1].elements[0].exitance.g + Ambient_green); |
| 2109 | terrain_sums[1][i].b = std::min(1.0f, Light_surfaces[i * 2 + 1].elements[0].exitance.b + Ambient_blue); |
| 2110 | } |
| 2111 | |
| 2112 | for (i = 0; i < (AREA_X) * (AREA_Z); i++) { |
| 2113 | int x, z; |
| 2114 | spectra total; |
| 2115 | int num_items = 0; |
| 2116 | int seg; |
| 2117 | |
| 2118 | memset(&total, 0, sizeof(spectra)); |
| 2119 | |
| 2120 | x = i % (AREA_X); |
| 2121 | z = i / (AREA_Z); |
| 2122 | |
| 2123 | seg = z * TERRAIN_WIDTH + x; |
| 2124 | |
| 2125 | if (x != 0) { |
| 2126 | AddSpectra(&total, &total, &terrain_sums[1][z * (AREA_X) + (x - 1)]); |
| 2127 | num_items++; |
| 2128 | } |
| 2129 | |
| 2130 | AddSpectra(&total, &total, &terrain_sums[0][z * (AREA_X) + x]); |
| 2131 | AddSpectra(&total, &total, &terrain_sums[1][z * (AREA_X) + x]); |
| 2132 | |
| 2133 | num_items += 2; |
| 2134 | |
| 2135 | if (x != 0) { |
| 2136 | if (z != 0) { |
| 2137 | AddSpectra(&total, &total, &terrain_sums[0][(z - 1) * (AREA_X) + (x - 1)]); |
| 2138 | AddSpectra(&total, &total, &terrain_sums[1][(z - 1) * (AREA_X) + (x - 1)]); |
| 2139 | num_items += 2; |
| 2140 | } |
| 2141 | } |
| 2142 | |
| 2143 | if (z != 0) { |
| 2144 | |
| 2145 | AddSpectra(&total, &total, &terrain_sums[0][(z - 1) * (AREA_X) + x]); |
| 2146 | num_items++; |
| 2147 | } |
| 2148 | |
| 2149 | total.r /= num_items; |
| 2150 | total.g /= num_items; |
| 2151 | total.b /= num_items; |
| 2152 | |
| 2153 | if (total.r > 1.0) |
| 2154 | total.r = 1.0; |
| 2155 | if (total.g > 1.0) |
| 2156 | total.g = 1.0; |
| 2157 | if (total.b > 1.0) |
| 2158 | total.b = 1.0; |
| 2159 | |
| 2160 | Terrain_seg[seg].l = Float_to_ubyte(GetMaxColor(&total)); |
| 2161 | |
| 2162 | Terrain_seg[seg].r = Float_to_ubyte(total.r); |
| 2163 | Terrain_seg[seg].g = Float_to_ubyte(total.g); |
| 2164 | Terrain_seg[seg].b = Float_to_ubyte(total.b); |
| 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | // Assign lightmaps to external rooms |
| 2169 | for (i = 0; i < MAX_ROOMS; i++) { |
| 2170 | if (Rooms[i].used) { |
| 2171 | if (!(Rooms[i].flags & RF_EXTERNAL)) |
| 2172 | continue; |
| 2173 | |
| 2174 | for (t = 0; t < Rooms[i].num_faces; t++, room_surf_start++) { |
| 2175 | AssignRoomSurfaceToLightmap(i, t, &Light_surfaces[room_surf_start]); |
| 2176 | } |
| 2177 | } |
| 2178 | } |
| 2179 | |
| 2180 | // Now assign lightmaps to objects |
| 2181 | AssignLightmapsToObjectSurfaces(obj_surf_start, 1); |
| 2182 | |
| 2183 | // Shade room/object lightmaps |
| 2184 | |
| 2185 | BlurLightmapInfos(LMI_EXTERNAL_ROOM); |
| 2186 | BlurLightmapInfos(LMI_TERRAIN_OBJECT); |
| 2187 | |
| 2188 | ShadeLightmapInfoEdges(LMI_EXTERNAL_ROOM); |
| 2189 | ShadeLightmapInfoEdges(LMI_TERRAIN_OBJECT); |
| 2190 | |
| 2191 | // Free memory |
| 2192 | |
| 2193 | for (i = 0; i < total_surfaces; i++) { |
| 2194 | mem_free(Light_surfaces[i].verts); |
| 2195 | for (int t = 0; t < Light_surfaces[i].xresolution * Light_surfaces[i].yresolution; t++) |
| 2196 | if (Light_surfaces[i].elements[t].num_verts > 0) |
| 2197 | mem_free(Light_surfaces[i].elements[t].verts); |
| 2198 | mem_free(Light_surfaces[i].elements); |
| 2199 | } |
| 2200 | |
| 2201 | mem_free(Light_surfaces); |
| 2202 | mem_free(terrain_sums[0]); |
| 2203 | mem_free(terrain_sums[1]); |
| 2204 | |
| 2205 | for (i = 0; i < Terrain_sky.num_satellites; i++) |
| 2206 | mem_free(TerrainLightSpeedup[i]); |
| 2207 | |
| 2208 | UpdateTerrainLightmaps(); |
| 2209 | SqueezeLightmaps(1, -1); |
| 2210 | // Figure out combined portals |
| 2211 | CheckCombinePortals(1); |
| 2212 | |
| 2213 | if (do_dynamic) |
| 2214 | DoTerrainDynamicTable(); |
| 2215 | |
| 2216 | OutrageMessageBox( |
| 2217 | "Terrain radiosity complete!\nIt is recommended you save the level so you won't have to rerun this operation."); |
| 2218 | } |
| 2219 | /* |
| 2220 | void DoRadiosityForTerrain () |
| 2221 | { |
| 2222 | int i,t,j,maxrays; |
| 2223 | int raynum=0; |
| 2224 | spectra *exitance; |
| 2225 | int lit=0; |
| 2226 | int do_dynamic=0; |
| 2227 | |
| 2228 | if ((MessageBox(NULL,"Do you wish to calculate dynamic terrain lighting when radiosity is completed? (Note: |
| 2229 | Dynamic lighting takes a long time)","Question",MB_YESNO))==IDYES) do_dynamic=1; |
| 2230 | |
| 2231 | // First make sure there is even a light source |
| 2232 | for (i=0;i<Terrain_sky.num_satellites;i++) |
| 2233 | { |
| 2234 | int tmap=Terrain_sky.satellite_texture[i]; |
| 2235 | if (GameTextures[tmap].r>0 || GameTextures[tmap].g || GameTextures[tmap].b) |
| 2236 | lit=1; |
| 2237 | } |
| 2238 | |
| 2239 | if (!lit) |
| 2240 | { |
| 2241 | OutrageMessageBox ("No moons/suns cast light so there is no point in running terrain radiosity!"); |
| 2242 | return; |
| 2243 | } |
| 2244 | |
| 2245 | maxrays=(TERRAIN_DEPTH-1)*(TERRAIN_WIDTH-1)*Terrain_sky.num_satellites; |
| 2246 | |
| 2247 | mprintf(0,"Calculating terrain radiosity for %d points! (be patient!)\n",maxrays); |
| 2248 | |
| 2249 | // Malloc some memory |
| 2250 | exitance=(spectra *)mem_malloc (TERRAIN_DEPTH*TERRAIN_WIDTH*sizeof(spectra)); |
| 2251 | ASSERT (exitance!=NULL); |
| 2252 | |
| 2253 | for (i=0;i<TERRAIN_DEPTH*TERRAIN_WIDTH;i++) |
| 2254 | { |
| 2255 | exitance[i].r=0; |
| 2256 | exitance[i].g=0; |
| 2257 | exitance[i].b=0; |
| 2258 | } |
| 2259 | |
| 2260 | // Now shoot a ray from every triangle on the terrain to every moon/sun in the |
| 2261 | // sky, tallying the results |
| 2262 | for (i=0;i<TERRAIN_DEPTH-1;i++) |
| 2263 | { |
| 2264 | for (t=0;t<TERRAIN_WIDTH-1;t++) |
| 2265 | { |
| 2266 | int tseg=i*TERRAIN_WIDTH+t; |
| 2267 | vector pos1,pos2; |
| 2268 | |
| 2269 | pos1.x=(t*TERRAIN_SIZE)+(TERRAIN_SIZE/3); |
| 2270 | pos1.z=(i*TERRAIN_SIZE)+(TERRAIN_SIZE * .66); |
| 2271 | pos1.y=GetTerrainGroundPoint (&pos1); |
| 2272 | pos1+=((TerrainNormals[MAX_TERRAIN_LOD-1][tseg].normal1)/16); |
| 2273 | |
| 2274 | pos2.x=(t*TERRAIN_SIZE)+(TERRAIN_SIZE * .66); |
| 2275 | pos2.z=(i*TERRAIN_SIZE)+(TERRAIN_SIZE/3); |
| 2276 | pos2.y=GetTerrainGroundPoint (&pos2); |
| 2277 | pos2+=((TerrainNormals[MAX_TERRAIN_LOD-1][tseg].normal2)/16); |
| 2278 | |
| 2279 | for (j=0;j<Terrain_sky.num_satellites;j++) |
| 2280 | { |
| 2281 | raynum++; |
| 2282 | mprintf_at(2,4,0,"Ray=%d ",raynum); |
| 2283 | |
| 2284 | int answer1,answer2; |
| 2285 | answer1=ShootRayForTerrainLight (&pos1,&Terrain_sky.satellite_vectors[j],tseg); |
| 2286 | answer2=ShootRayForTerrainLight (&pos2,&Terrain_sky.satellite_vectors[j],tseg); |
| 2287 | |
| 2288 | if (answer1==0 && answer2==0) |
| 2289 | continue; |
| 2290 | |
| 2291 | float r=exitance[tseg].r; |
| 2292 | float g=exitance[tseg].g; |
| 2293 | float b=exitance[tseg].b; |
| 2294 | int tmap=Terrain_sky.satellite_texture[j]; |
| 2295 | |
| 2296 | float light1,light2,light_avg; |
| 2297 | vector tnorm1=TerrainNormals[MAX_TERRAIN_LOD-1][tseg].normal1; |
| 2298 | vector tnorm2=TerrainNormals[MAX_TERRAIN_LOD-1][tseg].normal2; |
| 2299 | |
| 2300 | vector normal; |
| 2301 | |
| 2302 | vm_GetNormalizedDir (&normal,&Terrain_sky.satellite_vectors[j],&pos1); |
| 2303 | light1=answer1*(vm_DotProduct (&normal,&tnorm1)); |
| 2304 | if (light1<0) |
| 2305 | light1=0; |
| 2306 | |
| 2307 | vm_GetNormalizedDir (&normal,&Terrain_sky.satellite_vectors[j],&pos2); |
| 2308 | light2=answer2*(vm_DotProduct (&normal,&tnorm2)); |
| 2309 | if (light2<0) |
| 2310 | light2=0; |
| 2311 | |
| 2312 | light_avg=(light1+light2)/2; |
| 2313 | |
| 2314 | r+=(light_avg*GameTextures[tmap].r); |
| 2315 | g+=(light_avg*GameTextures[tmap].g); |
| 2316 | b+=(light_avg*GameTextures[tmap].b); |
| 2317 | |
| 2318 | exitance[tseg].r=r; |
| 2319 | exitance[tseg].g=g; |
| 2320 | exitance[tseg].b=b; |
| 2321 | } |
| 2322 | } |
| 2323 | } |
| 2324 | |
| 2325 | // Clip to 1.0 |
| 2326 | for (i=0;i<TERRAIN_DEPTH*TERRAIN_WIDTH;i++) |
| 2327 | { |
| 2328 | exitance[i].r=std::min(exitance[i].r,1.0f); |
| 2329 | exitance[i].g=std::min(exitance[i].g,1.0f); |
| 2330 | exitance[i].b=std::min(exitance[i].b,1.0f); |
| 2331 | } |
| 2332 | |
| 2333 | |
| 2334 | // Now average the surrounding cells to get a 'smooth' shadow effect |
| 2335 | for (i=0;i<TERRAIN_DEPTH;i++) |
| 2336 | { |
| 2337 | for (t=0;t<TERRAIN_WIDTH;t++) |
| 2338 | { |
| 2339 | int tseg=i*TERRAIN_WIDTH+t; |
| 2340 | spectra total={0,0,0}; |
| 2341 | int num=0; |
| 2342 | |
| 2343 | AddSpectra (&total,&total,&exitance[tseg]); |
| 2344 | num++; |
| 2345 | |
| 2346 | if (i!=0) |
| 2347 | { |
| 2348 | AddSpectra (&total,&total,&exitance[(i-1)*TERRAIN_WIDTH+t]); |
| 2349 | num++; |
| 2350 | if (t!=0) |
| 2351 | { |
| 2352 | AddSpectra (&total,&total,&exitance[(i-1)*TERRAIN_WIDTH+(t-1)]); |
| 2353 | num++; |
| 2354 | } |
| 2355 | |
| 2356 | } |
| 2357 | if (t!=0) |
| 2358 | { |
| 2359 | AddSpectra (&total,&total,&exitance[i*TERRAIN_WIDTH+(t-1)]); |
| 2360 | num++; |
| 2361 | } |
| 2362 | |
| 2363 | total.r/=num; |
| 2364 | total.g/=num; |
| 2365 | total.b/=num; |
| 2366 | |
| 2367 | Terrain_seg[tseg].r=total.r*255; |
| 2368 | Terrain_seg[tseg].g=total.g*255; |
| 2369 | Terrain_seg[tseg].b=total.b*255; |
| 2370 | Terrain_seg[tseg].l=((total.r+total.g+total.b)*255)/3; |
| 2371 | } |
| 2372 | } |
| 2373 | |
| 2374 | free (exitance); |
| 2375 | |
| 2376 | if (do_dynamic) |
| 2377 | DoTerrainDynamicTable(); |
| 2378 | |
| 2379 | UpdateTerrainLightmaps(); |
| 2380 | OutrageMessageBox ("Terrain radiosity complete!\nIt is recommended you save the level so you won't have to rerun |
| 2381 | this operation."); |
| 2382 | |
| 2383 | }*/ |
| 2384 | |
| 2385 | void BuildLightmapUVs(int *room_list, int *face_list, int count, vector *lightmap_poly, int nv, int external) { |
| 2386 | matrix face_matrix, trans_matrix; |
| 2387 | vector fvec; |
| 2388 | vector avg_vert; |
| 2389 | vector verts[MAX_VERTS_PER_FACE * 5]; |
| 2390 | vector facevert; |
| 2391 | vector rot_vert; |
| 2392 | int i, t; |
| 2393 | int lmi_handle; |
| 2394 | |
| 2395 | // find the center point of this face |
| 2396 | vm_MakeZero(&avg_vert); |
| 2397 | for (i = 0; i < nv; i++) |
| 2398 | avg_vert += lightmap_poly[i]; |
| 2399 | |
| 2400 | avg_vert /= nv; |
| 2401 | |
| 2402 | // Make the orientation matrix |
| 2403 | // Reverse the normal because we're looking "at" the face, not from it |
| 2404 | fvec = -Rooms[room_list[0]].faces[face_list[0]].normal; |
| 2405 | |
| 2406 | vm_VectorToMatrix(&face_matrix, &fvec, NULL, NULL); |
| 2407 | // Make the transformation matrix |
| 2408 | |
| 2409 | angvec avec; |
| 2410 | vm_ExtractAnglesFromMatrix(&avec, &face_matrix); |
| 2411 | vm_AnglesToMatrix(&trans_matrix, avec.p, avec.h, avec.b); |
| 2412 | |
| 2413 | // Rotate all the points |
| 2414 | for (i = 0; i < nv; i++) { |
| 2415 | vector vert = lightmap_poly[i]; |
| 2416 | |
| 2417 | vert -= avg_vert; |
| 2418 | vm_MatrixMulVector(&rot_vert, &vert, &trans_matrix); |
| 2419 | |
| 2420 | verts[i] = rot_vert; |
| 2421 | } |
| 2422 | |
| 2423 | // Find left most point |
| 2424 | int leftmost_point = -1; |
| 2425 | float leftmost_x = 900000.00f; // a big number |
| 2426 | |
| 2427 | for (i = 0; i < nv; i++) { |
| 2428 | if (verts[i].x < leftmost_x) { |
| 2429 | leftmost_point = i; |
| 2430 | leftmost_x = verts[i].x; |
| 2431 | } |
| 2432 | } |
| 2433 | |
| 2434 | ASSERT(leftmost_point != -1); |
| 2435 | |
| 2436 | // Find top most point |
| 2437 | int topmost_point = -1; |
| 2438 | float topmost_y = -900000.0f; // a big number |
| 2439 | |
| 2440 | for (i = 0; i < nv; i++) { |
| 2441 | if (verts[i].y > topmost_y) { |
| 2442 | topmost_point = i; |
| 2443 | topmost_y = verts[i].y; |
| 2444 | } |
| 2445 | } |
| 2446 | |
| 2447 | ASSERT(topmost_point != -1); |
| 2448 | |
| 2449 | // Find right most point |
| 2450 | int rightmost_point = -1; |
| 2451 | float rightmost_x = -900000.00f; // a big number |
| 2452 | |
| 2453 | for (i = 0; i < nv; i++) { |
| 2454 | if (verts[i].x > rightmost_x) { |
| 2455 | rightmost_point = i; |
| 2456 | rightmost_x = verts[i].x; |
| 2457 | } |
| 2458 | } |
| 2459 | |
| 2460 | ASSERT(rightmost_point != -1); |
| 2461 | |
| 2462 | // Find bottom most point |
| 2463 | int bottommost_point = -1; |
| 2464 | float bottommost_y = 900000.0f; // a big number |
| 2465 | |
| 2466 | for (i = 0; i < nv; i++) { |
| 2467 | if (verts[i].y < bottommost_y) { |
| 2468 | bottommost_point = i; |
| 2469 | bottommost_y = verts[i].y; |
| 2470 | } |
| 2471 | } |
| 2472 | |
| 2473 | ASSERT(bottommost_point != -1); |
| 2474 | |
| 2475 | // now set the base vertex, which is where we base uv 0,0 on |
| 2476 | |
| 2477 | vector base_vector; |
| 2478 | |
| 2479 | base_vector.x = verts[leftmost_point].x; |
| 2480 | base_vector.y = verts[topmost_point].y; |
| 2481 | base_vector.z = 0; |
| 2482 | |
| 2483 | // Figure out lightmap resolution |
| 2484 | float xdiff = verts[rightmost_point].x - verts[leftmost_point].x; |
| 2485 | float ydiff = verts[topmost_point].y - verts[bottommost_point].y; |
| 2486 | float max_diff = (float)std::max(xdiff, ydiff); |
| 2487 | |
| 2488 | int lightmap_x_res = -1, lightmap_y_res = -1; |
| 2489 | float xspacing = LightSpacing; |
| 2490 | float yspacing = LightSpacing; |
| 2491 | float spacing = LightSpacing; |
| 2492 | int res, done_spacing = 0; |
| 2493 | int xspace_int, yspace_int; |
| 2494 | |
| 2495 | // If the default spacing would make us go over our lightmap resolution |
| 2496 | // limit, then increase the spacing and try again |
| 2497 | while (!done_spacing) { |
| 2498 | res = (xdiff / xspacing); |
| 2499 | if (((xdiff / xspacing) - res) > 0) |
| 2500 | res++; |
| 2501 | |
| 2502 | res++; |
| 2503 | |
| 2504 | if (res > 126) |
| 2505 | xspacing += 1; |
| 2506 | else |
| 2507 | done_spacing = 1; |
| 2508 | } |
| 2509 | |
| 2510 | // Set a mininum, at least |
| 2511 | if (res < 2) |
| 2512 | res = 2; |
| 2513 | |
| 2514 | lightmap_x_res = res; |
| 2515 | |
| 2516 | done_spacing = 0; |
| 2517 | while (!done_spacing) { |
| 2518 | res = (ydiff / yspacing); |
| 2519 | if (((ydiff / yspacing) - res) > 0) |
| 2520 | res++; |
| 2521 | |
| 2522 | res++; |
| 2523 | |
| 2524 | if (res > 126) |
| 2525 | yspacing += 1; |
| 2526 | else |
| 2527 | done_spacing = 1; |
| 2528 | } |
| 2529 | |
| 2530 | // Set a mininum, at least |
| 2531 | if (res < 2) |
| 2532 | res = 2; |
| 2533 | |
| 2534 | lightmap_y_res = res; |
| 2535 | |
| 2536 | /*// Find power of 2 number |
| 2537 | for (i=0;i<=7;i++) |
| 2538 | { |
| 2539 | int low_num=1<i; |
| 2540 | int hi_num=2<<i; |
| 2541 | if (res<=hi_num && res>low_num) |
| 2542 | { |
| 2543 | lightmap_res=hi_num; |
| 2544 | break; |
| 2545 | } |
| 2546 | }*/ |
| 2547 | |
| 2548 | if (external) |
| 2549 | lmi_handle = AllocLightmapInfo(lightmap_x_res, lightmap_y_res, LMI_EXTERNAL_ROOM); |
| 2550 | else |
| 2551 | lmi_handle = AllocLightmapInfo(lightmap_x_res, lightmap_y_res, LMI_ROOM); |
| 2552 | |
| 2553 | ASSERT(lmi_handle != BAD_LMI_INDEX); |
| 2554 | ASSERT(lmi_handle >= 0 && lmi_handle <= MAX_LIGHTMAP_INFOS); |
| 2555 | Lightmaps_for_rad++; |
| 2556 | |
| 2557 | // Now do best fit spacing |
| 2558 | if (BestFit) { |
| 2559 | xspace_int = (xdiff / lightmap_x_res); |
| 2560 | if ((xdiff - (lightmap_x_res * xspace_int)) > 0) |
| 2561 | xspace_int++; |
| 2562 | |
| 2563 | yspace_int = (ydiff / lightmap_y_res); |
| 2564 | if ((ydiff - (lightmap_y_res * yspace_int)) > 0) |
| 2565 | yspace_int++; |
| 2566 | } else { |
| 2567 | xspace_int = xspacing; |
| 2568 | yspace_int = yspacing; |
| 2569 | } |
| 2570 | |
| 2571 | // Figure out lightmap uvs |
| 2572 | |
| 2573 | // Rotate all the face points |
| 2574 | for (i = 0; i < count; i++) { |
| 2575 | Rooms[room_list[i]].faces[face_list[i]].lmi_handle = lmi_handle; |
| 2576 | |
| 2577 | for (t = 0; t < Rooms[room_list[i]].faces[face_list[i]].num_verts; t++) { |
| 2578 | room *rp = &Rooms[room_list[i]]; |
| 2579 | face *fp = &rp->faces[face_list[i]]; |
| 2580 | vector vert = rp->verts[fp->face_verts[t]]; |
| 2581 | |
| 2582 | vert -= avg_vert; |
| 2583 | vm_MatrixMulVector(&rot_vert, &vert, &trans_matrix); |
| 2584 | |
| 2585 | facevert = rot_vert; |
| 2586 | |
| 2587 | // Find uv2s for this vertex |
| 2588 | fp->face_uvls[t].u2 = (facevert.x - verts[leftmost_point].x) / (float)(lightmap_x_res * xspace_int); |
| 2589 | fp->face_uvls[t].v2 = fabs((verts[topmost_point].y - facevert.y)) / (float)(lightmap_y_res * yspace_int); |
| 2590 | |
| 2591 | if (fp->face_uvls[t].u2 < 0) |
| 2592 | fp->face_uvls[t].u2 = 0; |
| 2593 | if (fp->face_uvls[t].v2 < 0) |
| 2594 | fp->face_uvls[t].v2 = 0; |
| 2595 | |
| 2596 | ASSERT(fp->face_uvls[t].u2 >= 0 && fp->face_uvls[t].u2 <= 1.0); |
| 2597 | ASSERT(fp->face_uvls[t].v2 >= 0 && fp->face_uvls[t].v2 <= 1.0); |
| 2598 | } |
| 2599 | } |
| 2600 | |
| 2601 | // Find upper left corner |
| 2602 | vm_TransposeMatrix(&trans_matrix); |
| 2603 | vm_MatrixMulVector(&rot_vert, &base_vector, &trans_matrix); |
| 2604 | LightmapInfo[lmi_handle].upper_left = rot_vert + avg_vert; |
| 2605 | |
| 2606 | LightmapInfo[lmi_handle].xspacing = xspace_int; |
| 2607 | LightmapInfo[lmi_handle].yspacing = yspace_int; |
| 2608 | LightmapInfo[lmi_handle].normal = Rooms[room_list[0]].faces[face_list[0]].normal; |
| 2609 | ScratchCenters[lmi_handle] = avg_vert; |
| 2610 | } |
| 2611 | |
| 2612 | #define MAX_COMBINES 50 |
| 2613 | #define LM_ADJACENT_FACE_THRESHOLD .999 |
| 2614 | uint8_t *RoomsAlreadyCombined[MAX_ROOMS]; |
| 2615 | |
| 2616 | /* |
| 2617 | // Returns number of verts in dest if face a can be safely combined with face b |
| 2618 | // Returns 0 if not |
| 2619 | int CombineLightFaces( vector *dest_verts,vector *averts, int nva, vector *norma,vector *bverts, int nvb,vector |
| 2620 | *normb,int aroom,int broom) |
| 2621 | { |
| 2622 | int starta, startb, i; |
| 2623 | vector va; |
| 2624 | float dp; |
| 2625 | |
| 2626 | dp=vm_DotProduct (normb,norma); |
| 2627 | |
| 2628 | if (dp < LM_ADJACENT_FACE_THRESHOLD) |
| 2629 | return 0; |
| 2630 | |
| 2631 | ASSERT (nva > 2 ); |
| 2632 | ASSERT (nvb > 2 ); |
| 2633 | |
| 2634 | // Go through each vertex and get a match |
| 2635 | |
| 2636 | for (starta=0; starta<nva; starta++ ) |
| 2637 | { |
| 2638 | int nexta=(starta+1)%nva; |
| 2639 | |
| 2640 | for (startb=0; startb<nvb; startb++ ) |
| 2641 | { |
| 2642 | int nextb=(startb+1)%nvb; |
| 2643 | |
| 2644 | if ((PointsAreSame (&averts[starta],&bverts[nextb])) && |
| 2645 | (PointsAreSame(&averts[nexta],&bverts[startb]))) |
| 2646 | { |
| 2647 | //MATCH!!!!!!!! |
| 2648 | |
| 2649 | int dnv = 0; |
| 2650 | |
| 2651 | for (i=1; i<nva; i++ ) |
| 2652 | { |
| 2653 | ASSERT(dnv < MAX_VERTS_PER_FACE*5); |
| 2654 | dest_verts[dnv] = averts[(starta+i)%nva]; |
| 2655 | va = dest_verts[dnv]; |
| 2656 | dnv++; |
| 2657 | } |
| 2658 | |
| 2659 | if ( (PointsAreSame(&va,&bverts[(startb+2)%nvb]))) |
| 2660 | mprintf(0, "WARNING!!! Faces were combined that caused the loss of a |
| 2661 | vertex!\n"); |
| 2662 | |
| 2663 | for (i=1; i<nvb; i++ ) |
| 2664 | { |
| 2665 | ASSERT(dnv < MAX_VERTS_PER_FACE*5 ); |
| 2666 | if ( (i==1) && (PointsAreSame(&va,&bverts[(startb+i+1)%nvb]))) |
| 2667 | continue; |
| 2668 | else if ( (i==2) && (PointsAreSame(&va,&bverts[(startb+i)%nvb]))) |
| 2669 | continue; |
| 2670 | else |
| 2671 | { |
| 2672 | dest_verts[dnv] = bverts[(startb+i)%nvb]; |
| 2673 | dnv++; |
| 2674 | |
| 2675 | } |
| 2676 | } |
| 2677 | ASSERT( dnv > 2 ); |
| 2678 | |
| 2679 | return dnv; |
| 2680 | } |
| 2681 | } |
| 2682 | } |
| 2683 | |
| 2684 | Now check for tjoint faces |
| 2685 | for (starta=0; starta<nva; starta++) |
| 2686 | { |
| 2687 | int nexta=(starta+1)%nva; |
| 2688 | |
| 2689 | for (startb=0; startb<nvb; startb++) |
| 2690 | { |
| 2691 | vector line_a=averts[nexta]-averts[starta]; |
| 2692 | |
| 2693 | |
| 2694 | |
| 2695 | } |
| 2696 | } |
| 2697 | |
| 2698 | // If these two faces are in the same room and are on the same plane |
| 2699 | // then we should combine them by default |
| 2700 | |
| 2701 | if (aroom!=-1 && aroom==broom) |
| 2702 | { |
| 2703 | int dnv = 0; |
| 2704 | int match=0; |
| 2705 | |
| 2706 | // Test to see if any points at all can touch |
| 2707 | |
| 2708 | for (i=0; i<nva && !match; i++ ) |
| 2709 | { |
| 2710 | for (int t=0; t<nvb && !match; t++ ) |
| 2711 | { |
| 2712 | if (PointsAreSame(&averts[i],&bverts[t])) |
| 2713 | match=1; |
| 2714 | } |
| 2715 | } |
| 2716 | |
| 2717 | if (!match) |
| 2718 | return 0; |
| 2719 | |
| 2720 | // At least one point is the same, so combine them! |
| 2721 | for (i=0; i<nva; i++ ) |
| 2722 | { |
| 2723 | dest_verts[dnv] = averts[i]; |
| 2724 | dnv++; |
| 2725 | ASSERT(dnv < MAX_VERTS_PER_FACE * 5); |
| 2726 | } |
| 2727 | |
| 2728 | for (i=0; i<nvb; i++ ) |
| 2729 | { |
| 2730 | dest_verts[dnv] = bverts[i]; |
| 2731 | dnv++; |
| 2732 | ASSERT(dnv < MAX_VERTS_PER_FACE * 5); |
| 2733 | } |
| 2734 | |
| 2735 | return dnv; |
| 2736 | } |
| 2737 | |
| 2738 | return 0; |
| 2739 | }*/ |
| 2740 | |
| 2741 | // Returns number of verts in dest if face a can be safely combined with face b |
| 2742 | // Returns 0 if not |
| 2743 | int CombineLightFaces(vector *dest_verts, vector *averts, int nva, vector *norma, vector *bverts, int nvb, |
| 2744 | vector *normb, int aroom, int broom) { |
| 2745 | int i; |
| 2746 | |
| 2747 | float dp; |
| 2748 | |
| 2749 | dp = vm_DotProduct(normb, norma); |
| 2750 | |
| 2751 | if (dp < LM_ADJACENT_FACE_THRESHOLD) |
| 2752 | return 0; |
| 2753 | |
| 2754 | ASSERT(nva > 2); |
| 2755 | ASSERT(nvb > 2); |
| 2756 | |
| 2757 | // Go through each vertex and get a match |
| 2758 | |
| 2759 | // If these two faces are in the same room and are on the same plane |
| 2760 | // then we should combine them by default |
| 2761 | |
| 2762 | if (1 || (aroom != -1 && aroom == broom)) { |
| 2763 | int dnv = 0; |
| 2764 | int match = 0; |
| 2765 | |
| 2766 | // Don't go over our point limit |
| 2767 | if ((nva + nvb) >= (MAX_VERTS_PER_FACE * 5)) |
| 2768 | return 0; |
| 2769 | |
| 2770 | // Test to see if any points at all can touch |
| 2771 | |
| 2772 | for (i = 0; i < nva && !match; i++) { |
| 2773 | for (int t = 0; t < nvb && !match; t++) { |
| 2774 | if (PointsAreSame(&averts[i], &bverts[t])) |
| 2775 | match = 1; |
| 2776 | } |
| 2777 | } |
| 2778 | |
| 2779 | if (!match) |
| 2780 | return 0; |
| 2781 | |
| 2782 | // At least one point is the same, so combine them! |
| 2783 | for (i = 0; i < nva; i++) { |
| 2784 | dest_verts[dnv] = averts[i]; |
| 2785 | dnv++; |
| 2786 | ASSERT(dnv < MAX_VERTS_PER_FACE * 5); |
| 2787 | } |
| 2788 | |
| 2789 | for (i = 0; i < nvb; i++) { |
| 2790 | dest_verts[dnv] = bverts[i]; |
| 2791 | dnv++; |
| 2792 | ASSERT(dnv < MAX_VERTS_PER_FACE * 5); |
| 2793 | } |
| 2794 | |
| 2795 | return dnv; |
| 2796 | } |
| 2797 | |
| 2798 | return 0; |
| 2799 | } |
| 2800 | |
| 2801 | // Given a roomnumber and a face, goes through the entire mine and checks to see |
| 2802 | // if this face can share a lightmap with any other face |
| 2803 | int TestLightAdjacency(int roomnum, int facenum, int external) { |
| 2804 | |
| 2805 | int i, t, k; |
| 2806 | room *arp = &Rooms[roomnum]; |
| 2807 | face *afp = &arp->faces[facenum]; |
| 2808 | |
| 2809 | vector averts[MAX_VERTS_PER_FACE * 5]; |
| 2810 | vector bverts[MAX_VERTS_PER_FACE * 5]; |
| 2811 | vector dest_verts[MAX_VERTS_PER_FACE * 5]; |
| 2812 | |
| 2813 | int face_combine_list[MAX_COMBINES]; |
| 2814 | int room_combine_list[MAX_COMBINES]; |
| 2815 | |
| 2816 | ASSERT(roomnum >= 0 && roomnum < MAX_ROOMS); |
| 2817 | |
| 2818 | if (!AllowCombining) |
| 2819 | return 0; |
| 2820 | |
| 2821 | // if (GameTextures[afp->tmap].r>0 || GameTextures[afp->tmap].g>0 || GameTextures[afp->tmap].b>0) |
| 2822 | // return 0; |
| 2823 | |
| 2824 | // Don't combine portals! |
| 2825 | if (afp->portal_num != -1) |
| 2826 | return 0; |
| 2827 | |
| 2828 | // Setup our 'base' face |
| 2829 | |
| 2830 | int anv = afp->num_verts; |
| 2831 | int total_faces = 1; |
| 2832 | |
| 2833 | room_combine_list[0] = roomnum; |
| 2834 | face_combine_list[0] = facenum; |
| 2835 | |
| 2836 | for (i = 0; i < afp->num_verts; i++) |
| 2837 | averts[i] = arp->verts[afp->face_verts[i]]; |
| 2838 | |
| 2839 | StartOver: |
| 2840 | |
| 2841 | // Go through each room and find an adjacent face |
| 2842 | for (i = roomnum; i < MAX_ROOMS; i++) { |
| 2843 | if (!Rooms[i].used) |
| 2844 | continue; |
| 2845 | |
| 2846 | if (Rooms[i].flags & RF_NO_LIGHT) |
| 2847 | continue; |
| 2848 | |
| 2849 | if (external && !(Rooms[i].flags & RF_EXTERNAL)) |
| 2850 | continue; |
| 2851 | |
| 2852 | if (!external && (Rooms[i].flags & RF_EXTERNAL)) |
| 2853 | continue; |
| 2854 | |
| 2855 | /*if (i!=roomnum) // Only combine faces in the same room |
| 2856 | continue;*/ |
| 2857 | |
| 2858 | for (t = 0; t < Rooms[i].num_faces; t++) { |
| 2859 | if (total_faces >= MAX_COMBINES - 1) |
| 2860 | continue; |
| 2861 | |
| 2862 | if (&Rooms[i] == arp && t == facenum) |
| 2863 | continue; // don't do self |
| 2864 | |
| 2865 | // Don't do if already spoken fore |
| 2866 | if (RoomsAlreadyCombined[i][t]) |
| 2867 | continue; |
| 2868 | |
| 2869 | room *brp = &Rooms[i]; |
| 2870 | face *bfp = &brp->faces[t]; |
| 2871 | |
| 2872 | // Don't do combine light sources |
| 2873 | // if (GameTextures[bfp->tmap].r>0 || GameTextures[bfp->tmap].g>0 || GameTextures[bfp->tmap].b>0) |
| 2874 | // continue; |
| 2875 | // Don't combine portals! |
| 2876 | if (bfp->portal_num != -1) |
| 2877 | continue; |
| 2878 | |
| 2879 | for (k = 0; k < bfp->num_verts; k++) |
| 2880 | bverts[k] = brp->verts[bfp->face_verts[k]]; |
| 2881 | |
| 2882 | int nv = |
| 2883 | CombineLightFaces(dest_verts, averts, anv, &afp->normal, bverts, bfp->num_verts, &bfp->normal, roomnum, i); |
| 2884 | |
| 2885 | // We have a combine! Mark this face in the appropriate list |
| 2886 | // And update our new polygon |
| 2887 | if (nv > 0) { |
| 2888 | room_combine_list[total_faces] = i; |
| 2889 | face_combine_list[total_faces] = t; |
| 2890 | total_faces++; |
| 2891 | |
| 2892 | RoomsAlreadyCombined[roomnum][facenum] = 1; |
| 2893 | RoomsAlreadyCombined[i][t] = 1; |
| 2894 | for (k = 0; k < nv; k++) |
| 2895 | averts[k] = dest_verts[k]; |
| 2896 | |
| 2897 | anv = nv; |
| 2898 | |
| 2899 | goto StartOver; |
| 2900 | } |
| 2901 | } |
| 2902 | } |
| 2903 | |
| 2904 | // Now build 1 lightmap to be shared across all the faces that were combined |
| 2905 | if (total_faces > 1) { |
| 2906 | BuildLightmapUVs(room_combine_list, face_combine_list, total_faces, averts, anv, external); |
| 2907 | } |
| 2908 | |
| 2909 | return 1; |
| 2910 | } |
| 2911 | |
| 2912 | // Computes the the mines UVs |
| 2913 | // Faces can now share one lightmap, so this routine goes through and tries to |
| 2914 | // combine as many faces as it can into one lightmap |
| 2915 | void ComputeAllRoomLightmapUVs(int external) { |
| 2916 | int i, t, k; |
| 2917 | int not_combined = 0; |
| 2918 | |
| 2919 | for (i = 0; i < MAX_ROOMS; i++) { |
| 2920 | if (Rooms[i].used) { |
| 2921 | if (Rooms[i].flags & RF_NO_LIGHT) |
| 2922 | continue; |
| 2923 | |
| 2924 | if (external && !(Rooms[i].flags & RF_EXTERNAL)) |
| 2925 | continue; |
| 2926 | |
| 2927 | if (!external && (Rooms[i].flags & RF_EXTERNAL)) |
| 2928 | continue; |
| 2929 | |
| 2930 | RoomsAlreadyCombined[i] = (uint8_t *)mem_malloc(Rooms[i].num_faces); |
| 2931 | ASSERT(RoomsAlreadyCombined[i]); |
| 2932 | for (k = 0; k < Rooms[i].num_faces; k++) |
| 2933 | RoomsAlreadyCombined[i][k] = 0; |
| 2934 | } |
| 2935 | } |
| 2936 | |
| 2937 | for (i = 0; i < MAX_ROOMS; i++) { |
| 2938 | if (Rooms[i].used) { |
| 2939 | if (Rooms[i].flags & RF_NO_LIGHT) |
| 2940 | continue; |
| 2941 | |
| 2942 | if (external && !(Rooms[i].flags & RF_EXTERNAL)) |
| 2943 | continue; |
| 2944 | |
| 2945 | if (!external && (Rooms[i].flags & RF_EXTERNAL)) |
| 2946 | continue; |
| 2947 | |
| 2948 | for (t = 0; t < Rooms[i].num_faces; t++) { |
| 2949 | if (*(RoomsAlreadyCombined[i] + t) == 0) |
| 2950 | TestLightAdjacency(i, t, external); |
| 2951 | } |
| 2952 | } |
| 2953 | } |
| 2954 | |
| 2955 | // Now build lightmaps for any faces that couldn't be combined |
| 2956 | for (i = 0; i < MAX_ROOMS; i++) { |
| 2957 | if (Rooms[i].used) { |
| 2958 | if (Rooms[i].flags & RF_NO_LIGHT) |
| 2959 | continue; |
| 2960 | |
| 2961 | if (external && !(Rooms[i].flags & RF_EXTERNAL)) |
| 2962 | continue; |
| 2963 | |
| 2964 | if (!external && (Rooms[i].flags & RF_EXTERNAL)) |
| 2965 | continue; |
| 2966 | |
| 2967 | for (t = 0; t < Rooms[i].num_faces; t++) { |
| 2968 | if (!RoomsAlreadyCombined[i][t]) { |
| 2969 | vector verts[MAX_VERTS_PER_FACE * 5]; |
| 2970 | int room_list[2], face_list[2]; |
| 2971 | for (k = 0; k < Rooms[i].faces[t].num_verts; k++) |
| 2972 | verts[k] = Rooms[i].verts[Rooms[i].faces[t].face_verts[k]]; |
| 2973 | |
| 2974 | room_list[0] = i; |
| 2975 | face_list[0] = t; |
| 2976 | |
| 2977 | BuildLightmapUVs(room_list, face_list, 1, verts, Rooms[i].faces[t].num_verts, external); |
| 2978 | not_combined++; |
| 2979 | } |
| 2980 | } |
| 2981 | } |
| 2982 | } |
| 2983 | |
| 2984 | mprintf(0, "%d faces couldn't be combined!\n", not_combined); |
| 2985 | |
| 2986 | // Free memory |
| 2987 | for (i = 0; i < MAX_ROOMS; i++) { |
| 2988 | if (Rooms[i].used) { |
| 2989 | if (Rooms[i].flags & RF_NO_LIGHT) |
| 2990 | continue; |
| 2991 | |
| 2992 | if (external && !(Rooms[i].flags & RF_EXTERNAL)) |
| 2993 | continue; |
| 2994 | |
| 2995 | if (!external && (Rooms[i].flags & RF_EXTERNAL)) |
| 2996 | continue; |
| 2997 | |
| 2998 | mem_free(RoomsAlreadyCombined[i]); |
| 2999 | } |
| 3000 | } |
| 3001 | } |
| 3002 | |
| 3003 | // Returns number preceding val modulo modulus. |
| 3004 | // prevmod(3,4) = 2 |
| 3005 | // prevmod(0,4) = 3 |
| 3006 | int SpecularPrevIndex(int val, int modulus) { |
| 3007 | if (val > 0) |
| 3008 | return val - 1; |
| 3009 | else |
| 3010 | return modulus - 1; |
| 3011 | } |
| 3012 | |
| 3013 | // Returns number succeeding val modulo modulus. |
| 3014 | // succmod(3,4) = 0 |
| 3015 | // succmod(0,4) = 1 |
| 3016 | int SpecularNextIndex(int val, int modulus) { |
| 3017 | if (val < modulus - 1) |
| 3018 | return val + 1; |
| 3019 | else |
| 3020 | return 0; |
| 3021 | } |
| 3022 | |
| 3023 | // Gets the left top, left bottom, right top, and right bottom indices. Also finds |
| 3024 | // lowest y index. |
| 3025 | void GetSpecularVertexOrdering(spec_vertex *verts, int nv, int *vlt, int *vlb, int *vrt, int *vrb, int *bottom_y_ind) { |
| 3026 | int i; |
| 3027 | float min_y, max_y; |
| 3028 | int min_y_ind; |
| 3029 | int original_vrt; |
| 3030 | float min_x; |
| 3031 | |
| 3032 | // Scan all vertices, set min_y_ind to vertex with smallest y coordinate. |
| 3033 | min_y = verts[0].y; |
| 3034 | max_y = min_y; |
| 3035 | min_y_ind = 0; |
| 3036 | min_x = verts[0].x; |
| 3037 | *bottom_y_ind = 0; |
| 3038 | |
| 3039 | for (i = 1; i < nv; i++) { |
| 3040 | if (verts[i].y < min_y) { |
| 3041 | min_y = verts[i].y; |
| 3042 | min_y_ind = i; |
| 3043 | min_x = verts[i].x; |
| 3044 | } else if (verts[i].y == min_y) { |
| 3045 | if (verts[i].x < min_x) { |
| 3046 | min_y_ind = i; |
| 3047 | min_x = verts[i].x; |
| 3048 | } |
| 3049 | } |
| 3050 | if (verts[i].y > max_y) { |
| 3051 | max_y = verts[i].y; |
| 3052 | *bottom_y_ind = i; |
| 3053 | } |
| 3054 | } |
| 3055 | |
| 3056 | // Set "vertex left top", etc. based on vertex with topmost y coordinate |
| 3057 | *vlt = min_y_ind; |
| 3058 | *vrt = *vlt; |
| 3059 | *vlb = SpecularPrevIndex(*vlt, nv); |
| 3060 | *vrb = SpecularNextIndex(*vrt, nv); |
| 3061 | |
| 3062 | // If right edge is horizontal, then advance along polygon bound until it no longer is or until all |
| 3063 | // vertices have been examined. |
| 3064 | // (Left edge cannot be horizontal, because *vlt is set to leftmost point with highest y coordinate.) |
| 3065 | |
| 3066 | original_vrt = *vrt; |
| 3067 | |
| 3068 | while (verts[*vrt].y == verts[*vrb].y) { |
| 3069 | if (SpecularNextIndex(*vrt, nv) == original_vrt) |
| 3070 | break; |
| 3071 | |
| 3072 | *vrt = SpecularNextIndex(*vrt, nv); |
| 3073 | *vrb = SpecularNextIndex(*vrt, nv); |
| 3074 | } |
| 3075 | } |
| 3076 | |
| 3077 | /* |
| 3078 | // Creates a map that contains all the interpolated normals for a particular face |
| 3079 | // Used in specular mapping |
| 3080 | void CreateNormalMapForFace (room *rp,face *fp) |
| 3081 | { |
| 3082 | int w=lmi_w(fp->lmi_handle); |
| 3083 | int h=lmi_h(fp->lmi_handle); |
| 3084 | special_face *sfp=&SpecialFaces[fp->special_handle]; |
| 3085 | spec_vertex spec_verts[MAX_VERTS_PER_FACE]; |
| 3086 | vector vertnorms[MAX_VERTS_PER_FACE]; |
| 3087 | int vlt,vlb,vrt,vrb,max_y_vertex,top_y,bottom_y,height; |
| 3088 | int no_height=0,no_width=0,no_right=0,no_left=0; |
| 3089 | lightmap_info *lmi_ptr=&LightmapInfo[fp->lmi_handle]; |
| 3090 | uint16_t *src_data=lm_data(lmi_ptr->lm_handle); |
| 3091 | |
| 3092 | matrix facematrix; |
| 3093 | vector fvec=-lmi_ptr->normal; |
| 3094 | vm_VectorToMatrix(&facematrix,&fvec,NULL,NULL); |
| 3095 | |
| 3096 | sfp->normal_map=(uint8_t *)mem_malloc (w*h*3); |
| 3097 | ASSERT (sfp->normal_map); |
| 3098 | |
| 3099 | for (int i=0;i<(w*h);i++) |
| 3100 | { |
| 3101 | sfp->normal_map[i*3+0]=0; |
| 3102 | sfp->normal_map[i*3+1]=0; |
| 3103 | sfp->normal_map[i*3+2]=128; |
| 3104 | } |
| 3105 | |
| 3106 | for (i=0;i<fp->num_verts;i++) |
| 3107 | { |
| 3108 | vm_MatrixMulVector (&vertnorms[i],&sfp->vertnorms[i],&facematrix); |
| 3109 | spec_verts[i].x=fp->face_uvls[i].u2; |
| 3110 | spec_verts[i].y=fp->face_uvls[i].v2; |
| 3111 | } |
| 3112 | |
| 3113 | GetSpecularVertexOrdering(spec_verts,fp->num_verts,&vlt,&vlb,&vrt,&vrb,&max_y_vertex); |
| 3114 | |
| 3115 | top_y=spec_verts[vlt].y*h; |
| 3116 | bottom_y=spec_verts[max_y_vertex].y*h; |
| 3117 | |
| 3118 | height = bottom_y-top_y; |
| 3119 | if(height == 0) |
| 3120 | no_height=1; |
| 3121 | |
| 3122 | // Setup left interpolant |
| 3123 | int left_height=((spec_verts[vlb].y-spec_verts[vlt].y)*(float)h); |
| 3124 | |
| 3125 | float left_x=spec_verts[vlt].x*w; |
| 3126 | float delta_left_x=((spec_verts[vlb].x-spec_verts[vlt].x)*w)/left_height; |
| 3127 | vector left_vnorm=vertnorms[vlt]; |
| 3128 | vector delta_left_vnorm=(vertnorms[vlb]-vertnorms[vlt])/left_height; |
| 3129 | int next_break_left = spec_verts[vlb].y*h; |
| 3130 | |
| 3131 | // Setup right interpolant |
| 3132 | |
| 3133 | int right_height=((spec_verts[vrb].y-spec_verts[vrt].y)*(float)h); |
| 3134 | |
| 3135 | float right_x=spec_verts[vrt].x*w; |
| 3136 | float delta_right_x=((spec_verts[vrb].x-spec_verts[vrt].x)*w)/right_height; |
| 3137 | vector right_vnorm=vertnorms[vrt]; |
| 3138 | vector delta_right_vnorm=(vertnorms[vrb]-vertnorms[vrt])/right_height; |
| 3139 | int next_break_right = spec_verts[vrb].y*h; |
| 3140 | |
| 3141 | // Do the loop |
| 3142 | for (int y=top_y;y<=bottom_y;y++) |
| 3143 | { |
| 3144 | if (y==next_break_left && !no_height) |
| 3145 | { |
| 3146 | while (y == (int)(spec_verts[vlb].y*h)) |
| 3147 | { |
| 3148 | vlt = vlb; |
| 3149 | vlb = SpecularPrevIndex(vlb,fp->num_verts); |
| 3150 | } |
| 3151 | |
| 3152 | // Setup left interpolant |
| 3153 | left_height=((spec_verts[vlb].y-spec_verts[vlt].y)*(float)h); |
| 3154 | left_x=spec_verts[vlt].x*w; |
| 3155 | delta_left_x=((spec_verts[vlb].x-spec_verts[vlt].x)*w)/left_height; |
| 3156 | left_vnorm=vertnorms[vlt]; |
| 3157 | delta_left_vnorm=(vertnorms[vlb]-vertnorms[vlt])/left_height; |
| 3158 | next_break_left = spec_verts[vlb].y*h; |
| 3159 | |
| 3160 | } |
| 3161 | |
| 3162 | if (y==next_break_right && !no_height) |
| 3163 | { |
| 3164 | while (y == (int)(spec_verts[vrb].y*h)) |
| 3165 | { |
| 3166 | vrt = vrb; |
| 3167 | vrb = SpecularNextIndex(vrb,fp->num_verts); |
| 3168 | } |
| 3169 | right_height=((spec_verts[vrb].y-spec_verts[vrt].y)*(float)h); |
| 3170 | right_x=spec_verts[vrt].x*w; |
| 3171 | delta_right_x=((spec_verts[vrb].x-spec_verts[vrt].x)*w)/right_height; |
| 3172 | right_vnorm=vertnorms[vrt]; |
| 3173 | delta_right_vnorm=(vertnorms[vrb]-vertnorms[vrt])/right_height; |
| 3174 | next_break_right = spec_verts[vrb].y*h; |
| 3175 | } |
| 3176 | |
| 3177 | int width=(right_x-left_x)+1; |
| 3178 | |
| 3179 | vector delta_vnorm=(right_vnorm-left_vnorm)/width; |
| 3180 | vector vnorm=left_vnorm; |
| 3181 | |
| 3182 | for (i=left_x;i<(left_x+width);i++) |
| 3183 | { |
| 3184 | int offset=y*w+i; |
| 3185 | |
| 3186 | if (!(src_data[offset] & OPAQUE_FLAG)) |
| 3187 | continue; |
| 3188 | |
| 3189 | sfp->normal_map[(offset*3)+0]=((vnorm.x+1.0)/2.0)*255.0; |
| 3190 | sfp->normal_map[(offset*3)+1]=((vnorm.y+1.0)/2.0)*255.0; |
| 3191 | sfp->normal_map[(offset*3)+2]=((vnorm.z+1.0)/2.0)*255.0; |
| 3192 | vnorm+=delta_vnorm; |
| 3193 | } |
| 3194 | |
| 3195 | // Update our interpolants |
| 3196 | left_x += delta_left_x; |
| 3197 | right_x += delta_right_x; |
| 3198 | right_vnorm+=delta_right_vnorm; |
| 3199 | left_vnorm+=delta_left_vnorm; |
| 3200 | } |
| 3201 | }*/ |
| 3202 | |
| 3203 | // Frees memory for specular lighting |
| 3204 | void CleanupSpecularLighting(int external) { |
| 3205 | int i, t; |
| 3206 | |
| 3207 | for (i = 0; i < MAX_ROOMS; i++) { |
| 3208 | if (Rooms[i].used) { |
| 3209 | if (Rooms[i].flags & RF_NO_LIGHT) |
| 3210 | continue; |
| 3211 | |
| 3212 | if ((Rooms[i].flags & RF_EXTERNAL) && !external) |
| 3213 | continue; |
| 3214 | |
| 3215 | if (!(Rooms[i].flags & RF_EXTERNAL) && external) |
| 3216 | continue; |
| 3217 | |
| 3218 | room *rp = &Rooms[i]; |
| 3219 | |
| 3220 | for (t = 0; t < 4; t++) { |
| 3221 | mem_free(Room_strongest_value[i][t]); |
| 3222 | } |
| 3223 | |
| 3224 | for (t = 0; t < rp->num_faces; t++) { |
| 3225 | face *fp = &rp->faces[t]; |
| 3226 | if (GameTextures[fp->tmap].flags & TF_SPECULAR) { |
| 3227 | ASSERT(fp->special_handle != BAD_SPECIAL_FACE_INDEX); |
| 3228 | int j, k, l; |
| 3229 | |
| 3230 | if (SpecialFaces[fp->special_handle].spec_instance[0].bright_color == 0) { |
| 3231 | // This face didn't get enough light, either free it up or |
| 3232 | // find another face that shares a lightmap and just use that |
| 3233 | // faces values |
| 3234 | |
| 3235 | int found = 0; |
| 3236 | float strongest_light = 0; |
| 3237 | face *best_face; |
| 3238 | |
| 3239 | for (l = 0; l < fp->num_verts; l++) { |
| 3240 | int vert_to_check = fp->face_verts[l]; |
| 3241 | for (j = 0; j < rp->num_faces; j++) { |
| 3242 | face *this_fp = &rp->faces[j]; |
| 3243 | |
| 3244 | if (this_fp == fp) |
| 3245 | continue; // don't do self |
| 3246 | if (this_fp->lmi_handle != fp->lmi_handle) |
| 3247 | continue; // only do faces that have same lightmaps |
| 3248 | |
| 3249 | if (this_fp->special_handle == BAD_SPECIAL_FACE_INDEX) |
| 3250 | continue; // Only do specmaps |
| 3251 | |
| 3252 | if (SpecialFaces[this_fp->special_handle].spec_instance[0].bright_color == 0) |
| 3253 | continue; // Don't do dark ones |
| 3254 | |
| 3255 | if (GameTextures[fp->tmap].flags & TF_SMOOTH_SPECULAR) |
| 3256 | continue; // Don't do smooth shades |
| 3257 | |
| 3258 | for (k = 0; k < this_fp->num_verts; k++) { |
| 3259 | if (vert_to_check == this_fp->face_verts[k]) { |
| 3260 | // We have a match, so see if this face is brighter |
| 3261 | // than all the others |
| 3262 | int color = SpecialFaces[this_fp->special_handle].spec_instance[0].bright_color; |
| 3263 | int r = (color >> 10) & 0x1f; |
| 3264 | int g = (color >> 5) & 0x1f; |
| 3265 | int b = (color & 0x1f); |
| 3266 | float sr = (float)r / 31.0; |
| 3267 | float sg = (float)g / 31.0; |
| 3268 | float sb = (float)b / 31.0; |
| 3269 | |
| 3270 | float mono_color = (sr * .3) + (sg * .59) + (sb * .11); |
| 3271 | if (mono_color > strongest_light) { |
| 3272 | strongest_light = mono_color; |
| 3273 | found = 1; |
| 3274 | best_face = this_fp; |
| 3275 | } |
| 3276 | } |
| 3277 | } |
| 3278 | } |
| 3279 | } |
| 3280 | // If after all that searching we couldn't find a good light, free it! |
| 3281 | if (!found) { |
| 3282 | FreeSpecialFace(fp->special_handle); |
| 3283 | fp->special_handle = BAD_SPECIAL_FACE_INDEX; |
| 3284 | } else { |
| 3285 | for (int z = 0; z < 4; z++) |
| 3286 | SpecialFaces[fp->special_handle].spec_instance[z] = |
| 3287 | SpecialFaces[best_face->special_handle].spec_instance[z]; |
| 3288 | } |
| 3289 | } |
| 3290 | } |
| 3291 | } |
| 3292 | } |
| 3293 | } |
| 3294 | } |
| 3295 | |
| 3296 | // Sets up memory usage for specular lighting |
| 3297 | void SetupSpecularLighting(int external) { |
| 3298 | int i, t; |
| 3299 | ClearAllRoomSpecmaps(external); |
| 3300 | |
| 3301 | for (i = 0; i < MAX_ROOMS; i++) { |
| 3302 | if (Rooms[i].used) { |
| 3303 | if (Rooms[i].flags & RF_NO_LIGHT) |
| 3304 | continue; |
| 3305 | |
| 3306 | if ((Rooms[i].flags & RF_EXTERNAL) && !external) |
| 3307 | continue; |
| 3308 | |
| 3309 | if (!(Rooms[i].flags & RF_EXTERNAL) && external) |
| 3310 | continue; |
| 3311 | |
| 3312 | room *rp = &Rooms[i]; |
| 3313 | |
| 3314 | // Calculate vertex normals for this room |
| 3315 | vector *vertnorms = (vector *)mem_malloc(sizeof(vector) * rp->num_verts); |
| 3316 | ASSERT(vertnorms); |
| 3317 | for (t = 0; t < rp->num_verts; t++) { |
| 3318 | int total = 0; |
| 3319 | vector normal; |
| 3320 | vm_MakeZero(&normal); |
| 3321 | |
| 3322 | for (int k = 0; k < rp->num_faces; k++) { |
| 3323 | face *fp = &rp->faces[k]; |
| 3324 | for (int j = 0; j < fp->num_verts; j++) { |
| 3325 | if (fp->face_verts[j] == t) { |
| 3326 | total++; |
| 3327 | normal += fp->normal; |
| 3328 | } |
| 3329 | } |
| 3330 | } |
| 3331 | |
| 3332 | if (total != 0) |
| 3333 | normal /= total; |
| 3334 | |
| 3335 | vertnorms[t] = normal; |
| 3336 | } |
| 3337 | |
| 3338 | for (t = 0; t < 4; t++) { |
| 3339 | Room_strongest_value[i][t] = (float *)mem_malloc(sizeof(float) * rp->num_faces); |
| 3340 | ASSERT(Room_strongest_value[i][t]); |
| 3341 | memset(Room_strongest_value[i][t], 0, sizeof(float) * rp->num_faces); |
| 3342 | } |
| 3343 | |
| 3344 | for (t = 0; t < rp->num_faces; t++) { |
| 3345 | face *fp = &rp->faces[t]; |
| 3346 | if (GameTextures[fp->tmap].flags & TF_SPECULAR) { |
| 3347 | ASSERT(fp->special_handle == BAD_SPECIAL_FACE_INDEX); |
| 3348 | |
| 3349 | int n; |
| 3350 | if (GameTextures[fp->tmap].flags & TF_SMOOTH_SPECULAR) |
| 3351 | n = AllocSpecialFace(SFT_SPECULAR, 4, true, fp->num_verts); |
| 3352 | else |
| 3353 | n = AllocSpecialFace(SFT_SPECULAR, 4); |
| 3354 | |
| 3355 | fp->special_handle = n; |
| 3356 | |
| 3357 | for (int k = 0; k < 4; k++) { |
| 3358 | SpecialFaces[n].spec_instance[k].bright_color = 0; |
| 3359 | vm_MakeZero(&SpecialFaces[n].spec_instance[k].bright_center); |
| 3360 | } |
| 3361 | |
| 3362 | // Get vertex normals for this punk |
| 3363 | if (GameTextures[fp->tmap].flags & TF_SMOOTH_SPECULAR) { |
| 3364 | for (k = 0; k < fp->num_verts; k++) { |
| 3365 | SpecialFaces[n].vertnorms[k] = vertnorms[fp->face_verts[k]]; |
| 3366 | } |
| 3367 | } |
| 3368 | } |
| 3369 | } |
| 3370 | mem_free(vertnorms); |
| 3371 | } |
| 3372 | } |
| 3373 | } |
| 3374 | |
| 3375 | // Computes the the mines UVs |
| 3376 | // Faces can now share one lightmap, so this routine goes through and tries to |
| 3377 | // combine as many faces as it can into one lightmap |
| 3378 | // Only works on one room |
| 3379 | /*void ComputeSingleRoomLightmapUVs (int roomnum) |
| 3380 | { |
| 3381 | int t,k; |
| 3382 | int not_combined=0; |
| 3383 | |
| 3384 | RoomsAlreadyCombined[roomnum]=(uint8_t *)mem_malloc (Rooms[roomnum].num_faces); |
| 3385 | ASSERT (RoomsAlreadyCombined[roomnum]); |
| 3386 | for (k=0;k<Rooms[roomnum].num_faces;k++) |
| 3387 | RoomsAlreadyCombined[roomnum][k]=0; |
| 3388 | |
| 3389 | |
| 3390 | for (t=0;t<Rooms[roomnum].num_faces;t++) |
| 3391 | { |
| 3392 | if (*(RoomsAlreadyCombined[roomnum]+t)==0) |
| 3393 | TestLightAdjacency (roomnum,t,0); |
| 3394 | } |
| 3395 | |
| 3396 | for (t=0;t<Rooms[roomnum].num_faces;t++) |
| 3397 | { |
| 3398 | if (!RoomsAlreadyCombined[roomnum][t]) |
| 3399 | { |
| 3400 | vector verts[MAX_VERTS_PER_FACE]; |
| 3401 | int room_list[2],face_list[2]; |
| 3402 | for (k=0;k<Rooms[roomnum].faces[t].num_verts;k++) |
| 3403 | verts[k]=Rooms[roomnum].verts[Rooms[roomnum].faces[t].face_verts[k]]; |
| 3404 | |
| 3405 | room_list[0]=i; |
| 3406 | face_list[0]=t; |
| 3407 | |
| 3408 | BuildLightmapUVs (room_list,face_list,1,verts,Rooms[roomnum].faces[t].num_verts,external); |
| 3409 | not_combined++; |
| 3410 | } |
| 3411 | } |
| 3412 | |
| 3413 | mprintf(0,"%d faces couldn't be combined!\n",not_combined); |
| 3414 | |
| 3415 | // Free memory |
| 3416 | free (RoomsAlreadyCombined[roomnum]); |
| 3417 | }*/ |