# copy buffer to std::vector

This topic is 2219 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In my opengl application I read the framebuffer using glReadPixels and store it into a buffer like this:

 .......................... glReadBuffer(GL_COLOR_ATTACHMENT0_EXT); glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboId); glPixelStorei(GL_PACK_ALIGNMENT, 1); glReadPixels(0, 0, screenWidth, screenHeight, GL_RGB, GL_UNSIGNED_BYTE, 0); GLubyte *screenColorBuffer = (GLubyte*)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY_ARB); .......................... 

After that I loop and store buffer values into a std::vector<GLubyte>.
I'd like to ask if there is actually a clever solution like copying the whole buffer in the vector at once, without using a loop.
Thanks in advance. Edited by Alessandro

##### Share on other sites
Well instead of binding a buffer before calling glReadPixels() you could make sure that your vector is big enough for the data and pass &vec[0] as the last argument of glReadPixels(). That will make OpenGL store the data in the vector in the first place.

##### Share on other sites
Thanks for the suggestion. I tried this but the resulting vector is empty

 std::vector<GLubyte> myBuffer; // declare vector myBuffer.reserve(size*size*3); // reserve size for it glReadBuffer(GL_COLOR_ATTACHMENT0_EXT); glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboId); glPixelStorei(GL_PACK_ALIGNMENT, 1); glReadPixels(0, 0, size1,size2, GL_RGB, GL_UNSIGNED_BYTE, &myBuffer[0]); glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB); // release pointer to the mapped buffer glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0); 

##### Share on other sites
reserve() only sets the reserved size, it doesn't actually set the vector size. You need to actually make the vector the right size, not just reserve space. One way is to specify the desired size as part of the constructor call. Another is to use resize() after the vector is created.

##### Share on other sites
 myBuffer.resize(size1*size2*3); 

Now the vector has the proper size but it's filled with 0's, so I take the glReadPixels isn't yet storing values there.

##### Share on other sites
When did you resize it? Before or after the glReadPixels() call?

##### Share on other sites
Before. Here is my code:

 std::vector<GLubyte>myBuffer; myBuffer.resize(size1*size2*3); glReadBuffer(GL_COLOR_ATTACHMENT0_EXT); glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboId); glPixelStorei(GL_PACK_ALIGNMENT, 1); glReadPixels(0, 0, size1,size2, GL_RGB, GL_UNSIGNED_BYTE, &myBuffer[0] ); glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB); // release pointer to the mapped buffer glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0); 

##### Share on other sites
I just noticed that you're still binding a buffer. As I said before, you would specify the memory address of the vector instead of binding a buffer. glReadPixels() will copy the pixel information to the bound buffer if one is bound, using the data argument as an offset into that buffer. In order for it to treat the data argument as a destination address, there needs to be no buffer bound.

##### Share on other sites
Thanks very much for the clarification, didn't really got in first instance that the buffer had to be unbound.

##### Share on other sites
I don't think, a vector is the right tool for this job. I had exactly the same problem a few weeks ago and figured I could not rely on the compiler to optimize away the initialization of the vector elements with zeros, which would be actually a big performance hit, if this code was to run more than a few times per minute or so.

I recommend you use a std::unique_ptr (or boost::shared_ptr if you can't use C++11) to a manually created array like this:

 std::unique_ptr<GLubyte[]> myBuffer(new GLubyte[size1*size2*3]); glReadPixels(...., myBuffer.get()); 

This way, you get the benefits you would also get from a vector (which very much boils down to automatic cleanup) but you don't get the drawbacks (unneccessary initialization of the memory buffer. With a vector, you might end up doubling the time needed to read your pixels...

I know, using vector&co for everything is an advice you hear very often, but sometimes you have to know when to use something different. Edited by rnlf

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
13

• 14
• 10
• 25
• 9
• 57
• ### Forum Statistics

• Total Topics
632644
• Total Posts
3007625
• ### Who's Online (See full list)

There are no registered users currently online

×