Repository navigation
Expand file tree
/
Copy pathobject_lights.c
More file actions
2739 lines (2507 loc) · 79.5 KB
/
Copy pathobject_lights.c
File metadata and controls
2739 lines (2507 loc) · 79.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
OBJECT_LIGHTS.C
symbols in this file:
00128730 0030:
_texture_cache_bitmap_load (0000)
00128760 0020:
_shade_scalar (0000)
00128780 0040:
_shade_vector2d (0000)
001287C0 0060:
_shade_vector3d (0000)
00128820 0100:
_sample_lightmap (0000)
00128920 00f0:
_sample_diffuse_texture (0000)
00128A10 0020:
_should_render_lights (0000)
00128A30 00c0:
_lights_initialize (0000)
00128AF0 0010:
_lights_dispose (0000)
00128B00 0030:
_lights_initialize_for_new_map (0000)
00128B30 0020:
_lights_dispose_from_old_map (0000)
00128B50 0010:
_lights_enable (0000)
00128B60 0040:
_light_delete (0000)
00128BA0 0060:
_light_build_cluster_array (0000)
00128C00 00d0:
_object_get_self_illumination (0000)
00128CD0 0260:
_light_particle (0000)
00128F30 0060:
_light_disconnect_from_map (0000)
00128F90 00b0:
_lights_disconnect_from_structure_bsp (0000)
00129040 0020:
_light_attenuation (0000)
00129060 00a0:
_brighten_real_rgb_color (0000)
00129100 0020:
_cluster_get_first_light (0000)
00129120 0020:
_cluster_get_next_light (0000)
00129140 0040:
_light_marker_begin (0000)
00129180 0060:
_light_unmarked (0000)
001291E0 0060:
_light_mark (0000)
00129240 0040:
_light_marker_end (0000)
00129280 0110:
_render_debug_light (0000)
00129390 00e0:
_lights_queue_lens_flare (0000)
00129470 0230:
_find_point_lights_for_object_in_cluster (0000)
001296A0 0400:
_build_distant_lights (0000)
00129AA0 00f0:
_light_compute_bounding_sphere (0000)
00129B90 00e0:
_light_get_bounding_sphere (0000)
00129C70 01d0:
_lights_render_diffuse (0000)
00129E40 0150:
_lights_render_specular (0000)
00129F90 02d0:
_lights_illumination_at_point (0000)
0012A260 0110:
_lights_prepare_for_object_dynamic (0000)
0012A370 03b0:
_lights_distant_lighting_at_point (0000)
0012A720 0280:
_light_reconnect_to_map (0000)
0012A9A0 0060:
_lights_reconnect_to_structure_bsp (0000)
0012AA00 00e0:
_light_new (0000)
0012AAE0 00f0:
_light_new_unattached (0000)
0012ABD0 0960:
_lights_preprocess_scene (0000)
0012B530 0340:
_lights_prepare_for_object_static (0000)
002896C0 00b0:
_default_object_lighting (0000)
00289770 0021:
??_C@_0CB@NDAECIIM@c?3?2halo?2source?2objects?2objects?4h@ (0000)
00289798 00b9:
??_C@_0LJ@HECDDFBP@material?9?$DOlightmap_vertices?4type@ (0000)
00289854 0027:
??_C@_0CH@GHJLLMCD@c?3?2halo?2SOURCE?2objects?2object_li@ (0000)
00289880 0095:
??_C@_0JF@BFHNCHBA@material?9?$DOvertices?4type?$DN?$DN_raster@ (0000)
00289918 002c:
??_C@_0CM@CCFOPHPG@couldn?8t?5allocate?5memory?5for?5obj@ (0000)
00289944 0014:
??_C@_0BE@MMBBLMKK@lights_game_globals?$AA@ (0000)
00289958 000b:
??_C@_0L@PNIBKHKO@light_data?$AA@ (0000)
00289964 000f:
??_C@_0P@MLOLKFEJ@lights?5globals?$AA@ (0000)
00289974 0007:
??_C@_06IDBEMGIK@lights?$AA@ (0000)
0028997C 003b:
??_C@_0DL@OAOFOGFN@TEST_FLAG?$CIlight?9?$DOflags?0?5_point_l@ (0000)
002899B8 0023:
??_C@_0CD@CEANBCDI@?$CBlights_globals?4marker_initializ@ (0000)
002899DC 0022:
??_C@_0CC@DMOOLGHL@lights_globals?4marker_initialize@ (0000)
00289A00 0004:
__real@3f34fdf4 (0000)
00289A04 003c:
??_C@_0DM@NOKGCMAM@?$CBTEST_FLAG?$CIlight?9?$DOflags?0?5_point_@ (0000)
00289A40 002a:
??_C@_0CK@JMHDKADE@lens_flare_parameters?4light_iden@ (0000)
00289A6C 0035:
??_C@_0DF@MMLEIBDB@light?9?$DOcolor?4blue?5?$DO?$DN0?40f?5?$CG?$CG?5ligh@ (0000)
00289AA4 0035:
??_C@_0DF@KFPEGOCE@light?9?$DOcolor?4green?$DO?$DN0?40f?5?$CG?$CG?5ligh@ (0000)
00289ADC 0033:
??_C@_0DD@NIPJINKE@light?9?$DOcolor?4red?5?$DO?$DN0?40f?5?$CG?$CG?5light@ (0000)
00289B10 0004:
__real@bf3504f3 (0000)
0030B388 0605:
_data_0030b388 (0000)
_object_light_ambient_base (05f8)
_object_light_ambient_scale (05fc)
_object_light_secondary_scale (0600)
_object_light_interpolate (0604)
00456E94 0004:
_lights_game_globals (0000)
*/
/* ---------- headers */
/* objects.h lies in this unit's own directory, so January's __FILE__ for it was the absolute
path (see OBJECTS_H_FILE in objects.h) */
#define OBJECTS_H_FILE "c:\\halo\\source\\objects\\objects.h"
#include "cseries/cseries.h"
#include "cseries/errors.h"
#include "cseries/profile.h"
#include "bitmaps/bitmaps.h"
#include "bitmaps/bitmap_group.h"
#include "bitmaps/bitmap_group_lookup.h"
#include "bitmaps/bitmaps_sampling.h"
#include "bitmaps/bitmap_utilities.h"
#include "effects/decals.h"
#include "math/real_math.h"
#include "cache/texture_cache.h"
#include "game/game.h"
#include "game/game_engine.h"
#include "interface/first_person_weapons.h"
#include "math/periodic_functions.h"
#include "memory/data.h"
#include "objects/light_definitions.h"
#include "objects/object_lights.h"
#include "objects/object_lights_rendering.h"
#include "objects/objects.h"
#include "render/render.h"
#include "render/render_debug.h"
#include "rasterizer/rasterizer.h"
#include "rasterizer/rasterizer_environment.h"
#include "rasterizer/rasterizer_geometry.h"
#include "rasterizer/rasterizer_geometry_environment.h"
#include "rasterizer/rasterizer_lights.h"
#include "saved games/game_state.h"
#include "scenario/scenario.h"
#include "shaders/shader_definitions.h"
#include "structures/cluster_partitions.h"
#include "structures/structure_bsp_definitions.h"
#include "structures/structure_render.h"
#include "structures/structure_render_lights.h"
#include "structures/structures.h"
#include "structures/structure_vector_tests.h"
#include "structures/structure_visibility.h"
#include "tag_files/tag_groups.h"
#include "units/units.h"
#include "cache/cache_files.h" /* port: (tag_iterator: light_port_glow_definition_get) */
void shield_glow_update(void); /* port: port/linux/game/shield_glow.c */
/* port: port/linux/src (halo.log) */
void platform_log(const char *format, ...);
#include "overshield_effect.h" /* port: port/linux/game/overshield_effect.c */
/* ---------- constants */
enum
{
_light_definition_dynamic_bit = 0,
_light_definition_no_specular_bit,
_light_definition_dont_light_own_object_bit,
_light_definition_supersize_in_first_person_bit,
_light_definition_is_first_person_flashlight_bit,
_light_definition_dont_fade_active_camouflage_bit,
};
enum
{
_point_light_dynamic_bit = 0,
_point_light_connects_to_map_bit = 1,
_point_light_connected_to_map_bit = 2,
_point_light_attached_to_first_person_weapon_bit = 3,
/* port: a glow the port colors and sizes itself (light_port_glow_set) */
_point_light_port_glow_bit = 4,
};
enum
{
MAXIMUM_CLUSTERS_PER_LIGHT = 512,
LENS_FLARE_FIRST_PERSON_MARKER_FLAG = FLAG(7),
};
enum
{
_distant_lighting_raycast_sideways_bit = 0,
_distant_lighting_block_on_textures_bit,
_distant_lighting_brighten_bit,
};
enum
{
NUMBER_OF_LIGHTMAP_SAMPLE_SIDEWAYS_RAYCASTS = 4,
};
enum
{
LENS_FLARE_DEFINITION_TAG = 'lens',
};
enum
{
_shader_type_environment = 3,
};
/* ---------- macros */
#define light_get(index) \
((struct light_datum *)datum_get(light_data, (index)))
#define light_definition_get(index) \
((struct point_light_definition *)tag_get(LIGHT_DEFINITION_TAG, (index)))
#define lens_flare_definition_get(index) \
((struct lens_flare_definition *)tag_get(LENS_FLARE_DEFINITION_TAG, (index)))
#define shader_environment_definition_get(shader) \
((struct shader_environment_definition *)shader_get_and_verify_type( \
(shader), \
_shader_type_environment))
#define structure_material_get_vertex(material, vertex_index) \
((struct environment_vertex_compressed const *)(material)->compressed_vertex_data.address \
+ (vertex_index))
#define structure_material_get_lightmap_vertex(material, vertex_index) \
((struct environment_lightmap_vertex_compressed const *) \
((struct environment_vertex_compressed const *)(material)->compressed_vertex_data.address \
+ (material)->vertices.count) \
+ (vertex_index))
/* ---------- structures */
struct environment_vertex_compressed
{
real_point3d position;
unsigned long normal;
unsigned long binormal;
unsigned long tangent;
real_point2d texcoord;
};
struct environment_lightmap_vertex_compressed
{
unsigned long incident_radiosity;
short lightmap_u;
short lightmap_v;
};
struct shader_environment_definition
{
struct shader shader;
byte reserved28[0x60];
struct tag_reference base_map;
};
struct lights_game_globals
{
boolean render_lights;
byte reserved01[3];
};
struct point_light_definition
{
long flags;
real radius;
real radius_modifier_lower_bound;
real radius_modifier_upper_bound;
real falloff_angle;
real cutoff_angle;
real lens_flare_radius;
real runtime_cosine_falloff_angle;
real runtime_cosine_cutoff_angle;
real specular_radius_multiplier;
real runtime_sine_cutoff_angle;
byte reserved2C[0x8];
unsigned long color_interpolation_flags;
real_argb_color color_lower_bound;
real_argb_color color_upper_bound;
byte reserved58[0x54];
struct tag_reference lens_flare;
byte reservedBC[0x38];
real transition_duration;
word reservedF8;
short falloff_function;
byte reservedFC[0x8];
};
struct light_datum
{
struct datum_header header;
word flags;
long definition_index;
long rasterizer_light_index;
long marker;
long cluster_reference;
real_rgb_color color;
byte reserved20[0xC];
long object_index;
real_point3d position;
real_vector3d forward;
real_vector3d up;
real radius;
long parent_light_index;
short attachment_marker_index;
short function_index;
union
{
short color_function_index;
struct
{
real_point3d relative_position;
real_vector3d relative_forward;
} node;
};
real intensity_scale;
};
struct rasterizer_lens_flare_submit_parameters
{
struct lens_flare_definition *definition;
real_point3d position;
unsigned long compressed_direction;
unsigned long compressed_up;
unsigned long compressed_light_color;
short light_identifier;
short light_index;
short lens_flare_index;
byte compressed_window_index;
byte compressed_light_scale;
long internal_occlusion_pixels;
};
struct lights_globals
{
boolean marker_initialized;
byte pad1[3];
long marker;
short scene_point_light_count;
short padA;
/* port: the native builds' (render.h: the Xbox's 128) */
long scene_point_lights[MAXIMUM_RENDERED_LIGHTS];
struct rasterizer_lens_flare_submit_parameters queued_lens_flares[8];
short queued_lens_flare_count;
short pad34E;
};
typedef char verify_lights_game_globals_size[
sizeof(struct lights_game_globals) == 0x4 ? 1 : -1];
typedef char verify_light_datum_flags_offset[
offsetof(struct light_datum, flags) == 0x2 ? 1 : -1];
typedef char verify_light_datum_cluster_reference_offset[
offsetof(struct light_datum, cluster_reference) == 0x10 ? 1 : -1];
typedef char verify_light_definition_lens_flare_offset[
offsetof(struct point_light_definition, lens_flare) == 0xAC ? 1 : -1];
typedef char verify_light_definition_color_offset[
offsetof(struct point_light_definition, color_interpolation_flags) == 0x34 ? 1 : -1];
typedef char verify_light_definition_transition_duration_offset[
offsetof(struct point_light_definition, transition_duration) == 0xF4 ? 1 : -1];
typedef char verify_light_definition_falloff_function_offset[
offsetof(struct point_light_definition, falloff_function) == 0xFA ? 1 : -1];
typedef char verify_light_datum_size[
sizeof(struct light_datum) == 0x7C ? 1 : -1];
typedef char verify_rasterizer_light_submit_parameters_size[
sizeof(struct rasterizer_light_submit_parameters) == 0x38 ? 1 : -1];
/* port: (scene_point_lights' size) */
typedef char verify_lights_globals_size[
sizeof(struct lights_globals) == 0x350 + 4 * (MAXIMUM_RENDERED_LIGHTS - 128) ? 1 : -1];
/* ---------- prototypes */
static boolean should_render_lights(
void);
static void light_marker_begin(
void);
static void light_marker_end(
void);
static boolean light_unmarked(
long light_index);
static boolean light_mark(
long light_index);
static long cluster_get_first_light(
long *reference_index,
short cluster_index);
static long cluster_get_next_light(
long *reference_index);
static void find_point_lights_for_object_in_cluster(
long object_index,
short cluster_index,
real_point3d const *center,
real radius,
long *light_indices,
real *light_intensities,
real *light_attenuations,
short *light_count,
short maximum_light_count);
static void shade_vector3d(
real_vector3d const *vertex0,
real_vector3d const *vertex1,
real_vector3d const *vertex2,
real b,
real c,
real_vector3d *result);
static void brighten_real_rgb_color(
real_rgb_color *color,
real fraction);
static void build_distant_lights(
long flags,
real_vector3d const *surface_normal,
real_rgb_color const *diffuse_color,
real_vector3d const *radiosity_normal,
real radiosity_accuracy,
real_rgb_color const *lightmap_color,
struct render_lighting *lighting);
static short light_build_cluster_array(
long light_index,
short maximum_count,
short *cluster_indices);
static void light_compute_bounding_sphere(
long light_index,
boolean maximum,
boolean specular,
boolean lens_flare_only,
real_point3d *position,
real *radius);
static void render_debug_light(
long light_index);
static void light_get_bounding_sphere(
long light_index,
real_point3d *position,
real *radius);
/* ---------- globals */
struct render_lighting const default_object_lighting =
{
{ 0.2f, 0.2f, 0.2f },
2,
0,
{
{ { 1.0f, 1.0f, 1.0f }, { -0.577f, -0.577f, -0.577f } },
{ { 0.4f, 0.4f, 0.5f }, { 0.0f, 0.0f, 1.0f } },
},
0,
0,
{ 0, 0 },
{ 0.5f, 1.0f, 1.0f, 1.0f },
{ 0.0f, 0.0f, -1.0f },
{ 0.0f, 0.0f, 0.0f },
};
static real_vector3d const lightmap_sample_raycast_down = { 0.0f, 0.0f, -10.0f };
static real_vector3d const lightmap_sample_raycast_sideways[NUMBER_OF_LIGHTMAP_SAMPLE_SIDEWAYS_RAYCASTS] =
{
{ -10.0f, 0.0f, 0.0f },
{ 10.0f, 0.0f, 0.0f },
{ 0.0f, -10.0f, 0.0f },
{ 0.0f, 10.0f, 0.0f },
};
static struct profile_section lights_section = { "render_lights", NONE, TRUE };
real object_light_ambient_base = 0.03f;
real object_light_ambient_scale = 0.4f;
real object_light_secondary_scale = 1.0f;
boolean object_light_interpolate = TRUE;
boolean debug_lights;
boolean debug_object_lights;
struct data_array *light_data;
struct cluster_partition light_cluster_partition;
/* port: the clusters' lights kept for lighting objects (lights_port_cluster):
as many as the light partition's lists can hold */
enum
{
LIGHTS_PORT_MAXIMUM_ENTRIES = HALO_PORT_MAXIMUM_CLUSTER_REFERENCES,
};
struct lights_port_entry
{
long light_index;
real_point3d position;
real radius;
};
static struct
{
/* each light's radius or rasterizer light written (lights_preprocess_scene) */
unsigned long radius_writes;
/* what the entries were kept for; each cluster's, and where they are */
unsigned long generation;
short entry_count;
unsigned long cluster_generations[MAXIMUM_CLUSTERS_PER_STRUCTURE];
short cluster_first_entries[MAXIMUM_CLUSTERS_PER_STRUCTURE];
short cluster_entry_counts[MAXIMUM_CLUSTERS_PER_STRUCTURE];
struct lights_port_entry entries[LIGHTS_PORT_MAXIMUM_ENTRIES];
} lights_port;
struct lights_game_globals *lights_game_globals = NULL;
short debug_rasterizer_light_count;
struct lights_globals lights_globals;
/* ---------- public code */
void *texture_cache_bitmap_load(
struct bitmap_data *bitmap)
{
void *hardware_format;
profile_texture_start();
hardware_format = _texture_cache_bitmap_get_hardware_format(
bitmap,
TRUE,
TRUE);
profile_texture_end();
return hardware_format;
}
static real shade_scalar(
real scalar0,
real scalar1,
real scalar2,
real b,
real c)
{
return (scalar1 - scalar0) * b + (scalar2 - scalar0) * c + scalar0;
}
static void shade_vector2d(
real_point2d const *vertex0,
real_point2d const *vertex1,
real_point2d const *vertex2,
real b,
real c,
real_point2d *result)
{
result->x = shade_scalar(vertex0->x, vertex1->x, vertex2->x, b, c);
result->y = shade_scalar(vertex0->y, vertex1->y, vertex2->y, b, c);
return;
}
static void shade_vector3d(
real_vector3d const *vertex0,
real_vector3d const *vertex1,
real_vector3d const *vertex2,
real b,
real c,
real_vector3d *result)
{
result->i = shade_scalar(vertex0->i, vertex1->i, vertex2->i, b, c);
result->j = shade_scalar(vertex0->j, vertex1->j, vertex2->j, b, c);
result->k = shade_scalar(vertex0->k, vertex1->k, vertex2->k, b, c);
return;
}
void sample_lightmap(
struct structure_material *material,
struct bitmap_data *bitmap,
struct structure_surface const *surface,
real s,
real t,
real_rgb_color *color)
{
real_point2d texcoords[3];
real_point2d texcoord;
match_assert(
"c:\\halo\\SOURCE\\objects\\object_lights.c",
143,
material->lightmap_vertices.type==_rasterizer_vertex_type_environment_lightmap_uncompressed || material->lightmap_vertices.type==_rasterizer_vertex_type_environment_lightmap_compressed);
environment_lightmap_vertex_compressed_get_texcoord(
structure_material_get_lightmap_vertex(material, surface->vertex_indices[0]),
&texcoords[0]);
environment_lightmap_vertex_compressed_get_texcoord(
structure_material_get_lightmap_vertex(material, surface->vertex_indices[1]),
&texcoords[1]);
environment_lightmap_vertex_compressed_get_texcoord(
structure_material_get_lightmap_vertex(material, surface->vertex_indices[2]),
&texcoords[2]);
shade_vector2d(
&texcoords[0],
&texcoords[1],
&texcoords[2],
s,
t,
&texcoord);
pixel32_to_real_rgb_color(
bitmap_2d_get_pixel(bitmap, &texcoord, 1.0f),
color);
return;
}
void sample_diffuse_texture(
struct structure_material *material,
struct bitmap_data *bitmap,
struct structure_surface const *surface,
real s,
real t,
real_rgb_color *color)
{
real_point2d texcoords[3];
real_point2d texcoord;
match_assert(
"c:\\halo\\SOURCE\\objects\\object_lights.c",
167,
material->vertices.type==_rasterizer_vertex_type_environment_uncompressed || material->vertices.type==_rasterizer_vertex_type_environment_compressed);
environment_vertex_compressed_get_texcoord(
structure_material_get_vertex(material, surface->vertex_indices[0]),
&texcoords[0]);
environment_vertex_compressed_get_texcoord(
structure_material_get_vertex(material, surface->vertex_indices[1]),
&texcoords[1]);
environment_vertex_compressed_get_texcoord(
structure_material_get_vertex(material, surface->vertex_indices[2]),
&texcoords[2]);
shade_vector2d(
&texcoords[0],
&texcoords[1],
&texcoords[2],
s,
t,
&texcoord);
pixel32_to_real_rgb_color(
bitmap_2d_get_pixel(bitmap, &texcoord, 0.3f),
color);
return;
}
static boolean should_render_lights(
void)
{
if (lights_game_globals->render_lights && game_engine_allow_dynamic_lighting())
{
return TRUE;
}
return FALSE;
}
void lights_initialize(
void)
{
light_data = game_state_data_new(
"lights",
MAXIMUM_LIGHTS_PER_MAP,
sizeof(struct light_datum));
lights_game_globals = game_state_malloc(
"lights globals",
NULL,
sizeof(struct lights_game_globals));
match_assert(
"c:\\halo\\SOURCE\\objects\\object_lights.c",
0xC2,
light_data);
match_assert(
"c:\\halo\\SOURCE\\objects\\object_lights.c",
0xC3,
lights_game_globals);
lights_game_globals->render_lights = TRUE;
if (light_data)
{
cluster_partition_new(&light_cluster_partition, "light");
}
else
{
error(_error_silent, "couldn't allocate memory for object lights.");
}
return;
}
void lights_dispose(
void)
{
overshield_effect_reset(); /* port: queued identities and model heights belong to this map. */
cluster_partition_delete(&light_cluster_partition);
return;
}
/* port: glows the port lights itself (port/linux/game/shield_glow.c): an
unattached light (light_new_unattached) on an object's node, kept alive and
colored and sized each frame by its maker, gone a moment after its maker
stops. Drawn with a light definition the map has (one without a lens flare
or a flashlight's flags, a plasma one where there is one), so no new tags;
NONE if it has none */
static long light_port_glow_definition = NONE - 1;
long light_port_glow_definition_get(
void)
{
if (light_port_glow_definition == NONE - 1)
{
struct tag_iterator iterator;
long tag_index;
long found = NONE;
tag_iterator_new(&iterator, LIGHT_DEFINITION_TAG);
for (tag_index = tag_iterator_next(&iterator); tag_index != NONE; tag_index = tag_iterator_next(&iterator))
{
struct point_light_definition *definition = light_definition_get(tag_index);
if (!definition || definition->lens_flare.index != NONE ||
TEST_FLAG(definition->flags, _light_definition_is_first_person_flashlight_bit))
{
continue;
}
found = tag_index;
if (strstr(tag_get_name(tag_index), "plasma"))
break;
}
light_port_glow_definition = found;
}
return light_port_glow_definition;
}
/* port: a glow's light (one made with that definition, on that object) its
color and radius for now; FALSE if it is gone */
boolean light_port_glow_set(
long light_index,
long object_index,
real_rgb_color const *color,
real radius)
{
struct light_datum *light;
if (light_index == NONE || !light_data->valid)
return FALSE;
light = (struct light_datum *)datum_try_and_get(light_data, light_index);
if (!light || light->definition_index != light_port_glow_definition || light->object_index != object_index)
return FALSE;
SET_FLAG(light->flags, _point_light_port_glow_bit, TRUE);
light->parent_light_index = game_time_get(); /* (alive another moment) */
light->color.red = PIN(color->red, 0.0f, 1.0f);
light->color.green = PIN(color->green, 0.0f, 1.0f);
light->color.blue = PIN(color->blue, 0.0f, 1.0f);
light->intensity_scale = radius;
return TRUE;
}
void lights_initialize_for_new_map(
void)
{
overshield_effect_reset(); /* port: queued identities and model heights belong to this map. */
light_port_glow_definition = NONE - 1; /* port: (looked for again) */
data_make_valid(light_data);
lights_game_globals->render_lights = TRUE;
cluster_partition_make_valid(&light_cluster_partition);
return;
}
void lights_dispose_from_old_map(
void)
{
overshield_effect_reset(); /* port: queued identities and model heights belong to this map. */
data_make_invalid(light_data);
cluster_partition_make_invalid(&light_cluster_partition);
return;
}
/* port: the lights array out of order in a map (game_state.c's
game_state_check_data_arrays reports how): Sentry's NATIVE-7 found it made
for no map, every light lost, and the next light made or looked up crashed.
Lights are only seen, so they all go: the array is made again, valid and
empty, its clusters' references with it, and the objects let go of the
lights they had (their attachments, light_delete and
object_get_self_illumination take none for lost). Lights made from then on
are as ever. Whether it did. */
boolean lights_port_recover(
void)
{
struct data_array *data = light_data;
struct object_iterator iterator;
struct object_datum *object;
if (data->signature == 'd@t@' && data->data == (void *)(data + 1) && data->valid && data->next_identifier &&
data->maximum_count == MAXIMUM_LIGHTS_PER_MAP && data->size == sizeof(struct light_datum) &&
data->count >= 0 && data->count <= data->maximum_count &&
data->actual_count >= 0 && data->actual_count <= data->count &&
data->first_free_absolute_index >= 0 && data->first_free_absolute_index <= data->maximum_count)
{
return FALSE;
}
/* (how, and since when) */
game_state_check_data_arrays();
data_initialize(data, "lights", MAXIMUM_LIGHTS_PER_MAP, sizeof(struct light_datum));
lights_initialize_for_new_map();
object_iterator_new(&iterator, _object_mask_all, 0);
while ((object = object_iterator_next(&iterator)) != NULL)
{
struct object_definition *definition = object_definition_get(object->definition_index);
short attachment_count = (short)MIN(definition->object.attachments.count, MAXIMUM_NUMBER_OF_ATTACHMENTS_PER_OBJECT);
short attachment_index;
for (attachment_index = 0; attachment_index < attachment_count; attachment_index++)
{
if (object->object.attachment_types[attachment_index] == _object_attachment_type_light)
object->object.attachment_indices[attachment_index] = NONE;
}
}
platform_log("lights: the lights were out of order and all of them were let go");
return TRUE;
}
boolean lights_enable(
boolean enable)
{
lights_game_globals->render_lights = enable;
return enable;
}
long light_new(
long definition_index,
long object_index,
short object_attachment_index,
short object_function_index,
short object_change_color_index)
{
struct point_light_definition *definition = light_definition_get(definition_index);
long light_index = NONE;
if (TEST_FLAG(definition->flags, _light_definition_dynamic_bit)
|| definition->lens_flare.index != NONE)
{
light_index = datum_new(light_data);
/* port: no light if the new one can't be had (data.c's data_usable) */
if (light_index != NONE && !datum_try_and_get(light_data, light_index))
light_index = NONE;
if (light_index != NONE)
{
struct light_datum *light = light_get(light_index);
light->definition_index = definition_index;
light->object_index = object_index;
light->attachment_marker_index = object_attachment_index;
/* port: a function and a change color the object holds, or none
(an attachment's references in a map; retail's are none to d for
the function, none to b for the color) */
light->function_index = object_function_index;
light->color_function_index = object_change_color_index;
if ((object_function_index!=NONE && !VALID_INDEX(object_function_index, NUMBER_OF_OUTGOING_OBJECT_FUNCTIONS)) ||
(object_change_color_index!=NONE && !VALID_INDEX(object_change_color_index, NUMBER_OF_OBJECT_CHANGE_COLORS)))
{
static boolean reference_reported = FALSE;
if (!reference_reported)
{
reference_reported = TRUE;
error(
_error_silent,
"### ERROR light %s is attached with function #%d and change color #%d; the bad one is none",
tag_get_name(definition_index),
object_function_index,
object_change_color_index);
}
if (!VALID_INDEX(object_function_index, NUMBER_OF_OUTGOING_OBJECT_FUNCTIONS))
{
light->function_index = NONE;
}
if (!VALID_INDEX(object_change_color_index, NUMBER_OF_OBJECT_CHANGE_COLORS))
{
light->color_function_index = NONE;
}
}
light->flags = 0;
SET_FLAG(light->flags, _point_light_dynamic_bit,
TEST_FLAG(definition->flags, _light_definition_dynamic_bit));
SET_FLAG(light->flags, _point_light_connects_to_map_bit,
TEST_FLAG(light->flags, _point_light_dynamic_bit)
|| definition->lens_flare.index != NONE);
light->cluster_reference = NONE;
light->parent_light_index = NONE;
light_reconnect_to_map(light_index);
light->marker = lights_globals.marker - 1;
}
}
return light_index;
}
long light_new_unattached(
long definition_index,
long object_index,
short object_node_index,
real_point3d const *position,
real_vector3d const *direction,
real scale)
{
long light_index = datum_new(light_data);
if (light_index != NONE)
{
struct light_datum *light = light_get(light_index);
struct point_light_definition *definition = light_definition_get(definition_index);
light->flags = 0;
light->parent_light_index = game_time_get();
light->definition_index = definition_index;
light->object_index = object_index;
light->intensity_scale = scale;
light->cluster_reference = NONE;
SET_FLAG(light->flags, _point_light_dynamic_bit, TRUE);
SET_FLAG(light->flags, _point_light_connects_to_map_bit, TRUE);
if (object_index == NONE)
{
light->position = *position;
light->forward = *direction;
}
else
{
light->attachment_marker_index = object_node_index;
light->node.relative_position = *position;
light->node.relative_forward = *direction;
}
light_reconnect_to_map(light_index);
light->marker = lights_globals.marker - 1;
}
return light_index;
}
void lights_preprocess_scene(
void)
{
long current_time = game_time_get();
long light_index;
short rendered_cluster_index;
short scene_light_index;
short queued_lens_flare_index;
profile_enter(lights_section);
debug_rasterizer_light_count = 0;
/* port: the lights in order before they are drawn (a frame can come
between ticks: lights_port_recover) */
lights_port_recover();
shield_glow_update(); /* port: port/linux/game/shield_glow.c */
for (light_index = data_next_index(light_data, NONE);
light_index != NONE;
light_index = data_next_index(light_data, light_index))
{
struct light_datum *light = light_get(light_index);
SET_FLAG(
light->flags,
_point_light_attached_to_first_person_weapon_bit,
FALSE);
light->rasterizer_light_index = NONE;
lights_port.radius_writes++;
if (light->parent_light_index != NONE)
{
struct point_light_definition *definition = light_definition_get(
light->definition_index);
real elapsed = (real)(current_time - light->parent_light_index);