Merge pull request #5 from xLightling/a6_meshes

A6: 3D Models, Materials & Shaders
This commit is contained in:
Lightling 2022-02-27 14:40:58 -05:00 committed by GitHub
commit ec64fc1475
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 24079 additions and 214 deletions

3
.gitignore vendored
View file

@ -348,3 +348,6 @@ MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
.ionide/
# Don't ignore models
!Assets/**/*.obj

90
Assets/Models/cube.obj Normal file
View file

@ -0,0 +1,90 @@
# Blender v2.83.2 OBJ File: ''
# www.blender.org
o cube.001
v -1.000000 -1.000000 1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
v 1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
v 1.000000 1.000000 -1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v -1.000000 -1.000000 1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
v 1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
v 1.000000 1.000000 -1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 0.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 0.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
vn 0.0000 -0.0000 1.0000
vn 0.0000 1.0000 0.0000
vn 0.0000 0.0000 -1.0000
vn 0.0000 -1.0000 -0.0000
vn 1.0000 0.0000 0.0000
vn -1.0000 0.0000 0.0000
s 1
f 1/1/1 2/2/1 3/3/1
f 3/3/1 2/2/1 4/4/1
f 3/5/2 4/6/2 5/7/2
f 5/7/2 4/6/2 6/8/2
f 5/9/3 6/10/3 7/11/3
f 7/11/3 6/10/3 8/12/3
f 7/13/4 8/14/4 1/15/4
f 1/15/4 8/14/4 2/16/4
f 2/17/5 8/18/5 4/4/5
f 4/4/5 8/18/5 6/10/5
f 7/11/6 1/1/6 5/19/6
f 5/19/6 1/1/6 3/20/6
f 9/21/1 10/22/1 11/23/1
f 11/23/1 10/22/1 12/24/1
f 11/25/2 12/26/2 13/27/2
f 13/27/2 12/26/2 14/28/2
f 13/29/3 14/30/3 15/31/3
f 15/31/3 14/30/3 16/32/3
f 15/33/4 16/34/4 9/35/4
f 9/35/4 16/34/4 10/36/4
f 10/37/5 16/38/5 12/24/5
f 12/24/5 16/38/5 14/30/5
f 15/31/6 9/21/6 13/39/6
f 13/39/6 9/21/6 11/40/6

357
Assets/Models/cylinder.obj Normal file
View file

