-1
$\begingroup$

After binding the data to the vertex buffer object as follows, how to modify the buffer data?

(Each subdata size is 3$*$ data size (three elements per index)

glm::vec3 vertex;
vertex.push_back(glm::vec3(0.0f, 0.0f, 0.0f));
vertex.push_back(glm::vec3(0.0f, 0.0f, 1.0f));
vertex.push_back(glm::vec3(1.0f, 0.0f, 0.0f));

glm::vec3 color;
color.push_back(glm::vec3(1.0f, 0.0f, 0.0f));
color.push_back(glm::vec3(0.0f, 1.0f, 0.0f));
color.push_back(glm::vec3(0.0f, 0.0f, 1.0f));

glBufferData(GL_ATTRAY_BUFFER, vertex_data_size+color_data_size, NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, vertex_data_size, data);
glBufferSubData(GL_ARRAY_BUFFER, vertex_data_size, color_data_size, data);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)vertex_data_size);

In detail, supposing I want to change the second vertex element to (1.0f, 1.0f, 1.0f). I know it with compute shader, but I just wnat to know how to do it without compute shader or OpenCl.

Is it possible to change the buffer data uploaed directly without uploading the whole data again?

$\endgroup$
1
$\begingroup$

Is it possible to change the buffer data uploaed directly without uploading the whole data again?

"Again"? You didn't upload "the whole data" the first time:

glBufferSubData(GL_ARRAY_BUFFER, 0, vertex_data_size, data);

That only affects the first vertex_data_size bytes of the buffer. And your second one only affects the data on the byte range [vertex_data_size, color_data_size + vertex_data_size).

There's no limit on how many bytes you can modify with a glBufferSubData call. Or more to the point, there's no limit on how few bytes you can modify. If you want to upload 12 bytes worth of data, you can.

Now, you shouldn't. You should avoid making lots of small uploads of data like that.

If you have access to persistent mapping, it would be better to persistently map the buffer and simply modify the location(s) at the appropriate time (using proper synchronization and multiple buffering).

$\endgroup$
  • $\begingroup$ I know what you mean. But my question is how to modify the data. You said " You should avoid making lots of small uploads of data like that." Of course, we should avoid, but it's just a simple example to describe my question in detail. $\endgroup$ – shashack Feb 3 '18 at 20:22
  • $\begingroup$ @shashack: "But my question is how to modify the data." But you already know how to modify the data. That's how you put it there in the first place. $\endgroup$ – Nicol Bolas Feb 3 '18 at 20:23
  • $\begingroup$ Ok what if the number of vertex size is 100000000 and I just need to modify just 1 vertex in the buffer data. Do I need to bind the buffer and upload data again? It would take so much time. $\endgroup$ – shashack Feb 3 '18 at 20:26
  • $\begingroup$ @shashack: Then only upload what you need. Just like your two glBufferSubData calls each only uploaded part of the total buffer size. There's no limit on how much or how little data you can upload. I don't know how to make "If you want to upload 12 bytes worth of data, you can." more clear without literally writing the code for you. $\endgroup$ – Nicol Bolas Feb 3 '18 at 20:27
  • $\begingroup$ Oh so you mean I can call glBufferSubData super many time? For example, If I have vertex 100000000 size, then 100000000 time? $\endgroup$ – shashack Feb 3 '18 at 20:33

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.