ggambetta
ggambetta

Reputation: 3443

Get depth buffer from QGLPixelBuffer

I'm using OpenGL in a QT application. At some point I'm rendering to a QGLPixelBuffer. I need to get the depth buffer of the image, what I'd normally accomplish with glReadPixels(..., GL_DEPTH_COMPONENT, ...); I tried making the QGLPixelBuffer current and then using glReadPixels() but all I get is a white image.

Here's my code

bufferCanvas->makeCurrent();
[ ...render... ]
QImage snapshot(QSize(_lastWidth, _lastHeight), QImage::Format_Indexed8);
glReadPixels(0, 0, _lastWidth, _lastHeight, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, snapshot.bits());
snapshot.save("depth.bmp");

Anything obviously wrong with it?

Upvotes: 0

Views: 1602

Answers (3)

Ariya Hidayat
Ariya Hidayat

Reputation: 12561

Well, there is no guarantee that the underlying pixel data stored in QImage (and obtained via its QImage::bits() function) is compatible to what OpenGL's glReadPixels() function writes.

Since you are using QGLPixelBuffer, what is wrong with QGLPixelBuffer::toImage() ?

Upvotes: 1

codelogic
codelogic

Reputation: 73752

Wild guess follows.

You're creating an indexed bitmap using QImage, however you're not assinging a color table. My guess is that the default color table is making your image appear white. Try this before saving your image:

for ( int i = 0 ; i <= 255 ; i++ ) {
    snapshot.setColor( i, qRGB( i, i, i ) );
}

Upvotes: 0

Ketan
Ketan

Reputation: 1015

I have never used QImage directly, but I would try to answer or look into following areas:

  1. Are you calling glClear() with Depth bit before reading image?
  2. Does your QGLFormat has depth buffer enabled?
  3. Can you dump readPixel directly and verify whether it has correct data?
  4. Does QImage::bits() ensure sequential memory store with required alignment?

Hope this helps

Upvotes: 0

Related Questions