2024-08-01 23:32:51 +00:00
|
|
|
#include <glad/glad.h>
|
|
|
|
#include "Renderer.h"
|
|
|
|
#include "Engine.h"
|
|
|
|
|
|
|
|
RendererData Renderer::s_Data;
|
|
|
|
|
2024-08-27 20:24:08 +00:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2024-08-01 23:32:51 +00:00
|
|
|
Renderer::Renderer() = default;
|
|
|
|
|
|
|
|
Renderer::~Renderer()
|
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::Init()
|
|
|
|
{
|
2024-08-27 20:24:08 +00:00
|
|
|
if (!gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress))) {
|
2024-08-01 23:32:51 +00:00
|
|
|
std::cerr << "Failed to initialize GLAD" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-27 20:24:08 +00:00
|
|
|
UI::Init(Engine::Get().GetWindow().GetWindow());
|
|
|
|
s_Data.m_Camera = new Camera(glm::vec3(0.0f, 0.0f, 3.0f));
|
|
|
|
|
2024-08-01 23:32:51 +00:00
|
|
|
LoadShaders();
|
|
|
|
SetupBuffers();
|
2024-08-27 20:24:08 +00:00
|
|
|
SetCallbacks();
|
2024-08-01 23:32:51 +00:00
|
|
|
}
|
|
|
|
|
2024-08-22 15:55:30 +00:00
|
|
|
RendererData Renderer::GetData()
|
|
|
|
{
|
|
|
|
return s_Data;
|
|
|
|
}
|
|
|
|
|
2024-08-01 23:32:51 +00:00
|
|
|
void Renderer::LoadShaders()
|
|
|
|
{
|
2024-08-22 15:55:30 +00:00
|
|
|
s_Data.m_Shader = new Shader(RESOURCES_PATH"shaders/vertex.glsl", RESOURCES_PATH"shaders/fragment.glsl");
|
2024-08-01 23:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::SetupBuffers()
|
|
|
|
{
|
|
|
|
s_Data.m_VAO = new VertexArray();
|
|
|
|
s_Data.m_VBO = new VertexBuffer();
|
2024-08-27 20:24:08 +00:00
|
|
|
s_Data.m_IBO = new IndexBuffer();
|
2024-08-01 23:32:51 +00:00
|
|
|
|
|
|
|
s_Data.m_VAO->Bind();
|
|
|
|
|
|
|
|
s_Data.m_VBO->SetData(sizeof(vertices), vertices);
|
2024-08-27 20:24:08 +00:00
|
|
|
s_Data.m_IBO->SetData(sizeof(indices), indices);
|
2024-08-01 23:32:51 +00:00
|
|
|
|
2024-08-27 20:24:08 +00:00
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), nullptr);
|
2024-08-01 23:32:51 +00:00
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
|
2024-08-27 20:24:08 +00:00
|
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), reinterpret_cast<void*>(3 * sizeof(float)));
|
2024-08-01 23:32:51 +00:00
|
|
|
glEnableVertexAttribArray(1);
|
|
|
|
|
|
|
|
VertexArray::Unbind();
|
|
|
|
VertexBuffer::Unbind();
|
2024-08-27 20:24:08 +00:00
|
|
|
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<float>(yOffset));
|
|
|
|
});
|
2024-08-01 23:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::Render() {
|
2024-08-27 20:24:08 +00:00
|
|
|
auto currentFrame = static_cast<float>(glfwGetTime());
|
|
|
|
deltaTime = currentFrame - lastFrame;
|
|
|
|
lastFrame = currentFrame;
|
|
|
|
|
2024-08-24 16:50:58 +00:00
|
|
|
s_Data.m_FBO->Bind();
|
|
|
|
|
2024-08-24 07:42:06 +00:00
|
|
|
glfwPollEvents();
|
2024-08-27 20:24:08 +00:00
|
|
|
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);
|
2024-08-24 07:42:06 +00:00
|
|
|
|
|
|
|
UI::Run();
|
|
|
|
|
2024-08-01 23:32:51 +00:00
|
|
|
s_Data.m_Shader->Use();
|
2024-08-27 20:24:08 +00:00
|
|
|
|
|
|
|
auto model = glm::mat4(1.0f);
|
|
|
|
model = glm::translate(model, UI::GetData().m_Position);
|
|
|
|
if(glm::length(UI::GetData().m_Rotation) != 0)
|
|
|
|
model = glm::rotate(model, glm::radians(length(UI::GetData().m_Rotation)), normalize(UI::GetData().m_Rotation));
|
|
|
|
model = glm::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<float>(size.Width) / static_cast<float>(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));
|
|
|
|
|
2024-08-01 23:32:51 +00:00
|
|
|
s_Data.m_VAO->Bind();
|
2024-08-27 20:24:08 +00:00
|
|
|
|
|
|
|
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
2024-08-01 23:32:51 +00:00
|
|
|
|
2024-08-24 07:42:06 +00:00
|
|
|
UI::Render(*s_Data.m_FBO);
|
|
|
|
|
2024-08-22 15:55:30 +00:00
|
|
|
glfwSwapBuffers(Engine::Get().GetWindow().GetWindow());
|
|
|
|
}
|
|
|
|
|
2024-08-27 20:24:08 +00:00
|
|
|
void Renderer::ProcessInput(GLFWwindow *window)
|
|
|
|
{
|
|
|
|
if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_2) == GLFW_PRESS)
|
|
|
|
{
|
|
|
|
double xPosIn, yPosIn;
|
|
|
|
glfwGetCursorPos(window, &xPosIn, &yPosIn);
|
|
|
|
|
|
|
|
auto xPos = static_cast<float>(xPosIn);
|
|
|
|
auto yPos = static_cast<float>(yPosIn);
|
|
|
|
|
|
|
|
if(firstMouse)
|
|
|
|
{
|
|
|
|
lastX = xPos;
|
|
|
|
lastY = yPos;
|
|
|
|
firstMouse = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float xOffset = xPos - lastX;
|
|
|
|
float yOffset = lastY - yPos;
|
|
|
|
|
|
|
|
lastX = xPos;
|
|
|
|
lastY = yPos;
|
|
|
|
|
|
|
|
s_Data.m_Camera->ProcessMouseMovement(xOffset, yOffset);
|
|
|
|
|
|
|
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
|
|
|
s_Data.m_Camera->ProcessKeyboard(FORWARD, deltaTime);
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
|
|
|
s_Data.m_Camera->ProcessKeyboard(BACKWARD, deltaTime);
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
|
|
|
s_Data.m_Camera->ProcessKeyboard(LEFT, deltaTime);
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
|
|
|
s_Data.m_Camera->ProcessKeyboard(RIGHT, deltaTime);
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
|
|
|
|
s_Data.m_Camera->ProcessKeyboard(DOWN, deltaTime);
|
|
|
|
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
|
|
|
|
s_Data.m_Camera->ProcessKeyboard(UP, deltaTime);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-08-01 23:32:51 +00:00
|
|
|
void Renderer::Shutdown()
|
|
|
|
{
|
|
|
|
s_Data.m_VAO->Shutdown();
|
|
|
|
s_Data.m_VBO->Shutdown();
|
|
|
|
s_Data.m_FBO->Shutdown();
|
2024-08-27 20:24:08 +00:00
|
|
|
s_Data.m_IBO->Shutdown();
|
2024-08-01 23:32:51 +00:00
|
|
|
s_Data.m_Shader->Shutdown();
|
|
|
|
}
|