diff --git a/assets/shaders/color3d.glsl b/assets/shaders/color3d.glsl new file mode 100644 index 0000000..8bdf313 --- /dev/null +++ b/assets/shaders/color3d.glsl @@ -0,0 +1,93 @@ +#type vertex +#version 450 core + +layout(location = 0) in vec3 a_Position; +layout(location = 1) in vec4 a_Color; +layout(location = 2) in vec3 a_Normal; +layout(location = 3) in vec2 a_TexCoord; +layout(location = 4) in int a_EntityID; + +layout(std140, binding = 0) uniform Camera +{ + mat4 u_ViewProjection; + vec3 u_ViewPosition; +}; + +layout(std140, binding = 1) uniform Transform +{ + mat4 u_Transform; +}; + +struct VertexOutput +{ + vec4 Color; + vec2 TexCoord; + vec3 Normal; + vec3 ViewPos; +}; + +layout (location = 0) out VertexOutput Output; +layout (location = 4) out flat int v_EntityID; +layout (location = 5) out vec4 v_Pos; + +void main() +{ + Output.Color = a_Color; + Output.TexCoord = a_TexCoord; + Output.Normal = mat3(transpose(inverse(u_Transform))) * a_Normal; + Output.ViewPos = u_ViewPosition; + + v_EntityID = a_EntityID; + + v_Pos = u_Transform * vec4(a_Position, 1.0); + gl_Position = u_ViewProjection * v_Pos; +} + +#type fragment +#version 450 core + +layout (location = 0) out vec4 color; +layout (location = 1) out int color2; + +struct VertexOutput +{ + vec4 Color; + vec2 TexCoord; + vec3 Normal; + vec3 ViewPos; +}; + +layout (location = 0) in VertexOutput Input; +layout (location = 4) in flat int v_EntityID; +layout (location = 5) in vec4 v_Pos; + +void main() +{ + vec3 lightPos = vec3(0.0f, 1.0f, 0.0f); + vec3 lightColor = vec3(1.0f, 1.0f, 1.0f); + + // Ambiant lighting + float ambientStrength = 0.8; + vec3 ambient = ambientStrength * lightColor; + + // Diffuse lighting + vec3 norm = normalize(Input.Normal); + vec3 lightDir = normalize(lightPos - v_Pos.xyz); + + float diff = max(dot(norm, lightDir), 0.0); + vec3 diffuse = diff * lightColor; + + // Specular highlight + float specularStrength = 0.5; + vec3 viewDir = normalize(Input.ViewPos - v_Pos.xyz); // .xyz here too + vec3 reflectDir = reflect(-lightDir, norm); + + float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32); + vec3 specular = specularStrength * spec * lightColor; + + vec3 result = (ambient + diffuse + specular) * Input.Color.rgb; // objectColor → Input.Color.rgb + + // Output + color = vec4(result, 1.0); // vec3 → vec4 + color2 = v_EntityID; +} diff --git a/conanfile.txt b/conanfile.txt index f26bc0b..b7fcf28 100644 --- a/conanfile.txt +++ b/conanfile.txt @@ -3,6 +3,7 @@ imgui/1.92.5-docking spdlog/1.16.0 entt/3.16.0 yaml-cpp/0.8.0 +joltphysics/5.2.0 [generators] CMakeDeps diff --git a/editor/include/editor.hpp b/editor/include/editor.hpp index beed995..edf230b 100755 --- a/editor/include/editor.hpp +++ b/editor/include/editor.hpp @@ -1,14 +1,9 @@ #ifndef EDITOR_HPP #define EDITOR_HPP -#include #include -#include "open_engine/application.hpp" -#include "open_engine/logging.hpp" -#include "open_engine/renderer/texture.hpp" -#include "open_engine/scene/entity.hpp" -#include "open_engine/scene/scene_serializer.hpp" +#include "open_engine/scene/components.hpp" #include "panels/content_browser.hpp" #include "panels/scene_hierarchy.hpp" @@ -19,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -95,6 +91,34 @@ namespace OpenEngine { icons["play"] = Texture2D::Create("resources/textures/icons/play.png"); icons["stop"] = Texture2D::Create("resources/textures/icons/stop.png"); + + Entity cube = scene->CreateEntity("cube"); + + + cube.AddComponent(); + + Ref mesh = CreateCube(cube); + + cube.AddComponent(mesh); + + Entity cube2 = scene->CreateEntity("cube2"); + cube2.AddComponent(); + cube2.GetComponents().translation = {2, 0, 0}; + + Ref mesh2 = CreateCube(cube2); + + cube2.AddComponent(mesh2); + OE_DEBUG("cubes is like {} {}", (uint32_t)cube, (uint32_t)cube2); + + /* + auto view = scene->GetRegistry().view(); + + for (auto& entity : view) { + auto tag = view->get(entity); + OE_DEBUG("entity: {}, tag: {}", (uint32_t)entity, tag.tag); + } + */ + // TODO: Add file texture. Get free icons and add license //icons["folder"] = Texture2D::Create("resources/textures/icons/folder.png"); /* @@ -140,7 +164,7 @@ namespace OpenEngine { } framebuffer->Bind(); - Renderer2D::ResetStats(); + //Renderer2D::ResetStats(); RenderCommand::SetClearColor({0.11f, 0.11f, 0.15f, 1.0f}); RenderCommand::Clear(); framebuffer->ClearBufferI(1, -1); @@ -282,12 +306,16 @@ namespace OpenEngine { void DrawViewport() { ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2{ 0, 0 }); - ImGui::Begin("Viewport"); + + bool was_focused = viewport_focused; viewport_focused = ImGui::IsWindowFocused(ImGuiFocusedFlags_RootWindow); viewport_hovered = ImGui::IsWindowHovered(ImGuiHoveredFlags_RootWindow); Application::Get().GetImGuiLayer()->SetBlockEvents((!viewport_focused && !viewport_hovered) || ImGui::IsAnyItemActive()); + if (viewport_focused && !was_focused) + editor_camera.ResetMousePosition(); + ImVec2 viewport_panel_size = ImGui::GetContentRegionAvail(); viewport_size = { viewport_panel_size.x, viewport_panel_size.y }; @@ -318,7 +346,7 @@ namespace OpenEngine { void DrawStats() { - auto stats = Renderer2D::GetStats(); + //auto stats = Renderer2D::GetStats(); static float time = 0; time += Time::DeltaTime(); @@ -330,17 +358,17 @@ namespace OpenEngine { ImGui::Begin("Statistics"); ImGui::SeparatorText("Performance"); ImGui::Text("FPS: %0.0f", 1 / Time::DeltaTime()); - ImGui::Text("Renderer2D:"); - ImGui::Text("\t\tDraw calls: %d", stats.draw_calls); - ImGui::Text("\t\tQuad count: %d", stats.quad_count); - ImGui::Text("\t\tVertices count: %d", stats.GetTotalVertexCount()); - ImGui::Text("\t\tIndices count: %d", stats.GetTotalIndexCount()); - if (selected_entity) { - ImGui::SeparatorText("Entities"); - ImGui::Text("Selected entity:"); - ImGui::Text("\t\tname: %s", selected_entity.GetComponents().tag.c_str()); - ImGui::Text("\t\tid: %d", (uint32_t)selected_entity); - } + //ImGui::Text("Renderer2D:"); + //ImGui::Text("\t\tDraw calls: %d", stats.draw_calls); + //ImGui::Text("\t\tQuad count: %d", stats.quad_count); + //ImGui::Text("\t\tVertices count: %d", stats.GetTotalVertexCount()); + //ImGui::Text("\t\tIndices count: %d", stats.GetTotalIndexCount()); + //if (selected_entity) { + // ImGui::SeparatorText("Entities"); + // ImGui::Text("Selected entity:"); + // ImGui::Text("\t\tname: %s", selected_entity.GetComponents().tag.c_str()); + // ImGui::Text("\t\tid: %d", (uint32_t)selected_entity); + //} ImGui::End(); }; diff --git a/editor/include/editor_component.hpp b/editor/include/editor_component.hpp index df2beb6..bc5623e 100644 --- a/editor/include/editor_component.hpp +++ b/editor/include/editor_component.hpp @@ -13,12 +13,10 @@ namespace OpenEngine { const std::array labels = {"x", "y", "z"}); void TagOnImGuiRender(entt::registry& registry, entt::entity entity); - void TransformOnImGuiRender(entt::registry& registry, entt::entity entity); - void SpriteOnImGuiRender(entt::registry& registry, entt::entity entity); - void CameraOnImGuiRender(entt::registry& registry, entt::entity entity); + void MeshOnImGuiRender(entt::registry®istry, entt::entity entity); } #endif // EDITOR_COMPONENT_HPP diff --git a/editor/include/panels/content_browser.hpp b/editor/include/panels/content_browser.hpp index b799ce3..f68ca4d 100644 --- a/editor/include/panels/content_browser.hpp +++ b/editor/include/panels/content_browser.hpp @@ -6,6 +6,8 @@ namespace OpenEngine { + class EditorLayer; + class ContentBrowserPanel { public: diff --git a/editor/src/editor_component.cpp b/editor/src/editor_component.cpp index ffb847b..051f377 100644 --- a/editor/src/editor_component.cpp +++ b/editor/src/editor_component.cpp @@ -170,4 +170,9 @@ namespace OpenEngine { } }; + + void MeshOnImGuiRender(entt::registry®istry, entt::entity entity) + { + + } } diff --git a/editor/src/panels/scene_hierarchy.cpp b/editor/src/panels/scene_hierarchy.cpp index 8245820..9d734c0 100644 --- a/editor/src/panels/scene_hierarchy.cpp +++ b/editor/src/panels/scene_hierarchy.cpp @@ -22,6 +22,7 @@ namespace OpenEngine { RegisterDrawer("Transform", &TransformOnImGuiRender); RegisterDrawer("Sprite Renderer", &SpriteOnImGuiRender); RegisterDrawer("Camera", &CameraOnImGuiRender); + RegisterDrawer("Mesh", &MeshOnImGuiRender); scene = context; selected_context = {}; @@ -32,17 +33,22 @@ namespace OpenEngine { ImGui::SeparatorText("Add"); if (!selected_context.HasComponent()) if (ImGui::MenuItem("Transform")) { - selected_context.AddComponents(); + selected_context.AddComponent(); ImGui::CloseCurrentPopup(); } if (!selected_context.HasComponent()) if (ImGui::MenuItem("Sprite")) { - selected_context.AddComponents(); + selected_context.AddComponent(); ImGui::CloseCurrentPopup(); } if (!selected_context.HasComponent()) if (ImGui::MenuItem("Camera")) { - selected_context.AddComponents(); + selected_context.AddComponent(); + ImGui::CloseCurrentPopup(); + } + if (!selected_context.HasComponent()) + if (ImGui::MenuItem("Mesh")) { + selected_context.AddComponent(); ImGui::CloseCurrentPopup(); } } @@ -202,6 +208,7 @@ namespace OpenEngine { entity.GetComponents(); TagOnImGuiRender(reg, entity); + std::vector component_to_delete; // 0 is null/invalid in entt usually // Iterate through every component type entt knows about for (auto [id, storage] : reg.storage()) { diff --git a/open_engine/CMakeLists.txt b/open_engine/CMakeLists.txt index a531afd..808c01d 100644 --- a/open_engine/CMakeLists.txt +++ b/open_engine/CMakeLists.txt @@ -13,6 +13,7 @@ add_definitions( -DOE_ENABLE_ASSERTS ) find_package(imgui REQUIRED) find_package(EnTT REQUIRED) find_package(yaml-cpp REQUIRED) +find_package(Jolt REQUIRED) file(GLOB_RECURSE SRC_FILES "src/*.cpp") file(GLOB IMGUIZMO_SRC_FILES "${CMAKE_SOURCE_DIR}/vendor/ImGuizmo/*.cpp") @@ -52,6 +53,16 @@ target_link_libraries(${PROJECT_EXECUTABLE_NAME} PUBLIC X11 yaml-cpp::yaml-cpp nfd + shaderc_combined + glslang + glslang-default-resource-limits + SPIRV + SPIRV-Tools + SPIRV-Tools-opt + spirv-cross-core + spirv-cross-glsl + spirv-cross-reflect + Jolt::Jolt ) #target_link_directories(${PROJECT_EXECUTABLE_NAME} PRIVATE diff --git a/open_engine/include/open_engine.hpp b/open_engine/include/open_engine.hpp index e3927ee..ba00954 100644 --- a/open_engine/include/open_engine.hpp +++ b/open_engine/include/open_engine.hpp @@ -25,6 +25,7 @@ #include "open_engine/renderer/subtexture2d.hpp" #include "open_engine/renderer/framebuffer.hpp" #include "open_engine/renderer/renderer2d.hpp" +#include "open_engine/renderer/renderer3d.hpp" #include "open_engine/renderer/renderer.hpp" #include "open_engine/renderer/texture.hpp" #include "open_engine/renderer/buffer.hpp" diff --git a/open_engine/include/open_engine/opengl/opengl_buffer.hpp b/open_engine/include/open_engine/opengl/opengl_buffer.hpp index f610385..4b70012 100644 --- a/open_engine/include/open_engine/opengl/opengl_buffer.hpp +++ b/open_engine/include/open_engine/opengl/opengl_buffer.hpp @@ -29,13 +29,14 @@ namespace OpenEngine { class OpenGLIndexBuffer : public IndexBuffer { public: - OpenGLIndexBuffer(uint32_t* indices, uint32_t count); + OpenGLIndexBuffer(const uint32_t* indices, uint32_t count); virtual ~OpenGLIndexBuffer(); virtual void Bind() const override; virtual void UnBind() const override; virtual uint32_t GetCount() const override { return count; }; + virtual uint32_t GetID() const override { return id; }; private: unsigned int id; diff --git a/open_engine/include/open_engine/renderer/buffer.hpp b/open_engine/include/open_engine/renderer/buffer.hpp index a282c2d..690748c 100644 --- a/open_engine/include/open_engine/renderer/buffer.hpp +++ b/open_engine/include/open_engine/renderer/buffer.hpp @@ -1,6 +1,8 @@ #ifndef BUFFER_HPP #define BUFFER_HPP +#include "open_engine/ref_scope.hpp" + #include #include #include @@ -82,8 +84,9 @@ namespace OpenEngine { virtual void UnBind() const = 0; virtual uint32_t GetCount() const = 0; + virtual uint32_t GetID() const = 0; - static Ref Create(uint32_t* indices, uint32_t count); + static Ref Create(const uint32_t* indices, uint32_t count); }; } diff --git a/open_engine/include/open_engine/renderer/editor_camera.hpp b/open_engine/include/open_engine/renderer/editor_camera.hpp index aaa242e..6c4d959 100644 --- a/open_engine/include/open_engine/renderer/editor_camera.hpp +++ b/open_engine/include/open_engine/renderer/editor_camera.hpp @@ -31,6 +31,7 @@ namespace OpenEngine { glm::vec3 GetForwardDirection() const; const glm::vec3& GetPosition() const { return position; } glm::quat GetOrientation() const; + void ResetMousePosition(); float GetPitch() const { return pitch; } float GetYaw() const { return yaw; } @@ -61,6 +62,7 @@ namespace OpenEngine { glm::vec3 focal_point = { 0.0f, 0.0f, 0.0f }; glm::vec2 initial_mouse_position = { 0.0f, 0.0f }; + bool mouse_button_was_pressed = false; float distance = 10.0f; float pitch = 0.0f, yaw = 0.0f; diff --git a/open_engine/include/open_engine/renderer/renderer3d.hpp b/open_engine/include/open_engine/renderer/renderer3d.hpp new file mode 100644 index 0000000..e4647d9 --- /dev/null +++ b/open_engine/include/open_engine/renderer/renderer3d.hpp @@ -0,0 +1,61 @@ +#ifndef RENDERER3D_HPP +#define RENDERER3D_HPP + +#include "open_engine/renderer/editor_camera.hpp" +#include "open_engine/renderer/vertex_array.hpp" +#include "open_engine/renderer/camera.hpp" +#include "open_engine/renderer/buffer.hpp" +#include + +namespace OpenEngine { + + // SHOULD BE MOVED ================================== + struct MeshVertex + { + glm::vec3 position; + glm::vec4 color; + glm::vec3 normal; + glm::vec2 text_coords; + uint32_t id = -1; + }; + + struct Mesh + { + Ref vertex_array; + Ref vertex_buffer; + Ref index_buffer; + + // TODO: Make them a ptr or something + std::vector vertices; + std::vector indices; + }; + + Ref CreateMesh(const std::vector& vertices, + const std::vector& indices); + Ref CreateCube(uint32_t id = -1); + // ================================================== + + class Renderer3D + { + public: + static void Init(); + static void Shutdown(); + + static void BeginScene(const Camera& camera, const glm::mat4& transform); + static void BeginScene(const EditorCamera& camera); + static void EndScene(); + + static void DrawMesh(const Ref& mesh, const glm::mat4& transform); + + /* + static void DrawCube(const glm::mat4& transform, const glm::vec4& color, + int entity_id); + static void DrawCube(const glm::mat4& transform, Ref, + int entity_id); + static void DrawCube(const glm::mat4& transform, Ref, + const glm::vec4& color, int entity_id); + */ + }; +} + +#endif // RENDERER3D_HPP diff --git a/open_engine/include/open_engine/renderer/uniform_buffer.cpp b/open_engine/include/open_engine/renderer/uniform_buffer.cpp deleted file mode 100644 index 16492fc..0000000 --- a/open_engine/include/open_engine/renderer/uniform_buffer.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include - -#include -#include -#include - -namespace OpenEngine { - - Ref UniformBuffer::Create(uint32_t size, uint32_t binding) - { - switch (Renderer::GetAPI()) - { - case RendererAPI::API::None: OE_CORE_ASSERT(false, "RendererAPI::None is currently not supported!"); return nullptr; - case RendererAPI::API::OpenGL: return CreateRef(size, binding); - } - - OE_CORE_ASSERT(false, "Unknown RendererAPI!"); - return nullptr; - } - -} diff --git a/open_engine/include/open_engine/renderer/vertex_array.hpp b/open_engine/include/open_engine/renderer/vertex_array.hpp index 19d9ea4..a2843b7 100644 --- a/open_engine/include/open_engine/renderer/vertex_array.hpp +++ b/open_engine/include/open_engine/renderer/vertex_array.hpp @@ -2,6 +2,7 @@ #define VERTEX_ARRAY_HPP #include "open_engine/renderer/buffer.hpp" +#include "open_engine/ref_scope.hpp" #include diff --git a/open_engine/include/open_engine/scene/components.hpp b/open_engine/include/open_engine/scene/components.hpp index 8bce589..b1e55f1 100644 --- a/open_engine/include/open_engine/scene/components.hpp +++ b/open_engine/include/open_engine/scene/components.hpp @@ -2,8 +2,10 @@ #define COMPONENTS_HPP #include "open_engine/scene/native_scriptable_entity.hpp" +#include "open_engine/renderer/renderer3d.hpp" #include "open_engine/scene/scene_camera.hpp" #include "open_engine/renderer/texture.hpp" +#include "open_engine/renderer/renderer3d.hpp" #include #include @@ -87,6 +89,16 @@ namespace OpenEngine { } }; + struct MeshComponent + { + Ref mesh; + + MeshComponent() = default; + MeshComponent(const MeshComponent&) = default; + MeshComponent(const Ref& mesh) + : mesh(mesh) {} + }; + } #endif // COMPONENTS_HPP diff --git a/open_engine/include/open_engine/scene/entity.hpp b/open_engine/include/open_engine/scene/entity.hpp index 55c8de9..bfe1e30 100644 --- a/open_engine/include/open_engine/scene/entity.hpp +++ b/open_engine/include/open_engine/scene/entity.hpp @@ -19,7 +19,7 @@ namespace OpenEngine { Entity(const Entity& other) = default; template - T& AddComponents(Args&&... args) + T& AddComponent(Args&&... args) { OE_ASSERT(!HasComponent(), "Entity already has component."); diff --git a/open_engine/src/open_engine/application.cpp b/open_engine/src/open_engine/application.cpp index 64b3e05..dabb8bd 100755 --- a/open_engine/src/open_engine/application.cpp +++ b/open_engine/src/open_engine/application.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -28,7 +29,8 @@ namespace OpenEngine { { OE_PROFILE_SCOPE("Initializing Renderers"); Renderer::Init(); - Renderer2D::Init(); + //Renderer2D::Init(); + Renderer3D::Init(); } imgui_layer = std::make_shared(); @@ -39,7 +41,8 @@ namespace OpenEngine { Application::~Application() { - OpenEngine::Renderer2D::Shutdown(); + //OpenEngine::Renderer2D::Shutdown(); + Renderer3D::Shutdown(); } void Application::Run() diff --git a/open_engine/src/open_engine/opengl/opengl_buffer.cpp b/open_engine/src/open_engine/opengl/opengl_buffer.cpp index 8571144..4691153 100644 --- a/open_engine/src/open_engine/opengl/opengl_buffer.cpp +++ b/open_engine/src/open_engine/opengl/opengl_buffer.cpp @@ -52,7 +52,7 @@ namespace OpenEngine { // Index Buffer ===================================================== // ================================================================== - OpenGLIndexBuffer::OpenGLIndexBuffer(uint32_t* indices, uint32_t count) + OpenGLIndexBuffer::OpenGLIndexBuffer(const uint32_t* indices, uint32_t count) : count(count) { OE_PROFILE_FUNCTION(); @@ -68,7 +68,7 @@ namespace OpenEngine { void OpenGLIndexBuffer::Bind() const { - glBindBuffer(GL_ARRAY_BUFFER, id); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id); } void OpenGLIndexBuffer::UnBind() const { diff --git a/open_engine/src/open_engine/opengl/opengl_renderer_api.cpp b/open_engine/src/open_engine/opengl/opengl_renderer_api.cpp index 2b14df8..d1e4d91 100644 --- a/open_engine/src/open_engine/opengl/opengl_renderer_api.cpp +++ b/open_engine/src/open_engine/opengl/opengl_renderer_api.cpp @@ -45,6 +45,7 @@ namespace OpenEngine { glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LESS); } void OpenGLRendererAPI::SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height) @@ -66,6 +67,9 @@ namespace OpenEngine { { OE_PROFILE_FUNCTION(); + OE_CORE_ASSERT(vertex_array, "VertexArray is null!"); + OE_CORE_ASSERT(vertex_array->GetIndexBuffer(), "IndexBuffer is null!"); + uint32_t count = index_count? index_count : vertex_array->GetIndexBuffer()->GetCount(); glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, nullptr); glBindTexture(GL_TEXTURE_2D, 0); diff --git a/open_engine/src/open_engine/renderer/buffer.cpp b/open_engine/src/open_engine/renderer/buffer.cpp index 655611c..056940d 100644 --- a/open_engine/src/open_engine/renderer/buffer.cpp +++ b/open_engine/src/open_engine/renderer/buffer.cpp @@ -91,7 +91,7 @@ namespace OpenEngine { return nullptr; } - Ref IndexBuffer::Create(uint32_t* indices, uint32_t count) + Ref IndexBuffer::Create(const uint32_t* indices, uint32_t count) { OE_PROFILE_FUNCTION(); diff --git a/open_engine/src/open_engine/renderer/editor_camera.cpp b/open_engine/src/open_engine/renderer/editor_camera.cpp index 9017b10..de34503 100644 --- a/open_engine/src/open_engine/renderer/editor_camera.cpp +++ b/open_engine/src/open_engine/renderer/editor_camera.cpp @@ -1,4 +1,3 @@ -#include "logging.hpp" #include #include @@ -63,10 +62,11 @@ namespace OpenEngine { void EditorCamera::OnUpdate() { + if (Input::IsKeyPressed(Key::LeftAlt)) { - const glm::vec2& mouse{ Input::GetMouseX(), Input::GetMouseY() }; - glm::vec2 delta = (mouse - initial_mouse_position) * 0.003f; - initial_mouse_position = mouse; + const glm::vec2& mouse{ Input::GetMouseX(), Input::GetMouseY() }; + glm::vec2 delta = (mouse - initial_mouse_position) * 0.003f; + initial_mouse_position = mouse; if (Input::IsMouseButtonPressed(Mouse::ButtonMiddle)) { MousePan(delta); @@ -151,4 +151,10 @@ namespace OpenEngine { return glm::quat(glm::vec3(-pitch, -yaw, 0.0f)); } + void EditorCamera::ResetMousePosition() + { + initial_mouse_position = { Input::GetMouseX(), Input::GetMouseY() }; + mouse_button_was_pressed = false; + } + } diff --git a/open_engine/src/open_engine/renderer/renderer2d.cpp b/open_engine/src/open_engine/renderer/renderer2d.cpp index 536882a..cfcb0e9 100755 --- a/open_engine/src/open_engine/renderer/renderer2d.cpp +++ b/open_engine/src/open_engine/renderer/renderer2d.cpp @@ -1,7 +1,7 @@ -#include "renderer/editor_camera.hpp" -#include "renderer/uniform_buffer.hpp" #include +#include "renderer/editor_camera.hpp" +#include "renderer/uniform_buffer.hpp" #include #include #include @@ -121,6 +121,8 @@ namespace OpenEngine { void Renderer2D::Shutdown() { + OE_PROFILE_FUNCTION(); + renderer_data.white_texture.reset(); renderer_data.vertex_array.reset(); renderer_data.vertex_buffer.reset(); @@ -130,8 +132,6 @@ namespace OpenEngine { for (uint32_t i = 0; i < renderer_data.texture_slot_index; i++) renderer_data.texture_slots[i].reset(); - - OE_PROFILE_FUNCTION(); } void Renderer2D::BeginScene(const OrthographicCamera& camera) diff --git a/open_engine/src/open_engine/renderer/renderer3d.cpp b/open_engine/src/open_engine/renderer/renderer3d.cpp new file mode 100644 index 0000000..5b5e1a4 --- /dev/null +++ b/open_engine/src/open_engine/renderer/renderer3d.cpp @@ -0,0 +1,200 @@ +#include "logging.hpp" +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +namespace OpenEngine { + + // SHOULD BE MOVED ================================== + Ref CreateMesh(const std::vector& vertices, + const std::vector& indices) + { + Ref mesh = CreateRef(); + mesh->vertices = vertices; + mesh->indices = indices; + + mesh->index_buffer = IndexBuffer::Create(indices.data(), indices.size()); + mesh->vertex_array = VertexArray::Create(); + mesh->vertex_buffer = VertexBuffer::Create(vertices.size() * sizeof(MeshVertex)); + + mesh->vertex_buffer->SetData(vertices.data(), vertices.size() * sizeof(MeshVertex)); + + BufferLayout layout = { + { ShaderDataType::Float3, "a_Position" }, + { ShaderDataType::Float4, "a_Color" }, + { ShaderDataType::Float3, "a_Normal" }, + { ShaderDataType::Float2, "a_TexCoord" }, + { ShaderDataType::Int, "a_EntityID"} + }; + + mesh->vertex_buffer->SetLayout(layout); + + mesh->vertex_array->AddVertexBuffer(mesh->vertex_buffer); + mesh->vertex_array->SetIndexBuffer(mesh->index_buffer); + + return mesh; + } + + Ref CreateCube(uint32_t id) + { + OE_CORE_DEBUG("Creating cube with id {}.", id); + std::vector vertices = { + // Back face (normal 0, 0, -1) + {{-0.5f, -0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {0,0,-1}, {0,0}, id}, + {{-0.5f, 0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {0,0,-1}, {0,1}, id}, + {{ 0.5f, 0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {0,0,-1}, {1,1}, id}, + {{ 0.5f, -0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {0,0,-1}, {1,0}, id}, + + // Front face (normal 0, 0, 1) + {{-0.5f, -0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {0,0,1}, {0,0}, id}, + {{-0.5f, 0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {0,0,1}, {0,1}, id}, + {{ 0.5f, 0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {0,0,1}, {1,1}, id}, + {{ 0.5f, -0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {0,0,1}, {1,0}, id}, + + // Left face (normal -1, 0, 0) + {{-0.5f, -0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {-1,0,0}, {0,0}, id}, + {{-0.5f, 0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {-1,0,0}, {0,1}, id}, + {{-0.5f, 0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {-1,0,0}, {1,1}, id}, + {{-0.5f, -0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {-1,0,0}, {1,0}, id}, + + // Right face (normal 1, 0, 0) + {{ 0.5f, -0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {1,0,0}, {0,0}, id}, + {{ 0.5f, 0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {1,0,0}, {0,1}, id}, + {{ 0.5f, 0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {1,0,0}, {1,1}, id}, + {{ 0.5f, -0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {1,0,0}, {1,0}, id}, + + // Top face (normal 0, 1, 0) + {{-0.5f, 0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {0,1,0}, {0,0}, id}, + {{-0.5f, 0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {0,1,0}, {0,1}, id}, + {{ 0.5f, 0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {0,1,0}, {1,1}, id}, + {{ 0.5f, 0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {0,1,0}, {1,0}, id}, + + // Bottom face (normal 0, -1, 0) + {{-0.5f, -0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {0,-1,0}, {0,0}, id}, + {{-0.5f, -0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {0,-1,0}, {0,1}, id}, + {{ 0.5f, -0.5f, 0.5f}, {0.5f,0.5f,0.5f,1}, {0,-1,0}, {1,1}, id}, + {{ 0.5f, -0.5f, -0.5f}, {0.5f,0.5f,0.5f,1}, {0,-1,0}, {1,0}, id}, + }; + + std::vector indices = { + // Back face (z=-0.5) + 0, 1, 2, 0, 2, 3, + // Front face (z=0.5) + 4, 6, 5, 4, 7, 6, + // Right face + 12, 13, 14, 12, 14, 15, + // Left face + 8, 10, 9, 8, 11, 10, + // Top face + 18, 16, 17, 18, 19, 16, + // Bottom face + 20, 22, 21, 20, 23, 22 + // TODO: Missing backface + }; + + return CreateMesh(vertices, indices); + } + + // ================================================== + struct Renderer3DData + { + Ref color_shader_3d; + + struct CameraData + { + glm::mat4 view_projection; + glm::vec3 view_position; + }; + CameraData camera_buffer; + Ref camera_uniform_buffer; + + struct TransformData + { + glm::mat4 transform; + }; + TransformData transform_buffer; + Ref transform_uniform_buffer; + }; + + static Renderer3DData renderer_data; + + void Renderer3D::Init() + { + OE_PROFILE_FUNCTION(); + + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + + renderer_data.color_shader_3d = Shader::Create("assets/shaders/color3d.glsl"); + renderer_data.camera_uniform_buffer = UniformBuffer::Create(sizeof(Renderer3DData::CameraData), 0); + renderer_data.transform_uniform_buffer = UniformBuffer::Create(sizeof(Renderer3DData::TransformData), 1); + + glEnable(GL_DEBUG_OUTPUT); + glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); + glDebugMessageCallback([](GLenum source, GLenum type, GLuint id, GLenum severity, + GLsizei length, const GLchar* message, const void* userParam) { + OE_CORE_ERROR("GL: {0}", message); + }, nullptr); + } + + void Renderer3D::Shutdown() + { + renderer_data.camera_uniform_buffer.reset(); + renderer_data.color_shader_3d.reset(); + } + + void Renderer3D::BeginScene(const Camera& camera, const glm::mat4& transform) + { + OE_PROFILE_FUNCTION(); + + renderer_data.camera_buffer.view_projection = camera.GetProjection() * glm::inverse(transform); + renderer_data.camera_buffer.view_position = transform[3]; + + renderer_data.camera_uniform_buffer->SetData(&renderer_data.camera_buffer, sizeof(Renderer3DData::CameraData)); + } + + void Renderer3D::BeginScene(const EditorCamera& camera) + { + OE_PROFILE_FUNCTION(); + + renderer_data.camera_buffer.view_projection = camera.GetViewProjection(); + renderer_data.camera_buffer.view_position = camera.GetPosition(); + + renderer_data.camera_uniform_buffer->SetData(&renderer_data.camera_buffer, sizeof(Renderer3DData::CameraData)); + } + + void Renderer3D::EndScene() + { + + } + + void Renderer3D::DrawMesh(const Ref& mesh, const glm::mat4& transform) + { + OE_PROFILE_FUNCTION(); + + renderer_data.color_shader_3d->Bind(); + + renderer_data.transform_buffer.transform = transform; + renderer_data.transform_uniform_buffer->SetData(&renderer_data.transform_buffer, sizeof(Renderer3DData::TransformData)); + + mesh->vertex_array->Bind(); + + RenderCommand::DrawIndexed(mesh->vertex_array, mesh->indices.size()); + } +} diff --git a/open_engine/src/open_engine/scene/scene.cpp b/open_engine/src/open_engine/scene/scene.cpp index c9cbb6e..3300279 100644 --- a/open_engine/src/open_engine/scene/scene.cpp +++ b/open_engine/src/open_engine/scene/scene.cpp @@ -1,16 +1,18 @@ +#include #include -#include +#include #include #include #include namespace OpenEngine { + Entity Scene::CreateEntity(const std::string& name) { Entity entity = { registry.create(), this }; - auto& tag = entity.AddComponents(); + auto& tag = entity.AddComponent(); tag.tag = name.empty() ? "Entity" : name; return entity; @@ -52,6 +54,7 @@ namespace OpenEngine { } } + /* if (main_camera) { Renderer2D::BeginScene(main_camera->GetProjection(), main_transform); @@ -70,27 +73,23 @@ namespace OpenEngine { Renderer2D::EndScene(); } + */ } } void Scene::OnUpdateEditor(EditorCamera& camera) { - Renderer2D::BeginScene(camera); + Renderer3D::BeginScene(camera); - auto group = registry.group(entt::get); - for (auto entity : group) - { - auto [transform, sprite] = group.get(entity); + auto view = registry.view(); - if (sprite.texture) - Renderer2D::DrawQuad(GetTransformFromComp(transform), - sprite.texture, sprite.color, (int)entity, sprite.tiling_factor); - else - Renderer2D::DrawQuad(GetTransformFromComp(transform), - sprite.color, (int)entity); - } + for (const auto& entity : view) { + auto [transform, mesh] = view.get(entity); - Renderer2D::EndScene(); + Renderer3D::DrawMesh(mesh.mesh, GetTransformFromComp(transform)); + } + + Renderer3D::EndScene(); } void Scene::OnViewportResize(uint32_t width, uint32_t height) @@ -151,4 +150,9 @@ namespace OpenEngine { void Scene::OnComponentAdded(Entity entity, NativeScriptComponent& component) { } + + template<> + void Scene::OnComponentAdded(Entity entity, MeshComponent& component) + { + } } diff --git a/open_engine/src/open_engine/scene/scene_serializer.cpp b/open_engine/src/open_engine/scene/scene_serializer.cpp index 2fd4490..270273e 100644 --- a/open_engine/src/open_engine/scene/scene_serializer.cpp +++ b/open_engine/src/open_engine/scene/scene_serializer.cpp @@ -209,7 +209,7 @@ namespace OpenEngine { auto transformComponent = entity["TransformComponent"]; if (transformComponent) { - auto& tc = deserializedEntity.AddComponents(); + auto& tc = deserializedEntity.AddComponent(); tc.translation = transformComponent["Translation"].as(); tc.rotation = transformComponent["Rotation"].as(); tc.scale = transformComponent["Scale"].as(); @@ -218,7 +218,7 @@ namespace OpenEngine { auto cameraComponent = entity["CameraComponent"]; if (cameraComponent) { - auto& cc = deserializedEntity.AddComponents(); + auto& cc = deserializedEntity.AddComponent(); auto camera_props = cameraComponent["Camera"]; cc.camera.SetProjectionType((SceneCamera::ProjectionType)camera_props["ProjectionType"].as()); @@ -238,7 +238,7 @@ namespace OpenEngine { auto spriteRendererComponent = entity["SpriteRendererComponent"]; if (spriteRendererComponent) { - auto& src = deserializedEntity.AddComponents(); + auto& src = deserializedEntity.AddComponent(); src.color = spriteRendererComponent["Color"].as(); } }