UBO bindg problem.. >_<

Hello, i’m trying to bind a UBO to a program without success…

I have this simple Vertex Shader:

#version 330

layout(std140) uniform MatriusGlobals
{
    mat4 ProjectionMatrix;
    mat4 LookAtMatrix;
};

in vec3 in_Position;
void main(void)
{
mat4 MVP = ProjectionMatrix * LookAtMatrix;
gl_Position = MVP * vec4(in_Position.x, in_Position.y, in_Position.z, 1.0);
}

I uploaded the two matrixs data in the UBO i created, but now i don’t know how to bind these two matrix to the uniforms inside the uniform block.

I have this code to make a Program:


 

m_program = new CGLProgram();
	       m_program->AttachShader("recursos/shaders/simple.vert",GL_VERTEX_SHADER);
	       m_program->Link();
	       m_program->AttachShader("recursos/shaders/simple.frag",GL_FRAGMENT_SHADER);
	       m_program->Link();

	     
	     
	     
	      
	       m_program->Bind_Attribute_Location(0,"in_Position");
	    
	       GLuint l_block_index = glGetUniformBlockIndex(m_program->m_ID,"MatriusGlobals");
//HERE I DON'T KNOW HOW TO BIND THE UNIFORM BUFFER WITH THE PROGRAM
	       glUniformBlockBinding(??????????????????????????????)
	     

	       m_program->Use();

Can anyone explain me how to use glGetUniformBlockIndex and glUniformBlockBinding ? Reading the spec = more mess in my head…

Thank you :slight_smile:

Read this.

Ok I read that. But I don’t understand it very well. I post the full code:


/*
THE SIMPLE VERTEX SHADER FIRST
*/
#version 330

layout(std140) uniform MatriusGlobals
{
    mat4 ProjectionMatrix;
    mat4 LookAtMatrix;
};

in vec3 in_Position;
void main(void)
{
	mat4 MVP = ProjectionMatrix * LookAtMatrix;
	gl_Position = MVP * vec4(in_Position.x, in_Position.y, in_Position.z, 1.0);
}




/*
*  GEN UBO BUFFER
*/
	glGenBuffers(1, &m_matrius_globals);
	glBindBuffer(GL_UNIFORM_BUFFER, m_matrius_globals);
	glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::mat4) * 2, NULL, GL_STREAM_DRAW);
	
	

//ADD DATA TO THE UNIFORM OBJECT (TWO 4x4 matrix)
    	glBindBuffer(GL_UNIFORM_BUFFER, m_matrius_globals);
	glm::mat4 l_matriu_projeccio=glm::perspective(45.0f, m_window_X / m_window_Y, 0.1f, 50.f);
  	glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::mat4),glm::value_ptr(l_matriu_projeccio));

 	glm::mat4 l_matriu_lookat = glm::lookAt(glm::vec3(0.0f,0.0f,10.0f),glm::vec3(0.0f,0.0f,0.0f),glm::vec3(0.0f,1.0f,0.0f));
   	glBufferSubData(GL_UNIFORM_BUFFER, sizeof(glm::mat4), sizeof(glm::mat4),glm::value_ptr(l_matriu_lookat));

	glBindBuffer(GL_UNIFORM_BUFFER, 0);

//...
//BLA BLA CODE...
//...

	
	//NOW I HAVE TO ASSIGN THE UBO TO A VBO ( HEREEEEEEEEEEE IS THE PROBLEM )
   	GLuint l_block_index = glGetUniformBlockIndex(m_program->m_ID,"MatriusGlobals");
	glUniformBlockBinding(m_program->m_ID,l_block_index,0);
	//I'M NOT SURE HERE: :S DON'T KNOW WHAT OR HOW TO DO IT 
	 glBindBufferRange(GL_UNIFORM_BUFFER,0,m_matrius_globals);




I dont know how i have to bind the uniforms that are inside the uniform block of the VBO to the UBO matrixs.

I’m stuck here :S.

BTW, Alfonse, i’m following your tutorial, but that part of the tutorial is a bit confuse ;). Maybe i’m too stupid ofc :smiley:

Thank you,

glBindBufferRange(GL_UNIFORM_BUFFER,0,m_matrius_globals);

This is not valid code, so I don’t know how it compiles. glBindBufferRange takes five parameters, not three. It needs the offset into the buffer object (in your case, 0), and the maximum size that this uniform block takes (in your case, sizeof(glm::mat4) * 2).

Ok Alfonse, I ended with this and is working very well.


 GLuint l_block_index = glGetUniformBlockIndex(m_program->m_ID,"MatriusGlobals");
	       glUniformBlockBinding(m_program->m_ID,l_block_index,0);
	       glBindBufferRange(GL_UNIFORM_BUFFER,0,m_matrius_globals,0 * sizeof(glm::mat4),2*sizeof(glm::mat4));


But this not answers my question at all. All I’m doing here is binding the entire UBO to the uniform block of a VBO. What i want to try is to bind only a part (a Matrix only) of the UBO to a uniform variable inside of the VBO uniform block.

Could you provide an example and which functions will I need? Thank you :slight_smile:

But this not answers my question at all. All I’m doing here is binding the entire UBO to the uniform block of a VBO. What i want to try is to bind only a part (a Matrix only) of the UBO to a uniform variable inside of the VBO uniform block.

First, a word on terminology.

There are Buffer Objects. These are GPU-allocated linear memory arrays that store arbitrary data.

“Vertex buffer object” is what you call a buffer object when you are using it to store vertex data. “Uniform buffer object” is what you call a buffer object when you use it to store uniform data. There’s nothing stopping you from using a buffer object to store both at the same time. And there’s certainly no such thing as a “VBO uniform block”.

Now that that’s out of the way, what you want is not possible. A uniform block has its data defined by a buffer object. That’s what it is for. You cannot have part of the data of a uniform block come from one buffer and part of it come from another. If you want to have one matrix come from one buffer object and the other matrix come from another, then you need to put them in two separate uniform blocks.