-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.h
797 lines (728 loc) · 23 KB
/
renderer.h
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
// minimalistic code to draw a single triangle, this is not part of the API.
// required for compiling shaders on the fly, consider pre-compiling instead
#include <d3dcompiler.h>
#include "test_pyramid.h"
#include "dev4.h"
#include "red.h"
#include "skybox.h"
#include "yellow.h"
#include "green.h"
#include "blue.h"
#include "purple.h"
#include "DDSTextureLoader.h"
#include <numeric>
#include <functional>
using namespace std::placeholders;
#pragma comment(lib, "d3dcompiler.lib")
// Simple Vertex Shader
//BE SURE TO USE THE PRAGMA PACK MATRIX ON SHADERS!!!
const char* vertexShaderSource = R"(
#pragma pack_matrix(row_major)
cbuffer SHDR_VARS // constant buggers are 16byte aligned
{
matrix w, v, p;
float4 lightDir; //adding 1 float for padding
float4 lightColor;
float4 ambientColor;
float4 pointLightPos;
float4 pointLightColor;
float4 camPos;
float4 spotLightPos;
float4 spotLightDir;
float4 spotLightColor;
float4 innerConeRatio;
float4 outerConeRatio;
float4 unlit;
};
struct VOUT
{
float4 posH : SV_POSITION;
float3 uvw : TEXCOORD0;
float3 nrm : NORMAL;
float3 plp : TEXCOORD1;
float3 cam : TEXCOORD2;
float3 slp : TEXCOORD3;
};
// an ultra simple hlsl vertex shader
VOUT main(float3 posL : POSITION, float3 uvw : TEXCOORD0, float3 nrm : NORMAL)
{
VOUT output;
float4 vert = float4(posL, 1);
float4 worldPos = mul(vert,w);
vert = mul(vert, w);
vert = mul(vert, v);
vert = mul(vert, p);
output.posH = vert;
output.uvw = uvw;
output.nrm = mul(nrm, w);
output.plp = normalize(pointLightPos.xyz-worldPos.xyz);
output.slp = normalize(spotLightPos.xyz-worldPos.xyz);
output.cam = normalize(camPos.xyz - worldPos.xyz);
return output;
}
)";
// Simple Pixel Shader
const char* pixelShaderSource = R"(
// an ultra simple hlsl pixel shader
cbuffer SHDR_VARS // constant buggers are 16byte aligned
{
matrix w, v, p;
float4 lightDir; //adding 1 float for padding
float4 lightColor;
float4 ambientColor;
float4 pointLightPos;
float4 pointLightColor;
float4 camPos;
float4 spotLightPos;
float4 spotLightDir;
float4 spotLightColor;
float4 innerConeRatio;
float4 outerConeRatio;
float4 unlit;
};
Texture2D mytexture;
SamplerState mysampler;
struct VOUT
{
float4 posH : SV_POSITION;
float3 uvw : TEXCOORD0;
float3 nrm : NORMAL;
float3 plp : TEXCOORD1;
float3 cam : TEXCOORD2;
float3 slp : TEXCOORD3;
};
float4 main(VOUT input) : SV_TARGET
{
float4 diffuse = mytexture.Sample(mysampler, input.uvw.xy);
if(unlit.x == 0)
{
float4 light = float4(0,0,0,0);
//Point Light
float distance = length(input.plp.xyz);
distance *= distance;
light +=saturate(dot(input.nrm, input.plp.xyz)) * pointLightColor / distance;
//Spot Light
distance = length(input.slp.xyz);
distance *= distance;
float surfaceRatio = saturate(dot(-input.slp.xyz,spotLightDir.xyz));
float lightRatio = saturate(dot(input.slp.xyz,input.nrm.xyz));
float spotAtten = 1.0 - saturate((innerConeRatio.x - surfaceRatio)/(innerConeRatio.x - outerConeRatio.x));
spotAtten *= spotAtten;
light += lightRatio * spotLightColor * spotAtten * distance;
//Ambient Light
light += ambientColor;
//Directional Light
light += saturate(dot(-lightDir.xyz,input.nrm)) * lightColor;
//Phong
light += max(pow(saturate(dot(input.nrm, normalize(-input.plp.xyz + input.cam.xyz))), 30.0f),0);
light += max(pow(saturate(dot(input.nrm, normalize(-input.slp.xyz + input.cam.xyz))), 30.0f),0);
//Final
light *= diffuse;
//Final Saturation
light = saturate(light);
light.w = diffuse.w;
if(light.w < 0.05)
{
discard;
}
diffuse = light;
}
//Return
return diffuse;
}
)";
// Creation, Rendering & Cleanup
bool CompareData(int first, int second, float* indexData)
{
return indexData[first] < indexData[second];
}
class Renderer
{
//Variables
std::chrono::system_clock::time_point oldTime = std::chrono::system_clock::now();
double timeDeltaTime = 0;
double timeSinceStart = 0;
float fovSpeed = 0;
float fov = 75;
float nearFarSpeed = 0;
float nearPlane = 0.1f;
float farPlane = 1000.0f;
float playerVelX = 0;
float playerVelZ = 0;
float playerVelY = 0;
//Keyboard
struct InputKeyboard
{
bool up;
bool down;
bool left;
bool right;
bool space;
bool shift;
bool plus;
bool minus;
bool bracketR;
bool bracketL;
bool one;
float mouseX;
float mouseY;
bool e;
bool q;
};
InputKeyboard keys;
//Camera
GW::MATH::GMATRIXF viewWorldM;
GW::MATH::GMATRIXF viewLocalM;
//MESHES
struct MESH
{
unsigned index_count;
GW::MATH::GMATRIXF w;
Microsoft::WRL::ComPtr<ID3D11Buffer> vertexBuffer;
Microsoft::WRL::ComPtr<ID3D11Buffer> indexBuffer;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> texture;
};
MESH skybox;
MESH meshes[3];
MESH transMeshes[20];
float transMeshDistance[20];
int transMeshIndex[20];
//SETUP
GW::INPUT::GInput ginput;
// proxy handles
GW::SYSTEM::GWindow win;
GW::GRAPHICS::GDirectX11Surface d3d;
// what we need at a minimum to draw a triangle
Microsoft::WRL::ComPtr<ID3D11VertexShader> vertexShader;
Microsoft::WRL::ComPtr<ID3D11PixelShader> pixelShader;
Microsoft::WRL::ComPtr<ID3D11InputLayout> vertexFormat;
ID3D11BlendState* g_pBlendStateNoBlend;
// Shader Variables
Microsoft::WRL::ComPtr<ID3D11Buffer> constantBuffer;
struct SHDR_VARS
{
GW::MATH::GMATRIXF w, v, p;
GW::MATH::GVECTORF lightDir; //adding 1 float for padding
GW::MATH::GVECTORF lightColor;
GW::MATH::GVECTORF ambientColor;
GW::MATH::GVECTORF pointLightPos;
GW::MATH::GVECTORF pointLightColor;
GW::MATH::GVECTORF camPos;
GW::MATH::GVECTORF spotLightPos;
GW::MATH::GVECTORF spotLightDir;
GW::MATH::GVECTORF spotLightColor;
GW::MATH::GVECTORF innerConeRatio;
GW::MATH::GVECTORF outerConeRatio;
GW::MATH::GVECTORF unlit;
}svars;
//math lib
GW::MATH::GMatrix m;
public:
bool FillMesh(MESH& fill, const OBJ_VERT* verts, unsigned num_vert, const unsigned* indices, unsigned num_index, const wchar_t* tex_file)
{
ID3D11Device* creator;
d3d.GetDevice((void**)&creator);
D3D11_SUBRESOURCE_DATA bData = { verts , 0, 0 };
CD3D11_BUFFER_DESC bDesc(sizeof(OBJ_VERT) * num_vert, D3D11_BIND_VERTEX_BUFFER);
creator->CreateBuffer(&bDesc, &bData, fill.vertexBuffer.GetAddressOf());
//index buffer
D3D11_SUBRESOURCE_DATA iData = { indices , 0, 0 };
CD3D11_BUFFER_DESC iDesc(sizeof(unsigned) * num_index, D3D11_BIND_INDEX_BUFFER);
creator->CreateBuffer(&iDesc, &iData, fill.indexBuffer.GetAddressOf());
fill.index_count = num_index;
//Try to load texture from disk
HRESULT hr = CreateDDSTextureFromFile(creator, tex_file, nullptr, fill.texture.GetAddressOf());
fill.w = GW::MATH::GIdentityMatrixF;
creator->Release();
return true;
}
void DrawMesh(const MESH& draw)
{
ID3D11DeviceContext* con;
ID3D11DepthStencilView* depth;
d3d.GetImmediateContext((void**)&con);
d3d.GetDepthStencilView((void**)&depth);
const UINT strides[] = { sizeof(OBJ_VERT) };
const UINT offsets[] = { 0 };
ID3D11Buffer* const buffs[] = { draw.vertexBuffer.Get() };
con->IASetVertexBuffers(0, ARRAYSIZE(buffs), buffs, strides, offsets);
con->IASetIndexBuffer(draw.indexBuffer.Get(), DXGI_FORMAT_R32_UINT, 0);
//set out texture
ID3D11ShaderResourceView* const srvs[] = { draw.texture.Get() };
con->PSSetShaderResources(0, 1, srvs);
svars.w = draw.w; //make sure our world matrix is used at ttime of drawing
con->UpdateSubresource(constantBuffer.Get(), 0, nullptr, &svars, sizeof(SHDR_VARS), 0);
con->DrawIndexed(draw.index_count, 0, 0);
con->Release();
}
Renderer(GW::SYSTEM::GWindow _win, GW::GRAPHICS::GDirectX11Surface _d3d)
{
win = _win;
d3d = _d3d;
//Input
GW::GReturn g = ginput.Create(win);
ID3D11Device* creator;
d3d.GetDevice((void**)&creator);
// Create Vertex Shader
UINT compilerFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#if _DEBUG
compilerFlags |= D3DCOMPILE_DEBUG;
#endif
Microsoft::WRL::ComPtr<ID3DBlob> vsBlob, errors;
if (SUCCEEDED(D3DCompile(vertexShaderSource, strlen(vertexShaderSource),
nullptr, nullptr, nullptr, "main", "vs_4_0", compilerFlags, 0,
vsBlob.GetAddressOf(), errors.GetAddressOf())))
{
creator->CreateVertexShader(vsBlob->GetBufferPointer(),
vsBlob->GetBufferSize(), nullptr, vertexShader.GetAddressOf());
}
else
std::cout << (char*)errors->GetBufferPointer() << std::endl;
// Create Pixel Shader
Microsoft::WRL::ComPtr<ID3DBlob> psBlob; errors.Reset();
if (SUCCEEDED(D3DCompile(pixelShaderSource, strlen(pixelShaderSource),
nullptr, nullptr, nullptr, "main", "ps_4_0", compilerFlags, 0,
psBlob.GetAddressOf(), errors.GetAddressOf())))
{
creator->CreatePixelShader(psBlob->GetBufferPointer(),
psBlob->GetBufferSize(), nullptr, pixelShader.GetAddressOf());
}
else
std::cout << (char*)errors->GetBufferPointer() << std::endl;
// Create Input Layout
D3D11_INPUT_ELEMENT_DESC format[] = {
{
"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0,
D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0
},{
"TEXCOORD", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0,
D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0
},{
"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0,
D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0
}
};
creator->CreateInputLayout(format, ARRAYSIZE(format),
vsBlob->GetBufferPointer(), vsBlob->GetBufferSize(),
vertexFormat.GetAddressOf());
//setup matricies
m.Create();
svars.w = GW::MATH::GIdentityMatrixF;
viewWorldM = GW::MATH::GIdentityMatrixF;
viewWorldM.data[14] = 1;
float ar;
d3d.GetAspectRatio(ar);
m.ProjectionDirectXLHF(G_DEGREE_TO_RADIAN(fov), ar, 0.1f, 1000.0f, svars.p);
m.LookAtLHF(GW::MATH::GVECTORF{ 0, 1, 0 }, GW::MATH::GVECTORF{ 0, 1, 0 + 1 }, GW::MATH::GVECTORF{ 0,1,0 }, viewLocalM);
// init light data
svars.lightColor = GW::MATH::GVECTORF{ .3,.3,.8,1 };
svars.lightDir = GW::MATH::GVECTORF{ -1,-1,1,0 };
svars.pointLightColor = GW::MATH::GVECTORF{ 255.0 / 255.0 / 2.0, 179.0 / 255.0 / 2.0, 15.0 / 255.0 / 2.0 ,0 };
svars.pointLightPos = GW::MATH::GVECTORF{ 1,-3,1,1 };
svars.ambientColor = GW::MATH::GVECTORF{ 39.0 / 255.0, 18.0 / 255.0, 53.0 / 255.0,0 };
svars.spotLightColor = GW::MATH::GVECTORF{ .8, .8, .8, 0 };
svars.spotLightPos = GW::MATH::GVECTORF{ 4,-5,4,1 };
svars.spotLightDir = GW::MATH::GVECTORF{ 1,-1,1,0 };
svars.innerConeRatio.x = .2;
svars.outerConeRatio.x = .2;
GW::MATH::GVector::NormalizeF(svars.lightDir, svars.lightDir);
//Blend State
D3D11_BLEND_DESC blendDesc;
//Set Settings
blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
//Bool Settings
blendDesc.AlphaToCoverageEnable = false;
blendDesc.IndependentBlendEnable = false;
blendDesc.RenderTarget[0].BlendEnable = true;
//Create Blend State
creator->CreateBlendState(&blendDesc, &g_pBlendStateNoBlend);
//Constant buffer crearte
D3D11_SUBRESOURCE_DATA cData = { &svars, 0, 0 };
CD3D11_BUFFER_DESC cDesc(sizeof(SHDR_VARS), D3D11_BIND_CONSTANT_BUFFER);
creator->CreateBuffer(&cDesc, &cData, constantBuffer.GetAddressOf());
//Set Up Models
ModelSetUp();
// free temporary handle
creator->Release();
}
void Render()
{
//Input
SetKeyboardInput();
//Time
SetDeltaTime();
//Physics
Physics();
//Update
Update();
// grab the context & render target
ID3D11DeviceContext* con;
ID3D11RenderTargetView* view;
ID3D11DepthStencilView* depth;
d3d.GetImmediateContext((void**)&con);
d3d.GetRenderTargetView((void**)&view);
d3d.GetDepthStencilView((void**)&depth);
// setup the pipeline
ID3D11RenderTargetView* const views[] = { view };
con->OMSetRenderTargets(ARRAYSIZE(views), views, depth);
con->VSSetShader(vertexShader.Get(), nullptr, 0);
con->PSSetShader(pixelShader.Get(), nullptr, 0);
con->IASetInputLayout(vertexFormat.Get());
ID3D11Buffer* const cbuffs[] = { constantBuffer.Get() };
con->VSSetConstantBuffers(0, 1, cbuffs);
con->PSSetConstantBuffers(0, 1, cbuffs);
// now we can draw
con->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
//Update Resource
con->UpdateSubresource(constantBuffer.Get(), 0, nullptr, &svars, sizeof(SHDR_VARS), 0);
//Update Blend State
float bf[] = { 1.0f, 1.0f, 1.0f, 1.0f };
UINT sampleMask = 0xffffffff;
con->OMSetBlendState(g_pBlendStateNoBlend, bf, sampleMask);
//Draw Skybox
svars.unlit.x = 1;
GW::MATH::GMATRIXF tempV = svars.v;
svars.v.data[12] = 0;
svars.v.data[13] = 0;
svars.v.data[14] = 0;
DrawMesh(skybox);
con->ClearDepthStencilView(depth, D3D11_CLEAR_DEPTH, 1, 0);
svars.v = tempV;
svars.unlit.x = 0;
//Draw Mesh
for (size_t i = 0; i < sizeof(meshes) / sizeof(meshes[0]); i++)
{
DrawMesh(meshes[i]);
}
//Draw Transparent Mesh
GW::MATH::GMATRIXF temptemp;
m.InverseF(svars.v, temptemp);
for (size_t i = 0; i < sizeof(transMeshDistance) / sizeof(transMeshDistance[0]); i++)
{
transMeshDistance[i] = sqrt(pow((temptemp.data[12] - transMeshes[i].w.data[12]), 2) + pow((temptemp.data[13] - transMeshes[i].w.data[13]), 2) + pow((temptemp.data[14] - transMeshes[i].w.data[14]), 2));
}
std::iota(std::begin(transMeshIndex), std::end(transMeshIndex), 0);
std::sort(std::begin(transMeshIndex), std::end(transMeshIndex), std::bind(CompareData, _1, _2, transMeshDistance));
std::reverse(std::begin(transMeshIndex), std::end(transMeshIndex));
for (size_t i = 0; i < sizeof(transMeshDistance) / sizeof(transMeshDistance[0]); i++)
{
DrawMesh(transMeshes[transMeshIndex[i]]);
}
// release temp handles
con->OMSetBlendState(nullptr, 0,0);
depth->Release();
view->Release();
con->Release();
}
~Renderer()
{
// ComPtr will auto release so nothing to do here
}
void ModelSetUp()
{
FillMesh(skybox, skybox_data, skybox_vertexcount, skybox_indicies, skybox_indexcount, L"../Skybox.dds");
FillMesh(meshes[0], test_pyramid_data, test_pyramid_vertexcount, test_pyramid_indicies, test_pyramid_indexcount, L"../Rock.dds");
FillMesh(meshes[1], dev4_data, dev4_vertexcount, dev4_indicies, dev4_indexcount, L"../dev4.dds");
FillMesh(meshes[2], test_pyramid_data, test_pyramid_vertexcount, test_pyramid_indicies, test_pyramid_indexcount, L"../Rock.dds");
FillMesh(transMeshes[0], red_data, red_vertexcount, red_indicies, red_indexcount, L"../dev4.dds");
FillMesh(transMeshes[1], yellow_data, yellow_vertexcount, yellow_indicies, yellow_indexcount, L"../dev4.dds");
FillMesh(transMeshes[2], green_data, green_vertexcount, green_indicies, green_indexcount, L"../dev4.dds");
FillMesh(transMeshes[3], blue_data, blue_vertexcount, blue_indicies, blue_indexcount, L"../dev4.dds");
FillMesh(transMeshes[4], purple_data, purple_vertexcount, purple_indicies, purple_indexcount, L"../dev4.dds");
FillMesh(transMeshes[5], red_data, red_vertexcount, red_indicies, red_indexcount, L"../dev4.dds");
FillMesh(transMeshes[6], yellow_data, yellow_vertexcount, yellow_indicies, yellow_indexcount, L"../dev4.dds");
FillMesh(transMeshes[7], green_data, green_vertexcount, green_indicies, green_indexcount, L"../dev4.dds");
FillMesh(transMeshes[8], blue_data, blue_vertexcount, blue_indicies, blue_indexcount, L"../dev4.dds");
FillMesh(transMeshes[9], purple_data, purple_vertexcount, purple_indicies, purple_indexcount, L"../dev4.dds");
FillMesh(transMeshes[10], red_data, red_vertexcount, red_indicies, red_indexcount, L"../dev4.dds");
FillMesh(transMeshes[11], yellow_data, yellow_vertexcount, yellow_indicies, yellow_indexcount, L"../dev4.dds");
FillMesh(transMeshes[12], green_data, green_vertexcount, green_indicies, green_indexcount, L"../dev4.dds");
FillMesh(transMeshes[13], blue_data, blue_vertexcount, blue_indicies, blue_indexcount, L"../dev4.dds");
FillMesh(transMeshes[14], purple_data, purple_vertexcount, purple_indicies, purple_indexcount, L"../dev4.dds");
FillMesh(transMeshes[15], red_data, red_vertexcount, red_indicies, red_indexcount, L"../dev4.dds");
FillMesh(transMeshes[16], yellow_data, yellow_vertexcount, yellow_indicies, yellow_indexcount, L"../dev4.dds");
FillMesh(transMeshes[17], green_data, green_vertexcount, green_indicies, green_indexcount, L"../dev4.dds");
FillMesh(transMeshes[18], blue_data, blue_vertexcount, blue_indicies, blue_indexcount, L"../dev4.dds");
FillMesh(transMeshes[19], purple_data, purple_vertexcount, purple_indicies, purple_indexcount, L"../dev4.dds");
//Balloon Offset
srand(time(NULL));
for (size_t i = 0; i < 4; i++)
{
int n = rand() % 20 -10;
int m = rand() % 20 -10;
int b = 5;
transMeshes[0 + (5 * i)].w.data[12] = -0.3 + n;
transMeshes[0 + (5 * i)].w.data[13] = -1.7f + b;
transMeshes[0 + (5 * i)].w.data[14] = .5 + m;
transMeshes[1 + (5 * i)].w.data[12] = -.4 + n;
transMeshes[1 + (5 * i)].w.data[13] = 0 + b;
transMeshes[1 + (5 * i)].w.data[14] = .3 + m;
transMeshes[2 + (5 * i)].w.data[12] = .4 + n;
transMeshes[2 + (5 * i)].w.data[13] = -1 + b;
transMeshes[2 + (5 * i)].w.data[14] = .3 + m;
transMeshes[3 + (5 * i)].w.data[12] = .9 + n;
transMeshes[3 + (5 * i)].w.data[13] = -3 + b;
transMeshes[3 + (5 * i)].w.data[14] = .9 + m;
transMeshes[4 + (5 * i)].w.data[12] = -.9 + n;
transMeshes[4 + (5 * i)].w.data[13] = -2 + b;
transMeshes[4 + (5 * i)].w.data[14] = -.6 + m;
}
}
void SetDeltaTime()
{
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
timeDeltaTime = std::chrono::duration_cast<std::chrono::microseconds>(now - oldTime).count();
timeDeltaTime /= 600000.0;
oldTime = now;
timeSinceStart += timeDeltaTime;
}
void SetKeyboardInput()
{
float w = 0;
float a = 0;
float s = 0;
float d = 0;
float up = 0;
float left = 0;
float down = 0;
float right = 0;
float shift = 0;
float space = 0;
float plus = 0;
float minus = 0;
float bracketR = 0;
float bracketL = 0;
float one = 0;
float mouseX = 0;
float mouseY = 0;
float e = 0;
float q = 0;
ginput.GetState(60, w);
ginput.GetState(38, a);
ginput.GetState(56, s);
ginput.GetState(41, d);
ginput.GetState(29, up);
ginput.GetState(31, left);
ginput.GetState(34, down);
ginput.GetState(32, right);
ginput.GetState(14, shift);
ginput.GetState(23, space);
ginput.GetState(3, plus);
ginput.GetState(2, minus);
ginput.GetState(6, bracketR);
ginput.GetState(7, bracketL);
ginput.GetState(65, one);
ginput.GetState(42, e);
ginput.GetState(54, q);
GW::GReturn result = ginput.GetMouseDelta(mouseX, mouseY);
if (result == GW::GReturn::REDUNDANT)
{
mouseX = 0;
mouseY = 0;
}
keys.up = (w + up);
keys.left = (a + left);
keys.down = (s + down);
keys.right = (d + right);
keys.space = (space);
keys.shift = (shift);
keys.plus = (plus);
keys.minus = (minus);
keys.bracketR = (bracketR);
keys.bracketL = (bracketL);
keys.one = (one);
keys.mouseX = mouseX;
keys.mouseY = mouseY;
keys.e = e;
keys.q = q;
}
void Physics()
{
//FOV
if (keys.minus)
{
fovSpeed += timeDeltaTime;
fov += fovSpeed / 20.0f;
}
else if (keys.plus)
{
fovSpeed += timeDeltaTime;
fov -= fovSpeed / 20.0f;
}
else
{
fovSpeed = 0;
}
if (fov > 120)
{
fovSpeed = 0;
fov = 120;
}
else if (fov < 10)
{
fovSpeed = 0;
fov = 10;
}
//NearFar
if (keys.bracketR)
{
nearFarSpeed += timeDeltaTime;
if (keys.shift)
{
farPlane += nearFarSpeed / 20.0f;
}
else
{
nearPlane += nearFarSpeed / 20.0f;
}
}
else if (keys.bracketL)
{
nearFarSpeed += timeDeltaTime;
if (keys.shift)
{
farPlane -= nearFarSpeed / 20.0f;
}
else
{
nearPlane -= nearFarSpeed / 20.0f;
}
}
else
{
nearFarSpeed = 0;
}
if (farPlane > 120)
{
nearFarSpeed = 0;
farPlane = 120;
}
else if (farPlane < 0.01)
{
nearFarSpeed = 0;
farPlane = 0.01;
}
if (nearPlane > 120)
{
nearFarSpeed = 0;
nearPlane = 120;
}
else if (nearPlane < 0.01)
{
nearFarSpeed = 0;
nearPlane = 0.01;
}
//Body
float maxed = .001f;
if (keys.shift)
{
maxed *= 3;
}
if (keys.up)
{
playerVelZ += timeDeltaTime / 50.0f;
playerVelZ = max(playerVelZ, -maxed);
playerVelZ = min(playerVelZ, maxed);
}
else if (keys.down)
{
playerVelZ -= timeDeltaTime / 50.0f;
playerVelZ = max(playerVelZ, -maxed);
playerVelZ = min(playerVelZ, maxed);
}
else if (playerVelZ > 0)
{
playerVelZ -= timeDeltaTime / 50.0f;
playerVelZ = max(playerVelZ, 0);
}
else if (playerVelZ < 0)
{
playerVelZ += timeDeltaTime / 50.0f;
playerVelZ = min(playerVelZ, 0);
}
if (keys.right)
{
playerVelX += timeDeltaTime / 50.0f;
playerVelX = max(playerVelX, -maxed);
playerVelX = min(playerVelX, maxed);
}
else if (keys.left)
{
playerVelX -= timeDeltaTime / 50.0f;
playerVelX = max(playerVelX, -maxed);
playerVelX = min(playerVelX, maxed);
}
else if (playerVelX > 0)
{
playerVelX -= timeDeltaTime / 50.0f;
playerVelX = max(playerVelX, 0);
}
else if (playerVelX < 0)
{
playerVelX += timeDeltaTime / 50.0f;
playerVelX = min(playerVelX, 0);
}
if (keys.e)
{
playerVelY += timeDeltaTime / 50.0f;
playerVelY = max(playerVelY, -maxed);
playerVelY = min(playerVelY, maxed);
}
else if (keys.q)
{
playerVelY -= timeDeltaTime / 50.0f;
playerVelY = max(playerVelY, -maxed);
playerVelY = min(playerVelY, maxed);
}
else if (playerVelY > 0)
{
playerVelY -= timeDeltaTime / 50.0f;
playerVelY = max(playerVelY, 0);
}
else if (playerVelY < 0)
{
playerVelY += timeDeltaTime / 50.0f;
playerVelY = min(playerVelY, 0);
}
//Apply Camera Data
float ar;
d3d.GetAspectRatio(ar);
m.ProjectionDirectXLHF(G_DEGREE_TO_RADIAN(fov), ar, nearPlane, farPlane, svars.p);
//Get Radians
float radianX = G_DEGREE_TO_RADIAN(keys.mouseX / 10.0f);
float radianY = G_DEGREE_TO_RADIAN(keys.mouseY / 10.0f);
//Get Rotation Matrices
GW::MATH::GMATRIXF rotatedX;
GW::MATH::GMATRIXF rotatedY;
m.RotationYawPitchRollF(-radianX, 0, 0, rotatedX);
m.RotationYawPitchRollF(0, -radianY, 0, rotatedY);
//Edit View Local
m.MultiplyMatrixF(viewLocalM, rotatedY, viewLocalM);
//Edit Character Local
m.MultiplyMatrixF(viewWorldM, rotatedX, viewWorldM);
m.TranslateGlobalF(viewWorldM, GW::MATH::GVECTORF{ -playerVelX,-playerVelY,-playerVelZ }, viewWorldM);
//Multiply views
m.MultiplyMatrixF(viewWorldM, viewLocalM, svars.v);
}
void Update()
{
GW::MATH::GMATRIXF temptemp;
m.InverseF(svars.v, temptemp);
svars.camPos = GW::MATH::GVECTORF{ temptemp.data[12],temptemp.data[13],temptemp.data[14] };
svars.lightDir.x = sin(timeSinceStart / 2.0);
svars.lightDir.y = (sin(timeSinceStart / 2.0) / 2) - 1;
svars.pointLightPos = GW::MATH::GVECTORF{ (float)cos(timeSinceStart),((float)sin(timeSinceStart) + 2),(float)cos(timeSinceStart),1 };
svars.spotLightPos = GW::MATH::GVECTORF{ (float)cos(timeSinceStart) + 10,3,(float)cos(timeSinceStart),1 };
svars.spotLightDir.x = sin(timeSinceStart / 2.0);
svars.innerConeRatio.x = 0.4f - min(sin(timeSinceStart), 0.0f);
svars.outerConeRatio.x = 0.4f;
meshes[0].w.row4 = svars.pointLightPos;
meshes[2].w.data[13] = 2;
}
};