feat: shadow integration
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/ext/matrix_clip_space.hpp>
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
#include <glm/gtx/euler_angles.hpp>
|
||||
|
||||
#include "renderer/renderer.h"
|
||||
#include "window/window.h"
|
||||
@ -26,13 +27,23 @@ Renderer::Renderer()
|
||||
FileManager::read("./src/shaders/pbr.fs")
|
||||
);
|
||||
|
||||
m_depthShader.init(
|
||||
FileManager::read("./src/shaders/depth.vs"),
|
||||
FileManager::read("./src/shaders/depth.fs")
|
||||
);
|
||||
|
||||
m_model = glm::mat4(1.f);
|
||||
|
||||
m_shader.use();
|
||||
SwitchShader(&m_shader);
|
||||
|
||||
m_shader.setMat4("u_projection", m_proj);
|
||||
}
|
||||
|
||||
void Renderer::SwitchShader(Shader *newShader) {
|
||||
m_currentShader = newShader;
|
||||
m_currentShader->use();
|
||||
}
|
||||
|
||||
void Renderer::OnWindowResized(int w, int h) {
|
||||
m_proj = glm::perspective(
|
||||
static_cast<float>(M_PI_2),
|
||||
@ -41,17 +52,30 @@ void Renderer::OnWindowResized(int w, int h) {
|
||||
100.0f
|
||||
);
|
||||
m_shader.setMat4("u_projection", m_proj);
|
||||
m_depthShader.setMat4("u_projection", m_proj);
|
||||
}
|
||||
|
||||
void Renderer::Render(entt::registry& registry) {
|
||||
auto view = registry.view<transform, mesh>();
|
||||
void Renderer::ApplyLights(entt::registry& registry) {
|
||||
auto lights = registry.view<light>();
|
||||
// TODO: Pass Lights Data to depth shader as well
|
||||
m_shader.setInt("lightsCount", static_cast<int>(lights.size()));
|
||||
size_t lightIndex = 0;
|
||||
for (auto entity : lights) {
|
||||
auto &comp = registry.get<light>(entity);
|
||||
auto &transf = registry.get<transform>(entity);
|
||||
|
||||
m_shader.setVec3("lights[" + std::to_string(lightIndex) + "].position", transf.position);
|
||||
m_shader.setVec3("lights[" + std::to_string(lightIndex) + "].color", comp.color);
|
||||
m_shader.setFloat("lights[" + std::to_string(lightIndex) + "].intensity", comp.intensity);
|
||||
|
||||
++lightIndex;
|
||||
}
|
||||
}
|
||||
|
||||
void Renderer::UpdateView(entt::registry& registry) {
|
||||
auto cam = registry.view<transform, camera>().back();
|
||||
auto camTransform = registry.get<transform>(cam);
|
||||
|
||||
auto lightEntt = registry.view<transform, light>().back();
|
||||
auto lightTransform = registry.get<transform>(lightEntt);
|
||||
|
||||
m_view = glm::lookAt(
|
||||
camTransform.position,
|
||||
camTransform.position + camTransform.rotation,
|
||||
@ -59,15 +83,11 @@ void Renderer::Render(entt::registry& registry) {
|
||||
);
|
||||
m_shader.setMat4("u_view", m_view);
|
||||
|
||||
m_shader.setVec3("lightColor", glm::vec3(1.0f, 1.0f, 1.0f));
|
||||
m_shader.setVec3("lightPos", lightTransform.position);
|
||||
m_shader.setVec3("viewPos", camTransform.position);
|
||||
}
|
||||
|
||||
// std::cout << "cam pos: " << "vec(" << camTransform.position.x << ", " << camTransform.position.y << ", " << camTransform.position.z << ")" << std::endl;
|
||||
// std::cout << "cam rot: " << "vec(" << camTransform.rotation.x << ", " << camTransform.rotation.y << ", " << camTransform.rotation.z << ")" << std::endl;
|
||||
|
||||
// std::cout << "light pos: " << "vec(" << lightTransform.position.x << ", " << lightTransform.position.y << ", " << lightTransform.position.z << ")" << std::endl;
|
||||
// std::cout << "light rot: " << "vec(" << lightTransform.rotation.x << ", " << lightTransform.rotation.y << ", " << lightTransform.rotation.z << ")" << std::endl;
|
||||
void Renderer::RenderScene(entt::registry& registry) {
|
||||
auto view = registry.view<transform, mesh>();
|
||||
|
||||
for (auto [entity, transf, mesh] : view.each()) {
|
||||
if (mesh.object == nullptr) {
|
||||
@ -75,20 +95,95 @@ void Renderer::Render(entt::registry& registry) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_shader.setBool("isLight", registry.all_of<light>(entity));
|
||||
if (registry.all_of<light>(entity)) {
|
||||
auto &comp = registry.get<light>(entity);
|
||||
m_currentShader->setBool("isLight", true);
|
||||
m_currentShader->setVec3("currentLightColor", comp.color);
|
||||
} else {
|
||||
m_currentShader->setBool("isLight", false);
|
||||
m_currentShader->setVec3("currentLightColor", glm::vec3(0.f));
|
||||
}
|
||||
|
||||
m_model = glm::mat4(1.0f);
|
||||
glm::mat4 rotation = glm::yawPitchRoll(transf.rotation.y, transf.rotation.x, transf.rotation.z);
|
||||
m_model = glm::translate(glm::mat4(1.f), transf.position) * rotation;
|
||||
|
||||
// Apply translation
|
||||
m_model = glm::translate(m_model, transf.position);
|
||||
m_currentShader->setMat4("u_model", m_model);
|
||||
|
||||
// Apply rotations (order matters!)
|
||||
m_model = glm::rotate(m_model, transf.rotation.x, glm::vec3(1, 0, 0)); // pitch
|
||||
m_model = glm::rotate(m_model, transf.rotation.y, glm::vec3(0, 1, 0)); // yaw
|
||||
m_model = glm::rotate(m_model, transf.rotation.z, glm::vec3(0, 0, 1)); // roll
|
||||
|
||||
m_shader.setMat4("u_model", m_model);
|
||||
|
||||
mesh.object->Render(m_shader);
|
||||
mesh.object->Render(*m_currentShader);
|
||||
}
|
||||
}
|
||||
|
||||
void Renderer::GenerateShadowMaps(entt::registry& registry) {
|
||||
SwitchShader(&m_depthShader);
|
||||
|
||||
ApplyLights(registry);
|
||||
UpdateView(registry);
|
||||
|
||||
glGenFramebuffers(1, &m_depth_fbo);
|
||||
|
||||
const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024;
|
||||
|
||||
glGenTextures(1, &m_depthMap);
|
||||
glBindTexture(GL_TEXTURE_2D, m_depthMap);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24,
|
||||
SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
|
||||
|
||||
float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
|
||||
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, m_depth_fbo);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depthMap, 0);
|
||||
glDrawBuffer(GL_NONE);
|
||||
glReadBuffer(GL_NONE);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
|
||||
m_shader.setInt("shadowMap", 31);
|
||||
}
|
||||
|
||||
void Renderer::Render(entt::registry& registry) {
|
||||
const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024;
|
||||
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
auto shadowLight = registry.view<light, transform>().back();
|
||||
auto &comp = registry.get<transform>(shadowLight);
|
||||
|
||||
float near_plane = -10.0f, far_plane = 20.0f;
|
||||
glm::mat4 lightProjection = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, near_plane, far_plane);
|
||||
|
||||
glm::mat4 lightView = glm::lookAt(comp.position,
|
||||
glm::vec3( 0.0f, 0.0f, 0.0f),
|
||||
glm::vec3( 0.0f, 1.0f, 0.0f));
|
||||
|
||||
glm::mat4 lightSpaceMatrix = lightProjection * lightView;
|
||||
SwitchShader(&m_depthShader);
|
||||
m_currentShader->setMat4("u_lightSpace", lightSpaceMatrix);
|
||||
|
||||
glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, m_depth_fbo);
|
||||
glClear(GL_DEPTH_BUFFER_BIT);
|
||||
RenderScene(registry);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
|
||||
glViewport(0, 0, Window::GetWidth(), Window::GetHeight());
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
SwitchShader(&m_shader);
|
||||
|
||||
ApplyLights(registry);
|
||||
|
||||
UpdateView(registry);
|
||||
|
||||
m_currentShader->setMat4("u_lightSpace", lightSpaceMatrix);
|
||||
|
||||
glActiveTexture(GL_TEXTURE31);
|
||||
glBindTexture(GL_TEXTURE_2D, m_depthMap);
|
||||
|
||||
RenderScene(registry);
|
||||
}
|
Reference in New Issue
Block a user