Jump to content
  • Advertisement
Sign in to follow this  
Alessandro

copy buffer to std::vector

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!