Move MV transform of lights from fragmentshader to cpu

This commit is contained in:
Peder Bergebakken Sundt 2019-03-15 17:29:59 +01:00
parent da7a29a2d1
commit 1b05c0ac82
2 changed files with 9 additions and 17 deletions

View File

@ -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;
}

View File

@ -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);