-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameObject.cpp
160 lines (126 loc) · 3.93 KB
/
GameObject.cpp
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
#include "GameObject.h"
#include "d3dUtil.h"
#include "DXTrace.h"
using namespace DirectX;
struct InstancedData
{
XMMATRIX world;
XMMATRIX worldInvTranspose;
};
Transform& GameObject::GetTransform()
{
return m_Transform;
}
const Transform& GameObject::GetTransform() const
{
return m_Transform;
}
BoundingBox GameObject::GetBoundingBox() const
{
BoundingBox box;
m_Model.boundingBox.Transform(box, m_Transform.GetLocalToWorldMatrixXM());
return box;
}
BoundingOrientedBox GameObject::GetBoundingOrientedBox() const
{
BoundingOrientedBox box;
BoundingOrientedBox::CreateFromBoundingBox(box, m_Model.boundingBox);
box.Transform(box, m_Transform.GetLocalToWorldMatrixXM());
return box;
}
BoundingBox GameObject::GetLocalBoundingBox() const
{
return m_Model.boundingBox;
}
size_t GameObject::GetCapacity() const
{
return m_Capacity;
}
void GameObject::ResizeBuffer(ID3D11Device * device, size_t count)
{
// 设置实例缓冲区描述
D3D11_BUFFER_DESC vbd;
ZeroMemory(&vbd, sizeof(vbd));
vbd.Usage = D3D11_USAGE_DYNAMIC;
vbd.ByteWidth = (UINT)count * sizeof(InstancedData);
vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
// 创建实例缓冲区
HR(device->CreateBuffer(&vbd, nullptr, m_pInstancedBuffer.ReleaseAndGetAddressOf()));
// 重新调整m_Capacity
m_Capacity = count;
}
void GameObject::SetModel(Model && model)
{
std::swap(m_Model, model);
model.modelParts.clear();
model.boundingBox = BoundingBox();
}
void GameObject::SetModel(const Model & model)
{
m_Model = model;
}
void GameObject::Draw(ID3D11DeviceContext * deviceContext, BasicEffect & effect)
{
UINT strides = m_Model.vertexStride;
UINT offsets = 0;
for (auto& part : m_Model.modelParts)
{
// 设置顶点/索引缓冲区
deviceContext->IASetVertexBuffers(0, 1, part.vertexBuffer.GetAddressOf(), &strides, &offsets);
deviceContext->IASetIndexBuffer(part.indexBuffer.Get(), part.indexFormat, 0);
// 更新数据并应用
effect.SetWorldMatrix(m_Transform.GetLocalToWorldMatrixXM());
effect.SetTextureDiffuse(part.texDiffuse.Get());
effect.SetMaterial(part.material);
effect.Apply(deviceContext);
deviceContext->DrawIndexed(part.indexCount, 0, 0);
}
}
void GameObject::DrawInstanced(ID3D11DeviceContext* deviceContext, BasicEffect& effect, const std::vector<Transform>& data)
{
D3D11_MAPPED_SUBRESOURCE mappedData;
UINT numInsts = (UINT)data.size();
// 若传入的数据比实例缓冲区还大,需要重新分配
if (numInsts > m_Capacity)
{
ComPtr<ID3D11Device> device;
deviceContext->GetDevice(device.GetAddressOf());
ResizeBuffer(device.Get(), numInsts);
}
HR(deviceContext->Map(m_pInstancedBuffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedData));
InstancedData* iter = reinterpret_cast<InstancedData*>(mappedData.pData);
for (auto& t : data)
{
XMMATRIX W = t.GetLocalToWorldMatrixXM();
iter->world = XMMatrixTranspose(W);
iter->worldInvTranspose = XMMatrixTranspose(InverseTranspose(W));
iter++;
}
deviceContext->Unmap(m_pInstancedBuffer.Get(), 0);
UINT strides[2] = { m_Model.vertexStride, sizeof(InstancedData) };
UINT offsets[2] = { 0, 0 };
ID3D11Buffer* buffers[2] = { nullptr, m_pInstancedBuffer.Get() };
for (auto& part : m_Model.modelParts)
{
buffers[0] = part.vertexBuffer.Get();
// 设置顶点/索引缓冲区
deviceContext->IASetVertexBuffers(0, 2, buffers, strides, offsets);
deviceContext->IASetIndexBuffer(part.indexBuffer.Get(), part.indexFormat, 0);
// 更新数据并应用
effect.SetTextureDiffuse(part.texDiffuse.Get());
effect.SetMaterial(part.material);
effect.Apply(deviceContext);
deviceContext->DrawIndexedInstanced(part.indexCount, numInsts, 0, 0, 0);
}
}
void GameObject::SetDebugObjectName(const std::string& name)
{
#if (defined(DEBUG) || defined(_DEBUG)) && (GRAPHICS_DEBUGGER_OBJECT_NAME)
m_Model.SetDebugObjectName(name);
if (m_pInstancedBuffer)
{
D3D11SetDebugObjectName(m_pInstancedBuffer.Get(), name + ".InstancedBuffer");
}
#endif
}