• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Alessandro

copy buffer to std::vector

16 posts in this topic

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

[code]
..........................
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);
..........................
[/code]

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
0

Share this post


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

Share this post


Link to post
Share on other sites
Thanks for the suggestion. I tried this but the resulting vector is empty [img]http://public.gamedev.net//public/style_emoticons/default/blink.png[/img]

[code]
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);
[/code]
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.
1

Share this post


Link to post
Share on other sites
Ok, added a
[code]
myBuffer.resize(size1*size2*3);
[/code]

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

Share this post


Link to post
Share on other sites
When did you resize it? Before or after the glReadPixels() call?
0

Share this post


Link to post
Share on other sites
Before. Here is my code:

[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);
[/code]
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 [b]instead[/b] 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.
2

Share this post


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

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:

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

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
0

Share this post


Link to post
Share on other sites
It depends on what the OP is using it for. For something like a screenshot, I wouldn't bother trying to optimise too heavily, the code is run so rarely relative to other parts of the system. If the code is run more frequently, the bigger performance problem is probably the copying of data from the GPU and the associated pipeline stall.
0

Share this post


Link to post
Share on other sites
Why bother? Where is it more work or less comfort to use a smart-pointer to an array instead of using a vector? It has nothing but advantages (you just cannot use .at() but who does anyways?)
0

Share this post


Link to post
Share on other sites
[quote name='rnlf' timestamp='1346064346' post='4973729']
Why bother? Where is it more work or less comfort to use a smart-pointer to an array instead of using a vector? It has nothing but advantages (you just cannot use .at() but who does anyways?)
[/quote]
You mean using a simple (smart) pointer has nothing but advantages over a data store that tracks its size automatically, can be easily resized, and can be optimized by the compiler when used with most standard algorithms to produce better code? You're probably right, except for all those places where you're wrong.

Certainly, if you want a raw chunk of memory for a fixed-length byte buffer it makes just as much sense to use a (smart) pointer and ::operator new (or, if you're stuck in the 1970's its grandfather, malloc()) as to use a std::vector. The minute you start doing anything more complicated than that, you're generally better off using an appropriate higher-level data structure.
0

Share this post


Link to post
Share on other sites
Bregma, yes. You're absolutely right. My comment is just for this situation (read a fixed-size buffer from a C library, do something like writing it to a file and free the buffer), where it is most unlikely that resizing or your off-the-shelf-C++-standard-library-algorithms are required. But how often do you sort() or erase_if() your pixel buffer data? It is most likely that the data will either be written directly to an image file or processed for post-effects. In that case there is really no advantage in using a full-fledged container.

I never said using vectors for anything but this general use case was a bad idea. I didn't say so, because this would in fact be seriously stupid. Edited by rnlf
0

Share this post


Link to post
Share on other sites
Code doesn't exist in isolation, the OP may already have code for dealing with the vector. This might need to be modified to account for changing to a smart array.

The main general drawback is that the vector remembers its size, whereas the smart array does not. This probably is not an issue here, as the vector's size is likely insufficient anyway - it is actually a two dimensional array being hidden in a one dimensional vector.
0

Share this post


Link to post
Share on other sites
For the necessities of my application using a std::vector to store pixels is just fine. I need to grab those pixels each time that the camera orientation/position (managed by the mouse) has changed. And so, each time that the mouse button is released the function runs, grab the pixels into a vector, sort and remove duplicate values (I use it later to do opengl picking based on colors). The operation takes less than 100ms, and is almost unnoticeable.
Interesting the use of the boost library, as I have some time I will try that as well, as I get some spare time...
0

Share this post


Link to post
Share on other sites
Wow... I didn't think my examples of standard algorithms that I thought nobody would like to use with pixel data are just what you want to do with your pixel data. I withdraw my objections from a few post up ;-)

But the algorithms do of course also work with arrays. ;-)
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0