@ -0,0 +1,357 @@
# Blender v2.83.2 OBJ File: ''
# www.blender.org
o Cylinder
v 0.000000 -1.000000 -1.000000
v 0.000000 1.000000 -1.000000
v 0.195090 -1.000000 -0.980785
v 0.195090 1.000000 -0.980785
v 0.382683 -1.000000 -0.923880
v 0.382683 1.000000 -0.923880
v 0.555570 -1.000000 -0.831470
v 0.555570 1.000000 -0.831470
v 0.707107 -1.000000 -0.707107
v 0.707107 1.000000 -0.707107
v 0.831470 -1.000000 -0.555570
v 0.831470 1.000000 -0.555570
v 0.923880 -1.000000 -0.382683
v 0.923880 1.000000 -0.382683
v 0.980785 -1.000000 -0.195090
v 0.980785 1.000000 -0.195090
v 1.000000 -1.000000 -0.000000
v 1.000000 1.000000 -0.000000
v 0.980785 -1.000000 0.195090
v 0.980785 1.000000 0.195090
v 0.923880 -1.000000 0.382683
v 0.923880 1.000000 0.382683
v 0.831470 -1.000000 0.555570
v 0.831470 1.000000 0.555570
v 0.707107 -1.000000 0.707107
v 0.707107 1.000000 0.707107
v 0.555570 -1.000000 0.831470
v 0.555570 1.000000 0.831470
v 0.382683 -1.000000 0.923880
v 0.382683 1.000000 0.923880
v 0.195090 -1.000000 0.980785
v 0.195090 1.000000 0.980785
v -0.000000 -1.000000 1.000000
v -0.000000 1.000000 1.000000
v -0.195091 -1.000000 0.980785
v -0.195091 1.000000 0.980785
v -0.382684 -1.000000 0.923879
v -0.382684 1.000000 0.923879
v -0.555571 -1.000000 0.831469
v -0.555571 1.000000 0.831469
v -0.707107 -1.000000 0.707106
v -0.707107 1.000000 0.707106
v -0.831470 -1.000000 0.555570
v -0.831470 1.000000 0.555570
v -0.923880 -1.000000 0.382683
v -0.923880 1.000000 0.382683
v -0.980785 -1.000000 0.195089
v -0.980785 1.000000 0.195089
v -1.000000 -1.000000 -0.000001
v -1.000000 1.000000 -0.000001
v -0.980785 -1.000000 -0.195091
v -0.980785 1.000000 -0.195091
v -0.923879 -1.000000 -0.382684
v -0.923879 1.000000 -0.382684
v -0.831469 -1.000000 -0.555571
v -0.831469 1.000000 -0.555571
v -0.707106 -1.000000 -0.707108
v -0.707106 1.000000 -0.707108
v -0.555569 -1.000000 -0.831470
v -0.555569 1.000000 -0.831470
v -0.382682 -1.000000 -0.923880
v -0.382682 1.000000 -0.923880
v -0.195089 -1.000000 -0.980786
v -0.195089 1.000000 -0.980786
vt 0.028269 0.341844
vt 0.158156 0.028269
vt 0.471731 0.158156
vt 0.985388 0.296822
vt 0.796822 0.014612
vt 0.514611 0.203179
vt 0.341844 0.471731
vt 0.296822 0.485388
vt 0.250000 0.490000
vt 0.203179 0.485389
vt 0.158156 0.471731
vt 0.116663 0.449553
vt 0.080295 0.419706
vt 0.050447 0.383337
vt 0.014612 0.296822
vt 0.010000 0.250000
vt 0.014611 0.203179
vt 0.028269 0.158156
vt 0.050447 0.116663
vt 0.080294 0.080294
vt 0.116663 0.050447
vt 0.203178 0.014612
vt 0.250000 0.010000
vt 0.296822 0.014612
vt 0.341844 0.028269
vt 0.383337 0.050447
vt 0.419706 0.080294
vt 0.449553 0.116663
vt 0.485388 0.203178
vt 0.490000 0.250000
vt 0.485388 0.296822
vt 0.471731 0.341844
vt 0.449553 0.383337
vt 0.419706 0.419706
vt 0.383337 0.449553
vt 0.703179 0.485389
vt 0.750000 0.490000
vt 0.796822 0.485388
vt 0.841844 0.471731
vt 0.883337 0.449553
vt 0.919706 0.419706
vt 0.949553 0.383337
vt 0.971731 0.341844
vt 0.990000 0.250000
vt 0.985388 0.203178
vt 0.971731 0.158156
vt 0.949553 0.116663
vt 0.919706 0.080294
vt 0.883337 0.050447
vt 0.841844 0.028269
vt 0.750000 0.010000
vt 0.703178 0.014612
vt 0.658156 0.028269
vt 0.616663 0.050447
vt 0.580294 0.080294
vt 0.550447 0.116663
vt 0.528269 0.158156
vt 0.510000 0.250000
vt 0.514612 0.296822
vt 0.528269 0.341844
vt 0.550447 0.383337
vt 0.580295 0.419706
vt 0.616663 0.449553
vt 0.658156 0.471731
vt 1.000000 1.000000
vt 0.968750 0.500000
vt 1.000000 0.500000
vt 0.968750 1.000000
vt 0.937500 0.500000
vt 0.937500 1.000000
vt 0.906250 0.500000
vt 0.906250 1.000000
vt 0.875000 0.500000
vt 0.875000 1.000000
vt 0.843750 0.500000
vt 0.843750 1.000000
vt 0.812500 0.500000
vt 0.812500 1.000000
vt 0.781250 0.500000
vt 0.781250 1.000000
vt 0.750000 0.500000
vt 0.750000 1.000000
vt 0.718750 0.500000
vt 0.718750 1.000000
vt 0.687500 0.500000
vt 0.687500 1.000000
vt 0.656250 0.500000
vt 0.656250 1.000000
vt 0.625000 0.500000
vt 0.625000 1.000000
vt 0.593750 0.500000
vt 0.593750 1.000000
vt 0.562500 0.500000
vt 0.562500 1.000000
vt 0.531250 0.500000
vt 0.531250 1.000000
vt 0.500000 0.500000
vt 0.500000 1.000000
vt 0.468750 0.500000
vt 0.468750 1.000000
vt 0.437500 0.500000
vt 0.437500 1.000000
vt 0.406250 0.500000
vt 0.406250 1.000000
vt 0.375000 0.500000
vt 0.375000 1.000000
vt 0.343750 0.500000
vt 0.343750 1.000000
vt 0.312500 0.500000
vt 0.312500 1.000000
vt 0.281250 0.500000
vt 0.281250 1.000000
vt 0.250000 0.500000
vt 0.250000 1.000000
vt 0.218750 0.500000
vt 0.218750 1.000000
vt 0.187500 0.500000
vt 0.187500 1.000000
vt 0.156250 0.500000
vt 0.156250 1.000000
vt 0.125000 0.500000
vt 0.125000 1.000000
vt 0.093750 0.500000
vt 0.093750 1.000000
vt 0.062500 0.500000
vt 0.062500 1.000000
vt 0.031250 0.500000
vt 0.031250 1.000000
vt 0.000000 0.500000
vt 0.000000 1.000000
vn 0.0000 1.0000 0.0000
vn 0.0000 -1.0000 0.0000
vn 0.0000 0.0000 -1.0000
vn 0.1951 0.0000 -0.9808
vn 0.3827 0.0000 -0.9239
vn 0.5556 0.0000 -0.8315
vn 0.7071 0.0000 -0.7071
vn 0.8315 0.0000 -0.5556
vn 0.9239 0.0000 -0.3827
vn 0.9808 0.0000 -0.1951
vn 1.0000 0.0000 -0.0000
vn 0.9808 0.0000 0.1951
vn 0.9239 0.0000 0.3827
vn 0.8315 0.0000 0.5556
vn 0.7071 0.0000 0.7071
vn 0.5556 0.0000 0.8315
vn 0.3827 0.0000 0.9239
vn 0.1951 0.0000 0.9808
vn -0.0000 0.0000 1.0000
vn -0.1951 0.0000 0.9808
vn -0.3827 0.0000 0.9239
vn -0.5556 0.0000 0.8315
vn -0.7071 0.0000 0.7071
vn -0.8315 0.0000 0.5556
vn -0.9239 0.0000 0.3827
vn -0.9808 0.0000 0.1951
vn -1.0000 0.0000 -0.0000
vn -0.9808 0.0000 -0.1951
vn -0.9239 0.0000 -0.3827
vn -0.8315 0.0000 -0.5556
vn -0.7071 0.0000 -0.7071
vn -0.5556 0.0000 -0.8315
vn -0.3827 0.0000 -0.9239
vn -0.1951 0.0000 -0.9808
s off
f 54/1/1 38/2/1 22/3/1
f 15/4/2 31/5/2 47/6/2
f 6/7/1 4/8/1 2/9/1
f 2/9/1 64/10/1 6/7/1
f 64/10/1 62/11/1 6/7/1
f 62/11/1 60/12/1 58/13/1
f 58/13/1 56/14/1 54/1/1
f 54/1/1 52/15/1 50/16/1
f 50/16/1 48/17/1 54/1/1
f 48/17/1 46/18/1 54/1/1
f 46/18/1 44/19/1 38/2/1
f 44/19/1 42/20/1 38/2/1
f 42/20/1 40/21/1 38/2/1
f 38/2/1 36/22/1 34/23/1
f 34/23/1 32/24/1 30/25/1
f 30/25/1 28/26/1 26/27/1
f 26/27/1 24/28/1 22/3/1
f 22/3/1 20/29/1 18/30/1
f 18/30/1 16/31/1 22/3/1
f 16/31/1 14/32/1 22/3/1
f 14/32/1 12/33/1 10/34/1
f 10/34/1 8/35/1 6/7/1
f 62/11/1 58/13/1 6/7/1
f 58/13/1 54/1/1 6/7/1
f 38/2/1 34/23/1 22/3/1
f 34/23/1 30/25/1 22/3/1
f 30/25/1 26/27/1 22/3/1
f 14/32/1 10/34/1 22/3/1
f 10/34/1 6/7/1 22/3/1
f 54/1/1 46/18/1 38/2/1
f 6/7/1 54/1/1 22/3/1
f 63/36/2 1/37/2 3/38/2
f 3/38/2 5/39/2 7/40/2
f 7/40/2 9/41/2 11/42/2
f 11/42/2 13/43/2 7/40/2
f 13/43/2 15/4/2 7/40/2
f 15/4/2 17/44/2 19/45/2
f 19/45/2 21/46/2 15/4/2
f 21/46/2 23/47/2 15/4/2
f 23/47/2 25/48/2 31/5/2
f 25/48/2 27/49/2 31/5/2
f 27/49/2 29/50/2 31/5/2
f 31/5/2 33/51/2 35/52/2
f 35/52/2 37/53/2 39/54/2
f 39/54/2 41/55/2 43/56/2
f 43/56/2 45/57/2 47/6/2
f 47/6/2 49/58/2 51/59/2
f 51/59/2 53/60/2 55/61/2
f 55/61/2 57/62/2 63/36/2
f 57/62/2 59/63/2 63/36/2
f 59/63/2 61/64/2 63/36/2
f 63/36/2 3/38/2 7/40/2
f 31/5/2 35/52/2 47/6/2
f 35/52/2 39/54/2 47/6/2
f 39/54/2 43/56/2 47/6/2
f 47/6/2 51/59/2 63/36/2
f 51/59/2 55/61/2 63/36/2
f 63/36/2 7/40/2 15/4/2
f 15/4/2 23/47/2 31/5/2
f 63/36/2 15/4/2 47/6/2
s 1
f 2/65/3 3/66/4 1/67/3
f 4/68/4 5/69/5 3/66/4
f 6/70/5 7/71/6 5/69/5
f 8/72/6 9/73/7 7/71/6
f 10/74/7 11/75/8 9/73/7
f 12/76/8 13/77/9 11/75/8
f 14/78/9 15/79/10 13/77/9
f 16/80/10 17/81/11 15/79/10
f 18/82/11 19/83/12 17/81/11
f 20/84/12 21/85/13 19/83/12
f 22/86/13 23/87/14 21/85/13
f 24/88/14 25/89/15 23/87/14
f 26/90/15 27/91/16 25/89/15
f 28/92/16 29/93/17 27/91/16
f 30/94/17 31/95/18 29/93/17
f 32/96/18 33/97/19 31/95/18
f 34/98/19 35/99/20 33/97/19
f 36/100/20 37/101/21 35/99/20
f 38/102/21 39/103/22 37/101/21
f 40/104/22 41/105/23 39/103/22
f 42/106/23 43/107/24 41/105/23
f 44/108/24 45/109/25 43/107/24
f 46/110/25 47/111/26 45/109/25
f 48/112/26 49/113/27 47/111/26
f 50/114/27 51/115/28 49/113/27
f 52/116/28 53/117/29 51/115/28
f 54/118/29 55/119/30 53/117/29
f 56/120/30 57/121/31 55/119/30
f 58/122/31 59/123/32 57/121/31
f 60/124/32 61/125/33 59/123/32
f 62/126/33 63/127/34 61/125/33
f 64/128/34 1/129/3 63/127/34
f 2/65/3 4/68/4 3/66/4
f 4/68/4 6/70/5 5/69/5
f 6/70/5 8/72/6 7/71/6
f 8/72/6 10/74/7 9/73/7
f 10/74/7 12/76/8 11/75/8
f 12/76/8 14/78/9 13/77/9
f 14/78/9 16/80/10 15/79/10
f 16/80/10 18/82/11 17/81/11
f 18/82/11 20/84/12 19/83/12
f 20/84/12 22/86/13 21/85/13
f 22/86/13 24/88/14 23/87/14
f 24/88/14 26/90/15 25/89/15
f 26/90/15 28/92/16 27/91/16
f 28/92/16 30/94/17 29/93/17
f 30/94/17 32/96/18 31/95/18
f 32/96/18 34/98/19 33/97/19
f 34/98/19 36/100/20 35/99/20
f 36/100/20 38/102/21 37/101/21
f 38/102/21 40/104/22 39/103/22
f 40/104/22 42/106/23 41/105/23
f 42/106/23 44/108/24 43/107/24
f 44/108/24 46/110/25 45/109/25
f 46/110/25 48/112/26 47/111/26
f 48/112/26 50/114/27 49/113/27
f 50/114/27 52/116/28 51/115/28
f 52/116/28 54/118/29 53/117/29
f 54/118/29 56/120/30 55/119/30
f 56/120/30 58/122/31 57/121/31
f 58/122/31 60/124/32 59/123/32
f 60/124/32 62/126/33 61/125/33
f 62/126/33 64/128/34 63/127/34
f 64/128/34 2/130/3 1/129/3

14856
Assets/Models/helix.obj Normal file

File diff suppressed because it is too large Load diff

15
Assets/Models/quad.obj Normal file
View file

@ -0,0 +1,15 @@
# Blender v2.83.2 OBJ File: ''
# www.blender.org
o Plane
v -1.000000 0.000000 1.000000
v 1.000000 0.000000 1.000000
v -1.000000 0.000000 -1.000000
v 1.000000 0.000000 -1.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 1.000000
vn 0.0000 1.0000 0.0000
s off
f 2/1/1 3/2/1 1/3/1
f 2/1/1 4/4/1 3/2/1

View file

@ -0,0 +1,24 @@
# Blender v2.83.2 OBJ File: ''
# www.blender.org
o Plane
v -1.000000 0.000000 1.000000
v 1.000000 0.000000 1.000000
v -1.000000 0.000000 -1.000000
v 1.000000 0.000000 -1.000000
v 1.000000 0.000000 1.000000
v -1.000000 -0.000000 1.000000
v 1.000000 0.000000 -1.000000
v -1.000000 -0.000000 -1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
vn 0.0000 1.0000 0.0000
vn 0.0000 -1.0000 0.0000
s off
f 1/1/1 2/2/1 4/3/1 3/4/1
f 5/5/2 6/6/2 8/7/2 7/8/2

