-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathbLois.cpp
More file actions
1587 lines (1247 loc) · 49.9 KB
/
bLois.cpp
File metadata and controls
1587 lines (1247 loc) · 49.9 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
//-----------------------------------------------------------------------------
// File: bLois.cpp
//
// Desc: DirectX window application created by the DirectX AppWizard
//-----------------------------------------------------------------------------
#define STRICT
#include <windows.h>
#include <commctrl.h>
#include <commdlg.h>
#include <basetsd.h>
#include <direct.h>
#include <math.h>
#include <stdio.h>
#include <d3dx9.h>
#include <dxerr9.h>
#include <tchar.h>
#include "DXUtil.h"
#include "D3DEnumeration.h"
#include "D3DSettings.h"
#include "D3DApp.h"
#include "D3DFont.h"
#include "D3DFile.h"
#include "D3DUtil.h"
#include "resource.h"
#include "bLois.h"
#include <algorithm>
#include <functional>
static float RandMinusOneToOne()
{
return float( double(rand()) / double(RAND_MAX) * 2.0 - 1.0 );
}
static float RandZeroToOne()
{
return float( double(rand()) / double(RAND_MAX) );
}
static const float kGravConst = 30.f;
class ClearVert
{
public:
D3DXVECTOR3 m_Pos;
D3DXVECTOR2 m_Uv;
};
static const int kVSize = sizeof(ClearVert);
static const DWORD kClearVertFVF = D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE2(0);
//-----------------------------------------------------------------------------
// Global access to the app (needed for the global WndProc())
//-----------------------------------------------------------------------------
CMyD3DApplication* g_pApp = NULL;
HINSTANCE g_hInst = NULL;
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: Entry point to the program. Initializes everything, and goes into a
// message-processing loop. Idle time is used to render the scene.
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
CMyD3DApplication d3dApp;
g_pApp = &d3dApp;
g_hInst = hInst;
InitCommonControls();
if( FAILED( d3dApp.Create( hInst ) ) )
return 0;
return d3dApp.Run();
}
//-----------------------------------------------------------------------------
// Name: CMyD3DApplication()
// Desc: Application constructor. Paired with ~CMyD3DApplication()
// Member variables should be initialized to a known state here.
// The application window has not yet been created and no Direct3D device
// has been created, so any initialization that depends on a window or
// Direct3D should be deferred to a later stage.
//-----------------------------------------------------------------------------
CMyD3DApplication::CMyD3DApplication()
{
m_dwCreationWidth = 500;
m_dwCreationHeight = 375;
m_strWindowTitle = TEXT( "bLois" );
m_d3dEnumeration.AppUsesDepthBuffer = TRUE;
m_bStartFullscreen = false;
m_bShowCursorWhenFullscreen = false;
m_bShowHelp = false;
m_bSortWater = true;
m_bDrawBump = false;
m_LastToggle = 0.f;
// Create a D3D font using d3dfont.cpp
m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
m_bLoadingApp = TRUE;
memset(m_bKey, 0x00, sizeof(m_bKey));
D3DXMatrixIdentity(&m_matView);
D3DXMatrixIdentity(&m_matPosition);
D3DXMatrixIdentity(&m_matProjection);
m_CompCosinesEff = NULL;
m_WaterEff = NULL;
m_WaterMesh = NULL;
m_LandMesh = NULL;
m_PillarsMesh = NULL;
m_EnvMap = NULL;
m_LandTex = NULL;
m_CosineLUT = NULL;
m_BiasNoiseMap = NULL;
m_BumpTex = NULL;
m_BumpSurf = NULL;
m_BumpRender = NULL;
m_BumpVBuffer = NULL;
m_WaterIndices = NULL;
m_WaterFacePos = NULL;
m_WaterSortData = NULL;
ResetWater();
}
//-----------------------------------------------------------------------------
// Name: ~CMyD3DApplication()
// Desc: Application destructor. Paired with CMyD3DApplication()
//-----------------------------------------------------------------------------
CMyD3DApplication::~CMyD3DApplication()
{
delete [] m_WaterIndices;
delete [] m_WaterFacePos;
delete [] m_WaterSortData;
}
//-----------------------------------------------------------------------------
// Name: OneTimeSceneInit()
// Desc: Paired with FinalCleanup().
// The window has been created and the IDirect3D9 interface has been
// created, but the device has not been created yet. Here you can
// perform application-related initialization and cleanup that does
// not depend on a device.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::OneTimeSceneInit()
{
// TODO: perform one time initialization
// Drawing loading status message until app finishes loading
SendMessage( m_hWnd, WM_PAINT, 0, 0 );
m_bLoadingApp = FALSE;
// Misc stuff
D3DXMatrixLookAtLH(&m_matView,
&D3DXVECTOR3(0.f, -150.f, 50.f),
&D3DXVECTOR3(0.f, 0.f, 0.f),
&D3DXVECTOR3(0.f, 0.f, 1.f));
D3DXMatrixInverse(&m_matPosition, NULL, &m_matView);
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: ConfirmDevice()
// Desc: Called during device initialization, this code checks the display device
// for some minimum set of capabilities
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps, DWORD dwBehavior,
D3DFORMAT Format )
{
UNREFERENCED_PARAMETER( Format );
UNREFERENCED_PARAMETER( dwBehavior );
UNREFERENCED_PARAMETER( pCaps );
BOOL bCapsAcceptable;
// TODO: Perform checks to see if these display caps are acceptable.
bCapsAcceptable = TRUE;
if( bCapsAcceptable )
return S_OK;
else
return E_FAIL;
}
//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Paired with DeleteDeviceObjects()
// The device has been created. Resources that are not lost on
// Reset() can be created here -- resources in D3DPOOL_MANAGED,
// D3DPOOL_SCRATCH, or D3DPOOL_SYSTEMMEM. Image surfaces created via
// CreateImageSurface are never lost and can be created here. Vertex
// shaders and pixel shaders can also be created here as they are not
// lost on Reset().
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InitDeviceObjects()
{
// TODO: create device objects
HRESULT hr;
// Init the font
m_pFont->InitDeviceObjects( m_pd3dDevice );
if( FAILED(hr = CreateCosineLUT()) )
return hr;
if( FAILED(hr = CreateBiasNoiseMap()) )
return hr;
if( FAILED(hr = D3DXCreateCubeTextureFromFile( m_pd3dDevice, TEXT("nvlobby_new_cube_mipmap.dds"), &m_EnvMap)) )
return DXTRACE_ERR( "EnvMap load", hr );
if( FAILED(hr = D3DXCreateTextureFromFile( m_pd3dDevice, "Sandy.dds", &m_LandTex)) )
return hr;
if( FAILED(hr = D3DXLoadMeshFromX(TEXT("LandMesh.x"), D3DXMESH_MANAGED, m_pd3dDevice, NULL, NULL, NULL, NULL, &m_LandMesh)) )
return hr;
if( FAILED(hr = D3DXLoadMeshFromX(TEXT("pillars.x"), D3DXMESH_MANAGED, m_pd3dDevice, NULL, NULL, NULL, NULL, &m_PillarsMesh)) )
return hr;
ID3DXMesh* waterMesh = NULL;
if( FAILED(hr = D3DXLoadMeshFromX(TEXT("WaterMesh.x"), D3DXMESH_MANAGED, m_pd3dDevice, NULL, NULL, NULL, NULL, &waterMesh)) )
return hr;
if( FAILED( hr = CreateWaterMesh(waterMesh)) )
return hr;
if( FAILED(hr = CreateClearBuffer()) )
return hr;
LPD3DXBUFFER pBufferErrors = NULL;
if( FAILED( hr = D3DXCreateEffectFromFile( m_pd3dDevice, "CompCosines.fx", NULL, NULL,
0, NULL, &m_CompCosinesEff, &pBufferErrors) ) )
{
char* errStr = (char*)pBufferErrors->GetBufferPointer();
OutputDebugString(errStr);
return hr;
}
GetCompCosineEffParams();
if( FAILED(hr = D3DXCreateEffectFromFile( m_pd3dDevice, "WaterRip.fx", NULL, NULL,
0, NULL, &m_WaterEff, &pBufferErrors) ) )
{
char* errStr = (char*)pBufferErrors->GetBufferPointer();
OutputDebugString(errStr);
return hr;
}
GetWaterParams();
return S_OK;
}
HRESULT CMyD3DApplication::CreateWaterMesh(ID3DXMesh* waterMesh)
{
// All this should happen off line. In particular, note the assumption of even
// spacing in calculating the edge lengths, which defeats the whole purpose.
// Still, it's a demo, not a game.
struct OutVert
{
D3DXVECTOR3 m_Pos;
DWORD m_Color;
};
const DWORD kWaterFVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;
HRESULT hr = waterMesh->CloneMeshFVF(D3DXMESH_MANAGED,
kWaterFVF,
m_pd3dDevice,
&m_WaterMesh);
SAFE_RELEASE(waterMesh);
if( FAILED(hr) )
return hr;
const int nVerts = m_WaterMesh->GetNumVertices();
OutVert* oVert;
if( FAILED(hr = m_WaterMesh->LockVertexBuffer(0, (void**)&oVert)) )
return hr;
OutVert* origVert = oVert;
D3DXVECTOR3 del = oVert[0].m_Pos - oVert[1].m_Pos;
float dist = D3DXVec3Length(&del);
if( dist < 1.f )
dist = 1.f;
UINT alpha = UINT(255.9f / dist);
int i;
for( i = 0; i < nVerts; i++ )
{
oVert->m_Color = alpha << 24 | 0x00ffffff;
oVert++;
}
oVert = origVert;
const int nFaces = m_WaterMesh->GetNumFaces();
m_WaterIndices = new FaceIndices[nFaces];
m_WaterFacePos = new D3DXVECTOR3[nFaces];
m_WaterSortData = new FaceSortData[nFaces];
DWORD* oIdx;
if( FAILED(hr = m_WaterMesh->LockIndexBuffer(0, (void**)&oIdx)) )
{
m_WaterMesh->UnlockVertexBuffer();
return hr;
}
memcpy(m_WaterIndices, oIdx, nFaces * sizeof(*m_WaterIndices));
for( i = 0; i < nFaces; i++ )
{
D3DXVECTOR3 pos = oVert[m_WaterIndices[i].m_Idx[0]].m_Pos;
pos += oVert[m_WaterIndices[i].m_Idx[1]].m_Pos;
pos += oVert[m_WaterIndices[i].m_Idx[2]].m_Pos;
pos.z = 0;
pos /= 3.f;
m_WaterFacePos[i] = pos;
}
m_WaterMesh->UnlockIndexBuffer();
m_WaterMesh->UnlockVertexBuffer();
return hr;
}
struct CompSortFace : public std::binary_function<FaceSortData, FaceSortData, bool>
{
bool operator()( const FaceSortData& lhs, const FaceSortData& rhs) const
{
return lhs.m_Dist < rhs.m_Dist;
}
};
void CMyD3DApplication::SortWaterMesh()
{
const D3DXVECTOR3 eyePos(m_matPosition._41, m_matPosition._42, 0.f);
const int nFaces = m_WaterMesh->GetNumFaces();
int i;
for( i = 0; i < nFaces; i++ )
{
D3DXVECTOR3 del(eyePos - m_WaterFacePos[i]);
m_WaterSortData[i].m_Dist = D3DXVec3LengthSq(&del);
m_WaterSortData[i].m_Idx = i;
}
FaceSortData* begin = m_WaterSortData;
FaceSortData* end = begin + nFaces;
std::sort(begin, end, CompSortFace());
FaceIndices* oIdx;
if( FAILED(m_WaterMesh->LockIndexBuffer(0, (void**)&oIdx)) )
{
m_WaterMesh->UnlockVertexBuffer();
return;
}
for( i = 0; i < nFaces; i++ )
{
oIdx[i].m_Idx[0] = m_WaterIndices[m_WaterSortData[i].m_Idx].m_Idx[0];
oIdx[i].m_Idx[1] = m_WaterIndices[m_WaterSortData[i].m_Idx].m_Idx[1];
oIdx[i].m_Idx[2] = m_WaterIndices[m_WaterSortData[i].m_Idx].m_Idx[2];
}
m_WaterMesh->UnlockIndexBuffer();
}
HRESULT CMyD3DApplication::CreateClearBuffer()
{
HRESULT hr;
if( FAILED( hr = m_pd3dDevice->CreateVertexBuffer( 4 * kVSize,
D3DUSAGE_WRITEONLY,
0,
D3DPOOL_MANAGED,
&m_BumpVBuffer,
NULL) ) )
{
return hr;
}
ClearVert* ptr;
if( FAILED( hr = m_BumpVBuffer->Lock( 0, 0, (void **)&ptr, 0 ) ) )
return hr;
ptr[2].m_Pos.x = -1.f;
ptr[2].m_Pos.y = -1.f;
ptr[2].m_Pos.z = 0.5f;
ptr[2].m_Uv.x = 0.5f / kBumpTexSize;
ptr[1].m_Uv.y = 0.5f / kBumpTexSize;
ptr[0] = ptr[2];
ptr[0].m_Pos.y += 2.f;
ptr[0].m_Uv.y += 1.f;
ptr[1] = ptr[2];
ptr[1].m_Pos.x += 2.f;
ptr[1].m_Pos.y += 2.f;
ptr[1].m_Uv.x += 1.f;
ptr[1].m_Uv.y += 1.f;
ptr[3] = ptr[2];
ptr[3].m_Pos.x += 2.f;
ptr[3].m_Uv.x += 1.f;
m_BumpVBuffer->Unlock();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Paired with InvalidateDeviceObjects()
// The device exists, but may have just been Reset(). Resources in
// D3DPOOL_DEFAULT and any other device state that persists during
// rendering should be set here. Render states, matrices, textures,
// etc., that don't change during rendering can be set once here to
// avoid redundant state setting during Render() or FrameMove().
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
// TODO: setup render states
// Setup a material
D3DMATERIAL9 mtrl;
D3DUtil_InitMaterial( mtrl, 1.0f, 1.0f, 1.0f );
m_pd3dDevice->SetMaterial( &mtrl );
// Set up the textures
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
// Set miscellaneous render states
m_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x000F0F0F );
m_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
// Set the world matrix
D3DXMATRIX matIdentity;
D3DXMatrixIdentity( &matIdentity );
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matIdentity );
// Set the projection matrix
FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
D3DXMatrixPerspectiveFovLH( &m_matProjection, D3DX_PI/4, fAspect, 1.0f, 10000.0f );
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &m_matProjection);
// Set up lighting states
D3DLIGHT9 light;
D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, -1.0f, -1.0f, -2.0f );
m_pd3dDevice->SetLight( 0, &light );
m_pd3dDevice->LightEnable( 0, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
// Restore the font
m_pFont->RestoreDeviceObjects();
HRESULT hr;
// Create our bump map. We'll composite the normals of our texture waves into this with renders.
if(FAILED(hr = D3DXCreateTexture(m_pd3dDevice, kBumpTexSize, kBumpTexSize, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_BumpTex)) &&
FAILED(hr = D3DXCreateTexture(m_pd3dDevice, kBumpTexSize, kBumpTexSize, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_BumpTex)))
{
return hr;
}
D3DSURFACE_DESC desc;
m_BumpTex->GetSurfaceLevel(0, &m_BumpSurf);
m_BumpSurf->GetDesc(&desc);
if(FAILED(hr = D3DXCreateRenderToSurface(m_pd3dDevice, desc.Width, desc.Height,
desc.Format, FALSE, D3DFMT_UNKNOWN, &m_BumpRender)))
return hr;
m_CompCosinesEff->OnResetDevice();
m_WaterEff->OnResetDevice();
return S_OK;
}
static void Clamp(float& val, float lo, float hi)
{
if( val < lo )
val = lo;
else if( val > hi )
val = hi;
}
void CMyD3DApplication::MoveOnInput()
{
//
// Process keyboard input
//
D3DXVECTOR3 vecT(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 vecR(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 vecZ(0.0f, 0.0f, 0.0f);
if(m_bKey[VK_NUMPAD1] || m_bKey[VK_LEFT]) vecT.x -= 1.0f; // Slide Left
if(m_bKey[VK_NUMPAD3] || m_bKey[VK_RIGHT]) vecT.x += 1.0f; // Slide Right
if(m_bKey[VK_DOWN]) vecT.y -= 1.0f; // Slide Down
if(m_bKey[VK_UP]) vecT.y += 1.0f; // Slide Up
if(m_bKey['W']) vecT.z += 2.0f; // Move Forward
if(m_bKey['S']) vecT.z -= 2.0f; // Move Backward
if(m_bKey['A'] || m_bKey[VK_NUMPAD8]) vecR.x -= 1.0f; // Pitch Down
if(m_bKey['Z'] || m_bKey[VK_NUMPAD2]) vecR.x += 1.0f; // Pitch Up
if(m_bKey['E'] || m_bKey[VK_NUMPAD6]) vecZ.z -= 1.0f; // Turn Right
if(m_bKey['Q'] || m_bKey[VK_NUMPAD4]) vecZ.z += 1.0f; // Turn Left
if(m_bKey[VK_NUMPAD9]) vecR.z -= 2.0f; // Roll CW
if(m_bKey[VK_NUMPAD7]) vecR.z += 2.0f; // Roll CCW
const float speed = 10.f;
const float angSpeed = D3DX_PI / 5.f;
vecT *= speed * m_fElapsedTime;
vecR *= angSpeed * m_fElapsedTime;
vecZ *= angSpeed * m_fElapsedTime;
if(m_bKey[VK_SHIFT])
{
vecT *= 4.f;
vecR *= 4.f;
vecZ *= 4.f;
}
//
// Update position and view matricies
//
D3DXMATRIXA16 matT, matR, matZ;
D3DXQUATERNION qR;
D3DXQUATERNION qZ;
D3DXMatrixTranslation(&matT, vecT.x, vecT.y, vecT.z);
D3DXMatrixMultiply(&m_matPosition, &matT, &m_matPosition);
D3DXQuaternionRotationYawPitchRoll(&qR, vecR.y, vecR.x, vecR.z);
D3DXMatrixRotationQuaternion(&matR, &qR);
D3DXQuaternionRotationYawPitchRoll(&qZ, vecZ.y, vecZ.x, vecZ.z);
D3DXMatrixRotationQuaternion(&matZ, &qZ);
D3DXMatrixMultiply(&m_matPosition, &matR, &m_matPosition);
D3DXMatrixMultiply(&m_matPosition, &m_matPosition, &matZ);
D3DXMatrixInverse(&m_matView, NULL, &m_matPosition);
float timeScale = m_bKey[VK_SHIFT] ? m_fElapsedTime : -m_fElapsedTime;
// MoreOptions
// Debounce the toggles
const float kMinToggle = 0.5f;
if( m_fTime - m_LastToggle > kMinToggle )
{
// Reset
if(m_bKey['R'])
{
if( m_bKey[VK_SHIFT] )
ResetWater();
else
InitWaves();
m_LastToggle = m_fTime;
}
// Show bump
if(m_bKey['B'])
{
m_bDrawBump = !m_bDrawBump;
m_LastToggle = m_fTime;
}
if(m_bKey[VK_SPACE])
{
m_bShowHelp = !m_bShowHelp;
m_LastToggle = m_fTime;
}
}
// Water Height
if(m_bKey['H'])
m_GeoState.m_WaterLevel += timeScale * 3.f;
// Geo Wave Height
if(m_bKey['J'])
{
m_GeoState.m_AmpOverLen += timeScale * 0.05f;
Clamp(m_GeoState.m_AmpOverLen, 0.f, 0.1f);
}
// Geo Chop
if(m_bKey['K'])
{
m_GeoState.m_Chop += timeScale * 0.5f;
Clamp(m_GeoState.m_Chop, 0.f, 4.f);
}
// Tex Wave Height
if(m_bKey['U'])
{
m_TexState.m_AmpOverLen += timeScale * 0.05f;
Clamp(m_TexState.m_AmpOverLen, 0.f, 0.5f);
}
// Tex scale
if(m_bKey['Y'])
{
m_TexState.m_RippleScale += timeScale * 1.f;
Clamp(m_TexState.m_RippleScale, 5.f, 50.f);
}
if(m_bKey['N'])
{
m_TexState.m_Noise += timeScale * 0.1f;
Clamp(m_TexState.m_Noise, 0.f, 1.f);
}
if(m_bKey['O'])
{
m_GeoState.m_AngleDeviation += timeScale * 10.f;
Clamp(m_GeoState.m_AngleDeviation, 0.f, 180.f);
}
if(m_bKey['P'])
{
m_TexState.m_AngleDeviation += timeScale * 10.f;
Clamp(m_TexState.m_AngleDeviation, 0.f, 180.f);
}
if(m_bKey['G'])
{
m_GeoState.m_EnvRadius *= 1.f + timeScale * 0.5f;
Clamp(m_GeoState.m_EnvRadius, 100.f, 10000.f);
}
if(m_bKey['F'])
{
m_GeoState.m_EnvHeight += timeScale * 10.f;
Clamp(m_GeoState.m_EnvHeight, -100.f, 100.f);
}
}
//-----------------------------------------------------------------------------
// Name: FrameMove()
// Desc: Called once per frame, the call is the entry point for animating
// the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FrameMove()
{
// TODO: update world
// Update user input state
MoveOnInput();
UpdateTexWaves(m_fElapsedTime);
UpdateGeoWaves(m_fElapsedTime);
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: UpdateInput()
// Desc: Update the user input. Called once per frame
//-----------------------------------------------------------------------------
void CMyD3DApplication::UpdateInput( UserInput* pUserInput )
{
pUserInput->bRotateUp = ( m_bActive && (GetAsyncKeyState( VK_UP ) & 0x8000) == 0x8000 );
pUserInput->bRotateDown = ( m_bActive && (GetAsyncKeyState( VK_DOWN ) & 0x8000) == 0x8000 );
pUserInput->bRotateLeft = ( m_bActive && (GetAsyncKeyState( VK_LEFT ) & 0x8000) == 0x8000 );
pUserInput->bRotateRight = ( m_bActive && (GetAsyncKeyState( VK_RIGHT ) & 0x8000) == 0x8000 );
pUserInput->bMoveUp = ( m_bActive && (GetAsyncKeyState( VK_UP ) & 0x8000) == 0x8000 );
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Called once per frame, the call is the entry point for 3d
// rendering. This function sets up render states, clears the
// viewport, and renders the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::Render()
{
RenderTexture();
// Clear the viewport
m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
0x00707070, 1.0f, 0L );
// Begin the scene
if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
{
// TODO: render world
m_pd3dDevice->SetTransform(D3DTS_PROJECTION, &m_matProjection);
m_pd3dDevice->SetTransform(D3DTS_VIEW, &m_matView);
m_pd3dDevice->SetTexture(0, m_LandTex);
m_LandMesh->DrawSubset(0);
m_pd3dDevice->SetTexture(0, NULL);
m_PillarsMesh->DrawSubset(0);
RenderWater();
if( m_bDrawBump )
{
D3DXMATRIXA16 matIdent;
D3DXMatrixIdentity(&matIdent);
m_pd3dDevice->SetTransform(D3DTS_VIEW, &matIdent);
matIdent._11 = 0.5f;
matIdent._22 = 0.5f;
m_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matIdent);
m_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
m_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ZERO);
m_pd3dDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_ALWAYS);
m_pd3dDevice->SetTexture(0, m_BumpTex);
m_pd3dDevice->SetFVF(kClearVertFVF);
m_pd3dDevice->SetStreamSource(0, m_BumpVBuffer, 0, kVSize);
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
}
// Render stats and help text
RenderText();
// End the scene.
m_pd3dDevice->EndScene();
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: RenderText()
// Desc: Renders stats and help text to the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RenderText()
{
D3DCOLOR fontColor = D3DCOLOR_ARGB(255,255,255,0);
TCHAR szMsg[MAX_PATH] = TEXT("");
// Output display stats
FLOAT fNextLine = 40.0f;
lstrcpy( szMsg, m_strDeviceStats );
fNextLine -= 20.0f;
m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, m_strFrameStats );
fNextLine -= 20.0f;
m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
// Output statistics & help
fNextLine = (FLOAT) m_d3dsdBackBuffer.Height;
lstrcpy( szMsg, TEXT("Spacebar toggles help") );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
if( m_bShowHelp )
{
lstrcpy( szMsg, TEXT("Press 'F2' to configure display") );
fNextLine -= 40.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("'F' - Environment map height") );
fNextLine -= 40.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("'G' - Environment map radius") );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("'P' - Texture wave angle deviation") );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("'O' - Geometric wave angle deviation") );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("'N' - Texture Noise") );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("'Y' - Texture scaling") );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("'U' - Texture wave height") );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("'K' - Geometric wave choppiness") );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("'J' - Geometric wave height") );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("'H' - Water level") );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("Parameters - Shift goes up, non-shift goes down") );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("'B' - Show bump map") );
fNextLine -= 40.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("'r' - Reinitializes system with current settings") );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("'R' - Resets system settings to default and re-init") );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("Toggles:") );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("Left/right arrow - slide left/right") );
fNextLine -= 40.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("Up/down arrow - move up/down") );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("W/S - move forward/backward") );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("A/Z - pitch up/down") );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("E/Q - rotate scene right/left") );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
lstrcpy( szMsg, TEXT("Movement - Shift is faster:") );
fNextLine -= 20.0f; m_pFont->DrawText( 2, fNextLine, fontColor, szMsg );
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: Overrrides the main WndProc, so the sample can do custom message
// handling (e.g. processing mouse, keyboard, or menu commands).
//-----------------------------------------------------------------------------
LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT msg, WPARAM wParam,
LPARAM lParam )
{
switch( msg )
{
case WM_KEYDOWN:
m_bKey[wParam] = TRUE;
break;
case WM_KEYUP:
m_bKey[wParam] = FALSE;
break;
case WM_PAINT:
{
if( m_bLoadingApp )
{
// Draw on the window tell the user that the app is loading
// TODO: change as needed
HDC hDC = GetDC( hWnd );
TCHAR strMsg[MAX_PATH];
wsprintf( strMsg, TEXT("Loading... Please wait") );
RECT rct;
GetClientRect( hWnd, &rct );
DrawText( hDC, strMsg, -1, &rct, DT_CENTER|DT_VCENTER|DT_SINGLELINE );
ReleaseDC( hWnd, hDC );
}
break;
}
}
return CD3DApplication::MsgProc( hWnd, msg, wParam, lParam );
}
//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc: Invalidates device objects. Paired with RestoreDeviceObjects()
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
// TODO: Cleanup any objects created in RestoreDeviceObjects()
m_pFont->InvalidateDeviceObjects();
m_CompCosinesEff->OnLostDevice();
m_WaterEff->OnLostDevice();
SAFE_RELEASE(m_BumpTex);
SAFE_RELEASE(m_BumpSurf);
SAFE_RELEASE(m_BumpRender);
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DeleteDeviceObjects()
// Desc: Paired with InitDeviceObjects()
// Called when the app is exiting, or the device is being changed,
// this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::DeleteDeviceObjects()
{
// TODO: Cleanup any objects created in InitDeviceObjects()
m_pFont->DeleteDeviceObjects();
SAFE_RELEASE(m_CompCosinesEff);
SAFE_RELEASE(m_WaterEff);
SAFE_RELEASE(m_CosineLUT);
SAFE_RELEASE(m_BiasNoiseMap);
SAFE_RELEASE(m_EnvMap);
SAFE_RELEASE(m_LandTex);
SAFE_RELEASE(m_WaterMesh);
SAFE_RELEASE(m_LandMesh);
SAFE_RELEASE(m_PillarsMesh);
SAFE_RELEASE(m_BumpVBuffer);
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: FinalCleanup()
// Desc: Paired with OneTimeSceneInit()
// Called before the app exits, this function gives the app the chance
// to cleanup after itself.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FinalCleanup()
{
// TODO: Perform any final cleanup needed
// Cleanup D3D font
SAFE_DELETE( m_pFont );