forked from stuart73/Onslaught
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDXEngine.cpp
More file actions
1598 lines (1216 loc) · 43.2 KB
/
Copy pathDXEngine.cpp
File metadata and controls
1598 lines (1216 loc) · 43.2 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
// DXEngine.cpp: implementation of the CDXEngine class.
//
//////////////////////////////////////////////////////////////////////
#include "common.h"
#ifdef _DIRECTX
#include "DXEngine.h"
#include "DX.h"
#include "State.h"
#include "Map.h"
#include "D3DUtils.h"
#include "Maptex.h"
#include "Game.h"
#include "RTMesh.h"
#include "RenderThing.h"
#include "Imposter.h"
#include "Thing.h"
#include "KempyCube.h"
#include "Water.h"
#include "Shadows.h"
#include "GCGamut.h"
#include "World.h"
#include "Camera.h"
#include "Profile.h"
#include "ParticleManager.h"
#include "ParticleSet.h"
#include "Line.h"
#include "UMTexture.h"
#include "DXHud.h"
#include "Lights.h"
#include "VBufTexture.h"
#include "Capture.h"
#include "Fog.h"
#include "Smoke.h"
#include "Atmospherics.h"
#include "Console.h"
#include "BattleLine.h"
#include "EventManager.h"
#include "landscape.h"
#include "cockpit.h"
#include "rendertarget.h"
#include "spriterenderer.h"
#include "waterreflection.h"
#include "cutscene.h"
#include "debris.h"
#include "renderqueue.h"
#include "screenfx.h"
#include "renderinfo.h"
#include "membuffer.h"
#include "gameinterface.h"
#include "Player.h"
#include "debugtext.h"
#include "debuglog.h"
#include "rain.h"
#include "cutsceneeditor.h"
#include "dxpatchmanager.h"
#include "debugmarker.h"
#include "visibilitytester.h"
#include "pausemenu.h"
#include "trees.h"
#include "cliparams.h"
//=============================================-===--==---=---- --- -- -
CDXEngine ENGINE;
//=============================================-===--==---=---- --- -- -
//
// Engine
//
//=============================================-===--==---=---- --- -- -
CDXEngine::CDXEngine()
{
mDefaultShader = NULL;
mScreenTexture = NULL;
mCurrentViewpoint = 0;
mRenderReflections=false;
mTextureReflections=true;
//#ifdef EDITORBUILD2
mNavDisplay = FALSE;
//#endif
}
//=============================================-===--==---=---- --- -- -
CDXEngine::~CDXEngine()
{
// ShutDown();
}
//=============================================-===--==---=---- --- -- -
void CDXEngine::ShutDown()
{
CEngine::Shutdown();
SAFE_RELEASE(mScreenTexture);
SAFE_RELEASE(mDefaultTexture);
SAFE_RELEASE(mTexOutline);
SAFE_RELEASE(mTexOpaque);
#if TARGET == PC
SAFE_RELEASE(mTexEdArrow);
#endif
SAFE_RELEASE(mDefaultMesh);
PATCHMANAGER.Shutdown();
mSunPD = NULL;
}
//=============================================-===--==---=---- --- -- -
// Sets the gamma ramp bias (0=normal, 1=silly contrast!)
//=============================================-===--==---=---- --- -- -
void con_setgammabias(char *cmd)
{
float value;
if (sscanf(cmd,"%*s %f",&value)!=1)
{
CONSOLE.Print("Syntax : SetGammaBias <value> (range 0-1)\n");
return;
}
ENGINE.SetGammaBias(value);
}
void CDXEngine::SetGammaBias(float value)
{
D3DGAMMARAMP ramp;
for (int i=0;i<256;i++)
{
float scale=(256-i)*value;
if (scale>1)
scale=1;
if (scale<0)
scale=0;
scale=1-scale;
ramp.red[i]=(BYTE) (mDefaultGammaRamp.red[i]*scale);
ramp.green[i]=(BYTE) (mDefaultGammaRamp.green[i]*scale);
ramp.blue[i]=(BYTE) (mDefaultGammaRamp.blue[i]*scale);
ramp.red[i]=(ramp.red[i]<<8)+ramp.red[i];
ramp.green[i]=(ramp.green[i]<<8)+ramp.green[i];
ramp.blue[i]=(ramp.blue[i]<<8)+ramp.blue[i];
}
LT.D3D_SetGammaRamp(D3DSGR_NO_CALIBRATION,&ramp);
}
//=============================================-===--==---=---- --- -- -
BOOL CDXEngine::Init()
{
if (!CEngine::Init())
return(FALSE);
mDefaultShader = NULL;
mScreenTexture = NULL;
mDrawDebugStuff = DRAW_COCKPIT ;
// HRESULT hr;
/*
if(FAILED(hr = mKempyCube->Init(MAP.GetGenerator()->SV.CubeNo))) return hr;
if(FAILED(hr = mSky->Init(MAP.GetGenerator()->SV.CloudNo))) return hr;
if(FAILED(hr = mWater->Init(MAP.GetGenerator()->SV.WaterTexture))) return hr;
*/
mNumGlobalMeshes = 0;
#if TARGET==PC
// Set up the capture texture
mScreenTexture = new( MEMTYPE_TEXTURE ) CTEXTURE();
mScreenTexture->InitCustom(1024,512,TEXFMT_X8R8G8B8,1,true);
strcpy(mScreenTexture->Name,"Screen contents texture");
mCaptureScreen=true;
#else
ASSERT(TARGET == XBOX);
// no screen capture texture here any more...
// mScreenTexture->InitCustom(1024,512,TEXFMT_LIN_R5G6B5,1,FALSE);
mCaptureScreen = false;
#endif
mCaptureAreaTop=0;
mCaptureAreaBottom=1000;
mBlurAlpha=0;
mNewBlurAlpha=-1;
mFirstFrame=TRUE;
// Grab the current gamma ramp
LT.D3D_GetGammaRamp(&mDefaultGammaRamp);
// Set up our console commands
CONSOLE.RegisterCommand("SetGammaBias","Alter the gamma ramp bias (0=normal, 1=black), 0-0.005 is a good range to try",&con_setgammabias);
CONSOLE.RegisterVariable("cg_renderreflections","Should water reflections be rendered",CVar_bool,&mRenderReflections);
CONSOLE.RegisterVariable("cg_texturereflections","Should render to texture be used for reflections",CVar_bool,&mTextureReflections);
PATCHMANAGER.Init(800,300,90);
// PATCHMANAGER.Init(1000,400,120);
// PATCHMANAGER.Init(2000,800,300);
return(TRUE);
}
//=============================================-===--==---=---- --- -- -
void CDXEngine::InitResources()
{
CEngine::InitResources();
mDefaultTexture = CTEXTURE::GetTextureByName("meshtex\\default.tga");
mTexOutline = CTEXTURE::GetTextureByName("meshtex\\outline.tga");
mTexOpaque = CTEXTURE::GetTextureByName("meshtex\\basicpanel.tga");
#if TARGET == PC
mTexEdArrow = CTEXTURE::GetTextureByName("meshtex\\EdArrow.tga");
#endif
mDefaultMesh = CMESH::GetMesh("default.msh");
mSunPD = PARTICLE_SET.GetPD("Sun Sprite");
}
//******************************************************************************************
void CDXEngine::DrawDebugCuboid(DWORD col, FVector &axes, FVector &pos, FMatrix ori, CTEXTURE *for_texture)
{
// Draw polys representing cuboid.
// ** ASSUMES local orientation already set up...
RENDERINFO.SRS( RS_ALPHABLENDENABLE, TRUE );
RENDERINFO.SRS( RS_SRCBLEND, BLEND_ONE );
RENDERINFO.SRS( RS_DESTBLEND, BLEND_ZERO );
RENDERINFO.Apply() ;
FVector ax = ori * FVector(axes.X, 0, 0);
FVector ay = ori * FVector(0, axes.Y, 0);
FVector az = ori * FVector(0, 0, axes.Z);
SINT c0;
CFVF_GTEX vert;
CVBufTexture *vb=CVBufTexture::GetTextureByTex(for_texture);
vb->SetupV(FVF_GTEX_FLAGS,D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC,sizeof(CFVF_GTEX),D3DPT_TRIANGLELIST,D3DPOOL_DEFAULT);
vb->SetupI(D3DFMT_INDEX16,D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC,2,D3DPOOL_DEFAULT);
for (c0 = 0; c0 < 6; c0++)
{
SINT i[4];
for (int c1 = 0;c1 < 4; c1 ++)
{
switch (c0)
{
case 0: vert.mPos = ax + ((c1 & 1) ? ay : -ay) + ((c1 & 2) ? az : -az) ; break;
case 1: vert.mPos = - ax + ((c1 & 1) ? ay : -ay) + ((c1 & 2) ? az : -az) ; break;
case 2: vert.mPos = ((c1 & 1) ? ax : -ax) + ay + ((c1 & 2) ? az : -az) ; break;
case 3: vert.mPos = ((c1 & 1) ? ax : -ax) - ay + ((c1 & 2) ? az : -az) ; break;
case 4: vert.mPos = ((c1 & 1) ? ax : -ax) + ((c1 & 2) ? ay : -ay) + az ; break;
case 5: vert.mPos = ((c1 & 1) ? ax : -ax) + ((c1 & 2) ? ay : -ay) - az ; break;
};
vert.mPos.X += pos.X;
vert.mPos.Y += pos.Y;
vert.mPos.Z += pos.Z;
vert.mU = (c1&1)?1.f:0.f;
vert.mV = (c1&2)?0.f:1.f;
// Make the front of objects a different colour
if (c0==2)
vert.mDiffuse = 0xFFFFFFFF;
else
vert.mDiffuse = col;
i[c1]=vb->AddVertices(&vert,1);
}
// double-sided...
UWORD indices[12];
indices[0]=i[0]; indices[1]=i[3]; indices[2]=i[2];
indices[3]=i[0]; indices[4]=i[1]; indices[5]=i[3];
indices[6]=i[0]; indices[7]=i[2]; indices[8]=i[3];
indices[9]=i[0]; indices[10]=i[3]; indices[11]=i[1];
vb->AddIndices(&indices,12);
}
vb->Draw(true);
SAFE_RELEASE(vb);
RENDERINFO.SRS( RS_SRCBLEND, BLEND_SRCALPHA );
RENDERINFO.SRS( RS_DESTBLEND, BLEND_INVSRCALPHA );
RENDERINFO.Apply() ;
}
//******************************************************************************************
void CDXEngine::DrawDebugLine(DWORD col, FVector &start, FVector &end)
{
FVector axes=FVector(0.01f,0.01f,(end-start).Magnitude()/2.0f);
FMatrix ori;
ori.BuildLookMatrix(end-start,FVector(0,0,1));
DrawDebugCuboid(col,axes,(start+end)/2.0f,ori,GetOpaqueTexture());
}
//******************************************************************************************
void CDXEngine::RenderArrow(FVector v1, FVector v2, DWORD col)
{
#if TARGET == PC
// draw arrow between 2 points
FMatrix ori = ID_FMATRIX;
FVector pos = FVector(0, 0, 0);
RENDERINFO.SetWorld(pos, ori);
FVector l = v2 - v1;
FVector s = l ^ FVector(0, 0, -1.f);
s.Normalise();
s *= 0.5f;
// LOG.AddMessage("arrow dif = %08x", col) ;
SINT c1;
CFVF_GTEX vert;
CVBufTexture *vb=CVBufTexture::GetTextureByTex(mTexEdArrow);
vb->SetupV(FVF_GTEX_FLAGS,D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC,sizeof(CFVF_GTEX),D3DPT_TRIANGLELIST,D3DPOOL_DEFAULT);
vb->SetupI(D3DFMT_INDEX16,D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC,2,D3DPOOL_DEFAULT);
SINT i[4];
for (c1 = 0;c1 < 4; c1 ++)
{
vert.mPos = ((c1 & 1) ? ZERO_FVECTOR : l) + ((c1 & 2) ? s : -s);
FVector vr = v1 + FVector(0, 0, -0.5f);
vert.mPos.X += vr.X ;
vert.mPos.Y += vr.Y ;
vert.mPos.Z += vr.Z ;
vert.mU = (c1&2)?1.f:0.f;
vert.mV = (c1&1)?0.f:1.f;
vert.mDiffuse = col;
// broken by me :-) vert.mDiffuse = col;
i[c1]=vb->AddVertices(&vert,1);
}
UWORD indices[6];
indices[0]=i[0]; indices[1]=i[3]; indices[2]=i[2];
indices[3]=i[0]; indices[1]=i[1]; indices[5]=i[3];
vb->AddIndices(&indices,6);
vb->Draw(true);
SAFE_RELEASE(vb);
#endif
}
#if TARGET == XBOX
static bool drawing_every_other = true;
static bool is_the_every_other = false;
#endif
//=============================================-===--==---=---- --- -- -
HRESULT CDXEngine::PreRender(CViewport *viewport)
{
#if TARGET == XBOX
if (drawing_every_other && LT.GetFlipMethod() == CXBOXDX::FULL_SYNC_LATE_HUD_MY_COPY)
{
// Interesting. We're only drawing every other frame.
is_the_every_other ^= true;
}
if (is_the_every_other) return S_OK;
#endif
// Do offscreen stuff
STATE.UseDefault();
SwitchAllLights(FALSE); // don't want lights showing on imps
#ifdef RESBUILDER
CIMPOSTER::MakeReady(mFirstFrame); // Build imposters if needed
#endif
// Clear whole screen
{
PROFILE_FN(RenderBeginScene);
PLATFORM.BeginScene();
}
mCurrentViewpoint=0;
memcpy(&mViewport[mCurrentViewpoint],viewport,sizeof(mViewport[mCurrentViewpoint]));
PLATFORM.SetViewport(viewport);
memcpy(&mCurrentViewport,viewport,sizeof(mCurrentViewport));
// Clear entire viewport (the whole screen)
#if TARGET == PC
LT.D3D_Clear( 0, NULL, D3DCLEAR_ZBUFFER, 0x00607080, 1.0f, 0L );
#else
LT.D3D_Clear( 0, NULL, D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL , 0x00607080, 1.0f, 0L );
// LT.D3D_Clear( 0, NULL, D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL | D3DCLEAR_TARGET, 0x00607080, 1.0f, 0L );
#endif
{
PROFILE_FN(RenderEndScene);
PLATFORM.EndScene();
}
// Update the ripple effect
mRipplePhase+=1;
return(S_OK);
}
//=============================================-===--==---=---- --- -- -
#if TARGET == XBOX
float SUN_TEST_SIZE = 1.2f;
float SUN_FAR_AWAY = 100.f;
DWORD SUN_COLOUR = 0x00ffffff;
static UINT num_sun_pixels_drawn[4];
static bool test_number_available[8] = {true, true, true, true};
static float OVERALL_SUN_SCALER = 2.f;
// let's draw a little sprite and see if any pixels are visible
static void draw_sun_test_sprite()
{
// We actually draw 4 sprites so the sun moves around in some clever way.
for (DWORD i = 0; i < 4; i++)
{
// pick first slot after the vis tester class' slots
const DWORD TEST_NUMBER = CXBOXVisibilityTester::NUM_SLOTS + i;
// because visibility tests take time to come through, i'll have two available for each section
if (test_number_available[i] || test_number_available[i + 4])
{
LT.GetDevice()->BeginVisibilityTest();
const SHeightFieldProperties &hfp=MAP.GetHFProperties();
FVector sun(hfp.SunPosX,hfp.SunPosY,hfp.SunPosZ);
// make the sprite screen-aligned
FMatrix vCamOrient = ENGINE.GetCamera()->GetOrientation();
FVector x( vCamOrient.Row[0].X, vCamOrient.Row[1].X, vCamOrient.Row[2].X);
FVector y(-vCamOrient.Row[0].Z, -vCamOrient.Row[1].Z, -vCamOrient.Row[2].Z);
FVector z( vCamOrient.Row[0].Y, vCamOrient.Row[1].Y, vCamOrient.Row[2].Y);
// work out something incredibly far away.
sun.Normalise();
// but we must make sure we're always testing a similar number of pixels. When the sun rotates near to the edge of the screen the number of
// pixels increases thanks to the marvels of the perspective transform.
// so we get the projected length of the sun vector along the view z axis
float projected_sun_length = sun * z;
sun *= SUN_FAR_AWAY / projected_sun_length;
x *= SUN_TEST_SIZE;
y *= SUN_TEST_SIZE;
FVector centre = ENGINE.GetCamera()->GetPos() + sun;
// now draw one of 4 sprites.
if (i & 1) centre -= x; else centre += x;
if (i & 2) centre -= y; else centre += y;
CPARTICLEFVF_GTEX verts[4];
verts[0].mPos = centre - x + y;
verts[1].mPos = centre + x + y;
verts[2].mPos = centre - x - y;
verts[3].mPos = centre + x - y;
verts[0].mDiffuse = SUN_COLOUR;
verts[1].mDiffuse = SUN_COLOUR;
verts[2].mDiffuse = SUN_COLOUR;
verts[3].mDiffuse = SUN_COLOUR;
verts[0].mU = 0.f;
verts[1].mU = 0.f;
verts[2].mU = 0.f;
verts[3].mU = 0.f;
verts[0].mV = 0.f;
verts[1].mV = 0.f;
verts[2].mV = 0.f;
verts[3].mV = 0.f;
RENDERINFO.SetWorld(ZERO_FVECTOR, ID_FMATRIX);
LT.SRS(D3DRS_ALPHATESTENABLE,FALSE);
RENDERINFO.STS(0, TSS_COLOROP, TOP_MODULATE);
RENDERINFO.Apply();
LT.D3D_SetVertexShader(D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1);
ENGINE.GetDefaultTexture()->SelectTexture();
ENGINE.GetDefaultTexture()->SetupRenderMode();
RENDERINFO.Apply();
LT.D3D_DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, verts, sizeof(verts[0]));
LT.SRS(D3DRS_ALPHATESTENABLE,TRUE);
RENDERINFO.STS(0, TSS_COLOROP, TOP_MODULATE2X);
RENDERINFO.Apply();
// that means we've used the test.
if (test_number_available[i])
{
test_number_available[i] = false;
LT.GetDevice()->EndVisibilityTest(TEST_NUMBER);
}
else
{
test_number_available[i + 4] = false;
LT.GetDevice()->EndVisibilityTest(TEST_NUMBER + 4);
}
}
// are any results pending?
if (!test_number_available[i] && SUCCEEDED(LT.GetDevice()->GetVisibilityTestResult(TEST_NUMBER, &num_sun_pixels_drawn[i], NULL)))
{
// the test is done. we can go again.
test_number_available[i] = true;
}
// are any results pending?
if (!test_number_available[i + 4] && SUCCEEDED(LT.GetDevice()->GetVisibilityTestResult(TEST_NUMBER, &num_sun_pixels_drawn[i], NULL)))
{
// the test is done. we can go again.
test_number_available[i + 4] = true;
}
}
}
#endif
//=============================================-===--==---=---- --- -- -
static CVisibilityTester sea_visibility_tester;
//=============================================-===--==---=---- --- -- -
void CDXEngine::ShutdownRestartLoop()
{
// We don't want the sea carrying over its visibility.
#if TARGET == XBOX
sea_visibility_tester.MissATest = 1;
#endif
}
// This is stuff that can be drawn during the VBLANK if you get really silly.
static void draw_fairly_late_stuff(CPlayer *player, int viewpoint)
{
//***********************************************************************
//** Backup values for HUD
RENDERINFO.GetWorld(&ENGINE.mWorldMatrices[viewpoint]);
RENDERINFO.GetView(&ENGINE.mViewMatrices[viewpoint]);
RENDERINFO.GetProjection(&ENGINE.mProjectionMatrices[viewpoint]);
//*****************************************
//** Render Cockpit
#ifdef DEV_VERSION
if(!CLIPARAMS.mKillHUD)
#endif
{
if (player)
{
CBattleEngine *be = player->GetBattleEngine();
if (be && (!be->IsDying()) && (player->ShouldRenderInternalCockpit() == TRUE))
{
// Turn the z-buffer back on for the sake of the sun visibility tester.
#if TARGET == XBOX
// but why doesn't it work? I give up and check in.
#endif
ENGINE.EnableAlpha();
// LT.SRS(D3DRS_LIGHTING,TRUE);
RENDERINFO.SetLightingEnabled(TRUE);
be->GetCockpit()->Render() ;
RENDERQUEUE.Render();
RENDERINFO.SetLightingEnabled(FALSE);
// LT.SRS(D3DRS_LIGHTING,FALSE);
}
}
}
// Render screen FX
SCREENFX.Render();
// Debug - draw reflection texture
#if TARGET == XBOX
// late sun.
//** SUN
if (!(GAME.IsPaused()))
{
const SHeightFieldProperties &hfp=MAP.GetHFProperties();
if (hfp.VisibleSun)
{
// let's draw a little sprite and see if any pixels are visible
// but draw it later.
draw_sun_test_sprite();
}
}
#endif
}
static FMatrix prev_vmatrix;
HRESULT CDXEngine::Render(int viewpoint)
{
#if TARGET == XBOX
if (is_the_every_other)
{
FMatrix matview;
FVector campos = mCamera[mCurrentViewpoint]->GetPos();
GetViewMatrixFromCamera(mCamera[mCurrentViewpoint], &matview);
RENDERINFO.SetView(&matview, &campos);
float zoom = mCamera[mCurrentViewpoint]->GetZoom();
float howfar = 700;
RENDERINFO.SetProjection(mNearZ,howfar,mNearZ*zoom,mNearZ*zoom*mCamera[mCurrentViewpoint]->GetAspectRatio());
mGamut->MakeIt(&matview,2048,zoom*1.0f,zoom*mCamera[mCurrentViewpoint]->GetAspectRatio(),&campos,false);
// Calculate LODs as early as possible to 'avoid stalls'
mLandscape->CalcLODs(mGamut, viewpoint);
return S_OK;
}
else if (LT.GetFlipMethod() == CXBOXDX::FULL_SYNC_LATE_HUD_MY_COPY)
{
// We're actually drawing. Let's store the view matrix.
GetViewMatrixFromCamera(mCamera[mCurrentViewpoint], &prev_vmatrix);
}
#endif
// FVector sunaspassedtowater;
if (!mCamera[viewpoint])
return S_OK; //! JCL - er..!!
SelectViewpoint(viewpoint);
// Create Gamut
float zoom = mCamera[mCurrentViewpoint]->GetZoom();
FMatrix matview;
FVector campos = mCamera[mCurrentViewpoint]->GetPos();
GetViewMatrixFromCamera(mCamera[mCurrentViewpoint], &matview);
mGamut->MakeIt(&matview,2048,zoom*1.0f,zoom*mCamera[mCurrentViewpoint]->GetAspectRatio(),&campos,false);
// Calculate LODs as early as possible to 'avoid stalls'
#if TARGET == XBOX
if (LT.GetFlipMethod() == CXBOXDX::FULL_SYNC_LATE_HUD_MY_COPY) goto dont_do_lods_now;
#endif
if (GAME.IsMultiplayer())
{
static int which_view_lod = 0;
if (viewpoint == which_view_lod)
{
mLandscape->CalcLODs(mGamut, viewpoint);
}
which_view_lod ^= 1;
}
else
{
mLandscape->CalcLODs(mGamut, viewpoint);
}
dont_do_lods_now:
// And then start doing drawing.
STATE.UseDefault();
RENDERQUEUE.Reset();
RENDERINFO.SetView(&matview, &campos);
// Render shadows
SwitchAllLights(FALSE); // don't want lights showing on shadows
{
PROFILE_FN(RenderShadowTextures);
SHADOWS.RenderTextures(); // render shadows picked last frame
// this only scribles on the z-buffer
CHECK_D3D_STATE("Shadow creation",0,0);
}
PROFILE_START(TotalRender);
{
PROFILE_FN(RenderBeginScene);
BOOL bs_ok = PLATFORM.BeginScene();
if (!bs_ok)
return S_OK;
}
PLATFORM.SetViewport(&mViewport[mCurrentViewpoint]);
SwitchAllLights(TRUE);
ASSERT(LT.D3DActive());
// Move debris into position around the camera
CDebris::ShuffleDebris(mCamera[mCurrentViewpoint]->GetPos());
float wlevel;
wlevel = MAP.GetWaterLevel();
// Horible hack stuff here
FMatrix bbori = mCamera[mCurrentViewpoint]->GetOrientation();
float angle = atan2f(bbori.Row[1].X,bbori.Row[1].Y);
CIMPOSTER::SetBillBoardAngle(angle);
// bbori.MakeRotationYawF(angle);
// D3DUtils.SetBillBoardMat(&bbori);
SetupLights();
mLights->Process();
// finish setting up the fog
RENDERINFO.SetFogColour(MAP.GetFog());
float density = MAP.GetFogDensity();
RENDERINFO.SetFogDensity(density);
//********************************************************
//** SKY
// Draw Sky after munging up a large view matrix
{
PROFILE_FN(RenderCube);
float vfar = 1*(float)sqrt((SKY_HEIGHT*SKY_HEIGHT)+((SKY_CELL_SIZE*SKY_RAD)*(SKY_CELL_SIZE*SKY_RAD)));
RENDERINFO.SetProjection(1.0f,vfar, zoom ,zoom*mCamera[mCurrentViewpoint]->GetAspectRatio());
mKempyCube->Render();
CHECK_D3D_STATE("Kempycube",0,0);
}
//********************************************************
//** LANDSCAPE
// set up the real view
float howfar = 700;
RENDERINFO.SetProjection(mNearZ,howfar,mNearZ*zoom,mNearZ*zoom*mCamera[mCurrentViewpoint]->GetAspectRatio());
// used for landscape, objects, shadows, particles, we can finally back the bastard up
if (mRenderLandscape)
{
mLandscape->Render(mGamut, viewpoint);
CHECK_D3D_STATE("Landscape",0,0);
SHADOWS.RenderLandscapeShadows();
CHECK_D3D_STATE("Shadows",0,0);
ReallySetDefaultMaterial();
}
SHADOWS.TestDraw();
CHECK_D3D_STATE("Shadow test drawing",0,0);
// Capture needs to be done here for cloaked stuff
#if TARGET == PC
if (mCaptureScreen)
{
int top=(LT.GetWindowHeight()*mCaptureAreaTop)/1000;
int bottom=(LT.GetWindowHeight()*mCaptureAreaBottom)/1000;
LPDIRECT3DSURFACE8 backbuffer;
LT.D3D_GetRenderTarget( &backbuffer );
mScreenTexture->GrabFromSurface(backbuffer,0,top,0,top,LT.GetWindowWidth(),bottom-top,false);
backbuffer->Release();
mCaptureScreen=false;
}
#endif
//********************************************************
//** WORLD
SHADOWS.ResetShads(); // clear shadow list
LT.SRS(D3DRS_ALPHATESTENABLE, TRUE );
DWORD rsx[]={D3DRS_ALPHATESTENABLE,0};
{
PROFILE_FN(RenderThings);
LT.STS(0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR); // trilinear on
LT.STS(0, D3DTSS_MINFILTER, D3DTEXF_ANISOTROPIC); // aniso on
LT.STS(0, D3DTSS_MAXANISOTROPY, 4);
float bias = -1.0f;
LT.STS(0,D3DTSS_MIPMAPLODBIAS, *((LPDWORD) (&bias)));
WORLD.Render(mGamut,FALSE,SHADOWS.BlobShadowsEnabled()); // render objects
LT.STS(0,D3DTSS_MIPMAPLODBIAS, 0);
LT.STS(0, D3DTSS_MIPFILTER, D3DTEXF_POINT); // trilinear off
LT.STS(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR); // bilinear on
LT.STS(0, D3DTSS_MAXANISOTROPY, 1);
}
{
PROFILE_FN(RenderImposters);
// WORLD.RenderImposters(mGamut) ; // render imposters
CHECK_D3D_STATE("World Imposters",rsx,0);
CIMPOSTER::RenderAllImposters(); // Render cache of all imposter stuff
CHECK_D3D_STATE("World Imposters",rsx,0);
}
// Render fast trees
TREES.Render();
// Update RTMesh render tracking stuff, kill redundant FoRs, etc
CRTMesh::UpdateRenderLists();
// water moved here
CHECK_D3D_STATE("Before Water",0,0);
if (sea_visibility_tester.BotherDrawing())
{
// The water is thought to be visible; draw it.
SetModulate2X(FALSE);
RENDERINFO.SetLightingEnabled(FALSE);
if (!mRenderReflections)
{
// if (wlevel<0) // why bother, all levels now have water
{
mWater->Render(wlevel);//, sunaspassedtowater);
CHECK_D3D_STATE("After Water",0,0);
}
}
RENDERINFO.SetLightingEnabled(TRUE);
SetModulate2X(TRUE);
sea_visibility_tester.DrawingFinished();
}
DEBUGMARKERS.Render(); // Render debug markers
{
PROFILE_FN(RenderShadows);
CHECK_D3D_STATE("World",rsx,0);
// SHADOWS.RenderObjectShadows(); // Render object shadows
CHECK_D3D_STATE("Shadows",rsx,0);
}
if (SHADOWS.BlobShadowsEnabled())
{
LT.SRS(D3DRS_ALPHABLENDENABLE, TRUE);
SHADOWS.DrawAllBlobShadows(); // Render VB contents out
CHECK_D3D_STATE("Blob shadows",rsx,0);
}
LT.SRS(D3DRS_ALPHATESTENABLE, FALSE );
// Update particle system
if (!GAME.IsPaused())
{
if (!ENGINE.DebugNoParticles())
if (viewpoint == 0)
PARTICLE_MANAGER.Process(GAME.GetRenderFrameLength(), GAME.IsFirstFrame());
}
if (!DebugNoParticles())
{
if (viewpoint == 0)
{
PROFILE_FN(PARTDoInterpolation);
PARTICLE_MANAGER.DoInterpolation(); // Needs to be after things are drawn and before the particles.
}
{
PROFILE_FN(RenderParticleMeshes);
RENDERINFO.SetFogEnabled(true);
PARTICLE_MANAGER.RenderMeshes(); // needs to be here to share the lighting model..
}
}
CHECK_D3D_STATE("Particles",0,0);
RENDERQUEUE.Render(); // Render all our batched stuff
//#endif
// Render effects
CAtmospherics::RenderAll();
CHECK_D3D_STATE("Atmospherics",0,0);
// Fog
// CFog::Render();
// Smoke
// CSmoke::Render();
// Rain
// jcl_water_test();
// Gamut debug
#ifdef VIS_GAMUT
mGamut->RenderGamut();
#endif
CHECK_D3D_STATE("Gamut debug",0,0);
//********************************************************
//** MAPWHO DEBUG
if (mDrawDebugStuff & DRAW_MAP_WHO)
{
EnableAlpha() ;
MAP_WHO.DrawMapWhoDebug() ;
CHECK_D3D_STATE("Mapwhodbug",0,0);
}
//CIMPOSTER::TestDrawAll();
//***********************************************************************
//** SUN
if (!(GAME.IsPaused()))
{
const SHeightFieldProperties &hfp=MAP.GetHFProperties();
if (hfp.VisibleSun)
{
float SUN_SCALE = 0.6f;
#if TARGET == XBOX
// we work out where to draw the sun based on how many pixels of our tester shapes were drawn
// these things can draw varying amounts because of the triangle draw so we'd better clamp the maxima
// And it all gets really noisy with low numbers of pixels so let's clamp that too.
static int SMALL_LARGE_NUMBER_OF_PIXELS = 49;
static int SMALL_SMALL_NUMBER_OF_PIXELS = 5;
UINT num_sun_pixels_drawn_local[4];
for (int cc = 0; cc < 4; cc++)
{
num_sun_pixels_drawn_local[cc] = num_sun_pixels_drawn[cc];
if (num_sun_pixels_drawn_local[cc] > SMALL_LARGE_NUMBER_OF_PIXELS) num_sun_pixels_drawn_local[cc] = SMALL_LARGE_NUMBER_OF_PIXELS;
// if we just do a clamp at the minimum it's still noisy, we need to fade off
if (num_sun_pixels_drawn_local[cc] == SMALL_SMALL_NUMBER_OF_PIXELS - 1) num_sun_pixels_drawn_local[cc] = SMALL_SMALL_NUMBER_OF_PIXELS / 2;
else
if (num_sun_pixels_drawn_local[cc] == SMALL_SMALL_NUMBER_OF_PIXELS - 2) num_sun_pixels_drawn_local[cc] = SMALL_SMALL_NUMBER_OF_PIXELS / 4;
else
if (num_sun_pixels_drawn_local[cc] < SMALL_SMALL_NUMBER_OF_PIXELS - 2) num_sun_pixels_drawn_local[cc] = 0;