copy buffer to std::vector

Started by
15 comments, last by rnlf_in_space 11 years, 7 months ago
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.
Advertisement
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.
Thanks for the suggestion. I tried this but the resulting vector is empty blink.png


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);
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.
Ok, added a

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.
When did you resize it? Before or after the glReadPixels() call?
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);
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.
Thanks very much for the clarification, didn't really got in first instance that the buffer had to be unbound.
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.

This topic is closed to new replies.

Advertisement