adding editor
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
#ifndef EDITOR_HPP
|
||||
#define EDITOR_HPP
|
||||
|
||||
#include "open_engine/renderer/renderer2d.hpp"
|
||||
#include "open_engine/input/input_system.hpp"
|
||||
#include <view_layer.hpp>
|
||||
#include <open_engine.hpp>
|
||||
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <glm/matrix.hpp>
|
||||
#include <glm/fwd.hpp>
|
||||
#include <imgui.h>
|
||||
#include <glad/glad.h>
|
||||
@@ -14,9 +16,9 @@
|
||||
class EditorLayer : public OpenEngine::Layer
|
||||
{
|
||||
public:
|
||||
EditorLayer()
|
||||
EditorLayer(OpenEngine::Ref<ViewLayer>& view_layer)
|
||||
: OpenEngine::Layer("editor_layer"),
|
||||
camera(1280.0f / 720.0f, 1.0f)
|
||||
view(view_layer)
|
||||
{
|
||||
}
|
||||
~EditorLayer() {};
|
||||
@@ -24,16 +26,6 @@ class EditorLayer : public OpenEngine::Layer
|
||||
void OnAttach() override
|
||||
{
|
||||
OE_PROFILE_FUNCTION();
|
||||
{
|
||||
OE_PROFILE_SCOPE("Texture2D Creation");
|
||||
face = OpenEngine::Texture2D::Create("assets/textures/awesomeface.png");
|
||||
}
|
||||
|
||||
OpenEngine::FramebufferSpecification specs;
|
||||
specs.width = 1280;
|
||||
specs.height = 720;
|
||||
|
||||
framebuffer = OpenEngine::FrameBuffer::Create(specs);
|
||||
}
|
||||
|
||||
void OnDetach() override
|
||||
@@ -44,41 +36,13 @@ class EditorLayer : public OpenEngine::Layer
|
||||
{
|
||||
|
||||
OpenEngine::Renderer2D::ResetStats();
|
||||
OE_PROFILE_FUNCTION()
|
||||
{
|
||||
camera.OnUpdate();
|
||||
framebuffer->Bind();
|
||||
OE_PROFILE_FUNCTION();
|
||||
|
||||
OE_PROFILE_SCOPE("Setting up Rendering");
|
||||
|
||||
OpenEngine::RenderCommand::SetClearColor({0.11f, 0.11f, 0.15f, 1.0f});
|
||||
OpenEngine::RenderCommand::Clear();
|
||||
OpenEngine::Renderer2D::BeginScene(camera.GetCamera());
|
||||
}
|
||||
{
|
||||
OE_PROFILE_SCOPE("Drawing Quads");
|
||||
OpenEngine::Transform tr1 = {glm::vec3(0.0f, 0.0f, -0.1f), glm::vec3(1.0f, 1.0f, 0.0f), angle};
|
||||
OpenEngine::Transform tr3 = {glm::vec3(-0.5f, -0.5f, 0.0f), glm::vec3(1.0f, 0.5f, 0.0f), angle * 0.5f};
|
||||
OpenEngine::Renderer2D::DrawQuad(tr1, face);
|
||||
OpenEngine::Renderer2D::DrawQuad(tr3, rectangle_color);
|
||||
view->GetCamera().OnUpdate();
|
||||
}
|
||||
|
||||
OpenEngine::Renderer2D::EndScene();
|
||||
|
||||
OpenEngine::Renderer2D::BeginScene(camera.GetCamera());
|
||||
|
||||
for (float y = -5.0f; y < 5.0f; y += 0.5f) {
|
||||
for (float x = -5.0f; x < 5.0f; x += 0.5f) {
|
||||
|
||||
glm::vec4 gradient_color = {(x + 5.0f) / 10.0f, 0.3f, (y + 5.0f) / 10.0f, 1.0f};
|
||||
OpenEngine::Renderer2D::DrawQuad({{x, y, 0.0f}, glm::vec3(0.45f, 0.45f, 0.0f)}, gradient_color);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
OpenEngine::Renderer2D::EndScene();
|
||||
|
||||
framebuffer->Unbind();
|
||||
}
|
||||
|
||||
void OnEvent(OpenEngine::Event& event) override
|
||||
@@ -87,20 +51,65 @@ class EditorLayer : public OpenEngine::Layer
|
||||
|
||||
{
|
||||
OE_PROFILE_SCOPE("Camera OnEvent");
|
||||
camera.OnEvent(event);
|
||||
view->GetCamera().OnEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
float remap(float value, float minInput, float maxInput, float minOutput, float maxOutput) {
|
||||
// 1. Normalize the input to a 0.0 - 1.0 range
|
||||
float t = (value - minInput) / (maxInput - minInput);
|
||||
|
||||
// 2. Use glm::mix to interpolate between the output range
|
||||
return glm::mix(minOutput, maxOutput, t);
|
||||
}
|
||||
|
||||
glm::vec3 ScreenToWorld(
|
||||
float screenX,
|
||||
float screenY,
|
||||
float screenZ, // depth value (0.0 = near plane, 1.0 = far plane)
|
||||
const glm::mat4& view,
|
||||
const glm::mat4& projection,
|
||||
int viewportWidth,
|
||||
int viewportHeight)
|
||||
{
|
||||
// 1. Convert screen coordinates to normalized device coordinates (NDC)
|
||||
// Screen space: origin at top-left, Y points down
|
||||
// NDC space: origin at center, range [-1, 1] for x and y
|
||||
|
||||
float x = (2.0f * screenX) / viewportWidth - 1.0f;
|
||||
float y = 1.0f - (2.0f * screenY) / viewportHeight; // Flip Y
|
||||
float z = 2.0f * screenZ - 1.0f; // [0,1] -> [-1,1]
|
||||
|
||||
glm::vec4 clipCoords(x, y, z, 1.0f);
|
||||
|
||||
// 2. Transform from clip space to view space
|
||||
glm::mat4 invProjection = glm::inverse(projection);
|
||||
glm::vec4 viewCoords = invProjection * clipCoords;
|
||||
|
||||
// 3. Transform from view space to world space
|
||||
glm::mat4 invView = glm::inverse(view);
|
||||
glm::vec4 worldCoords = invView * viewCoords;
|
||||
|
||||
// 4. Perspective divide
|
||||
if (worldCoords.w != 0.0f) {
|
||||
worldCoords /= worldCoords.w;
|
||||
}
|
||||
|
||||
return glm::vec3(worldCoords);
|
||||
}
|
||||
|
||||
void OnImGuiRender() override
|
||||
{
|
||||
OE_PROFILE_FUNCTION();
|
||||
|
||||
OpenEngine::Ref<OpenEngine::FrameBuffer> framebuffer = view->GetFramebuffer();
|
||||
|
||||
ImGui::DockSpaceOverViewport();
|
||||
|
||||
ImGui::Begin("Square settings");
|
||||
|
||||
ImGui::SliderFloat("Angle", &angle, -360, 360);
|
||||
ImGui::ColorPicker4("Rectangle color", &rectangle_color[0]);
|
||||
//ImGui::SliderFloat("Angle", &angle, -360, 360);
|
||||
//ImGui::ColorPicker4("Rectangle color", &rectangle_color[0]);
|
||||
|
||||
for (auto& result : profiling_results)
|
||||
{
|
||||
@@ -120,10 +129,51 @@ class EditorLayer : public OpenEngine::Layer
|
||||
framebuffer->Resize(viewport_panel_size.x, viewport_panel_size.y);
|
||||
viewport_size = { viewport_panel_size.x, viewport_panel_size.y };
|
||||
|
||||
camera.OnResize(viewport_panel_size.x, viewport_panel_size.y);
|
||||
view->GetCamera().OnResize(viewport_panel_size.x, viewport_panel_size.y);
|
||||
}
|
||||
uint32_t texture_id = framebuffer->GetColorAttachmentRendererID();
|
||||
ImGui::Image((void*)texture_id, ImVec2{ viewport_size.x, viewport_size.y }, ImVec2{ 0, 1 }, ImVec2{ 1, 0 });
|
||||
ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_None;
|
||||
if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags))
|
||||
{
|
||||
if (ImGui::BeginTabItem("Avocado"))
|
||||
{
|
||||
ImVec2 imgui_cursor_position = ImGui::GetCursorScreenPos();
|
||||
|
||||
auto pair_position = OpenEngine::Input::GetMousePosition();
|
||||
glm::vec2 mouse_position = { pair_position.first, pair_position.second };
|
||||
|
||||
int max_x = viewport_size.x + imgui_cursor_position.x;
|
||||
int max_y = viewport_size.y + imgui_cursor_position.y;
|
||||
|
||||
if ((mouse_position.x <= max_x && mouse_position.x >= imgui_cursor_position.x)
|
||||
&& (mouse_position.y <= max_y && mouse_position.y >= imgui_cursor_position.y)) {
|
||||
|
||||
float x = mouse_position.x - imgui_cursor_position.x;
|
||||
float y = mouse_position.y - imgui_cursor_position.y;
|
||||
|
||||
auto& camera = view->GetCamera().GetCamera();
|
||||
auto world_coords = ScreenToWorld(x, y, 0.0f,
|
||||
camera.GetViewMatrix(),
|
||||
camera.GetProjectionMatrix(),
|
||||
viewport_size.x,
|
||||
viewport_size.y);
|
||||
view->SetCursorPos({ world_coords.x, world_coords.y });
|
||||
}
|
||||
ImGui::Image((void*)texture_id, ImVec2{ viewport_size.x, viewport_size.y }, ImVec2{ 0, 1 }, ImVec2{ 1, 0 });
|
||||
ImGui::EndTabItem();
|
||||
}
|
||||
if (ImGui::BeginTabItem("Broccoli"))
|
||||
{
|
||||
ImGui::Text("This is the Broccoli tab!\nblah blah blah blah blah");
|
||||
ImGui::EndTabItem();
|
||||
}
|
||||
if (ImGui::BeginTabItem("Cucumber"))
|
||||
{
|
||||
ImGui::Text("This is the Cucumber tab!\nblah blah blah blah blah");
|
||||
ImGui::EndTabItem();
|
||||
}
|
||||
ImGui::EndTabBar();
|
||||
}
|
||||
ImGui::End();
|
||||
ImGui::PopStyleVar();
|
||||
|
||||
@@ -143,20 +193,16 @@ class EditorLayer : public OpenEngine::Layer
|
||||
ImGui::Text("\t\tQuad count: %d", stats.quad_count);
|
||||
ImGui::Text("\t\tVertices count: %d", stats.GetToralVertexCount());
|
||||
ImGui::Text("\t\tIndices count: %d", stats.GetToralIndexCount());
|
||||
ImGui::ShowDemoWindow();
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
private:
|
||||
float angle = 0.0f;
|
||||
OpenEngine::Ref<OpenEngine::Texture2D> face;
|
||||
|
||||
OpenEngine::OrthographicCameraController camera;
|
||||
|
||||
std::vector<OpenEngine::Time::ProfilingResult> profiling_results;
|
||||
|
||||
glm::vec2 viewport_size = { 0.0f, 0.0f };
|
||||
OpenEngine::Ref<OpenEngine::FrameBuffer> framebuffer;
|
||||
glm::vec4 rectangle_color = {0.8f, 0.2f, 0.7f, 1.0f};
|
||||
|
||||
OpenEngine::Ref<ViewLayer> view;
|
||||
};
|
||||
|
||||
class EditorApp : public OpenEngine::Application
|
||||
|
||||
Reference in New Issue
Block a user