memyself
memyself

Reputation: 12628

updating multiple line coordinates with one glSubBuffer call?

I use the following code to render a line, given the coordinates in data:

create and upload data:

nPoints = 3 # let's use three points as an example, in reality this would be 1000
data = [x1y1 x2y2 x3y3]

# upload data
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, mode)

draw the line:

# draw line
stride = 0
offset_to_first_vertex = 0
glVertexPointer(2, GL_FLOAT, stride, offset_to_first_vertex)

offset_to_first_vertex = 0    
glDrawArrays(GL_LINE_STRIP, offset_to_first_vertex, nPoints)

update y coordinate of 2nd point:

offset = 12
glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(new_data), new_data)

things become more complicated if I want to update the y coordinate of multiple points at the same time, e.g. of the 2nd and 3rd point:

offset = 12
glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(new_data), new_data)
offset = 20
glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(new_data), new_data)

Calling glBufferSubData is very expensive so I'd rather prefer to have one call and overwrite all values at the same time. Unfortunately, you can't pass a stride value to glBufferSubData, which means that I can only overwrite a continuous part of memory.

I was thinking that maybe I need to re-organize my data in memory differently:

data = [x1x2x3y1y2y3]

This way I can overwrite multiple values with one call of glBufferSubData. However, if I do this, then I can't render the points with glVertexPointer and glDrawArrays anymore.

Therefore my question: What is the best way of updating multiple single coordinates of my line? How should I layout the data in memory? and how can I draw the line?

EDIT1: I could probably keep track of the x values and update multiple points by overwriting the corresponding x/y pairs. But I'd like to prevent this if possible.

EDIT2: is there a way to render a line two VBOs? let's say one VBO keeps the x values and another one has the y values. It doesn't seem like that's possible.

Upvotes: 1

Views: 361

Answers (1)

Tim
Tim

Reputation: 35933

Can you use shaders? I guess if you want you could have the x and y position as separate attributes, and then combine them in the shader (you could even put x and y in separate buffers).

If in the fixed pipeline, you could check out glMapBuffer, which allows you to map the entire buffer to your local client. You can then update any of the values in any way that you want. I don't know what the impact of performance will be for this.

Upvotes: 1

Related Questions