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.
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.
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.
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.
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:
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.