Added basic shadows to point lights; Better than directional shadows

This commit is contained in:
grayson committed 2021-09-13 23:45:42 -05:00
1 parent 4b14362a04
commit 1a8320feab
21 files changed
+250 -71

No files matched your search

+3 -2
View File
@@ -57,7 +57,8 @@ namespace nf {
m_vao->push<float>(2);
m_vao->finishBufferLayout();
Application::getApp()->getCurrentState()->m_nfObjects.push_back(this);
if (!Application::getApp()->getCurrentState()->isRunning())
Application::getApp()->getCurrentState()->m_nfObjects.push_back(this);
}
const char* Button::identity() {
@@ -131,6 +132,6 @@ namespace nf {
}
Button::~Button() {
destroy();
}
}
@@ -114,7 +114,8 @@ namespace nf {
m_vao->push<float>(3);
m_vao->finishBufferLayout();
Application::getApp()->getCurrentState()->m_nfObjects.push_back(this);
if (!Application::getApp()->getCurrentState()->isRunning())
Application::getApp()->getCurrentState()->m_nfObjects.push_back(this);
}
bool Cubemap::isConstructed() {
@@ -140,6 +141,6 @@ namespace nf {
}
Cubemap::~Cubemap() {
destroy();
}
}
+4 -3
View File
@@ -31,7 +31,8 @@ namespace nf {
model->loadedModel = m_model;
}
Application::getApp()->getCurrentState()->m_nfObjects.push_back(this);
if (!Application::getApp()->getCurrentState()->isRunning())
Application::getApp()->getCurrentState()->m_nfObjects.push_back(this);
}
bool Entity::isConstructed() {
@@ -88,7 +89,7 @@ namespace nf {
void Entity::destroy() {
m_constructed = false;
if(m_model && !m_model->isBaseAsset())
if (m_model && !m_model->isBaseAsset())
delete m_model;
m_model = nullptr;
m_position = Vec3(0.0);
@@ -97,6 +98,6 @@ namespace nf {
}
Entity::~Entity() {
destroy();
}
}
+3 -2
View File
@@ -19,7 +19,8 @@ namespace nf {
m_type = type;
m_strength = (float)strength;
Application::getApp()->getCurrentState()->m_nfObjects.push_back(this);
if (!Application::getApp()->getCurrentState()->isRunning())
Application::getApp()->getCurrentState()->m_nfObjects.push_back(this);
}
bool Light::isConstructed() {
@@ -88,6 +89,6 @@ namespace nf {
}
Light::~Light() {
destroy();
}
}
+4 -3
View File
@@ -68,7 +68,8 @@ namespace nf {
m_string = "Loading...";
}
else {
Application::getApp()->getCurrentState()->m_nfObjects.push_back(this);
if (!Application::getApp()->getCurrentState()->isRunning())
Application::getApp()->getCurrentState()->m_nfObjects.push_back(this);
}
}
@@ -95,7 +96,7 @@ namespace nf {
void Text::render(Shader* shader, unsigned int windowWidth, unsigned int windowHeight, bool onButton, float buttonWidth, float buttonHeight, const Vec2& buttonPos) {
float scale = windowWidth / 4000.0f;
if (onButton)
scale *= buttonHeight / 100.0f;
scale *= buttonWidth / 400.0f;
float currX = (float)m_position.x * windowWidth, currY = (float)m_position.y * windowHeight;
std::string::const_iterator si;
if (m_centeredX || m_centeredY) {
@@ -177,6 +178,6 @@ namespace nf {
}
Text::~Text() {
destroy();
}
}
@@ -47,7 +47,8 @@ namespace nf {
m_vao->push<float>(2);
m_vao->finishBufferLayout();
Application::getApp()->getCurrentState()->m_nfObjects.push_back(this);
if (!Application::getApp()->getCurrentState()->isRunning())
Application::getApp()->getCurrentState()->m_nfObjects.push_back(this);
}
const char* UITexture::identity() {
@@ -100,6 +101,6 @@ namespace nf {
}
UITexture::~UITexture() {
destroy();
}
}
+92 -30
View File
@@ -18,6 +18,8 @@
namespace nf {
Renderer::Renderer(Application* app) :
m_shadowMapFBO(0),
m_directionalDepthTexSize(0),
m_pointDepthTexSize(0),
m_cubemap(nullptr),
m_fadeIn(false),
m_fadeOut(false),
@@ -71,6 +73,8 @@ namespace nf {
loadBaseAssets();
m_directionalDepthTexSize = 4096;
m_pointDepthTexSize = 1024;
createShadowMap();
if (!m_app->isCustomWindowIcon()) {
@@ -176,7 +180,7 @@ namespace nf {
currLightsDrawn = lightsRemaining;
lightsRemaining -= currLightsDrawn;
currLight += (int)currLightsDrawn;
m_entityShader->setUniform("numberOfLights", (int)currLightsDrawn + 1);
m_entityShader->setUniform("numberOfLights", (int)currLightsDrawn);
for (unsigned int j = 0; j < currLightsDrawn; j++) {
m_lights[j + (unsigned int)currLight]->bind(m_entityShader, j);
}
@@ -269,35 +273,78 @@ namespace nf {
}
void Renderer::renderShadowMaps(unsigned int startingLight, unsigned int count) {
glViewport(0, 0, 4096, 4096);
float nearP = 0.1f, farP = 500.0f;
glm::mat4 lightProj = glm::ortho(-50.0f, 50.0f, -50.0f, 50.0f, nearP, farP);
float nearP = 0.1f, farP = 400.0f;
glm::mat4 directionalLightProj = glm::ortho(-50.0f, 50.0f, -50.0f, 50.0f, nearP, farP);
glm::mat4 pointLightProj = glm::perspective(glm::radians(90.0f), 1.0f, nearP, farP);
glm::mat4 lightView;
glm::mat4 lightSpaceMat;
glBindFramebuffer(GL_FRAMEBUFFER, m_shadowMapFBO);
for (unsigned int i = 0; i < count; i++) {
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_shadowMaps[i], 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glClear(GL_DEPTH_BUFFER_BIT);
Vec3 posTemp = m_lights[startingLight + i]->getPosition();
glm::vec3 posTemp2(posTemp.x, posTemp.y, posTemp.z);
lightView = glm::lookAt(posTemp2, glm::vec3(0.0), glm::vec3(0.0, 1.0, 0.0));
lightSpaceMat = lightProj * lightView;
m_directionalShadowShader->setUniform("lightSpace", lightSpaceMat);
std::string stringPos = "lightSpaceMat[";
stringPos += std::to_string(i);
stringPos += "]";
m_entityShader->setUniform(stringPos, lightSpaceMat);
for (Entity* curr : m_lGame) {
curr->render(m_directionalShadowShader, true);
Light::Type type = m_lights[i]->getType();
unsigned int tex = type == Light::Type::DIRECTIONAL ? m_directionalShadowMaps[i] : m_pointShadowMaps[i];
switch (type) {
case Light::Type::DIRECTIONAL: {
glViewport(0, 0, m_directionalDepthTexSize, m_directionalDepthTexSize);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, tex, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glClear(GL_DEPTH_BUFFER_BIT);
Vec3 posTemp = m_lights[startingLight + i]->getPosition();
glm::vec3 lightPos(posTemp.x, posTemp.y, posTemp.z);
lightView = glm::lookAt(lightPos, glm::vec3(0.0), glm::vec3(0.0, 1.0, 0.0));
lightSpaceMat = directionalLightProj * lightView;
m_directionalShadowShader->setUniform("lightSpace", lightSpaceMat);
std::string stringPos = "lightSpaceMat[";
stringPos += std::to_string(i);
stringPos += "]";
m_entityShader->setUniform(stringPos, lightSpaceMat);
for (Entity* curr : m_lGame) {
curr->render(m_directionalShadowShader, true);
}
stringPos = "light[";
stringPos += std::to_string(i);
stringPos += "].directionalDepthTex";
glActiveTexture(GL_TEXTURE3 + i);
glBindTexture(GL_TEXTURE_2D, tex);
m_entityShader->setUniform(stringPos, 3 + (int)i);
break;
}
case Light::Type::POINT: {
glViewport(0, 0, m_pointDepthTexSize, m_pointDepthTexSize);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, tex, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glClear(GL_DEPTH_BUFFER_BIT);
Vec3 posTemp = m_lights[startingLight + i]->getPosition();
glm::vec3 lightPos(posTemp.x, posTemp.y, posTemp.z);
std::vector<glm::mat4> lightSpaceMats;
lightSpaceMats.push_back(pointLightProj * glm::lookAt(lightPos, lightPos + glm::vec3(1.0, 0.0, 0.0), glm::vec3(0.0, -1.0, 0.0)));
lightSpaceMats.push_back(pointLightProj * glm::lookAt(lightPos, lightPos + glm::vec3(-1.0, 0.0, 0.0), glm::vec3(0.0, -1.0, 0.0)));
lightSpaceMats.push_back(pointLightProj * glm::lookAt(lightPos, lightPos + glm::vec3(0.0, 1.0, 0.0), glm::vec3(0.0, 0.0, 1.0)));
lightSpaceMats.push_back(pointLightProj * glm::lookAt(lightPos, lightPos + glm::vec3(0.0, -1.0, 0.0), glm::vec3(0.0, 0.0, -1.0)));
lightSpaceMats.push_back(pointLightProj * glm::lookAt(lightPos, lightPos + glm::vec3(0.0, 0.0, 1.0), glm::vec3(0.0, -1.0, 0.0)));
lightSpaceMats.push_back(pointLightProj * glm::lookAt(lightPos, lightPos + glm::vec3(0.0, 0.0, -1.0), glm::vec3(0.0, -1.0, 0.0)));
for (int j = 0; j < 6; j++) {
std::string stringPos = "lightSpaceMat[";
stringPos += std::to_string(j);
stringPos += "]";
m_pointShadowShader->setUniform(stringPos, lightSpaceMats[j]);
}
m_pointShadowShader->setUniform("farPlane", farP);
m_pointShadowShader->setUniform("lightPos", lightPos);
for (Entity* curr : m_lGame) {
curr->render(m_pointShadowShader, true);
}
std::string stringPos = "light[";
stringPos += std::to_string(i);
stringPos += "].pointDepthTex";
glActiveTexture(GL_TEXTURE3 + i);
glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
m_entityShader->setUniform(stringPos, 3 + (int)i);
m_entityShader->setUniform("farPlane", farP);
break;
}
}
stringPos = "light[";
stringPos += std::to_string(i);
stringPos += "].depthTex";
glActiveTexture(GL_TEXTURE3 + i);
glBindTexture(GL_TEXTURE_2D, m_shadowMaps[i]);
m_entityShader->setUniform(stringPos, 3 + (int)i);
}
m_entityShader->setUniform("numMats", (int)count);
}
@@ -322,6 +369,10 @@ namespace nf {
const char* directionalShadowVertex = m_baseAP["directionalShadowVertex.shader"]->data;
const char* directionalShadowFragment = m_baseAP["directionalShadowFragment.shader"]->data;
m_directionalShadowShader = new Shader(directionalShadowVertex, directionalShadowFragment);
const char* pointShadowVertex = m_baseAP["pointShadowVertex.shader"]->data;
const char* pointShadowGeometry = m_baseAP["pointShadowGeometry.shader"]->data;
const char* pointShadowFragment = m_baseAP["pointShadowFragment.shader"]->data;
m_pointShadowShader = new Shader(pointShadowVertex, pointShadowFragment, pointShadowGeometry);
BaseAssets::cube = (AModel*)m_baseAP["cube.obj"];
BaseAssets::plane = (AModel*)m_baseAP["plane.obj"];
@@ -340,17 +391,28 @@ namespace nf {
glGenFramebuffers(1, &m_shadowMapFBO);
glBindFramebuffer(GL_FRAMEBUFFER, m_shadowMapFBO);
for (unsigned int i = 0; i < m_texSlots; i++) {
unsigned int depthMap;
glGenTextures(1, &depthMap);
glBindTexture(GL_TEXTURE_2D, depthMap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 4096, 4096, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
unsigned int directionalDepthMap, pointDepthMap;
glGenTextures(1, &directionalDepthMap);
glGenTextures(1, &pointDepthMap);
glBindTexture(GL_TEXTURE_2D, directionalDepthMap);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT24, m_directionalDepthTexSize, m_directionalDepthTexSize);
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);
m_shadowMaps.push_back(depthMap);
glBindTexture(GL_TEXTURE_2D, 0);
glBindTexture(GL_TEXTURE_CUBE_MAP, pointDepthMap);
glTexStorage2D(GL_TEXTURE_CUBE_MAP, 1, GL_DEPTH_COMPONENT24, m_pointDepthTexSize, m_pointDepthTexSize);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
m_directionalShadowMaps.push_back(directionalDepthMap);
m_pointShadowMaps.push_back(pointDepthMap);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
+24 -3
View File
@@ -5,16 +5,30 @@
#include "Utility.h"
namespace nf {
Shader::Shader(const char* vertexSource, const char* fragmentSource) {
Shader::Shader(const char* vertexSource, const char* fragmentSource, const char* geometrySource) {
m_id = glCreateProgram();
unsigned int vs = glCreateShader(GL_VERTEX_SHADER);
unsigned int fs = glCreateShader(GL_FRAGMENT_SHADER);
unsigned int gs = 0;
glShaderSource(vs, 1, &vertexSource, nullptr);
glShaderSource(fs, 1, &fragmentSource, nullptr);
glCompileShader(vs);
glCompileShader(fs);
for (int i = 0; i < 2; i++) {
unsigned int curr = (i == 0 ? vs : fs);
if (geometrySource) {
gs = glCreateShader(GL_GEOMETRY_SHADER);
glShaderSource(gs, 1, &geometrySource, nullptr);
glCompileShader(gs);
}
for (int i = 0; i < 3; i++) {
unsigned int curr;
if (i == 0)
curr = vs;
else if (i == 1)
curr = fs;
else if (i == 2)
curr = gs;
if (curr == 0)
break;
int result;
glGetShaderiv(curr, GL_COMPILE_STATUS, &result);
if (result != GL_TRUE) {
@@ -28,6 +42,7 @@ namespace nf {
}
glAttachShader(m_id, vs);
glAttachShader(m_id, fs);
if (gs) glAttachShader(m_id, gs);
glLinkProgram(m_id);
glValidateProgram(m_id);
int result;
@@ -39,8 +54,14 @@ namespace nf {
glGetProgramInfoLog(m_id, length, &length, message);
Error("OpenGL Error: " + (std::string)message);
}
glDetachShader(m_id, vs);
glDetachShader(m_id, fs);
glDeleteShader(vs);
glDeleteShader(fs);
if (gs) {
glDetachShader(m_id, gs);
glDeleteShader(gs);
}
}
void Shader::bind() {