-
Notifications
You must be signed in to change notification settings - Fork 30
/
lights.c
executable file
·1572 lines (1391 loc) · 43.2 KB
/
lights.c
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
/*===================================================================
* l i g h t s . c
* All routines to do with Lights...
===================================================================*/
#include <stdio.h>
#include "new3d.h"
#include "quat.h"
#include "compobjects.h"
#include "bgobjects.h"
#include "object.h"
#include "networking.h"
#include "lights.h"
#include "water.h"
#include "visi.h"
#include "text.h"
#include "main.h"
#include "util.h"
#ifdef OPT_ON
#pragma optimize( "gty", on )
#endif
#undef TESTING_SUBTRACTIVE
/*===================================================================
Externals...
===================================================================*/
extern bool ShowPlaneRGB;
extern float WhiteOut;
extern u_int16_t NumGroupsVisible;
extern u_int16_t GroupsVisible[MAXGROUPS];
extern int16_t ShowPortal;
extern int NumOfVertsConsidered;
extern int NumOfVertsTouched;
extern float framelag;
extern float SoundInfo[MAXGROUPS][MAXGROUPS];
extern u_int16_t GroupWaterInfo[MAXGROUPS];
extern float GroupWaterLevel[MAXGROUPS];
extern float GroupWaterIntensity_Red[MAXGROUPS];
extern float GroupWaterIntensity_Green[MAXGROUPS];
extern float GroupWaterIntensity_Blue[MAXGROUPS];
extern bool BrightShips;
extern CAMERA CurrentCamera;
void PrintInitViewStatus( BYTE Status );
/*===================================================================
Globals...
===================================================================*/
COLOR GroupColours[ 8 ] = {
RGBA_MAKE( 0,128,0,255 ),
RGBA_MAKE( 0,0,128,255 ),
RGBA_MAKE( 0,128,128,255 ),
RGBA_MAKE( 128,128,0,255 ),
RGBA_MAKE( 128,0,128,255 ),
RGBA_MAKE( 128,128,128,255 ),
RGBA_MAKE( 64,128,64,255 ),
RGBA_MAKE( 128,64,64,255 )
};
XLIGHT * FirstLightVisible = NULL;
XLIGHT XLights[MAXXLIGHTS];
u_int16_t FirstXLightUsed;
u_int16_t FirstXLightFree;
WORD status;
DWORD chop_status;
/*===================================================================
Floating Point Cull Mode
===================================================================*/
#ifdef USEASM
__inline
void start_chop(void)
{
__asm
{
fstcw word ptr status
mov eax,dword ptr status
or eax,3072
mov dword ptr chop_status,eax
fldcw word ptr chop_status
}
}
__inline
void end_chop(void)
{
__asm
{
fldcw word ptr status
}
}
#endif
/*===================================================================
Procedure : Set up And Init all XLights
Input : nothing
Output : nothing
===================================================================*/
void InitXLights()
{
u_int16_t i;
FirstXLightUsed = (u_int16_t) -1;
FirstXLightFree = 0;
for( i = 0 ; i < MAXXLIGHTS ; i++ )
{
XLights[i].Index = i;
XLights[i].Next = i + 1;
XLights[i].Prev = (u_int16_t) -1;
XLights[i].Type = POINT_LIGHT;
}
XLights[MAXXLIGHTS-1].Next = (u_int16_t) -1;
}
/*===================================================================
Procedure : Find a free light and move it from the free list to
the used list
Input : nothing
Output : u_int16_t number of light free....
===================================================================*/
u_int16_t FindFreeXLight()
{
u_int16_t i;
i = FirstXLightFree;
if ( i == (u_int16_t) -1)
return i;
XLights[i].Prev = FirstXLightUsed;
if ( FirstXLightUsed != (u_int16_t) -1)
{
XLights[FirstXLightUsed].Next = i;
}
FirstXLightUsed = i;
FirstXLightFree = XLights[i].Next;
XLights[i].Type = POINT_LIGHT;
XLights[i].Visible = true;
return i ;
}
/*===================================================================
Procedure : Kill a used light and move it from the used list to
the free list
Input : u_int16_t number of light free....
Output : nothing
===================================================================*/
void KillUsedXLight( u_int16_t light )
{
u_int16_t its_prev;
u_int16_t its_next;
its_prev = XLights[light].Prev;
its_next = XLights[light].Next;
if ( light == FirstXLightUsed )
FirstXLightUsed = XLights[light].Prev;
if( its_prev != (u_int16_t) -1)
XLights[its_prev].Next = its_next;
if( its_next != (u_int16_t) -1)
XLights[its_next].Prev = its_prev;
XLights[light].Prev = (u_int16_t) -1;
XLights[light].Next = FirstXLightFree;
FirstXLightFree = light;
}
/*===================================================================
Procedure : Process all XLights
Input : nothing
Output : nothing
===================================================================*/
bool ProcessXLights( MLOADHEADER * Mloadheader )
{
u_int16_t light;
u_int16_t oldlight;
light = FirstXLightUsed;
while( light != (u_int16_t ) -1 )
{
oldlight = XLights[light].Prev;
if( XLights[light].SizeCount != 0.0F )
{
XLights[light].Size -= XLights[light].SizeCount * framelag;
XLights[light].r -= XLights[light].ColorCount * framelag;
XLights[light].g -= XLights[light].ColorCount * framelag;
XLights[light].b -= XLights[light].ColorCount * framelag;
if( XLights[light].r <= 0.0F )
XLights[light].r = 0.0F;
if( XLights[light].g <= 0.0F )
XLights[light].g = 0.0F;
if( XLights[light].b <= 0.0F )
XLights[light].b = 0.0F;
if( XLights[light].Size <= 0.0F)
KillUsedXLight(light);
}
light = oldlight;
}
return true;
}
/*===================================================================
* cause a light to go red and get smaller then die...
===================================================================*/
void SetLightDie ( u_int16_t light )
{
XLights[light].SizeCount = 50.0F;
XLights[light].ColorCount = 2.0F;
XLights[light].r = 255.0F;
XLights[light].g = 0.0F;
XLights[light].b = 0.0F;
}
/*===================================================================
Procedure : Xlight 1 Group Only...
Input : nothing
Output : nothing
===================================================================*/
float cral = 0.0F;
bool XLight1Group( MLOADHEADER * Mloadheader, u_int16_t group )
{
XLIGHT * XLightPnt;
float blf;
float glf;
float rlf;
VECTOR Temp;
VECTOR CellIndex;
float distance;
int execbuf;
int vert;
LPLVERTEX lpPointer = NULL;
LPLVERTEX lpLVERTEX = NULL;
LPLVERTEX lpLVERTEX2 = NULL;
COLOR col;
VERTEXCELL * VertexCellPnt;
u_int16_t * VertexIndexPnt;
u_int16_t * OrgVertexIndexPnt;
int Cell;
int CellIndex_x;
int CellIndex_y;
int CellIndex_z;
int CellRange_x;
int CellRange_y;
int CellRange_z;
int Cellx;
int Celly;
int Cellz;
int NumOfxCells;
int NumOfyCells;
int NumOfzCells;
float CellSize;
float Size,OSize;
float SizeX2;
float x,y,z;
float Posx,Posy,Posz;
float Dirx = 0.0f, Diry = 0.0f, Dirz = 0.0f;
float Cosa,CosArc = 0.0f;
float rlen;
float intense;
u_int32_t tempiR;
u_int32_t tempiG;
u_int32_t tempiB;
u_int32_t tempiA;
float centerx;
float centery;
float centerz;
float half_sizex;
float half_sizey;
float half_sizez;
u_int32_t inc;
u_int32_t carry;
u_int32_t clamp;
u_int32_t r,g,b,intWhiteOut;
POLYANIM * PolyAnim;
int i,e;
u_int32_t * u_int32Pnt;
TANIMUV * TanimUV;
float intensity;
#ifdef NEW_LIGHTING
render_lighting_enabled = 1;
render_lighting_point_lights_only = 0;
if( WhiteOut != 0.0f )
render_lighting_env_whiteout = (int) WhiteOut;
if(GroupWaterInfo[group] != WATERSTATE_NOWATER)
{
render_lighting_env_water = 1;
if( GroupWaterInfo[group] != WATERSTATE_ALLWATER )
{
render_lighting_env_water = 2;
render_lighting_env_water_level = GroupWaterLevel[group];
}
render_lighting_env_water_red = GroupWaterIntensity_Red[group];
render_lighting_env_water_green = GroupWaterIntensity_Green[group];
render_lighting_env_water_blue= GroupWaterIntensity_Blue[group];
}
return true;
#endif
intWhiteOut = (int)WhiteOut;
if( intWhiteOut >= 256 )
{
intWhiteOut = (256 - (intWhiteOut-256) );
}
CellSize = Mloadheader->CellSize;
execbuf = Mloadheader->Group[group].num_execbufs;
while( execbuf--)
{
if (!(FSLockVertexBuffer((RENDEROBJECT*)&Mloadheader->Group[group].renderObject[execbuf], &lpPointer)))
{
return false;
}
// lpPointer = (LPLVERTEX) debDesc.lpData;
VertexCellPnt = Mloadheader->Group[group].vertex_cell_pnt[execbuf];
OrgVertexIndexPnt = Mloadheader->Group[group].vertex_index_pnt[execbuf];
NumOfxCells = Mloadheader->Group[group].xcells[execbuf];
NumOfyCells = Mloadheader->Group[group].ycells[execbuf];
NumOfzCells = Mloadheader->Group[group].zcells[execbuf];
if( Mloadheader->Group[group].num_animating_polys[execbuf] != 0 )
{
PolyAnim = Mloadheader->Group[group].polyanim[execbuf];
for( i = 0 ; i < Mloadheader->Group[group].num_animating_polys[execbuf] ; i++ )
{
if( PolyAnim->currentframe != PolyAnim->newframe )
{
// something has changed....
u_int32Pnt = (u_int32_t*)PolyAnim->vert;
for( e = 0 ; e < PolyAnim->vertices ; e++ )
{
lpLVERTEX = lpPointer+ *u_int32Pnt++;
TanimUV = PolyAnim->UVs;
TanimUV += e + (PolyAnim->vertices * PolyAnim->newframe);
lpLVERTEX->tu = TanimUV->u;
lpLVERTEX->tv = TanimUV->v;
}
PolyAnim->currentframe = PolyAnim->newframe;
}
PolyAnim++;
}
}
lpLVERTEX = lpPointer;
{
lpLVERTEX2 = Mloadheader->Group[group].originalVerts[execbuf];
vert = Mloadheader->Group[group].num_verts_per_execbuf[execbuf];
if( GroupWaterInfo[group] == WATERSTATE_NOWATER || ShowPlaneRGB )
{
if( WhiteOut == 0.0F )
{
#ifdef USEASM
__asm
{
mov eax, DWORD PTR ShowPlaneRGB
or eax, eax
jnz useplanergb
mov esi, [lpLVERTEX2]
add esi,16
jmp go
useplanergb:
mov esi, [lpLVERTEX2]
add esi,12
go:
mov edi, [lpLVERTEX]
add edi,16
mov ecx, vert
clear: mov eax, [esi]
add esi, 32
mov [edi],eax
add edi, 32
dec ecx
jnz clear
}
#else //USEASM
if ( ShowPlaneRGB )
{
while( vert --)
{
// lpLVERTEX->color = (COLOR) lpLVERTEX2->dwReserved;
lpLVERTEX++;
lpLVERTEX2++;
}
}
else
{
while( vert --)
{
lpLVERTEX->color = lpLVERTEX2->color;
lpLVERTEX++;
lpLVERTEX2++;
}
}
#endif //USEASM
}
else
{
// Special Lighting effects
while( vert --)
{
x = (float)((int) (lpLVERTEX2->x * 0.35F) % 360);
y = (float)((int) (lpLVERTEX2->y * 0.35F) % 360);
z = (float)((int) (lpLVERTEX2->z * 0.35F) % 360);
// distance = (float) sqrt( (x*x) + (y*y) + (z*z) );
col = lpLVERTEX2->color;
col &= 0xffff;
// tal = 128.0F + 127.0F * (float) sin( (distance+cral) * (PI / 180.0F ) );
b = (int) ( ( sin( D2R( x + cral ) ) + sin( D2R( y + cral ) ) + sin( D2R( z + cral ) ) ) * 127.0F * 0.3333333F + 128.0F ) ;
b+= intWhiteOut;
if( b > 255 )
b = 255;
col |= (b<<24)+(b<<16);
lpLVERTEX->color = col;
lpLVERTEX++;
lpLVERTEX2++;
}
}
}else if( GroupWaterInfo[group] == WATERSTATE_ALLWATER )
{
// ****************** Full Water Effect ********************************
lpLVERTEX2 = Mloadheader->Group[group].originalVerts[execbuf];
lpLVERTEX = lpPointer;
vert = Mloadheader->Group[group].num_verts_per_execbuf[execbuf];
// Special Lighting effects
while( vert --)
{
x = (float)((int) (lpLVERTEX2->x * 0.35F) % 360);
y = (float)((int) (lpLVERTEX2->y * 0.35F) % 360);
z = (float)((int) (lpLVERTEX2->z * 0.35F) % 360);
col = lpLVERTEX2->color;
r = RGBA_GETRED(col);
g = RGBA_GETGREEN(col);
b = RGBA_GETBLUE(col);
r >>=2;
g >>=2;
b >>=2;
intensity = (float) ( ( sin( D2R( x + cral ) ) + sin( D2R( y + cral ) ) + sin( D2R( z + cral ) ) ) * 127.0F * 0.3333333F + 128.0F ) ;
r += (int) (GroupWaterIntensity_Red[group] * intensity);
if( r > 255 )
r = 255;
g += (int) (GroupWaterIntensity_Green[group] * intensity);
if( g > 255 )
g = 255;
b += (int) (GroupWaterIntensity_Blue[group] * intensity);
if( b > 255 )
b = 255;
lpLVERTEX->color = RGBA_MAKE( r ,g ,b , 128 );
lpLVERTEX++;
lpLVERTEX2++;
}
// ****************** End of Water Effect ******************************
}else{
// ****************** Partial Water Effect ******************************
lpLVERTEX2 = Mloadheader->Group[group].originalVerts[execbuf];
lpLVERTEX = lpPointer;
vert = Mloadheader->Group[group].num_verts_per_execbuf[execbuf];
// Special Lighting effects
while( vert --)
{
if( lpLVERTEX2->y < GroupWaterLevel[group] )
{
x = (float)((int) (lpLVERTEX2->x * 0.35F) % 360);
y = (float)((int) (lpLVERTEX2->y * 0.35F) % 360);
z = (float)((int) (lpLVERTEX2->z * 0.35F) % 360);
col = lpLVERTEX2->color;
r = RGBA_GETRED(col);
g = RGBA_GETGREEN(col);
b = RGBA_GETBLUE(col);
r >>=2;
g >>=2;
b >>=2;
intensity = (float) ( ( sin( D2R( x + cral ) ) + sin( D2R( y + cral ) ) + sin( D2R( z + cral ) ) ) * 127.0F * 0.3333333F + 128.0F ) ;
r += (int) (GroupWaterIntensity_Red[group] * intensity);
if( r > 255 )
r = 255;
g += (int) (GroupWaterIntensity_Green[group] * intensity);
if( g > 255 )
g = 255;
b += (int) (GroupWaterIntensity_Blue[group] * intensity);
if( b > 255 )
b = 255;
lpLVERTEX->color = RGBA_MAKE( r ,g ,b , 128 );
}else{
lpLVERTEX->color = lpLVERTEX2->color;
}
lpLVERTEX++;
lpLVERTEX2++;
}
// ****************** End of Water Effect ******************************
}
}
if( WhiteOut == 0.0F )
{
centerx = Mloadheader->Group[group].center.x;
centery = Mloadheader->Group[group].center.y;
centerz = Mloadheader->Group[group].center.z;
half_sizex = Mloadheader->Group[group].half_size.x;
half_sizey = Mloadheader->Group[group].half_size.y;
half_sizez = Mloadheader->Group[group].half_size.z;
XLightPnt = FirstLightVisible;
while( XLightPnt )
{
if( GroupsAreVisible( group, XLightPnt->Group ) )
{
Posx = XLightPnt->Pos.x;
Posy = XLightPnt->Pos.y;
Posz = XLightPnt->Pos.z;
OSize = XLightPnt->Size;
Temp.x = Posx - centerx;
if( Temp.x < 0.0F ) Temp.x *= -1.0F;
Temp.y = Posy - centery;
if( Temp.y < 0.0F ) Temp.y *= -1.0F;
Temp.z = Posz - centerz;
if( Temp.z < 0.0F ) Temp.z *= -1.0F;
if ( (Temp.x <= ( half_sizex + OSize ) ) &&
(Temp.y <= ( half_sizey + OSize ) ) &&
(Temp.z <= ( half_sizez + OSize ) ) )
{
SizeX2 = OSize * OSize;
Size = 1 / SizeX2;
rlf = XLightPnt->r;
glf = XLightPnt->g;
blf = XLightPnt->b;
/* bjd curr driver = 0 use to be software mode
if(!render_info.CurrDriver ) // is it ramp mode..
{
rlf = ( rlf+glf+blf ) * 0.33333F;
glf = rlf;
blf = glf;
}
*/
if( XLightPnt->Type == SPOT_LIGHT )
{
Dirx = XLightPnt->Dir.x;
Diry = XLightPnt->Dir.y;
Dirz = XLightPnt->Dir.z;
CosArc = XLightPnt->CosArc;
}
CellIndex.x = Posx - Mloadheader->Group[group].cell_origin[execbuf].x;
CellIndex.y = Posy - Mloadheader->Group[group].cell_origin[execbuf].y;
CellIndex.z = Posz - Mloadheader->Group[group].cell_origin[execbuf].z;
CellIndex_x = (int) floor( (CellIndex.x - OSize) * CellSize );
CellIndex_y = (int) floor( (CellIndex.y - OSize) * CellSize );
CellIndex_z = (int) floor( (CellIndex.z - OSize) * CellSize );
CellRange_x = (int) floor( (CellIndex.x + OSize) * CellSize );
CellRange_y = (int) floor( (CellIndex.y + OSize) * CellSize );
CellRange_z = (int) floor( (CellIndex.z + OSize) * CellSize );
if( CellIndex_x < 0 )
{
CellIndex_x = 0;
}else{
if( CellIndex_x >= NumOfxCells )
CellIndex_x = NumOfxCells-1;
}
if( CellIndex_y < 0 )
{
CellIndex_y = 0;
}else{
if( CellIndex_y >= NumOfyCells )
CellIndex_y = NumOfyCells-1;
}
if( CellIndex_z < 0 )
{
CellIndex_z = 0;
}else{
if( CellIndex_z >= NumOfzCells )
CellIndex_z = NumOfzCells-1;
}
if( CellRange_x < 0 )
{
CellRange_x = 0;
}else{
if( CellRange_x >= NumOfxCells )
CellRange_x = NumOfxCells-1;
}
if( CellRange_y < 0 )
{
CellRange_y = 0;
}else{
if( CellRange_y >=NumOfyCells )
CellRange_y = NumOfyCells-1;
}
if( CellRange_z < 0 )
{
CellRange_z = 0;
}else{
if( CellRange_z >= NumOfzCells )
CellRange_z = NumOfzCells-1;
}
switch( XLightPnt->Type )
{
case POINT_LIGHT:
Cellz = CellIndex_z;
#ifdef USEASM
start_chop();
#endif //USEASM
while( Cellz <= CellRange_z )
{
Celly = CellIndex_y;
while( Celly <= CellRange_y )
{
Cell = ( CellIndex_x + NumOfxCells *
( Celly + NumOfyCells *
Cellz ) );
Cellx = CellRange_x - CellIndex_x + 1;
while( Cellx-- )
{
VertexIndexPnt = OrgVertexIndexPnt+VertexCellPnt[Cell].start_vert_in_cell;
vert = VertexCellPnt[Cell].num_verts_in_cell;
while( vert-- )
{
// NumOfVertsConsidered++;
lpLVERTEX = lpPointer + *VertexIndexPnt++;
/* find the distance from vert to light */
x = lpLVERTEX->x;
y = lpLVERTEX->y;
z = lpLVERTEX->z;
x -= Posx;
y -= Posy;
z -= Posz;
distance = (x*x) + (y*y) + (z*z);
// distance = 0.0F;
if ( distance < SizeX2 ) // float
{
// NumOfVertsTouched++;
distance = 1.0F - ( distance * Size ); // float
#ifdef USEASM
__asm
{
#if 1
fld rlf ;float load rlf
fmul distance ;float mul distance 2
fld blf ;float load blf
fmul distance ;float mul distance 1
fld glf ;float load glf
fmul distance ;float mul distance 0
fxch st(2)
fistp tempiR ;float int store tempiR
fxch st(1)
fistp tempiG ;float int store tempiG
fxch st(0)
fistp tempiB ;float int store tempiG
mov esi , [lpLVERTEX];set up the pointer
mov ecx , [esi+16] ;int ecx = col get the color
mov edi , ecx ; edi = col..
and ecx , 0x00ffffff ; and out the alpha
and edi , 0xff000000 ; keep the alpha for later
mov ebx , tempiR ;move the red into inc
mov edx , ecx ;edx = col
shl ebx , 8 ;shift it up 8 bits
or ebx , tempiG ;or in the green
shl ebx , 8 ;shift it up 8 bits
or ebx , tempiB ;or in the Blue
#else
fld rlf ;float load rlf
fmul distance ;float mul distance
mov esi , [lpLVERTEX];set up the pointer
fistp tempiR ;float int store tempiR
mov ecx , [esi+16] ;int ecx = col get the color
fld glf ;float load glf
fmul distance ;float mul distance
mov edi , ecx ; edi = col..
and ecx , 0x00ffffff ; and out the alpha
and edi , 0xff000000 ; keep the alpha for later
mov ebx , tempiR ;move the red into inc
fistp tempiG ;float int store tempiG
shl ebx , 8 ;shift it up 8 bits
fld blf ;float load blf
fmul distance ;float mul distance
or ebx , tempiG ;or in the green
mov edx , ecx ;edx = col
fistp tempiB ;float int store tempiG
shl ebx , 8 ;shift it up 8 bits
or ebx , tempiB ;or in the Blue
#endif
add ecx , ebx ;ecx = col + inc
xor edx , ebx ;edx = col ^ inc
xor edx , ecx ;edx = ( col + inc ) ^ ( col ^ inc )
and edx , 0x01010100 ;edx = carry = ( ( col + inc ) ^ ( col ^ inc ) ) & 0x01010100
mov eax , edx ;eax = carry
shr edx , 8 ;edx = ( carry >> 8 )
sub eax , edx ;eax = clamp = carry - ( carry >> 8 )
sub ecx , edx ;ecx = (col + inc) - carry
or ecx , eax ;col = ecx | clamp
or ecx , edi ; or back in the alpha..
mov [esi+16] , ecx ;int put the color back
// carry = ( ( col + inc ) ^ ( col ^ inc ) ) & 0x01010100;
// clamp = carry - ( carry >> 8 );
// col = ( col + inc - carry ) | clamp;
}
#else //USEASM
col = lpLVERTEX->color;
#ifdef TESTING_SUBTRACTIVE
tempiA = col & 0xff000000;
// for subtraction, simply add the negative
tempiR = ( (int) -( rlf * distance ) ) & 0xFF;
tempiG = ( (int) -( glf * distance ) ) & 0xFF;
tempiB = ( (int) -( blf * distance ) ) & 0xFF;
// so far so much nearly the same...
inc = ( tempiR << 16 ) + ( tempiG << 8 ) + tempiB;
carry = ( ( col + inc ) ^ ( col ^ inc ) ) & 0x01010100;
col = col + inc - carry;
// set the carry also for those channels where we are subtracting 0 (this seems wrong!)
// where 0 components are detected by inverting the inc
// adding 1, and checking the carry out
inc = inc ^ 0x00FFFFFF;
carry |= ( ( inc + 0x00010101) ^ ( inc ^ 0x00010101 ) ) & 0x01010100;
// ...and now it's nearly the same as for additive saturation
clamp = carry - ( carry >> 8 );
col = ( col & clamp ) & 0x00ffffff;
col |= tempiA;
#else
tempiA = col & 0xff000000;
tempiR = (int) ( rlf * distance );
tempiG = (int) ( glf * distance );
tempiB = (int) ( blf * distance );
inc = ( tempiR << 16 ) + ( tempiG << 8 ) + tempiB;
carry = ( ( col + inc ) ^ ( col ^ inc ) ) & 0x01010100;
clamp = carry - ( carry >> 8 );
col = ( ( col + inc - carry ) | clamp) & 0x00ffffff;
col |= tempiA;
#endif
lpLVERTEX->color = col;
#endif //USEASM
}
}
Cell++;
}
Celly++;
}
Cellz++;
}
#ifdef USEASM
end_chop();
#endif
break;
case SPOT_LIGHT:
Cellz = CellIndex_z;
while( Cellz <= CellRange_z )
{
Celly = CellIndex_y;
while( Celly <= CellRange_y )
{
Cell = (u_int16_t) ( CellIndex_x + NumOfxCells *
( Celly + NumOfyCells *
Cellz ) );
Cellx = CellRange_x - CellIndex_x + 1;
while( Cellx-- )
{
VertexIndexPnt = OrgVertexIndexPnt+VertexCellPnt[Cell].start_vert_in_cell;
vert = VertexCellPnt[Cell].num_verts_in_cell;
while( vert-- )
{
// NumOfVertsConsidered++;
// lpLVERTEX = ((LPLVERTEX ) lpPointer) + *VertexIndexPnt++;
lpLVERTEX = lpPointer + *VertexIndexPnt++;
/* find the distance from vert to light */
x = lpLVERTEX->x - Posx;
y = lpLVERTEX->y - Posy;
z = lpLVERTEX->z - Posz;
//distance = (float) sqrt( (x*x) + (y*y) + (z*z));
distance = (x*x) + (y*y) + (z*z);
if ( distance < SizeX2 )
{
// NumOfVertsTouched++;
if( distance > 0.0F )
{
distance = (float) sqrt( distance );
rlen = 1.0F / distance;
x *= rlen;
y *= rlen;
z *= rlen;
}
Cosa = ( ( x*Dirx ) + ( y*Diry ) + ( z*Dirz ) );
if( distance > 0.5F * OSize )
{
if ( Cosa > CosArc )
{
// Lights Are Go....
intense = ( ( OSize - distance ) / ( 0.75F * OSize ) ) * ( ( Cosa - CosArc ) / ( 1.0F - CosArc ) );
}else{
continue;
}
}else if ( distance > MIN_LIGHT_SIZE ) {
float cosarc2;
cosarc2 = CosArc * ( 1.0F - ( ( OSize * 0.5F - distance ) / ( OSize * 0.5F - MIN_LIGHT_SIZE ) ) );
if ( Cosa > cosarc2 )
{
intense = ( ( OSize - distance ) / ( OSize - MIN_LIGHT_SIZE ) ) * ( ( Cosa - cosarc2 ) / ( 1.0F - cosarc2 ) );
}else{
continue;
}
}else {
if ( Cosa > 0.0F )
{
intense = 1.0F;
}else{
intense = 1.0F + Cosa;
}
}
col = lpLVERTEX->color; // int
tempiA = col & 0xff000000;
tempiR = (int) ( rlf * intense );
tempiG = (int) ( glf * intense );
tempiB = (int) ( blf * intense );
inc = ( tempiR << 16 ) + ( tempiG << 8 ) + tempiB;
carry = ( ( col + inc ) ^ ( col ^ inc ) ) & 0x01010100;
clamp = carry - ( carry >> 8 );
col = ( ( col + inc - carry ) | clamp) & 0x00ffffff;
col |= tempiA;
lpLVERTEX->color = col;
}
}
Cell++;
}
Celly++;
}
Cellz++;
}
break;
}
}
}
XLightPnt = XLightPnt->NextVisible;
}
}
/* unlock the execute buffer */
if(!FSUnlockVertexBuffer((RENDEROBJECT*)&Mloadheader->Group[group].renderObject[execbuf]))
return false;
}
return true;
}
/*===================================================================
Procedure : Xlight Mxloadheader...
Input : nothing
Output : nothing
===================================================================*/
bool XLightMxloadHeader( MXLOADHEADER * MXloadheader , VECTOR * Pos , float Radius , MATRIX * Matrix )
{
XLIGHT * XLightPnt;
VECTOR Temp;
float distance;
int group;
int execbuf;
int vert;
LPLVERTEX lpPointer = NULL;
LPLVERTEX lpLVERTEX = NULL;
LPLVERTEX lpLVERTEX2 = NULL;
COLOR col;
float Size,OSize;
float SizeX2;
float x,y,z;
float Posx,Posy,Posz;
float Dirx = 0.0f, Diry = 0.0f, Dirz = 0.0f;
float Cosa,CosArc = 0.0f;
float rlen;
float intense;
int tempiR;
int tempiG;
int tempiB;
int tempiA;
u_int32_t inc;
u_int32_t carry;
u_int32_t clamp;
float blf;
float glf;
float rlf;
#ifdef NEW_LIGHTING
render_lighting_enabled = 1;
render_lighting_point_lights_only = 0;
return true;
#endif
group = MXloadheader->num_groups;
while( group--)
{
execbuf = MXloadheader->Group[group].num_execbufs;
while( execbuf--)
{
if (!(FSLockVertexBuffer(&MXloadheader->Group[group].renderObject[execbuf], &lpPointer)))
{
return false;
}
// lpPointer = (LPLVERTEX) debDesc.lpData;
lpLVERTEX = lpPointer;
vert = MXloadheader->Group[group].num_verts_per_execbuf[execbuf];
lpLVERTEX2 = MXloadheader->Group[group].originalVerts[execbuf];
while( vert --)
{
lpLVERTEX->color = lpLVERTEX2->color;
lpLVERTEX++;
lpLVERTEX2++;
}
XLightPnt = FirstLightVisible;
while( XLightPnt )
{
{
Posx = XLightPnt->Pos.x;
Posy = XLightPnt->Pos.y;
Posz = XLightPnt->Pos.z;
OSize = XLightPnt->Size;
Temp.x = Posx - Pos->x;
if( Temp.x < 0.0F ) Temp.x *= -1.0F;
Temp.y = Posy - Pos->y;
if( Temp.y < 0.0F ) Temp.y *= -1.0F;
Temp.z = Posz - Pos->z;
if( Temp.z < 0.0F ) Temp.z *= -1.0F;
if ( (Temp.x <= ( Radius + OSize ) ) &&