ECS with entt
This commit is contained in:
@@ -2,8 +2,10 @@
|
||||
#define OPEN_ENGINE_HPP
|
||||
|
||||
#include "open_engine/orthographic_camera_controller.hpp"
|
||||
#include "open_engine/scene/native_scriptable_entity.hpp"
|
||||
#include "open_engine/imgui/imgui_layer.hpp"
|
||||
#include "open_engine/events/key_event.hpp"
|
||||
#include "open_engine/scene/components.hpp"
|
||||
#include "open_engine/application.hpp"
|
||||
#include "open_engine/ref_scope.hpp"
|
||||
#include "open_engine/logging.hpp"
|
||||
@@ -21,7 +23,9 @@
|
||||
#include "open_engine/renderer/renderer2d.hpp"
|
||||
#include "open_engine/renderer/renderer.hpp"
|
||||
#include "open_engine/renderer/texture.hpp"
|
||||
#include "open_engine/scene/components.hpp"
|
||||
#include "open_engine/renderer/buffer.hpp"
|
||||
#include "open_engine/renderer/shader.hpp"
|
||||
|
||||
#include "open_engine/scene/entity.hpp"
|
||||
#include "open_engine/scene/scene.hpp"
|
||||
#endif // OPEN_ENGINE_HPP
|
||||
|
||||
@@ -22,6 +22,7 @@ namespace OpenEngine {
|
||||
void QueueLayerPop(Ref<Layer> layer);
|
||||
void QueueOverlayPush(Ref<Layer> layer);
|
||||
void QueueOverlayPop(Ref<Layer> layer);
|
||||
ImGuiLayer* GetImGuiLayer() { return imgui_layer.get(); };
|
||||
|
||||
inline static Application& Get() { return *instance; };
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
#define BIT(x) (1 << x)
|
||||
|
||||
#define BIND_EVENT_FN(function) std::bind(&function, this, std::placeholders::_1)
|
||||
//#define BIND_EVENT_FN(function) std::bind(&function, this, std::placeholders::_1)
|
||||
#define BIND_EVENT_FN(fn) [this](auto&&...args) -> decltype(auto) { return this->fn(std::forward<decltype(args)>(args)...); }
|
||||
|
||||
#endif // CORE_HPP
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef IMGUI_LAYER_HPP
|
||||
#define IMGUI_LAYER_HPP
|
||||
|
||||
#include "open_engine/events/key_event.hpp"
|
||||
#include "open_engine/events/event.hpp"
|
||||
#include "open_engine/layer.hpp"
|
||||
|
||||
@@ -15,11 +16,15 @@ namespace OpenEngine {
|
||||
virtual void OnDetach() override;
|
||||
virtual void OnImGuiRender() override;
|
||||
virtual void OnEvent(Event& event) override;
|
||||
bool CloseWindow(OpenEngine::KeyPressedEvent& event);
|
||||
|
||||
void SetBlockEvents(bool block) { block_events = block; };
|
||||
|
||||
void Begin();
|
||||
void End();
|
||||
|
||||
private:
|
||||
bool block_events = false;
|
||||
float previous_frame_time = 0.0f;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef CAMERA_HPP
|
||||
#define CAMERA_HPP
|
||||
#ifndef ORTHOGRAPHIC_CAMERA_HPP
|
||||
#define ORTHOGRAPHIC_CAMERA_HPP
|
||||
|
||||
#include <glm/fwd.hpp>
|
||||
#include <glm/glm.hpp>
|
||||
@@ -33,4 +33,4 @@ namespace OpenEngine {
|
||||
};
|
||||
}
|
||||
|
||||
#endif // CAMERA_HPP
|
||||
#endif // ORTHOGRAPHIC_CAMERA_HPP
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <array>
|
||||
|
||||
|
||||
26
open_engine/include/open_engine/renderer/camera.hpp
Normal file
26
open_engine/include/open_engine/renderer/camera.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef CAMERA_HPP
|
||||
#define CAMERA_HPP
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
namespace OpenEngine {
|
||||
|
||||
class Camera
|
||||
{
|
||||
public:
|
||||
Camera() = default;
|
||||
~Camera() = default;
|
||||
Camera(const glm::mat4& projection)
|
||||
: projection(projection)
|
||||
{
|
||||
};
|
||||
|
||||
const glm::mat4& GetProjection() const { return projection; };
|
||||
|
||||
protected:
|
||||
glm::mat4 projection = glm::mat4(1.0f);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // CAMERA_HPP
|
||||
@@ -1,9 +1,10 @@
|
||||
#ifndef RENDERER2D_HPP
|
||||
#define RENDERER2D_HPP
|
||||
|
||||
#include "open_engine/orthographic_camera.hpp"
|
||||
#include "open_engine/renderer/subtexture2d.hpp"
|
||||
#include "open_engine/orthographic_camera.hpp"
|
||||
#include "open_engine/renderer/texture.hpp"
|
||||
#include "open_engine/renderer/camera.hpp"
|
||||
#include "open_engine/ref_scope.hpp"
|
||||
|
||||
#include <glm/fwd.hpp>
|
||||
@@ -32,6 +33,7 @@ namespace OpenEngine {
|
||||
static void Shutdown();
|
||||
|
||||
static void BeginScene(const OrthographicCamera& camera);
|
||||
static void BeginScene(const Camera& camera, const glm::mat4& transform);
|
||||
static void EndScene();
|
||||
static void Flush();
|
||||
|
||||
@@ -39,6 +41,10 @@ namespace OpenEngine {
|
||||
static void DrawQuad(const Transform& transform_data, const Ref<Texture2D>& texture, float tiling_factor = 1.0f);
|
||||
static void DrawQuad(const Transform& transform_data, const Ref<Subtexture2D>& subtexture, float tiling_factor = 1.0f);
|
||||
|
||||
static void DrawQuad(const glm::mat4& transform, const glm::vec4& color);
|
||||
static void DrawQuad(const glm::mat4& transform, const Ref<Texture2D>& texture, float tiling_factor = 1.0f);
|
||||
static void DrawQuad(const glm::mat4& transform, const Ref<Subtexture2D>& subtexture, float tiling_factor = 1.0f);
|
||||
|
||||
static void ResetStats();
|
||||
static const Statistics& GetStats();
|
||||
|
||||
|
||||
151
open_engine/include/open_engine/scene/components.hpp
Normal file
151
open_engine/include/open_engine/scene/components.hpp
Normal file
@@ -0,0 +1,151 @@
|
||||
#ifndef COMPONENTS_HPP
|
||||
#define COMPONENTS_HPP
|
||||
|
||||
#include "open_engine/scene/native_scriptable_entity.hpp"
|
||||
#include "open_engine/scene/scene_camera.hpp"
|
||||
|
||||
#include <cstring>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <imgui.h>
|
||||
#include <string>
|
||||
|
||||
namespace OpenEngine {
|
||||
|
||||
struct TagComponent
|
||||
{
|
||||
std::string tag;
|
||||
|
||||
TagComponent() = default;
|
||||
TagComponent(const TagComponent&) = default;
|
||||
TagComponent(const std::string& tag)
|
||||
: tag(tag) {}
|
||||
|
||||
void OnImGuiRender(Entity& entity)
|
||||
{
|
||||
char buffer[256];
|
||||
std::memset(buffer, 0, sizeof(buffer));
|
||||
std::strncpy(buffer, tag.c_str(), sizeof(buffer));
|
||||
|
||||
ImGui::Text("Name");
|
||||
if (ImGui::InputText("##tagEditor", buffer, sizeof(buffer), ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_AutoSelectAll)
|
||||
|| (!ImGui::IsItemActive() && ImGui::IsMouseClicked(0)))
|
||||
tag = buffer;
|
||||
};
|
||||
};
|
||||
|
||||
struct TransformComponent
|
||||
{
|
||||
glm::mat4 transform{ 1.0f };
|
||||
|
||||
TransformComponent() = default;
|
||||
TransformComponent(const TransformComponent&) = default;
|
||||
TransformComponent(const glm::mat4& transform_mat)
|
||||
: transform(transform_mat) {}
|
||||
|
||||
operator glm::mat4() { return transform; };
|
||||
operator const glm::mat4() const { return transform; };
|
||||
|
||||
void OnImGuiRender(Entity& entity)
|
||||
{
|
||||
ImGui::DragFloat3("Position", glm::value_ptr(transform[3]), 0.1f);
|
||||
};
|
||||
};
|
||||
|
||||
struct SpriteRendererComponent
|
||||
{
|
||||
glm::vec4 color{ 1.0f, 1.0f, 1.0f, 1.0f };
|
||||
|
||||
SpriteRendererComponent() = default;
|
||||
SpriteRendererComponent(const SpriteRendererComponent&) = default;
|
||||
SpriteRendererComponent(const glm::vec4& color)
|
||||
: color(color) {}
|
||||
|
||||
void OnImGuiRender(Entity& entity)
|
||||
{
|
||||
ImGui::ColorEdit4("color", glm::value_ptr(color));
|
||||
};
|
||||
};
|
||||
|
||||
struct CameraComponent
|
||||
{
|
||||
SceneCamera camera;
|
||||
bool primary = true;
|
||||
bool fixed_aspect_ratio = false;
|
||||
|
||||
CameraComponent() = default;
|
||||
CameraComponent(const CameraComponent&) = default;
|
||||
|
||||
void OnImGuiRender(Entity& entity)
|
||||
{
|
||||
ImGui::Checkbox("Is primary", &primary);
|
||||
const char* projection_type_strings[] = {"Perspective", "Orthographic"};
|
||||
const char* current_projection_type = projection_type_strings[(int)camera.GetProjectionType()];
|
||||
|
||||
if (ImGui::BeginCombo("Projection", current_projection_type)) {
|
||||
for (int i = 0; i < 2; i++) {
|
||||
bool is_selected = current_projection_type == projection_type_strings[i];
|
||||
if (ImGui::Selectable(projection_type_strings[i], is_selected)) {
|
||||
current_projection_type = projection_type_strings[i];
|
||||
camera.SetProjectionType((SceneCamera::ProjectionType)i);
|
||||
}
|
||||
|
||||
if (is_selected)
|
||||
ImGui::SetItemDefaultFocus();
|
||||
}
|
||||
ImGui::EndCombo();
|
||||
}
|
||||
|
||||
if (camera.GetProjectionType() == SceneCamera::ProjectionType::Perspective) {
|
||||
float fov = camera.GetVerticalFov();
|
||||
if (ImGui::DragFloat("Fov", &fov, 0.1f))
|
||||
camera.SetVerticalFov(fov);
|
||||
|
||||
float near_clip = camera.GetPerspectiveNearClip();
|
||||
if (ImGui::DragFloat("Near clip", &near_clip, 0.1f))
|
||||
camera.SetPerspectiveNearClip(near_clip);
|
||||
|
||||
float far_clip = camera.GetPerspectiveFarClip();
|
||||
if (ImGui::DragFloat("Far clip", &far_clip, 0.1f))
|
||||
camera.SetPerspectiveFarClip(far_clip);
|
||||
}
|
||||
else if (camera.GetProjectionType() == SceneCamera::ProjectionType::Orthographic) {
|
||||
ImGui::Checkbox("Has fixed aspect ratio", &fixed_aspect_ratio);
|
||||
float ortho_size = camera.GetOrthographicSize();
|
||||
if (ImGui::DragFloat("Orthographic size", &ortho_size, 0.1f))
|
||||
camera.SetOrthographicSize(ortho_size);
|
||||
|
||||
float near_clip = camera.GetOrthographicNearClip();
|
||||
if (ImGui::DragFloat("Near clip", &near_clip, 0.1f))
|
||||
camera.SetOrthographicNearClip(near_clip);
|
||||
|
||||
float far_clip = camera.GetOrthographicFarClip();
|
||||
if (ImGui::DragFloat("Far clip", &far_clip, 0.1f))
|
||||
camera.SetOrthographicFarClip(far_clip);
|
||||
}
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
struct NativeScriptComponent
|
||||
{
|
||||
NativeScriptableEntity* instance = nullptr;
|
||||
|
||||
NativeScriptableEntity* (*InstanciateScript)();
|
||||
void (*DestroyInstanceScript)(NativeScriptComponent*);
|
||||
|
||||
template <typename T>
|
||||
void Bind()
|
||||
{
|
||||
InstanciateScript = []() { return static_cast<NativeScriptableEntity*>(new T()); };
|
||||
DestroyInstanceScript = [](NativeScriptComponent* nsc)
|
||||
{
|
||||
(delete nsc->instance);
|
||||
nsc->instance = nullptr;
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // COMPONENTS_HPP
|
||||
64
open_engine/include/open_engine/scene/entity.hpp
Normal file
64
open_engine/include/open_engine/scene/entity.hpp
Normal file
@@ -0,0 +1,64 @@
|
||||
#ifndef ENTITY_HPP
|
||||
#define ENTITY_HPP
|
||||
|
||||
#include "open_engine/core.hpp"
|
||||
|
||||
#include "open_engine/scene/scene.hpp"
|
||||
|
||||
#include <entt/entt.hpp>
|
||||
#include <utility>
|
||||
|
||||
namespace OpenEngine {
|
||||
class Entity
|
||||
{
|
||||
public:
|
||||
Entity() = default;
|
||||
Entity(entt::entity handle, Scene* scene);
|
||||
Entity(const Entity& other) = default;
|
||||
|
||||
template<typename T, typename ... Args>
|
||||
T& AddComponents(Args&&... args)
|
||||
{
|
||||
OE_ASSERT(!HasComponent<T>(), "Entity already has component.");
|
||||
return scene->registry.emplace<T>(handle, std::forward<Args>(args)...);
|
||||
};
|
||||
|
||||
template<typename T, typename ... Args>
|
||||
T& GetComponents(Args&&... args)
|
||||
{
|
||||
OE_ASSERT(HasComponent<T>(), "Entity doesn't have component.");
|
||||
return scene->registry.get<T>(handle);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
void RemoveComponents()
|
||||
{
|
||||
OE_ASSERT(HasComponent<T>(), "Entity doesn't have component.");
|
||||
scene->registry.remove<T>(handle);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
bool HasComponent()
|
||||
{
|
||||
return scene->registry.all_of<T>(handle);
|
||||
};
|
||||
|
||||
operator bool() const { return handle != entt::null; };
|
||||
operator uint32_t() const { return (uint32_t)handle; };
|
||||
|
||||
bool operator ==(const Entity& other) const {
|
||||
return handle == other.handle && scene == other.scene;
|
||||
};
|
||||
|
||||
bool operator !=(const Entity& other) const {
|
||||
return !operator==(other);
|
||||
};
|
||||
|
||||
private:
|
||||
entt::entity handle { entt::null };
|
||||
OpenEngine::Scene* scene = nullptr;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif // ENTITY_HPP
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef NATIVE_SCRIPTABLE_ENTITY_HPP
|
||||
#define NATIVE_SCRIPTABLE_ENTITY_HPP
|
||||
|
||||
#include "open_engine/scene/entity.hpp"
|
||||
|
||||
namespace OpenEngine {
|
||||
|
||||
class NativeScriptableEntity
|
||||
{
|
||||
public:
|
||||
virtual ~NativeScriptableEntity() = default;
|
||||
template <typename T>
|
||||
T& GetComponent() { return entity.GetComponents<T>(); };
|
||||
|
||||
protected:
|
||||
virtual void OnCreate() {};
|
||||
virtual void OnUpdate() {};
|
||||
virtual void OnDestroy() {};
|
||||
|
||||
private:
|
||||
Entity entity;
|
||||
|
||||
friend class Scene;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // NATIVE_SCRIPTABLE_ENTITY_HPP
|
||||
33
open_engine/include/open_engine/scene/scene.hpp
Normal file
33
open_engine/include/open_engine/scene/scene.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef SCENE_HPP
|
||||
#define SCENE_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <entt/entt.hpp>
|
||||
|
||||
namespace OpenEngine {
|
||||
|
||||
class Entity;
|
||||
|
||||
class Scene
|
||||
{
|
||||
public:
|
||||
Scene() {};
|
||||
~Scene() = default;
|
||||
|
||||
Entity CreateEntity(const std::string& name = std::string());
|
||||
|
||||
void OnUpdate();
|
||||
void OnViewportResize(uint32_t width, uint32_t height);
|
||||
|
||||
entt::registry& GetRegistry() { return registry; };
|
||||
|
||||
private:
|
||||
entt::registry registry;
|
||||
|
||||
uint32_t viewport_width = 0, viewport_height = 0;
|
||||
|
||||
friend class Entity;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // SCENE_HPP
|
||||
74
open_engine/include/open_engine/scene/scene_camera.hpp
Normal file
74
open_engine/include/open_engine/scene/scene_camera.hpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#ifndef SCENE_CAMERA_HPP
|
||||
#define SCENE_CAMERA_HPP
|
||||
|
||||
#include "open_engine/renderer/camera.hpp"
|
||||
#include <cstdint>
|
||||
|
||||
namespace OpenEngine {
|
||||
class SceneCamera : public Camera
|
||||
{
|
||||
public:
|
||||
enum class ProjectionType { Perspective = 0, Orthographic = 1 };
|
||||
public:
|
||||
SceneCamera();
|
||||
virtual ~SceneCamera() = default;
|
||||
|
||||
void SetViewportSize(uint32_t width, uint32_t height);
|
||||
|
||||
ProjectionType GetProjectionType() { return projection_type; };
|
||||
void SetProjectionType(ProjectionType type) {
|
||||
projection_type = type;
|
||||
RecalculateProjection();
|
||||
};
|
||||
|
||||
void SetOrthographic(float size, float near_clip, float far_clip);
|
||||
void SetPerspective(float fov, float near_clip, float far_clip);
|
||||
|
||||
float GetOrthographicSize() const { return orthographic_size; }
|
||||
void SetOrthographicSize(float size) {
|
||||
orthographic_size = size;
|
||||
RecalculateProjection();
|
||||
};
|
||||
float GetOrthographicNearClip() const { return orthographic_near; };
|
||||
void SetOrthographicNearClip(float near) {
|
||||
orthographic_near = near;
|
||||
RecalculateProjection();
|
||||
};
|
||||
float GetOrthographicFarClip() const { return orthographic_far; };
|
||||
void SetOrthographicFarClip(float far) {
|
||||
orthographic_far = far;
|
||||
RecalculateProjection();
|
||||
};
|
||||
|
||||
float GetVerticalFov() const { return fov; }
|
||||
void SetVerticalFov(float fov_value) {
|
||||
fov = fov_value;
|
||||
RecalculateProjection();
|
||||
};
|
||||
float GetPerspectiveNearClip() const { return perspective_near; };
|
||||
void SetPerspectiveNearClip(float near) {
|
||||
perspective_near = near;
|
||||
RecalculateProjection();
|
||||
};
|
||||
float GetPerspectiveFarClip() const { return perspective_far; };
|
||||
void SetPerspectiveFarClip(float far) {
|
||||
perspective_far = far;
|
||||
RecalculateProjection();
|
||||
};
|
||||
|
||||
private:
|
||||
void RecalculateProjection();
|
||||
|
||||
private:
|
||||
ProjectionType projection_type = ProjectionType::Orthographic;
|
||||
float orthographic_size = 10.0f;
|
||||
float orthographic_near = -1.0f, orthographic_far = 1.0f;
|
||||
|
||||
float fov = 45.0f;
|
||||
float perspective_near = 0.01f, perspective_far = 1000.0f;
|
||||
|
||||
float aspect_ratio = 0.0f;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // SCENE_CAMERA_HPP
|
||||
@@ -1,14 +1,14 @@
|
||||
#include "imgui.h"
|
||||
#include "instrumentor.hpp"
|
||||
#include <pch.hpp>
|
||||
|
||||
#include <imgui/imgui_layer.hpp>
|
||||
|
||||
#include <opengl/imgui_opengl.h>
|
||||
#include <opengl/imgui_glfw.h>
|
||||
#include <instrumentor.hpp>
|
||||
#include <application.hpp>
|
||||
#include <layer.hpp>
|
||||
|
||||
#include "imgui.h"
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
@@ -340,10 +340,10 @@ namespace OpenEngine {
|
||||
|
||||
void ImGuiLayer::OnEvent(Event& event)
|
||||
{
|
||||
/*
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
event.handled |= event.IsInCategory(EventCategoryMouse) & io.WantCaptureMouse;
|
||||
event.handled |= event.IsInCategory(EventCategoryKeyboard) & io.WantCaptureKeyboard;
|
||||
*/
|
||||
if (block_events) {
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
event.handled |= event.IsInCategory(EventCategoryMouse) & io.WantCaptureMouse;
|
||||
event.handled |= event.IsInCategory(EventCategoryKeyboard) & io.WantCaptureKeyboard;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include "logging.hpp"
|
||||
#include <pch.hpp>
|
||||
|
||||
#include <core.hpp>
|
||||
@@ -6,6 +7,7 @@
|
||||
#include <glad/glad.h>
|
||||
|
||||
namespace OpenEngine {
|
||||
static const uint32_t max_framebuffer_size = 8192;
|
||||
OpenGLFramebuffer::OpenGLFramebuffer(const FramebufferSpecification& spec)
|
||||
: specs(spec)
|
||||
{
|
||||
@@ -61,6 +63,10 @@ namespace OpenEngine {
|
||||
|
||||
void OpenGLFramebuffer::Resize(uint32_t width, uint32_t height)
|
||||
{
|
||||
if (width == 0 || height == 0 || width > max_framebuffer_size || height > max_framebuffer_size) {
|
||||
OE_CORE_WARN("Attempted to resize framebuffer to {} {}", width, height);
|
||||
return;
|
||||
}
|
||||
specs.width = width;
|
||||
specs.height = height;
|
||||
|
||||
|
||||
7
open_engine/src/open_engine/renderer/camera.cpp
Normal file
7
open_engine/src/open_engine/renderer/camera.cpp
Normal file
@@ -0,0 +1,7 @@
|
||||
#include <pch.hpp>
|
||||
|
||||
#include <open_engine/renderer/camera.hpp>
|
||||
|
||||
namespace OpenEngine {
|
||||
|
||||
}
|
||||
@@ -7,10 +7,11 @@
|
||||
#include <renderer/buffer.hpp>
|
||||
#include <renderer/shader.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
#include <glm/fwd.hpp>
|
||||
#include <glm/trigonometric.hpp>
|
||||
#include <glm/ext/quaternion_common.hpp>
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
#include <glm/trigonometric.hpp>
|
||||
#include <glm/fwd.hpp>
|
||||
#include <cstdint>
|
||||
|
||||
namespace OpenEngine {
|
||||
struct QuadVertex
|
||||
@@ -135,6 +136,21 @@ namespace OpenEngine {
|
||||
renderer_data.texture_slot_index = 1;
|
||||
}
|
||||
|
||||
void Renderer2D::BeginScene(const Camera& camera, const glm::mat4& transform)
|
||||
{
|
||||
OE_PROFILE_FUNCTION();
|
||||
|
||||
glm::mat4 view_projection = camera.GetProjection() * glm::inverse(transform);
|
||||
|
||||
renderer_data.texture_shader->Bind();
|
||||
renderer_data.texture_shader->SetMat4("u_ViewProjection", view_projection);
|
||||
|
||||
renderer_data.quad_index_count = 0;
|
||||
renderer_data.quad_vertex_ptr = renderer_data.quad_vertex_base;
|
||||
|
||||
renderer_data.texture_slot_index = 1;
|
||||
}
|
||||
|
||||
void Renderer2D::EndScene()
|
||||
{
|
||||
OE_PROFILE_FUNCTION();
|
||||
@@ -196,17 +212,7 @@ namespace OpenEngine {
|
||||
* glm::rotate(glm::mat4(1.0f), glm::radians(transform_data.rotation), { 0.0f, 0.0f, 1.0f })
|
||||
* glm::scale(glm::mat4(1.0f), transform_data.size);
|
||||
|
||||
for (size_t i = 0; i < quad_vertex_count; i++) {
|
||||
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[i];
|
||||
renderer_data.quad_vertex_ptr->color = color;
|
||||
renderer_data.quad_vertex_ptr->tex_coord = texture_coords[i];
|
||||
renderer_data.quad_vertex_ptr->tex_index = texture_index;
|
||||
renderer_data.quad_vertex_ptr->tiling_factor = tiling_factor;
|
||||
renderer_data.quad_vertex_ptr++;
|
||||
}
|
||||
|
||||
renderer_data.quad_index_count += 6;
|
||||
renderer_data.stats.quad_count++;
|
||||
DrawQuad(transform, texture);
|
||||
}
|
||||
|
||||
void Renderer2D::DrawQuad(const Transform& transform_data, const Ref<Subtexture2D>& subtexture, float tiling_factor)
|
||||
@@ -246,17 +252,7 @@ namespace OpenEngine {
|
||||
* glm::rotate(glm::mat4(1.0f), glm::radians(transform_data.rotation), { 0.0f, 0.0f, 1.0f })
|
||||
* glm::scale(glm::mat4(1.0f), transform_data.size);
|
||||
|
||||
for (size_t i = 0; i < quad_vertex_count; i++) {
|
||||
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[i];
|
||||
renderer_data.quad_vertex_ptr->color = color;
|
||||
renderer_data.quad_vertex_ptr->tex_coord = texture_coords[i];
|
||||
renderer_data.quad_vertex_ptr->tex_index = texture_index;
|
||||
renderer_data.quad_vertex_ptr->tiling_factor = tiling_factor;
|
||||
renderer_data.quad_vertex_ptr++;
|
||||
}
|
||||
|
||||
renderer_data.quad_index_count += 6;
|
||||
renderer_data.stats.quad_count++;
|
||||
DrawQuad(transform, subtexture);
|
||||
}
|
||||
|
||||
void Renderer2D::DrawQuad(const Transform& transform_data, const glm::vec4& color)
|
||||
@@ -277,6 +273,104 @@ namespace OpenEngine {
|
||||
* glm::scale(glm::mat4(1.0f), transform_data.size);
|
||||
|
||||
|
||||
DrawQuad(transform, color);
|
||||
}
|
||||
|
||||
void Renderer2D::DrawQuad(const glm::mat4& transform, const Ref<Texture2D>& texture, float tiling_factor)
|
||||
{
|
||||
OE_PROFILE_FUNCTION();
|
||||
|
||||
constexpr size_t quad_vertex_count = 4;
|
||||
constexpr glm::vec2 texture_coords[] = { { 0.0f, 0.0f }, { 1.0f, 0.0f }, { 1.0f, 1.0f }, { 0.0f, 1.0f }};
|
||||
|
||||
if (renderer_data.quad_index_count >= Renderer2DData::max_indices)
|
||||
FlushAndReset();
|
||||
|
||||
constexpr glm::vec4 color = glm::vec4(1.0f);
|
||||
|
||||
float texture_index = 0;
|
||||
for (uint32_t i = 1; i < renderer_data.texture_slot_index; i++) {
|
||||
if (*renderer_data.texture_slots[i].get() == *texture.get()) {
|
||||
texture_index = (float)i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (texture_index == 0) {
|
||||
if (renderer_data.texture_slot_index >= Renderer2DData::max_texture_slots)
|
||||
FlushAndReset();
|
||||
|
||||
texture_index = (float)renderer_data.texture_slot_index;
|
||||
renderer_data.texture_slots[renderer_data.texture_slot_index] = texture;
|
||||
renderer_data.texture_slot_index++;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < quad_vertex_count; i++) {
|
||||
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[i];
|
||||
renderer_data.quad_vertex_ptr->color = color;
|
||||
renderer_data.quad_vertex_ptr->tex_coord = texture_coords[i];
|
||||
renderer_data.quad_vertex_ptr->tex_index = texture_index;
|
||||
renderer_data.quad_vertex_ptr->tiling_factor = tiling_factor;
|
||||
renderer_data.quad_vertex_ptr++;
|
||||
}
|
||||
|
||||
renderer_data.quad_index_count += 6;
|
||||
renderer_data.stats.quad_count++;
|
||||
}
|
||||
|
||||
void Renderer2D::DrawQuad(const glm::mat4& transform, const Ref<Subtexture2D>& subtexture, float tiling_factor)
|
||||
{
|
||||
OE_PROFILE_FUNCTION();
|
||||
|
||||
constexpr size_t quad_vertex_count = 4;
|
||||
const glm::vec2* texture_coords = subtexture->GetCoords();
|
||||
const Ref<Texture2D>& texture = subtexture->GetTexture();
|
||||
|
||||
if (renderer_data.quad_index_count >= Renderer2DData::max_indices)
|
||||
FlushAndReset();
|
||||
|
||||
constexpr glm::vec4 color = glm::vec4(1.0f);
|
||||
|
||||
float texture_index = 0;
|
||||
for (uint32_t i = 1; i < renderer_data.texture_slot_index; i++) {
|
||||
if (*renderer_data.texture_slots[i].get() == *texture.get()) {
|
||||
texture_index = (float)i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (texture_index == 0) {
|
||||
if (renderer_data.texture_slot_index >= Renderer2DData::max_texture_slots)
|
||||
FlushAndReset();
|
||||
|
||||
texture_index = (float)renderer_data.texture_slot_index;
|
||||
renderer_data.texture_slots[renderer_data.texture_slot_index] = texture;
|
||||
renderer_data.texture_slot_index++;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < quad_vertex_count; i++) {
|
||||
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[i];
|
||||
renderer_data.quad_vertex_ptr->color = color;
|
||||
renderer_data.quad_vertex_ptr->tex_coord = texture_coords[i];
|
||||
renderer_data.quad_vertex_ptr->tex_index = texture_index;
|
||||
renderer_data.quad_vertex_ptr->tiling_factor = tiling_factor;
|
||||
renderer_data.quad_vertex_ptr++;
|
||||
}
|
||||
|
||||
renderer_data.quad_index_count += 6;
|
||||
renderer_data.stats.quad_count++;
|
||||
}
|
||||
|
||||
void Renderer2D::DrawQuad(const glm::mat4& transform, const glm::vec4& color)
|
||||
{
|
||||
OE_PROFILE_FUNCTION();
|
||||
|
||||
constexpr size_t quad_vertex_count = 4;
|
||||
constexpr glm::vec2 texture_coords[] = { { 0.0f, 0.0f }, { 1.0f, 0.0f }, { 1.0f, 1.0f }, { 0.0f, 1.0f }};
|
||||
|
||||
if (renderer_data.quad_index_count >= Renderer2DData::max_indices)
|
||||
FlushAndReset();
|
||||
|
||||
for (size_t i = 0; i < quad_vertex_count; i++) {
|
||||
renderer_data.quad_vertex_ptr->position = transform * renderer_data.quad_vertex_positioning[i];
|
||||
renderer_data.quad_vertex_ptr->color = color;
|
||||
@@ -290,6 +384,7 @@ namespace OpenEngine {
|
||||
renderer_data.stats.quad_count++;
|
||||
}
|
||||
|
||||
|
||||
void Renderer2D::ResetStats()
|
||||
{
|
||||
memset(&renderer_data.stats, 0, sizeof(Statistics));
|
||||
|
||||
60
open_engine/src/open_engine/scene/camera.cpp
Executable file
60
open_engine/src/open_engine/scene/camera.cpp
Executable file
@@ -0,0 +1,60 @@
|
||||
#include <glm/trigonometric.hpp>
|
||||
#include <pch.hpp>
|
||||
|
||||
#include <scene/scene_camera.hpp>
|
||||
#include <glm/ext/matrix_clip_space.hpp>
|
||||
|
||||
namespace OpenEngine {
|
||||
SceneCamera::SceneCamera()
|
||||
{
|
||||
RecalculateProjection();
|
||||
}
|
||||
|
||||
void SceneCamera::SetOrthographic(float size, float near_clip, float far_clip)
|
||||
{
|
||||
projection_type = ProjectionType::Orthographic;
|
||||
orthographic_size = size;
|
||||
orthographic_near = near_clip;
|
||||
orthographic_far = far_clip;
|
||||
|
||||
RecalculateProjection();
|
||||
}
|
||||
|
||||
void SceneCamera::SetPerspective(float fov_value, float near_clip, float far_clip)
|
||||
{
|
||||
projection_type = ProjectionType::Perspective;
|
||||
fov = fov_value;
|
||||
perspective_near = near_clip;
|
||||
perspective_far = far_clip;
|
||||
|
||||
RecalculateProjection();
|
||||
}
|
||||
|
||||
void SceneCamera::SetViewportSize(uint32_t width, uint32_t height)
|
||||
{
|
||||
aspect_ratio = (float)width / (float)height;
|
||||
|
||||
RecalculateProjection();
|
||||
}
|
||||
|
||||
void SceneCamera::RecalculateProjection()
|
||||
{
|
||||
|
||||
if (projection_type == ProjectionType::Orthographic) {
|
||||
float ortho_left = -orthographic_size * aspect_ratio * 0.5;
|
||||
float ortho_right = orthographic_size * aspect_ratio * 0.5;
|
||||
float ortho_bottom = orthographic_size * -0.5;
|
||||
float ortho_top = orthographic_size * 0.5;
|
||||
|
||||
projection = glm::ortho(
|
||||
ortho_left, ortho_right,
|
||||
ortho_bottom, ortho_top,
|
||||
orthographic_near, orthographic_far);
|
||||
}
|
||||
else if (projection_type == ProjectionType::Perspective) {
|
||||
projection = glm::perspective(
|
||||
glm::radians(fov), aspect_ratio,
|
||||
perspective_near, perspective_far);
|
||||
}
|
||||
}
|
||||
}
|
||||
10
open_engine/src/open_engine/scene/entity.cpp
Normal file
10
open_engine/src/open_engine/scene/entity.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include <pch.hpp>
|
||||
|
||||
#include <scene/entity.hpp>
|
||||
|
||||
namespace OpenEngine {
|
||||
Entity::Entity(entt::entity handle, Scene* scene)
|
||||
: handle(handle), scene(scene)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
#include <pch.hpp>
|
||||
78
open_engine/src/open_engine/scene/scene.cpp
Normal file
78
open_engine/src/open_engine/scene/scene.cpp
Normal file
@@ -0,0 +1,78 @@
|
||||
#include <pch.hpp>
|
||||
|
||||
#include <scene/scene.hpp>
|
||||
#include <renderer/renderer2d.hpp>
|
||||
|
||||
#include <scene/entity.hpp>
|
||||
#include <scene/components.hpp>
|
||||
|
||||
namespace OpenEngine {
|
||||
Entity Scene::CreateEntity(const std::string& name)
|
||||
{
|
||||
Entity entity = { registry.create(), this };
|
||||
|
||||
auto& tag = entity.AddComponents<TagComponent>();
|
||||
tag.tag = name.empty() ? "Entity" : name;
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
void Scene::OnUpdate()
|
||||
{
|
||||
{
|
||||
OE_PROFILE_SCOPE("Updating Sripts");
|
||||
|
||||
registry.view<NativeScriptComponent>().each([=, this](auto entity, auto& nsc)
|
||||
{
|
||||
if (!nsc.instance) {
|
||||
nsc.instance = nsc.InstanciateScript();
|
||||
nsc.instance->entity = Entity{ entity, this };
|
||||
nsc.instance->OnCreate();
|
||||
}
|
||||
|
||||
nsc.instance->OnUpdate();
|
||||
});
|
||||
}
|
||||
|
||||
Camera* main_camera = nullptr;
|
||||
glm::mat4* main_transform = nullptr;
|
||||
{
|
||||
auto camera_view = registry.view<CameraComponent, TransformComponent>();
|
||||
for (auto entity : camera_view) {
|
||||
auto [camera_component, transform] = camera_view.get<CameraComponent, TransformComponent>(entity);
|
||||
|
||||
if (camera_component.primary) {
|
||||
main_camera = &camera_component.camera;
|
||||
main_transform = &transform.transform;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (main_camera) {
|
||||
Renderer2D::BeginScene(main_camera->GetProjection(), *main_transform);
|
||||
|
||||
auto view = registry.view<TransformComponent, SpriteRendererComponent>();
|
||||
|
||||
for (const auto& entity : view) {
|
||||
auto [transform, sprite] = view.get<TransformComponent, SpriteRendererComponent>(entity);
|
||||
Renderer2D::DrawQuad(transform.transform, sprite.color);
|
||||
}
|
||||
|
||||
Renderer2D::EndScene();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Scene::OnViewportResize(uint32_t width, uint32_t height)
|
||||
{
|
||||
viewport_width = width;
|
||||
viewport_height = height;
|
||||
|
||||
auto view = registry.view<CameraComponent>();
|
||||
for (auto entity : view) {
|
||||
auto& camera_comp = view.get<CameraComponent>(entity);
|
||||
if (!camera_comp.fixed_aspect_ratio)
|
||||
camera_comp.camera.SetViewportSize(width, height);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user