Math
- Added Math namespace - Added Rand functions - Maintenance
This commit is contained in:
parent
b3997880fe
commit
32df2e01c6
7 changed files with 77 additions and 67 deletions
|
@ -1,9 +1,9 @@
|
|||
#include "ltpch.h"
|
||||
#include "TempExtensions.h"
|
||||
#include "Random.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace Light {
|
||||
namespace Light { namespace Math {
|
||||
|
||||
float Math::Rand(int min, int max, int decimals /* = 0 */)
|
||||
{
|
||||
|
@ -32,10 +32,10 @@ namespace Light {
|
|||
min *= dec;
|
||||
max *= dec;
|
||||
|
||||
float r1 = (min + (rand() % (max)-min)) / (float)dec;
|
||||
float r2 = (min + (rand() % (max)-min)) / (float)dec;
|
||||
float r3 = (min + (rand() % (max)-min)) / (float)dec;
|
||||
float r1 = (min + (rand() % (max - min))) / (float)dec;
|
||||
float r2 = (min + (rand() % (max - min))) / (float)dec;
|
||||
float r3 = (min + (rand() % (max - min))) / (float)dec;
|
||||
|
||||
return glm::vec3(r1, r2, r3);
|
||||
}
|
||||
}
|
||||
}}
|
16
Engine/src/Engine/Math/Random.h
Normal file
16
Engine/src/Engine/Math/Random.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#pragma once
|
||||
|
||||
// #todo: make proper math stuff
|
||||
|
||||
// this is a temporary header file to extend the glm math
|
||||
// light engine will either have it's own math library or extend upon the glm
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
namespace Light { namespace Math {
|
||||
|
||||
float Rand(int min, int max, int decimals = 0);
|
||||
glm::vec2 RandVec2(int min, int max, int decimals = 0);
|
||||
glm::vec3 RandVec3(int min, int max, int decimals = 0);
|
||||
|
||||
}}
|
|
@ -1,20 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
// #todo: make proper math stuff
|
||||
|
||||
// this is a temporary header file to extend the glm math
|
||||
// light engine will either have it's own math library or extend upon the glm
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
namespace Light {
|
||||
|
||||
class Math
|
||||
{
|
||||
public:
|
||||
static float Rand(int min, int max, int decimals = 0);
|
||||
static glm::vec2 RandVec2(int min, int max, int decimals = 0);
|
||||
static glm::vec3 RandVec3(int min, int max, int decimals = 0);
|
||||
};
|
||||
|
||||
}
|
|
@ -93,6 +93,17 @@ namespace Light {
|
|||
m_Textures[name] = Ref<Texture>(Texture::Create(width, height, components, pixels, m_SharedGraphicsContext));
|
||||
}
|
||||
|
||||
void ResourceManager::ReleaseTextureImpl(const std::string& name)
|
||||
{
|
||||
if (!m_Textures[name])
|
||||
{
|
||||
LT_ENGINE_WARN("ResourceManager::ReleaseTextureImpl: failed to find texture named: {}", name);
|
||||
return;
|
||||
}
|
||||
|
||||
m_Textures[name] = nullptr;
|
||||
}
|
||||
|
||||
void ResourceManager::ExtractShaderSource(std::string& src, const std::string& delim)
|
||||
{
|
||||
size_t begDelimPos, endDelimPos;
|
||||
|
|
|
@ -29,6 +29,8 @@ namespace Light {
|
|||
|
||||
static inline void LoadTexture(const std::string& name, const std::string& path, unsigned int desiredComponents = 4u) { s_Context->LoadTextureImpl(name, path, desiredComponents); }
|
||||
|
||||
static inline void ReleaseTexture(const std::string& name) { s_Context->ReleaseTextureImpl(name); }
|
||||
|
||||
static inline Ref<Shader> GetShader(const std::string& name) { return s_Context->m_Shaders[name]; }
|
||||
static inline Ref<Texture> GetTexture(const std::string& name) { return s_Context->m_Textures[name]; }
|
||||
|
||||
|
@ -40,6 +42,8 @@ namespace Light {
|
|||
|
||||
void LoadTextureImpl(const std::string& name, const std::string& path, unsigned int desiredComponents = 4u);
|
||||
|
||||
void ReleaseTextureImpl(const std::string& name);
|
||||
|
||||
private:
|
||||
void ExtractShaderSource(std::string& src, const std::string& delim);
|
||||
};
|
||||
|
|
|
@ -46,7 +46,7 @@
|
|||
#include <imgui.h>
|
||||
|
||||
// math
|
||||
#include "Math/TempExtensions.h"
|
||||
#include "Math/Random.h"
|
||||
|
||||
// scene
|
||||
#include "Scene/Scene.h"
|
||||
|
|
|
@ -3,73 +3,68 @@
|
|||
namespace Light {
|
||||
|
||||
EditorLayer::EditorLayer(const std::string& name)
|
||||
: Layer(name), m_Direction(glm::vec2(0.0f, 0.0f))
|
||||
: Layer(name)
|
||||
{
|
||||
m_Scene = CreateRef<Scene>();
|
||||
|
||||
m_PropertiesPanel = CreateRef<PropertiesPanel>();
|
||||
m_SceneHierarchyPanel = CreateRef<SceneHierarchyPanel>(m_Scene, m_PropertiesPanel);
|
||||
SummonAwesomeness();
|
||||
|
||||
m_CameraEntity = m_Scene->CreateEntity("Camera", TransformComponent(glm::vec3(0.0f, 0.0f, 1000.0f)));
|
||||
m_Framebuffer = Framebuffer::Create({ 1, 1, 1 }, GraphicsContext::GetSharedContext());
|
||||
|
||||
m_CameraEntity = m_Scene->CreateEntity("Game Camera", TransformComponent({0.0f, 0.0f, 1000.0f}));
|
||||
m_CameraEntity.AddComponent<CameraComponent>(SceneCamera(), true);
|
||||
|
||||
m_Framebuffer = std::shared_ptr<Framebuffer>(Framebuffer::Create({ 1u, 1u, 1u }, GraphicsContext::GetSharedContext()));
|
||||
|
||||
|
||||
// for native scripts
|
||||
// for native script components
|
||||
m_Scene->OnCreate();
|
||||
}
|
||||
|
||||
// #temp
|
||||
srand(time(NULL));
|
||||
}
|
||||
|
||||
void EditorLayer::OnUpdate(float deltaTime)
|
||||
{
|
||||
if (Input::GetKeyboardKey(Key::A))
|
||||
m_Direction.x = -1.0f;
|
||||
else if (Input::GetKeyboardKey(Key::D))
|
||||
m_Direction.x = 1.0f;
|
||||
else
|
||||
m_Direction.x = 0.0f;
|
||||
|
||||
if (Input::GetKeyboardKey(Key::W))
|
||||
m_Direction.y = 1.0f;
|
||||
else if (Input::GetKeyboardKey(Key::S))
|
||||
m_Direction.y = -1.0f;
|
||||
else
|
||||
m_Direction.y = 0.0f;
|
||||
|
||||
auto& transform = m_CameraEntity.GetComponent<TransformComponent>();
|
||||
transform.translation += glm::vec3(m_Direction * m_Speed * deltaTime, 0.0);
|
||||
|
||||
m_Scene->OnUpdate(deltaTime);
|
||||
}
|
||||
|
||||
m_Direction.x = Input::GetKeyboardKey(Key::A) ? -1.0f :
|
||||
Input::GetKeyboardKey(Key::D) ? 1.0f : 0.0f;
|
||||
|
||||
|
||||
m_Direction.y = Input::GetKeyboardKey(Key::S) ? -1.0f :
|
||||
Input::GetKeyboardKey(Key::W) ? 1.0f : 0.0f;
|
||||
|
||||
auto& cameraTranslation = m_CameraEntity.GetComponent<TransformComponent>().translation;
|
||||
cameraTranslation += glm::vec3(m_Direction * m_Speed * deltaTime, 0.0f);
|
||||
}
|
||||
|
||||
void EditorLayer::OnRender()
|
||||
{
|
||||
m_Scene->OnRender(m_Framebuffer);
|
||||
}
|
||||
|
||||
|
||||
void EditorLayer::OnUserInterfaceUpdate()
|
||||
{
|
||||
UserInterface::DockspaceBegin();
|
||||
ImGui::ShowDemoWindow();
|
||||
|
||||
if (ImGui::Begin("GameView"))
|
||||
if (ImGui::Begin("Game"))
|
||||
{
|
||||
Input::ReceiveGameEvents(ImGui::IsWindowFocused());
|
||||
|
||||
ImVec2 regionAvail = ImGui::GetContentRegionAvail();
|
||||
|
||||
if (m_AvailableContentRegionPrev != regionAvail)
|
||||
if(m_AvailableContentRegionPrev != regionAvail)
|
||||
{
|
||||
m_AvailableContentRegionPrev = regionAvail;
|
||||
m_Framebuffer->Resize({ regionAvail.x, regionAvail.y });
|
||||
auto& camera = m_CameraEntity.GetComponent<CameraComponent>().camera;
|
||||
camera.SetViewportSize(regionAvail.x, regionAvail.y);;
|
||||
|
||||
auto& cameraComp = m_CameraEntity.GetComponent<CameraComponent>();
|
||||
cameraComp.camera.SetViewportSize(regionAvail.x, regionAvail.y);
|
||||
m_AvailableContentRegionPrev = regionAvail;
|
||||
}
|
||||
|
||||
if (GraphicsContext::GetGraphicsAPI() == GraphicsAPI::DirectX)
|
||||
if(GraphicsContext::GetGraphicsAPI() == GraphicsAPI::DirectX)
|
||||
ImGui::Image(m_Framebuffer->GetColorAttachment(), regionAvail);
|
||||
else // opengl
|
||||
else
|
||||
ImGui::Image(m_Framebuffer->GetColorAttachment(), regionAvail, ImVec2(0, 1), ImVec2(1, 0));
|
||||
} ImGui::End();
|
||||
|
||||
|
@ -78,21 +73,25 @@ namespace Light {
|
|||
|
||||
UserInterface::DockspaceEnd();
|
||||
}
|
||||
|
||||
|
||||
void EditorLayer::SummonAwesomeness()
|
||||
{
|
||||
ResourceManager::LoadTexture("awesomeface", "res/Textures/awesomeface.png");
|
||||
auto texture = ResourceManager::GetTexture("awesomeface");
|
||||
|
||||
for (int i = 0; i < 420; i++)
|
||||
for(unsigned int i = 0; i < 250; i++)
|
||||
{
|
||||
const glm::vec3 translation = Math::RandVec3(-500, 500);
|
||||
const glm::vec3 scale = glm::vec3(Math::Rand(250, 350));
|
||||
const glm::vec3 rotation = glm::radians(Math::RandVec3(0, 360, 2));;
|
||||
const glm::vec3 scale = glm::vec3(Math::Rand(125, 200));
|
||||
const glm::vec3 rotation = glm::radians(glm::vec3(0.0f, 0.0f, Math::Rand(0, 360)));
|
||||
|
||||
Entity quad = m_Scene->CreateEntity("quad" + std::to_string(i), { translation, scale, rotation });
|
||||
quad.AddComponent<SpriteRendererComponent>(texture);
|
||||
const glm::vec4 tint = glm::vec4(Math::RandVec3(0, 1, 2), 1.0f);
|
||||
|
||||
auto& entity = m_Scene->CreateEntity("quad" + std::to_string(i), { translation, scale, rotation });
|
||||
entity.AddComponent<SpriteRendererComponent>(texture, tint);
|
||||
}
|
||||
|
||||
ResourceManager::ReleaseTexture("awesomeface");
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue