-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameObject.cpp
124 lines (104 loc) · 3.57 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
#include "GameObject.h"
GameObject::GameObject(void)
{
m_model = 0;
m_colorVerts = 0;
m_colorNormVerts = 0;
m_textureVerts = 0;
m_flatTextureVerts = 0;
m_numVert = 0;
m_numInds = 0;
m_indices = 0;
m_parentMatrix = new XMFLOAT4X4();
XMStoreFloat4x4(&m_worldMatrix, XMMatrixIdentity());
XMStoreFloat4x4(&m_scaleMatrix, XMMatrixIdentity());
XMStoreFloat4x4(&m_rotationMatrix, XMMatrixIdentity());
XMStoreFloat4x4(&m_orbitMatrix, XMMatrixIdentity());
XMStoreFloat4x4(&m_translationMatrix, XMMatrixIdentity());
XMStoreFloat4x4(m_parentMatrix, XMMatrixIdentity());
}
GameObject::~GameObject(void)
{
m_model = 0;
m_colorVerts = 0;
m_colorNormVerts = 0;
m_textureVerts = 0;
m_flatTextureVerts = 0;
m_indices = 0;
m_parentMatrix = 0;
}
void GameObject::shutdown()
{
if (m_model) m_model->shutdown();
if (m_colorVerts) delete[] m_colorVerts;
if (m_colorNormVerts) delete[] m_colorNormVerts;
if (m_textureVerts) delete[] m_textureVerts;
if (m_flatTextureVerts) delete[] m_flatTextureVerts;
if (m_indices) delete[] m_indices;
}
Model* GameObject::getModel() { return m_model; }
bool GameObject::isColorObject() { return m_colorVerts ? true : false; }
bool GameObject::isColorNormObject() { return m_colorNormVerts ? true : false; }
bool GameObject::isTextureObject() { return m_textureVerts ? true : false; }
bool GameObject::isFlatTextureObject() { return m_flatTextureVerts ? true : false; }
XMFLOAT4X4* GameObject::getWorldMatrix()
{
XMStoreFloat4x4(&m_worldMatrix,
XMLoadFloat4x4(&m_scaleMatrix) *
XMLoadFloat4x4(&m_rotationMatrix) *
XMLoadFloat4x4(&m_orbitMatrix) *
XMLoadFloat4x4(&m_translationMatrix) *
XMLoadFloat4x4(m_parentMatrix));
return &m_worldMatrix;
}
void GameObject::setParentMatrix(XMFLOAT4X4* matrix)
{
m_parentMatrix = matrix;
}
void GameObject::scale(float xFactor, float yFactor, float zFactor)
{
XMStoreFloat4x4(&m_scaleMatrix, XMLoadFloat4x4(&m_scaleMatrix) * XMMatrixScaling(xFactor, yFactor, zFactor));
}
void GameObject::rotateX(float theta)
{
XMStoreFloat4x4(&m_rotationMatrix, XMLoadFloat4x4(&m_rotationMatrix) * XMMatrixRotationX(theta));
}
void GameObject::rotateY(float theta)
{
XMStoreFloat4x4(&m_rotationMatrix, XMLoadFloat4x4(&m_rotationMatrix) * XMMatrixRotationY(theta));
}
void GameObject::rotateZ(float theta)
{
XMStoreFloat4x4(&m_rotationMatrix, XMLoadFloat4x4(&m_rotationMatrix) * XMMatrixRotationZ(theta));
}
void GameObject::orbitX(float theta, XMFLOAT3 pos)
{
XMStoreFloat4x4(&m_orbitMatrix, XMLoadFloat4x4(&m_orbitMatrix) *
XMMatrixTranslation(-pos.x, -pos.y, -pos.z) *
XMMatrixRotationX(theta) *
XMMatrixTranslation(pos.x, pos.y, pos.z));
}
void GameObject::orbitY(float theta, XMFLOAT3 pos)
{
XMStoreFloat4x4(&m_orbitMatrix, XMLoadFloat4x4(&m_orbitMatrix) *
XMMatrixTranslation(-pos.x, -pos.y, -pos.z) *
XMMatrixRotationY(theta) *
XMMatrixTranslation(pos.x, pos.y, pos.z));
}
void GameObject::orbitZ(float theta, XMFLOAT3 pos)
{
XMStoreFloat4x4(&m_orbitMatrix, XMLoadFloat4x4(&m_orbitMatrix) *
XMMatrixTranslation(-pos.x, -pos.y, -pos.z) *
XMMatrixRotationZ(theta) *
XMMatrixTranslation(pos.x, pos.y, pos.z));
}
void GameObject::translate(float dx, float dy, float dz)
{
XMStoreFloat4x4(&m_translationMatrix, XMLoadFloat4x4(&m_translationMatrix) * XMMatrixTranslation(dx, dy, dz));
}
XMFLOAT3 GameObject::getPos()
{
XMFLOAT3 pos;
XMStoreFloat3(&pos, XMVector3Transform(XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f), XMLoadFloat4x4(&m_translationMatrix) * XMLoadFloat4x4(m_parentMatrix)));
return pos;
}