-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntity.cpp
230 lines (191 loc) · 5.45 KB
/
Entity.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
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
#include "Entity.h"
void Entity::Create(float radius, float mass, ID3D11ShaderResourceView* texture, XMFLOAT3 position, XMFLOAT2 velocity)
{
_radius = radius;
_mass = mass;
_texture = texture;
_enableColors = true;
this->SetPosition(position);
this->SetVelocity(velocity);
}
bool Entity::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, ConstantBuffer<CB_VS_vertexshader>& cb_vs_vertexshader, ConstantBuffer<CB_PS_pixelshader>& cb_ps_pixelshader)
{
_dev = device;
_devcon = deviceContext;
_cb_vs_vertexshader = &cb_vs_vertexshader;
_cb_ps_pixelshader = &cb_ps_pixelshader;
// create a square using the VERTEX struct
Vertex v[] =
{
Vertex(-_radius, -_radius, 0.0f, 0.0f, 1.0f), // Bottom Left
Vertex(-_radius, _radius, 0.0f, 0.0f, 0.0f), // Top Left
Vertex(_radius, _radius, 0.0f, 1.0f, 0.0f), // Top Right
Vertex(-_radius, -_radius, 0.0f, 0.0f, 1.0f), // Bottom Left
Vertex(_radius, _radius, 0.0f, 1.0f, 0.0f), // Top Right
Vertex(_radius, -_radius, 0.0f, 1.0f, 1.0f), // Bottom Right
};
HRESULT hr;
hr = _vertexBuffer.Initialize(_dev, v, ARRAYSIZE(v)); // create the buffer
if (FAILED(hr))
{
ErrorLogger::Log(hr, "Failed to create vertex buffer.");
return false;
}
DWORD indices[] =
{
0,1,2,3,4,5
};
hr = _indexBuffer.Initialize(_dev, indices, ARRAYSIZE(indices));
if (FAILED(hr))
{
ErrorLogger::Log(hr, "Failed to create index buffer.");
return false;
}
this->UpdateWorldMatrix();
return true;
}
void Entity::Draw(const XMMATRIX& viewProjectionMatrix)
{
// Update constant buffers
_cb_vs_vertexshader->_data.mat = _worldMatrix * viewProjectionMatrix;
_cb_vs_vertexshader->_data.mat = XMMatrixTranspose(_cb_vs_vertexshader->_data.mat);
if (!_cb_vs_vertexshader->ApplyChanges())
return;
_devcon->VSSetConstantBuffers(0, 1, _cb_vs_vertexshader->GetAddressOf());
float magnitude = sqrtf((_velocity.x * _velocity.x) + (_velocity.y * _velocity.y)); // velocity vector module
static float max = 0.1;
if (magnitude > max && magnitude < 2)
max = magnitude;
_cb_ps_pixelshader->_data.v_magnitude = magnitude / max; // pass normalized velocity value to pixel shader
_cb_ps_pixelshader->_data.r_mod = _colorMod.r;
_cb_ps_pixelshader->_data.g_mod = _colorMod.g;
_cb_ps_pixelshader->_data.b_mod = _colorMod.b;
_cb_ps_pixelshader->_data.enableColor = _enableColors;
_cb_ps_pixelshader->ApplyChanges();
_devcon->PSSetConstantBuffers(0, 1, _cb_ps_pixelshader->GetAddressOf());
_devcon->PSSetShaderResources(0, 1, &_texture); // set texture
_devcon->IASetIndexBuffer(_indexBuffer.Get(), DXGI_FORMAT_R32_UINT, 0);
UINT offset = 0;
_devcon->IASetVertexBuffers(0, 1, _vertexBuffer.GetAddressOf(), _vertexBuffer.StridePtr(), &offset);
_devcon->DrawIndexed(_indexBuffer.BufferSize(), 0, 0);
}
void Entity::UpdateWorldMatrix()
{
_worldMatrix = DirectX::XMMatrixTranslation(_pos.x, _pos.y, _pos.z);
}
void Entity::Release()
{
if (_vertexBuffer.GetAddressOf()) _vertexBuffer.Release();
if (_indexBuffer.GetAddressOf()) _indexBuffer.Release();
}
const XMVECTOR& Entity::GetPositionVector() const
{
return _posVector;
}
const XMFLOAT3& Entity::GetPositionFloat3() const
{
return _pos;
}
void Entity::SetPosition(const XMVECTOR& pos)
{
XMStoreFloat3(&_pos, pos);
_posVector = pos;
this->UpdateWorldMatrix();
}
void Entity::SetPosition(const XMFLOAT3& pos)
{
_pos = pos;
_posVector = XMLoadFloat3(&_pos);
this->UpdateWorldMatrix();
}
void Entity::SetPosition(float x, float y, float z)
{
_pos = XMFLOAT3(x, y, z);
_posVector = XMLoadFloat3(&_pos);
this->UpdateWorldMatrix();
}
void Entity::AdjustPosition(const XMVECTOR& pos)
{
_posVector += pos;
XMStoreFloat3(&_pos, pos);
this->UpdateWorldMatrix();
}
void Entity::AdjustPosition(const XMFLOAT3& pos)
{
_pos.x += pos.x;
_pos.y += pos.y;
_pos.z += pos.z;
_posVector += XMLoadFloat3(&pos);
this->UpdateWorldMatrix();
}
void Entity::AdjustPosition(float x, float y, float z)
{
_pos.x += x;
_pos.y += y;
_pos.z += z;
_posVector += XMLoadFloat3(&_pos);
this->UpdateWorldMatrix();
}
void Entity::AdjustPosition()
{
this->AdjustPosition(_velocity.x, _velocity.y, 0.0f);
}
const XMFLOAT2& Entity::GetVelocityFloat2() const
{
return _velocity;
}
void Entity::UpdateVelocity(float newXVelocity, float newYVelocity)
{
_velocity.x += newXVelocity;
_velocity.y += newYVelocity;
_velocityVector += XMLoadFloat2(&_velocity);
}
void Entity::SetVelocity(const XMFLOAT2& velocity)
{
_velocity.x = velocity.x;
_velocity.y = velocity.y;
_velocityVector = XMLoadFloat2(&_velocity);
}
void Entity::SetVelocity(float x, float y)
{
_velocity.x = x;
_velocity.y = y;
}
float Entity::GetRadius() const
{
return _radius;
}
void Entity::SetRadius(float radius)
{
_radius = radius;
}
void Entity::UpdateRadius(float radius)
{
_radius += radius;
}
float Entity::GetMass() const
{
return _mass;
}
void Entity::SetMass(float mass)
{
_mass = mass;
}
void Entity::UpdateMass(float mass)
{
_mass += mass;
}
EColor Entity::GetColorModifiers() const
{
return _colorMod;
}
void Entity::SetColorModifiers(double r, double g, double b)
{
_colorMod.r = r;
_colorMod.g = g;
_colorMod.b = b;
}
void Entity::SetColorFeature(bool newStatus)
{
_enableColors = newStatus;
}