I am trying to use unified buffers, but it does not work as intended. I have two identical buffers, one is light and the other is material. The problem is that the colors are not what they should be, and they change every time I move the camera. This problem did not exist when I used the regular form. Here are the photos to show what I mean: When using uniform buffers and when using regular uniforms !
This is my fragment shader:
#version 400 // Fragment Shader uniform layout(std140); in vec3 EyePosition; in vec3 EyeNormal; in vec2 TexCoord; out vec4 FragColor; uniform sampler2D Texture; uniform LightBlock { vec4 Position; vec4 Intensity; } Light; uniform MaterialBlock { vec4 Ambient; vec4 Diffuse; } Material; vec4 PointLight(in int i, in vec3 ECPosition, in vec3 ECNormal) { vec3 n = normalize(ECNormal); vec3 s = normalize(Light.Position.xyz - ECPosition); return Light.Intensity * (Material.Ambient + Material.Diffuse * max(dot(s, n), 0.0)); } void main() { FragColor = texture(Texture, TexCoord); FragColor *= PointLight(0, EyePosition, EyeNormal); }
I'm not sure I did everything right, but here is how I create uniform buffers:
glGenBuffers(1, &light_buffer); glGenBuffers(1, &material_buffer); glBindBuffer(GL_UNIFORM_BUFFER, light_buffer); glBufferData(GL_UNIFORM_BUFFER, sizeof(LightBlock), nullptr, GL_DYNAMIC_DRAW); glBindBuffer(GL_UNIFORM_BUFFER, material_buffer); glBufferData(GL_UNIFORM_BUFFER, sizeof(MaterialBlock), nullptr, GL_DYNAMIC_DRAW); GLuint program = Shaders.GetProgram(); light_index = glGetUniformBlockIndex(program, "LightBlock"); material_index = glGetUniformBlockIndex(program, "MaterialBlock"); glUniformBlockBinding(program, light_index, 0); glUniformBlockBinding(program, material_index, 1); glBindBufferBase(GL_UNIFORM_BUFFER, 0, light_buffer); glBindBufferBase(GL_UNIFORM_BUFFER, 1, material_buffer);
EDIT: Here, as I fill the buffers: