{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObject.cpp
More file actions
493 lines (410 loc) · 11 KB
/
Copy pathObject.cpp
File metadata and controls
493 lines (410 loc) · 11 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
#include "Builder.h"
void Vertex_t::SetPos (float x_,
float y_,
float z_)
{
x = x_;
y = y_;
z = z_;
}
#ifndef IGNORE_VERTEX_NORMAL
void Vertex_t::SetNormal (float nx_,
float ny_,
float nz_)
{
nx = nx_;
ny = ny_;
nz = nz_;
}
#endif
#ifndef IGNORE_VERTEX_TEXTURE
void Vertex_t::SetTexture (float u_,
float v_,
float w_)
{
u = u_;
v = v_;
w = w_;
}
#endif
#ifndef IGNORE_VERTEX_COLOR
void Vertex_t::SetColor (float r_,
float g_,
float b_,
float a_)
{
r = r_;
g = g_;
b = b_;
a = a_;
}
#endif
void Direct3DObject::ok ()
{
DEFAULT_OK_BLOCK
}
Direct3DObject::Direct3DObject (XMMATRIX& world,
bool blending,
bool drawIndexed,
D3D11_PRIMITIVE_TOPOLOGY topology) :
vertices_ (),
indices_ (),
topology_ (topology),
drawIndexed_ (drawIndexed),
objectId_ (),
blending_ (blending),
currM_ ({ { XMMatrixIdentity (), XMMatrixIdentity () } , world }),
vertexBuffer_ (nullptr),
indexBuffer_ (nullptr),
cbManager_ (nullptr),
objectBufferN_ (0),
buffersSet_ (false),
objectBufferSet_ (false),
vertexShader_ (-1),
pixelShader_ (-1),
geometryShader_ (-1),
layoutN_ (-1),
draw_ ()
{
InitializeCriticalSection (&draw_);
}
void Direct3DObject::SetCBManager (Direct3DConstantBufferManager* cbManager)
{
cbManager_ = cbManager;
}
Direct3DObject::~Direct3DObject ()
{
vertices_.clear ();
indices_.clear ();
objectId_ = 0;
if (buffersSet_)
{
vertexBuffer_->Release ();
vertexBuffer_ = nullptr;
if (drawIndexed_)
{
indexBuffer_->Release ();
indexBuffer_ = nullptr;
}
}
DeleteCriticalSection (&draw_);
}
void Direct3DObject::SetID (uint64_t objectId)
{
BEGIN_EXCEPTION_HANDLING
objectId_ = objectId;
END_EXCEPTION_HANDLING (SET_ID)
}
void Direct3DObject::AddVertexArray (Vertex_t* vert, size_t n)
{
BEGIN_EXCEPTION_HANDLING
/*if (buffersSet_)
_EXC_N (BUFFERS_SET, "D3D: Cannot add vertices to buffered object")*/
vertices_.insert (vertices_.begin (), vert, vert + n);
END_EXCEPTION_HANDLING (ADD_VERTEX_ARRAY)
}
void Direct3DObject::ClearVertexArray ()
{
BEGIN_EXCEPTION_HANDLING
vertices_.clear ();
END_EXCEPTION_HANDLING (ADD_VERTEX_ARRAY)
}
void Direct3DObject::AddIndexArray (UINT* ind, size_t n)
{
BEGIN_EXCEPTION_HANDLING
/*if (buffersSet_)
_EXC_N (BUFFERS_SET, "D3D: Cannot add indices to buffered object")*/
indices_.insert (indices_.begin (), ind, ind + n);
END_EXCEPTION_HANDLING (ADD_INDEX_ARRAY)
}
void Direct3DObject::SetupBuffers (ID3D11Device* device)
{
BEGIN_EXCEPTION_HANDLING
if (buffersSet_) ClearBuffers ();
HRESULT result = S_OK;
SetVertexBuffer (device);
SetIndexBuffer (device);
SetObjectBuffer (device);
buffersSet_ = true;
END_EXCEPTION_HANDLING (SETUP_BUFFERS)
}
/*
void Direct3DObject::SetWVP (XMMATRIX& matrix)
{
currM_.objData_.WVP = matrix;
}*/
void Direct3DObject::SetWorld (XMMATRIX& matrix)
{
currM_.objData_.World = matrix;
}
XMMATRIX& Direct3DObject::GetWorld ()
{
return currM_.world_;
}
void Direct3DObject::Draw (ID3D11DeviceContext* deviceContext,
Direct3DCamera* cam)
{
BEGIN_EXCEPTION_HANDLING
EnterCriticalSection ();
//printf ("%d\n", vertices_.size());
uint16_t i = rand () % vertices_.size ();
//printf ("%f %f %f\n", vertices_[i].x, vertices_[i].y, vertices_[i].z);
if (buffersSet_ == false)
_EXC_N (BUFFERS_NOT_SET,
"D3D: Unable to draw with the buffers not set (obj %d)" _
objectId_)
if (vertices_.size () == 0)
_EXC_N (EMPTY_VERTICES,
"D3D: Unable to draw object with empty vertex buffer (obj %d)" _
objectId_)
UINT stride = sizeof (Vertex_t);
UINT offset = 0;
deviceContext->IASetVertexBuffers (0, 1, &vertexBuffer_, &stride, &offset);
if (drawIndexed_)
{
if (indices_.size () == 0)
_EXC_N (EMPTY_INDICES,
"D3D: Unable to draw object with empty index buffer (obj %d)" _
objectId_)
deviceContext->IASetIndexBuffer (indexBuffer_, DXGI_FORMAT_R32_UINT, 0);
}
XMVECTOR temp;
currM_.objData_.World = XMMatrixTranspose (currM_.world_);
currM_.objData_.View = XMMatrixTranspose (cam->GetView());
currM_.objData_.Projection = XMMatrixTranspose (cam->GetProjection());
currM_.objData_.InverseView = XMMatrixTranspose (XMMatrixInverse (&temp, cam->GetView ()));
currM_.objData_.InverseProjection = XMMatrixTranspose (XMMatrixInverse(&temp, cam->GetProjection ()));
cbManager_->Update (objectBufferN_, deviceContext);
cbManager_->SendVSBuffer (objectBufferN_, deviceContext);
cbManager_->SendGSBuffer (objectBufferN_, deviceContext);
cbManager_->SendPSBuffer (objectBufferN_, deviceContext);
deviceContext->IASetPrimitiveTopology (topology_);
if (drawIndexed_)
deviceContext->DrawIndexed (static_cast<UINT> (indices_.size ()), 0, 0);
else
deviceContext->Draw (static_cast<UINT> (vertices_.size ()), 0);
ExitCriticalSection ();
END_EXCEPTION_HANDLING (DRAW_OBJECT)
}
void Direct3DObject::SaveLayout (LayoutIndex_t n)
{
layoutN_ = n;
}
void Direct3DObject::ClearBuffers ()
{
BEGIN_EXCEPTION_HANDLING
if (!buffersSet_) return;
//_EXC_N (BUFFERS_NOT_SET, "D3D: Unable to clear empty buffers (obj %d)" _
// objectId_)
buffersSet_ = false;
if (vertexBuffer_)
vertexBuffer_->Release ();
vertexBuffer_ = nullptr;
if (indexBuffer_)
indexBuffer_->Release ();
indexBuffer_ = nullptr;
END_EXCEPTION_HANDLING (CLEAR_BUFFERS)
}
void Direct3DObject::SetVertexBuffer (ID3D11Device* device)
{
BEGIN_EXCEPTION_HANDLING
//printf ("Setting\n");
if (vertexBuffer_ != nullptr)
{
vertexBuffer_->Release ();
vertexBuffer_ = nullptr;
}
HRESULT result = S_OK;
//printf ("vertex A\n");
if (vertices_.size () == 0)
_EXC_N (EMPTY_VERTICES, "D3D: Cannot create empty vertex buffer (obj %d)" _ objectId_)
D3D11_BUFFER_DESC bufferDesc = {};
bufferDesc.Usage = D3D11_USAGE_DYNAMIC;
bufferDesc.ByteWidth = static_cast<UINT> (sizeof (Vertex_t) * vertices_.size ());
bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
D3D11_SUBRESOURCE_DATA vertexSd = { vertices_.data () };
result = device->CreateBuffer (&bufferDesc,
&vertexSd,
&vertexBuffer_);
//printf ("vertex B\n");
if (result != S_OK)
_EXC_N (VERTEX_BUFFER, "D3D: Failed to create vertex buffer (obj %d)" _ objectId_)
END_EXCEPTION_HANDLING (SET_VERTEX_BUFFER)
}
void Direct3DObject::SetIndexBuffer (ID3D11Device* device)
{
BEGIN_EXCEPTION_HANDLING
if (drawIndexed_ && indices_.size () != 0)
{
if (indexBuffer_ != nullptr)
{
indexBuffer_->Release ();
indexBuffer_ = nullptr;
}
D3D11_BUFFER_DESC bufferDesc = {};
HRESULT result = S_OK;
bufferDesc.Usage = D3D11_USAGE_DEFAULT;
bufferDesc.ByteWidth = static_cast<UINT> (indices_.size () * sizeof (UINT));
bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA indexSd = { indices_.data () };
result = device->CreateBuffer (&bufferDesc,
&indexSd,
&indexBuffer_);
//printf ("index A\n");
if (result != S_OK)
_EXC_N (INDEX_BUFFER, "D3D: Failed to create index buffer (obj %d)" _ objectId_)
}
//printf ("index B\n");
if (drawIndexed_ && indices_.size () == 0)
_EXC_N (EMPTY_INDICES, "D3D: Cannot create empty index buffer (obj %d)" _ objectId_)
END_EXCEPTION_HANDLING (SET_INDEX_BUFFER)
}
void Direct3DObject::SetObjectBuffer (ID3D11Device* device)
{
BEGIN_EXCEPTION_HANDLING
//printf ("object A\n");
if (!cbManager_)
_EXC_N (NULL_CB_MANAGER, "D3D: Cannot set up object buffer without buffer manager (obj %d)" _ objectId_)
//printf ("object B\n");
if (objectBufferSet_) return;
//_EXC_N (OBJECT_BUFFER_SET, "D3D: Object buffer has already been set (obj %d)" _ objectId_)
//printf ("object C\n");
objectBufferN_ = cbManager_->Bind (&currM_.objData_,
sizeof (Direct3DObjectBuffer),
0,
device);
objectBufferSet_ = true;
END_EXCEPTION_HANDLING (SET_OBJECT_BUFFER)
}
void Direct3DObject::EnterCriticalSection ()
{
::EnterCriticalSection (&draw_);
}
void Direct3DObject::ExitCriticalSection ()
{
::LeaveCriticalSection (&draw_);
}
std::vector<Vertex_t>& Direct3DObject::GetVertices ()
{
return vertices_;
}
uint64_t Direct3DObject::GetID()
{
return objectId_;
}
void Direct3DCamera::ok ()
{
DEFAULT_OK_BLOCK
if (wnd_ == nullptr)
_EXC_N (NULL_WND, "D3D: Null window");
}
Direct3DCamera::Direct3DCamera (WindowClass* wnd,
float posX, float posY, float posZ,
float tgtX, float tgtY, float tgtZ,
float upX, float upY, float upZ,
float fov, float zNear, float zFar)
try :
wnd_ (wnd),
position_ (XMVectorSet (posX, posY, posZ, 0.0f)),
direction_ (XMVectorSet (tgtX, tgtY, tgtZ, 0.0f)),
up_ (XMVectorSet (upX, upY, upZ, 0.0f)),
projectionSettings_ (fov, zNear, zFar),
view_ (),
projection_ (XMMatrixIdentity ())
{
if (wnd == nullptr)
_EXC_N (NULL_WND,
"D3D: Cannot create camera over null window");
Update ();
}
_END_EXCEPTION_HANDLING (CTOR)
void Direct3DCamera::Update ()
{
BEGIN_EXCEPTION_HANDLING
XMVECTOR target_ = position_ + direction_;
view_ = XMMatrixLookAtLH (position_, target_, up_);
projection_ = XMMatrixPerspectiveFovLH (projectionSettings_.x * 3.141592f,
((float)wnd_->width ()) / wnd_->height (),
projectionSettings_.y,
projectionSettings_.z);
END_EXCEPTION_HANDLING (UPDATE_CAMERA)
}
const XMMATRIX& Direct3DCamera::GetView () const
{
return view_;
}
const XMMATRIX& Direct3DCamera::GetProjection () const
{
return projection_;
}
void Direct3DCamera::TranslatePos (XMFLOAT3 move)
{
XMVECTOR movement = XMLoadFloat3 (&move);
position_ += movement;
direction_ += movement;
}
void Direct3DCamera::TranslatePos (XMVECTOR move)
{
position_ += move;
//direction_ += move;
}
void Direct3DCamera::RotateDir (float hor, float ver)
{
direction_ = XMVector4Transform (direction_, XMMatrixRotationAxis (up_, hor));
direction_ = XMVector4Transform (direction_, XMMatrixRotationAxis (XMVector3Cross (up_, direction_), ver));
}
void Direct3DCamera::MoveForward (float d)
{
TranslatePos (XMVector3Normalize (direction_) * d);
}
void Direct3DCamera::MoveBackward (float d)
{
TranslatePos (-XMVector3Normalize (direction_) * d);
}
void Direct3DCamera::MoveRight (float d)
{
TranslatePos (-XMVector3Normalize (XMVector3Cross (direction_, up_)) * d);
}
void Direct3DCamera::MoveLeft (float d)
{
TranslatePos (XMVector3Normalize (XMVector3Cross (direction_, up_)) * d);
}
void Direct3DCamera::RotateHorizontal (float a)
{
RotateDir (a, 0.0f);
}
void Direct3DCamera::RotateVertical (float a)
{
RotateDir (0.0f, -a);
}
void Direct3DCamera::StorePos (XMFLOAT4& pos) const
{
XMStoreFloat4 (&pos, position_);
}
void Direct3DCamera::SetPos (XMFLOAT4 pos)
{
position_ = XMLoadFloat4 (&pos);
}
void Direct3DCamera::SetDir (XMFLOAT4 dir)
{
direction_ = XMLoadFloat4 (&dir);
}
XMVECTOR & Direct3DCamera::GetDir ()
{
return direction_;
}
XMVECTOR & Direct3DCamera::GetPos ()
{
return position_;
}
float& Direct3DCamera::GetFOV ()
{
return projectionSettings_.x;
}
void* GetValidObjectPtr ()
{
return _aligned_malloc (sizeof (Direct3DObject), 16);
}
You can’t perform that action at this time.
