2

I want to know how to restore all attribute binding as we unbound and rebound a program. As far as I know if I am using a VAO, it is as simple as rebounding the VAO, but I am not sure when I am not using the VAO. I am creating VBO but not VAO.

If I do the following

glUseProgram(shader1);

// Now set uniforms.
glUniformMatrix4fv(matrix_handdle, 1, false, matrix);

glBindBuffer(GL_ARRAY_BUFFER, bufferIndex);
glEnableVertexAttribArray(m_attributes[position_1]);

glVertexAttribPointer(m_attributes[POSITION_HANDLE], 3, GL_FLOAT, false, 3 * sizeof(GLfloat), 0);

Now save the current program, vbo binded. Then use second program

glUseProgram(shader2);
//bind some new  vbo, set some uniform, vertex attribute variable.
element.draw();

Then again use the first shader program. Rebind the vbo

glUseProgram(shader1); //Here, do the attributes set in first shader program remain?
element.draw();

I think I need to get the vertex attribute values using glGetVertexAttrib() function for size, stride , type, normalize flag, buffer binding and array pointer of all vertex attribute and save them. Then when I rebind the program, I will set the vertex attribute values. I think I need to get the Max attributes for a shader program by using GL_MAX_VERTEX_ATTRIBS and then save/ restore values of all attribute locations.Is it correct or I am wrong somewhere ?

Pankaj Bansal
  • 889
  • 1
  • 12
  • 37

2 Answers2

2

You use glVertexAttribPointer to define how to bind VBO data to shader calls. So what you can do is call glVertexAttribPointer one or more times, e. g. to define vertex positions, normals, and texture data, and then draw.

The point of a VAO is to save you the trouble of these multiple calls to glVertexAttribPointer. Instead of calling glVertexAttribPointer before drawing, you store the state in a VAO, and then you only have to call glBindVertexArray​ once before drawing. So the entire point of a VAO is that you don't have store the state yourself, you let the VAO handle it. There are many tutorials on how to do that.

As long as your shaders use the same input format, you can use multiple shaders with the same VAO.

On a side note: please try to ask one question at a time. Thank you. :)

Andreas Haferburg
  • 5,189
  • 3
  • 37
  • 63
  • My question was about situation when I am not using VAO. I am just using VBO. I think I need to get GL_MAX_VERTEX_ATTRIBS and store size, stride , type, normalize flag, buffer binding and array pointer of all vertex attribute and save. Then when I rebind the shader program, then again set the vertex attributes values using glVertexAttribPointer. Is it correct or I am wrong somewhere ? – Pankaj Bansal Jun 13 '16 at 07:01
  • 1
    But. Who's the one setting these attributes in the first place? I would think that *you* already have all the information somewhere in your program. Are you trying to build a library that implements VAOs? Why? There is one already, it's called OpenGL 3.0. ;) – Andreas Haferburg Jun 13 '16 at 07:07
  • These values will be set by the one using the library. Yes you are correct I am trying what VAO does for you. I need to do it if someone using opengl 2.0. I have updated my question. Please have a look. I – Pankaj Bansal Jun 13 '16 at 07:08
  • [This question](http://stackoverflow.com/questions/26552642/when-is-what-bound-to-a-vao) has some good answers. I would like to emphasize that you should read the OpenGL specs at https://www.opengl.org/registry/, especially if you're writing a library. – Andreas Haferburg Jun 13 '16 at 07:27
2

Now save the current program, vbo binded.

Program objects do not have buffers bound to them. Ever. The only state besides the compiled program that program objects have is uniform state.

And vertex arrays are not uniforms.

Buffers used for vertex data can be stored in a vertex array object (VAO). If you don't have access to VAOs, then you must reset this state every time you want to render with those buffers. That means you'll have to do the glBindBuffer, glEnableVertexAttribArray, and glVertexAttribPointer calls every time.

VAOs exist to avoid doing that. But if you don't have access to them, then you make due with what you have.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982