Answer the question
In order to leave comments, you need to log in
How to properly use uniform buffers?
Hello everyone, I'm having a problem using uniform buffers.
I have 3 objects that are drawn using three shader programs
It seems that only from two shader programs I get uniform blocks (although in the shader programs themselves these uniform blocks are indicated by a different name)
unsigned int FirstProgram = glGetUniformBlockIndex(shaders.ShaderPrograms[0]->mProgram, "Matrices");//достаю номер юниформ блока в первой шейдерной программе
unsigned int SecondProgram = glGetUniformBlockIndex(shaders.ShaderPrograms[1]->mProgram, "Matrices");//достаю номер юниформ во второй шейдерной программе
glUniformBlockBinding(shaders.ShaderPrograms[0]->mProgram, FirstProgram, 0);
glUniformBlockBinding(shaders.ShaderPrograms[1]->mProgram, SecondProgram, 0);
unsigned int uboMatrices;
glGenBuffers(1, &uboMatrices);
glBindBuffer(GL_UNIFORM_BUFFER, uboMatrices);
glBufferData(GL_UNIFORM_BUFFER, 2 * sizeof(glm::mat4), NULL, GL_STATIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
glBindBufferRange(GL_UNIFORM_BUFFER, 0, uboMatrices, 0, 2 * sizeof(glm::mat4));
glBindBuffer(GL_UNIFORM_BUFFER, uboMatrices);
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::mat4), glm::value_ptr(proj));
glBindBuffer(GL_UNIFORM_BUFFER, 0);
#version 330
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 uv;
layout (location = 2) in vec3 normals;
layout (std140) uniform Matrices1
{
mat4 projectionMatrix;
mat4 ViewMatrix;
};
out vec3 Normal;
out vec3 FragPos;
out vec2 TexCoord;
uniform mat4 ModelMatrix;
void main()
{
gl_Position = projectionMatrix * ViewMatrix * ModelMatrix * vec4(position, 1.0f);
FragPos = vec3(ModelMatrix * vec4(position, 1.0f));
Normal = mat3(transpose(inverse(ModelMatrix))) * normals;
TexCoord = uv;
}
#version 330
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 uv;
layout (location = 2) in vec3 normals;
layout (std140) uniform Matrices2
{
mat4 projectionMatrix;
mat4 ViewMatrix;
};
out vec3 Normal;
out vec3 FragPos;
out vec2 TexCoord;
uniform mat4 ModelMatrix;
void main()
{
gl_Position = projectionMatrix * ViewMatrix * ModelMatrix * vec4(position, 1.0f);
FragPos = vec3(ModelMatrix * vec4(position, 1.0f));
Normal = mat3(transpose(inverse(ModelMatrix))) * normals;
TexCoord = uv;
#version 330
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 uv;
layout (location = 2) in vec3 normals;
layout (std140) uniform Matrices3
{
mat4 projectionMatrix;
mat4 ViewMatrix;
};
out vec3 Normal;
out vec3 FragPos;
out vec2 TexCoord;
uniform mat4 ModelMatrix;
void main()
{
gl_Position = projectionMatrix * ViewMatrix * ModelMatrix * vec4(position, 1.0f);
FragPos = vec3(ModelMatrix * vec4(position, 1.0f));
Normal = mat3(transpose(inverse(ModelMatrix))) * normals;
TexCoord = uv;
}
Answer the question
In order to leave comments, you need to log in
I already figured it out, it turns out this is due to the fact that all uniform blocks in shaders are by default associated with the anchor point 0
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question