0

Firstly, everything works fine when I pass one lightPos

struct UniformBufferObject {
    alignas(16) glm::mat4 model;
    alignas(16) glm::mat4 view;
    alignas(16) glm::mat4 proj;
    alignas(16) glm::vec3 lightColor;
    alignas(16) glm::vec3 viewPos;
    alignas(16) glm::vec3 lightPos;
};

but when I change the lightPos to array

struct UniformBufferObject {
    alignas(16) glm::mat4 model;
    alignas(16) glm::mat4 view;
    alignas(16) glm::mat4 proj;
    alignas(16) glm::vec3 lightColor;
    alignas(16) glm::vec3 viewPos;
    alignas(32) glm::vec3 lightPos[2];
};

I think I meet some alignment issue. I pass the same value to lightPos0 and lightPos1, but the values I get in frag shader are strange

glm::vec3 defaultLightPos1 = {0.0f, 0.0f, 1.0f};
UniformBufferObject ubo{};
ubo.lightPos[0] = defaultLightPos1;
ubo.lightPos[1] = defaultLightPos1;

//frag shader
layout(binding = 0) uniform UniformBufferObject {
    mat4 model;
    mat4 view;
    mat4 proj;
    vec3 lightColor;
    vec3 viewPos;
    vec3 lightPos[2];
} ubo;

void main() {
    outColor = vec4(ubo.lightPos[0/1], 1.0);
}

when the

outColor = vec4(ubo.lightPos[0], 1.0);

I get

enter image description here

when the

outColor = vec4(ubo.lightPos[1], 1.0);

I get

enter image description here

the result don't change when I change the

alignas(32) glm::vec3 lightPos[2] 

to

alignas(16) glm::vec3 lightPos[2]
dreakzts
  • 11
  • 3
  • GLSL UB array elements of type `vec3` are aligned to the size of `vec4`. However the elements of `glm::vec3 lightPos[2];` are aligned to 4 bytes. – Rabbid76 Jul 01 '21 at 10:08
  • @Rabbid76, thanks for replying. You mean I should change to alignas(4) glm::vec3 lightPos[2]? – dreakzts Jul 01 '21 at 11:27
  • when I use alignas(4) glm::vec3 lightPos[2], lightPos[0] would get green cube, lightPos[1] would get red cube – dreakzts Jul 01 '21 at 11:28
  • No, you should use `glm::vec4` instead of `glm::vec3` – Rabbid76 Jul 01 '21 at 11:50
  • @Rabbid76, thanks! That works, but would you mind give more details about the reason? uniform vec3 lightPos works fine in OpenGL, https://learnopengl.com/PBR/Lighting https://learnopengl.com/code_viewer_gh.php?code=src/6.pbr/1.1.lighting/1.1.pbr.fs – dreakzts Jul 01 '21 at 12:03
  • If you have an array of `vec3`, each element takes the size of `vec4` – Rabbid76 Jul 01 '21 at 12:30
  • Get it, thanks so much! – dreakzts Jul 01 '21 at 12:39

0 Answers0