-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclouds.cpp
More file actions
2868 lines (2130 loc) · 63.8 KB
/
clouds.cpp
File metadata and controls
2868 lines (2130 loc) · 63.8 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
// Lod
// Generate Clouds using parametric alpha textures and spheres.
#include "F18.h"
#include "clouds.h"
#include "gamesettings.h"
#include "particle.h"
// Draw cloud
#define MAX_SPHERES_PER_CLOUD 32
#define MAX_CLOUDS_PER_WORLD 72
#define MAX_SEGMENTS 8.0f
#define MAX_CLOUDS_PER_FRAME 64
#define MAX_PTS_PER_SLICE 32
//extern TextureBuff *pAdiTexture;
extern TextureBuff *pLightningTexture, *pLightningTexture1, *pLightningTexture2, *pLightningTexture3;
void Render2DSpriteSX( Sprite *spt, float r, float g, float b, float a, float scale, int sx, int sy);
void DrawLightCloudMaps( RotPoint3D **lpoints,int NumPoints,FPoint &normal, DWORD light_bits);
void SoundThunder(int iVolume, int iPan, int iMode);
void DrawUnlitAddBillboardRel(FPoint &btm,FPoint &top,float width,TextureBuff *texture,float alpha = 1.0f,FPoint *color = NULL,BOOL mip_maps = FALSE);
void RemoveLightning( void);
// Clouds.
typedef struct _gpSphere
{
FPoint Position;
float radius;
float segments;
float hemisphere;
}gpSphere;
typedef struct _gpCloud
{
int NumSpheres;
FPoint Position;
FPoint GridOffset;
float BoundingRadius; // determing Visiblity
gpSphere Sphere[MAX_SPHERES_PER_CLOUD];
}gpCloud;
typedef struct _SpherePrimative
{
float EdgePts;
int Segments;
RotPoint3D SpherePoints[ MAX_PTS_PER_SLICE * MAX_PTS_PER_SLICE];
}SpherePrimative;
SpherePrimative SphereModel; // Currently Built Sphere.
void DrawPoly(GrBuff *dest, int numpts, RotPoint3D **points );
void DrawPolyNew(GrBuff *dest, int numpts, RotPoint3D **ppPoints, int gop);
void RenderCloud( gpCloud *pCloud, int segments, float alpha);
void RenderFlatClouds( float CloudAlt, float alpha, float tscale, int ShowLightning=0);
void LoadCloudTexture( void);
void DrawSphere( float Radius, int segments, float hemisphere, FPoint &RelPosition);
void DrawSpherePreBuilt( float Radius, float hemisphere, FPoint &RelPosition, float alpha, FPoint diffuse);
void CalculateLighting( FPoint &Position, FPoint &Normal,float radius, FPoint &diffuse );
void CalculateViewLighting( FPoint &Position, FPoint &Normal,float radius, FPoint &diffuse );
void BuildSphere( int segments );
void DisplayRain( BYTE density );
void RenderCloudWall( float KA );
void DrawCloudWall( float alpha, float tscale, float uoffset, float voffset, float Ka=1.0f, char Inverse=0 );
//Lightning
void AddLightning( FPoint wposition, float scale );
void MoveLightning( void);
void DisplayLighningSprite(FPointDouble &top, FPointDouble &bottom);
void PlayThunderSound( FPoint WorldPosition, int iMode=0);
extern BOOL PushBackFog;
typedef struct _CloudSort
{
gpCloud *pCloud;
float reldist;
_CloudSort *pBack;
_CloudSort *pFront;
}CloudSort;
CloudSort ClouldSortList[ MAX_CLOUDS_PER_FRAME];
gpCloud WorldClouds[ MAX_CLOUDS_PER_WORLD];
TextureBuff *pCloudTexture=NULL;
TextureBuff *pCloudFlatTexture=NULL;
BOOL SnowOn, doLightning=0, doRain = 0;
int cloudalt, cloudalt2;
DWORD cloudlightbits;
typedef struct _tLightning
{
float Timer;
char Active;
int Flag;
float Time;
float TotalTime;
float vFlicker;
float EmmisonRate;
PointLightSource **ppLight;
PointLightSource **ppLightLow;
}tLightning;
tLightning Lightning;
float MaxDist = 8 MILES;
float FadeOutDist = 1 MILES;
float CloudGridSize = 16 MILES;//32 MILES;//64 MILES;
float uscale, vscale, uoffset, voffset;
void SetUVScaleOffset( float Uscale, float Uoffset, float Vscale, float Voffset)
{
uscale = Uscale;
vscale = Vscale;
uoffset = Uoffset;
voffset = Voffset;
}
//Note This should be fast enought to render on the fly.. but we might want to speed it up a bit by saving the pts.
// or just doing one LOD sphere for a cloud group and scale it for each cloud
void LoadCloudTexture( void)
{
TextureRef temp_ref;
DDCOLORKEY key;
memset(&key,0,sizeof(DDCOLORKEY));
ZeroMemory(&temp_ref, sizeof(temp_ref));
temp_ref.CellColumns = 0;
temp_ref.TotalCells = 0;
temp_ref.CellWidth = 1.0f;
temp_ref.CellHeight = 1.0f;
sprintf( temp_ref.Name, "Cloud00.pcx");//Cloud01
bNoAlphaFilter = TRUE;
SetTextureFormat( FT_16BIT_DATA );
pCloudTexture = Load3DTexture(&temp_ref, UT_PURE_ALPHA);
//pCloudTexture = Load3DTexture(&temp_ref, UT_DEFAULT);
if (WorldParams.Weather & WR_FLATCLOUD_OVERCAST) //Includes Stormy
sprintf( temp_ref.Name, "cloudoc.pcx");//0a, 3a, 8a
else
{
if (WorldParams.Weather & WR_FLATCLOUD_SCATTERED)
sprintf( temp_ref.Name, "cloudsc.pcx");//0a, 3a, 8a
else
sprintf( temp_ref.Name, "cloud0a.pcx");//0a, 3a, 8a
}
pCloudFlatTexture = Load3DTexture(&temp_ref, UT_DEFAULT);//UT_PURE_ALPHA
bNoAlphaFilter = FALSE;
ReSetTextureFormat();
}
void ShutdownClouds( void)
{
if( pCloudTexture )
Free3DTexture(pCloudTexture);
pCloudTexture = NULL;
if( pCloudFlatTexture )
Free3DTexture(pCloudFlatTexture );
pCloudFlatTexture = NULL;
RemoveLightning( );
}
void InitClouds( void )
{
doRain = doLightning = SnowOn = 0;
ZeroMemory( &Lightning, sizeof(tLightning) );
if( (g_Settings.gr.dwGraph & GP_GRAPH_RAIN_SNOW ) && (WorldParams.Weather & WR_FLATCLOUD_STORMY) == WR_FLATCLOUD_STORMY)
{
if( frand() < 0.90f ) // 90% chance of rain
doRain = 1;
if( doRain && (frand() < 0.20f) ) // 20% chance of snow
SnowOn = 1;
if( (g_Settings.gr.dwGraph &GP_GRAPH_LIGHTNING) && doRain && !SnowOn )
doLightning=1;
}
// Initialize the clouds some how. Big Small etc.
// Might want to create a quick tool to do this
int i;
float rx, rz;
float rradius;
float MaxSize = 300 METERS;
float MaxRadius = 400 METERS;
FPoint Pos;
float wx, wz;
//TEMP UNTIL IN CONFIG SCREEN
CloudGridSize = 32 MILES;
// char *res = GetRegValue( "Clouds" );
// if( res )
// {
// if( !strcmp( res, "Max"))
// CloudGridSize = 16 MILES;
// else
// CloudGridSize = 32 MILES;
// doClouds =1;
// }
// WorldParams.CloudAlt = 8000 FEET;
// if( GetRegValue( "CloudAlt" ) )
// {
// int alt = GetRegValueL( "CloudAlt" );
// WorldParams.CloudAlt = alt FEET;
// }
gpCloud *pCloud = &WorldClouds[ 0];
ZeroMemory( WorldClouds, sizeof( WorldClouds) );
gpSphere *pSphere;
/*
wx = wz = 0;
for( i=0; i< MAX_CLOUDS_PER_WORLD; i++,pCloud++)
{
wx = frand() * CloudGridSize;
wz = frand() * CloudGridSize;
Pos.SetValues( wx, WorldParams.CloudAlt, wz);
pCloud->NumSpheres = 16;// 8 ;
pCloud->BoundingRadius = 0.0f; //Not in use
pCloud->Position = Pos;
pSphere = pCloud->Sphere;
for( int i=0; i<pCloud->NumSpheres; i++)
{
rx = (frand() - 0.5f) * 2.0f;
rz = (frand() - 0.5f) * 2.0f;
rradius = frand();
pSphere->Position = Pos;
pSphere->Position.X += rx * MaxRadius;
pSphere->Position.Z += rz * MaxRadius;
pSphere->radius = rradius * MaxSize + (100 METERS);
pSphere->Position.Y += pSphere->radius ;// adjust for same height
pSphere++;
}
}
*/
wx = wz = 0;
float scale;
scale = 1.0f;
scale = frand();
if( scale < 0.25)
scale += 1.2;
else
scale = 1.0f;
for( i=0; i< MAX_CLOUDS_PER_WORLD; i++,pCloud++)
{
wx = frand() * CloudGridSize;
wz = frand() * CloudGridSize;
Pos.SetValues( wx, (float)(WorldParams.CloudAlt- (3000.0f FEET)), wz);
//Pos.SetValues( wx, (float)(10000.0f FEET), wz);
pCloud->NumSpheres = 14;//12;//
pCloud->BoundingRadius = 0.0f; //Not in use
pCloud->Position = Pos;
pSphere = pCloud->Sphere;
for( int i=0; i<pCloud->NumSpheres; i++)
{
rx = (frand() - 0.5f) * 2.0f;
rz = (frand() - 0.5f) * 2.0f;
rradius = frand();
pSphere->Position = Pos;
pSphere->Position.X += rx * MaxRadius;
pSphere->Position.Z += rz * MaxRadius;
pSphere->radius = rradius * MaxSize + (100 METERS);
pSphere->radius *= scale;
pSphere->Position.Y += pSphere->radius ;// adjust for same height
pSphere++;
Pos.Z += pSphere->radius;
}
}
}
CloudSort *GetSortHeader( void )
{
CloudSort *pSort, *pLast;
pSort = &ClouldSortList[0];
pLast = &ClouldSortList[MAX_CLOUDS_PER_FRAME];
while( pSort< pLast)
{
if( !pSort->pCloud)
return pSort;
pSort++;
}
return NULL;
}
void InsertSort( CloudSort *pNew, CloudSort *pList)
{
if( pNew->reldist > pList->reldist)
{
if( !pList->pBack)
{
pList->pBack = pNew;
return;
}
else
InsertSort( pNew, pList->pBack);
}
else
{
if( !pList->pFront)
{
pList->pFront = pNew;
return;
}
else
InsertSort( pNew, pList->pFront);
}
}
void RenderSortedCloud( CloudSort *pSort)
{
float segs;
float dist = pSort->reldist;
float Ka=1.00; // Ambient Alpha
if( dist > MaxDist)
{
segs = 4;
Ka = 1.0f - ( (dist-MaxDist) * 1.0f/(FadeOutDist));
}
else
{
segs = MAX_SEGMENTS *(1.0f - dist/MaxDist) + 4;
Ka = 1.0f;
}
BuildSphere( segs ); // Build 1 sphere for entire cloud group
//if( RadiusInView( pCloud->Position, pCloud->BoundingRadius) ) /// NEED TO ADD BOunding SPHERE for CLOUD Gruop!!!
RenderCloud( pSort->pCloud, segs, Ka );
}
void InsertCloud( gpCloud *pCloud, float dist)
{
CloudSort *pSort, *pNew;
pSort = &ClouldSortList[0];
pNew = GetSortHeader( );
if( pNew )
{
pNew->pCloud = pCloud;
pNew->reldist = dist;
if( pNew!= pSort )
InsertSort( pNew, pSort);
}
}
void RenderList( CloudSort *pSort)
{
if( pSort->pBack)
RenderList( pSort->pBack);
if( pSort->pCloud)
RenderSortedCloud( pSort );
if( pSort->pFront)
RenderList( pSort->pFront);
pSort->pCloud = NULL;
pSort->pFront = NULL;
pSort->pBack = NULL;
}
void RenderCloudList( void )
{
//back to front
RenderList( &ClouldSortList[0]);
}
extern BOOL NoFogging;
void DisplayClouds( CameraInstance *camera )
{
float Ka, Ka2;
float dist, dist2;
float CloudEffectRange=4000 FEET;
float FadeCloudLayerStart= 200 FEET;
float FadeCloudLayerDist= 800 FEET;
int ShowLightning= 0;
//lpD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, FALSE);
//lpD3DDevice->SetRenderState(D3DRENDERSTATE_SPECULARENABLE, FALSE);
//NoFogging = TRUE;
cloudalt = WorldParams.CloudAlt + 2000 FEET;
cloudalt2 = cloudalt + ( 2000 FEET);
dist = fabs(camera->CameraLocation.Y -cloudalt);
if( dist < (FadeCloudLayerDist + FadeCloudLayerStart))
{
Ka = (dist-FadeCloudLayerStart);
if (Ka <= 0)
Ka = 0;
else
Ka *= 1.0f/FadeCloudLayerDist;
}
else
Ka = 1.0f;
dist2 = fabs(camera->CameraLocation.Y -cloudalt2);
if( dist2 < (FadeCloudLayerDist+FadeCloudLayerStart))
{
Ka2 = (dist2-FadeCloudLayerStart);
if (Ka2 <= 0)
Ka2 = 0;
else
Ka2 *= 1.0f/FadeCloudLayerDist;
}
else
Ka2 = 1.0f;
//Ka2 *= 0.75;
Ka*= Ka;
Ka*= Ka;
Ka2 *= Ka2;
Ka2 *= Ka2;
if( camera->CameraLocation.Y < cloudalt)//WorldParams.CloudAlt )
{
if ((WorldParams.Weather & WR_FLATCLOUD) || (!(g_Settings.gr.dwGraph & GP_GRAPH_VOLUMETRIC_CLOUDS ) && (WorldParams.Weather & WR_PUFFCLOUD_SCATTERED)))
{
ShowLightning = doLightning;
RenderFlatClouds( cloudalt, Ka, 8.0f, ShowLightning );
RenderFlatClouds( cloudalt2, Ka2, 10.0f, FALSE );
if( doLightning)
{
tLightning *pLightning;
pLightning = &Lightning;
if( (frand()< 0.40f) && pLightning->ppLightLow && pLightning->Time < 0.5f ) //> pLightning->TotalTime)
DisplayLighningSprite((*(pLightning->ppLight))->WorldPosition, (*(pLightning->ppLightLow))->WorldPosition);
else
{
if( pLightning->ppLightLow )
RemoveLight((LightSource **)pLightning->ppLightLow );
pLightning->ppLightLow = NULL;
}
}
}
if( (g_Settings.gr.dwGraph & GP_GRAPH_VOLUMETRIC_CLOUDS ) && (WorldParams.Weather & WR_PUFFCLOUD_SCATTERED))
ProcessClouds( );
}
else
{
FlushAlphaLists();
ResetAlphaLists();
if(( g_Settings.gr.dwGraph & GP_GRAPH_VOLUMETRIC_CLOUDS) && (WorldParams.Weather & WR_PUFFCLOUD_SCATTERED))
ProcessClouds( );
if ((WorldParams.Weather & WR_FLATCLOUD) || (!(g_Settings.gr.dwGraph & GP_GRAPH_VOLUMETRIC_CLOUDS ) && (WorldParams.Weather & WR_PUFFCLOUD_SCATTERED)))
{
if( camera->CameraLocation.Y < cloudalt2)
{
tLightning *pLightning;
pLightning = &Lightning;
ShowLightning = doLightning;
if( (frand()< 0.40f) && pLightning->ppLightLow && pLightning->Time < 0.5f ) //> pLightning->TotalTime)
DisplayLighningSprite((*(pLightning->ppLight))->WorldPosition, (*(pLightning->ppLightLow))->WorldPosition);
else
{
if( pLightning->ppLightLow )
RemoveLight((LightSource **)pLightning->ppLightLow );
pLightning->ppLightLow = NULL;
}
}
//Put check for overcast days... draw a dark flat cloud instead
//if( overcast &&
RenderFlatClouds( cloudalt, Ka, 8.0f, ShowLightning);
ShowLightning = doLightning;
RenderFlatClouds( cloudalt2 , Ka2, 10.0f, ShowLightning );
}
}
if (WorldParams.Weather & WR_FLATCLOUD)
{
if( dist2< dist)
dist = dist2; // get close layer
if( dist < CloudEffectRange )
RenderCloudWall( 1.0f - (dist/CloudEffectRange) );
}
if( doRain )//(WorldParams.Weather & WR_FLATCLOUD_STORMY) == WR_FLATCLOUD_STORMY)
{
if( camera->CameraLocation.Y>WorldParams.CloudAlt && camera->CameraLocation.Y< cloudalt)
{
float density;
density = 1.0f -(camera->CameraLocation.Y - WorldParams.CloudAlt)/(cloudalt - WorldParams.CloudAlt);
density *= 255.0f;
DisplayRain( density);
}
else if( camera->CameraLocation.Y<WorldParams.CloudAlt )
DisplayRain( 255 );
}
// NoFogging = FALSE;
//lpD3DDevice->SetRenderState(D3DRENDERSTATE_SPECULARENABLE, TRUE);
//lpD3DDevice->SetRenderState(D3DRENDERSTATE_FOGENABLE, TRUE);
}
void ProcessClouds( void )
{
int i;
FPoint CloudGridOffset;
FPoint RelPosition;
gpCloud *pCloud;
int gx, gy;
SetUVScaleOffset( 1.0f, 0.0f, 1.0f, 0.0f); //reset
ZeroMemory( &ClouldSortList[0], sizeof( ClouldSortList)); // reset head of sort list
CloudGridOffset.X = ((int)(Camera1.CameraLocation.X/CloudGridSize)) * CloudGridSize;
CloudGridOffset.Y = 0.0f;
CloudGridOffset.Z = ((int)(Camera1.CameraLocation.Z/CloudGridSize)) * CloudGridSize;
if( (Camera1.CameraLocation.X -CloudGridOffset.X ) < ( CloudGridOffset.X+CloudGridSize - Camera1.CameraLocation.X) )
gx = -CloudGridSize;
else
gx = CloudGridSize;
// draw 3 closets grids
CloudGridOffset.X += gx;
pCloud = &WorldClouds[0];
for( i=0; i< MAX_CLOUDS_PER_WORLD; i++,pCloud++)
{
if( pCloud->NumSpheres )
{
RelPosition= pCloud->Position;
pCloud->GridOffset = CloudGridOffset;
RelPosition += CloudGridOffset;
RelPosition -= Camera1.CameraLocation;
float dist = RelPosition.QuickLength();
if( dist < (MaxDist + FadeOutDist))
InsertCloud( pCloud, dist);
}
}
RenderCloudList();
CloudGridOffset.X -= gx;
/// Y PIECE
if( (Camera1.CameraLocation.Z -CloudGridOffset.Z ) < ( CloudGridOffset.Z+CloudGridSize - Camera1.CameraLocation.Z) )
gy = -CloudGridSize;
else
gy = CloudGridSize;
// draw 3 closets grids
CloudGridOffset.Z += gy;
pCloud = &WorldClouds[0];
for( i=0; i< MAX_CLOUDS_PER_WORLD; i++,pCloud++)
{
if( pCloud->NumSpheres )
{
RelPosition= pCloud->Position;
pCloud->GridOffset = CloudGridOffset;
RelPosition += CloudGridOffset;
RelPosition -= Camera1.CameraLocation;
float dist = RelPosition.QuickLength();
if( dist < (MaxDist + FadeOutDist))
InsertCloud( pCloud, dist);
}
}
RenderCloudList();
/// CORNER PIECES
// draw 3 closets grids
CloudGridOffset.X += gx;
//CloudGridOffset.Y = 0.0f;
//CloudGridOffset.Z = ((int)(Camera1.CameraLocation.Z/CloudGridSize)) * (CloudGridSize)+gy;
pCloud = &WorldClouds[0];
for( i=0; i< MAX_CLOUDS_PER_WORLD; i++,pCloud++)
{
if( pCloud->NumSpheres )
{
RelPosition= pCloud->Position;
pCloud->GridOffset = CloudGridOffset;
RelPosition += CloudGridOffset;
RelPosition -= Camera1.CameraLocation;
float dist = RelPosition.QuickLength();
if( dist < (MaxDist + FadeOutDist))
InsertCloud( pCloud, dist);
}
}
RenderCloudList();
CloudGridOffset.X -= gx;
CloudGridOffset.Z -= gy;
// CloudGridOffset.X = ((int)(Camera1.CameraLocation.X/CloudGridSize)) * CloudGridSize;
// CloudGridOffset.Y = 0.0f;
// CloudGridOffset.Z = ((int)(Camera1.CameraLocation.Z/CloudGridSize)) * CloudGridSize;
pCloud = &WorldClouds[0];
for( i=0; i< MAX_CLOUDS_PER_WORLD; i++,pCloud++)
{
if( pCloud->NumSpheres )
{
RelPosition= pCloud->Position;
pCloud->GridOffset = CloudGridOffset;
RelPosition += CloudGridOffset;
RelPosition -= Camera1.CameraLocation;
float dist = RelPosition.QuickLength();
if( dist < (MaxDist + FadeOutDist))
InsertCloud( pCloud, dist);
}
}
RenderCloudList();
//**************
//RemoveViewpointLights(); // This should be in render.cpp
}
void RenderCloud( gpCloud *pCloud, int segments, float alpha)
{
FPoint Diffuse;
FPoint RelPosition;
float hemi = 1.0f;
int gop = PRIM_TEXTURE | PRIM_ALPHAIMM| PRIM_NO_Z_WRITE;
gpSphere *pSphere = pCloud->Sphere;
CurrentTexture = pCloudTexture;
Diffuse.SetValues( 0.8f);
for( int i=0; i< pCloud->NumSpheres; i++)
{
RelPosition = pSphere->Position;
RelPosition += pCloud->GridOffset ;
RelPosition -= Camera1.CameraLocation;
if( RadiusInView( RelPosition, pSphere->radius) )
DrawSpherePreBuilt( pSphere->radius, hemi, RelPosition, alpha, Diffuse, gop);
//DrawSphere( pSphere->radius, segments, hemi, RelPosition );
pSphere++;
}
}
int VisibleFace( int numpts, RotPoint3D **ppPoints)
{
FPoint N, A, B;
FPoint EyeVector;
float dot;
//Cross Product
A = (*(ppPoints+1))->Rotated;
A -= (*(ppPoints))->Rotated;
B = (*(ppPoints+2))->Rotated;
B -= (*(ppPoints))->Rotated;
N = A;
N %= B; //Cross Product
EyeVector = (*ppPoints)->Rotated;
EyeVector *= -1.0f;
// Dot Product
dot = EyeVector * N;
if( dot>=0.0f)
return 0;
else
return 1;
}
void BuildSphere( int segments )
{
int i, j;
float slicepts;
float sliceangle, loftangle;
float angle, radian;
RotPoint3D firstedge[MAX_PTS_PER_SLICE];
RotPoint3D *pCurrent, *pFirst;
float sn, cs;
float uscale = 1.0f /360.0f;
float vscale = 1.0f /180.0f;
float Radius = 1.0f;
if( segments <4 ) // need atleast 4 to make something
return;
SphereModel.EdgePts = slicepts = segments-1;
SphereModel.Segments = segments;
if( slicepts >MAX_PTS_PER_SLICE)
return ;
loftangle = 360.0f/(float)segments;
sliceangle= 180.0f/ (float)(slicepts-1); //180.0f* hemisphere
// Generate first edge which we will loft
pFirst = &firstedge[0];
angle = 0;
for( i=0; i< slicepts; i++)
{
radian = angle * (3.14159265359f/180.0f);
sn = sin(radian);
cs = cos(radian);
pFirst->Rotated.X = sn * Radius;
pFirst->Rotated.Y = cs * Radius;
pFirst->Rotated.Z = 0.0f;
pFirst->U = 0.0f;
pFirst->V = angle * vscale;
angle += sliceangle;
pFirst++;
}
// loft the first edge to build the polys
angle = 0.0;//loftangle;
pCurrent = &SphereModel.SpherePoints[0];
for( i=0; i< segments; i++)
{
// Rotate new edge // Note We are actually rotating the 2 End pts which we don't neeed to do
// unlese we use the hemisphere opitoin.
pFirst = &firstedge[0];
radian = angle * (3.14159265359f/180.0f);
for( j=0; j<slicepts; j++)
{
sn = sin(radian);
cs = cos(radian);
pCurrent->Rotated.Y = pFirst->Rotated.Y;
pCurrent->Rotated.X = cs * pFirst->Rotated.X; // loft around y
pCurrent->Rotated.Z = sn * pFirst->Rotated.X;
pCurrent->U = angle * uscale;
pCurrent->V = pFirst->V;
pCurrent->SP_Rotated = pCurrent->Rotated; // save the Normal here temporarly
pCurrent->SP_Rotated.Normalize();//Don't really need this
pCurrent++;
pFirst++;
}
angle += loftangle;
}
//copy first row to last row
memcpy( pCurrent, SphereModel.SpherePoints, slicepts * sizeof( RotPoint3D) );
for( i=0; i< slicepts; i++)
(pCurrent++)->U = 1.0f;
// Rotate Points
pCurrent = &SphereModel.SpherePoints[0];
for( i=0; i< ((segments+1) * slicepts); i++)
{
pCurrent->Rotated *= ViewMatrix;
pCurrent->SP_Rotated *= ViewMatrix; //rotate normals
pCurrent++;
}
}
void CalcAlphaAndLighting( RotPoint3D *pPt, float Radius ,int gop)
{
float dot;
FPoint Normal, EyeVector;
FPoint Diffuse;
if( pPt->Flags)
return;
EyeVector = pPt->Rotated;
EyeVector *= -1.0f;
EyeVector.Normalize();
Normal = pPt->SP_Rotated; // we stored the normal in here
//if( FlipSphereNormals )
// Normal *= -1.0f;
dot = Normal * EyeVector;
if( dot>1.0f)
dot = 1.0f;
if( dot>0)
pPt->Alpha *= dot*dot*dot*dot ;//* Ka;
else
pPt->Alpha = 0.0f;
Diffuse.SetValues(0.0f);
CalculateViewLighting( pPt->Rotated, Normal, Radius, Diffuse);
pPt->Diffuse *= Diffuse;
pPt->Diffuse.Limit( 1.0f);
if (gop & PRIM_ALPHA_ADD)
{
pPt->Diffuse *= pPt->Alpha;
pPt->Alpha = 1.0f;
}
}
void DrawSpherePreBuilt( float R, float hemisphere, FPoint &RP, float alpha, FPoint d, int gop, FMatrix *pMat)
{
int i, j;
RotPoint3D Sphere[MAX_PTS_PER_SLICE * MAX_PTS_PER_SLICE];// too big for the stack?
RotPoint3D *pPoint, *pEdge, *pNext;
RotPoint3D *ppPoints[4];
FPoint offset;
float dist;
int slicepts = SphereModel.EdgePts;
int segments = SphereModel.Segments;
FPoint short_pos;
FPoint Diffuse;
FPoint &RelPosition = (ImagingMethod & IT_GREEN) ? short_pos : RP;
float Radius = R;
if (ImagingMethod & IT_GREEN)
{
RelPosition.SetValues(0.8f,RP);
Radius *= 0.8f;
PushBackFog = TRUE;
}
Diffuse = d;
if( gop & PRIM_ALPHA_ADD)
{
Diffuse *= alpha;
alpha = 1.0f;
}
offset = RelPosition;
offset *= ViewMatrix;
dist = RelPosition.QuickLength();
// mult radius + offset
pPoint = &SphereModel.SpherePoints[0];
pEdge = &Sphere[0];
for( i=0; i< (slicepts * (segments+1)); i++)
{
pEdge->Rotated = pPoint->Rotated;
pEdge->Rotated *= Radius;
if( pMat)
pEdge->Rotated *= *pMat;
pEdge->Rotated += offset;//RelPosition;