This repository was archived by the owner on Jun 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObject.cpp
More file actions
48 lines (37 loc) · 1.73 KB
/
GameObject.cpp
File metadata and controls
48 lines (37 loc) · 1.73 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
#include "GameObject.h"
GameObject::GameObject(string type, std::shared_ptr<Geometry> geometry, std::shared_ptr<Material> material)
{
_type = type;
//optionals
_parent = nullptr;
_physicsModel = nullptr;
_appearance = new Appearance(geometry, material);
_transform = new Transform();
}
GameObject::~GameObject()
{
_parent = nullptr;
if (_appearance) delete(_appearance);
if (_physicsModel) delete(_physicsModel);
if (_transform) delete(_transform);
}
void GameObject::Update(float t)
{
//update the physics
if (_physicsModel) _physicsModel->Update(t);
// Calculate world matrix
XMMATRIX scale = XMMatrixScaling(_transform->GetScale().x, _transform->GetScale().y, _transform->GetScale().z);
XMMATRIX rotation = XMMatrixRotationQuaternion(XMVectorSet(_transform->GetOrientation().GetVector().x, _transform->GetOrientation().GetVector().y, _transform->GetOrientation().GetVector().z, _transform->GetOrientation().n));
XMMATRIX translation = XMMatrixTranslation(_transform->GetPosition().x, _transform->GetPosition().y, _transform->GetPosition().z);
XMStoreFloat4x4(&_world, scale * rotation * translation);
if (_parent != nullptr) XMStoreFloat4x4(&_world, this->GetWorldMatrix() * _parent->GetWorldMatrix());
}
void GameObject::Draw(ID3D11DeviceContext * pImmediateContext)
{
// NOTE: We are assuming that the constant buffers and all other draw setup has already taken place
// Set vertex and index buffers
Geometry geometry = _appearance->GetGeometryData();
pImmediateContext->IASetVertexBuffers(0, 1, &geometry.vertexBuffer, &geometry.vertexBufferStride, &geometry.vertexBufferOffset);
pImmediateContext->IASetIndexBuffer(geometry.indexBuffer, DXGI_FORMAT_R16_UINT, 0);
pImmediateContext->DrawIndexed(geometry.numberOfIndices, 0, 0);
}