ThreeLab/Editor/editor.cpp

133 lines
4.9 KiB
C++
Raw Normal View History

2025-04-01 01:56:49 +00:00
#include "../Engine/Engine.h"
2025-04-01 16:45:59 +00:00
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
2025-04-01 16:02:52 +00:00
2025-04-01 16:45:59 +00:00
// Simple editor camera.
struct EditorCamera {
glm::vec3 position = glm::vec3(0.0f, 0.0f, 5.0f);
float yaw = -90.0f;
float pitch = 0.0f;
float speed = 5.0f;
float sensitivity = 0.1f;
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
};
2025-04-01 01:25:39 +00:00
2025-04-01 16:45:59 +00:00
EditorCamera editorCamera;
void ProcessEditorCamera(GLFWwindow* window, float deltaTime) {
glm::vec3 front;
front.x = cos(glm::radians(editorCamera.yaw)) * cos(glm::radians(editorCamera.pitch));
front.y = sin(glm::radians(editorCamera.pitch));
front.z = sin(glm::radians(editorCamera.yaw)) * cos(glm::radians(editorCamera.pitch));
front = glm::normalize(front);
glm::vec3 right = glm::normalize(glm::cross(front, glm::vec3(0,1,0)));
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
editorCamera.position += front * editorCamera.speed * deltaTime;
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
editorCamera.position -= front * editorCamera.speed * deltaTime;
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
editorCamera.position -= right * editorCamera.speed * deltaTime;
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
editorCamera.position += right * editorCamera.speed * deltaTime;
static double lastX = 0, lastY = 0;
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS) {
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
if (lastX == 0 && lastY == 0) { lastX = xpos; lastY = ypos; }
float offsetX = (float)(xpos - lastX);
float offsetY = (float)(lastY - ypos);
lastX = xpos;
lastY = ypos;
editorCamera.yaw += offsetX * editorCamera.sensitivity;
editorCamera.pitch += offsetY * editorCamera.sensitivity;
if (editorCamera.pitch > 89.0f) editorCamera.pitch = 89.0f;
if (editorCamera.pitch < -89.0f) editorCamera.pitch = -89.0f;
} else {
lastX = lastY = 0;
}
editorCamera.view = glm::lookAt(editorCamera.position, editorCamera.position + front, glm::vec3(0,1,0));
}
int main() {
if (!Engine::Init())
2025-04-01 01:25:39 +00:00
return 1;
2025-04-01 16:45:59 +00:00
// Setup ImGui.
2025-04-01 01:25:39 +00:00
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
2025-04-01 16:45:59 +00:00
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
2025-04-01 01:25:39 +00:00
ImGui::StyleColorsDark();
2025-04-01 16:45:59 +00:00
const char* glsl_version = "#version 330";
ImGui_ImplGlfw_InitForOpenGL(Engine::GetWindow(), true);
ImGui_ImplOpenGL3_Init(glsl_version);
2025-04-01 01:25:39 +00:00
2025-04-01 16:45:59 +00:00
float lastFrameTime = (float)glfwGetTime();
2025-04-01 01:25:39 +00:00
2025-04-01 16:45:59 +00:00
while (!glfwWindowShouldClose(Engine::GetWindow())) {
float currentFrameTime = (float)glfwGetTime();
float deltaTime = currentFrameTime - lastFrameTime;
lastFrameTime = currentFrameTime;
ProcessEditorCamera(Engine::GetWindow(), deltaTime);
// Update editor camera projection based on the window size.
int winWidth, winHeight;
glfwGetFramebufferSize(Engine::GetWindow(), &winWidth, &winHeight);
editorCamera.projection = glm::perspective(glm::radians(45.0f), (float)winWidth / winHeight, 0.1f, 100.0f);
// Offscreen rendering: Resize framebuffer and render scene.
Engine::ResizeFramebuffer(winWidth, winHeight);
ImTextureID offscreenTexture = Engine::RenderScene(editorCamera.view, editorCamera.projection, editorCamera.position);
// Clear the default framebuffer background.
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, winWidth, winHeight);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Clear with black (or choose any color).
glClear(GL_COLOR_BUFFER_BIT);
// Start a single ImGui frame.
2025-04-01 01:25:39 +00:00
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
2025-04-01 16:45:59 +00:00
// Create a full-viewport dock space.
ImGui::DockSpaceOverViewport(0, ImGui::GetMainViewport(), ImGuiDockNodeFlags_PassthruCentralNode);
2025-04-01 01:25:39 +00:00
2025-04-01 16:45:59 +00:00
// Create an "Editor Panel" window.
2025-04-01 01:25:39 +00:00
ImGui::Begin("Editor Panel");
ImGui::Text("Welcome to the Editor!");
2025-04-01 16:45:59 +00:00
// (Additional UI elements can go here.)
ImGui::End();
// Create a "Rendered Output" window.
ImGui::Begin("Rendered Output");
// Get available region size for the rendered output.
ImVec2 viewportSize = ImGui::GetContentRegionAvail();
// Display the offscreen texture. The UVs are flipped to correct the upside-down image.
ImGui::Image(offscreenTexture, viewportSize, ImVec2(0,1), ImVec2(1,0));
2025-04-01 01:25:39 +00:00
ImGui::End();
2025-04-01 16:45:59 +00:00
// Finalize and render the ImGui frame.
2025-04-01 01:25:39 +00:00
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
2025-04-01 16:45:59 +00:00
glfwSwapBuffers(Engine::GetWindow());
glfwPollEvents();
2025-04-01 01:25:39 +00:00
}
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
2025-04-01 16:45:59 +00:00
Engine::Shutdown();
2025-04-01 01:25:39 +00:00
return 0;
}