#include "Renderer.h" #include "Engine.h" RendererData Renderer::s_Data; float Renderer::deltaTime = 0.0f; float Renderer::lastFrame = 0.0f; bool Renderer::firstMouse = true; float Renderer::lastX; float Renderer::lastY; float vertices[] = { -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, -0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 1.0f, 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, }; unsigned int indices[] = { 0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4, 0, 1, 5, 5, 4, 0, 2, 3, 7, 7, 6, 2, 0, 3, 7, 7, 4, 0, 1, 2, 6, 6, 5, 1 }; Renderer::Renderer() = default; Renderer::~Renderer() { Shutdown(); } void Renderer::Init() { if (!gladLoadGLLoader(reinterpret_cast(glfwGetProcAddress))) { std::cerr << "Failed to initialize GLAD" << std::endl; return; } UI::Init(Engine::Get().GetWindow().GetWindow()); s_Data.m_Camera = new Camera(glm::vec3(0.0f, 0.0f, 3.0f)); LoadShaders(); SetupBuffers(); SetCallbacks(); } RendererData Renderer::GetData() { return s_Data; } void Renderer::LoadShaders() { s_Data.m_Shader = new Shader(RESOURCES_PATH"shaders/vertex.glsl", RESOURCES_PATH"shaders/fragment.glsl"); } void Renderer::SetupBuffers() { s_Data.m_VAO = new VertexArray(); s_Data.m_VBO = new VertexBuffer(); s_Data.m_IBO = new IndexBuffer(); s_Data.m_VAO->Bind(); s_Data.m_VBO->SetData(sizeof(vertices), vertices); s_Data.m_IBO->SetData(sizeof(indices), indices); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), nullptr); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), reinterpret_cast(3 * sizeof(float))); glEnableVertexAttribArray(1); VertexArray::Unbind(); VertexBuffer::Unbind(); IndexBuffer::Unbind(); s_Data.m_FBO = new FrameBuffer(); WindowSize windowSize = Engine::Get().GetWindow().GetSize(); s_Data.m_FBO->AttachTexture(windowSize.Width, windowSize.Height); FrameBuffer::Unbind(); } void Renderer::SetCallbacks() { glfwSetWindowSizeCallback(Engine::Get().GetWindow().GetWindow(), [](GLFWwindow* window, int width, int height) { SetupBuffers(); }); glfwSetFramebufferSizeCallback(Engine::Get().GetWindow().GetWindow(), [](GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); }); glfwSetScrollCallback(Engine::Get().GetWindow().GetWindow(), [](GLFWwindow* window, double xOffset, double yOffset) { s_Data.m_Camera->ProcessMouseScroll(static_cast(yOffset)); }); } void Renderer::Render() { auto currentFrame = static_cast(glfwGetTime()); deltaTime = currentFrame - lastFrame; lastFrame = currentFrame; s_Data.m_FBO->Bind(); glfwPollEvents(); ProcessInput(Engine::Get().GetWindow().GetWindow()); glEnable(GL_DEPTH_TEST); glm::vec3 color = UI::GetData().m_BgColor; glClearColor(color.x, color.y, color.z, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); UI::Run(); s_Data.m_Shader->Use(); auto model = glm::mat4(1.0f); model = translate(model, UI::GetData().m_Position); if(length(UI::GetData().m_Rotation) != 0) model = rotate(model, glm::radians(length(UI::GetData().m_Rotation)), normalize(UI::GetData().m_Rotation)); model = scale(model, UI::GetData().m_Scale); glm::mat4 view = s_Data.m_Camera->GetViewMatrix(); WindowSize size = Engine::Get().GetWindow().GetSize(); glm::mat4 projection = glm::perspective(glm::radians(45.0f), static_cast(size.Width) / static_cast(size.Height), 0.1f, 100.0f); glUniformMatrix4fv(s_Data.m_Shader->GetUniformLocation("model"), 1, GL_FALSE, glm::value_ptr(model)); glUniformMatrix4fv(s_Data.m_Shader->GetUniformLocation("view"), 1, GL_FALSE, glm::value_ptr(view)); glUniformMatrix4fv(s_Data.m_Shader->GetUniformLocation("projection"), 1, GL_FALSE, glm::value_ptr(projection)); glUniform3fv(s_Data.m_Shader->GetUniformLocation("color"), 1, glm::value_ptr(UI::GetData().m_ShaderColor)); s_Data.m_VAO->Bind(); glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr); glBindFramebuffer(GL_FRAMEBUFFER, 0); UI::Render(*s_Data.m_FBO); glfwSwapBuffers(Engine::Get().GetWindow().GetWindow()); } void Renderer::ProcessInput(GLFWwindow *window) { if(Input::IsMouseButtonPressed(ButtonRight)) { auto mousePos = Input::GetMousePosition(); if(firstMouse) { lastX = mousePos.x; lastY = mousePos.y; firstMouse = false; } float xOffset = mousePos.x - lastX; float yOffset = lastY - mousePos.y; lastX = mousePos.x; lastY = mousePos.y; s_Data.m_Camera->ProcessMouseMovement(xOffset, yOffset); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); } else { glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); } if (Input::IsKeyPressed(W)) s_Data.m_Camera->ProcessKeyboard(FORWARD, deltaTime); if (Input::IsKeyPressed(S)) s_Data.m_Camera->ProcessKeyboard(BACKWARD, deltaTime); if (Input::IsKeyPressed(A)) s_Data.m_Camera->ProcessKeyboard(LEFT, deltaTime); if (Input::IsKeyPressed(D)) s_Data.m_Camera->ProcessKeyboard(RIGHT, deltaTime); if (Input::IsKeyPressed(Q)) s_Data.m_Camera->ProcessKeyboard(DOWN, deltaTime); if (Input::IsKeyPressed(E)) s_Data.m_Camera->ProcessKeyboard(UP, deltaTime); } void Renderer::Shutdown() { s_Data.m_VAO->Shutdown(); s_Data.m_VBO->Shutdown(); s_Data.m_FBO->Shutdown(); s_Data.m_IBO->Shutdown(); s_Data.m_Shader->Shutdown(); }