2487
Assets/Models/sphere.obj Normal file

File diff suppressed because it is too large Load diff

3225
Assets/Models/torus.obj Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,11 +0,0 @@
#pragma once
#include <DirectXMath.h>
struct VertexShaderExternalData
{
DirectX::XMFLOAT4 colorTint;
DirectX::XMFLOAT4X4 world;
DirectX::XMFLOAT4X4 view;
DirectX::XMFLOAT4X4 projection;
};

View file

@ -129,17 +129,20 @@
<ClCompile Include="Game.cpp" />
<ClCompile Include="Input.cpp" />
<ClCompile Include="Main.cpp" />
<ClCompile Include="Material.cpp" />
<ClCompile Include="Mesh.cpp" />
<ClCompile Include="SimpleShader.cpp" />
<ClCompile Include="Transform.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="BufferStructs.h" />
<ClInclude Include="Camera.h" />
<ClInclude Include="DXCore.h" />
<ClInclude Include="Entity.h" />
<ClInclude Include="Game.h" />
<ClInclude Include="Input.h" />
<ClInclude Include="Material.h" />
<ClInclude Include="Mesh.h" />
<ClInclude Include="SimpleShader.h" />
<ClInclude Include="Transform.h" />
<ClInclude Include="Vertex.h" />
</ItemGroup>
@ -154,6 +157,16 @@
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Pixel</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|x64'">5.0</ShaderModel>
</FxCompile>
<FxCompile Include="RandomPixelShader.hlsl">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Pixel</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Pixel</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Pixel</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">5.0</ShaderModel>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">5.0</ShaderModel>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">5.0</ShaderModel>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|x64'">5.0</ShaderModel>
</FxCompile>
<FxCompile Include="VertexShader.hlsl">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Vertex</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">5.0</ShaderModel>
@ -165,6 +178,113 @@
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|x64'">5.0</ShaderModel>
</FxCompile>
</ItemGroup>
<ItemGroup>
<CopyFileToFolders Include="Assets\Models\cube.obj">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</DeploymentContent>
<FileType>Document</FileType>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</DeploymentContent>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</DeploymentContent>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(OutDir)/Assets/Models</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(OutDir)/Assets/Models</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(OutDir)/Assets/Models</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(OutDir)/Assets/Models</DestinationFolders>
</CopyFileToFolders>
<CopyFileToFolders Include="Assets\Models\cylinder.obj">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</DeploymentContent>
<FileType>Document</FileType>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</DeploymentContent>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</DeploymentContent>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(OutDir)/Assets/Models</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(OutDir)/Assets/Models</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(OutDir)/Assets/Models</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(OutDir)/Assets/Models</DestinationFolders>
</CopyFileToFolders>
<CopyFileToFolders Include="Assets\Models\helix.obj">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</DeploymentContent>
<FileType>Document</FileType>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</DeploymentContent>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</DeploymentContent>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(OutDir)/Assets/Models</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(OutDir)/Assets/Models</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(OutDir)/Assets/Models</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(OutDir)/Assets/Models</DestinationFolders>
</CopyFileToFolders>
<CopyFileToFolders Include="Assets\Models\quad.obj">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</DeploymentContent>
<FileType>Document</FileType>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</DeploymentContent>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</DeploymentContent>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(OutDir)/Assets/Models</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(OutDir)/Assets/Models</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(OutDir)/Assets/Models</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(OutDir)/Assets/Models</DestinationFolders>
</CopyFileToFolders>
<CopyFileToFolders Include="Assets\Models\quad_double_sided.obj">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</DeploymentContent>
<FileType>Document</FileType>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</DeploymentContent>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</DeploymentContent>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(OutDir)/Assets/Models</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(OutDir)/Assets/Models</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(OutDir)/Assets/Models</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(OutDir)/Assets/Models</DestinationFolders>
</CopyFileToFolders>
<CopyFileToFolders Include="Assets\Models\sphere.obj">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</DeploymentContent>
<FileType>Document</FileType>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</DeploymentContent>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</DeploymentContent>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(OutDir)/Assets/Models</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(OutDir)/Assets/Models</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(OutDir)/Assets/Models</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(OutDir)/Assets/Models</DestinationFolders>
</CopyFileToFolders>
<CopyFileToFolders Include="Assets\Models\torus.obj">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</DeploymentContent>
<FileType>Document</FileType>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</DeploymentContent>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</DeploymentContent>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(OutDir)/Assets/Models</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(OutDir)/Assets/Models</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(OutDir)/Assets/Models</DestinationFolders>
<DestinationFolders Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(OutDir)/Assets/Models</DestinationFolders>
</CopyFileToFolders>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>

View file

@ -16,6 +16,12 @@
<Filter Include="Shaders">
<UniqueIdentifier>{e76f01f5-08db-40d4-8720-b5b21f7ec0a3}</UniqueIdentifier>
</Filter>
<Filter Include="Assets">
<UniqueIdentifier>{27304946-4bdd-48b3-86d6-d0de49df247a}</UniqueIdentifier>
</Filter>
<Filter Include="Assets\Models">
<UniqueIdentifier>{70d904c1-abb7-4ffe-a6fd-58c67ea6f72b}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="DXCore.cpp">
@ -42,6 +48,12 @@
<ClCompile Include="Camera.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="SimpleShader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Material.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Vertex.h">
@ -59,9 +71,6 @@
<ClInclude Include="Mesh.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="BufferStructs.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Transform.h">
<Filter>Header Files</Filter>
</ClInclude>
@ -71,6 +80,12 @@
<ClInclude Include="Camera.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="SimpleShader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Material.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<FxCompile Include="PixelShader.hlsl">
@ -79,5 +94,31 @@
<FxCompile Include="VertexShader.hlsl">
<Filter>Shaders</Filter>
</FxCompile>
<FxCompile Include="RandomPixelShader.hlsl">
<Filter>Shaders</Filter>
</FxCompile>
</ItemGroup>
<ItemGroup>
<CopyFileToFolders Include="Assets\Models\cube.obj">
<Filter>Assets\Models</Filter>
</CopyFileToFolders>
<CopyFileToFolders Include="Assets\Models\cylinder.obj">
<Filter>Assets\Models</Filter>
</CopyFileToFolders>
<CopyFileToFolders Include="Assets\Models\helix.obj">
<Filter>Assets\Models</Filter>
</CopyFileToFolders>
<CopyFileToFolders Include="Assets\Models\quad.obj">
<Filter>Assets\Models</Filter>
</CopyFileToFolders>
<CopyFileToFolders Include="Assets\Models\quad_double_sided.obj">
<Filter>Assets\Models</Filter>
</CopyFileToFolders>
<CopyFileToFolders Include="Assets\Models\sphere.obj">
<Filter>Assets\Models</Filter>
</CopyFileToFolders>
<CopyFileToFolders Include="Assets\Models\torus.obj">
<Filter>Assets\Models</Filter>
</CopyFileToFolders>
</ItemGroup>
</Project>

View file

@ -1,7 +1,8 @@
#include "Entity.h"
Entity::Entity(std::shared_ptr<Mesh> _mesh)
Entity::Entity(std::shared_ptr<Material> _material, std::shared_ptr<Mesh> _mesh)
{
material = _material;
mesh = _mesh;
}
@ -14,3 +15,13 @@ std::shared_ptr<Mesh> Entity::GetMesh()
{
return mesh;
}
std::shared_ptr<Material> Entity::GetMaterial()
{
return material;
}
void Entity::SetMaterial(std::shared_ptr<Material> _material)
{
material = _material;
}

View file

@ -2,17 +2,24 @@
#include "Mesh.h"
#include "Transform.h"
#include "Material.h"
#include <memory>
class Entity
{
public:
Entity(std::shared_ptr<Mesh> _mesh);
Entity(
std::shared_ptr<Material> _material,
std::shared_ptr<Mesh> _mesh);
Transform* GetTransform();
std::shared_ptr<Mesh> GetMesh();
Transform* GetTransform();
std::shared_ptr<Mesh> GetMesh();
std::shared_ptr<Material> GetMaterial();
void SetMaterial(std::shared_ptr<Material> _material);
private:
Transform transform;
std::shared_ptr<Mesh> mesh;
Transform transform;
std::shared_ptr<Mesh> mesh;
std::shared_ptr<Material> material;
};

231
Game.cpp
View file

