Debian Codegraph @

editor/editor_object_lighting.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <algorithm>
20
21 #include "3d.h"
22 #include "gametexture.h"
23 #include "editor_lighting.h"
24 #include "room.h"
25 #include "lightmap.h"
26 #include "polymodel.h"
27 #include <string.h>
28 #include <stdlib.h>
29 #include "radiosity.h"
30 #include "lightmap_info.h"
31 #include "object_lighting.h"
32 #include "mem.h"
33
34 void ComputeObjectSurfaceRes(rad_surface *surf, object *obj, int subnum, int facenum) {
35 int i;
36 float left = 1.1f, right = -1, top = 1.1f, bottom = -1;
37 lightmap_object_face *lfp = &obj->lm_object.lightmap_faces[subnum][facenum];
38 int lw = lmi_w(lfp->lmi_handle);
39 int lh = lmi_h(lfp->lmi_handle);
40
41 for (i = 0; i < lfp->num_verts; i++) {
42 if (lfp->u2[i] < left)
43 left = lfp->u2[i];
44 if (lfp->u2[i] > right)
45 right = lfp->u2[i];
46 if (lfp->v2[i] < top)
47 top = lfp->v2[i];
48 if (lfp->v2[i] > bottom)
49 bottom = lfp->v2[i];
50 }
51
52 float left_result = (left * lw) + .0001;
53 float right_result = (right * lw) + .0001;
54 float top_result = (top * lh) + .0001;
55 float bottom_result = (bottom * lh) + .0001;
56
57 surf->x1 = floor(left_result);
58 surf->x2 = floor(right_result);
59
60 surf->y1 = floor(top_result);
61 surf->y2 = floor(bottom_result);
62
63 surf->xresolution = (surf->x2 - surf->x1);
64 surf->yresolution = (surf->y2 - surf->y1);
65
66 // Adjust for a accuracy errors
67 if (((right_result) - (float)surf->x2) > .005)
68 surf->xresolution++;
69
70 if (((bottom_result) - (float)surf->y2) > .005)
71 surf->yresolution++;
72
73 if (((top_result) - (float)surf->y1) > .99)
74 surf->y1++;
75
76 if (((left_result) - (float)surf->x1) > .99)
77 surf->x1++;
78 }
79
80 void ApplyLightmapToObjectSurface(object *obj, int subnum, int facenum, rad_surface *sp) {
81 lightmap_object_face *fp = &obj->lm_object.lightmap_faces[subnum][facenum];
82 int i, t, lmi_handle;
83 int xres, yres;
84 int lw, lh;
85 int x1 = sp->x1;
86 int y1 = sp->y1;
87
88 xres = sp->xresolution;
89 yres = sp->yresolution;
90
91 ASSERT(fp->lmi_handle != BAD_LMI_INDEX);
92 lmi_handle = fp->lmi_handle;
93
94 lw = lmi_w(lmi_handle);
95 lh = lmi_h(lmi_handle);
96
97 ASSERT((xres + x1) <= lw);
98 ASSERT((yres + y1) <= lh);
99
100 ASSERT(lw >= 2);
101 ASSERT(lh >= 2);
102
103 uint16_t *dest_data = lm_data(LightmapInfo[lmi_handle].lm_handle);
104
105 for (i = 0; i < yres; i++) {
106 for (t = 0; t < xres; t++) {
107 if (!(sp->elements[i * xres + t].flags & EF_IGNORE)) {
108 ddgr_color color = GR_16_TO_COLOR(dest_data[(i + y1) * lw + (t + x1)]);
109 int red = GR_COLOR_RED(color);
110 int green = GR_COLOR_GREEN(color);
111 int blue = GR_COLOR_BLUE(color);
112
113 float fr, fg, fb;
114
115 if (!(dest_data[(i + y1) * lw + (t + x1)] & OPAQUE_FLAG)) {
116 red = green = blue = 0;
117 }
118
119 fr = std::min(1.0f, sp->elements[i * xres + t].exitance.r + Ambient_red);
120 fg = std::min(1.0f, sp->elements[i * xres + t].exitance.g + Ambient_green);
121 fb = std::min(1.0f, sp->elements[i * xres + t].exitance.b + Ambient_blue);
122
123 fr = (fr * 255) + .5;
124 fg = (fg * 255) + .5;
125 fb = (fb * 255) + .5;
126
127 red += (int)fr;
128 green += (int)fg;
129 blue += (int)fb;
130
131 if (dest_data[(i + y1) * lw + (t + x1)] & OPAQUE_FLAG) {
132
133 red /= 2;
134 green /= 2;
135 blue /= 2;
136 }
137
138 red = std::min(red, 255);
139 green = std::min(green, 255);
140 blue = std::min(blue, 255);
141
142 dest_data[(i + y1) * lw + (t + x1)] = OPAQUE_FLAG | GR_RGB16(red, green, blue);
143 }
144 }
145 }
146 }
147
148 void GetPointInObjectSpace(vector *dest, vector *pos, object *obj, int subnum, int world) {
149 poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num];
150 bsp_info *sm = &pm->submodel[subnum];
151 float normalized_time[MAX_SUBOBJECTS];
152 int i;
153 int rotate_list[MAX_SUBOBJECTS];
154 int num_to_rotate = 0;
155
156 if (!pm->new_style)
157 return;
158
159 for (i = 0; i < MAX_SUBOBJECTS; i++)
160 normalized_time[i] = 0.0;
161
162 SetModelAnglesAndPos(pm, normalized_time);
163
164 vector pnt = *pos;
165 int mn = subnum;
166 vector tpnt;
167 matrix m;
168
169 while (mn != -1) {
170 rotate_list[num_to_rotate] = mn;
171 num_to_rotate++;
172 mn = pm->submodel[mn].parent;
173 }
174
175 // Subtract and rotate position
176 if (world)
177 tpnt = pnt - obj->pos;
178 else
179 tpnt = pnt;
180
181 pnt = tpnt * obj->orient;
182
183 for (i = num_to_rotate - 1; i >= 0; i--) {
184 // Subtract and rotate position for this submodel
185 mn = rotate_list[i];
186
187 if (world)
188 tpnt = pnt - pm->submodel[mn].offset;
189 else
190 tpnt = pnt;
191
192 vm_AnglesToMatrix(&m, pm->submodel[mn].angs.p, pm->submodel[mn].angs.h, pm->submodel[mn].angs.b);
193
194 pnt = tpnt * m;
195 }
196
197 *dest = pnt;
198 }
199
200 // Goes through all objects and fills in the lightmap data for them
201 void AssignLightmapsToObjectSurfaces(int surface_index, int terrain) {
202 int i, t, j;
203 uint8_t rotated[MAX_LIGHTMAP_INFOS];
204
205 memset(rotated, 0, MAX_LIGHTMAP_INFOS);
206
207 for (i = 0; i <= Highest_object_index; i++) {
208 if ((terrain != 0) != (OBJECT_OUTSIDE(&Objects[i]) != 0))
209 continue;
210
211 if (Objects[i].type != OBJ_NONE && Objects[i].lighting_render_type == LRT_LIGHTMAPS) {
212 poly_model *po = &Poly_models[Objects[i].rtype.pobj_info.model_num];
213
214 if (!po->new_style)
215 continue;
216
217 for (t = 0; t < po->n_models; t++) {
218 bsp_info *sm = &po->submodel[t];
219
220 if (IsNonRenderableSubmodel(po, t))
221 continue;
222
223 for (j = 0; j < sm->num_faces; j++, surface_index++) {
224 ApplyLightmapToObjectSurface(&Objects[i], t, j, &Light_surfaces[surface_index]);
225
226 // Rotate the lightmap upper left
227 object *obj = &Objects[i];
228 lightmap_object_face *fp = &obj->lm_object.lightmap_faces[t][j];
229 lightmap_info *lmi_ptr = &LightmapInfo[fp->lmi_handle];
230
231 if (!rotated[fp->lmi_handle]) {
232 vector uleft, rvec, uvec, norm;
233 GetPointInObjectSpace(&uleft, &lmi_ptr->upper_left, obj, t, 1);
234 GetPointInObjectSpace(&norm, &lmi_ptr->normal, obj, t, 0);
235 lmi_ptr->normal = norm;
236 lmi_ptr->upper_left = uleft;
237
238 GetPointInObjectSpace(&rvec, &ScratchRVecs[fp->lmi_handle], obj, t, 0);
239 GetPointInObjectSpace(&uvec, &ScratchUVecs[fp->lmi_handle], obj, t, 0);
240
241 rotated[fp->lmi_handle] = 1;
242
243 // Find all the faces in this submodel that have this lightmap info handle
244 for (int k = 0; k < sm->num_faces; k++) {
245 lightmap_object_face *this_fp = &obj->lm_object.lightmap_faces[t][k];
246 if (fp->lmi_handle == this_fp->lmi_handle) {
247 this_fp->rvec = rvec;
248 this_fp->uvec = uvec;
249 }
250 }
251 }
252 }
253 }
254 }
255 }
256 }
257
258 // Goes through all objects int a room and fills in the lightmap data for them
259 void AssignLightmapsToObjectSurfacesForSingleRoom(int surface_index, int roomnum) {
260 int i, t, j;
261
262 for (i = 0; i <= Highest_object_index; i++) {
263
264 if (Objects[i].type != OBJ_NONE && Objects[i].lighting_render_type == LRT_LIGHTMAPS &&
265 Objects[i].roomnum == roomnum) {
266 poly_model *po = &Poly_models[Objects[i].rtype.pobj_info.model_num];
267
268 if (!po->new_style)
269 continue;
270
271 for (t = 0; t < po->n_models; t++) {
272 bsp_info *sm = &po->submodel[t];
273
274 if (IsNonRenderableSubmodel(po, t))
275 continue;
276
277 for (j = 0; j < sm->num_faces; j++, surface_index++)
278 ApplyLightmapToObjectSurface(&Objects[i], t, j, &Light_surfaces[surface_index]);
279 }
280 }
281 }
282 }
283
284 // Sets up radiosity surfaces for objects in the mine
285 // Returns the number of new surfaces
286 int ComputeSurfacesForObjects(int surface_index, int terrain) {
287 int i, t, j;
288
289 for (i = 0; i <= Highest_object_index; i++) {
290 if ((terrain != 0) != (OBJECT_OUTSIDE(&Objects[i]) != 0))
291 continue;
292
293 if (Objects[i].type != OBJ_NONE && Objects[i].lighting_render_type == LRT_LIGHTMAPS) {
294 poly_model *po = &Poly_models[Objects[i].rtype.pobj_info.model_num];
295
296 if (!po->new_style)
297 continue;
298
299 SetupObjectLightmapMemory(&Objects[i]);
300
301 if (terrain)
302 CombineObjectLightmapUVs(&Objects[i], LMI_TERRAIN_OBJECT);
303 else
304 CombineObjectLightmapUVs(&Objects[i], LMI_ROOM_OBJECT);
305
306 for (t = 0; t < po->n_models; t++) {
307 bsp_info *sm = &po->submodel[t];
308
309 if (IsNonRenderableSubmodel(po, t))
310 continue;
311
312 for (j = 0; j < sm->num_faces; j++, surface_index++) {
313 ComputeObjectSurfaceRes(&Light_surfaces[surface_index], &Objects[i], t, j);
314
315 if (sm->faces[j].nverts > 0) {
316 Light_surfaces[surface_index].verts = (vector *)mem_malloc(sm->faces[j].nverts * sizeof(vector));
317 ASSERT(Light_surfaces[surface_index].verts != NULL);
318 } else
319 Light_surfaces[surface_index].verts = NULL;
320
321 if (Light_surfaces[surface_index].xresolution * Light_surfaces[surface_index].yresolution > 0) {
322 Light_surfaces[surface_index].elements =
323 (rad_element *)mem_malloc(Light_surfaces[surface_index].xresolution *
324 Light_surfaces[surface_index].yresolution * sizeof(rad_element));
325 ASSERT(Light_surfaces[surface_index].elements != NULL);
326 } else
327 Light_surfaces[surface_index].elements = NULL;
328
329 Light_surfaces[surface_index].flags = 0;
330
331 if (sm->faces[j].texnum == -1) {
332 Light_surfaces[surface_index].emittance.r = 0;
333 Light_surfaces[surface_index].emittance.g = 0;
334 Light_surfaces[surface_index].emittance.b = 0;
335 Light_surfaces[surface_index].reflectivity = .5;
336 } else {
337 Light_surfaces[surface_index].emittance.r = (float)GameTextures[po->textures[sm->faces[j].texnum]].r;
338 Light_surfaces[surface_index].emittance.g = (float)GameTextures[po->textures[sm->faces[j].texnum]].g;
339 Light_surfaces[surface_index].emittance.b = (float)GameTextures[po->textures[sm->faces[j].texnum]].b;
340 Light_surfaces[surface_index].reflectivity = GameTextures[po->textures[sm->faces[j].texnum]].reflectivity;
341 if ((GetMaxColor(&Light_surfaces[surface_index].emittance)) > .005)
342 Light_surfaces[surface_index].flags |= SF_LIGHTSOURCE;
343 }
344
345 if (terrain)
346 Light_surfaces[surface_index].surface_type = ST_TERRAIN_OBJECT;
347 else
348 Light_surfaces[surface_index].surface_type = ST_ROOM_OBJECT;
349
350 Light_surfaces[surface_index].normal =
351 LightmapInfo[Objects[i].lm_object.lightmap_faces[t][j].lmi_handle].normal;
352 Light_surfaces[surface_index].roomnum = Objects[i].roomnum;
353
354 if (Light_surfaces[surface_index].surface_type == ST_ROOM_OBJECT) {
355 if (Rooms[Objects[i].roomnum].flags & RF_TOUCHES_TERRAIN)
356 Light_surfaces[surface_index].flags |= SF_TOUCHES_TERRAIN;
357
358 for (int k = 0; k < Rooms[Objects[i].roomnum].num_portals; k++) {
359 if (Rooms[Objects[i].roomnum].portals[k].croom == -1 ||
360 (Rooms[Rooms[Objects[i].roomnum].portals[k].croom].flags & RF_EXTERNAL))
361 Light_surfaces[surface_index].flags |= SF_TOUCHES_TERRAIN;
362 }
363 }
364
365 // Set the vertices for each element
366 BuildElementListForObjectFace(i, t, j, &Light_surfaces[surface_index]);
367 }
368 }
369 }
370 }
371
372 return 0;
373 }
374
375 // Sets up radiosity surfaces for objects in a room
376 // Returns the number of new surfaces
377 int ComputeSurfacesForObjectsForSingleRoom(int surface_index, int roomnum) {
378 int i, t, j;
379
380 for (i = 0; i <= Highest_object_index; i++) {
381
382 if (Objects[i].type != OBJ_NONE && Objects[i].lighting_render_type == LRT_LIGHTMAPS &&
383 Objects[i].roomnum == roomnum) {
384 poly_model *po = &Poly_models[Objects[i].rtype.pobj_info.model_num];
385
386 if (!po->new_style)
387 continue;
388
389 SetupObjectLightmapMemory(&Objects[i]);
390 CombineObjectLightmapUVs(&Objects[i], LMI_ROOM_OBJECT);
391
392 for (t = 0; t < po->n_models; t++) {
393 bsp_info *sm = &po->submodel[t];
394
395 if (IsNonRenderableSubmodel(po, t))
396 continue;
397
398 for (j = 0; j < sm->num_faces; j++, surface_index++) {
399 ComputeObjectSurfaceRes(&Light_surfaces[surface_index], &Objects[i], t, j);
400
401 if (sm->faces[j].nverts > 0) {
402 Light_surfaces[surface_index].verts = (vector *)mem_malloc(sm->faces[j].nverts * sizeof(vector));
403 ASSERT(Light_surfaces[surface_index].verts != NULL);
404 } else
405 Light_surfaces[surface_index].verts = NULL;
406
407 if (Light_surfaces[surface_index].xresolution * Light_surfaces[surface_index].yresolution > 0) {
408 Light_surfaces[surface_index].elements =
409 (rad_element *)mem_malloc(Light_surfaces[surface_index].xresolution *
410 Light_surfaces[surface_index].yresolution * sizeof(rad_element));
411 ASSERT(Light_surfaces[surface_index].elements != NULL);
412 } else
413 Light_surfaces[surface_index].elements = NULL;
414
415 if (sm->faces[j].texnum == -1) {
416 Light_surfaces[surface_index].emittance.r = 0;
417 Light_surfaces[surface_index].emittance.g = 0;
418 Light_surfaces[surface_index].emittance.b = 0;
419 Light_surfaces[surface_index].reflectivity = .5;
420 } else {
421 Light_surfaces[surface_index].emittance.r = (float)GameTextures[po->textures[sm->faces[j].texnum]].r;
422 Light_surfaces[surface_index].emittance.g = (float)GameTextures[po->textures[sm->faces[j].texnum]].g;
423 Light_surfaces[surface_index].emittance.b = (float)GameTextures[po->textures[sm->faces[j].texnum]].b;
424 Light_surfaces[surface_index].reflectivity = GameTextures[po->textures[sm->faces[j].texnum]].reflectivity;
425 }
426
427 Light_surfaces[surface_index].surface_type = ST_ROOM_OBJECT;
428
429 Light_surfaces[surface_index].normal =
430 LightmapInfo[Objects[i].lm_object.lightmap_faces[t][j].lmi_handle].normal;
431 Light_surfaces[surface_index].roomnum = Objects[i].roomnum;
432
433 // Set the vertices for each element
434 BuildElementListForObjectFace(i, t, j, &Light_surfaces[surface_index]);
435 }
436 }
437 }
438 }
439
440 return 0;
441 }
442
443 // Gets the total number of object faces that exist in a mine
444 int GetTotalObjectFaces(int terrain) {
445 int i;
446 int facecount = 0;
447
448 for (i = 0; i <= Highest_object_index; i++) {
449 if (Objects[i].type != OBJ_NONE) {
450 if ((terrain != 0) != (OBJECT_OUTSIDE(&Objects[i]) != 0))
451 continue;
452
453 if (Objects[i].lighting_render_type == LRT_LIGHTMAPS) {
454 poly_model *po = &Poly_models[Objects[i].rtype.pobj_info.model_num];
455
456 if (!po->new_style)
457 continue;
458
459 facecount += CountFacesInPolymodel(po);
460 }
461 }
462 }
463
464 return facecount;
465 }
466
467 // Gets the total number of object faces that exist in a room
468 int GetTotalObjectFacesForSingleRoom(int roomnum) {
469 int i;
470 int facecount = 0;
471
472 for (i = 0; i <= Highest_object_index; i++) {
473 if (Objects[i].type != OBJ_NONE) {
474 if (Objects[i].roomnum != roomnum)
475 continue;
476
477 if (Objects[i].lighting_render_type == LRT_LIGHTMAPS) {
478 poly_model *po = &Poly_models[Objects[i].rtype.pobj_info.model_num];
479
480 if (!po->new_style)
481 continue;
482
483 facecount += CountFacesInPolymodel(po);
484 }
485 }
486 }
487
488 return facecount;
489 }
490
491 void BuildObjectLightmapUVs(object *obj, int *sublist, int *facelist, int count, vector *lightmap_poly, int nv,
492 int lm_type) {
493 matrix face_matrix, trans_matrix;
494 vector fvec;
495 vector avg_vert;
496 vector verts[MAX_VERTS_PER_FACE * 5];
497 vector facevert;
498 vector rot_vert;
499 int i, t;
500 int lmi_handle;
501 vector world_verts[32];
502
503 poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num];
504
505 for (i = 0; i < pm->submodel[sublist[0]].faces[facelist[0]].nverts; i++)
506 GetObjectPointInWorld(&world_verts[i], obj, sublist[0], pm->submodel[sublist[0]].faces[facelist[0]].vertnums[i]);
507
508 // find the center point of this face
509 vm_MakeZero(&avg_vert);
510 for (i = 0; i < nv; i++)
511 avg_vert += lightmap_poly[i];
512
513 avg_vert /= nv;
514
515 // Make the orientation matrix
516 // Reverse the normal because we're looking "at" the face, not from it
517
518 vm_GetNormal(&fvec, &world_verts[0], &world_verts[1], &world_verts[2]);
519 fvec = -fvec;
520
521 if ((vm_NormalizeVector(&fvec)) != 0)
522 vm_VectorToMatrix(&face_matrix, &fvec, NULL, NULL);
523 else
524 vm_MakeIdentity(&face_matrix);
525 // Make the transformation matrix
526
527 angvec avec;
528 vm_ExtractAnglesFromMatrix(&avec, &face_matrix);
529 vm_AnglesToMatrix(&trans_matrix, avec.p, avec.h, avec.b);
530
531 // Rotate all the points
532 for (i = 0; i < nv; i++) {
533 vector vert = lightmap_poly[i];
534
535 vert -= avg_vert;
536 vm_MatrixMulVector(&rot_vert, &vert, &trans_matrix);
537
538 verts[i] = rot_vert;
539 }
540
541 // Find left most point
542 int leftmost_point = -1;
543 float leftmost_x = 900000.00f; // a big number
544
545 for (i = 0; i < nv; i++) {
546 if (verts[i].x < leftmost_x) {
547 leftmost_point = i;
548 leftmost_x = verts[i].x;
549 }
550 }
551
552 ASSERT(leftmost_point != -1);
553
554 // Find top most point
555 int topmost_point = -1;
556 float topmost_y = -900000.0f; // a big number
557
558 for (i = 0; i < nv; i++) {
559 if (verts[i].y > topmost_y) {
560 topmost_point = i;
561 topmost_y = verts[i].y;
562 }
563 }
564
565 ASSERT(topmost_point != -1);
566
567 // Find right most point
568 int rightmost_point = -1;
569 float rightmost_x = -900000.00f; // a big number
570
571 for (i = 0; i < nv; i++) {
572 if (verts[i].x > rightmost_x) {
573 rightmost_point = i;
574 rightmost_x = verts[i].x;
575 }
576 }
577
578 ASSERT(rightmost_point != -1);
579
580 // Find bottom most point
581 int bottommost_point = -1;
582 float bottommost_y = 900000.0f; // a big number
583
584 for (i = 0; i < nv; i++) {
585 if (verts[i].y < bottommost_y) {
586 bottommost_point = i;
587 bottommost_y = verts[i].y;
588 }
589 }
590
591 ASSERT(bottommost_point != -1);
592
593 // now set the base vertex, which is where we base uv 0,0 on
594
595 vector base_vector;
596
597 base_vector.x = verts[leftmost_point].x;
598 base_vector.y = verts[topmost_point].y;
599 base_vector.z = 0;
600
601 // Figure out lightmap resolution
602 float xdiff = verts[rightmost_point].x - verts[leftmost_point].x;
603 float ydiff = verts[topmost_point].y - verts[bottommost_point].y;
604 float max_diff = (float)std::max(xdiff, ydiff);
605
606 int lightmap_x_res = -1, lightmap_y_res = -1;
607 float xspacing = LightSpacing;
608 float yspacing = LightSpacing;
609 float spacing = LightSpacing;
610 int res, done_spacing = 0;
611 int xspace_int, yspace_int;
612
613 // If the default spacing would make us go over our lightmap resolution
614 // limit, then increase the spacing and try again
615 while (!done_spacing) {
616 res = (xdiff / xspacing);
617 if (((xdiff / xspacing) - res) > 0)
618 res++;
619
620 res++;
621
622 if (res > 126)
623 xspacing += 1;
624 else
625 done_spacing = 1;
626 }
627
628 // Set a mininum, at least
629 if (res < 2)
630 res = 2;
631
632 lightmap_x_res = res;
633
634 done_spacing = 0;
635 while (!done_spacing) {
636 res = (ydiff / yspacing);
637 if (((ydiff / yspacing) - res) > 0)
638 res++;
639
640 res++;
641
642 if (res > 126)
643 yspacing += 1;
644 else
645 done_spacing = 1;
646 }
647
648 // Set a mininum, at least
649 if (res < 2)
650 res = 2;
651
652 lightmap_y_res = res;
653
654 /*
655 // Find power of 2 number
656 for (i=0;i<=7;i++)
657 {
658 int low_num=1<i;
659 int hi_num=2<<i;
660 if (res<=hi_num && res>low_num)
661 {
662 lightmap_res=hi_num;
663 break;
664 }
665 }*/
666
667 lmi_handle = AllocLightmapInfo(lightmap_x_res, lightmap_y_res, lm_type);
668 ASSERT(lmi_handle != BAD_LMI_INDEX);
669
670 // Now do best fit spacing
671 if (BestFit) {
672 xspace_int = (xdiff / lightmap_x_res);
673 if ((xdiff - (lightmap_x_res * xspace_int)) > 0)
674 xspace_int++;
675
676 yspace_int = (ydiff / lightmap_y_res);
677 if ((ydiff - (lightmap_y_res * yspace_int)) > 0)
678 yspace_int++;
679 } else {
680 xspace_int = xspacing;
681 yspace_int = yspacing;
682 }
683
684 // Figure out lightmap uvs
685
686 // Rotate all the face points
687 for (i = 0; i < count; i++) {
688 obj->lm_object.lightmap_faces[sublist[i]][facelist[i]].lmi_handle = lmi_handle;
689 bsp_info *sm = &pm->submodel[sublist[i]];
690 polyface *fp = &sm->faces[facelist[i]];
691 lightmap_object_face *lfp = &obj->lm_object.lightmap_faces[sublist[i]][facelist[i]];
692
693 for (t = 0; t < fp->nverts; t++)
694 GetObjectPointInWorld(&world_verts[t], obj, sublist[i], fp->vertnums[t]);
695
696 for (t = 0; t < fp->nverts; t++) {
697 vector vert = world_verts[t];
698
699 vert -= avg_vert;
700 vm_MatrixMulVector(&rot_vert, &vert, &trans_matrix);
701
702 facevert = rot_vert;
703
704 // Find uv2s for this vertex
705 lfp->u2[t] = (facevert.x - verts[leftmost_point].x) / (float)(lightmap_x_res * xspace_int);
706 lfp->v2[t] = fabs((verts[topmost_point].y - facevert.y)) / (float)(lightmap_y_res * yspace_int);
707
708 ASSERT(lfp->u2[t] >= 0 && lfp->u2[t] <= 1.0);
709 ASSERT(lfp->v2[t] >= 0 && lfp->v2[t] <= 1.0);
710 }
711 }
712
713 // Find upper left corner
714 vm_TransposeMatrix(&trans_matrix);
715 vm_MatrixMulVector(&rot_vert, &base_vector, &trans_matrix);
716 LightmapInfo[lmi_handle].upper_left = rot_vert + avg_vert;
717
718 LightmapInfo[lmi_handle].xspacing = xspace_int;
719 LightmapInfo[lmi_handle].yspacing = yspace_int;
720 LightmapInfo[lmi_handle].normal = -fvec;
721 ScratchCenters[lmi_handle] = avg_vert;
722 }
723
724 // Important - vertnum is the index into the face_verts[] array in the face structure,
725 // not an index into the verts[] array of the room structure
726 void BuildElementListForObjectFace(int objnum, int subnum, int facenum, rad_surface *surf) {
727 matrix face_matrix, trans_matrix;
728 vector fvec;
729 vector avg_vert;
730 vector verts[MAX_VERTS_PER_FACE * 5];
731 vector rot_vert;
732 vector vert;
733 vector world_verts[32];
734 int i, t;
735 int xres, yres;
736 int lmi_handle;
737 int x1 = surf->x1, y1 = surf->y1;
738 poly_model *pm = &Poly_models[Objects[objnum].rtype.pobj_info.model_num];
739 bsp_info *sm = &pm->submodel[subnum];
740 polyface *fp = &sm->faces[facenum];
741
742 xres = surf->xresolution;
743 yres = surf->yresolution;
744
745 ASSERT(pm->used);
746 ASSERT(fp->nverts >= 3);
747 ASSERT(Objects[objnum].lm_object.lightmap_faces[subnum][facenum].lmi_handle != BAD_LMI_INDEX);
748
749 ASSERT(fp->nverts < 32);
750
751 for (i = 0; i < fp->nverts; i++)
752 GetObjectPointInWorld(&world_verts[i], &Objects[objnum], subnum, fp->vertnums[i]);
753
754 lmi_handle = Objects[objnum].lm_object.lightmap_faces[subnum][facenum].lmi_handle;
755 avg_vert = ScratchCenters[lmi_handle];
756
757 // Make the orientation matrix
758 // Reverse the normal because we're looking "at" the face, not from it
759 fvec = -LightmapInfo[lmi_handle].normal;
760
761 if ((vm_NormalizeVector(&fvec)) != 0)
762 vm_VectorToMatrix(&face_matrix, &fvec, NULL, NULL);
763 else
764 vm_MakeIdentity(&face_matrix);
765
766 ScratchRVecs[lmi_handle] = face_matrix.rvec;
767 ScratchUVecs[lmi_handle] = face_matrix.uvec;
768
769 // Make the transformation matrix
770
771 angvec avec;
772 vm_ExtractAnglesFromMatrix(&avec, &face_matrix);
773 vm_AnglesToMatrix(&trans_matrix, avec.p, avec.h, avec.b);
774
775 // Rotate all the points
776 for (i = 0; i < fp->nverts; i++) {
777 vert = world_verts[i];
778
779 vert -= avg_vert;
780 vm_MatrixMulVector(&rot_vert, &vert, &trans_matrix);
781
782 verts[i] = rot_vert;
783 }
784
785 // Find a base vector
786 vector base_vector;
787 vector xdiff, ydiff;
788
789 vm_MakeZero(&xdiff);
790 vm_MakeZero(&ydiff);
791
792 // Rotate our upper left point into our 2d space
793 vert = LightmapInfo[lmi_handle].upper_left - avg_vert;
794 vm_MatrixMulVector(&base_vector, &vert, &trans_matrix);
795
796 vm_TransposeMatrix(&trans_matrix);
797
798 xdiff.x = LightmapInfo[lmi_handle].xspacing;
799 ydiff.y = LightmapInfo[lmi_handle].yspacing;
800
801 for (i = 0; i < yres; i++) {
802 for (t = 0; t < xres; t++) {
803 int element_index = i * xres + t;
804 vector clip_verts[4];
805
806 rad_element *ep = &surf->elements[element_index];
807
808 clip_verts[0] = base_vector + (xdiff * (t + x1)) - (ydiff * (i + y1));
809 clip_verts[1] = base_vector + (xdiff * (t + x1 + 1)) - (ydiff * (i + y1));
810 clip_verts[2] = base_vector + (xdiff * (t + x1 + 1)) - (ydiff * (i + y1 + 1));
811 clip_verts[3] = base_vector + (xdiff * (t + x1)) - (ydiff * (i + y1 + 1));
812 ClipSurfaceElement(verts, ep, clip_verts, fp->nverts);
813
814 for (int k = 0; k < ep->num_verts; k++) {
815 vm_MatrixMulVector(&rot_vert, &ep->verts[k], &trans_matrix);
816 ep->verts[k] = rot_vert + avg_vert;
817 }
818 }
819 }
820
821 if (Square_surfaces) {
822 surf->verts[0] = base_vector;
823 surf->verts[1] = base_vector + (xdiff * xres);
824 surf->verts[2] = base_vector + (xdiff * xres) - (ydiff * yres);
825 surf->verts[3] = base_vector - (ydiff * yres);
826
827 for (int k = 0; k < 4; k++) {
828 vm_MatrixMulVector(&rot_vert, &surf->verts[k], &trans_matrix);
829 surf->verts[k] = rot_vert + avg_vert;
830 }
831 surf->num_verts = 4;
832 } else {
833 surf->num_verts = fp->nverts;
834 for (int k = 0; k < surf->num_verts; k++) {
835 surf->verts[k] = world_verts[k];
836 }
837 }
838 }
839
840 #define MAX_COMBINES 50
841 #define LM_ADJACENT_FACE_THRESHOLD .95
842 uint8_t *ObjectsAlreadyCombined[MAX_OBJECTS];
843
844 // Given a submodel and a face, goes through the entire object and checks to see
845 // if this face can share a lightmap with any other face
846 int TestObjectLightAdjacency(object *obj, int subnum, int facenum, int lmi_type) {
847 int i, t, k;
848 poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num];
849 bsp_info *a_sm = &pm->submodel[subnum];
850 polyface *afp = &a_sm->faces[facenum];
851 vector anormal;
852
853 vector averts[MAX_VERTS_PER_FACE * 5];
854 vector bverts[MAX_VERTS_PER_FACE * 5];
855 vector dest_verts[MAX_VERTS_PER_FACE * 5];
856
857 int face_combine_list[MAX_COMBINES];
858 int submodel_combine_list[MAX_COMBINES];
859
860 if (afp->texnum == -1)
861 return 0;
862 int tex = pm->textures[afp->texnum];
863
864 if (GameTextures[tex].r > 0 || GameTextures[tex].g > 0 || GameTextures[tex].b > 0)
865 return 0;
866
867 // Setup our 'base' face
868
869 int anv = afp->nverts;
870 int total_faces = 1;
871
872 submodel_combine_list[0] = subnum;
873 face_combine_list[0] = facenum;
874
875 for (i = 0; i < afp->nverts; i++)
876 GetObjectPointInWorld(&averts[i], obj, subnum, afp->vertnums[i]);
877 vm_GetNormal(&anormal, &averts[0], &averts[1], &averts[2]);
878
879 StartOver:
880
881 // Go through each room and find an adjacent face
882 for (i = 0; i < pm->n_models; i++) {
883 bsp_info *bsm = &pm->submodel[i];
884
885 if (IsNonRenderableSubmodel(pm, i))
886 continue;
887
888 if (bsm != a_sm) // only combine faces in the same submodel
889 continue;
890
891 for (t = 0; t < bsm->num_faces; t++) {
892 if (total_faces >= MAX_COMBINES - 1)
893 continue;
894
895 if (bsm == a_sm && t == facenum)
896 continue; // don't do self
897
898 // Don't do if already spoken fore
899 if (ObjectsAlreadyCombined[i][t])
900 continue;
901
902 polyface *bfp = &bsm->faces[t];
903 vector bnormal;
904
905 // Don't do combine light sources
906
907 tex = pm->textures[bfp->texnum];
908 if (GameTextures[tex].r > 0 || GameTextures[tex].g > 0 || GameTextures[tex].b > 0)
909 continue;
910
911 for (k = 0; k < bfp->nverts; k++)
912 GetObjectPointInWorld(&bverts[k], obj, i, bfp->vertnums[k]);
913
914 vm_GetNormal(&bnormal, &bverts[0], &bverts[1], &bverts[2]);
915
916 int nv = CombineLightFaces(dest_verts, averts, anv, &anormal, bverts, bfp->nverts, &bnormal);
917
918 // We have a combine! Mark this face in the appropriate list
919 // And update our new polygon
920 if (nv > 0) {
921 submodel_combine_list[total_faces] = i;
922 face_combine_list[total_faces] = t;
923 total_faces++;
924
925 ObjectsAlreadyCombined[subnum][facenum] = 1;
926 ObjectsAlreadyCombined[i][t] = 1;
927 for (k = 0; k < nv; k++)
928 averts[k] = dest_verts[k];
929
930 anv = nv;
931
932 goto StartOver;
933 }
934 }
935 }
936
937 // Now build 1 lightmap to be shared across all the faces that were combined
938 if (total_faces > 1) {
939 BuildObjectLightmapUVs(obj, submodel_combine_list, face_combine_list, total_faces, averts, anv, lmi_type);
940 }
941
942 return 1;
943 }
944
945 // Computes the the mines UVs
946 // Faces can now share one lightmap, so this routine goes through and tries to
947 // combine as many faces as it can into one lightmap
948 void CombineObjectLightmapUVs(object *obj, int lmi_type) {
949 int i, t, k;
950 int not_combined = 0;
951
952 poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num];
953 ASSERT(obj->lm_object.used);
954
955 for (i = 0; i < pm->n_models; i++) {
956 bsp_info *sm = &pm->submodel[i];
957
958 if (IsNonRenderableSubmodel(pm, i))
959 continue;
960
961 ObjectsAlreadyCombined[i] = (uint8_t *)mem_malloc(sm->num_faces);
962 ASSERT(ObjectsAlreadyCombined[i]);
963 for (k = 0; k < sm->num_faces; k++)
964 ObjectsAlreadyCombined[i][k] = 0;
965 }
966
967 for (i = 0; i < pm->n_models; i++) {
968 bsp_info *sm = &pm->submodel[i];
969
970 if (IsNonRenderableSubmodel(pm, i))
971 continue;
972
973 for (t = 0; t < sm->num_faces; t++) {
974 if (*(ObjectsAlreadyCombined[i] + t) == 0)
975 TestObjectLightAdjacency(obj, i, t, lmi_type);
976 }
977 }
978
979 // Now build lightmaps for any faces that couldn't be combined
980 for (i = 0; i < pm->n_models; i++) {
981 bsp_info *sm = &pm->submodel[i];
982
983 if (IsNonRenderableSubmodel(pm, i))
984 continue;
985
986 for (t = 0; t < sm->num_faces; t++) {
987 if (!ObjectsAlreadyCombined[i][t]) {
988 vector verts[MAX_VERTS_PER_FACE * 5];
989 int submodel_list[2], face_list[2];
990 for (k = 0; k < sm->faces[t].nverts; k++) {
991 GetObjectPointInWorld(&verts[k], obj, i, sm->faces[t].vertnums[k]);
992 }
993
994 submodel_list[0] = i;
995 face_list[0] = t;
996 BuildObjectLightmapUVs(obj, submodel_list, face_list, 1, verts, sm->faces[t].nverts, lmi_type);
997 not_combined++;
998 }
999 }
1000 }
1001
1002 mprintf(0, "%d %s faces couldn't be combined!\n", not_combined, pm->name);
1003
1004 // Free memory
1005 for (i = 0; i < pm->n_models; i++) {
1006 bsp_info *sm = &pm->submodel[i];
1007
1008 if (IsNonRenderableSubmodel(pm, i))
1009 continue;
1010 mem_free(ObjectsAlreadyCombined[i]);
1011 }
1012 }