title: OpenGL Copy Vertex Buffer Object
body: <p>I'm attempting to copy two vertex buffer objects from one <code>Mesh</code> object to another, through the copy assignment operator. Initially, my Vertex Array Object and the Buffers are initialized as follows:</p>
  
  <pre><code>void Mesh::construct(Vertex* vertices, unsigned int nVerts) {
  
      vertexCount = nVerts;
  
      glGenVertexArrays(1, &amp;vao);
      glBindVertexArray(vao);
  
      std::vector&lt;glm::vec3&gt; positions;
      std::vector&lt;glm::vec2&gt; texCoords;
  
      positions.reserve(nVerts);
      texCoords.reserve(nVerts);
  
      for (unsigned int i = 0; i &lt; nVerts; i++) {
          positions.push_back(vertices[i].getPosition());
          texCoords.push_back(vertices[i].getTexCoord());
      }
  
      for (int i = 0; i &lt; NUM_BUFFERS; i++) {
          glGenBuffers(1, &amp;vab[i]);
      }
  
      glBindBuffer(GL_ARRAY_BUFFER, vab[POSITION_VB]);
      glBufferData(GL_ARRAY_BUFFER, nVerts * sizeof (positions[0]), &amp;positions[0], GL_STATIC_DRAW);
  
      glBindBuffer(GL_ARRAY_BUFFER, vab[TEXCOORD_VB]);
      glBufferData(GL_ARRAY_BUFFER, nVerts * sizeof (texCoords[0]), &amp;texCoords[0], GL_STATIC_DRAW);
  
      glEnableVertexAttribArray(1);
      glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
  
      glEnableVertexAttribArray(0);
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
  
      glBindBuffer(GL_ARRAY_BUFFER, 0);
      glBindVertexArray(0);
  
  }
  </code></pre>
  
  <p>This works fine when instantiating a <code>Mesh</code> object, and calling:</p>
  
  <pre><code>void Mesh::render() {
  
      glBindVertexArray(vao);
  
      glDrawArrays(GL_TRIANGLES, 0, vertexCount);
  
      glBindVertexArray(0);
  
  }
  </code></pre>
  
  <p>However, when I try to copy the mesh into another, and render it, I get a segmentation fault on the <code>glDrawArrays(GL_TRIANGLES, 0, vertexCount);</code> line.. This is my copy assignment operator:</p>
  
  <pre><code>Mesh&amp; Mesh::operator=(const Mesh&amp; param) {
  
      if (this == &amp;param) {
          return *this;
      } else {
  
          GLint size = 0;
  
          glGenVertexArrays(1, &amp;vao);
          glBindVertexArray(vao);
  
          for (int i = 0; i &lt; NUM_BUFFERS; i++) {
              glGenBuffers(1, &amp;vab[i]);
          }
  
          // Vertices
          // Bind Buffers
          glBindBuffer(GL_COPY_READ_BUFFER, param.vab[POSITION_VB]);
          glGetBufferParameteriv(GL_COPY_READ_BUFFER, GL_BUFFER_SIZE, &amp;size);
  
          glBindBuffer(GL_COPY_WRITE_BUFFER, vab[POSITION_VB]);
          glBufferData(GL_COPY_WRITE_BUFFER, size, nullptr, GL_STATIC_DRAW);
  
          glEnableVertexAttribArray(0);
          glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
  
          // Copy Data
          glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, size);
  
          // Texture Coords
          // Bind Buffers
          glBindBuffer(GL_COPY_READ_BUFFER, param.vab[TEXCOORD_VB]);
          glGetBufferParameteriv(GL_COPY_READ_BUFFER, GL_BUFFER_SIZE, &amp;size);
  
          glBindBuffer(GL_COPY_WRITE_BUFFER, vab[TEXCOORD_VB]);
          glBufferData(GL_COPY_WRITE_BUFFER, size, nullptr, GL_STATIC_DRAW);
  
          glEnableVertexAttribArray(1);
          glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
  
          // Copy Data
          glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, size);
  
          // Unbind buffers
          glBindVertexArray(0);
  
          this-&gt;vertexCount = param.vertexCount;
  
          return *this;
      }
  
  }
  </code></pre>
  
  <p>Can anyone see any problems with this? I've checked that the size being returned from <code>glGetBufferParameteriv(GL_COPY_READ_BUFFER, GL_BUFFER_SIZE, &amp;size);</code> is correct in both cases (for both position and texture coordinate buffer). I've also checked <code>glGetError()</code> after both calls to <code>glCopyBufferSubData</code>, which both return 0. Not sure what to try next? My error may be elsewhere, but this is the first time I have tried copying buffers, so want to check that I'm doing that part right. If it helps, my <code>Mesh</code> destructor is: </p>
  
  <pre><code>Mesh::~Mesh() {
  
      glDeleteVertexArrays(1, &amp;vao);
      glDeleteBuffers(NUM_BUFFERS, vab);
  
  }
  </code></pre>
  
  <p>Through a debugger I can see that this is, of course, being called once, after the line:</p>
  
  <pre><code>this-&gt;mesh = Mesh(*texture);
  </code></pre>
  
  <p>Which is simply constructing a mesh, then assigning it (the texture just sizes the quad to the size of the texture, and calls the constructor shown at the start with the correct vertex positions).</p>