@ -1,7 +1,7 @@
#include "Game.h"
#include "Vertex.h"
#include "Input.h"
#include "BufferStructs.h"
#include "SimpleShader.h"
// Needed for a helper function to read compiled shader files from the hard drive
#pragma comment(lib, "d3dcompiler.lib")
@ -33,7 +33,7 @@ Game::Game(HINSTANCE hInstance)
CreateConsoleWindow(500, 120, 32, 120);
printf("Console window created successfully. Feel free to printf() here.\n");
#endif
camera = std::make_shared<Camera>(0.0f, 0.0f, -4.0f, (float)width / height, 60, 0.01f, 1000.0f);
camera = std::make_shared<Camera>(0.0f, 0.0f, -20.0f, (float)width / height, 60, 0.01f, 1000.0f);
}
// --------------------------------------------------------
@ -66,21 +66,6 @@ void Game::Init()
// geometric primitives (points, lines or triangles) we want to draw.
// Essentially: "What kind of shape should the GPU draw with our data?"
context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// Get size as the next multiple of 16 (instead of hardcoding a size here!)
unsigned int size = sizeof(VertexShaderExternalData);
// This will work even if your struct size changes.
// Adding 15 ensures either go past next multiple of 16, or if size is already a multiple, we almost get to next multiple.
// Integer division tells us how many 16's would fit (w/o remainder). Get back to multiple of 16 with multiplication step.
size = (size + 15) / 16 * 16;
// Describe constant buffer
D3D11_BUFFER_DESC cbDesc = {}; // zero-out
cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbDesc.ByteWidth = size; // must be multiple of 16
cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
cbDesc.Usage = D3D11_USAGE_DYNAMIC;
device->CreateBuffer(&cbDesc, 0, constantBufferVS.GetAddressOf());
}
// --------------------------------------------------------
@ -93,125 +78,66 @@ void Game::Init()
// --------------------------------------------------------
void Game::LoadShaders()
{
// Blob for reading raw data
// - This is a simplified way of handling raw data
ID3DBlob* shaderBlob;
vertexShader = std::make_shared<SimpleVertexShader>(device, context, GetFullPathTo_Wide(L"VertexShader.cso").c_str());
pixelShader = //std::make_shared<SimplePixelShader>(device, context, GetFullPathTo_Wide(L"PixelShader.cso").c_str());
std::make_shared<SimplePixelShader>(device, context, GetFullPathTo_Wide(L"RandomPixelShader.cso").c_str());
// Read our compiled vertex shader code into a blob
// - Essentially just "open the file and plop its contents here"
D3DReadFileToBlob(
GetFullPathTo_Wide(L"VertexShader.cso").c_str(), // Using a custom helper for file paths
&shaderBlob);
// thanks to https://harry7557558.github.io/tools/colorpicker.html for having the only 0f-1f picker i could find
XMFLOAT4 white = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
XMFLOAT4 deeppink = XMFLOAT4(1.0f, 0.08f, 0.4f, 1.0f);
XMFLOAT4 deepcoral = XMFLOAT4(1.0f, 0.39f, 0.22f, 1.0f);
// Create a vertex shader from the information we
// have read into the blob above
// - A blob can give a pointer to its contents, and knows its own size
device->CreateVertexShader(
shaderBlob->GetBufferPointer(), // Get a pointer to the blob's contents
shaderBlob->GetBufferSize(), // How big is that data?
0, // No classes in this shader
vertexShader.GetAddressOf()); // The address of the ID3D11VertexShader*
// Create an input layout that describes the vertex format
// used by the vertex shader we're using
// - This is used by the pipeline to know how to interpret the raw data
// sitting inside a vertex buffer
// - Doing this NOW because it requires a vertex shader's byte code to verify against!
// - Luckily, we already have that loaded (the blob above)
D3D11_INPUT_ELEMENT_DESC inputElements[2] = {};
// Set up the first element - a position, which is 3 float values
inputElements[0].Format = DXGI_FORMAT_R32G32B32_FLOAT; // Most formats are described as color channels; really it just means "Three 32-bit floats"
inputElements[0].SemanticName = "POSITION"; // This is "POSITION" - needs to match the semantics in our vertex shader input!
inputElements[0].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; // How far into the vertex is this? Assume it's after the previous element
// Set up the second element - a color, which is 4 more float values
inputElements[1].Format = DXGI_FORMAT_R32G32B32A32_FLOAT; // 4x 32-bit floats
inputElements[1].SemanticName = "COLOR"; // Match our vertex shader input!
inputElements[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; // After the previous element
// Create the input layout, verifying our description against actual shader code
device->CreateInputLayout(
inputElements, // An array of descriptions
2, // How many elements in that array
shaderBlob->GetBufferPointer(), // Pointer to the code of a shader that uses this layout
shaderBlob->GetBufferSize(), // Size of the shader code that uses this layout
inputLayout.GetAddressOf()); // Address of the resulting ID3D11InputLayout*
// Read and create the pixel shader
// - Reusing the same blob here, since we're done with the vert shader code
D3DReadFileToBlob(
GetFullPathTo_Wide(L"PixelShader.cso").c_str(), // Using a custom helper for file paths
&shaderBlob);
device->CreatePixelShader(
shaderBlob->GetBufferPointer(),
shaderBlob->GetBufferSize(),
0,
pixelShader.GetAddressOf());
materials = {
std::make_shared<Material>(white, vertexShader, pixelShader),
std::make_shared<Material>(deeppink, vertexShader, pixelShader),
std::make_shared<Material>(deepcoral, vertexShader, pixelShader),
};
}
// --------------------------------------------------------
// Creates the geometry we're going to draw - a single triangle for now
// --------------------------------------------------------
void Game::CreateBasicGeometry()
{
// Create some temporary variables to represent colors
// - Not necessary, just makes things more readable
XMFLOAT4 red = XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f);
XMFLOAT4 green = XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f);
XMFLOAT4 blue = XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f);
XMFLOAT4 black = XMFLOAT4(0.0f, 0.0f, 0.0f, 1.0f);
XMFLOAT4 white = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
Vertex verts1[] = {
{ XMFLOAT3(+0.00f, +0.00f, +0.25f), white },
{ XMFLOAT3(-0.25f, -0.25f, -0.25f), red },
{ XMFLOAT3(+0.00f, +0.25f, -0.25f), green },
{ XMFLOAT3(+0.25f, -0.25f, -0.25f), blue },
};
unsigned int ind1[] = { 0,1,2 , 0,2,3 , 0,3,1 , 3,2,1 };
Vertex verts2[] = {
{ XMFLOAT3(-0.75f, +0.50f, +0.00f), red },
{ XMFLOAT3(-0.50f, +0.50f, +0.00f), blue },
{ XMFLOAT3(-0.50f, +0.20f, +0.00f), red },
{ XMFLOAT3(-0.75f, +0.20f, +0.00f), blue },
};
unsigned int ind2[] = { 0,1,2, 0,2,3 , 3,2,0 , 2,1,0 };
Vertex verts3[] = {
{ XMFLOAT3(+0.00f, +0.30f, +0.15f), white },
{ XMFLOAT3(+0.30f, +0.15f, +0.00f), black },
{ XMFLOAT3(+0.30f, -0.15f, +0.00f), white },
{ XMFLOAT3(+0.00f, -0.30f, +0.15f), black },
{ XMFLOAT3(-0.30f, -0.15f, +0.00f), white },
{ XMFLOAT3(-0.30f, +0.15f, +0.00f), black },
};
unsigned int ind3[] = { 0,1,5 , 1,2,5 , 2,3,4 , 2,4,5 , 5,4,2 , 4,3,2 , 5,2,1 , 5,1,0 };
shapes = {
std::make_shared<Mesh>(verts1, 4, ind1, 12, device, context),
std::make_shared<Mesh>(verts2, 4, ind2, 12, device, context),
std::make_shared<Mesh>(verts3, 6, ind3, 24, device, context),
std::make_shared<Mesh>(
GetFullPathTo("Assets/Models/cube.obj").c_str(),
device, context),
std::make_shared<Mesh>(
GetFullPathTo("Assets/Models/cylinder.obj").c_str(),
device, context),
std::make_shared<Mesh>(
GetFullPathTo("Assets/Models/helix.obj").c_str(),
device, context),
std::make_shared<Mesh>(
GetFullPathTo("Assets/Models/quad.obj").c_str(),
device, context),
std::make_shared<Mesh>(
GetFullPathTo("Assets/Models/quad_double_sided.obj").c_str(),
device, context),
std::make_shared<Mesh>(
GetFullPathTo("Assets/Models/sphere.obj").c_str(),
device, context),
std::make_shared<Mesh>(
GetFullPathTo("Assets/Models/torus.obj").c_str(),
device, context),
};
entities = {
std::make_shared<Entity>(shapes[0]),
std::make_shared<Entity>(shapes[0]),
std::make_shared<Entity>(shapes[0]),
std::make_shared<Entity>(shapes[1]),
std::make_shared<Entity>(shapes[1]),
std::make_shared<Entity>(shapes[1]),
std::make_shared<Entity>(shapes[2]),
std::make_shared<Entity>(shapes[2]),
std::make_shared<Entity>(shapes[2]),
std::make_shared<Entity>(materials[0], shapes[0]),
std::make_shared<Entity>(materials[1], shapes[1]),
std::make_shared<Entity>(materials[2], shapes[2]),
std::make_shared<Entity>(materials[0], shapes[3]),
std::make_shared<Entity>(materials[1], shapes[4]),
std::make_shared<Entity>(materials[2], shapes[5]),
std::make_shared<Entity>(materials[0], shapes[6]),
};
for (int i = 0; i < entities.size(); ++i)
{
entities[i]->GetTransform()->SetPosition((-(int)(entities.size() / 2) + i) * 5, 0, 0);
}
}
@ -241,23 +167,7 @@ void Game::Update(float deltaTime, float totalTime)
for (int i = 0; i < entities.size(); ++i)
{
entities[i]->GetTransform()->SetScale(0.2f * (i + 1), 0.2f * (i + 1), 0.2f * (i + 1));
entities[i]->GetTransform()->SetRotation(0.1f * (i + 1) * sin(totalTime), 0.1f * (i + 1) * sin(totalTime), 0.1f * (i + 1) * sin(totalTime));
// this range uses shapes[0] for testing
if (i < 3)
{
entities[i]->GetTransform()->SetPosition(tan((double)totalTime * ((double)i + (double)1)) * 0.1f, sin(totalTime) * 0.1f, (double)i * 0.1f);
}
// this range uses shapes[1] for testing
else if (i < 6)
{
entities[i]->GetTransform()->SetPosition(sin((double)totalTime * ((double)i + (double)1)) * 0.1f, cos(totalTime) * 0.1f, (double)i * 0.1f);
}
// this range uses shapes[2] for testing
else
{
entities[i]->GetTransform()->SetPosition(sin((double)totalTime * ((double)i + (double)1)) * cos(totalTime) * 0.1f, 0, (double)i * 0.1f);
}
entities[i]->GetTransform()->SetRotation(1.0f * (i + 1) * sin(totalTime), 1.0f * (i + 1) * sin(totalTime), 1.0f * (i + 1) * sin(totalTime));
}
}
@ -281,40 +191,19 @@ void Game::Draw(float deltaTime, float totalTime)
for (auto entity : entities)
{
// create constant buffer
VertexShaderExternalData vsData;
vsData.colorTint = XMFLOAT4(1.0f, 0.5f, 0.5f, 1.0f);
vsData.world = entity->GetTransform()->GetWorldMatrix();
vsData.view = camera->GetViewMatrix();
vsData.projection = camera->GetProjectionMatrix();
std::shared_ptr<SimpleVertexShader> vs = entity->GetMaterial()->GetVertexShader();
vs->SetMatrix4x4("world", entity->GetTransform()->GetWorldMatrix());
vs->SetMatrix4x4("view", camera->GetViewMatrix());
vs->SetMatrix4x4("projection", camera->GetProjectionMatrix());
vs->CopyAllBufferData();
// copy constant buffer to resource
D3D11_MAPPED_SUBRESOURCE mappedBuffer = {};
context->Map(constantBufferVS.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedBuffer);
memcpy(mappedBuffer.pData, &vsData, sizeof(vsData));
context->Unmap(constantBufferVS.Get(), 0);
// bind constant buffer
context->VSSetConstantBuffers(
0, // which slot (register) to bind buffer to?
1, // how many are we activating? can do multiple at once?
constantBufferVS.GetAddressOf() // Array of buffers (or address of one)
);
// Set the vertex and pixel shaders to use for the next Draw() command
// - These don't technically need to be set every frame
// - Once you start applying different shaders to different objects,
// you'll need to swap the current shaders before each draw
context->VSSetShader(vertexShader.Get(), 0, 0);
context->PSSetShader(pixelShader.Get(), 0, 0);
// Ensure the pipeline knows how to interpret the data (numbers)
// from the vertex buffer.
// - If all of your 3D models use the exact same vertex layout,
// this could simply be done once in Init()
// - However, this isn't always the case (but might be for this course)
context->IASetInputLayout(inputLayout.Get());
std::shared_ptr<SimplePixelShader> ps = entity->GetMaterial()->GetPixelShader();
ps->SetFloat4("tint", entity->GetMaterial()->GetTint());
ps->SetFloat("noise", 2.5f + cos(totalTime));
ps->CopyAllBufferData();
entity->GetMaterial()->GetVertexShader()->SetShader();
entity->GetMaterial()->GetPixelShader()->SetShader();
entity->GetMesh()->Draw();
}

13
Game.h
View file

@ -4,6 +4,8 @@
#include "Camera.h"
#include "Mesh.h"
#include "Entity.h"
#include "SimpleShader.h"
#include "Material.h"
#include <DirectXMath.h>
#include <wrl/client.h> // Used for ComPtr - a smart pointer for COM objects
#include <memory>
@ -39,16 +41,17 @@ private:
// - More info here: https://github.com/Microsoft/DirectXTK/wiki/ComPtr
// Shaders and shader-related constructs
Microsoft::WRL::ComPtr<ID3D11PixelShader> pixelShader;
Microsoft::WRL::ComPtr<ID3D11VertexShader> vertexShader;
Microsoft::WRL::ComPtr<ID3D11InputLayout> inputLayout;
std::shared_ptr<SimplePixelShader> pixelShader;
std::shared_ptr<SimpleVertexShader> vertexShader;
// Temporary A2 shapes
// A2 shapes
std::vector<std::shared_ptr<Mesh>> shapes;
// Temporary A4 entities;
// A4 entities;
std::vector<std::shared_ptr<Entity>> entities;
// A5 Camera
std::shared_ptr<Camera> camera;
// A6 Materials
std::vector<std::shared_ptr<Material>> materials;
Microsoft::WRL::ComPtr<ID3D11Buffer> constantBufferVS;
};

42
Material.cpp Normal file
View file

@ -0,0 +1,42 @@
#include "Material.h"
Material::Material(DirectX::XMFLOAT4 _tint, std::shared_ptr<SimpleVertexShader> _vertexShader, std::shared_ptr<SimplePixelShader> _pixelShader)
{
tint = _tint;
vertexShader = _vertexShader;
pixelShader = _pixelShader;
}
Material::~Material()
{
}
DirectX::XMFLOAT4 Material::GetTint()
{
return tint;
}
std::shared_ptr<SimpleVertexShader> Material::GetVertexShader()
{
return vertexShader;
}
std::shared_ptr<SimplePixelShader> Material::GetPixelShader()
{
return pixelShader;
}
void Material::SetTint(DirectX::XMFLOAT4 _tint)
{
tint = _tint;
}
void Material::SetVertexShader(std::shared_ptr<SimpleVertexShader> _vertexShader)
{
vertexShader = _vertexShader;
}
void Material::SetPixelShader(std::shared_ptr<SimplePixelShader> _pixelShader)
{
pixelShader = _pixelShader;
}

28
Material.h Normal file
View file

@ -0,0 +1,28 @@
#pragma once
#include <DirectXMath.h>
#include <memory>
#include "SimpleShader.h"
class Material
{
public:
Material(
DirectX::XMFLOAT4 _tint,
std::shared_ptr<SimpleVertexShader> _vertexShader,
std::shared_ptr<SimplePixelShader> _pixelShader);
~Material();
DirectX::XMFLOAT4 GetTint();
std::shared_ptr<SimpleVertexShader> GetVertexShader();
std::shared_ptr<SimplePixelShader> GetPixelShader();
void SetTint(DirectX::XMFLOAT4 _tint);
void SetVertexShader(std::shared_ptr<SimpleVertexShader> _vertexShader);
void SetPixelShader(std::shared_ptr<SimplePixelShader> _pixelShader);
private:
DirectX::XMFLOAT4 tint;
std::shared_ptr<SimpleVertexShader> vertexShader;
std::shared_ptr<SimplePixelShader> pixelShader;
};

231
Mesh.cpp
View file

@ -1,8 +1,239 @@
#include "Mesh.h"
#include <fstream>
#include <vector>
using namespace DirectX;
Mesh::Mesh(Vertex* _vertices, int _vertexCount, unsigned int* _indices, int _indexCount, Microsoft::WRL::ComPtr<ID3D11Device> _device, Microsoft::WRL::ComPtr<ID3D11DeviceContext> _context)
{
CreateMesh(_vertices, _vertexCount, _indices, _indexCount, _device, _context);
}
Mesh::Mesh(const char* _file, Microsoft::WRL::ComPtr<ID3D11Device> _device, Microsoft::WRL::ComPtr<ID3D11DeviceContext> _context)
{
/// BEGIN OBJLOADER ///
// Author: Chris Cascioli
// Purpose: Basic .OBJ 3D model loading, supporting positions, uvs and normals
//
// - You are allowed to directly copy/paste this into your code base
// for assignments, given that you clearly cite that this is not
// code of your own design.
//
// - NOTE: You'll need to #include <fstream>
// File input object
std::ifstream obj(_file);
// Check for successful open
if (!obj.is_open())
return;
// Variables used while reading the file
std::vector<XMFLOAT3> positions; // Positions from the file
std::vector<XMFLOAT3> normals; // Normals from the file
std::vector<XMFLOAT2> uvs; // UVs from the file
std::vector<Vertex> verts; // Verts we're assembling
std::vector<UINT> indices; // Indices of these verts
int vertCounter = 0; // Count of vertices
int indexCounter = 0; // Count of indices
char chars[100]; // String for line reading
// Still have data left?
while (obj.good())
{
// Get the line (100 characters should be more than enough)
obj.getline(chars, 100);
// Check the type of line
if (chars[0] == 'v' && chars[1] == 'n')
{
// Read the 3 numbers directly into an XMFLOAT3
XMFLOAT3 norm;
sscanf_s(
chars,
"vn %f %f %f",
&norm.x, &norm.y, &norm.z);
// Add to the list of normals
normals.push_back(norm);
}
else if (chars[0] == 'v' && chars[1] == 't')
{
// Read the 2 numbers directly into an XMFLOAT2
XMFLOAT2 uv;
sscanf_s(
chars,
"vt %f %f",
&uv.x, &uv.y);
// Add to the list of uv's
uvs.push_back(uv);
}
else if (chars[0] == 'v')
{
// Read the 3 numbers directly into an XMFLOAT3
XMFLOAT3 pos;
sscanf_s(
chars,
"v %f %f %f",
&pos.x, &pos.y, &pos.z);
// Add to the positions
positions.push_back(pos);
}
else if (chars[0] == 'f')
{
// Read the face indices into an array
// NOTE: This assumes the given obj file contains
// vertex positions, uv coordinates AND normals.
unsigned int i[12];
int numbersRead = sscanf_s(
chars,
"f %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d",
&i[0], &i[1], &i[2],
&i[3], &i[4], &i[5],
&i[6], &i[7], &i[8],
&i[9], &i[10], &i[11]);
// If we only got the first number, chances are the OBJ
// file has no UV coordinates. This isn't great, but we
// still want to load the model without crashing, so we
// need to re-read a different pattern (in which we assume
// there are no UVs denoted for any of the vertices)
if (numbersRead == 1)
{
// Re-read with a different pattern
numbersRead = sscanf_s(
chars,
"f %d//%d %d//%d %d//%d %d//%d",
&i[0], &i[2],
&i[3], &i[5],
&i[6], &i[8],
&i[9], &i[11]);
// The following indices are where the UVs should
// have been, so give them a valid value
i[1] = 1;
i[4] = 1;
i[7] = 1;
i[10] = 1;
// If we have no UVs, create a single UV coordinate
// that will be used for all vertices
if (uvs.size() == 0)
uvs.push_back(XMFLOAT2(0, 0));
}
// - Create the verts by looking up
// corresponding data from vectors
// - OBJ File indices are 1-based, so
// they need to be adusted
Vertex v1;
v1.Position = positions[i[0] - 1];
v1.UV = uvs[i[1] - 1];
v1.Normal = normals[i[2] - 1];
Vertex v2;
v2.Position = positions[i[3] - 1];
v2.UV = uvs[i[4] - 1];
v2.Normal = normals[i[5] - 1];
Vertex v3;
v3.Position = positions[i[6] - 1];
v3.UV = uvs[i[7] - 1];
v3.Normal = normals[i[8] - 1];
// The model is most likely in a right-handed space,
// especially if it came from Maya. We want to convert
// to a left-handed space for DirectX. This means we
// need to:
// - Invert the Z position
// - Invert the normal's Z
// - Flip the winding order
// We also need to flip the UV coordinate since DirectX
// defines (0,0) as the top left of the texture, and many
// 3D modeling packages use the bottom left as (0,0)
// Flip the UV's since they're probably "upside down"
v1.UV.y = 1.0f - v1.UV.y;
v2.UV.y = 1.0f - v2.UV.y;
v3.UV.y = 1.0f - v3.UV.y;
// Flip Z (LH vs. RH)
v1.Position.z *= -1.0f;
v2.Position.z *= -1.0f;
v3.Position.z *= -1.0f;
// Flip normal's Z
v1.Normal.z *= -1.0f;
v2.Normal.z *= -1.0f;
v3.Normal.z *= -1.0f;
// Add the verts to the vector (flipping the winding order)
verts.push_back(v1);
verts.push_back(v3);
verts.push_back(v2);
vertCounter += 3;
// Add three more indices
indices.push_back(indexCounter); indexCounter += 1;
indices.push_back(indexCounter); indexCounter += 1;
indices.push_back(indexCounter); indexCounter += 1;
// Was there a 4th face?
// - 12 numbers read means 4 faces WITH uv's
// - 8 numbers read means 4 faces WITHOUT uv's
if (numbersRead == 12 || numbersRead == 8)
{
// Make the last vertex
Vertex v4;
v4.Position = positions[i[9] - 1];
v4.UV = uvs[i[10] - 1];
v4.Normal = normals[i[11] - 1];
// Flip the UV, Z pos and normal's Z
v4.UV.y = 1.0f - v4.UV.y;
v4.Position.z *= -1.0f;
v4.Normal.z *= -1.0f;
// Add a whole triangle (flipping the winding order)
verts.push_back(v1);
verts.push_back(v4);
verts.push_back(v3);
vertCounter += 3;
// Add three more indices
indices.push_back(indexCounter); indexCounter += 1;
indices.push_back(indexCounter); indexCounter += 1;
indices.push_back(indexCounter); indexCounter += 1;
}
}
}
// Close the file and create the actual buffers
obj.close();
// - At this point, "verts" is a vector of Vertex structs, and can be used
// directly to create a vertex buffer: &verts[0] is the address of the first vert
//
// - The vector "indices" is similar. It's a vector of unsigned ints and
// can be used directly for the index buffer: &indices[0] is the address of the first int
//
// - "vertCounter" is the number of vertices
// - "indexCounter" is the number of indices
// - Yes, these are effectively the same since OBJs do not index entire vertices! This means
// an index buffer isn't doing much for us. We could try to optimize the mesh ourselves
// and detect duplicate vertices, but at that point it would be better to use a more
// sophisticated model loading library like TinyOBJLoader or AssImp (yes, that's its name)
/// END OBJLOADER ///
CreateMesh(&verts[0], verts.size(), &indices[0], indices.size(), _device, _context);
}
void Mesh::CreateMesh(Vertex* _vertices, int _vertexCount, unsigned int* _indices, int _indexCount, Microsoft::WRL::ComPtr<ID3D11Device> _device, Microsoft::WRL::ComPtr<ID3D11DeviceContext> _context)
{
// Create the VERTEX BUFFER description
D3D11_BUFFER_DESC vbd = {};

23
Mesh.h
View file

@ -6,11 +6,6 @@
class Mesh
{
private:
Microsoft::WRL::ComPtr<ID3D11Buffer> bufferVertex;
Microsoft::WRL::ComPtr<ID3D11Buffer> bufferIndex;
Microsoft::WRL::ComPtr<ID3D11DeviceContext> deviceContext;
int countIndex;
public:
Mesh(
Vertex* _vertices,
@ -19,10 +14,28 @@ public:
int _indexCount,
Microsoft::WRL::ComPtr<ID3D11Device> _device,
Microsoft::WRL::ComPtr<ID3D11DeviceContext> _context);
Mesh(
const char* _file,
Microsoft::WRL::ComPtr<ID3D11Device> _device,
Microsoft::WRL::ComPtr<ID3D11DeviceContext> _context);
~Mesh();
void Draw();
Microsoft::WRL::ComPtr<ID3D11Buffer>* GetVertexBuffer();
Microsoft::WRL::ComPtr<ID3D11Buffer>* GetIndexBuffer();
int GetIndexCount();
private:
Microsoft::WRL::ComPtr<ID3D11Buffer> bufferVertex;
Microsoft::WRL::ComPtr<ID3D11Buffer> bufferIndex;
Microsoft::WRL::ComPtr<ID3D11DeviceContext> deviceContext;
int countIndex;
void CreateMesh(
Vertex* _vertices,
int _vertexCount,
unsigned int* _indices,
int _indexCount,
Microsoft::WRL::ComPtr<ID3D11Device> _device,
Microsoft::WRL::ComPtr<ID3D11DeviceContext> _context);
};

View file

@ -1,3 +1,7 @@
cbuffer ExternalData : register(b0)
{
float4 tint;
}
// Struct representing the data we expect to receive from earlier pipeline stages
// - Should match the output of our corresponding vertex shader
@ -12,7 +16,7 @@ struct VertexToPixel
// | | |
// v v v
float4 screenPosition : SV_POSITION;
float4 color : COLOR;
float2 uv : TEXCOORD;
};
// --------------------------------------------------------
@ -26,9 +30,9 @@ struct VertexToPixel
// --------------------------------------------------------
float4 main(VertexToPixel input) : SV_TARGET
{
// Just return the input color
// Just return the tint
// - This color (like most values passing through the rasterizer) is
// interpolated for each pixel between the corresponding vertices
// of the triangle we're rendering
return input.color;
return tint;
}

130
RandomPixelShader.hlsl Normal file
View file

@ -0,0 +1,130 @@
/// BEGIN THIRD PARTY ///
/// SOURCE: https://thebookofshaders.com/edit.php#11/2d-snoise-clear.frag ///
// Some useful functions
float3 mod289(float3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
float2 mod289(float2 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
float3 permute(float3 x) { return mod289(((x * 34.0) + 1.0) * x); }
//
// Description : GLSL 2D simplex noise function
// Author : Ian McEwan, Ashima Arts
// Maintainer : ijm
// Lastmod : 20110822 (ijm)
// License :
// Copyright (C) 2011 Ashima Arts. All rights reserved.
// Distributed under the MIT License. See LICENSE file.
// https://github.com/ashima/webgl-noise
//
float snoise(float2 v)
{
// Precompute values for skewed triangular grid
const float4 C = float4(0.211324865405187,
// (3.0-sqrt(3.0))/6.0
0.366025403784439,
// 0.5*(sqrt(3.0)-1.0)
-0.577350269189626,
// -1.0 + 2.0 * C.x
0.024390243902439);
// 1.0 / 41.0
// First corner (x0)
float2 i = floor(v + dot(v, C.yy));
float2 x0 = v - i + dot(i, C.xx);
// Other two corners (x1, x2)
float2 i1 = float2(0, 0);
i1 = (x0.x > x0.y) ? float2(1.0, 0.0) : float2(0.0, 1.0);
float2 x1 = x0.xy + C.xx - i1;
float2 x2 = x0.xy + C.zz;
// Do some permutations to avoid
// truncation effects in permutation
i = mod289(i);
float3 p = permute(
permute(i.y + float3(0.0, i1.y, 1.0))
+ i.x + float3(0.0, i1.x, 1.0));
float3 m = max(0.5 - float3(
dot(x0, x0),
dot(x1, x1),
dot(x2, x2)
), 0.0);
m = m * m;
m = m * m;
// Gradients:
// 41 pts uniformly over a line, mapped onto a diamond
// The ring size 17*17 = 289 is close to a multiple
// of 41 (41*7 = 287)
float3 x = 2.0 * frac(p * C.www) - 1.0;
float3 h = abs(x) - 0.5;
float3 ox = floor(x + 0.5);
float3 a0 = x - ox;
// Normalise gradients implicitly by scaling m
// Approximation of: m *= inversesqrt(a0*a0 + h*h);
m *= 1.79284291400159 - 0.85373472095314 * (a0 * a0 + h * h);
// Compute final noise value at P
float3 g = float3(0, 0, 0);
g.x = a0.x * x0.x + h.x * x0.y;
g.yz = a0.yz * float2(x1.x, x2.x) + h.yz * float2(x1.y, x2.y);
return 130.0 * dot(m, g);
}
/// SOURCE: https://thebookofshaders.com/10/ ///
float random(float2 st)
{
return frac(sin(dot(st.xy,
float2(12.9898, 78.233))) *
43758.5453123);
}
/// END THIRD PARTY ///
cbuffer ExternalData : register(b0)
{
float4 tint;
float noise;
}
// Struct representing the data we expect to receive from earlier pipeline stages
// - Should match the output of our corresponding vertex shader
// - The name of the struct itself is unimportant
// - The variable names don't have to match other shaders (just the semantics)
// - Each variable must have a semantic, which defines its usage
struct VertexToPixel
{
// Data type
// |
// | Name Semantic
// | | |
// v v v
float4 screenPosition : SV_POSITION;
float2 uv : TEXCOORD;
};
// --------------------------------------------------------
// The entry point (main method) for our pixel shader
//
// - Input is the data coming down the pipeline (defined by the struct)
// - Output is a single color (float4)
// - Has a special semantic (SV_TARGET), which means
// "put the output of this into the current render target"
// - Named "main" because that's the default the shader compiler looks for
// --------------------------------------------------------
float4 main(VertexToPixel input) : SV_TARGET
{
// Just return the tint
// - This color (like most values passing through the rasterizer) is
// interpolated for each pixel between the corresponding vertices
// of the triangle we're rendering
return float4(
sin(snoise(input.uv * noise) + tint.r - random(input.uv)) + (tint.r * 0.5),
sin(snoise(input.uv * noise) + tint.g - random(input.uv)) + (tint.g * 0.5),
sin(snoise(input.uv * noise) + tint.b - random(input.uv)) + (tint.b * 0.5),
cos(random(input.screenPosition)));
}

1973
SimpleShader.cpp Normal file

File diff suppressed because it is too large Load diff

326
SimpleShader.h Normal file
View file

@ -0,0 +1,326 @@
// CREATOR: https://github.com/vixorien/SimpleShader
// LICENSE: MIT
#pragma once
#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "d3dcompiler.lib")
#include <d3d11.h>
#include <d3dcompiler.h>
#include <DirectXMath.h>
#include <wrl/client.h>
#include <unordered_map>
#include <vector>
#include <string>
// --------------------------------------------------------
// Used by simple shaders to store information about
// specific variables in constant buffers
// --------------------------------------------------------
struct SimpleShaderVariable
{
unsigned int ByteOffset;
unsigned int Size;
unsigned int ConstantBufferIndex;
};
// --------------------------------------------------------
// Contains information about a specific
// constant buffer in a shader, as well as
// the local data buffer for it
// --------------------------------------------------------
struct SimpleConstantBuffer
{
std::string Name;
D3D_CBUFFER_TYPE Type = D3D_CBUFFER_TYPE::D3D11_CT_CBUFFER;
unsigned int Size = 0;
unsigned int BindIndex = 0;
Microsoft::WRL::ComPtr<ID3D11Buffer> ConstantBuffer = 0;
unsigned char* LocalDataBuffer = 0;
std::vector<SimpleShaderVariable> Variables;
};
// --------------------------------------------------------
// Contains info about a single SRV in a shader
// --------------------------------------------------------
struct SimpleSRV
{
unsigned int Index; // The raw index of the SRV
unsigned int BindIndex; // The register of the SRV
};
// --------------------------------------------------------
// Contains info about a single Sampler in a shader
// --------------------------------------------------------
struct SimpleSampler
{
unsigned int Index; // The raw index of the Sampler
unsigned int BindIndex; // The register of the Sampler
};
// --------------------------------------------------------
// Base abstract class for simplifying shader handling
// --------------------------------------------------------
class ISimpleShader
{
public:
ISimpleShader(Microsoft::WRL::ComPtr<ID3D11Device> device, Microsoft::WRL::ComPtr<ID3D11DeviceContext> context);
virtual ~ISimpleShader();
// Simple helpers
bool IsShaderValid() { return shaderValid; }
// Activating the shader and copying data
void SetShader();
void CopyAllBufferData();
void CopyBufferData(unsigned int index);
void CopyBufferData(std::string bufferName);
// Sets arbitrary shader data
bool SetData(std::string name, const void* data, unsigned int size);
bool SetInt(std::string name, int data);
bool SetFloat(std::string name, float data);
bool SetFloat2(std::string name, const float data[2]);
bool SetFloat2(std::string name, const DirectX::XMFLOAT2 data);
bool SetFloat3(std::string name, const float data[3]);
bool SetFloat3(std::string name, const DirectX::XMFLOAT3 data);
bool SetFloat4(std::string name, const float data[4]);
bool SetFloat4(std::string name, const DirectX::XMFLOAT4 data);
bool SetMatrix4x4(std::string name, const float data[16]);
bool SetMatrix4x4(std::string name, const DirectX::XMFLOAT4X4 data);
// Setting shader resources
virtual bool SetShaderResourceView(std::string name, Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> srv) = 0;
virtual bool SetSamplerState(std::string name, Microsoft::WRL::ComPtr<ID3D11SamplerState> samplerState) = 0;
// Simple resource checking
bool HasVariable(std::string name);
bool HasShaderResourceView(std::string name);
bool HasSamplerState(std::string name);
// Getting data about variables and resources
const SimpleShaderVariable* GetVariableInfo(std::string name);
const SimpleSRV* GetShaderResourceViewInfo(std::string name);
const SimpleSRV* GetShaderResourceViewInfo(unsigned int index);
size_t GetShaderResourceViewCount() { return textureTable.size(); }
const SimpleSampler* GetSamplerInfo(std::string name);
const SimpleSampler* GetSamplerInfo(unsigned int index);
size_t GetSamplerCount() { return samplerTable.size(); }
// Get data about constant buffers
unsigned int GetBufferCount();
unsigned int GetBufferSize(unsigned int index);
const SimpleConstantBuffer* GetBufferInfo(std::string name);
const SimpleConstantBuffer* GetBufferInfo(unsigned int index);
// Misc getters
Microsoft::WRL::ComPtr<ID3DBlob> GetShaderBlob() { return shaderBlob; }
// Error reporting
static bool ReportErrors;
static bool ReportWarnings;
protected:
bool shaderValid;
Microsoft::WRL::ComPtr<ID3DBlob> shaderBlob;
Microsoft::WRL::ComPtr<ID3D11Device> device;
Microsoft::WRL::ComPtr<ID3D11DeviceContext> deviceContext;
// Resource counts
unsigned int constantBufferCount;
// Maps for variables and buffers
SimpleConstantBuffer* constantBuffers; // For index-based lookup
std::vector<SimpleSRV*> shaderResourceViews;
std::vector<SimpleSampler*> samplerStates;
std::unordered_map<std::string, SimpleConstantBuffer*> cbTable;
std::unordered_map<std::string, SimpleShaderVariable> varTable;
std::unordered_map<std::string, SimpleSRV*> textureTable;
std::unordered_map<std::string, SimpleSampler*> samplerTable;
// Initialization method
bool LoadShaderFile(LPCWSTR shaderFile);
// Pure virtual functions for dealing with shader types
virtual bool CreateShader(Microsoft::WRL::ComPtr<ID3DBlob> shaderBlob) = 0;
virtual void SetShaderAndCBs() = 0;
virtual void CleanUp();
// Helpers for finding data by name
SimpleShaderVariable* FindVariable(std::string name, int size);
SimpleConstantBuffer* FindConstantBuffer(std::string name);
// Error logging
void Log(std::string message, WORD color);
void LogW(std::wstring message, WORD color);
void Log(std::string message);
void LogW(std::wstring message);
void LogError(std::string message);
void LogErrorW(std::wstring message);
void LogWarning(std::string message);
void LogWarningW(std::wstring message);
};
// --------------------------------------------------------
// Derived class for VERTEX shaders ///////////////////////
// --------------------------------------------------------
class SimpleVertexShader : public ISimpleShader
{
public:
SimpleVertexShader(Microsoft::WRL::ComPtr<ID3D11Device> device, Microsoft::WRL::ComPtr<ID3D11DeviceContext> context, LPCWSTR shaderFile);
SimpleVertexShader(Microsoft::WRL::ComPtr<ID3D11Device> device, Microsoft::WRL::ComPtr<ID3D11DeviceContext> context, LPCWSTR shaderFile, Microsoft::WRL::ComPtr<ID3D11InputLayout> inputLayout, bool perInstanceCompatible);
~SimpleVertexShader();
Microsoft::WRL::ComPtr<ID3D11VertexShader> GetDirectXShader() { return shader; }
Microsoft::WRL::ComPtr<ID3D11InputLayout> GetInputLayout() { return inputLayout; }
bool GetPerInstanceCompatible() { return perInstanceCompatible; }
bool SetShaderResourceView(std::string name, Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> srv);
bool SetSamplerState(std::string name, Microsoft::WRL::ComPtr<ID3D11SamplerState> samplerState);
protected:
bool perInstanceCompatible;
Microsoft::WRL::ComPtr<ID3D11InputLayout> inputLayout;
Microsoft::WRL::ComPtr<ID3D11VertexShader> shader;
bool CreateShader(Microsoft::WRL::ComPtr<ID3DBlob> shaderBlob);
void SetShaderAndCBs();
void CleanUp();
};
// --------------------------------------------------------
// Derived class for PIXEL shaders ////////////////////////
// --------------------------------------------------------
class SimplePixelShader : public ISimpleShader
{
public:
SimplePixelShader(Microsoft::WRL::ComPtr<ID3D11Device> device, Microsoft::WRL::ComPtr<ID3D11DeviceContext> context, LPCWSTR shaderFile);
~SimplePixelShader();
Microsoft::WRL::ComPtr<ID3D11PixelShader> GetDirectXShader() { return shader; }
bool SetShaderResourceView(std::string name, Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> srv);
bool SetSamplerState(std::string name, Microsoft::WRL::ComPtr<ID3D11SamplerState> samplerState);
protected:
Microsoft::WRL::ComPtr<ID3D11PixelShader> shader;
bool CreateShader(Microsoft::WRL::ComPtr<ID3DBlob> shaderBlob);
void SetShaderAndCBs();
void CleanUp();
};
// --------------------------------------------------------
// Derived class for DOMAIN shaders ///////////////////////
// --------------------------------------------------------
class SimpleDomainShader : public ISimpleShader
{
public:
SimpleDomainShader(Microsoft::WRL::ComPtr<ID3D11Device> device, Microsoft::WRL::ComPtr<ID3D11DeviceContext> context, LPCWSTR shaderFile);
~SimpleDomainShader();
Microsoft::WRL::ComPtr<ID3D11DomainShader> GetDirectXShader() { return shader; }
bool SetShaderResourceView(std::string name, Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> srv);
bool SetSamplerState(std::string name, Microsoft::WRL::ComPtr<ID3D11SamplerState> samplerState);
protected:
Microsoft::WRL::ComPtr<ID3D11DomainShader> shader;
bool CreateShader(Microsoft::WRL::ComPtr<ID3DBlob> shaderBlob);
void SetShaderAndCBs();
void CleanUp();
};
// --------------------------------------------------------
// Derived class for HULL shaders /////////////////////////
// --------------------------------------------------------
class SimpleHullShader : public ISimpleShader
{
public:
SimpleHullShader(Microsoft::WRL::ComPtr<ID3D11Device> device, Microsoft::WRL::ComPtr<ID3D11DeviceContext> context, LPCWSTR shaderFile);
~SimpleHullShader();
Microsoft::WRL::ComPtr<ID3D11HullShader> GetDirectXShader() { return shader; }
bool SetShaderResourceView(std::string name, Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> srv);
bool SetSamplerState(std::string name, Microsoft::WRL::ComPtr<ID3D11SamplerState> samplerState);
protected:
Microsoft::WRL::ComPtr<ID3D11HullShader> shader;
bool CreateShader(Microsoft::WRL::ComPtr<ID3DBlob> shaderBlob);
void SetShaderAndCBs();
void CleanUp();
};
// --------------------------------------------------------
// Derived class for GEOMETRY shaders /////////////////////
// --------------------------------------------------------
class SimpleGeometryShader : public ISimpleShader
{
public:
SimpleGeometryShader(Microsoft::WRL::ComPtr<ID3D11Device> device, Microsoft::WRL::ComPtr<ID3D11DeviceContext> context, LPCWSTR shaderFile, bool useStreamOut = 0, bool allowStreamOutRasterization = 0);
~SimpleGeometryShader();
Microsoft::WRL::ComPtr<ID3D11GeometryShader> GetDirectXShader() { return shader; }
bool SetShaderResourceView(std::string name, Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> srv);
bool SetSamplerState(std::string name, Microsoft::WRL::ComPtr<ID3D11SamplerState> samplerState);
bool CreateCompatibleStreamOutBuffer(Microsoft::WRL::ComPtr<ID3D11Buffer> buffer, int vertexCount);
static void UnbindStreamOutStage(Microsoft::WRL::ComPtr<ID3D11DeviceContext> deviceContext);
protected:
// Shader itself
Microsoft::WRL::ComPtr<ID3D11GeometryShader> shader;
// Stream out related
bool useStreamOut;
bool allowStreamOutRasterization;
unsigned int streamOutVertexSize;
bool CreateShader(Microsoft::WRL::ComPtr<ID3DBlob> shaderBlob);
bool CreateShaderWithStreamOut(Microsoft::WRL::ComPtr<ID3DBlob> shaderBlob);
void SetShaderAndCBs();
void CleanUp();
// Helpers
unsigned int CalcComponentCount(unsigned int mask);
};
// --------------------------------------------------------
// Derived class for COMPUTE shaders //////////////////////
// --------------------------------------------------------
class SimpleComputeShader : public ISimpleShader
{
public:
SimpleComputeShader(Microsoft::WRL::ComPtr<ID3D11Device> device, Microsoft::WRL::ComPtr<ID3D11DeviceContext> context, LPCWSTR shaderFile);
~SimpleComputeShader();
Microsoft::WRL::ComPtr<ID3D11ComputeShader> GetDirectXShader() { return shader; }
void DispatchByGroups(unsigned int groupsX, unsigned int groupsY, unsigned int groupsZ);
void DispatchByThreads(unsigned int threadsX, unsigned int threadsY, unsigned int threadsZ);
bool HasUnorderedAccessView(std::string name);
bool SetShaderResourceView(std::string name, Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> srv);
bool SetSamplerState(std::string name, Microsoft::WRL::ComPtr<ID3D11SamplerState> samplerState);
bool SetUnorderedAccessView(std::string name, Microsoft::WRL::ComPtr<ID3D11UnorderedAccessView> uav, unsigned int appendConsumeOffset = -1);
int GetUnorderedAccessViewIndex(std::string name);
protected:
Microsoft::WRL::ComPtr<ID3D11ComputeShader> shader;
std::unordered_map<std::string, unsigned int> uavTable;
unsigned int threadsX;
unsigned int threadsY;
unsigned int threadsZ;
unsigned int threadsTotal;
bool CreateShader(Microsoft::WRL::ComPtr<ID3DBlob> shaderBlob);
void SetShaderAndCBs();
void CleanUp();
};

View file

@ -9,6 +9,7 @@
// --------------------------------------------------------
struct Vertex
{
DirectX::XMFLOAT3 Position; // The local position of the vertex
DirectX::XMFLOAT4 Color; // The color of the vertex
DirectX::XMFLOAT3 Position;
DirectX::XMFLOAT3 Normal;
DirectX::XMFLOAT2 UV;
};

View file

@ -1,6 +1,5 @@
cbuffer ExternalData : register(b0)
{
float4 colorTint;
matrix world;
matrix view;
matrix projection;
@ -18,8 +17,9 @@ struct VertexShaderInput
// | Name Semantic
// | | |
// v v v
float3 localPosition : POSITION; // XYZ position
float4 color : COLOR; // RGBA color
float3 localPosition : POSITION;
float3 normal : NORMAL;
float2 uv : UV;
};
// Struct representing the data we're sending down the pipeline
@ -34,8 +34,8 @@ struct VertexToPixel
// | Name Semantic
// | | |
// v v v
float4 screenPosition : SV_POSITION; // XYZW position (System Value Position)
float4 color : COLOR; // RGBA color
float4 screenPosition : SV_POSITION;
float2 uv : TEXCOORD;
};
// --------------------------------------------------------
@ -63,10 +63,10 @@ VertexToPixel main( VertexShaderInput input )
// a perspective projection matrix, which we'll get to in the future).
output.screenPosition = mul(worldViewProjection, float4(input.localPosition, 1.0f));
// Pass the color through
// Pass the uvs through
// - The values will be interpolated per-pixel by the rasterizer
// - We don't need to alter it here, but we do need to send it to the pixel shader
output.color = input.color * colorTint;
output.uv = input.uv;
// Whatever we return will make its way through the pipeline to the
// next programmable stage we're using (the pixel shader for now)