Query regarding glCopyBufferSubData()

I am trying to copy data within same buffer using glCopyBufferSubData().

array[24];

glCopyBufferSubData(GL_ARRAY_BUFFER,GL_ARRAY_BUFFER,sizeof(array)/2,0,sizeof(array)) ;

glVertexAttribPointer(index,3, GL_FLOAT, GL_FALSE, 0,&array[12]);

glDrawElements();

Above code doesn’t seem to work.

Any suggestions ?

Ok, let’s take this apart. First of all, what’s array[24] supposed to hold? Are there any concrete values? If you don’t specify any the values are undefined. Second, what’s the type of the array elements?

glCopyBufferSubData(GL_ARRAY_BUFFER,GL_ARRAY_BUFFE  R,sizeof(array)/2,0,sizeof(array)) ;

Do you honestly need to determine the offsets and size of the copy operation by deducing from a fixed size array? How can you be sure that sizeof(array) is actually a valid size and that sizeof(array)/2 + sizeof(array) isn’t out of range? Equivalent code, which is much more readable, would be:


GLintptr bufferSize = numElements * sizeof(elementType);
GLintptr readOffset = bufferSize >> 1;

glCopyBufferSubData(GL_ARRAY_BUFFER, GL_ARRAY_BUFFER, readOffset, 0, bufferSize) ;

I suppose you simply have a buffer of 24 elements and want to copy the second half to the first half. Is that correct?

Regarding the second call, do you want legacy vertex arrays or do you want to render using vertex buffer objects?

Please clarify your intent.

[QUOTE=thokra;1243269]Ok, let’s take this apart. First of all, what’s array[24] supposed to hold? Are there any concrete values? If you don’t specify any the values are undefined. Second, what’s the type of the array elements?

glCopyBufferSubData(GL_ARRAY_BUFFER,GL_ARRAY_BUFFE  R,sizeof(array)/2,0,sizeof(array)) ;

Do you honestly need to determine the offsets and size of the copy operation by deducing from a fixed size array? How can you be sure that sizeof(array) is actually a valid size and that sizeof(array)/2 + sizeof(array) isn’t out of range? Equivalent code, which is much more readable, would be:


GLintptr bufferSize = numElements * sizeof(elementType);
GLintptr readOffset = bufferSize >> 1;

glCopyBufferSubData(GL_ARRAY_BUFFER, GL_ARRAY_BUFFER, readOffset, 0, bufferSize) ;

I suppose you simply have a buffer of 24 elements and want to copy the second half to the first half. Is that correct?

Regarding the second call, do you want legacy vertex arrays or do you want to render using vertex buffer objects?

Please clarify your intent.[/QUOTE]

  1. The values in the array are concrete for sure.

  2. The values are all within limits. I have done proper error checking in actual code

  3. I wish to render using VBO only , but if the data is in second half ( glCopyBufferSubData will be slightly different in that case ) , nothing is rendered. Either glCopyBufferSubData() fails ( surprisingly no error on NVidia driver ) or glVertexAttribPointer() fails.

Well, think about what &array[12] means as an offset into the vertex buffer’s data store. The pointer returned by &array[12] is an address to a stack allocated memory location. What you need is a valid byte offset. For instance, if you store 24 float values and you need to start at the second half of the buffer, your offset would simply be (const GLvoid*)(12 * sizeof(GLfloat)) and not &array[12].

You got it right man :slight_smile:
Huge mistake !
Thanks for the reply

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.