Drawing pixel rectangle

I want to draw pixel rectangle (possibly clipped) to GL window. So I intended to use something like this:


UINT *Buffer;

void GLView::CaptureFrontBuffer()
{
glReadBuffer( GL_FRONT );
glReadPixels( 0, 0, nWndWidth, nWndHeight, GL_RGBA, GL_UNSIGNED_INT, Buffer );
}

void GLView::DrawStoredPixelRect()
{
glRasterPos( dXOrigin, dYOrigin );
glDrawPixels( nWndWidth, nWndHeight, GL_RGBA, GL_UNSIGNED_INT, Buffer );
}

But when origin point lies outside the view we don’t get clipped pixel rectangle. We get nothing at all because clipped points are rejected by glRasterPos.
It could be done using auxiliary buffer and glCopyPixels but it seems that M$ OpenGL implementation doesn’t support aux buffers :frowning:

Currently I use BitBlt (!) but this way is extremely buggy, machine dependent and ineffective.

How to draw clipped pixel rectangle?

[This message has been edited by Belgrad (edited 10-05-2002).]

[This message has been edited by Belgrad (edited 10-05-2002).]

[This message has been edited by Belgrad (edited 10-05-2002).]

Do the clipping yourself. If the raster position is outside the window, set it to the corner and draw only the part that is visible.

For example, if the raster position is located so that only the upper right quarter of the image is visible, set the raster position to the lower left corner of the window, and draw only the upper right quarter.

Bob
and draw only the upper right quarter
This is the problem itself. glDrawPixels can not draw from specified offset - only entire buffer.

Look up glPixelStore.

Just to give you a hint. What you’re supposed to look for is GL_UNPACK_ROW_LENGTH, GL_UNPACK_SKIP_ROWS and GL_UNPACK_SKIP_PIXELS.

Yes, that’s it! Thanks, Bob

Why is it so slow? Several times slower than BitBlt? Because of main memory <-> videomemory transfers? Is there any way how to retain buffer for glReadPixels/glDrawPixels within videomemory? Nowadays we have plenty of it so why to waste AGP bandwidth?

The most important thing is to make sure the picture’s format matches the format of the frame buffer. For example, if your picture is 32 bit, but the frame buffer is only 16 bit, every single pixels needs to be converted. To solve this, just convert your image into the same format as the frame buffer. Order of the components (RGB vs BGR) may affect performance also.

How to retrieve exact pixel format of the framebuffer?

You can use glGet* to get some information like bith depth and so. You also have to do a little research yourself, like visiting yor manucacturers site and see if they have some more information about optimization in the developer section. Maybe you find a document or two describing what pixel formats are used in what cases. Can be a little bit tricky, since the pixel format is generally hiddeb by OpenGL.

It also never hurts to try different combinations, right? The right pixel format generally increases the performance a bit compared to the wrong one.