Results 1 to 6 of 6

Thread: Changing data in Buffer Objects

  1. #1
    Member Newbie
    Join Date
    Nov 2011
    Location
    Fairborn, OH
    Posts
    31

    Changing data in Buffer Objects

    My current code structure generates a maze using GL_LINES. I'm putting the vertex and index data into vertex buffer objects (GL_ARRAY_BUFFER and GL_ELEMENT_ARRAY_BUFFER) during the initialization stage of my code.

    my glBufferData commands are currently set to GL_STATIC_DRAW.

    What I want to do is to have a keypress that lets me go back and reinitialize the entire maze. I have the code now that calls the initialize maze command on keypress, and it appears to work fine. I'm clearing the maze array, clearing the vertex array, and clearing the index array, and then regenerating all of that data (m_vertices.push_back, m_indices.push_back) and running the following lines again:

    Code :
    	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    	glEnableClientState(GL_VERTEX_ARRAY);
     
    	glGenBuffers(MAX_BUFFERS, &m_vbos[0]);
    	glBindBuffer(GL_ARRAY_BUFFER, m_vbos[VERTEX_BUFFER]); //Bind the vertex buffer
    	glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * m_vertices.size(), &m_vertices[0], GL_STATIC_DRAW); //Send the data to OpenGL
     
    	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vbos[INDEX_BUFFER]); //Bind the index buffer
    	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * m_indices.size(), &m_indices[0], GL_STATIC_DRAW); //Send the data to OpenGL
     
    	glBindBuffer(GL_ARRAY_BUFFER, m_vbos[VERTEX_BUFFER]);
    	glVertexPointer(3, GL_FLOAT, 0, 0);

    And then it renders the new data properly

    My question is...is it ok to just keep calling that block of code every time i want to regenerate? Or should I be destroying the buffers first and then recreating them with the above code? if I should be destroying them, what is the proper code and order to destroy or unallocate the buffers? I'm pretty new to this, so go easy

    Thanks!

  2. #2
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,161

    Re: Changing data in Buffer Objects

    is it ok to just keep calling that block of code every time i want to regenerate?
    There's no need to keep calling glGenBuffers.
    You can call glSubBufferData and fill the same buffer with new data, or if the buffer has changed in size, just use glbufferData instead.
    Or should I be destroying the buffers first
    You mean with glDeleteBuffers? No, no need to do that. Keep the same BufferID.

  3. #3
    Member Newbie
    Join Date
    Nov 2011
    Location
    Fairborn, OH
    Posts
    31

    Re: Changing data in Buffer Objects

    Thanks!

    so I'll need to separate all the buffer setup code from my maze generation/vertex/index code and on first run, call the bindbuffers and bufferdata...and then on all subsequent calls, only call the glbufferdata commands? Do the subsequent calls to glBufferData look the same or will the parameters need to change?

    Edit: To be more specific, will I also need to call the glEnableClientState and glVertexPointer commands also on the subsequent runs?

  4. #4
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,161

    Re: Changing data in Buffer Objects

    Yes you'll need to call glEnableClientState and glVertexPointer unless you are using VAOs.
    You'll need to separate the buffer creation from the buffer filling. The code for filling is exactly like you currently have.

  5. #5
    Member Newbie
    Join Date
    Nov 2011
    Location
    Fairborn, OH
    Posts
    31

    Re: Changing data in Buffer Objects

    Excellent...I think...Here's what I have that works:

    when I generate the maze and populate the m_vertices vector array, and then create the indices and populate the m_indices array...I call the above code, exactly as it is written.

    I created a new routine that ONLY calls the two glBufferData lines out of that block (lines 6 & 9 if you include spaces) the same way they're written. It appears to work, but I don't want to be creating any memory leaks or overflows after many different maze generations. It appears that I'm just re-buffering new data right?

    Given the code above, that's VAO's right?

    Thank you so much for the help...I'm learning a WHOLE lot here.

  6. #6
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,161

    Re: Changing data in Buffer Objects

    Given the code above, that's VAO's right?
    No, but that's what the VAO object encapsulates. Vertex Array Objects have to generated and bound, just like the buffer object.

Similar Threads

  1. Replies: 1
    Last Post: 12-28-2015, 02:18 AM
  2. Corrupted data during mapped buffer! (Uniform Buffer Objects)
    By FrankBoltzmann in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 06-24-2014, 03:15 AM
  3. Replies: 1
    Last Post: 11-05-2013, 08:50 AM
  4. Buffer Objects with Vertex-Array Data
    By Oragon in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 04-05-2012, 02:13 AM
  5. Changing LOD of polygonial objects
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-08-2002, 12:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean