5#include "application_class.h"
10 m_scaleMatrix = XMMatrixIdentity();
11 m_rotateMatrix = XMMatrixIdentity();
12 m_translateMatrix = XMMatrixIdentity();
13 m_srMatrix = XMMatrixIdentity();
14 m_worldMatrix = XMMatrixIdentity();
15 m_previousPosition = XMVectorZero();
16 m_velocity = XMVectorZero();
17 m_acceleration = XMVectorZero();
21 m_boundingRadius = 1.0f;
27 m_scaleMatrix = XMMatrixIdentity();
28 m_rotateMatrix = XMMatrixIdentity();
29 m_translateMatrix = XMMatrixIdentity();
30 m_srMatrix = XMMatrixIdentity();
31 m_worldMatrix = XMMatrixIdentity();
32 m_previousPosition = XMVectorZero();
33 m_velocity = XMVectorZero();
34 m_acceleration = XMVectorZero();
38 m_boundingRadius = 1.0f;
45bool object::Initialize(
47 ID3D11DeviceContext* deviceContext,
53 std::string filename(modelFilename);
54 auto it = g_model_cache.find(filename);
55 if (it != g_model_cache.end())
57 m_model_ = it->second;
61 auto new_model = std::make_shared<model_class>();
62 if (!new_model->Initialize(device, deviceContext, modelFilename, texturesContainer))
66 g_model_cache[filename] = new_model;
73void object::SetScaleMatrix(XMMATRIX scaleMatrix)
75 m_scaleMatrix = scaleMatrix;
78void object::SetRotateMatrix(XMMATRIX rotateMatrix)
80 m_rotateMatrix = rotateMatrix;
83void object::SetTranslateMatrix(XMMATRIX translateMatrix)
85 m_translateMatrix = translateMatrix;
88void object::SetSRMatrix(XMMATRIX srMatrix)
90 m_srMatrix = srMatrix;
93void object::SetWorldMatrix(XMMATRIX worldMatrix)
95 m_worldMatrix = worldMatrix;
98XMMATRIX object::GetScaleMatrix()
const
100 return m_scaleMatrix;
103XMMATRIX object::GetRotateMatrix()
const
105 return m_rotateMatrix;
108XMMATRIX object::GetTranslateMatrix()
const
110 return m_translateMatrix;
113XMMATRIX object::GetSRMatrix()
const
118XMMATRIX object::GetWorldMatrix()
const
120 return m_worldMatrix;
123XMVECTOR object::GetPosition()
126 XMStoreFloat4x4(&matrix, m_translateMatrix);
127 return XMVectorSet(matrix._41, matrix._42, matrix._43, 0.0f);
130XMVECTOR object::GetRotation()
133 XMStoreFloat4x4(&matrix, m_rotateMatrix);
134 float rotationX = atan2f(matrix._32, matrix._33);
135 float rotationY = atan2f(-matrix._31, sqrtf(matrix._32 * matrix._32 + matrix._33 * matrix._33));
136 float rotationZ = atan2f(matrix._21, matrix._11);
137 return XMVectorSet(rotationX, rotationY, rotationZ, 0.0f);
140XMVECTOR object::GetScale()
143 XMStoreFloat4x4(&matrix, m_scaleMatrix);
146 XMVECTOR row1 = XMLoadFloat3(
reinterpret_cast<XMFLOAT3*
>(&matrix._11));
147 XMVECTOR row2 = XMLoadFloat3(
reinterpret_cast<XMFLOAT3*
>(&matrix._21));
148 XMVECTOR row3 = XMLoadFloat3(
reinterpret_cast<XMFLOAT3*
>(&matrix._31));
151 XMVECTOR scale = XMVectorSet(
152 XMVectorGetX(XMVector3Length(row1)),
153 XMVectorGetX(XMVector3Length(row2)),
154 XMVectorGetX(XMVector3Length(row3)),
163void object::SetPosition(XMVECTOR position)
166 XMStoreFloat4x4(&matrix, m_translateMatrix);
167 matrix._41 = XMVectorGetX(position);
168 matrix._42 = XMVectorGetY(position);
169 matrix._43 = XMVectorGetZ(position);
170 m_translateMatrix = XMLoadFloat4x4(&matrix);
173void object::SetRotation(XMVECTOR rotation)
176 XMStoreFloat4x4(&matrix, m_rotateMatrix);
177 XMMATRIX rotationMatrix = XMMatrixRotationRollPitchYaw(XMVectorGetX(rotation), XMVectorGetY(rotation), XMVectorGetZ(rotation));
178 m_rotateMatrix = rotationMatrix;
181void object::SetScale(XMVECTOR scale)
184 XMStoreFloat4x4(&matrix, m_scaleMatrix);
185 matrix._11 = XMVectorGetX(scale);
186 matrix._22 = XMVectorGetY(scale);
187 matrix._33 = XMVectorGetZ(scale);
188 m_scaleMatrix = XMLoadFloat4x4(&matrix);
191void object::UpdateWorldMatrix()
193 m_worldMatrix = m_scaleMatrix * m_rotateMatrix * m_translateMatrix;
196void object::UpdateSRMatrix()
198 m_srMatrix = m_scaleMatrix * m_rotateMatrix;
201void object::UpdateTranslateMatrix()
203 m_translateMatrix = XMMatrixTranslationFromVector(GetPosition());
206void object::UpdateRotateMatrix()
208 m_rotateMatrix = XMMatrixRotationRollPitchYawFromVector(GetRotation());
211void object::UpdateScaleMatrix()
213 m_scaleMatrix = XMMatrixScalingFromVector(GetScale());
220 UpdateTranslateMatrix();
221 UpdateRotateMatrix();
225std::string object::GetName()
230void object::SetName(std::string name)
235void object::SetVelocity(XMVECTOR velocity)
237 m_velocity = velocity;
240void object::AddVelocity(
float deltaTime)
242 m_velocity += m_acceleration * deltaTime;
245XMVECTOR object::GetVelocity()
const
250void object::SetAcceleration(XMVECTOR acceleration)
252 m_acceleration = acceleration;
255XMVECTOR object::GetAcceleration()
const
257 return m_acceleration;
260void object::SetMass(
float mass)
265float object::GetMass()
const
270void object::SetGrounded(
bool isGrounded)
272 m_isGrounded = isGrounded;
275bool object::IsGrounded()
const
280int object::SetId(
int id)
285int object::GetId()
const
290bool object::IsPhysicsEnabled()
const
292 return m_isPhysicsEnabled;
295void object::SetPhysicsEnabled(
bool state)
297 m_isPhysicsEnabled = state;
300float object::GetBoundingRadius()
const
302 return m_boundingRadius;
305void object::UpdatePosition(
float deltaTime)
307 XMVECTOR position = GetPosition();
308 position = position + GetVelocity() * deltaTime;
309 SetPosition(position);
312void object::SetType(ObjectType type)
317std::string object::ObjectTypeToString(ObjectType type) {
319 case ObjectType::Cube:
return "Cube";
320 case ObjectType::Sphere:
return "Sphere";
322 default:
return "Unknown";
326ObjectType object::StringToObjectType(
const std::string& str) {
327 if (str ==
"Cube")
return ObjectType::Cube;
328 if (str ==
"Sphere")
return ObjectType::Sphere;
330 return ObjectType::Unknown;
333std::string object::ShaderTypeToString(ShaderType type) {
335 case ShaderType::ALPHA_MAPPING:
return "ALPHA_MAPPING";
336 case ShaderType::CEL_SHADING:
return "CEL_SHADING";
337 case ShaderType::NORMAL_MAPPING:
return "NORMAL_MAPPING";
338 case ShaderType::SPECULAR_MAPPING:
return "SPECULAR_MAPPING";
339 case ShaderType::TEXTURE:
return "TEXTURE";
340 case ShaderType::LIGHTING:
return "LIGHTING";
341 case ShaderType::SUNLIGHT:
return "SUNLIGHT";
343 default:
return "Unknown";
347ShaderType object::StringToShaderType(
const std::string& str) {
348 if (str ==
"ALPHA_MAPPING")
return ShaderType::ALPHA_MAPPING;
349 if (str ==
"CEL_SHADING")
return ShaderType::CEL_SHADING;
350 if (str ==
"NORMAL_MAPPING")
return ShaderType::NORMAL_MAPPING;
351 if (str ==
"SPECULAR_MAPPING")
return ShaderType::SPECULAR_MAPPING;
352 if (str ==
"TEXTURE")
return ShaderType::TEXTURE;
353 if (str ==
"LIGHTING")
return ShaderType::LIGHTING;
354 if (str ==
"SUNLIGHT")
return ShaderType::SUNLIGHT;
356 return ShaderType::TEXTURE;
359void object::LaunchObject()
362 const float gravity = -9.81f;
365 float alphaRadians = m_alpha * (XM_PI / 180.0f);
368 float scaleFactor = 200.0f;
372 float velocityMagnitude = m_initialStretch * sqrtf(m_springConstant / m_mass) *
373 sqrtf(1.0f - powf((m_mass * gravity * sinf(alphaRadians) / (m_springConstant * m_initialStretch)), 2.0f));
376 velocityMagnitude *= scaleFactor;
379 XMVECTOR velocity = XMVectorSet(
380 velocityMagnitude * cosf(alphaRadians),
381 velocityMagnitude * sinf(alphaRadians),
387 SetVelocity(velocity);
390 SetPhysicsEnabled(
true);
397 sprintf_s(buffer,
"Launch velocity: %f m/s at angle %f degrees", XMVectorGetX(XMVector3Length(velocity)), m_alpha);
398 OutputDebugStringA(buffer);
401bool object::LoadTexturesFromPath(std::vector<std::wstring>& texturePaths,
TextureContainer& texturesContainer,
409 for (
const auto& texturePath : texturePaths)
411 ID3D11ShaderResourceView* texture =
nullptr;
412 result = DirectX::CreateWICTextureFromFile(m_Direct3D->
get_device(), m_Direct3D->
get_device_context(), texturePath.c_str(),
nullptr, &texture);
417 _com_error err(result);
418 LPCTSTR errMsg = err.ErrorMessage();
421 std::wstring ws(errMsg);
422 std::string str(ws.begin(), ws.end());
424 Logger::Get().
Log(
"Failed to load texture: " + std::string(texturePath.begin(), texturePath.end()) +
425 "\nError: " + std::to_string(result) +
426 "\nDescription: " + str,
427 __FILE__, __LINE__, Logger::LogLevel::Error);
430 texturesContainer.AssignTexture(texturesContainer, texture,texturePath , i);
void Log(const std::string &message, const std::string &fileName, int lineNumber, LogLevel level=LogLevel::Info)
ID3D11Device * get_device()
Gets the Direct3D device.
ID3D11DeviceContext * get_device_context()
Gets the Direct3D device context.