-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameObject.cpp
109 lines (82 loc) · 2.51 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
#include "GameObject.h"
GameObject::GameObject() :rotation_(0.0f, 0.0f, 0.0f), scale_(1.0f, 1.0f, 1.0f), position_(0.0f, 0.0f, 0.0f)
{
}
bool GameObject::Initialize(XMFLOAT3 Position, XMFLOAT3 Rotation, XMFLOAT3 Scale)
{
SetPosition(Position);
SetRotation(Rotation);
SetScale(Scale);
return true;
}
bool GameObject::InitWorldCB(ID3D11Device* device)
{
HRESULT result;
//Constant buffers description
D3D11_BUFFER_DESC constDesc;
ZeroMemory(&constDesc, sizeof(constDesc));
constDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
constDesc.ByteWidth = sizeof(XMMATRIX);
constDesc.Usage = D3D11_USAGE_DEFAULT;
result = device->CreateBuffer(&constDesc, 0, &m_RotateCB);
if (FAILED(result))
{
MessageBox(NULL, "Failed to create world const buffer", NULL, NULL);
return false;
}
constDesc.ByteWidth = sizeof(WorldViewProj);
result = device->CreateBuffer(&constDesc, 0, &m_WorldViewProjCB);
if (FAILED(result))
{
MessageBox(NULL, "Failed to create matrices const buffer", NULL, NULL);
return false;
}
return true;
}
void GameObject::SetPosition(XMFLOAT3 position)
{
position_ = position;
};
void GameObject::SetRotation(XMFLOAT3 rotation)
{
rotation_ = rotation;
};
void GameObject::SetScale(XMFLOAT3 scale)
{
scale_ = scale;
};
XMMATRIX GameObject::GetWorldMatrix()
{
XMMATRIX translation = XMMatrixTranslation(position_.x, position_.y, position_.z);
rotationMat = XMMatrixRotationRollPitchYaw(rotation_.x, rotation_.y, rotation_.z);
XMMATRIX scale = XMMatrixScaling(scale_.x, scale_.y, scale_.z);
return rotationMat*scale*translation;
};
void GameObject::Update()
{
WorldViewProj_.world = GetWorldMatrix();
rotationMat = XMMatrixTranspose(rotationMat);
}
void GameObject::PrepareRender(DirectDevice* device, XMMATRIX& viewMat, XMMATRIX& projMat)
{
WorldViewProj_.world = XMMatrixTranspose(WorldViewProj_.world);
WorldViewProj_.view = XMMatrixTranspose(viewMat);
WorldViewProj_.project = XMMatrixTranspose(projMat);
//Update sub resources
device->d3dContext_->UpdateSubresource(m_RotateCB, 0, 0, &rotationMat, 0, 0);
device->d3dContext_->VSSetConstantBuffers(0, 1, &m_RotateCB);
//update domain shader resource
device->d3dContext_->UpdateSubresource(m_WorldViewProjCB, 0, 0, &WorldViewProj_, 0, 0);
device->d3dContext_->DSSetConstantBuffers(0, 1, &m_WorldViewProjCB);
}
void GameObject::Release()
{
if(m_RotateCB !=nullptr) m_RotateCB->Release();
m_RotateCB = 0;
if (m_WorldViewProjCB != nullptr) m_WorldViewProjCB->Release();
m_WorldViewProjCB = 0;
};
GameObject::~GameObject()
{
this->Release();
}