-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderer.cpp
More file actions
649 lines (514 loc) · 18.3 KB
/
Renderer.cpp
File metadata and controls
649 lines (514 loc) · 18.3 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
#include "Renderer.h"
/// <summary>
/// Adjust view distance in specified direction
/// </summary>
/// <param name="direction">Adjustment direction</param>
void Renderer::AdjustViewDistance(int direction) {
float step = 0.1f;
if (direction > 0) {
_viewZ += step;
}
else {
_viewZ -= step;
}
}
/// <summary>
/// Get object info
/// </summary>
/// <returns></returns>
Renderer::ObjectInfo Renderer::GetObjectInfo() {
return _objectInfo;
}
/// <summary>
/// Initialize renderer
/// </summary>
bool Renderer::Initialize(HWND outputWindow, UINT width, UINT height) {
// Save parameters
_outputWindow = outputWindow;
_windowWidth = width;
_windowHeight = height;
// Direct3D
if (!InitializeDirect3D()) return false;
// Lights
if (!InitializeLights()) return false;
// Success
return true;
}
/// <summary>
/// Load object
/// </summary>
/// <returns>Load success state</returns>
bool Renderer::LoadObject(std::string objectPathname, std::wstring& errorReason) {
// Extract mesh data from object
ObjectReader reader;
if (!reader.ReadObjectFile(objectPathname, errorReason)) {
return false;
}
// Free old buffers if required
if(_vertexBuffer) _vertexBuffer->Release();
if(_indexBuffer) _indexBuffer->Release();
if(_vsConstantBuffer) _vsConstantBuffer->Release();
if(_psConstantBuffer) _psConstantBuffer->Release();
// Get object vertices and indices
_vertices = reader.GetVertices();
_indices = reader.GetIndices();
// Set object info
_objectInfo.numVertices = _vertices.size();
_objectInfo.numLayers = reader.GetNumLayers();
_objectInfo.numNonTriangles = reader.GetNumNonTriangles();
_objectInfo.numTriangles = reader.GetNumTriangles();
// Buffers
if (!InitializeBuffers()) return false;
// Initialize transforms
if (!InitializeObjectTransforms()) return false;
return true;
}
/// <summary>
/// Present the current frame
/// </summary>
void Renderer::Present() {
// Flip the back buffer
HRESULT hr = _swapChain->Present(0, 0);
assert(!FAILED(hr));
}
/// <summary>
/// Render a frame
/// </summary>
void Renderer::Render() {
// Update constant buffers
_deviceContext->UpdateSubresource(_vsConstantBuffer, 0, nullptr, &_vsConstantBufferData, 0, 0);
_deviceContext->UpdateSubresource(_psConstantBuffer, 0, nullptr, &_psConstantBufferData, 0, 0);
// Bind render target (Output-Merger stage)
_deviceContext->OMSetRenderTargets(1, &_renderTargetView, _depthStencilView);
// Clear render target
float clearColor[4] = { 0.1f, 0.1f, 0.1f, 1.0f };
_deviceContext->ClearRenderTargetView(_renderTargetView, clearColor);
_deviceContext->ClearDepthStencilView(_depthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
// Set constant buffers
_deviceContext->VSSetConstantBuffers(0, 1, &_vsConstantBuffer); // Register b0
_deviceContext->PSSetConstantBuffers(1, 1, &_psConstantBuffer); // Register b1
// Set vertex shader stage
_deviceContext->VSSetShader(_vertexShader, nullptr, 0);
// Set pixel shader stage
_deviceContext->PSSetShader(_pixelShader, nullptr, 0);
// Draw indexed triangles
_deviceContext->DrawIndexed(_indices.size(), 0, 0);
}
/// <summary>
/// Reset object transformations
/// </summary>
void Renderer::ResetTransformations() {
// Translate
DirectX::XMMATRIX objectTranslation = DirectX::XMMatrixTranslation(0.0f, 0.0f, 0.0f);
// Rotate
DirectX::XMMATRIX objectRotation = DirectX::XMMatrixRotationRollPitchYaw(0, 0, 0);
// Update model matrix in vertex shader
_modelMatrix = objectTranslation * objectRotation;
DirectX::XMStoreFloat4x4(&_vsConstantBufferData.world, _modelMatrix);
}
/// <summary>
/// Rotate object
/// </summary>
/// <param name="yaw">Yaw rotation amount</param>
/// <param name="pitch">Pitch rotation amount</param>
void Renderer::Rotate(float yaw, float pitch) {
// Rotate
DirectX::XMMATRIX objectRotation = DirectX::XMMatrixRotationRollPitchYaw(pitch, yaw, 0);
// Update model matrix in vertex shader
_modelMatrix = _modelMatrix * objectRotation;
DirectX::XMStoreFloat4x4(&_vsConstantBufferData.world, _modelMatrix);
}
/// <summary>
/// Shut down renderer
/// </summary>
void Renderer::Shutdown() {
// Depth stencil
if (_depthStencilState) _depthStencilState->Release();
if (_depthStencilView) _depthStencilView->Release();
// Shader resources
_vertexShader->Release();
_pixelShader->Release();
_inputLayout->Release();
// Constant buffers
if(_vsConstantBuffer) _vsConstantBuffer->Release();
if(_psConstantBuffer) _psConstantBuffer->Release();
// Object buffers
if(_vertexBuffer) _vertexBuffer->Release();
if(_indexBuffer) _indexBuffer->Release();
// D3D resources
_renderTargetView->Release();
_swapChain->Release();
_deviceContext->Release();
_device->Release();
}
/// <summary>
/// Tumble model
/// </summary>
/// <param name="tumble">Tumble state</param>
void Renderer::Tumble(bool tumble) {
_tumble = tumble;
}
/// <summary>
/// Update the scene
/// </summary>
void Renderer::Update() {
// Get current time
ULONGLONG currentTime = GetTickCount64();
// Initialize start time
if (_time == 0) _time = currentTime;
// Calculate elapsed time
float elapsedTime = (float)(currentTime - _time);
// Calculate view matrix
DirectX::XMVECTOR eyePt = DirectX::XMVectorSet(0.0f, 0.0f, _viewZ, 0.0f);
DirectX::XMVECTOR lookAt = DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);
DirectX::XMVECTOR upVec = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
_viewMatrix = DirectX::XMMatrixTranspose(DirectX::XMMatrixLookAtRH(eyePt, lookAt, upVec));
// Calculate projection matrix
float aspectRatio = (float)_windowWidth / (float)_windowHeight;
float fovAngleY = 45.0f * (DirectX::XM_PI / 180.0f);
float nearPlane = 0.01f;
float farPlane = 500.0f;
_projectionMatrix = DirectX::XMMatrixTranspose(DirectX::XMMatrixPerspectiveFovRH(fovAngleY, aspectRatio, nearPlane, farPlane));
// Update world-view
DirectX::XMMATRIX worldViewMatrix = _modelMatrix * _viewMatrix;
DirectX::XMStoreFloat4x4(&_vsConstantBufferData.worldView, worldViewMatrix);
// Update world-view-projection
DirectX::XMMATRIX worldViewProjectionMatrix = _projectionMatrix * _viewMatrix * _modelMatrix;
DirectX::XMStoreFloat4x4(&_vsConstantBufferData.worldViewProj, worldViewProjectionMatrix);
}
/// <summary>
/// Initialize the depth buffer
/// </summary>
/// <returns>Initialization success</returns>
bool Renderer::InitializeDepthBuffer() {
HRESULT hr;
// Initialize buffer description
D3D11_TEXTURE2D_DESC depthTextureDesc;
ZeroMemory(&depthTextureDesc, sizeof(depthTextureDesc));
depthTextureDesc.Width = _windowWidth;
depthTextureDesc.Height = _windowHeight;
depthTextureDesc.MipLevels = 1;
depthTextureDesc.ArraySize = 1;
depthTextureDesc.SampleDesc.Count = 1;
depthTextureDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthTextureDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
// Create the depth stencil texture
ID3D11Texture2D* depthStencilTexture;
hr = _device->CreateTexture2D(&depthTextureDesc, NULL, &depthStencilTexture);
// Check for errors
if (FAILED(hr)) {
MessageBox(nullptr, L"Error creating depth stencil texture", L"Depth Buffer Initialization Error", MB_OK);
return false;
}
// Initialize depth stencil view description
D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc));
depthStencilViewDesc.Format = depthTextureDesc.Format;
depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;
// Create the depth stencil view
hr = _device->CreateDepthStencilView(depthStencilTexture, &depthStencilViewDesc, &_depthStencilView);
depthStencilTexture->Release();
// Check for errors
if (FAILED(hr)) {
MessageBox(nullptr, L"Error creating depth stencil view", L"Depth Buffer Initialization Error", MB_OK);
return false;
}
return true;
}
/// <summary>
/// Initialize Direct3D device and context
/// </summary>
/// <returns>Initialization success</returns>
bool Renderer::InitializeDirect3D() {
// Device
if (!InitializeDevice()) return false;
// Depth buffer
if (!InitializeDepthBuffer()) return false;
// Render target view
if (!InitializeRenderTargetView()) return false;
// Viewport
if (!InitializeViewport()) return false;
// Shaders
if (!InitializeShaders()) return false;
// Success
return true;
}
/// <summary>
/// Initialize D3D device
/// </summary>
/// <returns>Initialization state</returns>
bool Renderer::InitializeDevice() {
// Set D3D supported feature levels for this app
D3D_FEATURE_LEVEL featureLevels[] = {
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_11_1
};
UINT numFeatureLevels = ARRAYSIZE(featureLevels);
// Set device support flags
UINT deviceFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
#if defined(DEBUG) || defined(_DEBUG)
deviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
// Configure swap chain parameters
DXGI_SWAP_CHAIN_DESC swapChainParams;
ZeroMemory(&swapChainParams, sizeof(DXGI_SWAP_CHAIN_DESC));
swapChainParams.Windowed = TRUE;
swapChainParams.BufferCount = 2;
swapChainParams.BufferDesc.Width = _windowWidth;
swapChainParams.BufferDesc.Height = _windowHeight;
swapChainParams.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
swapChainParams.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainParams.SampleDesc.Count = 1;
swapChainParams.SampleDesc.Quality = 0;
swapChainParams.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
swapChainParams.OutputWindow = _outputWindow;
// Create D3D device, context and swap chain
HRESULT hr;
hr = D3D11CreateDeviceAndSwapChain(
nullptr, // Use the default adapter
D3D_DRIVER_TYPE_HARDWARE, // Use the hardware driver
0, // Not using software
deviceFlags, // Debug and Direct2D compatibility flags
featureLevels, // Supported feature levels
numFeatureLevels, // Size of the feature levels array
D3D11_SDK_VERSION, // D3D SDK version
&swapChainParams, // Swap chain parameters
&_swapChain, // Swap chain
&_device, // Return created D3D device
&_deviceFeatureLevel, // Return selected feature level
&_deviceContext // Return created D3D device context
);
// Check for errors
if (FAILED(hr)) {
return false;
}
// Success
return true;
}
/// <summary>
/// Initialize render target view
/// </summary>
/// <returns>Initialization success</returns>
bool Renderer::InitializeRenderTargetView() {
// Get back buffer from swap chain
ID3D11Resource* backBuffer;
HRESULT hr = _swapChain->GetBuffer(0, __uuidof(backBuffer), (void**)&backBuffer);
assert(!FAILED(hr));
// Create render target view
hr = _device->CreateRenderTargetView(backBuffer, NULL, &_renderTargetView);
assert(!FAILED(hr));
// Release the ref count on the back buffer (from GetBuffer)
backBuffer->Release();
return true;
}
/// <summary>
/// Initialize vertex and pixel shaders
/// </summary>
/// <returns>Initialization success</returns>
bool Renderer::InitializeShaders() {
HRESULT hr;
// Set shader parameters
LPCWSTR vsFilename = L"Shaders\\VertexShader.hlsl";
LPCWSTR psFilename = L"Shaders\\PixelShader.hlsl";
///////////////////////////////////////
// Vertex Shader
// Compile vertex shader from file
ID3DBlob* compiledVS = CompileShaderFromFile(vsFilename, VS_COMPILER_TARGET);
if (compiledVS == nullptr) {
MessageBox(nullptr, L"Error compiling vertex shader", L"Shader Initialization Error", MB_OK);
return false;
}
// Create shader from compiled code
hr = _device->CreateVertexShader(compiledVS->GetBufferPointer(), compiledVS->GetBufferSize(), nullptr, &_vertexShader);
if (FAILED(hr)) {
if (compiledVS) compiledVS->Release();
MessageBox(nullptr, L"Error creating vertex shader", L"Shader Initialization Error", MB_OK);
return false;
}
///////////////////////////////////////
// Pixel Shader
// Compile pixel shader from file
ID3DBlob* compiledPS = CompileShaderFromFile(psFilename, PS_COMPILER_TARGET);
if (compiledPS == nullptr) {
MessageBox(nullptr, L"Error compiling pixel shader", L"Shader Initialization Error", MB_OK);
return false;
}
// Create shader from compiled code
hr = _device->CreatePixelShader(compiledPS->GetBufferPointer(), compiledPS->GetBufferSize(), nullptr, &_pixelShader);
if (FAILED(hr)) {
if (compiledVS) compiledVS->Release();
if (compiledPS) compiledPS->Release();
MessageBox(nullptr, L"Error creating pixel shader", L"Shader Initialization Error", MB_OK);
return false;
}
///////////////////////////////////////
// Input Layout
// Define shader input layout
D3D11_INPUT_ELEMENT_DESC layoutDescription[] = {
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
// { "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
UINT numLayoutElements = ARRAYSIZE(layoutDescription);
// Create input layout
hr = _device->CreateInputLayout(layoutDescription, numLayoutElements, compiledVS->GetBufferPointer(), compiledVS->GetBufferSize(), &_inputLayout);
if (FAILED(hr)) {
if (compiledVS) compiledVS->Release();
if (compiledPS) compiledPS->Release();
return false;
}
// Set input layout for shaders
_deviceContext->IASetInputLayout(_inputLayout);
// Release resources
if (compiledVS) compiledVS->Release();
if (compiledPS) compiledPS->Release();
return true;
}
/// <summary>
/// Initialize viewport
/// </summary>
/// <returns>Initialization success</returns>
bool Renderer::InitializeViewport() {
// Initialize viewport parameters
ZeroMemory(&_viewport, sizeof(D3D11_VIEWPORT));
_viewport.Width = (float)_windowWidth;
_viewport.Height = (float)_windowHeight;
_viewport.MinDepth = 0;
_viewport.MaxDepth = 1;
// Set viewport
_deviceContext->RSSetViewports(1, &_viewport);
return true;
}
/// <summary>
/// Initialize vertex, pixel and constant buffers
/// </summary>
/// <returns>Initialization success</returns>
bool Renderer::InitializeBuffers() {
HRESULT hr;
///////////////////////////////////////
// Vertex Buffer
// Describe vertex buffer description and initialization data
D3D11_BUFFER_DESC bufferDescription;
ZeroMemory(&bufferDescription, sizeof(D3D11_BUFFER_DESC));
bufferDescription.Usage = D3D11_USAGE_DEFAULT;
bufferDescription.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bufferDescription.ByteWidth = sizeof(VERTEX) * _vertices.size();
// Create vertex buffer
D3D11_SUBRESOURCE_DATA vertexInitData;
ZeroMemory(&vertexInitData, sizeof(D3D11_SUBRESOURCE_DATA));
vertexInitData.pSysMem = &_vertices[0];
hr = _device->CreateBuffer(&bufferDescription, &vertexInitData, &_vertexBuffer);
if (FAILED(hr)) return false;
// Set vertex buffer (Input-Assembler stage)
UINT stride = sizeof(VERTEX);
UINT offset = 0;
_deviceContext->IASetVertexBuffers(0, 1, &_vertexBuffer, &stride, &offset);
///////////////////////////////////////
// Index Buffer
// Configure index buffer description
bufferDescription.Usage = D3D11_USAGE_DEFAULT;
bufferDescription.ByteWidth = sizeof(WORD) * _indices.size();
bufferDescription.BindFlags = D3D11_BIND_INDEX_BUFFER;
bufferDescription.CPUAccessFlags = 0;
// Configure index buffer initialization data
D3D11_SUBRESOURCE_DATA indexInitData;
ZeroMemory(&indexInitData, sizeof(D3D11_SUBRESOURCE_DATA));
indexInitData.pSysMem = &_indices[0];
// Create index buffer
hr = _device->CreateBuffer(&bufferDescription, &indexInitData, &_indexBuffer);
if (FAILED(hr)) return false;
// Set index buffer
_deviceContext->IASetIndexBuffer(_indexBuffer, DXGI_FORMAT_R16_UINT, 0);
// Configure primitive topology
_deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
///////////////////////////////////////
// Constant Buffers
// Create vertex shader constant buffer
CD3D11_BUFFER_DESC vsConstantBufferDesc(sizeof(_vsConstantBufferData), D3D11_BIND_CONSTANT_BUFFER);
hr = _device->CreateBuffer(&vsConstantBufferDesc, nullptr, &_vsConstantBuffer);
if (FAILED(hr)) {
return false;
}
// Create pixel shader constant buffer
CD3D11_BUFFER_DESC psConstantBufferDesc(sizeof(_psConstantBufferData), D3D11_BIND_CONSTANT_BUFFER);
hr = _device->CreateBuffer(&psConstantBufferDesc, nullptr, &_psConstantBuffer);
if (FAILED(hr)) {
return false;
}
return true;
}
/// <summary>
/// Initialize and store object transformations
/// </summary>
/// <returns></returns>
bool Renderer::InitializeObjectTransforms() {
// Initialize object translation
_modelMatrix = DirectX::XMMatrixTranslation(0.0f, 0.0f, 0.0f);
// Get object dimensions
float objectWidth = GetObjectWidth();
// Calculate required view distance
_viewZ = (objectWidth / 2.0f) / tan(45.0f) * -15.0f;
return true;
}
/// <summary>
/// Initialize lights
/// </summary>
/// <returns></returns>
bool Renderer::InitializeLights() {
// Store ambient light
_psConstantBufferData.ambient = { 0.2f, 0.2f, 0.2f, 1.0f };
// Store light position
_psConstantBufferData.lightPosition = DirectX::XMFLOAT3(10.0f, 0.0f, 10.0f);
return true;
}
/// <summary>
/// Compile shader from source file
/// </summary>
/// <param name="shaderPathname">Pathname of HLSL source file</param>
/// <param name="compilerTarget">Compiler target string</param>
/// <returns>Compiled shader</returns>
ID3DBlob* Renderer::CompileShaderFromFile(LPCWSTR shaderPathname, LPCSTR compilerTarget) {
ID3DBlob* compiledShader = nullptr;
ID3DBlob* compilationErrors;
// Set compile flags
// Set compilation flags
#if _DEBUG
UINT compileFlags = D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
#else
UINT compileFlags = 0;
#endif
// Compile shader from file
HRESULT hr = D3DCompileFromFile(shaderPathname, nullptr, nullptr, SHADER_ENTRY_POINT, compilerTarget, compileFlags, 0, &compiledShader, &compilationErrors);
if (FAILED(hr)) {
// Compilation error
OutputDebugString((LPCWSTR)compilationErrors->GetBufferPointer());
// Release resources
if (compiledShader) compiledShader->Release();
}
// Release resources
if (compilationErrors) compilationErrors->Release();
return compiledShader;
}
/// <summary>
/// Get width of object from vertex data
/// </summary>
/// <returns>Object width in units</returns>
float Renderer::GetObjectWidth() {
// Initialize min and max dimensions
float minX = _vertices[0].pos.x;
float maxX = minX;
// Check all vertices
for (VERTEX& vertex : _vertices) {
// Update min and max
if (vertex.pos.x < minX) {
minX = vertex.pos.x;
}
else if (vertex.pos.x > maxX) {
maxX = vertex.pos.x;
}
}
return maxX - minX;
}