1
votes

I've got an extremely simple fragment shader:

#version 330 core

// Interpolated values from the vertex shaders
in vec3 positionW;

// Output data
out vec3 frag_color;

uniform vec3 alsoLightW;

void main(void) {
    vec3 temp = normalize(alsoLightW);
    frag_color = vec3((temp.x + 1.0) / 2, (temp.y + 1.0) / 2, (temp.z + 1.0) / 2);
}

All it does is normalize a uniform vec3 I've passed to it, and then set my fragment color to it (between 0 and 1 - so a 0 vector would have a color of 0.5, 0.5, 0.5). I print this vector out in my c++ code, and verify that it is passed in properly with renderdoc, however the actual colors that it spits out are consistently wrong - they don't seem to have anything to do with my uniform, but it's always giving me the same color, regardless. For instance, my vec3 is supposed to have a y-coordinate of 0 so I expect a Green value of 0.5, but according to renderdoc, I'm instead getting something around .2

What could be the reason for this? It's been driving me crazy, and is making my much more complicated shader not work at all.

Edit: passing uniform to OpenGL:

ctx.shader->Enable();

...

glm::vec3 oppOffset = *Object::globalOffset;
oppOffset *= -1;

glUniform3fv(lightLocation, 1, glm::value_ptr(oppOffset));

glm::vec3 norm = glm::normalize(oppOffset);
//Prints the correct information
std::cout << (norm.x + 1) / 2 << "," << (norm.y + 1) / 2 << "," << (norm.z + 1) / 2 << std::endl;

Edit2: I tried passing them as individual floats, and I'm getting the same exact weird numbers as before. I've uploaded an image of what I mean - the bottom left numbers are the ones I'm getting from cout, and the top right are the ones that RenderDoc are giving me

This is what my uniform passing looks like now:

glm::vec3 norm = glm::normalize(oppOffset);
norm = (norm + glm::vec3(1.0, 1.0, 1.0)) * glm::vec3(0.5, 0.5, 0.5);
float x, y, z;
x = norm.x;
y = norm.y;
z = norm.z;
ctx.shader->uniform1fv("lightx", 1, &x);
ctx.shader->uniform1fv("lighty", 1, &y);
ctx.shader->uniform1fv("lightz", 1, &z);
std::cout << x << "," << y << "," << z << std::endl;

And my shader:

#version 330 core

// Interpolated values from the vertex shaders
in vec3 positionW;

// Output data
out vec3 frag_color;

uniform float lightx;
uniform float lighty;
uniform float lightz;

void main(void) {
    //vec3 temp = normalize(alsoLightW);
    frag_color = vec3(lightx, lighty, lightz);
}

Something very weird is happening...

1
Can You post the code that actually passes the value of alsoLightW to the shader?Matso
I updated the post with the informationOtto von Bisquick
And how is lightLocation obtained? Is the shader bound at when it happens?lisyarus
It's also worth saying that I can pass this same vector to a different fragment shader in a different rendering phase, and it works just fine. I don't change the vector at all in between.Otto von Bisquick
I use ctx.shader->getUniformLocation("alsoLightW"), which is just a wrapper for glGetUniformLocationOtto von Bisquick

1 Answers

1
votes

The problem is not with the uniform (or passing the uniform) but with the format of the renderbuffer. The buffer has a SRGB format meaning that it will not store linear colors but gamma corrected ones.

All your results will be determined by this equation:

color_srgb = pow(color_rgb, 2.2);