diff --git a/BaseGLProject/DSGeometryPass.frag b/BaseGLProject/DSGeometryPass.frag index 1e11416..da17819 100644 --- a/BaseGLProject/DSGeometryPass.frag +++ b/BaseGLProject/DSGeometryPass.frag @@ -12,8 +12,6 @@ uniform sampler2D tex; void main() { gPosition = FragPos; - // - //gPosition = normalize(abs(FragPos)); gNormal = normalize(Normal); gAlbedoSpec.rgb = texture(tex, TexCoords).rgb; gAlbedoSpec.a = 1; diff --git a/BaseGLProject/DSLightPass.frag b/BaseGLProject/DSLightPass.frag index dbe8c31..75af3c1 100644 --- a/BaseGLProject/DSLightPass.frag +++ b/BaseGLProject/DSLightPass.frag @@ -29,12 +29,13 @@ void main() vec3 viewDir = normalize(view_pos - FragPos); for (int i = 0; i < NLights; ++i) { -// diffuse - vec3 lightDir = normalize(lights[i].Position - FragPos); - vec3 diffuse = max(dot(Normal, lightDir), 0.0) * Albedo * lights[i].Color; - // specular - vec3 halfwayDir = normalize(lightDir + viewDir); - float spec = pow(max(dot(Normal, halfwayDir), 0.0), 16.0); + vec3 L = normalize(lights[i].Position.xyz - FragPos); + vec3 V = normalize(view_pos - FragPos); + vec3 H = normalize(V + L); + + vec3 diffuse = max(dot(Normal, L), 0.0) * Albedo * lights[i].Color; + // specular + float spec = pow(max(dot(Normal, H), 0.0), 16.0); vec3 specular = lights[i].Color * spec * Specular; lighting += diffuse + specular; } diff --git a/BaseGLProject/Models/Mesh.cpp b/BaseGLProject/Models/Mesh.cpp index 8bfe5b7..c16c27e 100644 --- a/BaseGLProject/Models/Mesh.cpp +++ b/BaseGLProject/Models/Mesh.cpp @@ -370,15 +370,12 @@ void Mesh::draw(SceneContext &ctx) { // Setting the space matrixes uniques to the object ctx.modelMatrix = model.getMatrix(); ctx.mvpMatrix = ctx.projectionMatrix * ctx.viewMatrix * ctx.modelMatrix; - ctx.modelViewMatrix = ctx.viewMatrix * ctx.modelMatrix; + //ctx.modelViewMatrix = ctx.viewMatrix * ctx.modelMatrix; ctx.normalMatrix = glm::transpose(glm::inverse(glm::mat3(ctx.modelMatrix))); shader->addUniform("mvp", ctx.mvpMatrix); - shader->addUniform("NormalMatrix", ctx.normalMatrix); - if (ctx.renderMode == FORWARD_LIGHT) - shader->addUniform("ModelViewMatrix", ctx.modelViewMatrix); - else - shader->addUniform("ModelMatrix", ctx.modelMatrix); + shader->addUniform("NormalMatrix", ctx.normalMatrix); + shader->addUniform("ModelMatrix", ctx.modelMatrix); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures["0"].tex_ref); diff --git a/BaseGLProject/Multipass.cpp b/BaseGLProject/Multipass.cpp index c32afcd..53605b8 100644 --- a/BaseGLProject/Multipass.cpp +++ b/BaseGLProject/Multipass.cpp @@ -79,6 +79,9 @@ void Multipass::gBufferSetup(SceneContext &scnctx, std::map void Multipass::recomputeDeferredLights(SceneContext &scnctx) { + if (scnctx.renderMode != DEFERRED_LIGHT) + return; + int i = 0; for (auto light : scnctx.lights) { @@ -156,7 +159,7 @@ void Multipass::forwardLightSetup(SceneContext &scnctx, std::mapshader = std::make_shared("tex_base_light.vert", "tex_base_light.frag"); - mesh.second->shader->addUniform("tex", 0); + mesh.second->shader->addUniform("tex", 0); } scnctx.firstRedraw = false; diff --git a/BaseGLProject/MyGLWindow.cpp b/BaseGLProject/MyGLWindow.cpp index b002325..df28c04 100644 --- a/BaseGLProject/MyGLWindow.cpp +++ b/BaseGLProject/MyGLWindow.cpp @@ -249,7 +249,19 @@ void MyGlWindow::drawForwardLight() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); for (auto it = meshes.begin(); it != meshes.end(); it++) + { + (*it).second->shader->addUniform("view_pos", scnctx.camPos); + int i = 0; + for (auto light : scnctx.lights) + { + (*it).second->shader->addUniform("lights[" + std::to_string(i) + "].Position", glm::vec3(light.location)); + (*it).second->shader->addUniform("lights[" + std::to_string(i) + "].Color", light.intensity); + i++; + } + + (*it).second->shader->addUniform("NLights", (int)scnctx.lights.size()); (*it).second->draw(scnctx); + } } diff --git a/BaseGLProject/Source.cpp b/BaseGLProject/Source.cpp index a416f8a..07e5245 100644 --- a/BaseGLProject/Source.cpp +++ b/BaseGLProject/Source.cpp @@ -288,13 +288,13 @@ int loop(GLFWwindow *window) ImGui::ListBox("Lights", ¤t_light, strings, lights_strs.size()); if (ImGui::SliderFloat("Selected light X", &glWin.scnctx.lights[current_light].location.x, -10, 10) - && glWin.scnctx.renderMode != GBUF_DEBUG) + && glWin.scnctx.renderMode == DEFERRED_LIGHT) glWin.multipassManager.recomputeDeferredLights(glWin.scnctx); if (ImGui::SliderFloat("Selected light Y", &glWin.scnctx.lights[current_light].location.y, -10, 10) - && glWin.scnctx.renderMode != GBUF_DEBUG) + && glWin.scnctx.renderMode == DEFERRED_LIGHT) glWin.multipassManager.recomputeDeferredLights(glWin.scnctx); if (ImGui::SliderFloat("Selected light Z", &glWin.scnctx.lights[current_light].location.z, -10, 10) - && glWin.scnctx.renderMode != GBUF_DEBUG) + && glWin.scnctx.renderMode == DEFERRED_LIGHT) glWin.multipassManager.recomputeDeferredLights(glWin.scnctx); diff --git a/BaseGLProject/tex_base_light.frag b/BaseGLProject/tex_base_light.frag index c0c826b..11ca95b 100644 --- a/BaseGLProject/tex_base_light.frag +++ b/BaseGLProject/tex_base_light.frag @@ -3,26 +3,22 @@ in vec3 f_color; out vec4 FragColors; -uniform vec3 Kd; -uniform vec3 Ka; -uniform vec3 Ks; -uniform float Shininess; - struct Light { - vec4 Position; + vec3 Position; vec3 Color; }; uniform Light lights[64]; uniform int NLights; - in vec3 fNormal; in vec3 pos; in vec2 texCoord; uniform sampler2D tex; +uniform vec3 view_pos; + void main() { vec3 finalColor; @@ -30,21 +26,21 @@ void main() vec3 specular_sum; vec3 ambient; - ambient = Ka * lights[0].Color; + vec4 texColor = texture(tex, texCoord); + //Hardcoded ambient + ambient = texColor.rgb * 0.1; for (int i = 0; i < NLights; i++) { vec3 L = normalize(lights[i].Position.xyz - pos); vec3 N = fNormal; - vec3 V = normalize(-pos); + vec3 V = normalize(view_pos - pos); vec3 H = normalize(V + L); - vec3 diffuse = Kd * lights[i].Color * max(dot(L, N), 0.0); - vec3 specular = Ks * lights[i].Color * pow(max(dot(H, N), 0.0), Shininess); + vec3 diffuse = lights[i].Color * texColor.rgb * max(dot(L, N), 0.0); + vec3 specular = lights[i].Color * pow(max(dot(H, N), 0.0), 16.0) * 0.5; diffuse_sum += diffuse; specular_sum += specular; } - - vec4 texColor = texture(tex, texCoord); - FragColors = (vec4(diffuse_sum + ambient, 1) * texColor + vec4(specular_sum, 1.0)); - FragColors = texColor + (FragColors * 0.01); + + FragColors = (vec4(ambient + diffuse_sum + specular_sum, 1)); } diff --git a/BaseGLProject/tex_base_light.vert b/BaseGLProject/tex_base_light.vert index c0a4ac5..cc60215 100644 --- a/BaseGLProject/tex_base_light.vert +++ b/BaseGLProject/tex_base_light.vert @@ -7,7 +7,7 @@ layout(location=3) in vec2 v_texmap; uniform mat4 mvp; uniform mat3 NormalMatrix; -uniform mat4 ModelViewMatrix; +uniform mat4 ModelMatrix; out vec3 fNormal; out vec3 pos; @@ -16,9 +16,7 @@ out vec2 texCoord; void main(void) { fNormal = normalize(NormalMatrix * v_normal); - pos = (ModelViewMatrix * vec4(coord3d, 1.0)).xyz; - + pos = (ModelMatrix * vec4(coord3d, 1.0)).xyz; texCoord = v_texmap; - gl_Position = mvp * vec4(coord3d, 1.0f); }