I have a C++ struct:
struct PointLight
{
glm::vec3 Position = glm::vec3(0.0f);
glm::vec3 Ambient = glm::vec3(0.25f);
glm::vec3 Diffuse = glm::vec3(1.0f);
glm::vec3 Specular = glm::vec3(1.0f);
};
And I want to send this struct to glsl struct That is set up as follows:
struct PointLight
{
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
So I create a UniformBuffer:
glGenBuffers(1, &ID);
glBindBuffer(GL_UNIFORM_BUFFER, ID);
glBufferData(GL_UNIFORM_BUFFER, sizeof(PointLight), NULL, GL_STATIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
glBindBufferBase(GL_UNIFORM_BUFFER, 1, ID);
Then I update it every frame:
glBindBuffer(GL_UNIFORM_BUFFER, ID);
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(PointLight), &light[0]); //First and only element PointLight in my lights vector
And Then I receive it inside shader like so:
layout (std140, binding = 1) uniform u_Lights
{
PointLight[NR_POINT_LIGHTS] u_PointLights;
};
NR_POINT_LIGHTS
is 1
The problem is that shader acts like the Specular is set to 0 so I checked RenderDoc and it turns out that shader Need 60 bytes and only 48 bytes are provided so I did something like this:
struct PointLight
{
glm::vec3 Position = glm::vec3(0.0f);
glm::vec3 Ambient = glm::vec3(0.25f);
glm::vec3 Diffuse = glm::vec3(1.0f);
glm::vec3 Specular = glm::vec3(1.0f, 1.0f, 1.0f);
glm::vec actualSpecular = glm::vec3(1.0f);
};
And then the value stored in actualSpecular
works inside the shader. How did that happend and why does glsl expect 60 bytes from me? And where did the Specular
disappear?
Solution: here's a link to answer that covers this problem: Should I ever use a `vec3` inside of a uniform buffer or shader storage buffer object?