1

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?

Szahu
  • 43
  • 5
  • @Rabbid76: Please mark questions as duplicates when they are duplicates; don't just link to them. – Nicol Bolas Nov 17 '19 at 17:15
  • 2
    @Rabbid76: Yes. I wrote that question specifically to be a dupe-target for all questions involving the GLSL sizes involving vec3s that don't match their C/C++ apparent equivalents. The answer tells you exactly why the code is broken and exactly what you need to do to fix it. – Nicol Bolas Nov 17 '19 at 17:17
  • @NicolBolas should I delete this question then? – Szahu Nov 17 '19 at 17:19
  • @Szahu: You don't need to delete a question just because it's been asked before. Such questions act as useful signposts for people to find via search engines. – Nicol Bolas Nov 17 '19 at 17:20
  • @NicolBolas Okay then. Thanks for help, You got me covered as usual! – Szahu Nov 17 '19 at 17:25

0 Answers0