From 1b05c0ac8201d7021c1e1b97662c240ca6926415 Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Fri, 15 Mar 2019 17:29:59 +0100 Subject: [PATCH] Move MV transform of lights from fragmentshader to cpu --- res/shaders/simple.frag | 16 +++++----------- src/gamelogic.cpp | 10 ++++------ 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/res/shaders/simple.frag b/res/shaders/simple.frag index 1500020..f571640 100644 --- a/res/shaders/simple.frag +++ b/res/shaders/simple.frag @@ -18,11 +18,10 @@ uniform bool isNormalMapped; uniform bool isInverted; // lights -struct Light { +struct Light { // coordinates in MV space vec3 position; - mat4 MV; + vec3 spot_target; bool is_spot; // false means point light - vec3 spot_target; // MV space coordinates for spots }; //named @@ -53,13 +52,13 @@ void main_(vec4 basecolor) { float specular_intensity = 0.0; for (int i = 0; i<3; i++) { - vec3 L = vec3(light[i].MV * vec4(light[i].position, 1.0f)) - vertex; + vec3 L = light[i].position - vertex; float l = length(L); float attenuation = clamp(2000/(1 + 1*l + 0.1*l*l), 0.0, 1.25); L = normalize(L); if (light[i].is_spot) { - vec3 L2 = normalize(vec3(light[i].MV * vec4(light[i].position, 1.0f)) - light[i].spot_target); + vec3 L2 = normalize(light[i].position - light[i].spot_target); if (dot(L2, L) < spot_cuttof_angle) { continue; } @@ -101,11 +100,6 @@ void main() { } } else { color = texture(diffuseTexture, UV); - - } - if (isInverted) { - color.r = 1 - color.r; - color.g = 1 - color.g; - color.b = 1 - color.b; } + if (isInverted) color.rgb = 1 - color.rgb; } diff --git a/src/gamelogic.cpp b/src/gamelogic.cpp index 9d3794c..dca9d63 100644 --- a/src/gamelogic.cpp +++ b/src/gamelogic.cpp @@ -395,14 +395,13 @@ void updateFrame(GLFWwindow* window) { void renderNode(SceneNode* node) { struct Light { // lights as stored in the shader - glm::vec3 position; - glm::mat4 MV; + // coordinates in MV space + vec3 position; + vec3 spot_target; bool is_spot; - glm::vec3 spot_target; // MV space coordinates void push_to_shader(Gloom::Shader* shader, uint id) { #define l(x) shader->location("light[" + std::to_string(id) + "]." + #x) - glUniformMatrix4fv(l(MV) , 1, GL_FALSE, glm::value_ptr(MV)); glUniform3fv (l(position) , 1, glm::value_ptr(position)); glUniform3fv (l(spot_target), 1, glm::value_ptr(spot_target)); glUniform1i (l(is_spot) , is_spot); @@ -449,8 +448,7 @@ void renderNode(SceneNode* node) { case SPOT_LIGHT: case POINT_LIGHT: { uint id = node->lightID; - lights[id].position = node->position; - lights[id].MV = node->MV; + lights[id].position = vec3(node->MV * glm::vec4(node->position, 1.0)); lights[id].is_spot = node->nodeType == SPOT_LIGHT; lights[id].spot_target = node->rotation; lights[id].push_to_shader(s, id);