-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorFieldVisualiser.cpp
More file actions
673 lines (597 loc) · 23.4 KB
/
VectorFieldVisualiser.cpp
File metadata and controls
673 lines (597 loc) · 23.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
// ReSharper disable CppClangTidyClangDiagnosticLanguageExtensionToken
// ReSharper disable CppFunctionResultShouldBeUsed
// ReSharper disable CppClangTidyConcurrencyMtUnsafe
// ReSharper disable CppDeclaratorNeverUsed
#include <windows.h>
#include <windowsx.h>
// ReSharper disable once CppInconsistentNaming
# define _USE_MATH_DEFINES // NOLINT(clang-diagnostic-reserved-macro-identifier, bugprone-reserved-identifier)
// DirectX 11
#include <dxgi1_6.h>
#include <d3d11.h>
#include <d3dcompiler.h>
#include <DirectXMath.h>
#include <iostream>
#include <stdexcept>
#include <vector>
#include "resource.h"
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "d3dcompiler.lib")
namespace
{
// Global variables
HWND window_handle;
UINT width = 800;
UINT height = 600;
int axes_size = 5;
int stride = 2;
float arrow_length = 0.5f;
float rotation_speed = 1.0f;
// Rendering variables
struct vertex
{
DirectX::XMFLOAT3 pos;
DirectX::XMFLOAT4 col;
};
struct camera
{
DirectX::XMVECTOR pos;
DirectX::XMVECTOR target;
DirectX::XMVECTOR up;
DirectX::XMMATRIX get_view_matrix() const
{
return DirectX::XMMatrixLookAtLH(pos, target, up);
}
void yaw(const float dx)
{
const DirectX::XMVECTOR dist = DirectX::XMVector3Length(DirectX::XMVectorSubtract(target, pos));
const DirectX::XMVECTOR right = DirectX::XMVector3Normalize(
DirectX::XMVector3Cross(up, DirectX::XMVector3Normalize(DirectX::XMVectorSubtract(target, pos))));
pos = DirectX::XMVectorScale(
DirectX::XMVector3Normalize(DirectX::XMVectorAdd(pos, DirectX::XMVectorScale(right, dx))),
DirectX::XMVectorGetX(dist));
const DirectX::XMVECTOR direction = DirectX::XMVector3Normalize(DirectX::XMVectorSubtract(target, pos));
up = DirectX::XMVector3Normalize(DirectX::XMVectorSubtract(
DirectX::XMVectorScale(DirectX::XMVectorSubtract(pos, target),
DirectX::XMVectorGetY(DirectX::XMVectorSubtract(pos, target))),
{
0.0f, std::pow(DirectX::XMVectorGetX(DirectX::XMVector3Length(DirectX::XMVectorSubtract(pos, target))), 2.0f),
0.0f
}
));
}
void pitch(const float dy)
{
const DirectX::XMVECTOR dist = DirectX::XMVector3Length(DirectX::XMVectorSubtract(target, pos));
const DirectX::XMVECTOR right = DirectX::XMVector3Normalize(
DirectX::XMVector3Cross(up, DirectX::XMVector3Normalize(DirectX::XMVectorSubtract(target, pos))));
pos = DirectX::XMVectorScale(
DirectX::XMVector3Normalize(DirectX::XMVectorAdd(pos, DirectX::XMVectorScale(up, dy))),
DirectX::XMVectorGetX(dist));
const DirectX::XMVECTOR direction = DirectX::XMVector3Normalize(DirectX::XMVectorSubtract(target, pos));
up = DirectX::XMVector3Cross(right, direction);
}
};
std::vector<vertex> arrows;
DirectX::XMFLOAT3 vector_field(const DirectX::XMFLOAT3 pos)
{
const float x = pos.x;
const float y = pos.y;
const float z = pos.z;
return {
(-10 * y + std::sin(z)) / (x * x + y * y + 1),
(10 * x + std::cos(z)) / (x * x + y * y + 1),
-5 * std::sin((z + 0.1f * x * y + 0.25f) * static_cast<float>(M_PI))
};
}
camera cam
{
{0.0, 5.0, 25.0, 1.0},
{0.0, 0.0, 0.0, 1.0},
DirectX::XMVector3Normalize({0.0, 25.0, 5.0, 0.0}),
};
DirectX::XMMATRIX init_world_orientation = DirectX::XMMatrixRotationY(DirectX::XMConvertToRadians(-150)) *
DirectX::XMMatrixRotationX(DirectX::XMConvertToRadians(15));
DirectX::XMMATRIX switch_y_z = {
1.0, 0.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.0, 1.0
};
// DirectX::XMMATRIX init_view = DirectX::XMMatrixLookAtLH(
// {0.0, 1.0, 10.0, 1.0},
// {0.0, 0.0, 0.0, 1.0},
// DirectX::XMVector3Normalize({0.0, 10.0, 1.0, 0.0})
// );
DirectX::XMMATRIX init_world = switch_y_z * init_world_orientation;
DirectX::XMMATRIX init_projection = DirectX::XMMatrixPerspectiveFovLH(
DirectX::XM_PIDIV4,
static_cast<float>(width) / static_cast<float>(height),
0.1f, 100.0f
);
DirectX::XMMATRIX wvp = init_world * cam.get_view_matrix() * init_projection;
struct cb_data
{
DirectX::XMMATRIX wvp;
};
cb_data cb = {wvp};
// Axes
std::vector<vertex> axes = {
// X-axis (red)
{{-static_cast<float>(axes_size), 0.0f, 0.0f}, {1, 0, 0, 1}},
{{0.5f + static_cast<float>(axes_size), 0.0f, 0.0f}, {1, 0, 0, 1}},
// Y-axis (green)
{{0.0f, -static_cast<float>(axes_size), 0.0f}, {0, 1, 0, 1}},
{{0.0f, 0.5f + static_cast<float>(axes_size), 0.0f}, {0, 1, 0, 1}},
// Z-axis (blue)
{{0.0f, 0.0f, -static_cast<float>(axes_size)}, {0, 0, 1, 1}},
{{0.0f, 0.0f, 0.5f + static_cast<float>(axes_size)}, {0, 0, 1, 1}},
};
std::vector<vertex> axes_ticks = {};
std::vector<vertex> axes_arrows = {
// x-axis
{{static_cast<float>(axes_size) + 0.5f, 0.0, 0.0}, {1.0, 0.0, 0.0, 1.0}},
{{static_cast<float>(axes_size) + 0.25f, -0.25, 0.0}, {1.0, 0.0, 0.0, 1.0}},
{{static_cast<float>(axes_size) + 0.25f, 0.25, 0.0}, {1.0, 0.0, 0.0, 1.0}},
// y-axis
{{0.0, static_cast<float>(axes_size) + 0.5f, 0.0}, {0.0, 1.0, 0.0, 1.0}},
{{-0.25, static_cast<float>(axes_size) + 0.25f, 0.0}, {0.0, 1.0, 0.0, 1.0}},
{{0.25, static_cast<float>(axes_size) + 0.25f, 0.0}, {0.0, 1.0, 0.0, 1.0}},
// z-axis
{{0.0, 0.0, static_cast<float>(axes_size) + 0.5f}, {0.0, 0.0, 1.0, 1.0}},
{{-0.25, 0.0, static_cast<float>(axes_size) + 0.25f}, {0.0, 0.0, 1.0, 1.0}},
{{0.25, 0.0, static_cast<float>(axes_size) + 0.25f}, {0.0, 0.0, 1.0, 1.0}},
};
std::vector<UINT> axes_arrows_indices = {
// x-axis
0, 2, 1,
// y-axis
3, 4, 5,
// z-axis
6, 7, 8,
};
// Buffers
ID3D11Buffer* axes_vertex_buffer; // A buffer for axes vertex data
ID3D11Buffer* axes_ticks_buffer; // A buffer for axes ticks data
ID3D11Buffer* axes_arrows_buffer; // A buffer for axes arrows data
ID3D11Buffer* axes_arrows_index_buffer; // A buffer for axes arrows index data
ID3D11Buffer* cb_buffer; // A buffer for constant data
ID3D11Buffer* arrows_vertex_buffer; // A buffer for arrow vertex data
ID3D11VertexShader* vertex_shader; // A vertex shader
ID3D11PixelShader* pixel_shader; // A pixel shader
ID3D11InputLayout* input_layout; // A layout for vertex data
ID3D11Device* device; // An interface to communicate with device GPU
ID3D11DeviceContext* context; // The context surrounding rendering to the device's screen
IDXGISwapChain* swap_chain; // A swap chain determines how to change frames
ID3D11RenderTargetView* target_view; // What is rendered on
ID3D11DepthStencilView* depth_view; // Manages the depth and visibility of objects
ID3D11RasterizerState* rasterizer_state = nullptr;
// Window Procedure: handles events/messages like close, resize, etc.
LRESULT CALLBACK window_proc(const HWND h_wnd, const UINT u_msg, const WPARAM w_param, const LPARAM l_param)
{
switch (u_msg)
{
case WM_DESTROY:
PostQuitMessage(0); // Tells the app to quit
return 0;
case WM_KEYDOWN:
switch (w_param)
{
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
case VK_LEFT:
std::cout << "Left" << std::endl;
cam.yaw(-0.1f);
return 0;
case VK_RIGHT:
std::cout << "Right" << std::endl;
cam.yaw(0.1f);
return 0;
case VK_UP:
std::cout << "Up" << std::endl;
cam.pitch(0.1f);
return 0;
case VK_DOWN:
std::cout << "Down" << std::endl;
cam.pitch(-0.1f);
return 0;
default: break;
}
default: break;
}
return DefWindowProc(h_wnd, u_msg, w_param, l_param);
}
// Initialise the window
void init_window(const HINSTANCE h_instance)
{
// 1. Register the window class
const auto class_name = L"VectorFieldVisualiser";
const auto title = L"Vector Field Visualiser";
WNDCLASSEX wc = {};
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = window_proc; // callback for messages
wc.hInstance = h_instance;
wc.hIcon = LoadIcon(h_instance, MAKEINTRESOURCE(IDI_VECTORFIELDVISUALISER));
wc.hIconSm = LoadIcon(h_instance, MAKEINTRESOURCE(IDI_SMALL));
wc.lpszClassName = class_name;
wc.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW);
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
RegisterClassEx(&wc);
// 2. Create the window
window_handle = CreateWindowEx(
0, // Optional window styles
class_name, // Window class
title, // Window text (title)
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
nullptr, // Parent window
nullptr, // Menu
h_instance, // Instance handle
nullptr // Additional application data
);
if (window_handle == nullptr)
{
throw std::runtime_error("Failed to create a window.");
}
ShowWindow(window_handle, SW_SHOW);
}
// ReSharper disable once CppInconsistentNaming
void init_D3D()
{
// Swap chain configuration
DXGI_SWAP_CHAIN_DESC scd = {};
scd.BufferCount = 1;
scd.BufferDesc.Width = width;
scd.BufferDesc.Height = height;
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
scd.BufferDesc.RefreshRate.Numerator = 60;
scd.BufferDesc.RefreshRate.Denominator = 1;
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
scd.OutputWindow = window_handle;
scd.SampleDesc.Count = 1;
scd.SampleDesc.Quality = 0;
scd.Windowed = true;
scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
// Depth view configuration
CD3D11_TEXTURE2D_DESC dvd = {};
dvd.Width = width;
dvd.Height = height;
dvd.MipLevels = 1;
dvd.ArraySize = 1;
dvd.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
dvd.SampleDesc.Count = 1;
dvd.BindFlags = D3D11_BIND_DEPTH_STENCIL;
// Axes vertex buffer configuration
D3D11_BUFFER_DESC axes_vertex_buffer_desc = {};
axes_vertex_buffer_desc.Usage = D3D11_USAGE_DEFAULT;
axes_vertex_buffer_desc.ByteWidth = sizeof(vertex) * axes.size();
axes_vertex_buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
axes_vertex_buffer_desc.CPUAccessFlags = 0;
D3D11_SUBRESOURCE_DATA init_axes_vertex_data = {};
init_axes_vertex_data.pSysMem = axes.data();
// Axes ticks buffer configuration
for (int i = -axes_size; i <= axes_size; i += stride)
{
// x-axis tick
axes_ticks.emplace_back(vertex{{static_cast<float>(i), -0.25, 0.0}, {1.0, 0.0, 0.0, 1.0}});
axes_ticks.emplace_back(vertex{{static_cast<float>(i), 0.25, 0.0}, {1.0, 0.0, 0.0, 1.0}});
// y-axis tick
axes_ticks.emplace_back(vertex{{-0.25, static_cast<float>(i), 0.0}, {0.0, 1.0, 0.0, 1.0}});
axes_ticks.emplace_back(vertex{{0.25, static_cast<float>(i), 0.0}, {0.0, 1.0, 0.0, 1.0}});
// z-axis tick
axes_ticks.emplace_back(vertex{{-0.25, 0.0, static_cast<float>(i)}, {0.0, 0.0, 1.0, 1.0}});
axes_ticks.emplace_back(vertex{{0.25, 0.0, static_cast<float>(i)}, {0.0, 0.0, 1.0, 1.0}});
}
D3D11_BUFFER_DESC axes_ticks_buffer_desc = {};
axes_ticks_buffer_desc.Usage = D3D11_USAGE_DEFAULT;
axes_ticks_buffer_desc.ByteWidth = sizeof(vertex) * axes_ticks.size();
axes_ticks_buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
axes_ticks_buffer_desc.CPUAccessFlags = 0;
D3D11_SUBRESOURCE_DATA init_axes_ticks_data = {};
init_axes_ticks_data.pSysMem = axes_ticks.data();
// Axes arrows buffer configuration
D3D11_BUFFER_DESC axes_arrows_buffer_desc = {};
axes_arrows_buffer_desc.Usage = D3D11_USAGE_DEFAULT;
axes_arrows_buffer_desc.ByteWidth = sizeof(vertex) * axes_arrows.size();
axes_arrows_buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
axes_arrows_buffer_desc.CPUAccessFlags = 0;
D3D11_SUBRESOURCE_DATA init_axes_arrows_data = {};
init_axes_arrows_data.pSysMem = axes_arrows.data();
// Axes arrows index buffer configuration
D3D11_BUFFER_DESC axes_arrows_index_buffer_desc = {};
axes_arrows_index_buffer_desc.Usage = D3D11_USAGE_DEFAULT;
axes_arrows_index_buffer_desc.ByteWidth = sizeof(UINT) * axes_arrows_indices.size();
axes_arrows_index_buffer_desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
axes_arrows_index_buffer_desc.CPUAccessFlags = 0;
D3D11_SUBRESOURCE_DATA init_axes_arrows_index_data = {};
init_axes_arrows_index_data.pSysMem = axes_arrows_indices.data();
// Constant buffer configuration
D3D11_BUFFER_DESC constant_buffer_desc = {};
constant_buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
constant_buffer_desc.ByteWidth = sizeof(cb_data);
constant_buffer_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
constant_buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
constant_buffer_desc.MiscFlags = 0;
constant_buffer_desc.StructureByteStride = 0;
D3D11_SUBRESOURCE_DATA init_cb_data = {};
init_cb_data.pSysMem = &cb;
init_cb_data.SysMemPitch = 0;
init_cb_data.SysMemSlicePitch = 0;
// Fill in the vector field
arrows.reserve(pow(axes_size * 2 + 1, 3));
for (int x = -axes_size; x <= axes_size; x += stride)
{
for (int y = -axes_size; y <= axes_size; y += stride)
{
for (int z = -axes_size; z <= axes_size; z += stride)
{
const DirectX::XMFLOAT3 start = {static_cast<float>(x), static_cast<float>(y), static_cast<float>(z)};
const DirectX::XMFLOAT3 vec = vector_field(start);
const double mag = DirectX::XMVectorGetX(DirectX::XMVector3LengthSq(DirectX::XMLoadFloat3(&vec)));
DirectX::XMFLOAT3 end;
DirectX::XMStoreFloat3(&end, DirectX::XMVectorAdd(DirectX::XMLoadFloat3(&start),
DirectX::XMVectorScale(
DirectX::XMVector3Normalize(DirectX::XMLoadFloat3(&vec)),
arrow_length)));
DirectX::XMFLOAT4 color;
switch (static_cast<int>(std::floor(mag / 5)))
{
case 0:
color = {0.0, 0.0, 1.0, 1.0};
break;
case 1:
color = {0.0, 1.0, 1.0, 1.0};
break;
case 2:
color = {0.0, 1.0, 0.0, 1.0};
break;
case 3:
color = {1.0, 1.0, 0.0, 1.0};
break;
default:
color = {1.0, 0.0, 0.0, 1.0};
break;
}
const DirectX::XMVECTOR direction_vec = DirectX::XMVectorSubtract(DirectX::XMLoadFloat3(&end), DirectX::XMLoadFloat3(&start));
// Calculate the positions of the tips
DirectX::XMVECTOR start_vec = DirectX::XMLoadFloat3(&start);
DirectX::XMFLOAT3 tip1;
DirectX::XMFLOAT3 tip2;
if (DirectX::XMVectorGetX(DirectX::XMVector3Dot(
direction_vec,
{0.0, 0.0, 1.0})) == 0.0)
{
DirectX::XMVECTOR perp_vec = { 1.0, 0.0, 0.0 };
DirectX::XMVECTOR tip1_vec = DirectX::XMVectorAdd(DirectX::XMVectorAdd(start_vec, DirectX::XMVectorScale(direction_vec, arrow_length * 0.75f)), perp_vec);
DirectX::XMVECTOR tip2_vec = DirectX::XMVectorSubtract(DirectX::XMVectorAdd(start_vec, DirectX::XMVectorScale(direction_vec, arrow_length * 0.75f)), perp_vec);
DirectX::XMStoreFloat3(&tip1, tip1_vec);
DirectX::XMStoreFloat3(&tip2, tip2_vec);
} else
{
DirectX::XMVECTOR perp_vec = DirectX::XMVectorScale(DirectX::XMVector3Normalize({
DirectX::XMVectorGetY(direction_vec),
-DirectX::XMVectorGetX(direction_vec),
0.0
}), arrow_length * 0.25f);
DirectX::XMVECTOR tip1_vec = DirectX::XMVectorAdd(DirectX::XMVectorAdd(start_vec, DirectX::XMVectorScale(direction_vec, arrow_length * 0.75f)), perp_vec);
DirectX::XMVECTOR tip2_vec = DirectX::XMVectorSubtract(DirectX::XMVectorAdd(start_vec, DirectX::XMVectorScale(direction_vec, arrow_length * 0.75f)), perp_vec);
DirectX::XMStoreFloat3(&tip1, tip1_vec);
DirectX::XMStoreFloat3(&tip2, tip2_vec);
}
arrows.emplace_back(vertex{start, color});
arrows.emplace_back(vertex{end, color});
arrows.emplace_back(vertex{tip1, color});
arrows.emplace_back(vertex{end, color});
arrows.emplace_back(vertex{tip2, color});
arrows.emplace_back(vertex{end, color});
}
}
}
// Configure the arrows_vertex_buffer
D3D11_BUFFER_DESC arrows_vertex_buffer_desc = {};
arrows_vertex_buffer_desc.Usage = D3D11_USAGE_DEFAULT;
arrows_vertex_buffer_desc.ByteWidth = sizeof(vertex) * arrows.size();
arrows_vertex_buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
arrows_vertex_buffer_desc.CPUAccessFlags = 0;
D3D11_SUBRESOURCE_DATA init_arrows_vertex_data = {};
init_arrows_vertex_data.pSysMem = arrows.data();
// TEMPORARY: Disable culling
D3D11_RASTERIZER_DESC rasterizer_desc = {};
rasterizer_desc.CullMode = D3D11_CULL_NONE;
rasterizer_desc.FillMode = D3D11_FILL_SOLID;
rasterizer_desc.FrontCounterClockwise = true;
rasterizer_desc.DepthBias = 0;
rasterizer_desc.DepthBiasClamp = 0.0f;
rasterizer_desc.SlopeScaledDepthBias = 0.0f;
rasterizer_desc.DepthClipEnable = true;
rasterizer_desc.ScissorEnable = false;
rasterizer_desc.MultisampleEnable = false;
rasterizer_desc.AntialiasedLineEnable = false;
D3D_FEATURE_LEVEL feature_level;
const HRESULT hr_device_swap_chain = D3D11CreateDeviceAndSwapChain(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
D3D11_CREATE_DEVICE_SINGLETHREADED,
nullptr,
0,
D3D11_SDK_VERSION,
&scd,
&swap_chain,
&device,
&feature_level,
&context
);
if (FAILED(hr_device_swap_chain))
{
char msg[128];
sprintf_s(msg, "D3D11CreateDeviceAndSwapChain failed with HRESULT: 0x%08X", hr_device_swap_chain);
std::cout << msg;
MessageBoxA(nullptr, msg, "Error", MB_OK | MB_ICONERROR);
exit(1);
}
device->CreateRasterizerState(&rasterizer_desc, &rasterizer_state);
// Create the axes vertex buffer
device->CreateBuffer(&axes_vertex_buffer_desc, &init_axes_vertex_data, &axes_vertex_buffer);
// Create the axes ticks buffer
device->CreateBuffer(&axes_ticks_buffer_desc, &init_axes_ticks_data, &axes_ticks_buffer);
// Create the axes' arrows buffer
device->CreateBuffer(&axes_arrows_buffer_desc, &init_axes_arrows_data, &axes_arrows_buffer);
// Create the axes' arrows index buffer
device->CreateBuffer(&axes_arrows_index_buffer_desc, &init_axes_arrows_index_data, &axes_arrows_index_buffer);
// Create arrows vertex buffer
device->CreateBuffer(&arrows_vertex_buffer_desc, &init_arrows_vertex_data, &arrows_vertex_buffer);
// Create the constant buffer
device->CreateBuffer(&constant_buffer_desc, &init_cb_data, &cb_buffer);
// Load compiled shaders
ID3DBlob* vertex_shader_blob = nullptr;
ID3DBlob* pixel_shader_blob = nullptr;
const HRESULT hr_vertex_shader = D3DReadFileToBlob(L"Shaders/Compiled/VertexShader.cso", &vertex_shader_blob);
if (FAILED(hr_vertex_shader))
{
char msg[128];
sprintf_s(msg, "D3DCompileFromFile failed with HRESULT: 0x%08X", hr_vertex_shader);
std::cout << msg;
MessageBoxA(nullptr, msg, "Error", MB_OK | MB_ICONERROR);
exit(1);
}
device->CreateVertexShader(
vertex_shader_blob->GetBufferPointer(),
vertex_shader_blob->GetBufferSize(),
nullptr,
&vertex_shader
);
const HRESULT hr_pixel_shader = D3DReadFileToBlob(L"Shaders/Compiled/PixelShader.cso", &pixel_shader_blob);
if (FAILED(hr_pixel_shader))
{
char msg[128];
sprintf_s(msg, "D3DCompileFromFile failed with HRESULT: 0x%08X", hr_pixel_shader);
std::cout << msg;
MessageBoxA(nullptr, msg, "Error", MB_OK | MB_ICONERROR);
exit(1);
}
device->CreatePixelShader(
pixel_shader_blob->GetBufferPointer(),
pixel_shader_blob->GetBufferSize(),
nullptr,
&pixel_shader
);
// Create the input layout
D3D11_INPUT_ELEMENT_DESC layout[] = {
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{
"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12 /*D3D11_APPEND_ALIGNED_ELEMENT*/, D3D11_INPUT_PER_VERTEX_DATA,
0
},
};
UINT num_elements = ARRAYSIZE(layout);
device->CreateInputLayout(
layout,
num_elements,
vertex_shader_blob->GetBufferPointer(),
vertex_shader_blob->GetBufferSize(),
&input_layout
);
ID3D11Texture2D* back_buffer = nullptr; // The back buffer of the swap chain
ID3D11Texture2D* depth_buffer = nullptr; // The depth buffer of the swap chain
swap_chain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&back_buffer));
device->CreateTexture2D(&dvd, nullptr, &depth_buffer);
device->CreateRenderTargetView(back_buffer, nullptr, &target_view);
device->CreateDepthStencilView(depth_buffer, nullptr, &depth_view);
back_buffer->Release();
depth_buffer->Release();
context->OMSetRenderTargets(1, &target_view, depth_view);
// Viewport configuration
D3D11_VIEWPORT viewport = {};
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = static_cast<float>(width);
viewport.Height = static_cast<float>(height);
context->RSSetViewports(1, &viewport);
context->RSSetState(rasterizer_state);
}
void render()
{
// Clear the screen for rendering
constexpr float background[4] = {0.0f, 0.0f, 0.0f, 1.0f}; // RGBA from 0 to 1
context->ClearRenderTargetView(target_view, background);
context->ClearDepthStencilView(depth_view, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
context->IASetInputLayout(input_layout);
// "Recalculate" the WVP matrix - currently no need to change it
const DirectX::XMMATRIX world = init_world;
const DirectX::XMMATRIX view = cam.get_view_matrix();
const DirectX::XMMATRIX projection = init_projection;
const DirectX::XMMATRIX wvp = world * view * projection;
const cb_data new_cb = {DirectX::XMMatrixTranspose(wvp)}; // Transpose the matrix for the shader
context->UpdateSubresource(cb_buffer, 0, nullptr, &new_cb, 0, 0);
// Configure shaders for rendering
context->VSSetShader(vertex_shader, nullptr, 0);
context->PSSetShader(pixel_shader, nullptr, 0);
context->VSSetConstantBuffers(0, 1, &cb_buffer);
// The following are useful for vertices in general
constexpr UINT stride = sizeof(vertex);
constexpr UINT offset = 0;
// Render the axes
context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
context->IASetVertexBuffers(0, 1, &axes_vertex_buffer, &stride, &offset);
context->Draw(axes.size(), 0);
// Render the axes ticks
context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
context->IASetVertexBuffers(0, 1, &axes_ticks_buffer, &stride, &offset);
context->Draw(axes_ticks.size(), 0);
// Render field arrows
context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
context->IASetVertexBuffers(0, 1, &arrows_vertex_buffer, &stride, &offset);
context->Draw(arrows.size(), 0);
// Render the axes' arrows
context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
context->IASetVertexBuffers(0, 1, &axes_arrows_buffer, &stride, &offset);
context->IASetIndexBuffer(axes_arrows_index_buffer, DXGI_FORMAT_R32_UINT, 0);
context->DrawIndexed(axes_arrows_indices.size(), 0, 0);
swap_chain->Present(1, 0);
}
void clean_up()
{
if (target_view != nullptr) target_view->Release();
if (depth_view != nullptr) depth_view->Release();
if (swap_chain != nullptr) swap_chain->Release();
if (context != nullptr) context->Release();
if (device != nullptr) device->Release();
if (vertex_shader != nullptr) vertex_shader->Release();
if (pixel_shader != nullptr) pixel_shader->Release();
if (input_layout != nullptr) input_layout->Release();
if (cb_buffer != nullptr) cb_buffer->Release();
if (axes_ticks_buffer != nullptr) axes_ticks_buffer->Release();
if (rasterizer_state != nullptr) rasterizer_state->Release();
if (axes_arrows_buffer != nullptr) axes_arrows_buffer->Release();
if (axes_vertex_buffer != nullptr) axes_vertex_buffer->Release();
if (axes_arrows_index_buffer != nullptr) axes_arrows_index_buffer->Release();
if (window_handle != nullptr) DestroyWindow(window_handle);
}
}
// WinMain: the entry point for a Windows desktop application
// ReSharper disable once CppInconsistentNaming
int WINAPI WinMain(const HINSTANCE hInstance, HINSTANCE, LPSTR, int)
{
init_window(hInstance);
init_D3D();
MSG msg = {};
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
render();
}
}
clean_up();
return static_cast<int>(msg.wParam);
}