forked from walbourn/directx-sdk-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollision.cpp
1201 lines (977 loc) · 49 KB
/
Collision.cpp
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: Collision.cpp
//
// Sample demonstrating DirectXMath's collision types using Direct3D 11, DXUT,
// and DirectXTK
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "DXUT.h"
#include "DXUTgui.h"
#include "DXUTmisc.h"
#include "DXUTCamera.h"
#include "DXUTSettingsDlg.h"
#include "SDKmisc.h"
#include "resource.h"
#include <DirectXColors.h>
#include <DirectXCollision.h>
#include "CommonStates.h"
#include "Effects.h"
#include "PrimitiveBatch.h"
#include "VertexTypes.h"
#pragma warning( disable : 4100 )
using namespace DirectX;
//--------------------------------------------------------------------------------------
// Types
//--------------------------------------------------------------------------------------
// Collision objects
struct CollisionSphere
{
BoundingSphere sphere;
ContainmentType collision;
};
struct CollisionBox
{
BoundingOrientedBox obox;
ContainmentType collision;
};
struct CollisionAABox
{
BoundingBox aabox;
ContainmentType collision;
};
struct CollisionFrustum
{
BoundingFrustum frustum;
ContainmentType collision;
};
struct CollisionTriangle
{
XMVECTOR pointa;
XMVECTOR pointb;
XMVECTOR pointc;
ContainmentType collision;
};
struct CollisionRay
{
XMVECTOR origin;
XMVECTOR direction;
};
//--------------------------------------------------------------------------------------
// Constants
//--------------------------------------------------------------------------------------
const int GROUP_COUNT = 4;
const int CAMERA_COUNT = 4;
const float CAMERA_SPACING = 50.f;
//--------------------------------------------------------------------------------------
// Global variables
//--------------------------------------------------------------------------------------
CModelViewerCamera g_Camera; // A model viewing camera
CDXUTDialogResourceManager g_DialogResourceManager; // manager for shared resources of dialogs
CD3DSettingsDlg g_SettingsDlg; // Device settings dialog
CDXUTTextHelper* g_pTxtHelper = nullptr;
CDXUTDialog g_HUD; // dialog for standard controls
CDXUTDialog g_SampleUI; // dialog for sample specific controls
ID3D11InputLayout* g_pBatchInputLayout = nullptr;
std::unique_ptr<CommonStates> g_States;
std::unique_ptr<BasicEffect> g_BatchEffect;
std::unique_ptr<PrimitiveBatch<VertexPositionColor>> g_Batch;
// Primary collision objects
BoundingFrustum g_PrimaryFrustum;
BoundingOrientedBox g_PrimaryOrientedBox;
BoundingBox g_PrimaryAABox;
CollisionRay g_PrimaryRay;
// Secondary collision objects
CollisionSphere g_SecondarySpheres[GROUP_COUNT];
CollisionBox g_SecondaryOrientedBoxes[GROUP_COUNT];
CollisionAABox g_SecondaryAABoxes[GROUP_COUNT];
CollisionTriangle g_SecondaryTriangles[GROUP_COUNT];
// Ray testing results display object
CollisionAABox g_RayHitResultBox;
// Camera preset locations
XMVECTOR g_CameraOrigins[CAMERA_COUNT];
//--------------------------------------------------------------------------------------
// UI control IDs
//--------------------------------------------------------------------------------------
#define IDC_STATIC -1
#define IDC_TOGGLEFULLSCREEN 1
#define IDC_TOGGLEREF 2
#define IDC_CHANGEDEVICE 3
#define IDC_TOGGLEWARP 4
#define IDC_GROUP 5
//--------------------------------------------------------------------------------------
// Forward declarations
//--------------------------------------------------------------------------------------
LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool* pbNoFurtherProcessing,
void* pUserContext );
void CALLBACK OnKeyboard( UINT nChar, bool bKeyDown, bool bAltDown, void* pUserContext );
void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext );
void CALLBACK OnFrameMove( double fTime, float fElapsedTime, void* pUserContext );
bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, void* pUserContext );
bool CALLBACK IsD3D11DeviceAcceptable( const CD3D11EnumAdapterInfo *AdapterInfo, UINT Output,
const CD3D11EnumDeviceInfo *DeviceInfo,
DXGI_FORMAT BackBufferFormat, bool bWindowed, void* pUserContext );
HRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc,
void* pUserContext );
HRESULT CALLBACK OnD3D11ResizedSwapChain( ID3D11Device* pd3dDevice, IDXGISwapChain* pSwapChain,
const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext );
void CALLBACK OnD3D11ReleasingSwapChain( void* pUserContext );
void CALLBACK OnD3D11DestroyDevice( void* pUserContext );
void CALLBACK OnD3D11FrameRender( ID3D11Device* pd3dDevice, ID3D11DeviceContext* pd3dImmediateContext, double fTime,
float fElapsedTime, void* pUserContext );
void InitApp();
void RenderText();
void InitializeObjects();
void Animate( double fTime );
void Collide();
void RenderObjects();
void SetViewForGroup( int group );
void DrawGrid( FXMVECTOR xAxis, FXMVECTOR yAxis, FXMVECTOR origin, size_t xdivs, size_t ydivs, GXMVECTOR color );
void DrawFrustum( const BoundingFrustum& frustum, FXMVECTOR color );
void DrawAabb( const BoundingBox& box, FXMVECTOR color );
void DrawObb( const BoundingOrientedBox& obb, FXMVECTOR color );
void DrawSphere( const BoundingSphere& sphere, FXMVECTOR color );
void DrawRay( FXMVECTOR Origin, FXMVECTOR Direction, bool bNormalize, FXMVECTOR color );
void DrawTriangle( FXMVECTOR PointA, FXMVECTOR PointB, FXMVECTOR PointC, CXMVECTOR color );
//--------------------------------------------------------------------------------------
// Entry point to the program. Initializes everything and goes into a message processing
// loop. Idle time is used to render the scene.
//--------------------------------------------------------------------------------------
int WINAPI wWinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow )
{
// Enable run-time memory check for debug builds.
#if defined(DEBUG) | defined(_DEBUG)
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
// DirectXMath uses SSE/SSE2 instructions on Windows. We should verify the CPU supports these instructions
// as early in the program as possible
if ( !XMVerifyCPUSupport() )
{
MessageBox( NULL, TEXT("This application requires the processor support SSE2 instructions."),
TEXT("Collision"), MB_OK | MB_ICONEXCLAMATION);
return -1;
}
// DXUT will create and use the best device
// that is available on the system depending on which D3D callbacks are set below
// Set DXUT callbacks
DXUTSetCallbackMsgProc( MsgProc );
DXUTSetCallbackKeyboard( OnKeyboard );
DXUTSetCallbackFrameMove( OnFrameMove );
DXUTSetCallbackDeviceChanging( ModifyDeviceSettings );
DXUTSetCallbackD3D11DeviceAcceptable( IsD3D11DeviceAcceptable );
DXUTSetCallbackD3D11DeviceCreated( OnD3D11CreateDevice );
DXUTSetCallbackD3D11SwapChainResized( OnD3D11ResizedSwapChain );
DXUTSetCallbackD3D11SwapChainReleasing( OnD3D11ReleasingSwapChain );
DXUTSetCallbackD3D11DeviceDestroyed( OnD3D11DestroyDevice );
DXUTSetCallbackD3D11FrameRender( OnD3D11FrameRender );
InitApp();
DXUTInit( true, true, nullptr ); // Parse the command line, show msgboxes on error, no extra command line params
DXUTSetCursorSettings( true, true );
DXUTCreateWindow( L"Collision for DirectXMath" );
// Only require 10-level hardware, change to D3D_FEATURE_LEVEL_11_0 to require 11-class hardware
// Switch to D3D_FEATURE_LEVEL_9_x for 10level9 hardware
DXUTCreateDevice( D3D_FEATURE_LEVEL_10_0, true, 800, 600 );
DXUTMainLoop(); // Enter into the DXUT render loop
return DXUTGetExitCode();
}
//--------------------------------------------------------------------------------------
// Initialize the app
//--------------------------------------------------------------------------------------
void InitApp()
{
g_SettingsDlg.Init( &g_DialogResourceManager );
g_HUD.Init( &g_DialogResourceManager );
g_SampleUI.Init( &g_DialogResourceManager );
g_HUD.SetCallback( OnGUIEvent );
int iY = 30;
int iYo = 26;
g_HUD.AddButton( IDC_TOGGLEFULLSCREEN, L"Toggle full screen", 0, iY, 170, 22 );
g_HUD.AddButton( IDC_CHANGEDEVICE, L"Change device (F2)", 0, iY += iYo, 170, 22, VK_F2 );
g_HUD.AddButton( IDC_TOGGLEREF, L"Toggle REF (F3)", 0, iY += iYo, 170, 22, VK_F3 );
g_HUD.AddButton( IDC_TOGGLEWARP, L"Toggle WARP (F4)", 0, iY += iYo, 170, 22, VK_F4 );
g_SampleUI.SetCallback( OnGUIEvent );
CDXUTComboBox* pComboBox = nullptr;
g_SampleUI.AddStatic( IDC_STATIC, L"(G)roup", 10, 0, 170, 25 );
g_SampleUI.AddComboBox( IDC_GROUP, 0, 25, 170, 24, 'G', false, &pComboBox );
if( pComboBox )
pComboBox->SetDropHeight( 50 );
pComboBox->AddItem( L"Frustum", IntToPtr( 0 ) );
pComboBox->AddItem( L"Axis-aligned Box", IntToPtr( 1 ) );
pComboBox->AddItem( L"Oriented Box", IntToPtr( 2 ) );
pComboBox->AddItem( L"Ray", IntToPtr( 3 ) );
InitializeObjects();
}
//--------------------------------------------------------------------------------------
// Render the help and statistics text.
//--------------------------------------------------------------------------------------
void RenderText()
{
g_pTxtHelper->Begin();
g_pTxtHelper->SetInsertionPos( 5, 5 );
g_pTxtHelper->SetForegroundColor( Colors::Yellow );
g_pTxtHelper->DrawTextLine( DXUTGetFrameStats( DXUTIsVsyncEnabled() ) );
g_pTxtHelper->DrawTextLine( DXUTGetDeviceStats() );
g_pTxtHelper->End();
}
//--------------------------------------------------------------------------------------
// Initialize the starting positions of the collision objects
//--------------------------------------------------------------------------------------
void InitializeObjects()
{
const XMVECTOR XMZero = XMVectorZero();
// Set up the primary frustum object from a D3D projection matrix
// NOTE: This can also be done on your camera's projection matrix. The projection
// matrix built here is somewhat contrived so it renders well.
XMMATRIX xmProj = XMMatrixPerspectiveFovLH( XM_PIDIV4, 1.77778f, 0.5f, 10.0f );
BoundingFrustum::CreateFromMatrix( g_PrimaryFrustum, xmProj );
g_PrimaryFrustum.Origin.z = -7.0f;
g_CameraOrigins[0] = XMVectorSet( 0, 0, 0, 0 );
// Set up the primary axis aligned box
g_PrimaryAABox.Center = XMFLOAT3( CAMERA_SPACING, 0, 0 );
g_PrimaryAABox.Extents = XMFLOAT3( 5, 5, 5 );
g_CameraOrigins[1] = XMVectorSet( CAMERA_SPACING, 0, 0, 0 );
// Set up the primary oriented box with some rotation
g_PrimaryOrientedBox.Center = XMFLOAT3( -CAMERA_SPACING, 0, 0 );
g_PrimaryOrientedBox.Extents = XMFLOAT3( 5, 5, 5 );
XMStoreFloat4( &g_PrimaryOrientedBox.Orientation, XMQuaternionRotationRollPitchYaw( XM_PIDIV4, XM_PIDIV4, 0 ) );
g_CameraOrigins[2] = XMVectorSet( -CAMERA_SPACING, 0, 0, 0 );
// Set up the primary ray
g_PrimaryRay.origin = XMVectorSet( 0, 0, CAMERA_SPACING, 0 );
g_PrimaryRay.direction = g_XMIdentityR2;
g_CameraOrigins[3] = XMVectorSet( 0, 0, CAMERA_SPACING, 0 );
// Initialize all of the secondary objects with default values
for( UINT i = 0; i < GROUP_COUNT; i++ )
{
g_SecondarySpheres[i].sphere.Radius = 1.0f;
g_SecondarySpheres[i].sphere.Center = XMFLOAT3( 0, 0, 0 );
g_SecondarySpheres[i].collision = DISJOINT;
g_SecondaryOrientedBoxes[i].obox.Center = XMFLOAT3( 0, 0, 0 );
g_SecondaryOrientedBoxes[i].obox.Extents = XMFLOAT3( 0.5f, 0.5f, 0.5f );
g_SecondaryOrientedBoxes[i].obox.Orientation = XMFLOAT4( 0, 0, 0, 1 );
g_SecondaryOrientedBoxes[i].collision = DISJOINT;
g_SecondaryAABoxes[i].aabox.Center = XMFLOAT3( 0, 0, 0 );
g_SecondaryAABoxes[i].aabox.Extents = XMFLOAT3( 0.5f, 0.5f, 0.5f );
g_SecondaryAABoxes[i].collision = DISJOINT;
g_SecondaryTriangles[i].pointa = XMZero;
g_SecondaryTriangles[i].pointb = XMZero;
g_SecondaryTriangles[i].pointc = XMZero;
g_SecondaryTriangles[i].collision = DISJOINT;
}
// Set up ray hit result box
g_RayHitResultBox.aabox.Center = XMFLOAT3( 0, 0, 0 );
g_RayHitResultBox.aabox.Extents = XMFLOAT3( 0.05f, 0.05f, 0.05f );
}
//--------------------------------------------------------------------------------------
// Move objects around over time
//--------------------------------------------------------------------------------------
void Animate( double fTime )
{
float t = ( FLOAT )(fTime * 0.2);
const float camera0OriginX = XMVectorGetX( g_CameraOrigins[0] );
const float camera1OriginX = XMVectorGetX( g_CameraOrigins[1] );
const float camera2OriginX = XMVectorGetX( g_CameraOrigins[2] );
const float camera3OriginX = XMVectorGetX( g_CameraOrigins[3] );
const float camera3OriginZ = XMVectorGetZ( g_CameraOrigins[3] );
// animate sphere 0 around the frustum
g_SecondarySpheres[0].sphere.Center.x = 10 * sinf( 3 * t );
g_SecondarySpheres[0].sphere.Center.y = 7 * cosf( 5 * t );
// animate oriented box 0 around the frustum
g_SecondaryOrientedBoxes[0].obox.Center.x = 8 * sinf( 3.5f * t );
g_SecondaryOrientedBoxes[0].obox.Center.y = 5 * cosf( 5.1f * t );
XMStoreFloat4( &( g_SecondaryOrientedBoxes[0].obox.Orientation ), XMQuaternionRotationRollPitchYaw( t * 1.4f,
t * 0.2f,
t ) );
// animate aligned box 0 around the frustum
g_SecondaryAABoxes[0].aabox.Center.x = 10 * sinf( 2.1f * t );
g_SecondaryAABoxes[0].aabox.Center.y = 7 * cosf( 3.8f * t );
// animate sphere 1 around the aligned box
g_SecondarySpheres[1].sphere.Center.x = 8 * sinf( 2.9f * t ) + camera1OriginX;
g_SecondarySpheres[1].sphere.Center.y = 8 * cosf( 4.6f * t );
g_SecondarySpheres[1].sphere.Center.z = 8 * cosf( 1.6f * t );
// animate oriented box 1 around the aligned box
g_SecondaryOrientedBoxes[1].obox.Center.x = 8 * sinf( 3.2f * t ) + camera1OriginX;
g_SecondaryOrientedBoxes[1].obox.Center.y = 8 * cosf( 2.1f * t );
g_SecondaryOrientedBoxes[1].obox.Center.z = 8 * sinf( 1.6f * t );
XMStoreFloat4( &( g_SecondaryOrientedBoxes[1].obox.Orientation ), XMQuaternionRotationRollPitchYaw( t * 0.7f,
t * 1.3f,
t ) );
// animate aligned box 1 around the aligned box
g_SecondaryAABoxes[1].aabox.Center.x = 8 * sinf( 1.1f * t ) + camera1OriginX;
g_SecondaryAABoxes[1].aabox.Center.y = 8 * cosf( 5.8f * t );
g_SecondaryAABoxes[1].aabox.Center.z = 8 * cosf( 3.0f * t );
// animate sphere 2 around the oriented box
g_SecondarySpheres[2].sphere.Center.x = 8 * sinf( 2.2f * t ) + camera2OriginX;
g_SecondarySpheres[2].sphere.Center.y = 8 * cosf( 4.3f * t );
g_SecondarySpheres[2].sphere.Center.z = 8 * cosf( 1.8f * t );
// animate oriented box 2 around the oriented box
g_SecondaryOrientedBoxes[2].obox.Center.x = 8 * sinf( 3.7f * t ) + camera2OriginX;
g_SecondaryOrientedBoxes[2].obox.Center.y = 8 * cosf( 2.5f * t );
g_SecondaryOrientedBoxes[2].obox.Center.z = 8 * sinf( 1.1f * t );
XMStoreFloat4( &( g_SecondaryOrientedBoxes[2].obox.Orientation ), XMQuaternionRotationRollPitchYaw( t * 0.9f,
t * 1.8f,
t ) );
// animate aligned box 2 around the oriented box
g_SecondaryAABoxes[2].aabox.Center.x = 8 * sinf( 1.3f * t ) + camera2OriginX;
g_SecondaryAABoxes[2].aabox.Center.y = 8 * cosf( 5.2f * t );
g_SecondaryAABoxes[2].aabox.Center.z = 8 * cosf( 3.5f * t );
// triangle points in local space - equilateral triangle with radius of 2
const XMVECTOR TrianglePointA = { 0, 2, 0, 0 };
const XMVECTOR TrianglePointB = { 1.732f, -1, 0, 0 };
const XMVECTOR TrianglePointC = { -1.732f, -1, 0, 0 };
// animate triangle 0 around the frustum
XMMATRIX TriangleCoords = XMMatrixRotationRollPitchYaw( t * 1.4f, t * 2.5f, t );
XMMATRIX Translation = XMMatrixTranslation( 5 * sinf( 5.3f * t ) + camera0OriginX,
5 * cosf( 2.3f * t ),
5 * sinf( 3.4f * t ) );
TriangleCoords = XMMatrixMultiply( TriangleCoords, Translation );
g_SecondaryTriangles[0].pointa = XMVector3Transform( TrianglePointA, TriangleCoords );
g_SecondaryTriangles[0].pointb = XMVector3Transform( TrianglePointB, TriangleCoords );
g_SecondaryTriangles[0].pointc = XMVector3Transform( TrianglePointC, TriangleCoords );
// animate triangle 1 around the aligned box
TriangleCoords = XMMatrixRotationRollPitchYaw( t * 1.4f, t * 2.5f, t );
Translation = XMMatrixTranslation( 8 * sinf( 5.3f * t ) + camera1OriginX,
8 * cosf( 2.3f * t ),
8 * sinf( 3.4f * t ) );
TriangleCoords = XMMatrixMultiply( TriangleCoords, Translation );
g_SecondaryTriangles[1].pointa = XMVector3Transform( TrianglePointA, TriangleCoords );
g_SecondaryTriangles[1].pointb = XMVector3Transform( TrianglePointB, TriangleCoords );
g_SecondaryTriangles[1].pointc = XMVector3Transform( TrianglePointC, TriangleCoords );
// animate triangle 2 around the oriented box
TriangleCoords = XMMatrixRotationRollPitchYaw( t * 1.4f, t * 2.5f, t );
Translation = XMMatrixTranslation( 8 * sinf( 5.3f * t ) + camera2OriginX,
8 * cosf( 2.3f * t ),
8 * sinf( 3.4f * t ) );
TriangleCoords = XMMatrixMultiply( TriangleCoords, Translation );
g_SecondaryTriangles[2].pointa = XMVector3Transform( TrianglePointA, TriangleCoords );
g_SecondaryTriangles[2].pointb = XMVector3Transform( TrianglePointB, TriangleCoords );
g_SecondaryTriangles[2].pointc = XMVector3Transform( TrianglePointC, TriangleCoords );
// animate primary ray (this is the only animated primary object)
g_PrimaryRay.direction = XMVectorSet( sinf( t * 3 ), 0, cosf( t * 3 ), 0 );
// animate sphere 3 around the ray
g_SecondarySpheres[3].sphere.Center = XMFLOAT3( camera3OriginX - 3,
0.5f * sinf( t * 5 ),
camera3OriginZ );
// animate aligned box 3 around the ray
g_SecondaryAABoxes[3].aabox.Center = XMFLOAT3( camera3OriginX + 3,
0.5f * sinf( t * 4 ),
camera3OriginZ );
// animate oriented box 3 around the ray
g_SecondaryOrientedBoxes[3].obox.Center = XMFLOAT3( camera3OriginX,
0.5f * sinf( t * 4.5f ),
camera3OriginZ + 3 );
XMStoreFloat4( &( g_SecondaryOrientedBoxes[3].obox.Orientation ), XMQuaternionRotationRollPitchYaw( t * 0.9f,
t * 1.8f,
t ) );
// animate triangle 3 around the ray
TriangleCoords = XMMatrixRotationRollPitchYaw( t * 1.4f, t * 2.5f, t );
Translation = XMMatrixTranslation( camera3OriginX,
0.5f * cosf( 4.3f * t ),
camera3OriginZ - 3 );
TriangleCoords = XMMatrixMultiply( TriangleCoords, Translation );
g_SecondaryTriangles[3].pointa = XMVector3Transform( TrianglePointA, TriangleCoords );
g_SecondaryTriangles[3].pointb = XMVector3Transform( TrianglePointB, TriangleCoords );
g_SecondaryTriangles[3].pointc = XMVector3Transform( TrianglePointC, TriangleCoords );
}
//--------------------------------------------------------------------------------------
// Test collisions between pairs of collision objects using XNACollision functions
//--------------------------------------------------------------------------------------
void Collide()
{
// test collisions between objects and frustum
g_SecondarySpheres[0].collision = g_PrimaryFrustum.Contains( g_SecondarySpheres[0].sphere );
g_SecondaryOrientedBoxes[0].collision = g_PrimaryFrustum.Contains( g_SecondaryOrientedBoxes[0].obox );
g_SecondaryAABoxes[0].collision = g_PrimaryFrustum.Contains( g_SecondaryAABoxes[0].aabox );
g_SecondaryTriangles[0].collision = g_PrimaryFrustum.Contains( g_SecondaryTriangles[0].pointa,
g_SecondaryTriangles[0].pointb,
g_SecondaryTriangles[0].pointc );
// test collisions between objects and aligned box
g_SecondarySpheres[1].collision = g_PrimaryAABox.Contains( g_SecondarySpheres[1].sphere );
g_SecondaryOrientedBoxes[1].collision = g_PrimaryAABox.Contains( g_SecondaryOrientedBoxes[1].obox );
g_SecondaryAABoxes[1].collision = g_PrimaryAABox.Contains( g_SecondaryAABoxes[1].aabox );
g_SecondaryTriangles[1].collision = g_PrimaryAABox.Contains( g_SecondaryTriangles[1].pointa,
g_SecondaryTriangles[1].pointb,
g_SecondaryTriangles[1].pointc );
// test collisions between objects and oriented box
g_SecondarySpheres[2].collision = g_PrimaryOrientedBox.Contains( g_SecondarySpheres[2].sphere );
g_SecondaryOrientedBoxes[2].collision = g_PrimaryOrientedBox.Contains( g_SecondaryOrientedBoxes[2].obox );
g_SecondaryAABoxes[2].collision = g_PrimaryOrientedBox.Contains( g_SecondaryAABoxes[2].aabox );
g_SecondaryTriangles[2].collision = g_PrimaryOrientedBox.Contains( g_SecondaryTriangles[2].pointa,
g_SecondaryTriangles[2].pointb,
g_SecondaryTriangles[2].pointc );
// test collisions between objects and ray
float fDistance = -1.0f;
float fDist;
if ( g_SecondarySpheres[3].sphere.Intersects( g_PrimaryRay.origin, g_PrimaryRay.direction, fDist ) )
{
fDistance = fDist;
g_SecondarySpheres[3].collision = INTERSECTS;
}
else
g_SecondarySpheres[3].collision = DISJOINT;
if ( g_SecondaryOrientedBoxes[3].obox.Intersects( g_PrimaryRay.origin, g_PrimaryRay.direction, fDist ) )
{
fDistance = fDist;
g_SecondaryOrientedBoxes[3].collision = INTERSECTS;
}
else
g_SecondaryOrientedBoxes[3].collision = DISJOINT;
if ( g_SecondaryAABoxes[3].aabox.Intersects( g_PrimaryRay.origin, g_PrimaryRay.direction, fDist ) )
{
fDistance = fDist;
g_SecondaryAABoxes[3].collision = INTERSECTS;
}
else
g_SecondaryAABoxes[3].collision = DISJOINT;
if ( TriangleTests::Intersects( g_PrimaryRay.origin, g_PrimaryRay.direction,
g_SecondaryTriangles[3].pointa,
g_SecondaryTriangles[3].pointb,
g_SecondaryTriangles[3].pointc,
fDist ) )
{
fDistance = fDist;
g_SecondaryTriangles[3].collision = INTERSECTS;
}
else
g_SecondaryTriangles[3].collision = DISJOINT;
// If one of the ray intersection tests was successful, fDistance will be positive.
// If so, compute the intersection location and store it in g_RayHitResultBox.
if( fDistance > 0 )
{
// The primary ray's direction is assumed to be normalized.
XMVECTOR HitLocation = XMVectorMultiplyAdd( g_PrimaryRay.direction, XMVectorReplicate( fDistance ),
g_PrimaryRay.origin );
XMStoreFloat3( &g_RayHitResultBox.aabox.Center, HitLocation );
g_RayHitResultBox.collision = INTERSECTS;
}
else
{
g_RayHitResultBox.collision = DISJOINT;
}
}
//--------------------------------------------------------------------------------------
// Returns the color based on the collision result and the gruop number.
// Frustum tests (group 0) return 0, 1, or 2 for outside, partially inside, and fully inside;
// all other tests return 0 or 1 for no collision or collision.
//--------------------------------------------------------------------------------------
inline XMVECTOR GetCollisionColor( ContainmentType collision, int groupnumber )
{
// special case: a value of 1 for groups 1 and higher needs to register as a full collision
if( groupnumber >= 3 && collision > 0 )
collision = CONTAINS;
switch( collision )
{
case DISJOINT: return Colors::Green;
case INTERSECTS: return Colors::Yellow;
case CONTAINS:
default: return Colors::Red;
}
}
//--------------------------------------------------------------------------------------
// Renders collision objects
//--------------------------------------------------------------------------------------
void RenderObjects()
{
// Draw ground planes
for( int i = 0; i < CAMERA_COUNT; ++i )
{
static const XMVECTORF32 s_vXAxis = { 20.f, 0.f, 0.f, 0.f };
static const XMVECTORF32 s_vYAxis = { 0.f, 0.f, 20.f, 0.f };
static const XMVECTORF32 s_Offset = { 0.f, 10.f, 0.f, 0.f };
XMVECTOR vOrigin = g_CameraOrigins[i] - s_Offset;
const int iXDivisions = 20;
const int iYDivisions = 20;
DrawGrid( s_vXAxis, s_vYAxis, vOrigin, iXDivisions, iYDivisions, Colors::Black );
}
// Draw primary collision objects in white
DrawFrustum( g_PrimaryFrustum, Colors::White );
DrawAabb( g_PrimaryAABox, Colors::White );
DrawObb( g_PrimaryOrientedBox, Colors::White );
{
XMVECTOR Direction = XMVectorScale( g_PrimaryRay.direction, 10.0f );
DrawRay( g_PrimaryRay.origin, Direction, false, Colors::LightGray );
DrawRay( g_PrimaryRay.origin, Direction, false, Colors::White);
}
// Draw secondary collision objects in colors based on collision results
for( int i = 0; i < GROUP_COUNT; ++i )
{
const CollisionSphere& sphere = g_SecondarySpheres[i];
XMVECTOR c = GetCollisionColor( sphere.collision, i );
DrawSphere( sphere.sphere, c );
const CollisionBox& obox = g_SecondaryOrientedBoxes[i];
c = GetCollisionColor( obox.collision, i );
DrawObb( obox.obox, c );
const CollisionAABox& aabox = g_SecondaryAABoxes[i];
c = GetCollisionColor( aabox.collision, i );
DrawAabb( aabox.aabox, c );
const CollisionTriangle& tri = g_SecondaryTriangles[i];
c = GetCollisionColor( tri.collision, i );
DrawTriangle( tri.pointa, tri.pointb, tri.pointc, c );
}
// Draw results of ray-object intersection, if there was a hit this frame
if( g_RayHitResultBox.collision != DISJOINT )
DrawAabb( g_RayHitResultBox.aabox, Colors::Yellow );
}
//--------------------------------------------------------------------------------------
// Sets the camera to view a particular group of objects
//--------------------------------------------------------------------------------------
void SetViewForGroup( int group )
{
assert( group < GROUP_COUNT );
g_Camera.Reset();
static const XMVECTORF32 s_Offset0 = { 0.f, 20.f, 20.f, 0.f };
static const XMVECTORF32 s_Offset = { 0.f, 20.f, -20.f, 0.f };
XMVECTOR vecEye = g_CameraOrigins[group] + ( ( group == 0 ) ? s_Offset0 : s_Offset );
g_Camera.SetViewParams( vecEye, g_CameraOrigins[group] );
XMFLOAT3 vecAt;
XMStoreFloat3( &vecAt, g_CameraOrigins[group] );
g_Camera.SetModelCenter( vecAt );
}
//--------------------------------------------------------------------------------------
void DrawGrid( FXMVECTOR xAxis, FXMVECTOR yAxis, FXMVECTOR origin, size_t xdivs, size_t ydivs, GXMVECTOR color )
{
auto context = DXUTGetD3D11DeviceContext();
g_BatchEffect->Apply( context );
context->IASetInputLayout( g_pBatchInputLayout );
g_Batch->Begin();
xdivs = std::max<size_t>( 1, xdivs );
ydivs = std::max<size_t>( 1, ydivs );
for( size_t i = 0; i <= xdivs; ++i )
{
float fPercent = float(i) / float(xdivs);
fPercent = ( fPercent * 2.0f ) - 1.0f;
XMVECTOR vScale = XMVectorScale( xAxis, fPercent );
vScale = XMVectorAdd( vScale, origin );
VertexPositionColor v1( XMVectorSubtract( vScale, yAxis ), color );
VertexPositionColor v2( XMVectorAdd( vScale, yAxis ), color );
g_Batch->DrawLine( v1, v2 );
}
for( size_t i = 0; i <= ydivs; i++ )
{
FLOAT fPercent = float(i) / float(ydivs);
fPercent = ( fPercent * 2.0f ) - 1.0f;
XMVECTOR vScale = XMVectorScale( yAxis, fPercent );
vScale = XMVectorAdd( vScale, origin );
VertexPositionColor v1( XMVectorSubtract( vScale, xAxis ), color );
VertexPositionColor v2( XMVectorAdd( vScale, xAxis ), color );
g_Batch->DrawLine( v1, v2 );
}
g_Batch->End();
}
//--------------------------------------------------------------------------------------
void DrawFrustum( const BoundingFrustum& frustum, FXMVECTOR color )
{
XMFLOAT3 corners[ BoundingFrustum::CORNER_COUNT ];
frustum.GetCorners( corners );
VertexPositionColor verts[24];
verts[0].position = corners[0];
verts[1].position = corners[1];
verts[2].position = corners[1];
verts[3].position = corners[2];
verts[4].position = corners[2];
verts[5].position = corners[3];
verts[6].position = corners[3];
verts[7].position = corners[0];
verts[8].position = corners[0];
verts[9].position = corners[4];
verts[10].position = corners[1];
verts[11].position = corners[5];
verts[12].position = corners[2];
verts[13].position = corners[6];
verts[14].position = corners[3];
verts[15].position = corners[7];
verts[16].position = corners[4];
verts[17].position = corners[5];
verts[18].position = corners[5];
verts[19].position = corners[6];
verts[20].position = corners[6];
verts[21].position = corners[7];
verts[22].position = corners[7];
verts[23].position = corners[4];
for( size_t j = 0; j < _countof(verts); ++j )
{
XMStoreFloat4( &verts[j].color, color );
}
auto context = DXUTGetD3D11DeviceContext();
g_BatchEffect->Apply( context );
context->IASetInputLayout( g_pBatchInputLayout );
g_Batch->Begin();
g_Batch->Draw( D3D11_PRIMITIVE_TOPOLOGY_LINELIST, verts, _countof( verts ) );
g_Batch->End();
}
//--------------------------------------------------------------------------------------
void DrawCube( CXMMATRIX mWorld, FXMVECTOR color )
{
static const XMVECTOR s_verts[8] =
{
{ -1, -1, -1, 0 },
{ 1, -1, -1, 0 },
{ 1, -1, 1, 0 },
{ -1, -1, 1, 0 },
{ -1, 1, -1, 0 },
{ 1, 1, -1, 0 },
{ 1, 1, 1, 0 },
{ -1, 1, 1, 0 }
};
static const WORD s_indices[] =
{
0, 1,
1, 2,
2, 3,
3, 0,
4, 5,
5, 6,
6, 7,
7, 4,
0, 4,
1, 5,
2, 6,
3, 7
};
VertexPositionColor verts[8];
for( int i=0; i < 8; ++i )
{
XMVECTOR v = XMVector3Transform( s_verts[i], mWorld );
XMStoreFloat3( &verts[i].position, v );
XMStoreFloat4( &verts[i].color, color );
}
auto context = DXUTGetD3D11DeviceContext();
g_BatchEffect->Apply( context );
context->IASetInputLayout( g_pBatchInputLayout );
g_Batch->Begin();
g_Batch->DrawIndexed( D3D11_PRIMITIVE_TOPOLOGY_LINELIST, s_indices, _countof( s_indices ), verts, 8 );
g_Batch->End();
}
//--------------------------------------------------------------------------------------
void DrawAabb( const BoundingBox& box, FXMVECTOR color )
{
XMMATRIX matWorld = XMMatrixScaling( box.Extents.x, box.Extents.y, box.Extents.z );
XMVECTOR position = XMLoadFloat3( &box.Center );
matWorld.r[3] = XMVectorSelect( matWorld.r[3], position, g_XMSelect1110 );
DrawCube( matWorld, color );
}
//--------------------------------------------------------------------------------------
void DrawObb( const BoundingOrientedBox& obb, FXMVECTOR color )
{
XMMATRIX matWorld = XMMatrixRotationQuaternion( XMLoadFloat4( &obb.Orientation ) );
XMMATRIX matScale = XMMatrixScaling( obb.Extents.x, obb.Extents.y, obb.Extents.z );
matWorld = XMMatrixMultiply( matScale, matWorld );
XMVECTOR position = XMLoadFloat3( &obb.Center );
matWorld.r[3] = XMVectorSelect( matWorld.r[3], position, g_XMSelect1110 );
DrawCube( matWorld, color );
}
//--------------------------------------------------------------------------------------
void DrawRing( FXMVECTOR Origin, FXMVECTOR MajorAxis, FXMVECTOR MinorAxis, CXMVECTOR color )
{
static const DWORD dwRingSegments = 32;
VertexPositionColor verts[ dwRingSegments + 1 ];
FLOAT fAngleDelta = XM_2PI / ( float )dwRingSegments;
// Instead of calling cos/sin for each segment we calculate
// the sign of the angle delta and then incrementally calculate sin
// and cosine from then on.
XMVECTOR cosDelta = XMVectorReplicate( cosf( fAngleDelta ) );
XMVECTOR sinDelta = XMVectorReplicate( sinf( fAngleDelta ) );
XMVECTOR incrementalSin = XMVectorZero();
static const XMVECTOR initialCos =
{
1.0f, 1.0f, 1.0f, 1.0f
};
XMVECTOR incrementalCos = initialCos;
for( DWORD i = 0; i < dwRingSegments; i++ )
{
XMVECTOR Pos;
Pos = XMVectorMultiplyAdd( MajorAxis, incrementalCos, Origin );
Pos = XMVectorMultiplyAdd( MinorAxis, incrementalSin, Pos );
XMStoreFloat3( &verts[i].position, Pos );
XMStoreFloat4( &verts[i].color, color );
// Standard formula to rotate a vector.
XMVECTOR newCos = incrementalCos * cosDelta - incrementalSin * sinDelta;
XMVECTOR newSin = incrementalCos * sinDelta + incrementalSin * cosDelta;
incrementalCos = newCos;
incrementalSin = newSin;
}
verts[ dwRingSegments ] = verts[0];
// Draw ring
auto context = DXUTGetD3D11DeviceContext();
g_BatchEffect->Apply( context );
context->IASetInputLayout( g_pBatchInputLayout );
g_Batch->Begin();
g_Batch->Draw( D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP, verts, dwRingSegments+1 );
g_Batch->End();
}
//--------------------------------------------------------------------------------------
void DrawSphere( const BoundingSphere& sphere, FXMVECTOR color )
{
XMVECTOR origin = XMLoadFloat3( &sphere.Center );
const float fRadius = sphere.Radius;
XMVECTOR xaxis = g_XMIdentityR0 * fRadius;
XMVECTOR yaxis = g_XMIdentityR1 * fRadius;
XMVECTOR zaxis = g_XMIdentityR2 * fRadius;
DrawRing( origin, xaxis, zaxis, color );
DrawRing( origin, xaxis, yaxis, color );
DrawRing( origin, yaxis, zaxis, color );
}
//--------------------------------------------------------------------------------------
void DrawRay( FXMVECTOR Origin, FXMVECTOR Direction, bool bNormalize, FXMVECTOR color )
{
VertexPositionColor verts[3];
XMStoreFloat3( &verts[0].position, Origin );
XMVECTOR NormDirection = XMVector3Normalize( Direction );
XMVECTOR RayDirection = ( bNormalize ) ? NormDirection : Direction;
XMVECTOR PerpVector = XMVector3Cross( NormDirection, g_XMIdentityR1 );
if( XMVector3Equal( XMVector3LengthSq( PerpVector ), g_XMZero ) )
{
PerpVector = XMVector3Cross( NormDirection, g_XMIdentityR2 );
}
PerpVector = XMVector3Normalize( PerpVector );
XMStoreFloat3( &verts[1].position, XMVectorAdd( RayDirection, Origin ) );
PerpVector = XMVectorScale( PerpVector, 0.0625f );
NormDirection = XMVectorScale( NormDirection, -0.25f );
RayDirection = XMVectorAdd( PerpVector, RayDirection );
RayDirection = XMVectorAdd( NormDirection, RayDirection );
XMStoreFloat3( &verts[2].position, XMVectorAdd( RayDirection, Origin ) );
XMStoreFloat4( &verts[0].color, color );
XMStoreFloat4( &verts[1].color, color );
XMStoreFloat4( &verts[2].color, color );
auto context = DXUTGetD3D11DeviceContext();
g_BatchEffect->Apply( context );
context->IASetInputLayout( g_pBatchInputLayout );
g_Batch->Begin();
g_Batch->Draw( D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP, verts, 2 );
g_Batch->End();
}
//--------------------------------------------------------------------------------------
void DrawTriangle( FXMVECTOR PointA, FXMVECTOR PointB, FXMVECTOR PointC, CXMVECTOR color )
{
VertexPositionColor verts[4];
XMStoreFloat3( &verts[0].position, PointA );
XMStoreFloat3( &verts[1].position, PointB );
XMStoreFloat3( &verts[2].position, PointC );
XMStoreFloat3( &verts[3].position, PointA );
XMStoreFloat4( &verts[0].color, color );
XMStoreFloat4( &verts[1].color, color );
XMStoreFloat4( &verts[2].color, color );
XMStoreFloat4( &verts[3].color, color );
auto context = DXUTGetD3D11DeviceContext();
g_BatchEffect->Apply( context );
context->IASetInputLayout( g_pBatchInputLayout );
g_Batch->Begin();
g_Batch->Draw( D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP, verts, 4 );
g_Batch->End();
}
//--------------------------------------------------------------------------------------
// Reject any D3D11 devices that aren't acceptable by returning false
//--------------------------------------------------------------------------------------
bool CALLBACK IsD3D11DeviceAcceptable( const CD3D11EnumAdapterInfo *AdapterInfo, UINT Output,
const CD3D11EnumDeviceInfo *DeviceInfo,
DXGI_FORMAT BackBufferFormat, bool bWindowed, void* pUserContext )
{
return true;
}
//--------------------------------------------------------------------------------------
// Create any D3D11 resources that aren't dependant on the back buffer
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnD3D11CreateDevice( ID3D11Device* pd3dDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc,
void* pUserContext )
{
HRESULT hr;
auto pd3dImmediateContext = DXUTGetD3D11DeviceContext();
V_RETURN( g_DialogResourceManager.OnD3D11CreateDevice( pd3dDevice, pd3dImmediateContext ) );
V_RETURN( g_SettingsDlg.OnD3D11CreateDevice( pd3dDevice ) );
g_pTxtHelper = new CDXUTTextHelper( pd3dDevice, pd3dImmediateContext, &g_DialogResourceManager, 15 );
// Create other render resources here
g_States = std::make_unique<CommonStates>( pd3dDevice );
g_Batch = std::make_unique<PrimitiveBatch<VertexPositionColor>>( pd3dImmediateContext );
g_BatchEffect = std::make_unique<BasicEffect>( pd3dDevice );
g_BatchEffect->SetVertexColorEnabled(true);
{
void const* shaderByteCode;
size_t byteCodeLength;
g_BatchEffect->GetVertexShaderBytecode( &shaderByteCode, &byteCodeLength );
hr = pd3dDevice->CreateInputLayout( VertexPositionColor::InputElements,
VertexPositionColor::InputElementCount,
shaderByteCode, byteCodeLength,
&g_pBatchInputLayout );
if( FAILED( hr ) )
return hr;
}
// Setup the camera's view parameters
auto pComboBox = g_SampleUI.GetComboBox( IDC_GROUP );
SetViewForGroup( (pComboBox) ? (int)PtrToInt( pComboBox->GetSelectedData() ) : 0 );
g_HUD.GetButton( IDC_TOGGLEWARP )->SetEnabled( true );
return S_OK;
}
//--------------------------------------------------------------------------------------
// Create any D3D11 resources that depend on the back buffer
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnD3D11ResizedSwapChain( ID3D11Device* pd3dDevice, IDXGISwapChain* pSwapChain,
const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
{
HRESULT hr;
V_RETURN( g_DialogResourceManager.OnD3D11ResizedSwapChain( pd3dDevice, pBackBufferSurfaceDesc ) );